package stk

  1. Overview
  2. Docs
SDL-based GUI toolkit

Install

Dune Dependency

Authors

Maintainers

Sources

ocaml-stk-0.2.0.tar.bz2
md5=84927cce2b859a484df176163a68a21c
sha512=7d79d0b18c1550d59932ec27e5162f5ec5cfeb6560de32224cc7915f392e11d2c2fd94c44128b4b828b6ed700c2f079d120b2423874d71544e71bbab9253e870

doc/src/stk/container.ml.html

Source file container.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
401
402
403
(*********************************************************************************)
(*                OCaml-Stk                                                      *)
(*                                                                               *)
(*    Copyright (C) 2023-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU General Public License as                    *)
(*    published by the Free Software Foundation, version 3 of the License.       *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public                  *)
(*    License along with this program; if not, write to the Free Software        *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    As a special exception, you have permission to link this program           *)
(*    with the OCaml compiler and distribute executables, as long as you         *)
(*    follow the requirements of the GNU GPL in regard to all of the             *)
(*    software in the executable aside from the OCaml compiler.                  *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** Container widget. *)

open Misc
open Tsdl
open Widget

(** A container child, parametrized by the type of
  the associated data. *)
type 'a child = {
    widget : Widget.widget ;
    mutable data : 'a ;
  }


