1 (***********************************************************************)
3 (* MLTk, Tcl/Tk interface of Objective Caml *)
5 (* Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis *)
6 (* projet Cristal, INRIA Rocquencourt *)
7 (* Jacques Garrigue, Kyoto University RIMS *)
9 (* Copyright 2002 Institut National de Recherche en Informatique et *)
10 (* en Automatique and Kyoto University. All rights reserved. *)
11 (* This file is distributed under the terms of the GNU Library *)
12 (* General Public License, with the special exception on linking *)
13 (* described in file LICENSE found in the Objective Caml source tree. *)
15 (***********************************************************************)
16 (* Some notion of synthetic events *)
21 (* To each event is associated a table of (widget, callback) *)
22 let events = Hashtbl.create 37
25 * "cascading" events (on the same event) are not supported
26 * Only one binding active at a time for each event on each widget.
29 (* Get the callback table associated with <name>. Initializes if required *)
31 try Hashtbl.find events name
34 let h = Hashtbl.create 37 in
35 Hashtbl.add events name h;
36 (* Initialize the callback invocation mechanism, based on
39 let var = "camltk_events(" ^ name ^")" in
40 let tkvar = Textvariable.coerce var in
42 Textvariable.handle tkvar
44 begin match Textvariable.get tkvar with
45 "all" -> (* Invoke all callbacks *)
52 | p -> (* Invoke callback for p *)
54 let w = cTKtoCAMLwidget p
55 and f = Hashtbl.find h p in
60 set ()(* reactivate the callback *)
65 (* Remove binding for event <name> on widget <w> *)
67 Hashtbl.remove (get_event name) (Widget.name w)
69 (* Adds <f> as callback for widget <w> on event <name> *)
72 Hashtbl.add (get_event name) (Widget.name w) f
74 (* Sends event <name> to all widgets *)
76 Textvariable.set (Textvariable.coerce ("camltk_events(" ^ name ^")")) "all"
78 (* Sends event <name> to widget <w> *)
80 Textvariable.set (Textvariable.coerce ("camltk_events(" ^ name ^")"))
83 (* Remove all callbacks associated to widget <w> *)
84 let remove_callbacks w =
85 Hashtbl.iter (fun _ h -> Hashtbl.remove h (Widget.name w)) events
88 add_destroy_hook remove_callbacks