1 (* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
3 * This file is part of lablgtkmathview, the Ocaml binding
4 * for the GtkMathView widget.
6 * lablgtkmathview is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * lablgtkmathview is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with lablgtkmathview; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * For details, send a mail to the author.
23 (******************************************************************************)
24 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
27 (* This is a simple test for the OCaml (LablGtk indeed) binding of the *)
29 (******************************************************************************)
32 let selection_changed mathview (element : Gdome.element option) =
33 print_endline ("selection_changed: " ^
35 None -> "selection_changed on nothing"
36 | Some element -> element#get_tagName#to_string
39 mathview#set_selection element;
43 let element_changed mathview (element : Gdome.element option) =
44 print_endline ("element_changed: " ^
46 None -> "element_changed on nothing"
47 | Some element -> element#get_tagName#to_string
53 let rec jump (element : Gdome.element) =
54 let module G = Gdome in
55 let attr = (element#getAttribute ~name:(G.domString "href"))#to_string in
57 match element#get_parentNode with
61 jump (new Gdome.element_of_node p)
63 GdomeInit.DOMCastException _ ->
64 print_string "jump: NO HREF FOR THIS NODE\n" ;
68 | None -> assert false (* every element has a parent *)
71 print_endline ("jump: " ^ attr) ;
77 let rec action mathview (element : Gdome.element) =
78 let module G = Gdome in
79 if element#get_tagName#to_string = "m:maction" then
81 if element#hasAttribute ~name:(G.domString "selection") then
82 int_of_string (element#getAttribute ~name:(G.domString "selection"))#to_string
87 (* the widget will cast the index back into a reasonable range *)
88 element#setAttribute ~name:(G.domString "selection") ~value:(G.domString (string_of_int (selection + 1))) ;
92 match element#get_parentNode with
96 action mathview (new Gdome.element_of_node p)
98 GdomeInit.DOMCastException _ ->
99 print_string "action: NO MACTION FOUND\n" ;
103 | None -> assert false (* every element has a parent *)
105 let clicked mathview (element : Gdome.element) =
106 let module G = Gdome in
107 if not (jump element) then
108 if not (action mathview element) then
110 match mathview#get_action with
112 mathview#action_toggle ;
113 print_string ("current action selection: " ^
114 string_of_int mathview#action_get_selected ^ "\n") ;
117 print_string ("mouse is on: " ^
118 (match (mathview#get_element : Gdome.element option) with
119 Some e -> e#get_tagName#to_string
120 | None -> "NO ELEMENT!\n"
122 print_endline ("clicked: " ^ element#get_tagName#to_string) ;
129 let activate_t1 mathview () =
130 mathview#set_font_manager_type ~fm_type:`font_manager_t1;
131 print_string "WIDGET SET WITH T1 FONTS\n" ;
135 let activate_gtk mathview () =
136 mathview#set_font_manager_type ~fm_type:`font_manager_gtk ;
137 print_string "WIDGET SET WITH GTK FONTS\n" ;
141 let get_font_manager_type mathview () =
142 print_string "CURRENT FONT MANAGER TYPE: ";
144 match mathview#get_font_manager_type with
145 | `font_manager_t1 -> print_string "T1"
146 | `font_manager_gtk -> print_string "GTK"
152 let get_transparency mathview () =
153 print_string ("CURRENT TRANSPARENCY: " ^
154 (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
158 let set_transparency mathview () =
159 mathview#set_transparency (not mathview#get_transparency) ;
160 print_string "TRANSPARENCY CHANGED\n" ;
165 let load_uri mathview () =
166 mathview#load_uri ~filename:"test.xml" ;
167 print_string "load: SEEMS TO WORK\n" ;
171 let load_doc mathview () =
172 mathview#load_doc ~dom:((Gdome.domImplementation ())#createDocumentFromURI ~uri:"test.xml" ()) ;
173 print_string "load from DOM: SEEMS TO WORK\n" ;
177 let test_get_selection mathview () =
179 match mathview#get_selection with
180 Some element -> element#get_tagName#to_string
181 | None -> "no selection!"
183 print_string ("get_selection: " ^ selection ^ "\n") ;
187 let test_set_selection mathview () =
189 match mathview#get_selection with
192 match element#get_parentNode with
196 mathview#set_selection (Some (new Gdome.element_of_node p));
197 print_string "set selection: SEEMS TO WORK\n"
199 GdomeInit.DOMCastException _ ->
200 print_string "EXCEPTION: no parent\n"
202 | None -> assert false (* every element has a parent *)
205 mathview#set_selection None;
206 print_string "no selection\n"
211 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
212 match mathview#get_selection with
213 Some e -> mathview#add_selection e
216 print_string "no selection to add\n" ;
221 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
222 mathview#set_selection None ;
223 mathview#remove_selections
225 let unload mathview () =
227 print_string "unload: SEEMS TO WORK\n" ;
231 let get_width mathview () =
232 print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
236 let get_height mathview () =
237 print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
241 let get_top mathview () =
242 let (x,y) = mathview#get_top in
243 print_string ("get_top: ("^ string_of_int x ^ "," ^ string_of_int y ^ ")\n") ;
247 let set_top mathview () =
248 mathview#set_top 0 0;
249 print_string "set_top: SEEM TO WORK\n" ;
253 let set_adjustments mathview () =
254 let adj1 = GData.adjustment () in
255 let adj2 = GData.adjustment () in
256 mathview#set_adjustments adj1 adj2 ;
257 adj1#set_value ((adj1#lower +. adj1#upper) /. 2.0) ;
258 adj2#set_value ((adj2#lower +. adj2#upper) /. 2.0) ;
259 print_string "set_adjustments: SEEM TO WORK\n" ;
263 let get_hadjustment mathview () =
264 let adj = mathview#get_hadjustment in
265 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
266 print_string "get_hadjustment: SEEM TO WORK\n" ;
270 let get_vadjustment mathview () =
271 let adj = mathview#get_vadjustment in
272 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
273 print_string "get_vadjustment: SEEM TO WORK\n" ;
277 let get_buffer mathview () =
278 let buffer = mathview#get_buffer in
279 Gdk.Draw.rectangle buffer (Gdk.GC.create buffer) ~x:0 ~y:0
280 ~width:50 ~height:50 ~filled:true () ;
281 print_string "get_buffer: SEEMS TO WORK (hint: force the widget redrawing)\n";
285 let get_frame mathview () =
286 let frame = mathview#get_frame in
287 frame#set_shadow_type `NONE ;
288 print_string "get_frame: SEEMS TO WORK\n" ;
292 let set_font_size mathview () =
293 mathview#set_font_size 24 ;
294 print_string "set_font_size: FONT IS NOW 24\n" ;
298 let get_font_size mathview () =
299 print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
303 let set_anti_aliasing mathview () =
304 mathview#set_anti_aliasing true ;
305 print_string "set_anti_aliasing: ON\n" ;
309 let get_anti_aliasing mathview () =
310 print_string ("get_anti_aliasing: " ^
311 (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
317 let set_kerning mathview () =
318 mathview#set_kerning true ;
319 print_string "set_kerning: ON\n" ;
323 let get_kerning mathview () =
324 print_string ("get_kerning: " ^
325 (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
331 let set_log_verbosity mathview () =
332 mathview#set_log_verbosity 3 ;
333 print_string "set_log_verbosity: NOW IS 3\n" ;
337 let get_log_verbosity mathview () =
338 print_string ("get_log_verbosity: " ^
339 string_of_int mathview#get_log_verbosity ^
344 let export_to_postscript (mathview : GMathViewAux.multi_selection_math_view) () =
345 mathview#export_to_postscript ~filename:"test.ps" ();
346 print_string "expor_to_postscript: SEEMS TO WORK (hint: look at test.ps)\n";
350 (* Widget creation *)
351 let main_window = GWindow.window ~title:"GtkMathView test" () in
352 let vbox = GPack.vbox ~packing:main_window#add () in
353 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
354 let mathview= GMathViewAux.multi_selection_math_view ~packing:sw#add ~width:50 ~height:50 () in
355 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
356 let button_gtk=GButton.button ~label:"activate Gtk fonts" ~packing:(table#attach ~left:0 ~top:0) () in
357 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
358 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
359 let button_get_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
360 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
361 let button_add_selection = GButton.button ~label:"add_selection" ~packing:(table#attach ~left:3 ~top:3) () in
362 let button_reset_selections = GButton.button ~label:"reset_selections" ~packing:(table#attach ~left:4 ~top:3) () in
363 let button_get_width = GButton.button ~label:"get_width" ~packing:(table#attach ~left:0 ~top:1) () in
364 let button_get_height = GButton.button ~label:"get_height" ~packing:(table#attach ~left:1 ~top:1) () in
365 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
366 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
367 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
368 let button_get_hadjustment = GButton.button ~label:"get_hadjustment" ~packing:(table#attach ~left:0 ~top:2) () in
369 let button_get_vadjustment = GButton.button ~label:"get_vadjustment" ~packing:(table#attach ~left:1 ~top:2) () in
370 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
371 let button_get_frame = GButton.button ~label:"get_frame" ~packing:(table#attach ~left:3 ~top:2) () in
372 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
373 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
374 let button_set_anti_aliasing = GButton.button ~label:"set_anti_aliasing" ~packing:(table#attach ~left:1 ~top:3) () in
375 let button_get_anti_aliasing = GButton.button ~label:"get_anti_aliasing" ~packing:(table#attach ~left:2 ~top:3) () in
376 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
377 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
378 let button_export_to_postscript = GButton.button ~label:"export_to_postscript" ~packing:(table#attach ~left:2 ~top:4) () in
379 let button_t1 = GButton.button ~label:"activate T1 fonts" ~packing:(table#attach ~left:3 ~top:4) () in
380 let button_get_font_manager_type = GButton.button ~label:"get_font_manager" ~packing:(table#attach ~left:4 ~top:4) () in
381 let button_get_transparency = GButton.button ~label:"get_transparency" ~packing:(table#attach ~left:0 ~top:5) () in
382 let button_set_transparency = GButton.button ~label:"set_transparency" ~packing:(table#attach ~left:1 ~top:5) () in
383 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
384 (* Signals connection *)
385 ignore(button_gtk#connect#clicked (activate_gtk mathview)) ;
386 ignore(button_load#connect#clicked (load_uri mathview)) ;
387 ignore(button_unload#connect#clicked (unload mathview)) ;
388 ignore(button_get_selection#connect#clicked (test_get_selection mathview)) ;
389 ignore(button_set_selection#connect#clicked (test_set_selection mathview)) ;
390 ignore(button_add_selection#connect#clicked (test_add_selection mathview)) ;
391 ignore(button_reset_selections#connect#clicked (test_reset_selections mathview)) ;
392 ignore(button_get_width#connect#clicked (get_width mathview)) ;
393 ignore(button_get_height#connect#clicked (get_height mathview)) ;
394 ignore(button_get_top#connect#clicked (get_top mathview)) ;
395 ignore(button_set_top#connect#clicked (set_top mathview)) ;
396 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
397 ignore(button_get_hadjustment#connect#clicked (get_hadjustment mathview)) ;
398 ignore(button_get_vadjustment#connect#clicked (get_vadjustment mathview)) ;
399 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
400 ignore(button_get_frame#connect#clicked (get_frame mathview)) ;
401 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
402 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
403 ignore(button_set_anti_aliasing#connect#clicked (set_anti_aliasing mathview)) ;
404 ignore(button_get_anti_aliasing#connect#clicked (get_anti_aliasing mathview)) ;
405 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
406 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
407 ignore(button_export_to_postscript#connect#clicked (export_to_postscript mathview)) ;
408 ignore(button_t1#connect#clicked (activate_t1 mathview)) ;
409 ignore(button_get_font_manager_type#connect#clicked (get_font_manager_type mathview)) ;
410 ignore(button_get_transparency#connect#clicked (get_transparency mathview)) ;
411 ignore(button_set_transparency#connect#clicked (set_transparency mathview)) ;
412 ignore(mathview#connect#clicked (clicked mathview)) ;
413 ignore(mathview#connect#selection_changed (selection_changed mathview));
414 ignore(mathview#connect#element_changed (element_changed mathview)) ;
415 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
417 main_window#show () ;