Source file frame.ml
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
(** Labelled frame widget. *)
open Misc
open Widget
open Tsdl
(** Property ["frame_border_width"] to specify the width of the
frame's border. *)
let border_width = Props.int_prop ~after:[Resize]
~default:1 "frame_border_width"
let css_border_width = Theme.int_prop border_width
(** The frame widget. Optional argument [label] can be used
to pass the widget used in label position. Any widget can be
used as label. *)
class frame ?classes ?name ?props ?wdata ?label () =
object(self)
inherit Bin.bin ?classes ?name ?props ?wdata () as super
(**/**)
method kind = "frame"
val mutable label = (label : Widget.widget option)
method! set_p p ?delay ?(propagate=false) v =
[%debug "%s#set_p ~propagate:%b" self#me propagate];
super#set_p ?delay ~propagate p v ;
match delay, Props.transition p with
| Some _, Some _ -> ()
| _ ->
if propagate then
match label with
| None -> ()
| Some w -> w#set_p ~propagate p v
else
()
method! child_reparented w =
match label with
| Some c when c#equal w -> self#set_label None
| _ -> super#child_reparented w
method! do_apply_theme ~root ~parent parent_path rules =
super#do_apply_theme ~root ~parent parent_path rules;
let path = self#css_path ~parent_path () in
Option.iter (fun w ->
w#do_apply_theme ~root ~parent:theme_props path rules)
label;
width_constraints <- None ;
height_constraints <- None
(**/**)
(** {2 Properties} *)
method frame_border_width = self#get_p border_width
method set_frame_border_width = self#set_p border_width
(**/**)
method! wtree =
let l = match label with
| None -> []
| Some w -> [w#wtree]
in
let l = match child with
| None -> l
| Some w -> l @ [w#wtree]
in
Widget.N (self#coerce, l)
method! render_child renderer ~offset ~g_none ~g_child =
match child with
| None -> ()
| Some _ -> super#render_child renderer ~offset ~g_none ~g_child
method! render_me_parent rend ~offset geom =
super#render_with_prepare rend ~offset geom ;
self#render_label rend ~offset geom
method render_label rend ~offset:(x,y) geom =
match label with
| None -> ()
| Some w ->
let offset = (x+g.x+g_inner.x, y+g.y+g_inner.x) in
let geom = G.translate ~x:(-g.x-g_inner.x) ~y:(-g.y-g_inner.x) geom in
w#render rend ~offset geom
method label_width_constraints =
match label with
| None -> Widget.size_constraints_fixed 0
| Some w -> w#width_constraints
method label_height_constraints =
match label with
| None -> Widget.size_constraints_fixed 0
| Some w -> w#height_constraints
method label_margin =
match label with
| None -> Props.trbl__ 0
| Some w -> w#margin
method private top_padding =
let p = self#padding in
let label_h = self#label_height_constraints.min in
max p.top (label_h / 2)
method! private width_constraints_ =
let p = self#padding in
let cm = self#child_margin in
let cw = self#child_width_constraints.min in
let bw = self#frame_border_width in
let min = super#width_constraints_.min - cw +
bw +
(max (max p.left cm.left + cw - cm.left - cm.right + max p.right cm.right)
(p.left + self#label_width_constraints.min + p.right)) +
bw
in
{ Widget.size_constraints_none with min }
method! private height_constraints =
let p = self#padding in
let bw = self#frame_border_width in
let lh = self#label_height_constraints.min in
let lm = self#label_margin in
let cm = self#child_margin in
let ch = self#child_height_constraints.min in
let min = super#height_constraints_.min - ch +
(max (max 0 (lm.top - p.top) + lh - lm.top - lm.bottom + max lm.bottom cm.top)
(bw + max p.top cm.top)) +
ch - cm.top - cm.bottom +
max p.bottom cm.bottom + bw
in
{ Widget.size_constraints_none with min }
method! compute_child_geometry w =
let glabel =
match label with
| None -> G.zero
| Some w -> w#geometry
in
let lm = self#label_margin in
let cm = w#margin in
let bw = self#frame_border_width in
let p = self#padding in
let x = bw + max cm.left p.left in
let y = max (bw + max cm.top p.top)
(max 0 (lm.top - p.top) + glabel.h + max lm.bottom cm.top)
in
let gc =
{ G.x ; y ;
w = g_inner.w - x - (bw + max p.right cm.right) ;
h = g_inner.h - y - (bw + max p.bottom cm.bottom) ;
}
in
[%debug "%s#compute_child_geometry: g=%a, g_inner=%a, gc=%a"
self#me G.pp g G.pp g_inner G.pp gc];
gc
method private set_label_parent =
match label with
| None -> ()
| Some w ->
w#add_class "frame_label";
w#set_parent ?with_rend:self#with_renderer(Some self#coerce)
(**/**)
(** {2 Label} *)
(** Returns the label widget, if any. *)
method label = label
(** [f#set_label w] sets widget [w] as label, replacing the
previous one if present. *)
method set_label w =
match label, w with
| Some l, Some w when l#equal w#as_widget ->
Log.warn (fun m -> m "%s is already label for %s" w#me self#me)
| _ ->
Option.iter (fun l ->
l#rem_class "frame_label";
l#set_parent ?with_rend:None None) label ;
label <- w;
let () =
match label with
| None -> ()
| Some w ->
Option.iter (fun p -> p#child_reparented w) w#parent ;
self#set_label_parent
in
self#need_resize
(**/**)
method! set_geometry geom =
(match label with
| None -> ()
| Some c ->
let old_geo = c#geometry in
let w = self#label_width_constraints.min in
let h = self#label_height_constraints.min in
let m = self#label_margin in
let p = self#padding in
let bw = self#frame_border_width in
let geo = {
G.x = bw + p.left + m.left ;
y = max 0 (m.top - p.top) ;
w = w - m.left - m.right;
h = h - m.top - m.bottom}
in
[%debug "%s#set_geometry label#geometry=>%a" self#me G.pp geo];
c#set_geometry geo;
if old_geo <> geo then
self#destroy_texture
);
super#set_geometry geom ;
method! prepare rend geom =
match self#texture rend with
| None ->
[%debug "%s#prepare: no texture" self#me];
None
| Some (`Exist t) -> Some t
| Some (`New t) ->
[%debug
"%s#prepare: drawing on my texture" self#me];
let p = self#padding in
let bw = self#frame_border_width in
let label_m = self#label_margin in
let glabel = match label with
| None -> G.zero
| Some w ->
let ge = w#geometry in
{ G.x = p.left + bw ;
y = ge.y ;
w = ge.w + label_m.left + label_m.right ;
h = ge.h ;
}
in
let () =
let x = 0 in
let y = max 0 (glabel.y + (glabel.h - bw) / 2) in
let w = g_inner.w in
let h = max 0 (g_inner.h - y) in
let r = ref { G.x ; y ; w ; h } in
let i = ref bw in
while !i > 0 do
[%debug "%s#prepare draw_rect %a glabel=%a" self#me G.pp !r G.pp glabel];
Texture.draw_rect_r rend t !r self#fg_color_now;
decr i;
r := G.enlarge ~w:(-1) ~h:(-1) !r;
done
in
(match label with
| Some _ ->
Texture.fill_rect rend t (Some glabel) self#bg_color_now
| None -> ()
);
Some t
method! on_sdl_event_down ~oldpos pos e =
if self#sensitive then
let b =
match label with
| None -> false
| Some w ->
let child_pos = Option.map self#to_child_coords pos in
let child_oldpos = Option.map self#to_child_coords oldpos in
w#on_sdl_event_down ~oldpos:child_oldpos child_pos e
in
match b with
| true -> true
| false -> super#on_sdl_event_down ~oldpos pos e
else
false
method! is_leaf_widget = false
method! leaf_widget_at ~x ~y =
match G.inside g ~x ~y with
| false -> None
| true ->
match
match label with
| None -> None
| Some w ->
let (x,y) = self#to_child_coords (x,y) in
w#leaf_widget_at ~x ~y
with
| None -> super#leaf_widget_at ~x ~y
| (Some _) as x -> x
method! next_widget ?inside ~loop pred w =
match w, label with
| None, None -> super#next_widget ?inside ~loop pred None
| None, Some l ->
(match l#next_widget ?inside ~loop pred None with
| None -> super#next_widget ?inside ~loop pred None
| x -> x)
| Some w, Some l when w#equal l -> l#next_widget ?inside ~loop pred None
| Some _, _ -> super#next_widget ?inside ~loop pred None
method! prev_widget ?inside ~loop pred w =
match w, child, label, parent, inside with
| None, None, None, _, Some i when self#equal i#coerce -> None
| None, None, None, None, _ -> None
| None, None, None, Some p, _ -> p#prev_widget ?inside ~loop pred (Some self#coerce)
| None, Some c, None, _, _ -> super#prev_widget ?inside ~loop pred None
| None, None, Some l, _, Some i when self#equal i#coerce ->
(match l#prev_widget ?inside ~loop pred None with
| None -> if loop then self#prev_widget ?inside ~loop pred None else None
| x -> x)
| None, None, Some l, None, _ ->
(match l#prev_widget ?inside ~loop pred None with
| None -> None
| x -> x)
| None, None, Some l, Some p, _ ->
(match l#prev_widget ?inside ~loop pred None with
| None -> p#prev_widget ?inside ~loop pred (Some self#coerce)
| x -> x)
| Some w, Some c, Some l, _, Some i when self#equal i && w#equal c ->
(match l#prev_widget ?inside ~loop pred None with
| None -> if loop then self#prev_widget ?inside ~loop pred None else None
| x -> x)
| Some w, Some c, Some l, None, _ when w#equal c->
(match l#prev_widget ?inside ~loop pred None with
| None -> None
| x -> x)
| Some w, Some c, Some l, Some p, _ when w#equal c ->
(match l#prev_widget ?inside ~loop pred None with
| None -> p#prev_widget ?inside ~loop pred (Some self#coerce)
| x -> x)
| _, _, _, _, Some i when self#equal i ->
if loop then self#prev_widget ?inside ~loop pred None else None
| _, _, _, None, _ -> None
| _, _, _, Some p, _ -> p#prev_widget ?inside ~loop pred (Some self#coerce)
initializer
self#set_label_parent;
ignore(self#connect (Object.Prop_changed Props.is_focus)
(fun ~prev ~now ->
[%debug "%s: is_focus -> %b, invalidating texture"
self#me now];
self#invalidate_texture))
end
type Widget.widget_type += Frame of frame
(** Convenient function to create a {!class-frame}.
Optional arguments [label] can be used to add the widget to
be placed in label position.
See {!Widget.widget_arguments} for other arguments. *)
let frame ?classes ?name ?props ?wdata ?label ?pack () =
let w = new frame ?classes ?name ?props ?wdata ?label () in
w#set_typ (Frame w);
Widget.may_pack ?pack w ;
w
(** Same as {!val:frame} but allows to indicate a text used
as label widget. The label is returned with the frame. *)
let text_frame ?classes ?name ?props ?wdata ?pack text =
let label = Text.label ~text () in
let f = frame ?classes ?name ?props ?wdata ~label:label#coerce ?pack () in
(f, label)