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
|
(***********************************************************************)
(* *)
(* MLTk, Tcl/Tk interface of OCaml *)
(* *)
(* Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis *)
(* projet Cristal, INRIA Rocquencourt *)
(* Jacques Garrigue, Kyoto University RIMS *)
(* *)
(* Copyright 2002 Institut National de Recherche en Informatique et *)
(* en Automatique and Kyoto University. All rights reserved. *)
(* This file is distributed under the terms of the GNU Library *)
(* General Public License, with the special exception on linking *)
(* described in file LICENSE found in the OCaml source tree. *)
(* *)
(***********************************************************************)
(* $Id$ *)
open Protocol
external internal_tracevar : string -> cbid -> unit
= "camltk_trace_var"
external internal_untracevar : string -> cbid -> unit
= "camltk_untrace_var"
external set : string -> string -> unit = "camltk_setvar"
external get : string -> string = "camltk_getvar"
type textVariable = string
(* List of handles *)
let handles = Hashtbl.create 401
let add_handle var cbid =
try
let r = Hashtbl.find handles var in
r := cbid :: !r
with
Not_found ->
Hashtbl.add handles var (ref [cbid])
let exceptq x =
let rec ex acc = function
[] -> acc
| y::l when y == x -> ex acc l
| y::l -> ex (y::acc) l
in
ex []
let rem_handle var cbid =
try
let r = Hashtbl.find handles var in
match exceptq cbid !r with
[] -> Hashtbl.remove handles var
| remaining -> r := remaining
with
Not_found -> ()
(* Used when we "free" the variable (otherwise, old handlers would apply to
* new usage of the variable)
*)
let rem_all_handles var =
try
let r = Hashtbl.find handles var in
List.iter (internal_untracevar var) !r;
Hashtbl.remove handles var
with
Not_found -> ()
(* Variable trace *)
let handle vname ~callback:f =
let id = new_function_id() in
let wrapped _ =
clear_callback id;
rem_handle vname id;
f() in
Hashtbl.add callback_naming_table id wrapped;
add_handle vname id;
if !Protocol.debug then begin
prerr_cbid id; prerr_string " for variable "; prerr_endline vname
end;
internal_tracevar vname id
(* Avoid space leak (all variables are global in Tcl) *)
module StringSet =
Set.Make(struct type t = string let compare = compare end)
let freelist = ref (StringSet.empty)
let memo = Hashtbl.create 101
(* Added a variable v referenced by widget w *)
let add w v =
let w = Widget.forget_type w in
let r =
try Hashtbl.find memo w
with
Not_found ->
let r = ref StringSet.empty in
Hashtbl.add memo w r;
r in
r := StringSet.add v !r
(* to be used with care ! *)
let free v =
rem_all_handles v;
freelist := StringSet.add v !freelist
(* Free variables associated with a widget *)
let freew w =
try
let r = Hashtbl.find memo w in
StringSet.iter free !r;
Hashtbl.remove memo w
with
Not_found -> ()
let _ = add_destroy_hook freew
(* Allocate a new variable *)
let counter = ref 0
let getv () =
let v =
if StringSet.is_empty !freelist then begin
incr counter;
"camlv("^ string_of_int !counter ^")"
end
else
let v = StringSet.choose !freelist in
freelist := StringSet.remove v !freelist;
v in
set v "";
v
let create ?on: w () =
let v = getv() in
begin
match w with
Some w -> add w v
| None -> ()
end;
v
(* to be used with care ! *)
let free v =
freelist := StringSet.add v !freelist
let cCAMLtoTKtextVariable s = TkToken s
let name s = s
let coerce s = s
|