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 load_uri mathview () =
165 mathview#load_uri ~filename:"test.xml" ;
166 print_string "load: SEEMS TO WORK\n" ;
170 let load_doc mathview () =
171 mathview#load_doc ~dom:((Gdome.domImplementation ())#createDocumentFromURI ~uri:"test.xml" ()) ;
172 print_string "load from DOM: SEEMS TO WORK\n" ;
176 let test_get_selection mathview () =
178 match mathview#get_selection with
179 Some element -> element#get_tagName#to_string
180 | None -> "no selection!"
182 print_string ("selection: " ^ selection ^ "\n") ;
186 let test_set_selection mathview () =
188 match mathview#get_selection with
191 match element#get_parentNode with
195 mathview#set_selection (Some (new Gdome.element_of_node p));
196 print_string "set selection: SEEMS TO WORK\n"
198 GdomeInit.DOMCastException _ ->
199 print_string "EXCEPTION: no parent\n"
201 | None -> assert false (* every element has a parent *)
204 mathview#set_selection None;
205 print_string "no selection\n"
210 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
211 match mathview#get_selection with
212 Some e -> mathview#add_selection e
215 print_string "no selection to add\n" ;
220 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
221 mathview#set_selection None ;
222 mathview#remove_selections
224 let unload mathview () =
226 print_string "unload: SEEMS TO WORK\n" ;
230 let get_width mathview () =
231 print_string ("width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
235 let get_height mathview () =
236 print_string ("height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
240 let get_top mathview () =
241 let (x,y) = mathview#get_top in
242 print_string ("top: ("^ string_of_int x ^ "," ^ string_of_int y ^ ")\n") ;
246 let set_top mathview () =
247 mathview#set_top 0 0;
248 print_string "set_top: SEEM TO WORK\n" ;
252 let set_adjustments mathview () =
253 let adj1 = GData.adjustment () in
254 let adj2 = GData.adjustment () in
255 mathview#set_adjustments adj1 adj2 ;
256 adj1#set_value ((adj1#lower +. adj1#upper) /. 2.0) ;
257 adj2#set_value ((adj2#lower +. adj2#upper) /. 2.0) ;
258 print_string "set_adjustments: SEEM TO WORK\n" ;
262 let get_hadjustment mathview () =
263 let adj = mathview#get_hadjustment in
264 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
265 print_string "hadjustment: SEEM TO WORK\n" ;
269 let get_vadjustment mathview () =
270 let adj = mathview#get_vadjustment in
271 adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
272 print_string "vadjustment: SEEM TO WORK\n" ;
276 let get_buffer mathview () =
277 let buffer = mathview#get_buffer in
278 Gdk.Draw.rectangle buffer (Gdk.GC.create buffer) ~x:0 ~y:0
279 ~width:50 ~height:50 ~filled:true () ;
280 print_string "buffer: SEEMS TO WORK (hint: force the widget redrawing)\n";
284 let set_font_size mathview () =
285 mathview#set_font_size 24 ;
286 print_string "set_font_size: FONT IS NOW 24\n" ;
290 let get_font_size mathview () =
291 print_string ("font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
295 let set_log_verbosity mathview () =
296 mathview#set_log_verbosity 3 ;
297 print_string "set_log_verbosity: NOW IS 3\n" ;
301 let get_log_verbosity mathview () =
302 print_string ("log_verbosity: " ^
303 string_of_int mathview#get_log_verbosity ^
311 let get_element_at mathview () =
313 match mathview#get_element_at !x_coord 10 with
314 None -> print_string ("there is no element at " ^ (string_of_int !x_coord) ^ " 10\n")
315 | Some e -> print_string ("at " ^ (string_of_int !x_coord) ^ " 10 found element " ^ (e#get_nodeName#to_string) ^ "\n")
317 x_coord := !x_coord + 10 ;
321 let get_drawing_area mathview () =
323 let da = mathview#get_drawing_area in
324 print_string ("don't know what to do with the drawing area\n")
329 (* Widget creation *)
330 let main_window = GWindow.window ~title:"GtkMathView test" () in
331 let vbox = GPack.vbox ~packing:main_window#add () in
332 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
333 let mathview= GMathViewAux.multi_selection_math_view ~packing:sw#add ~width:50 ~height:50 () in
334 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
335 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
336 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
337 let button_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
338 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
339 let button_add_selection = GButton.button ~label:"add_selection" ~packing:(table#attach ~left:3 ~top:3) () in
340 let button_reset_selections = GButton.button ~label:"reset_selections" ~packing:(table#attach ~left:4 ~top:3) () in
341 let button_get_width = GButton.button ~label:"get_width" ~packing:(table#attach ~left:0 ~top:1) () in
342 let button_get_height = GButton.button ~label:"get_height" ~packing:(table#attach ~left:1 ~top:1) () in
343 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
344 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
345 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
346 let button_get_hadjustment = GButton.button ~label:"get_hadjustment" ~packing:(table#attach ~left:0 ~top:2) () in
347 let button_get_vadjustment = GButton.button ~label:"get_vadjustment" ~packing:(table#attach ~left:1 ~top:2) () in
348 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
349 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
350 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
351 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
352 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
353 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
354 let button_get_element_at = GButton.button ~label:"get_element_at" ~packing:(table#attach ~left:3 ~top:5) () in
355 let button_get_drawing_area = GButton.button ~label:"get_drawing_area" ~packing:(table#attach ~left:4 ~top:5) () in
356 (* Signals connection *)
357 ignore(button_load#connect#clicked (load_uri mathview)) ;
358 ignore(button_unload#connect#clicked (unload mathview)) ;
359 ignore(button_selection#connect#clicked (test_get_selection mathview)) ;
360 ignore(button_set_selection#connect#clicked (test_set_selection mathview)) ;
361 ignore(button_add_selection#connect#clicked (test_add_selection mathview)) ;
362 ignore(button_reset_selections#connect#clicked (test_reset_selections mathview)) ;
363 ignore(button_get_width#connect#clicked (get_width mathview)) ;
364 ignore(button_get_height#connect#clicked (get_height mathview)) ;
365 ignore(button_get_top#connect#clicked (get_top mathview)) ;
366 ignore(button_set_top#connect#clicked (set_top mathview)) ;
367 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
368 ignore(button_get_hadjustment#connect#clicked (get_hadjustment mathview)) ;
369 ignore(button_get_vadjustment#connect#clicked (get_vadjustment mathview)) ;
370 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
371 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
372 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
373 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
374 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
375 ignore(mathview#connect#click (click mathview)) ;
376 ignore(mathview#connect#selection_changed (selection_changed mathview));
377 ignore(mathview#connect#element_over (element_over mathview)) ;
378 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
379 ignore(button_get_element_at#connect#clicked (get_element_at mathview)) ;
380 ignore(button_get_drawing_area#connect#clicked (get_drawing_area mathview)) ;
382 main_window#show () ;