~ubuntu-branches/ubuntu/trusty/ocamlbricks/trusty-proposed

« back to all changes in this revision

Viewing changes to WIDGETS/widget.ml

  • Committer: Package Import Robot
  • Author(s): Lucas Nussbaum
  • Date: 2013-05-28 16:38:50 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20130528163850-njreq52k3sdi3szn
Tags: 0.90+bzr364.3-1
* New upstream snapshot. Should fix the build failures on
  non-native ocaml architectures.
* Add no-ocamlopt-arches.diff: work-around for partial upstream fix
  (see https://bugs.launchpad.net/ocamlbricks/+bug/1130098).

Show diffs side-by-side

added added

removed removed

Lines of Context:
107
107
 
108
108
(** {2 Class definition} *)
109
109
 
110
 
(** A ComboTextTree is a combo with eventually some dependent {i childs} (or {i slaves}).
 
110
(** A ComboTextTree is a combo with eventually some dependent {i children} (or {i slaves}).
111
111
    The choice list of a node in the tree depends on the father's selected value and on the
112
112
    ancestors's selected values. The list of choices of a node is given dynamically by a function
113
113
    called the {i generator} which is used to calculte or recalculate the choice list.
120
120
  (* The first input for the generator. *)
121
121
  ~(msg:string Environment.string_env)
122
122
 
123
 
  (* The key of the pair (key,value) send to its childs. *)
 
123
  (* The key of the pair (key,value) send to its children. *)
124
124
  ~(key:string)
125
125
 
126
126
  (* An optional callback function, to call at any change *)
144
144
      is not really dependent from its argument, but it is a simple costant function. *)
145
145
  method generator : (string Environment.string_env -> string list) = generator
146
146
 
147
 
  (** The key of the pair (key,value) which this widget (node) eventually transmit to its childs (slaves). This field
 
147
  (** The key of the pair (key,value) which this widget (node) eventually transmit to its children (slaves). This field
148
148
      is set at the creation. The value of the pair (key,value) will be the selected value of the widget, of course. *)
149
149
  method key : string = key
150
150
 
151
151
  (** A secondary function to call at any change of the selected item. This represent an additional callback.
152
 
      The principal callback is the method [childs_rebuild] which propagate the selected value to all childs. *)
 
152
      The principal callback is the method [children_rebuild] which propagate the selected value to all children. *)
153
153
  method callback  : (string -> unit) = match callback with None   -> (fun x->()) | Some f -> f
154
154
 
155
155
  (** The function to call to attach self somewhere. For instance :
156
156
      {[ packing = dialog#table#attach ~left:1 ~top:2 ~right:3 ]}
157
 
      Every time the comboTextTree is rebuilded, the old box is destroyed, rebuilded and finally repackaged
 
157
      Every time the comboTextTree is rebuilt, the old box is destroyed, rebuilt and finally repackaged
158
158
      with this packing function. *)
159
159
  method packing   : (GObj.widget -> unit) = match packing with None -> (fun x->()) | Some f -> f
160
160
 
162
162
 
163
163
  (** This fields stores the environment used for the last generation of the choice list.
164
164
      This information is fundamental because if this widget has some ancestors and also some descendents,
165
 
      for any alteration of its state, it must resend to its childs the last environment received
 
165
      for any alteration of its state, it must resend to its children the last environment received
166
166
      from its ancestors enriched with the pair (key,value) representing its own state. In this way,
167
167
      every descendent know the state of all its ancestors (not only the state of its father). *)
168
168
  val mutable env     : (string Environment.string_env)     = msg
174
174
  val mutable box     : #GEdit.combo_box  = initial_box
175
175
  val mutable col     : ('a GTree.column) = initial_col
176
176
 
177
 
  (** The childs list of this widget. *)
178
 
  val mutable childs  : comboTextTree list = []
 
177
  (** The children list of this widget. *)
 
178
  val mutable children  : comboTextTree list = []
179
179
 
180
180
  (** Accessors *)
181
181
 
183
183
  method choices = choices
184
184
  method box     = box
185
185
  method col     = col
186
 
  method childs  = childs
187
 
  method child i = List.nth childs i
 
186
  method children  = children
 
187
  method child i = List.nth children i
188
188
 
189
189
  (** Convenient aliases *)
190
190
 
191
 
  method slave   = List.nth childs 0
192
 
  method slave0  = List.nth childs 0
193
 
  method slave1  = List.nth childs 1
194
 
  method slave2  = List.nth childs 2
195
 
  method slave3  = List.nth childs 3
196
 
  method slave4  = List.nth childs 4
197
 
  method slave5  = List.nth childs 5
 
191
  method slave   = List.nth children 0
 
192
  method slave0  = List.nth children 0
 
193
  method slave1  = List.nth children 1
 
194
  method slave2  = List.nth children 2
 
195
  method slave3  = List.nth children 3
 
196
  method slave4  = List.nth children 4
 
197
  method slave5  = List.nth children 5
198
198
 
199
199
  (** Fixing variable fields *)
200
200
 
202
202
  method set_choices l = choices <- l
203
203
  method set_box     b = box     <- b
204
204
  method set_col     c = col     <- c
205
 
  method set_childs  l = childs  <- l
206
 
  method add_child   x = childs  <- childs @ [x]
 
205
  method set_children  l = children  <- l
 
206
  method add_child   x = children  <- children @ [x]
207
207
 
208
208
  (** Selected item *)
209
209
 
220
220
    try
221
221
      let i = Option.extract (ListExtra.indexOf v self#choices) in
222
222
      self#box#set_active i ;
223
 
      self#childs_rebuild ()
 
223
      self#children_rebuild ()
224
224
    with _ -> ()
225
225
 
226
226
 
227
 
  (** Rebuilding self and childs *)
 
227
  (** Rebuilding self and children *)
228
228
 
229
 
  (** Demands to all childs to rebuild theirself and their childs and so on.
230
 
      This procedure is performed sending to all childs the ancestor environment (method [env]) enriched by
 
229
  (** Demands to all children to rebuild theirself and their children and so on.
 
230
      This procedure is performed sending to all children the ancestor environment (method [env]) enriched by
231
231
      the pair (key,value), where value is the current selected item of this node. *)
232
 
  method childs_rebuild () =
 
232
  method children_rebuild () =
233
233
    let msg = Environment.make_string_env (self#env#to_list @ [(self#key,self#selected)]) in  (* x = self#selected *)
234
 
    List.iter (fun w -> w#rebuild msg) self#childs
235
 
 
236
 
 
237
 
  (** Rebuild this widget, and its eventually all childs, with the new given environment. *)
 
234
    List.iter (fun w -> w#rebuild msg) self#children
 
235
 
 
236
 
 
237
  (** Rebuild this widget, and its eventually all children, with the new given environment. *)
238
238
  method rebuild (msg : string Environment.string_env) =
239
239
    begin
240
240
      (* Save the current selected choice. We will try to reset it. *)
260
260
      let i = ((ListExtra.indexOf previous self#choices) |=> 0) in
261
261
      (self#box#set_active i) ;
262
262
 
263
 
      (* Propagate to its childs. *)
264
 
      self#childs_rebuild () ;
 
263
      (* Propagate to its children. *)
 
264
      self#children_rebuild () ;
265
265
 
266
266
      ()
267
267
    end
286
286
  (* This method must be called by a constructor after the bootstrap.
287
287
     These action cannot be placed in the boostrap of the instance. *)
288
288
  method initialize_callbacks =
289
 
    let _ = self#changedAndGetActive (fun x -> self#childs_rebuild ()) in  (** First connect the standard callback. *)
 
289
    let _ = self#changedAndGetActive (fun x -> self#children_rebuild ()) in  (** First connect the standard callback. *)
290
290
    let _ = self#changedAndGetActive self#callback in ()    (** Second connect the given callback. *)
291
291
 
292
292
end;; (* class comboTextTree *)
307
307
 
308
308
  ~(generator: (string Environment.string_env)->(string list)) (** The option generator. May be a constant function as particular case. *)
309
309
  ~(msg:string Environment.string_env)                         (** The input for the generator. *)
310
 
  ~(key:string)                                      (** The key of the pair (key,value) send to its childs. *)
 
310
  ~(key:string)                                      (** The key of the pair (key,value) send to its children. *)
311
311
  ~(callback:(choice->unit) option)                  (** An optional callback function, to call at any change *)
312
312
  ~(packing:(GObj.widget -> unit) option)            (** The packing function. *)
313
313
 
316
316
;;
317
317
 
318
318
 
319
 
(** Make a simple combo text with no childs.
320
 
     You can specify a [key] (if you plan to affect some childs to this widget) and an additional [callback]
 
319
(** Make a simple combo text with no children.
 
320
     You can specify a [key] (if you plan to affect some children to this widget) and an additional [callback]
321
321
     fonction of type [choice -> unit], which will be called every time the user will modify its selection.
322
322
     You also can specify a packing function. Examples:
323
323
 
451
451
       slave0   slave1
452
452
v} } *)
453
453
 
454
 
(** Make a simple tree with 3 nodes: a root combo with two combos (dependent) childs (which can be accessed with the handlers
 
454
(** Make a simple tree with 3 nodes: a root combo with two combos (dependent) children (which can be accessed with the handlers
455
455
    [master#slave0] and [master#slave1]). This function is in this API as an exemple. See the code in order to easily
456
456
    define your own comboTextTree. *)
457
457
let fromListWithTwoSlaves