1 (* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
3 * This file is part of lablgtkmathview, the Ocaml binding
4 * for the GtkMathView widget.
6 * lablgtkmathview is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * lablgtkmathview is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with lablgtkmathview; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * For details, send a mail to the author.
23 (* finds the common node ancestor of two nodes *)
24 let common_ancestor (first : Gdome.node) (last : Gdome.node) =
26 match n#get_parentNode with
28 | Some p -> n::(path p)
32 _, hd1::tl1, hd2::tl2 when hd1#isSameNode hd2 -> (last_common ((Some hd1),tl1,tl2))
34 | _,_,_ -> assert false
36 (last_common (None,(List.rev (path first)),(List.rev (path last))))
38 let same_element (e1 : Gdome.element option) (e2 : Gdome.element option) =
41 | Some e1, Some e2 when (e1 :> Gdome.node)#isSameNode (e2 :> Gdome.node) -> true
44 (* true if n1 is n2 or one of n2's descendants *)
45 let rec descendant_of (n1 : Gdome.node) (n2 : Gdome.node) =
46 if n1#isSameNode n2 then true
48 match n1#get_parentNode with
50 | Some n1' -> descendant_of n1' n2
52 let remove_descendants_of (el : Gdome.element) =
56 | hd::tl when descendant_of (hd :> Gdome.node) (el :> Gdome.node) -> aux tl
57 | hd::tl -> hd::(aux tl)
61 (* mem el l = true if the node n is stored in the list l *)
62 let mem (el : Gdome.element) =
65 hd::_ when (hd :> Gdome.node)#isSameNode (el :> Gdome.node) -> true
71 (* remove el l = l' where l' has the same nodes as l except that all
72 * the occurrences of n have been removed *)
73 let remove (el : Gdome.element) =
76 hd::tl when (hd :> Gdome.node)#isSameNode (el :> Gdome.node) -> remove_aux tl
77 | hd::tl -> hd::(remove_aux tl)
82 class single_selection_math_view_signals obj (set_selection_changed : (Gdome.element option -> unit) -> unit) =
84 inherit GMathView.math_view_signals obj
85 method selection_changed = set_selection_changed
89 class single_selection_math_view obj =
91 inherit GMathView.math_view_skel obj
92 val mutable first_selected = None
93 val mutable root_selected = None
94 val mutable selection_changed = (fun _ -> ())
96 method set_selection elem =
98 match root_selected with
100 | Some e -> self#unselect e
102 root_selected <- elem;
105 | Some e -> self#select e
107 method get_selection = root_selected
110 new single_selection_math_view_signals obj (function f -> selection_changed <- f)
112 method action_toggle (elem : Gdome.element) =
113 match elem#get_namespaceURI, elem#get_localName with
114 Some ns, Some ln when ns#to_string = "http://www.w3.org/1998/Math/MathML" &&
115 ln#to_string = "maction" ->
117 let selection_attr = Gdome.domString "selection" in
119 if elem#hasAttribute ~name:selection_attr then
120 int_of_string (elem#getAttribute ~name:selection_attr)#to_string
125 (* the widget will cast the index back into a valid range *)
126 elem#setAttribute ~name:selection_attr ~value:(Gdome.domString (string_of_int (selection + 1))) ;
132 match elem#get_parentNode with
136 self#action_toggle (new Gdome.element_of_node p)
138 GdomeInit.DOMCastException _ -> false
140 | None -> assert false (* every element has a parent *)
145 (self#connect#select_begin
146 (fun (elem : Gdome.element option) _ ->
147 if not (same_element root_selected elem) then
149 self#set_selection elem ;
150 selection_changed elem
152 first_selected <- elem)) ;
155 (self#connect#select_over
156 (fun (elem : Gdome.element option) _ ->
158 match first_selected, elem with
159 Some first', Some last' ->
160 (Some (new Gdome.element_of_node (common_ancestor (first' :> Gdome.node) (last' :> Gdome.node))))
163 if not (same_element root_selected new_selected) then
165 self#set_selection new_selected ;
166 selection_changed new_selected
170 (self#connect#select_end
171 (fun (elem : Gdome.element option) _ -> first_selected <- None)) ;
174 (self#connect#select_abort
176 first_selected <- None ;
177 selection_changed None)) ;
179 ignore (self#connect#click (fun _ _ -> self#set_selection None))
183 let single_selection_math_view ?adjustmenth ?adjustmentv ?font_size ?font_manager ?border_width
184 ?width ?height ?packing ?show () =
186 GtkMathView.MathView.create
187 ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
188 ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
191 GtkBase.Container.set w ?border_width ?width ?height;
192 let mathview = GObj.pack_return (new single_selection_math_view w) ~packing ~show in
195 | Some size -> mathview#set_font_size size
199 match font_manager with
200 | Some manager -> mathview#set_font_manager_type ~fm_type:manager
206 class multi_selection_math_view_signals obj (set_selection_changed : (Gdome.element option -> unit) -> unit) =
208 inherit GMathView.math_view_signals obj
209 method selection_changed = set_selection_changed
213 class multi_selection_math_view obj =
215 inherit single_selection_math_view obj
216 val mutable selected : Gdome.element list = []
218 method remove_selection (elem : Gdome.element) =
219 if mem elem selected then
220 selected <- remove elem selected ;
223 method remove_selections =
224 List.iter (fun e -> self#unselect e) selected ;
226 match self#get_selection with
228 | Some e -> self#select e
230 method add_selection (elem : Gdome.element) =
231 selected <- elem::(remove_descendants_of elem selected) ;
234 method get_selections = selected
236 method set_selection elem =
238 match root_selected with
240 | Some e -> self#unselect e ; List.iter (fun e -> self#select e) selected
242 root_selected <- elem;
245 | Some e -> self#select e
249 (self#connect#select_begin
251 if not (List.mem `CONTROL (Gdk.Convert.modifier state)) then self#remove_selections)) ;
254 (self#connect#select_over
256 Printf.printf "stable selections: %d\n" (List.length selected) ;
257 Printf.printf "select_over with state: " ;
262 | `CONTROL -> "control "
266 List.iter (fun x -> print_string (c x)) (Gdk.Convert.modifier state) ;
271 (self#connect#select_end
273 Printf.printf "select_end\n" ; flush stdout ;
274 if not (List.mem `CONTROL (Gdk.Convert.modifier state)) then self#remove_selections ;
275 match root_selected with
277 | Some e -> self#set_selection None ; self#add_selection e)) ;
281 (fun _ _ -> self#remove_selections))
285 let multi_selection_math_view ?adjustmenth ?adjustmentv ?font_size ?font_manager ?border_width
286 ?width ?height ?packing ?show () =
288 GtkMathView.MathView.create
289 ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
290 ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
293 GtkBase.Container.set w ?border_width ?width ?height;
294 let mathview = GObj.pack_return (new multi_selection_math_view w) ~packing ~show in
297 | Some size -> mathview#set_font_size size
301 match font_manager with
302 | Some manager -> mathview#set_font_manager_type ~fm_type:manager