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) _ =
107 let module G = Gdome in
108 if not (jump element) then
109 if not (mathview#action_toggle element) then
114 let activate_t1 mathview () =
115 mathview#set_font_manager_type ~fm_type:`font_manager_t1;
116 print_string "WIDGET SET WITH T1 FONTS\n" ;
120 let activate_gtk mathview () =
121 mathview#set_font_manager_type ~fm_type:`font_manager_gtk ;
122 print_string "WIDGET SET WITH GTK FONTS\n" ;
126 let get_font_manager_type mathview () =
127 print_string "CURRENT FONT MANAGER TYPE: ";
129 match mathview#get_font_manager_type with
130 | `font_manager_t1 -> print_string "T1"
131 | `font_manager_gtk -> print_string "GTK"
137 let get_transparency mathview () =
138 print_string ("CURRENT TRANSPARENCY: " ^
139 (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
143 let set_transparency mathview () =
144 mathview#set_transparency (not mathview#get_transparency) ;
145 print_string "TRANSPARENCY CHANGED\n" ;
150 let load_uri mathview () =
151 mathview#load_uri ~filename:"test.xml" ;
152 print_string "load: SEEMS TO WORK\n" ;
156 let load_doc mathview () =
157 mathview#load_doc ~dom:((Gdome.domImplementation ())#createDocumentFromURI ~uri:"test.xml" ()) ;
158 print_string "load from DOM: SEEMS TO WORK\n" ;
162 let test_get_selection mathview () =
164 match mathview#get_selection with
165 Some element -> element#get_tagName#to_string
166 | None -> "no selection!"
168 print_string ("get_selection: " ^ selection ^ "\n") ;
172 let test_set_selection mathview () =
174 match mathview#get_selection with
177 match element#get_parentNode with
181 mathview#set_selection (Some (new Gdome.element_of_node p));
182 print_string "set selection: SEEMS TO WORK\n"
184 GdomeInit.DOMCastException _ ->
185 print_string "EXCEPTION: no parent\n"
187 | None -> assert false (* every element has a parent *)
190 mathview#set_selection None;
191 print_string "no selection\n"
196 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
197 match mathview#get_selection with
198 Some e -> mathview#add_selection e
201 print_string "no selection to add\n" ;
206 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
207 mathview#set_selection None ;
208 mathview#remove_selections
210 let unload mathview () =
212 print_string "unload: SEEMS TO WORK\n" ;
216 let get_width mathview () =
217 print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
221 let get_height mathview () =
222 print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
226 let get_top mathview () =
227 let (x,y) = mathview#get_top in
228 print_string ("get_top: ("^ string_of_int x ^ "," ^ string_of_int y ^ ")\n") ;
232 let set_top mathview () =
233 mathview#set_top 0 0;
234 print_string "set_top: SEEM TO WORK\n" ;
238 let set_adjustments mathview () =
239 let adj1 = GData.adjustment () in
240 let adj2 = GData.adjustment () in
241 mathview#set_adjustments adj1 adj2 ;
242 adj1#set_value ((adj1#lower +. adj1#upper) /. 2.0) ;
243 adj2#set_value ((adj2#lower +. adj2#upper) /. 2.0) ;
244 print_string "set_adjustments: SEEM TO WORK\n" ;
248 let get_hadjustment mathview () =
249 let adj = mathview#get_hadjustment in
250 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
251 print_string "get_hadjustment: SEEM TO WORK\n" ;
255 let get_vadjustment mathview () =
256 let adj = mathview#get_vadjustment in
257 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
258 print_string "get_vadjustment: SEEM TO WORK\n" ;
262 let get_buffer mathview () =
263 let buffer = mathview#get_buffer in
264 Gdk.Draw.rectangle buffer (Gdk.GC.create buffer) ~x:0 ~y:0
265 ~width:50 ~height:50 ~filled:true () ;
266 print_string "get_buffer: SEEMS TO WORK (hint: force the widget redrawing)\n";
270 let get_frame mathview () =
271 let frame = mathview#get_frame in
272 frame#set_shadow_type `NONE ;
273 print_string "get_frame: SEEMS TO WORK\n" ;
277 let set_font_size mathview () =
278 mathview#set_font_size 24 ;
279 print_string "set_font_size: FONT IS NOW 24\n" ;
283 let get_font_size mathview () =
284 print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
288 let set_anti_aliasing mathview () =
289 mathview#set_anti_aliasing true ;
290 print_string "set_anti_aliasing: ON\n" ;
294 let get_anti_aliasing mathview () =
295 print_string ("get_anti_aliasing: " ^
296 (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
302 let set_kerning mathview () =
303 mathview#set_kerning true ;
304 print_string "set_kerning: ON\n" ;
308 let get_kerning mathview () =
309 print_string ("get_kerning: " ^
310 (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
316 let set_log_verbosity mathview () =
317 mathview#set_log_verbosity 3 ;
318 print_string "set_log_verbosity: NOW IS 3\n" ;
322 let get_log_verbosity mathview () =
323 print_string ("get_log_verbosity: " ^
324 string_of_int mathview#get_log_verbosity ^
329 let export_to_postscript (mathview : GMathViewAux.multi_selection_math_view) () =
330 mathview#export_to_postscript ~filename:"test.ps" ();
331 print_string "expor_to_postscript: SEEMS TO WORK (hint: look at test.ps)\n";
338 let get_element_at mathview () =
340 match mathview#get_element_at !x_coord 10 with
341 None -> print_string ("there is no element at " ^ (string_of_int !x_coord) ^ " 10\n")
342 | Some e -> print_string ("at " ^ (string_of_int !x_coord) ^ " 10 found element " ^ (e#get_nodeName#to_string) ^ "\n")
344 x_coord := !x_coord + 10 ;
348 let get_drawing_area mathview () =
350 let da = mathview#get_drawing_area in
351 print_string ("don't know what to do with the drawing area\n")
356 (* Widget creation *)
357 let main_window = GWindow.window ~title:"GtkMathView test" () in
358 let vbox = GPack.vbox ~packing:main_window#add () in
359 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
360 let mathview= GMathViewAux.multi_selection_math_view ~packing:sw#add ~width:50 ~height:50 () in
361 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
362 let button_gtk=GButton.button ~label:"activate Gtk fonts" ~packing:(table#attach ~left:0 ~top:0) () in
363 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
364 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
365 let button_get_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
366 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
367 let button_add_selection = GButton.button ~label:"add_selection" ~packing:(table#attach ~left:3 ~top:3) () in
368 let button_reset_selections = GButton.button ~label:"reset_selections" ~packing:(table#attach ~left:4 ~top:3) () in
369 let button_get_width = GButton.button ~label:"get_width" ~packing:(table#attach ~left:0 ~top:1) () in
370 let button_get_height = GButton.button ~label:"get_height" ~packing:(table#attach ~left:1 ~top:1) () in
371 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
372 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
373 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
374 let button_get_hadjustment = GButton.button ~label:"get_hadjustment" ~packing:(table#attach ~left:0 ~top:2) () in
375 let button_get_vadjustment = GButton.button ~label:"get_vadjustment" ~packing:(table#attach ~left:1 ~top:2) () in
376 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
377 let button_get_frame = GButton.button ~label:"get_frame" ~packing:(table#attach ~left:3 ~top:2) () in
378 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
379 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
380 let button_set_anti_aliasing = GButton.button ~label:"set_anti_aliasing" ~packing:(table#attach ~left:1 ~top:3) () in
381 let button_get_anti_aliasing = GButton.button ~label:"get_anti_aliasing" ~packing:(table#attach ~left:2 ~top:3) () in
382 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
383 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
384 let button_export_to_postscript = GButton.button ~label:"export_to_postscript" ~packing:(table#attach ~left:2 ~top:4) () in
385 let button_t1 = GButton.button ~label:"activate T1 fonts" ~packing:(table#attach ~left:3 ~top:4) () in
386 let button_get_font_manager_type = GButton.button ~label:"get_font_manager" ~packing:(table#attach ~left:4 ~top:4) () in
387 let button_get_transparency = GButton.button ~label:"get_transparency" ~packing:(table#attach ~left:0 ~top:5) () in
388 let button_set_transparency = GButton.button ~label:"set_transparency" ~packing:(table#attach ~left:1 ~top:5) () in
389 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
390 let button_get_element_at = GButton.button ~label:"get_element_at" ~packing:(table#attach ~left:3 ~top:5) () in
391 let button_get_drawing_area = GButton.button ~label:"get_drawing_area" ~packing:(table#attach ~left:4 ~top:5) () in
392 (* Signals connection *)
393 ignore(button_gtk#connect#clicked (activate_gtk mathview)) ;
394 ignore(button_load#connect#clicked (load_uri mathview)) ;
395 ignore(button_unload#connect#clicked (unload mathview)) ;
396 ignore(button_get_selection#connect#clicked (test_get_selection mathview)) ;
397 ignore(button_set_selection#connect#clicked (test_set_selection mathview)) ;
398 ignore(button_add_selection#connect#clicked (test_add_selection mathview)) ;
399 ignore(button_reset_selections#connect#clicked (test_reset_selections mathview)) ;
400 ignore(button_get_width#connect#clicked (get_width mathview)) ;
401 ignore(button_get_height#connect#clicked (get_height mathview)) ;
402 ignore(button_get_top#connect#clicked (get_top mathview)) ;
403 ignore(button_set_top#connect#clicked (set_top mathview)) ;
404 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
405 ignore(button_get_hadjustment#connect#clicked (get_hadjustment mathview)) ;
406 ignore(button_get_vadjustment#connect#clicked (get_vadjustment mathview)) ;
407 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
408 ignore(button_get_frame#connect#clicked (get_frame mathview)) ;
409 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
410 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
411 ignore(button_set_anti_aliasing#connect#clicked (set_anti_aliasing mathview)) ;
412 ignore(button_get_anti_aliasing#connect#clicked (get_anti_aliasing mathview)) ;
413 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
414 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
415 ignore(button_export_to_postscript#connect#clicked (export_to_postscript mathview)) ;
416 ignore(button_t1#connect#clicked (activate_t1 mathview)) ;
417 ignore(button_get_font_manager_type#connect#clicked (get_font_manager_type mathview)) ;
418 ignore(button_get_transparency#connect#clicked (get_transparency mathview)) ;
419 ignore(button_set_transparency#connect#clicked (set_transparency mathview)) ;
420 ignore(mathview#connect#click (click mathview)) ;
421 ignore(mathview#connect#selection_changed (selection_changed mathview));
422 ignore(mathview#connect#element_over (element_over mathview)) ;
423 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
424 ignore(button_get_element_at#connect#clicked (get_element_at mathview)) ;
425 ignore(button_get_drawing_area#connect#clicked (get_drawing_area mathview)) ;
427 main_window#show () ;