check_box.anubis
4.97 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
The Anubis Project.
A Widget System (4th version).
The 'check box' widget.
Copyright (c) Alain Proute' 2005.
Authors: Alain Proute'
In this file, the 'check box' widget is defined.
In order to create a check box, you must provide a dynamic variable of type
'Var(Bool)'. The check box always reflects the state of this variable. If the variable
contains 'true', the box is checked, otherwise it is not checked. When the user left
clicks the check box, the state of the variable is negated.
By default, this widget looks like a small square of 10x10 pixels, with another small
solid square inside it when it is checked.
read widget.anubis
public define Widget
check_box
(
Var(Bool) v
).
However, you can optionally provide two images, one for the non checked box, and
another one for the checked box. These two images should be of the same size.
public define Widget
check_box
(
Var(Bool) v,
HostImage not_checked,
HostImage checked
).
--- That's all for the public part ! --------------------------------------------------
read tools/basis.anubis
read tools.anubis
read trace.anubis
The size method when there is no image:
define (Int32,Int32)
size_no_image
(
One u
) =
(10,10).
The size method when there are images:
define One -> (Int32,Int32)
size_images
(
HostImage not_checked,
HostImage checked
) =
(One u) |->
if size(not_checked) is (w1,h1) then
if size(checked) is (w2,h2) then
(max(w1,w2),max(h1,h2)).
The redraw method when there is no image.
define WidgetDrawToolBox -> One
redraw_no_image
(
Var(Bool) v
) =
with black = rgb(0,0,0),
red = rgb(180,0,0),
white = rgb(255,255,255),
(WidgetDrawToolBox dtb) |->
draw(dtb)(rect(0,0,10,10),white);
draw_relief_edge(dtb,1,rect(0,0,10,10),black,black);
if *v
then draw(dtb)(rect(2,2,8,8),red)
else unique.
The redraw method when there are images.
define WidgetDrawToolBox -> One
redraw_images
(
Var(Bool) v,
HostImage not_checked,
HostImage checked
) =
(WidgetDrawToolBox dtb) |->
draw(dtb)(if *v then checked else not_checked,0,0).
define Widget
check_box
(
Var(Bool) v,
Maybe((HostImage,HostImage)) mb_images
) =
create_widget
(
/* getting the size of the check box */
if mb_images is
{
failure then size_no_image,
success(p) then if p is (not_checked,checked) then
size_images(not_checked,checked)
},
/* redrawing the check box */
if mb_images is
{
failure then redraw_no_image(v),
success(p) then if p is (not_checked,checked) then
redraw_images(v,not_checked,checked)
},
/* handling events */
with the_rect = (WidgetRectangle)if mb_images is
{
failure then rect(0,0,10,10),
success(p) then if p is (not_checked,checked) then
if size_images(not_checked,checked)(unique) is (w,h) then
rect(0,0,w,h)
},
(WidgetEventToolBox etb, WidgetEvent e) |->
if e is
{
mouse_move(ks,x,y) then ignored,
mouse_click(ks,mc,x,y) then if mc is
{
left_down then v <- not(*v);
handled(area(etb)([the_rect])),
left_up then ignored,
middle_down then ignored,
middle_up then ignored,
right_down then ignored,
right_up then ignored
},
mouse_wheel(ks,d,x,y) then ignored,
mouse_gone then ignored,
captured_mouse_move(_) then ignored,
captured_mouse_liberated(_) then ignored,
key_down(_) then ignored,
keyboard_recaptured(_) then ignored,
changed(l) then
if member(l,var_id(v))
then handled(area(etb)([the_rect]))
else ignored
},
/* registering the variable */
(One u) |-> [register(v)]
).
public define Widget
check_box
(
Var(Bool) v
) =
check_box(v,failure).
public define Widget
check_box
(
Var(Bool) v,
HostImage not_checked,
HostImage checked
) =
check_box(v,success((not_checked,checked))).