]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/gMathViewAux.ml
d59732cb25231dc13341890c894f589e7f6ee06f
[helm.git] / helm / DEVEL / lablgtkmathview / gMathViewAux.ml
1 (* Copyright (C) 2000-2003, Luca Padovani <luca.padovani@cs.unibo.it>,
2  *                          Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>.
3  *
4  * This file is part of lablgtkmathview, the Ocaml binding
5  * for the GtkMathView widget.
6  * 
7  * lablgtkmathview is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * lablgtkmathview is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with lablgtkmathview; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  * 
21  * For details, send a mail to the author.
22  *)
23
24 (* finds the common node ancestor of two nodes *)
25 let common_ancestor (first : Gdome.node) (last : Gdome.node) =
26  let rec path n =
27   match n#get_parentNode with
28      None -> [n]
29    | Some p -> n::(path p)
30  in
31   let rec last_common =
32    function
33       _, hd1::tl1, hd2::tl2 when hd1#isSameNode hd2 -> (last_common ((Some hd1),tl1,tl2))
34     | Some e, _, _ -> e
35     | _,_,_ -> assert false
36   in
37    (last_common (None,(List.rev (path first)),(List.rev (path last))))
38  
39 let same_element (e1 : Gdome.element option) (e2 : Gdome.element option) =
40  match e1, e2 with
41     None, None -> true
42   | Some e1, Some e2 when (e1 :> Gdome.node)#isSameNode (e2 :> Gdome.node) -> true
43   | _ -> false
44         
45 (* true if n1 is n2 or one of n2's descendants *)
46 let rec descendant_of (n1 : Gdome.node) (n2 : Gdome.node) =
47  if n1#isSameNode n2 then true
48  else
49   match n1#get_parentNode with
50      None -> false
51    | Some n1' -> descendant_of n1' n2
52
53 let remove_descendants_of (el : Gdome.element) =
54  let rec aux =
55   function
56      [] -> []
57    | hd::tl when descendant_of (hd :> Gdome.node) (el :> Gdome.node) -> aux tl
58    | hd::tl -> hd::(aux tl)
59  in
60   aux
61
62 (* mem el l = true if the node n is stored in the list l *)
63 let mem (el : Gdome.element) =
64  let rec mem_aux =
65   function
66      hd::_ when (hd :> Gdome.node)#isSameNode (el :> Gdome.node) -> true
67    | _::tl -> mem_aux tl
68    | _ -> false
69  in
70   mem_aux
71
72 (* remove el l = l' where l' has the same nodes as l except that all
73  * the occurrences of n have been removed *)
74 let remove (el : Gdome.element) =
75  let rec remove_aux =
76   function
77      hd::tl when (hd :> Gdome.node)#isSameNode (el :> Gdome.node) ->
78       remove_aux tl
79    | hd::tl -> hd::(remove_aux tl)
80    | [] -> []
81  in
82   remove_aux
83
84 class single_selection_math_view_signals obj (set_selection_changed : (Gdome.element option -> unit) -> unit) =
85  object
86   inherit GMathView.math_view_signals obj
87   method selection_changed = set_selection_changed
88  end
89 ;;
90
91 class single_selection_math_view obj =
92   object(self)
93    inherit GMathView.math_view_skel obj
94    val mutable first_selected = None
95    val mutable root_selected = None
96    val mutable selection_changed = (fun _ -> ())
97
98    method set_selection elem =
99     self#freeze ;
100     begin
101      match root_selected with
102         None -> ()
103       | Some e -> self#unselect e
104     end;
105     root_selected <- elem ;
106     begin
107      match elem with
108         None -> ()
109       | Some e -> self#select e
110     end ;
111     self#thaw
112
113    method get_selection = root_selected
114
115    method connect =
116     new
117      single_selection_math_view_signals obj
118       (function f -> selection_changed <- f)
119
120    method action_toggle (elem : Gdome.element) =
121     match elem#get_namespaceURI, elem#get_localName with
122        Some ns, Some ln
123         when ns#to_string = "http://www.w3.org/1998/Math/MathML" &&
124          ln#to_string = "maction"
125        ->
126         begin
127          let selection_attr = Gdome.domString "selection" in
128          let selection =
129           if elem#hasAttribute ~name:selection_attr then
130            int_of_string (elem#getAttribute ~name:selection_attr)#to_string
131           else
132            1
133          in
134           self#freeze ;
135           (* the widget will cast the index back into a valid range *)
136           elem#setAttribute ~name:selection_attr
137            ~value:(Gdome.domString (string_of_int (selection + 1))) ;
138           self#thaw ;
139           true
140         end
141      | _ ->
142         begin
143          match elem#get_parentNode with
144             Some p ->
145              begin
146               try
147                self#action_toggle (new Gdome.element_of_node p)
148               with
149                GdomeInit.DOMCastException _ -> false
150              end
151           | None -> assert false (* every element has a parent *)
152         end
153      
154    initializer
155     selection_changed <- self#set_selection ;
156
157     ignore
158      (self#connect#select_begin
159        (fun (elem : Gdome.element option) _ ->
160          if not (same_element root_selected elem) then selection_changed elem ;
161          first_selected <- elem)) ;
162
163     ignore
164      (self#connect#select_over
165        (fun (elem : Gdome.element option) _ ->
166          let new_selected =
167           match first_selected, elem with
168              Some first', Some last' ->
169               (Some
170                (new Gdome.element_of_node
171                 (common_ancestor (first' :> Gdome.node) (last' :> Gdome.node))))
172            | _ -> None
173          in
174           if not (same_element root_selected new_selected) then
175             selection_changed new_selected)) ;
176              
177     ignore
178      (self#connect#select_end
179        (fun (elem : Gdome.element option) _ -> first_selected <- None)) ;
180
181     ignore
182      (self#connect#select_abort
183        (fun () ->
184          first_selected <- None ;
185          selection_changed None)) ;
186
187     ignore (self#connect#click (fun _ _ -> self#set_selection None))
188   end
189 ;;
190
191 let single_selection_math_view ?hadjustment ?vadjustment ?font_size ?log_verbosity =
192   GtkBase.Container.make_params ~cont:(
193   OgtkMathViewProps.pack_return
194     (fun p -> OgtkMathViewProps.set_params (new single_selection_math_view (GtkMathViewProps.MathView.create p)) ~font_size ~log_verbosity)) []
195 ;;
196
197 class multi_selection_math_view_signals obj
198  (set_selection_changed : (Gdome.element option -> unit) -> unit)
199 =
200  object
201   inherit GMathView.math_view_signals obj
202   method selection_changed = set_selection_changed
203  end
204 ;;
205
206 class multi_selection_math_view obj =
207   object(self)
208    inherit single_selection_math_view obj
209    val mutable selected : Gdome.element list = []
210
211    method remove_selection (elem : Gdome.element) =
212     if mem elem selected then
213      selected <- remove elem selected ;
214      self#unselect elem
215
216    method remove_selections =
217     self#freeze ;
218     List.iter (fun e -> self#unselect e) selected ;
219     selected <- [] ;
220     begin
221      match self#get_selection with
222         None -> ()
223       | Some e -> self#select e
224     end ;
225     self#thaw
226
227    method add_selection (elem : Gdome.element) =
228     selected <- elem::(remove_descendants_of elem selected) ;
229     self#select elem
230
231    method get_selections = selected
232
233    method set_selection elem =
234     self#freeze ;
235     begin
236      match root_selected with
237         None -> ()
238       | Some e -> self#unselect e ; List.iter (fun e -> self#select e) selected
239     end;
240     root_selected <- elem;
241     begin
242      match elem with
243         None -> ()
244       | Some e -> self#select e
245     end ;
246     self#thaw
247
248    initializer
249     ignore
250      (self#connect#select_begin
251        (fun _ state ->
252          if not (List.mem `CONTROL (Gdk.Convert.modifier state)) then
253           self#remove_selections)) ;
254
255     ignore
256      (self#connect#select_over
257        (fun _ state ->
258          let c = 
259           function
260              `SHIFT -> "shift "
261            | `LOCK -> "lock "
262            | `CONTROL -> "control "
263            | `MOD1 -> "mod1 "
264            | _ -> ""
265          in
266           List.iter (fun x -> print_string (c x)) (Gdk.Convert.modifier state) ;
267           print_char '\n' ;
268           flush stdout)) ;
269
270     ignore
271      (self#connect#select_end
272        (fun _ state ->
273          if not (List.mem `CONTROL (Gdk.Convert.modifier state)) then
274           self#remove_selections ;
275          match root_selected with
276             None -> ()
277          | Some e -> self#set_selection None ; self#add_selection e)) ;
278
279     ignore
280      (self#connect#click
281        (fun _ _ -> self#remove_selections))
282    end
283  ;;
284
285 let multi_selection_math_view ?hadjustment ?vadjustment ?font_size ?log_verbosity =
286   GtkBase.Container.make_params ~cont:(
287   OgtkMathViewProps.pack_return
288     (fun p -> OgtkMathViewProps.set_params (new multi_selection_math_view (GtkMathViewProps.MathView.create p)) ~font_size ~log_verbosity)) []
289 ;;