package stk

  1. Overview
  2. Docs
SDL-based GUI toolkit

Install

Dune Dependency

Authors

Maintainers

Sources

ocaml-stk-0.1.0.tar.bz2
md5=c334ffabde8b710f1eba6699db0f601a
sha512=7978e3f10bc196ee6177ded9ae0313a5ba65e1a74e501fbecbe5ebc216ca6ee7117deaff5bc4c414083a4a55851a81e5dedaa8d0a880ad72689b3f56f3b064f5

doc/src/stk/frame.ml.html

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
(** 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"

(** 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 ?(class_="frame") ?name ?props ?label () =
  object(self)
    inherit Bin.bin ~class_ ?name ?props () as super

    (**/**)
    val mutable label = (label : Widget.widget option)
    method! set_p ?(propagate=false) p v =
      Log.debug (fun m -> m "%s#set_p ~propagate:%b" self#me propagate);
      super#set_p ~propagate p v ;
      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

    (**/**)

    (** {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 ~layer renderer ~offset ~g_none ~g_child =
      match child with
      | None -> ()
      | Some _ -> super#render_child ~layer renderer ~offset ~g_none ~g_child

    method! render_me_parent ~layer rend ~offset geom =
      super#render_with_prepare ~layer rend ~offset geom ;
      self#render_label ~layer rend ~offset geom

    method render_label ~layer 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 ~layer rend ~offset geom

    method label_min_width =
      match label with
      | None -> 0
      | Some w -> w#min_width
    method label_min_height =
      match label with
      | None -> 0
      | Some w -> w#min_height
    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_min_height in
      max p.top (label_h / 2)

    method! private min_width_ =
      let p = self#padding in
      let cm = self#child_margin in
      let cw = self#child_min_width in
      let bw = self#frame_border_width in
      (* use super#min_width but remove child_min_width *)
      super#min_width_ - cw +
      bw +
      (max (max p.left cm.left + cw - cm.left - cm.right + max p.right cm.right)
         (p.left + self#label_min_width + p.right)) +
        bw

    method! private min_height_ =
      let p = self#padding in
      let bw = self#frame_border_width in
      let lh = self#label_min_height in
      let lm = self#label_margin in
      let cm = self#child_margin in
      let ch = self#child_min_height in
      (* use super#min_height but remove child_min_height *)
      super#min_height_ - 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

    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
      Log.debug (fun m -> m "%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#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#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_min_width in
           let h = self#label_min_height 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
          Log.debug (fun m -> m "%s#set_geometry label#geometry=>%a" self#me G.pp geo);
          c#set_geometry geo;
          (* if label's geometry changed, we destroy our texture
             or else the frame border will not be redrawn *)
          if old_geo <> geo then
            self#destroy_texture
      );
      super#set_geometry geom ;

    method! prepare ~layer rend geom =
      if layer = self#get_p Props.layer then
        (
         match self#texture rend with
         | None ->
             Log.debug (fun m -> m "%s#prepare: no texture" self#me);
             None
         | Some (`Exist t) -> Some t
         | Some (`New t) ->
             Log.debug (fun m -> m
                "%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 cm = self#child_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 (*+ max 0 (label_m.top - p.top) + max label_m.bottom cm.top*) ;
                   }
             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
                 Log.debug (fun m -> m "%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
        )
      else
        None

    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;
      (* TODO: handle properties which should trigger invalidating the texture *)
      ignore(self#connect (Object.Prop_changed Props.is_focus)
        (fun ~prev ~now ->
          Log.debug (fun m -> m "%s: is_focus -> %b, invalidating texture"
             self#me now);
          self#invalidate_texture))
  end

(** 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 ?name ?props ?label ?pack () =
  let w = new frame ?name ?props ?label () in
  Widget.may_pack ?pack w ;
  w
OCaml

Innovation. Community. Security.