(** A container widget is a widget which can contain other widgets.
Each widget can be associated to additional data ['a],
so each container has type [['a] container].
This class must be inherited, as it is for example
by {!class-container_list}, which specialises this class by using
a list to store children widgets.
*)
class virtual ['a, 'pos] container ?classes ?name ?props ?wdata () =
  object(self)
    inherit widget ?classes ?name ?props ?wdata () as super

    (**/**)

    method private virtual child_by_widget : Widget.widget -> 'a child option
    method private virtual find_child_opt : ('a child -> bool) -> 'a child option
    method private virtual find_child_pos_opt : ('a child -> bool) -> 'pos option
    method private virtual iter_children : ('a child -> unit) -> unit
    method private virtual fold_children_right : 'b. ('a child -> 'b -> 'b) -> 'b -> 'b
    method private virtual fold_children_left : 'b. ('b -> 'a child -> 'b) -> 'b -> 'b
    method private virtual add_to_children : ?pos:'pos -> 'a child -> unit
    method private virtual remove_from_children : Widget.widget -> bool

    method private child_by_widget (w:Widget.widget) =
      let id = w#id in
      self#find_child_opt (fun c -> Oid.equal c.widget#id id)

    method private child_by_coords ~x ~y =
      self#find_child_opt (fun c ->
         c.widget#visible &&
           G.inside ~x ~y c.widget#geometry)

    method private widget_index (w:Widget.widget) =
      let id = w#id in
      self#find_child_pos_opt (fun c -> Oid.equal c.widget#id id)

    method private widget_data (w:Widget.widget) =
      match self#child_by_widget w with
      | None -> None
      | Some c -> Some c.data

    method private set_widget_data w data =
      match self#child_by_widget w with
      | None -> Log.warn (fun m -> m "No widget %s in %s" w#me self#me)
      | Some c -> c.data <- data

    method! set_p p ?delay ?(propagate=false) v =
      [%debug "%s#set_p %s ~propagate:%b" self#me (Props.name p) propagate];
      super#set_p ?delay ~propagate p v ;
      match delay, Props.transition p with
      | Some _, Some _ -> ()
      | _ ->
          if propagate then
            self#iter_children (fun c -> c.widget#set_p ~propagate p v)

    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
      self#iter_children (fun c ->
         c.widget#do_apply_theme ~root ~parent:theme_props path rules)

    method private focused_child =
      self#find_child_opt (fun c -> c.widget#get_p Props.is_focus)

    method! focused_widget =
      if self#is_focus then
        match self#focused_child with
        | None -> Some self#coerce
        | Some c -> c.widget#focused_widget
      else
        None

    method! release_focus =
      match
        match self#focused_child with
        | None -> true
        | Some c -> c.widget#release_focus
      with
      | true ->
          self#set_p Props.is_focus false ;
          self#set_p Props.has_focus false ;
          true
      | _ -> false

    method! get_focus =
      match super#get_focus with
      | None -> None
      | Some has_focus ->
          self#iter_children (fun c -> c.widget#set_p Props.is_focus false);
          Some has_focus

    method! set_has_focus b =
      match super#set_has_focus b with
      | true -> true
      | false ->
          match self#focused_child with
          | None -> false
          | Some c -> c.widget#set_has_focus b

    method private visible_children =
      self#fold_children_right
        (fun c acc ->
           if c.widget#visible then c.widget::acc else acc) []

    method! on_sdl_event_down ~oldpos pos e =
      [%debug "%s#on_sdl_event_down e=%a" self#me Fmts.pp_event e];
      if self#sensitive then
        match
          let f (x,y) = (x - g.x - g_inner.x , y - g.y - g_inner.y) in
          let oldpos = Option.map f oldpos in
          let pos = Option.map f pos in
          match Sdl.Event.(enum (get e typ)) with
          | `Key_down | `Key_up | `Text_input | `Text_editing ->
              (
               match self#find_child_opt
                 (fun c -> c.widget#get_p Props.is_focus)
               with
               | None -> false
               | Some c -> c.widget#on_sdl_event_down ~oldpos pos e
              )
          | _ ->
              self#fold_children_left
                (fun acc c ->
                   let w = c.widget in
                   if w#visible &&
                     ((match oldpos with
                      | Some (x,y) -> G.inside ~x ~y w#geometry
                      | None -> false
                     ) ||
                       match pos with
                       | Some (x,y) -> G.inside ~x ~y w#geometry
                       | None -> false
                     )
                   then
                     (
                      [%debug "%s#on_sdl_event_down: propagating event to %s"
                         self#me w#me];
                      let b = w#on_sdl_event_down ~oldpos pos e in
                      acc || b
                     )
                   else
                     acc
                )
                false
        with
        | true -> true
        | false -> self#on_sdl_event pos e
      else
        false

    method! child_reparented w = self#remove w; self#need_resize

    method private add ?pos w data =
      Log.info (fun m -> m "%s#add %s" self#me w#me);
      match self#find_child_opt (fun c -> Oid.equal c.widget#id w#id) with
      | Some _ ->
          Log.warn (fun m -> m "%s is already packed in %s" w#me self#me);
          false
      | None ->
          let old_parent = w#parent in
          match old_parent with
          | Some p when p#equal self#as_widget -> false
          | _ ->
              min_width <- None ;
              min_height <- None ;
              self#add_to_children ?pos { widget = w ; data } ;
              Option.iter (fun p -> p#child_reparented w) old_parent ;
              w#set_parent ?with_rend:with_renderer (Some self#coerce) ;
              true

    method private remove (w:widget) =
      let b = self#remove_from_children w in
      if b then
        (
         min_width <- None ;
         min_height <- None ;
         w#set_parent None
        )
      else
        Log.warn (fun m -> m "%s is not packed in %s, not removing" w#me self#me);
      b

    method! set_parent ?with_rend w =
      super#set_parent ?with_rend w ;
      self#iter_children
        (fun c -> c.widget#set_parent ?with_rend (Some self#coerce))

    method! child_need_resize w =
      match self#find_child_opt (fun c -> Oid.equal c.widget#id w#id) with
      | None ->
          Log.warn (fun m -> m
             "%s#child_need_resize: %s not in children" self#me w#me);
          ()
      | Some c -> if w#visible then self#need_resize

    method! private render_me ~layer rend ~offset:(x,y) rg =
      let off_x = g.x + g_inner.x in
      let off_y = g.y + g_inner.y in
      let offset = (x + off_x, y + off_y) in
      let rg = G.translate ~x:(-off_x) ~y:(-off_y) rg in
      self#iter_children
        (fun c -> c.widget#render ~layer ~offset rend rg)

    method! is_leaf_widget = false
    method! leaf_widget_at ~x ~y =
      let x = x - g.x - g_inner.x in
      let y = y - g.y - g_inner.y in
      match self#child_by_coords ~x ~y with
      | None -> None
      | Some c ->  c.widget#leaf_widget_at ~x ~y

    method! destroy =
      self#iter_children (fun c -> c.widget#destroy) ;
      super#destroy ;
  end

(* This class specialises {!class-container} to store children widgets in a list.
It must be inherited (as it is for example by {!Pack.class-box} or {!Pack.class-paned}).
*)
class virtual ['a] container_list ?classes ?name ?props ?wdata () =
  object(self)
    inherit ['a, int] container ?classes ?name ?props ?wdata () as super

    (**/**)
    val mutable children = ([]:'a child list)
    method private children = children

    method private find_child_opt pred = List.find_opt pred children
    method private find_child_pos_opt pred = List.find_index pred children
    method private iter_children f = List.iter f children
    method private fold_children_right f acc = List.fold_right f children acc
    method private fold_children_left f acc = List.fold_left f acc children

    method private reorder_child w pos =
      let id = w#id in
      match self#child_by_widget w with
      | None -> ()
      | Some child ->
          let rec iter acc p = function
          | [] -> List.rev acc
          | l when p = pos -> iter (child::acc) (p+1) l
          | c :: q ->
              if Oid.equal c.widget#id id then
                iter acc p q
              else iter (c::acc) (p+1) q
          in
          children <- iter [] 0 children;
          self#need_resize

    method! wtree = N (self#coerce, List.map (fun c -> c.widget#wtree) children)

    method! grab_focus ?(last=false) () =
      [%debug "%s#grab_focus ~last:%b" self#me last];
      if self#visible then
        match self#get_p Props.can_focus with
        | false -> false
        | true ->
            match super#grab_focus ~last () with
            | true -> true
            | false ->
                let rec iter = function
                | [] -> false
                | c :: q ->
                    match c.widget#visible, c.widget#get_p Props.can_focus with
                    | true, true ->
                        (match c.widget#grab_focus ~last () with
                         | false -> iter q
                         | true -> true)
                    | _ -> iter q
                in
                iter (if last then List.rev children else children)
      else
        false

    method private child_move_focus (w:widget) last children on_none =
      let rec iter = function
      | c :: next :: q ->
          if Oid.equal c.widget#id w#id then
            ([%debug "%s#child_move_focus next.widget=%s, visible=%b"
              self#me next.widget#me next.widget#visible];
             if next.widget#visible then
               match next.widget#grab_focus ~last () with
               | false -> iter (c :: q)
               | true -> true
             else
               iter (c :: q)
            )
          else
            iter (next :: q)
      | [] | [_] -> on_none ()
      in
      iter children

    method! child_focus_next (w:widget) =
      self#child_move_focus w false
        children (fun () -> self#focus_next)

    method! child_focus_prev (w:widget) =
      self#child_move_focus w true
        (List.rev children)
        (fun () -> self#focus_prev)

    method private add_to_children ?pos c =
      children <- Misc.list_add ?pos children c

    method private remove_from_children w =
      let len = List.length children in
      children <- List.filter (fun c -> not (Oid.equal c.widget#id w#id)) children ;
      let len2 = List.length children in
      len <> len2

    method! next_widget ?inside ~loop pred w =
      let rec iter = function
      | [] ->
          (match inside, parent with
           | Some i,_ when self#equal i ->
               if loop then self#next_widget ?inside ~loop pred None else None
           | _, None -> None
           | _, Some p -> p#next_widget ?inside ~loop pred (Some self#coerce))
      | c :: q when pred c.widget -> Some c.widget
      | c :: q ->
          match c.widget#next_widget ?inside ~loop pred None with
          | None -> iter q
          | x -> x
      in
      match w with
      | None -> iter children
      | Some w ->
          let rec find = function
          | [] -> iter []
          | c :: q when c.widget#equal w -> iter q
          | _ :: q -> find q
          in
          find children

    method! prev_widget ?inside ~loop pred w =
      let rec iter = function
      | [] ->
          (match inside, parent with
           | 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))
      | c :: q when pred c.widget -> Some c.widget
      | c :: q ->
          match c.widget#prev_widget ?inside ~loop pred None with
          | None -> iter q
          | x -> x
      in
      match w with
      | None -> iter (List.rev children)
      | Some w ->
          let rec find = function
          | [] -> iter []
          | c :: q when c.widget#equal w -> iter q
          | _ :: q -> find q
          in
          find (List.rev children)
  end


OCaml

Innovation. Community. Security.