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
164 let activate_t1 mathview () =
165 mathview#set_font_manager_type ~fm_type:`font_manager_t1;
166 print_string "WIDGET SET WITH T1 FONTS\n" ;
170 let activate_gtk mathview () =
171 mathview#set_font_manager_type ~fm_type:`font_manager_gtk ;
172 print_string "WIDGET SET WITH GTK FONTS\n" ;
176 let get_font_manager_type mathview () =
177 print_string "CURRENT FONT MANAGER TYPE: ";
179 match mathview#get_font_manager_type with
180 | `font_manager_t1 -> print_string "T1"
181 | `font_manager_gtk -> print_string "GTK"
187 let get_transparency mathview () =
188 print_string ("CURRENT TRANSPARENCY: " ^
189 (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
193 let set_transparency mathview () =
194 mathview#set_transparency (not mathview#get_transparency) ;
195 print_string "TRANSPARENCY CHANGED\n" ;
200 let load_uri mathview () =
201 mathview#load_uri ~filename:"test.xml" ;
202 print_string "load: SEEMS TO WORK\n" ;
206 let load_doc mathview () =
207 mathview#load_doc ~dom:((Gdome.domImplementation ())#createDocumentFromURI ~uri:"test.xml" ()) ;
208 print_string "load from DOM: SEEMS TO WORK\n" ;
212 let test_get_selection mathview () =
214 match mathview#get_selection with
215 Some element -> element#get_tagName#to_string
216 | None -> "no selection!"
218 print_string ("get_selection: " ^ selection ^ "\n") ;
222 let test_set_selection mathview () =
224 match mathview#get_selection with
227 match element#get_parentNode with
231 mathview#set_selection (Some (new Gdome.element_of_node p));
232 print_string "set selection: SEEMS TO WORK\n"
234 GdomeInit.DOMCastException _ ->
235 print_string "EXCEPTION: no parent\n"
237 | None -> assert false (* every element has a parent *)
240 mathview#set_selection None;
241 print_string "no selection\n"
246 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
247 match mathview#get_selection with
248 Some e -> mathview#add_selection e
251 print_string "no selection to add\n" ;
256 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
257 mathview#set_selection None ;
258 mathview#remove_selections
260 let unload mathview () =
262 print_string "unload: SEEMS TO WORK\n" ;
266 let get_width mathview () =
267 print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
271 let get_height mathview () =
272 print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
276 let get_top mathview () =
277 let (x,y) = mathview#get_top in
278 print_string ("get_top: ("^ string_of_int x ^ "," ^ string_of_int y ^ ")\n") ;
282 let set_top mathview () =
283 mathview#set_top 0 0;
284 print_string "set_top: SEEM TO WORK\n" ;
288 let set_adjustments mathview () =
289 let adj1 = GData.adjustment () in
290 let adj2 = GData.adjustment () in
291 mathview#set_adjustments adj1 adj2 ;
292 adj1#set_value ((adj1#lower +. adj1#upper) /. 2.0) ;
293 adj2#set_value ((adj2#lower +. adj2#upper) /. 2.0) ;
294 print_string "set_adjustments: SEEM TO WORK\n" ;
298 let get_hadjustment mathview () =
299 let adj = mathview#get_hadjustment in
300 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
301 print_string "get_hadjustment: SEEM TO WORK\n" ;
305 let get_vadjustment mathview () =
306 let adj = mathview#get_vadjustment in
307 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
308 print_string "get_vadjustment: SEEM TO WORK\n" ;
312 let get_buffer mathview () =
313 let buffer = mathview#get_buffer in
314 Gdk.Draw.rectangle buffer (Gdk.GC.create buffer) ~x:0 ~y:0
315 ~width:50 ~height:50 ~filled:true () ;
316 print_string "get_buffer: SEEMS TO WORK (hint: force the widget redrawing)\n";
320 let get_frame mathview () =
321 let frame = mathview#get_frame in
322 frame#set_shadow_type `NONE ;
323 print_string "get_frame: SEEMS TO WORK\n" ;
327 let set_font_size mathview () =
328 mathview#set_font_size 24 ;
329 print_string "set_font_size: FONT IS NOW 24\n" ;
333 let get_font_size mathview () =
334 print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
338 let set_anti_aliasing mathview () =
339 mathview#set_anti_aliasing true ;
340 print_string "set_anti_aliasing: ON\n" ;
344 let get_anti_aliasing mathview () =
345 print_string ("get_anti_aliasing: " ^
346 (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
352 let set_kerning mathview () =
353 mathview#set_kerning true ;
354 print_string "set_kerning: ON\n" ;
358 let get_kerning mathview () =
359 print_string ("get_kerning: " ^
360 (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
366 let set_log_verbosity mathview () =
367 mathview#set_log_verbosity 3 ;
368 print_string "set_log_verbosity: NOW IS 3\n" ;
372 let get_log_verbosity mathview () =
373 print_string ("get_log_verbosity: " ^
374 string_of_int mathview#get_log_verbosity ^
379 let export_to_postscript (mathview : GMathViewAux.multi_selection_math_view) () =
380 mathview#export_to_postscript ~filename:"test.ps" ();
381 print_string "expor_to_postscript: SEEMS TO WORK (hint: look at test.ps)\n";
388 let get_element_at mathview () =
390 match mathview#get_element_at !x_coord 10 with
391 None -> print_string ("there is no element at " ^ (string_of_int !x_coord) ^ " 10\n")
392 | Some e -> print_string ("at " ^ (string_of_int !x_coord) ^ " 10 found element " ^ (e#get_nodeName#to_string) ^ "\n")
394 x_coord := !x_coord + 10 ;
398 let get_drawing_area mathview () =
400 let da = mathview#get_drawing_area in
401 print_string ("don't know what to do with the drawing area\n")
406 (* Widget creation *)
407 let main_window = GWindow.window ~title:"GtkMathView test" () in
408 let vbox = GPack.vbox ~packing:main_window#add () in
409 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
410 let mathview= GMathViewAux.multi_selection_math_view ~packing:sw#add ~width:50 ~height:50 () in
411 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
412 let button_gtk=GButton.button ~label:"activate Gtk fonts" ~packing:(table#attach ~left:0 ~top:0) () in
413 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
414 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
415 let button_get_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
416 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
417 let button_add_selection = GButton.button ~label:"add_selection" ~packing:(table#attach ~left:3 ~top:3) () in
418 let button_reset_selections = GButton.button ~label:"reset_selections" ~packing:(table#attach ~left:4 ~top:3) () in
419 let button_get_width = GButton.button ~label:"get_width" ~packing:(table#attach ~left:0 ~top:1) () in
420 let button_get_height = GButton.button ~label:"get_height" ~packing:(table#attach ~left:1 ~top:1) () in
421 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
422 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
423 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
424 let button_get_hadjustment = GButton.button ~label:"get_hadjustment" ~packing:(table#attach ~left:0 ~top:2) () in
425 let button_get_vadjustment = GButton.button ~label:"get_vadjustment" ~packing:(table#attach ~left:1 ~top:2) () in
426 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
427 let button_get_frame = GButton.button ~label:"get_frame" ~packing:(table#attach ~left:3 ~top:2) () in
428 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
429 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
430 let button_set_anti_aliasing = GButton.button ~label:"set_anti_aliasing" ~packing:(table#attach ~left:1 ~top:3) () in
431 let button_get_anti_aliasing = GButton.button ~label:"get_anti_aliasing" ~packing:(table#attach ~left:2 ~top:3) () in
432 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
433 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
434 let button_export_to_postscript = GButton.button ~label:"export_to_postscript" ~packing:(table#attach ~left:2 ~top:4) () in
435 let button_t1 = GButton.button ~label:"activate T1 fonts" ~packing:(table#attach ~left:3 ~top:4) () in
436 let button_get_font_manager_type = GButton.button ~label:"get_font_manager" ~packing:(table#attach ~left:4 ~top:4) () in
437 let button_get_transparency = GButton.button ~label:"get_transparency" ~packing:(table#attach ~left:0 ~top:5) () in
438 let button_set_transparency = GButton.button ~label:"set_transparency" ~packing:(table#attach ~left:1 ~top:5) () in
439 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
440 let button_get_element_at = GButton.button ~label:"get_element_at" ~packing:(table#attach ~left:3 ~top:5) () in
441 let button_get_drawing_area = GButton.button ~label:"get_drawing_area" ~packing:(table#attach ~left:4 ~top:5) () in
442 (* Signals connection *)
443 ignore(button_gtk#connect#clicked (activate_gtk mathview)) ;
444 ignore(button_load#connect#clicked (load_uri mathview)) ;
445 ignore(button_unload#connect#clicked (unload mathview)) ;
446 ignore(button_get_selection#connect#clicked (test_get_selection mathview)) ;
447 ignore(button_set_selection#connect#clicked (test_set_selection mathview)) ;
448 ignore(button_add_selection#connect#clicked (test_add_selection mathview)) ;
449 ignore(button_reset_selections#connect#clicked (test_reset_selections mathview)) ;
450 ignore(button_get_width#connect#clicked (get_width mathview)) ;
451 ignore(button_get_height#connect#clicked (get_height mathview)) ;
452 ignore(button_get_top#connect#clicked (get_top mathview)) ;
453 ignore(button_set_top#connect#clicked (set_top mathview)) ;
454 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
455 ignore(button_get_hadjustment#connect#clicked (get_hadjustment mathview)) ;
456 ignore(button_get_vadjustment#connect#clicked (get_vadjustment mathview)) ;
457 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
458 ignore(button_get_frame#connect#clicked (get_frame mathview)) ;
459 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
460 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
461 ignore(button_set_anti_aliasing#connect#clicked (set_anti_aliasing mathview)) ;
462 ignore(button_get_anti_aliasing#connect#clicked (get_anti_aliasing mathview)) ;
463 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
464 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
465 ignore(button_export_to_postscript#connect#clicked (export_to_postscript mathview)) ;
466 ignore(button_t1#connect#clicked (activate_t1 mathview)) ;
467 ignore(button_get_font_manager_type#connect#clicked (get_font_manager_type mathview)) ;
468 ignore(button_get_transparency#connect#clicked (get_transparency mathview)) ;
469 ignore(button_set_transparency#connect#clicked (set_transparency mathview)) ;
470 ignore(mathview#connect#click (click mathview)) ;
471 ignore(mathview#connect#selection_changed (selection_changed mathview));
472 ignore(mathview#connect#element_over (element_over mathview)) ;
473 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
474 ignore(button_get_element_at#connect#clicked (get_element_at mathview)) ;
475 ignore(button_get_drawing_area#connect#clicked (get_drawing_area mathview)) ;
477 main_window#show () ;