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 (******************************************************************************)
33 let selection_changed mathview (element : Gdome.element option) =
34 print_endline ("selection_changed: " ^
36 None -> "selection_changed on nothing"
37 | Some element -> element#get_tagName#to_string
40 mathview#set_selection element;
44 let element_over mathview (element : Gdome.element option) _ =
45 print_endline ("element_over: " ^
47 None -> "element_over on nothing"
48 | Some element -> element#get_tagName#to_string
54 let rec jump (element : Gdome.element) =
55 let module G = Gdome in
56 let attr = (element#getAttribute ~name:(G.domString "href"))#to_string in
58 match element#get_parentNode with
62 jump (new Gdome.element_of_node p)
64 GdomeInit.DOMCastException _ ->
65 print_string "jump: NO HREF FOR THIS NODE\n" ;
69 | None -> assert false (* every element has a parent *)
72 print_endline ("jump: " ^ attr) ;
78 let rec action mathview (element : Gdome.element) =
79 let module G = Gdome in
80 if element#get_tagName#to_string = "m:maction" then
82 if element#hasAttribute ~name:(G.domString "selection") then
83 int_of_string (element#getAttribute ~name:(G.domString "selection"))#to_string
88 (* the widget will cast the index back into a reasonable range *)
89 element#setAttribute ~name:(G.domString "selection") ~value:(G.domString (string_of_int (selection + 1))) ;
93 match element#get_parentNode with
97 action mathview (new Gdome.element_of_node p)
99 GdomeInit.DOMCastException _ ->
100 print_string "action: NO MACTION FOUND\n" ;
104 | None -> assert false (* every element has a parent *)
106 let click mathview (element : Gdome.element option) _ =
107 let module G = Gdome in
109 None -> print_string "CLICKED ON NOTHING\n" ; flush stdout
111 if not (jump element) then
112 if not (mathview#action_toggle element) then
117 let activate_t1 mathview () =
118 mathview#set_font_manager_type ~fm_type:`font_manager_t1;
119 print_string "WIDGET SET WITH T1 FONTS\n" ;
123 let activate_gtk mathview () =
124 mathview#set_font_manager_type ~fm_type:`font_manager_gtk ;
125 print_string "WIDGET SET WITH GTK FONTS\n" ;
129 let get_font_manager_type mathview () =
130 print_string "CURRENT FONT MANAGER TYPE: ";
132 match mathview#get_font_manager_type with
133 | `font_manager_t1 -> print_string "T1"
134 | `font_manager_gtk -> print_string "GTK"
140 let get_transparency mathview () =
141 print_string ("CURRENT TRANSPARENCY: " ^
142 (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
146 let set_transparency mathview () =
147 mathview#set_transparency (not mathview#get_transparency) ;
148 print_string "TRANSPARENCY CHANGED\n" ;
153 let load_uri mathview () =
154 mathview#load_uri ~filename:"test.xml" ;
155 print_string "load: SEEMS TO WORK\n" ;
159 let load_doc mathview () =
160 mathview#load_doc ~dom:((Gdome.domImplementation ())#createDocumentFromURI ~uri:"test.xml" ()) ;
161 print_string "load from DOM: SEEMS TO WORK\n" ;
165 let test_get_selection mathview () =
167 match mathview#get_selection with
168 Some element -> element#get_tagName#to_string
169 | None -> "no selection!"
171 print_string ("get_selection: " ^ selection ^ "\n") ;
175 let test_set_selection mathview () =
177 match mathview#get_selection with
180 match element#get_parentNode with
184 mathview#set_selection (Some (new Gdome.element_of_node p));
185 print_string "set selection: SEEMS TO WORK\n"
187 GdomeInit.DOMCastException _ ->
188 print_string "EXCEPTION: no parent\n"
190 | None -> assert false (* every element has a parent *)
193 mathview#set_selection None;
194 print_string "no selection\n"
199 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
200 match mathview#get_selection with
201 Some e -> mathview#add_selection e
204 print_string "no selection to add\n" ;
209 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
210 mathview#set_selection None ;
211 mathview#remove_selections
213 let unload mathview () =
215 print_string "unload: SEEMS TO WORK\n" ;
219 let get_width mathview () =
220 print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
224 let get_height mathview () =
225 print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
229 let get_top mathview () =
230 let (x,y) = mathview#get_top in
231 print_string ("get_top: ("^ string_of_int x ^ "," ^ string_of_int y ^ ")\n") ;
235 let set_top mathview () =
236 mathview#set_top 0 0;
237 print_string "set_top: SEEM TO WORK\n" ;
241 let set_adjustments mathview () =
242 let adj1 = GData.adjustment () in
243 let adj2 = GData.adjustment () in
244 mathview#set_adjustments adj1 adj2 ;
245 adj1#set_value ((adj1#lower +. adj1#upper) /. 2.0) ;
246 adj2#set_value ((adj2#lower +. adj2#upper) /. 2.0) ;
247 print_string "set_adjustments: SEEM TO WORK\n" ;
251 let get_hadjustment mathview () =
252 let adj = mathview#get_hadjustment in
253 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
254 print_string "get_hadjustment: SEEM TO WORK\n" ;
258 let get_vadjustment mathview () =
259 let adj = mathview#get_vadjustment in
260 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
261 print_string "get_vadjustment: SEEM TO WORK\n" ;
265 let get_buffer mathview () =
266 let buffer = mathview#get_buffer in
267 Gdk.Draw.rectangle buffer (Gdk.GC.create buffer) ~x:0 ~y:0
268 ~width:50 ~height:50 ~filled:true () ;
269 print_string "get_buffer: SEEMS TO WORK (hint: force the widget redrawing)\n";
273 let get_frame mathview () =
274 let frame = mathview#get_frame in
275 frame#set_shadow_type `NONE ;
276 print_string "get_frame: SEEMS TO WORK\n" ;
280 let set_font_size mathview () =
281 mathview#set_font_size 24 ;
282 print_string "set_font_size: FONT IS NOW 24\n" ;
286 let get_font_size mathview () =
287 print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
291 let set_anti_aliasing mathview () =
292 mathview#set_anti_aliasing true ;
293 print_string "set_anti_aliasing: ON\n" ;
297 let get_anti_aliasing mathview () =
298 print_string ("get_anti_aliasing: " ^
299 (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
305 let set_kerning mathview () =
306 mathview#set_kerning true ;
307 print_string "set_kerning: ON\n" ;
311 let get_kerning mathview () =
312 print_string ("get_kerning: " ^
313 (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
319 let set_log_verbosity mathview () =
320 mathview#set_log_verbosity 3 ;
321 print_string "set_log_verbosity: NOW IS 3\n" ;
325 let get_log_verbosity mathview () =
326 print_string ("get_log_verbosity: " ^
327 string_of_int mathview#get_log_verbosity ^
332 let export_to_postscript (mathview : GMathViewAux.multi_selection_math_view) () =
333 mathview#export_to_postscript ~filename:"test.ps" ();
334 print_string "expor_to_postscript: SEEMS TO WORK (hint: look at test.ps)\n";
341 let get_element_at mathview () =
343 match mathview#get_element_at !x_coord 10 with
344 None -> print_string ("there is no element at " ^ (string_of_int !x_coord) ^ " 10\n")
345 | Some e -> print_string ("at " ^ (string_of_int !x_coord) ^ " 10 found element " ^ (e#get_nodeName#to_string) ^ "\n")
347 x_coord := !x_coord + 10 ;
351 let get_drawing_area mathview () =
353 let da = mathview#get_drawing_area in
354 print_string ("don't know what to do with the drawing area\n")
359 (* Widget creation *)
360 let main_window = GWindow.window ~title:"GtkMathView test" () in
361 let vbox = GPack.vbox ~packing:main_window#add () in
362 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
363 let mathview= GMathViewAux.multi_selection_math_view ~packing:sw#add ~width:50 ~height:50 () in
364 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
365 let button_gtk=GButton.button ~label:"activate Gtk fonts" ~packing:(table#attach ~left:0 ~top:0) () in
366 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
367 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
368 let button_get_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
369 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
370 let button_add_selection = GButton.button ~label:"add_selection" ~packing:(table#attach ~left:3 ~top:3) () in
371 let button_reset_selections = GButton.button ~label:"reset_selections" ~packing:(table#attach ~left:4 ~top:3) () in
372 let button_get_width = GButton.button ~label:"get_width" ~packing:(table#attach ~left:0 ~top:1) () in
373 let button_get_height = GButton.button ~label:"get_height" ~packing:(table#attach ~left:1 ~top:1) () in
374 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
375 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
376 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
377 let button_get_hadjustment = GButton.button ~label:"get_hadjustment" ~packing:(table#attach ~left:0 ~top:2) () in
378 let button_get_vadjustment = GButton.button ~label:"get_vadjustment" ~packing:(table#attach ~left:1 ~top:2) () in
379 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
380 let button_get_frame = GButton.button ~label:"get_frame" ~packing:(table#attach ~left:3 ~top:2) () in
381 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
382 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
383 let button_set_anti_aliasing = GButton.button ~label:"set_anti_aliasing" ~packing:(table#attach ~left:1 ~top:3) () in
384 let button_get_anti_aliasing = GButton.button ~label:"get_anti_aliasing" ~packing:(table#attach ~left:2 ~top:3) () in
385 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
386 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
387 let button_export_to_postscript = GButton.button ~label:"export_to_postscript" ~packing:(table#attach ~left:2 ~top:4) () in
388 let button_t1 = GButton.button ~label:"activate T1 fonts" ~packing:(table#attach ~left:3 ~top:4) () in
389 let button_get_font_manager_type = GButton.button ~label:"get_font_manager" ~packing:(table#attach ~left:4 ~top:4) () in
390 let button_get_transparency = GButton.button ~label:"get_transparency" ~packing:(table#attach ~left:0 ~top:5) () in
391 let button_set_transparency = GButton.button ~label:"set_transparency" ~packing:(table#attach ~left:1 ~top:5) () in
392 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
393 let button_get_element_at = GButton.button ~label:"get_element_at" ~packing:(table#attach ~left:3 ~top:5) () in
394 let button_get_drawing_area = GButton.button ~label:"get_drawing_area" ~packing:(table#attach ~left:4 ~top:5) () in
395 (* Signals connection *)
396 ignore(button_gtk#connect#clicked (activate_gtk mathview)) ;
397 ignore(button_load#connect#clicked (load_uri mathview)) ;
398 ignore(button_unload#connect#clicked (unload mathview)) ;
399 ignore(button_get_selection#connect#clicked (test_get_selection mathview)) ;
400 ignore(button_set_selection#connect#clicked (test_set_selection mathview)) ;
401 ignore(button_add_selection#connect#clicked (test_add_selection mathview)) ;
402 ignore(button_reset_selections#connect#clicked (test_reset_selections mathview)) ;
403 ignore(button_get_width#connect#clicked (get_width mathview)) ;
404 ignore(button_get_height#connect#clicked (get_height mathview)) ;
405 ignore(button_get_top#connect#clicked (get_top mathview)) ;
406 ignore(button_set_top#connect#clicked (set_top mathview)) ;
407 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
408 ignore(button_get_hadjustment#connect#clicked (get_hadjustment mathview)) ;
409 ignore(button_get_vadjustment#connect#clicked (get_vadjustment mathview)) ;
410 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
411 ignore(button_get_frame#connect#clicked (get_frame mathview)) ;
412 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
413 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
414 ignore(button_set_anti_aliasing#connect#clicked (set_anti_aliasing mathview)) ;
415 ignore(button_get_anti_aliasing#connect#clicked (get_anti_aliasing mathview)) ;
416 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
417 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
418 ignore(button_export_to_postscript#connect#clicked (export_to_postscript mathview)) ;
419 ignore(button_t1#connect#clicked (activate_t1 mathview)) ;
420 ignore(button_get_font_manager_type#connect#clicked (get_font_manager_type mathview)) ;
421 ignore(button_get_transparency#connect#clicked (get_transparency mathview)) ;
422 ignore(button_set_transparency#connect#clicked (set_transparency mathview)) ;
423 ignore(mathview#connect#click (click mathview)) ;
424 ignore(mathview#connect#selection_changed (selection_changed mathview));
425 ignore(mathview#connect#element_over (element_over mathview)) ;
426 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
427 ignore(button_get_element_at#connect#clicked (get_element_at mathview)) ;
428 ignore(button_get_drawing_area#connect#clicked (get_drawing_area mathview)) ;
430 main_window#show () ;