simple_window.anubis
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
The Anubis Project.
A Widget System (4th version).
The 'simple window' widget.
Copyright (c) Alain Proute' 2005.
Authors: Alain Prouté
Olivier Duvernois
read tools/basis.anubis
read widget.anubis
read tools.anubis
This file defines simple 'nude' windows. If you need more sophisticated windows (with
boundary and/or automatic scrollbars and/or resizer) see 'window.anubis'.
A 'simple window' is a rectangular area which is able to show part of another widget
(its 'content') through itself:
+......................................+
: | content :
: | :
: y_scroll :
: | :
: V :
:-- x_scroll -->+-------------+ :
: | window | :
: | | :
: | | :
: | | :
: | | :
: | | :
: +-------------+ :
: :
: :
+......................................+
In other words, the content is invisible except through the window.
The window has a position (x_scroll,y_scroll) relative to its content, which may change
dynamically. This position should not be confused with the position of the window
relative to its parent widget or relative to the host window. Also, the size of the
window may change dynamically.
The size of a window must eventually be coordinated with the size of another window. It
may be changed from outside the window, the position of the content may also be changed
or read from outside the window, for example by scrollbars. For these reasons, these
dimensions are stored in variables which must be provided at the creation of the simple
window.
A simple window widget is created by:
public define Widget
simple_window
(
Var(Word32) width_v, // width of window
Var(Word32) height_v, // height of window
Var(Word32) x_scroll_v, // position of window relative to its content
Var(Word32) y_scroll_v,
Var(Word32) content_width_v, // the window updates these variables when
Var(Word32) content_height_v, // the size of the content changes
Widget content,
One -> RGB background_color
).
The variables 'content_width_v' and 'content_height_v' may have any initial values (for
example 0). They are kept up to date by the simple window widget.
--- That's all for the public part ! --------------------------------------------------
*** [] Tools.
*** [] Putting bounds on scrolling.
If (cont_w,cont_h) is the size of the content, and (width,height) the size of the
window, we must always have:
0 =< x_scroll =< sup(0,cont_w - width)
0 =< y_scroll =< sup(0,cont_h - height)
In other words, 'x_scroll' is 0 when the window is wider than its content.
define Word32
bound_scroll
(
Word32 new_value,
Word32 content_size, // may be either width or height
Word32 window_size // idem
) =
max(0,min(new_value,content_size-window_size)).
*** [] The draw method.
We must draw the content at relative position (i.e. relative to the simple window
widget):
(-x_scroll,-y_scroll)
and the drawing must be clipped by the rectangle of the window itself.
define WidgetDrawToolBox -> One
draw_method
(
Var(Word32) width_v,
Var(Word32) height_v,
Var(Word32) x_scroll_v,
Var(Word32) y_scroll_v,
Widget content,
One -> RGB bgcolor
) =
(WidgetDrawToolBox dtb) |->
draw(dtb)(rect(0,0,*width_v,*height_v),bgcolor(unique));
draw(dtb)(content,
-*x_scroll_v,-*y_scroll_v, // position of content relative to window
rect(0,0,*width_v,*height_v) // rectangle of window
).
*** [] Handling events.
*** [] Handling left click events.
The next function is applied to the answer returned by the content whenever the event
was a left down mouse click.
define WidgetAnswer
handle_left_click_answer
(
WidgetEventToolBox etb,
Var(Word32) width_v,
Var(Word32) height_v,
Var(Word32) x_scroll_v,
Var(Word32) y_scroll_v,
WidgetAnswer a,
Var(One) capture_ticket,
Var(Word32) content_width_v,
Var(Word32) content_height_v,
Widget content
) =
if a is
{
not_handled(ar) then
want_to_capture_mouse(capture_ticket,compress,ar),
handled(area) then a,
resized then
if size(content) is (w,h) then
content_width_v <- w;
content_height_v <- h;
x_scroll_v <- bound_scroll(*x_scroll_v,w,*width_v);
y_scroll_v <- bound_scroll(*y_scroll_v,h,*height_v);
handled(area(etb)([rect(0,0,*width_v,*height_v)])),
ignored then
want_to_capture_mouse(capture_ticket,compress,area(etb)([])),
want_to_capture_mouse(_,_,_) then a,
want_to_capture_keyboard(_,_) then a
}.
*** [] Handling 'changed' events.
When the window receives a 'changed' event, must first determine its own needs.
1. Check if one of 'width_v' or 'height_v' and in this case consider that the
window must be resized.
2. Check if one of 'x_scroll_v' or 'y_scroll_v' has changed, and in this case
consider that the window (and its content) must be redrawn.
type NeedsOnChanged:
none, // the window has no need
resize, // the window must be resized (hence also redrawn)
redraw. // the window must be redrawn
Compute the needs of our window:
define NeedsOnChanged
needs
(
List(Word32) ids,
Var(Word32) width_v,
Var(Word32) height_v,
Var(Word32) x_scroll_v,
Var(Word32) y_scroll_v
) =
if member(ids,var_id(width_v)) then resize else // this will also redraw
if member(ids,var_id(height_v)) then resize else // this will also redraw
if member(ids,var_id(x_scroll_v)) then redraw else
if member(ids,var_id(y_scroll_v)) then redraw else
none.
Now, when a 'changed' event is received, this event must also be transmited to the
content, and this gives an answer. How, this answer is transformed depends on the needs
of the window.
define WidgetAnswer
transform
(
WidgetEventToolBox etb,
NeedsOnChanged n,
WidgetAnswer a, // answer returned by content
Var(Word32) width_v,
Var(Word32) height_v,
Var(Word32) x_scroll_v,
Var(Word32) y_scroll_v,
Widget content
) =
if n is
{
none then a,
resize then if size(content) is (content_width,content_height) then
x_scroll_v <- bound_scroll(*x_scroll_v,content_width,*width_v);
y_scroll_v <- bound_scroll(*y_scroll_v,content_height,*height_v);
resized,
redraw then handled(area(etb)[rect(0,0,*width_v,*height_v)])
}.
*** [] The simple window event handler.
define (WidgetEventToolBox etb, WidgetEvent e) -> WidgetAnswer
event_handler
(
Var(Word32) width_v,
Var(Word32) height_v,
Var(Word32) x_scroll_v,
Var(Word32) y_scroll_v,
Var(Word32) content_width_v,
Var(Word32) content_height_v,
Var(Word32) pmx_v, // previous mouse position (if any)
Var(Word32) pmy_v,
Widget content,
Var(One) capture_ticket
) =
(WidgetEventToolBox etb, WidgetEvent e) |->
with answer =
if e is
{
quit then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
mouse_move(ks,x,y) then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
mouse_click(ks,mc,x,y) then
if mc is left_down
then
(
pmx_v <- x;
pmy_v <- y;
handle_left_click_answer(
etb,width_v,height_v,
x_scroll_v,y_scroll_v,
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
capture_ticket,
content_width_v,
content_height_v,
content)
)
else transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
mouse_wheel(ks,delta,mx,my) then
if size(content) is (content_width,content_height) then
y_scroll_v <- bound_scroll(*y_scroll_v-10*delta,content_height,*height_v);
handled(area(etb)([rect(0,0,*width_v,*height_v)])),
mouse_gone then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
captured_mouse_move(f) then
if f(capture_ticket,etb) is
{
failure then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
success(p) then if p is (mx,my) then
if size(content) is (content_width,content_height) then
x_scroll_v <- bound_scroll(*x_scroll_v - (mx-*pmx_v),content_width,*width_v);
y_scroll_v <- bound_scroll(*y_scroll_v - (my-*pmy_v),content_height,*height_v);
pmx_v <- mx;
pmy_v <- my;
handled(area(etb)([rect(0,0,*width_v,*height_v)]))
},
captured_mouse_liberated(f) then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
key_down(f) then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
keyboard_recaptured(f) then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
changed(l) then
transform
(
etb,
needs(l,width_v,height_v,x_scroll_v,y_scroll_v),
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
width_v,height_v,
x_scroll_v,y_scroll_v,
content
),
could_drop(_,_,_,_) then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e),
want_to_drop(_,_,_) then
transmit(etb)(content,-*x_scroll_v,-*y_scroll_v,e)
},
if answer is resized
then
(
if size(content) is (w,h) then
(content_width_v <- w;
x_scroll_v <- 0;
content_height_v <- h;
y_scroll_v <- 0;
answer)
)
else answer.
*** [] Creating the window.
public define Widget
simple_window
(
Var(Word32) width_v,
Var(Word32) height_v,
Var(Word32) x_scroll_v,
Var(Word32) y_scroll_v,
Var(Word32) content_width_v,
Var(Word32) content_height_v,
Widget content,
One -> RGB bgcolor
) =
with pmx_v = var((Word32)0), // previous position of mouse
pmy_v = var((Word32)0),
capture_ticket = var((One)unique),
if size(content) is (w,h) then
content_width_v <- w;
content_height_v <- h;
create_widget
(
/* stretching capabilities */
(One u) |-> if screen_size is (w,h) then stretch_cap(10,10,w,h),
/* stretch */
(Word32 w1, Word32 h1) |-> if screen_size is (scrw,scrh) then
width_v <- limit(w1,10,scrw);
height_v <- limit(h1,10,scrh),
/* get size */
(One u) |-> (*width_v,*height_v),
/* redraw */
draw_method(width_v,height_v,x_scroll_v,y_scroll_v,content,bgcolor),
/* handle events */
event_handler(width_v,height_v,
x_scroll_v,y_scroll_v,
content_width_v, content_height_v,
pmx_v,pmy_v,
content,
capture_ticket),
/* register variables */
(One u) |-> [
register(width_v),
register(height_v),
register(x_scroll_v),
register(y_scroll_v)
]+registrations(content)(u)
).