1 (* Copyright (C) 2000-2003, Luca Padovani <luca.padovani@cs.unibo.it>,
2 * Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>.
4 * This file is part of lablgtkmathview, the Ocaml binding
5 * for the GtkMathView widget.
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.
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.
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.
21 * For details, send a mail to the author.
24 (******************************************************************************)
25 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
28 (* This is a simple test for the OCaml (LablGtk indeed) binding of the *)
30 (******************************************************************************)
32 let helmns = Gdome.domString "http://www.cs.unibo.it/helm";;
35 let choose_selection mmlwidget (element : Gdome.element option) =
36 let module G = Gdome in
38 if element#hasAttributeNS
39 ~namespaceURI:Misc.helmns
40 ~localName:(G.domString "xref")
42 mmlwidget#set_selection (Some element)
45 match element#get_parentNode with
47 (*CSC: OCAML DIVERGES!
48 | Some p -> aux (new G.element_of_node p)
50 | Some p -> aux (new Gdome.element_of_node p)
52 GdomeInit.DOMCastException _ ->
54 "******* trying to select above the document root ********"
58 | None -> mmlwidget#set_selection None
63 let selection_changed mathview (element : Gdome.element option) =
65 if element#hasAttributeNS
67 ~localName:(Gdome.domString "xref")
69 mathview#set_selection (Some element)
72 match element#get_parentNode with
73 None -> mathview#set_selection None
74 | Some p -> aux (new Gdome.element_of_node p)
76 GdomeInit.DOMCastException _ ->
77 prerr_endline "******* trying to select above the document root ********"
79 print_endline ("selection_changed: " ^
81 None -> "selection_changed on nothing"
82 | Some element -> element#get_tagName#to_string
91 let element_over mathview ((element : Gdome.element option), _, _, _) =
92 print_endline ("element_over: " ^
94 None -> "element_over on nothing"
95 | Some element -> element#get_tagName#to_string
101 let rec jump (element : Gdome.element) =
102 let module G = Gdome in
103 let attr = (element#getAttribute ~name:(G.domString "href"))#to_string in
105 match element#get_parentNode with
109 jump (new Gdome.element_of_node p)
111 GdomeInit.DOMCastException _ ->
112 print_string "jump: NO HREF FOR THIS NODE\n" ;
116 | None -> assert false (* every element has a parent *)
119 print_endline ("jump: " ^ attr) ;
125 let rec action mathview (element : Gdome.element) =
126 let module G = Gdome in
127 if element#get_tagName#to_string = "m:maction" then
129 if element#hasAttribute ~name:(G.domString "selection") then
130 int_of_string (element#getAttribute ~name:(G.domString "selection"))#to_string
135 (* the widget will cast the index back into a reasonable range *)
136 element#setAttribute ~name:(G.domString "selection") ~value:(G.domString (string_of_int (selection + 1))) ;
140 match element#get_parentNode with
144 action mathview (new Gdome.element_of_node p)
146 GdomeInit.DOMCastException _ ->
147 print_string "action: NO MACTION FOUND\n" ;
151 | None -> assert false (* every element has a parent *)
153 let click mathview ((element : Gdome.element option), _, _, _) =
154 let module G = Gdome in
156 None -> print_string "CLICKED ON NOTHING\n" ; flush stdout
158 if not (jump element) then
159 if not (mathview#action_toggle element) then
163 let load_uri mathview () =
164 mathview#load_uri ~filename:"test.xml" ;
165 print_string "load: SEEMS TO WORK\n" ;
169 let get_document mathview () =
170 (match mathview#get_document with
171 | None -> print_string "no document loaded\n"
173 let name = "out.xml" in
174 ignore ((Gdome.domImplementation ())#saveDocumentToFile ~doc ~name ());
175 print_string ("document loaded and saved to " ^ name ^ "\n"));
179 let load_doc mathview () =
180 mathview#load_root ~root:(((Gdome.domImplementation ())#createDocumentFromURI ~uri:"test.xml" ())#get_documentElement) ;
181 print_string "load from DOM: SEEMS TO WORK\n" ;
185 let test_get_selection mathview () =
187 match mathview#get_selection with
188 Some element -> element#get_tagName#to_string
189 | None -> "no selection! but there are " ^ (string_of_int (List.length mathview#get_selections)) ^ " multiple selections!"
191 print_string ("selection: " ^ selection ^ "\n") ;
195 let test_set_selection mathview () =
197 match mathview#get_selection with
200 match element#get_parentNode with
204 mathview#set_selection (Some (new Gdome.element_of_node p));
205 print_string "set selection: SEEMS TO WORK\n"
207 GdomeInit.DOMCastException _ ->
208 print_string "EXCEPTION: no parent\n"
210 | None -> assert false (* every element has a parent *)
213 mathview#set_selection None;
214 print_string "no selection\n"
219 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
220 match mathview#get_selection with
221 Some e -> mathview#add_selection e
224 print_string "no selection to add\n" ;
229 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
230 mathview#set_selection None ;
231 mathview#remove_selections
233 let select_over (mathview : GMathViewAux.multi_selection_math_view) =
234 (fun (_,_,_,state) ->
238 | `CONTROL -> "control"
244 (List.filter (fun s -> s <> "")
245 (List.map c (Gdk.Convert.modifier state)))
247 if msg <> "" then begin
248 print_endline ("modifiers: " ^ msg);
252 let unload mathview () =
254 print_string "unload: SEEMS TO WORK\n" ;
258 let get_size mathview () =
259 let width, height = mathview#get_size in
260 print_string ("width: " ^ string_of_int width ^ ", height: " ^ string_of_int height ^ "\n") ;
264 let get_top mathview () =
265 let (x,y) = mathview#get_top in
266 print_string ("top: ("^ string_of_int x ^ "," ^ string_of_int y ^ ")\n") ;
270 let set_top mathview () =
271 mathview#set_top 0 0;
272 print_string "set_top: SEEM TO WORK\n" ;
276 let set_adjustments mathview () =
277 let adj1 = GData.adjustment () in
278 let adj2 = GData.adjustment () in
279 mathview#set_adjustments adj1 adj2 ;
280 adj1#set_value ((adj1#lower +. adj1#upper) /. 2.0) ;
281 adj2#set_value ((adj2#lower +. adj2#upper) /. 2.0) ;
282 print_string "set_adjustments: SEEM TO WORK\n" ;
286 let get_adjustments mathview () =
287 let hadj, vadj = mathview#get_adjustments in
288 hadj#set_value ((hadj#lower +. hadj#upper) /. 2.0) ;
289 vadj#set_value ((vadj#lower +. vadj#upper) /. 2.0) ;
290 print_string "hadjustment: SEEM TO WORK\n" ;
294 let get_buffer mathview () =
295 let buffer = mathview#get_buffer in
296 Gdk.Draw.rectangle buffer (Gdk.GC.create buffer) ~x:0 ~y:0
297 ~width:50 ~height:50 ~filled:true () ;
298 print_string "buffer: SEEMS TO WORK (hint: force the widget redrawing)\n";
302 let set_font_size mathview () =
303 mathview#set_font_size 24 ;
304 print_string "set_font_size: FONT IS NOW 24\n" ;
308 let get_font_size mathview () =
309 print_string ("font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
313 let set_log_verbosity mathview () =
314 mathview#set_log_verbosity 3 ;
315 print_string "set_log_verbosity: NOW IS 3\n" ;
319 let get_log_verbosity mathview () =
320 print_string ("log_verbosity: " ^
321 string_of_int mathview#get_log_verbosity ^
330 let get_element_at mathview () =
332 match mathview#get_element_at !x_coord 10 with
333 None -> print_string ("there is no element at " ^ (string_of_int !x_coord) ^ " 10\n")
334 | Some e -> print_string ("at " ^ (string_of_int !x_coord) ^ " 10 found element " ^ (e#get_nodeName#to_string) ^ "\n")
336 x_coord := !x_coord + 10 ;
341 let _ = (GtkMain.Main.init ())
344 (* Widget creation *)
345 let main_window = GWindow.window ~title:"GtkMathView test" () in
346 let vbox = GPack.vbox ~packing:main_window#add () in
347 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
348 let mathview= GMathViewAux.multi_selection_math_view ~packing:sw#add ~width:50 ~height:50 () in
349 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
350 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
351 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
352 let button_get_document = GButton.button ~label:"get_document" ~packing:(table#attach ~left:1 ~top:1) () in
353 let button_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
354 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
355 let button_add_selection = GButton.button ~label:"add_selection" ~packing:(table#attach ~left:3 ~top:3) () in
356 let button_reset_selections = GButton.button ~label:"reset_selections" ~packing:(table#attach ~left:4 ~top:3) () in
357 let button_get_size = GButton.button ~label:"get_size" ~packing:(table#attach ~left:0 ~top:1) () in
358 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
359 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
360 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
361 let button_get_adjustments = GButton.button ~label:"get_adjustments" ~packing:(table#attach ~left:0 ~top:2) () in
362 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
363 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
364 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
365 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
366 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
367 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
368 (* let button_get_element_at = GButton.button ~label:"get_element_at" ~packing:(table#attach ~left:3 ~top:5) () in *)
369 (* Signals connection *)
370 ignore(button_load#connect#clicked (load_uri mathview)) ;
371 ignore(button_unload#connect#clicked (unload mathview)) ;
372 ignore(button_get_document#connect#clicked (get_document mathview)) ;
373 ignore(button_selection#connect#clicked (test_get_selection mathview)) ;
374 ignore(button_set_selection#connect#clicked (test_set_selection mathview)) ;
375 ignore(button_add_selection#connect#clicked (test_add_selection mathview)) ;
376 ignore(button_reset_selections#connect#clicked (test_reset_selections mathview)) ;
377 ignore(button_get_size#connect#clicked (get_size mathview)) ;
378 ignore(button_get_top#connect#clicked (get_top mathview)) ;
379 ignore(button_set_top#connect#clicked (set_top mathview)) ;
380 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
381 ignore(button_get_adjustments#connect#clicked (get_adjustments mathview)) ;
382 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
383 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
384 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
385 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
386 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
387 ignore(mathview#connect#click (click mathview)) ;
388 ignore(mathview#connect#selection_changed (selection_changed mathview));
389 ignore(mathview#connect#element_over (element_over mathview)) ;
390 ignore(mathview#connect#select_over (select_over mathview));
391 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
392 ignore(main_window#connect#destroy (fun _ -> GMain.quit ()));
393 (* ignore(button_get_element_at#connect#clicked (get_element_at mathview)) ; *)
395 main_window#show () ;