]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/test/test.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / DEVEL / lablgtkmathview / test / test.ml
1 (* Copyright (C) 2000-2003, Luca Padovani <luca.padovani@cs.unibo.it>,
2  *                          Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>.
3  *
4  * This file is part of lablgtkmathview, the Ocaml binding
5  * for the GtkMathView widget.
6  * 
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.
11  *
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.
16  *
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.
20  * 
21  * For details, send a mail to the author.
22  *)
23
24 (******************************************************************************)
25 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
26 (*                                 25/09/2000                                 *)
27 (*                                                                            *)
28 (*     This is a simple test for the OCaml (LablGtk indeed) binding of the    *)
29 (*                             MathView widget                                *)
30 (******************************************************************************)
31
32 let helmns = Gdome.domString "http://www.cs.unibo.it/helm";;
33
34 (*
35 let choose_selection mmlwidget (element : Gdome.element option) =
36  let module G = Gdome in
37   let rec aux element =
38    if element#hasAttributeNS
39        ~namespaceURI:Misc.helmns
40        ~localName:(G.domString "xref")
41    then
42      mmlwidget#set_selection (Some element)
43    else
44     try
45       match element#get_parentNode with
46          None -> assert false
47        (*CSC: OCAML DIVERGES!
48        | Some p -> aux (new G.element_of_node p)
49        *)
50        | Some p -> aux (new Gdome.element_of_node p)
51     with
52        GdomeInit.DOMCastException _ ->
53         prerr_endline
54          "******* trying to select above the document root ********"
55   in 
56    match element with
57      Some x -> aux x
58    | None   -> mmlwidget#set_selection None
59 ;;     
60 *)
61
62 (* Callbacks *)
63 let selection_changed mathview (element : Gdome.element option) =
64  let rec aux element =
65   if element#hasAttributeNS
66       ~namespaceURI:helmns
67       ~localName:(Gdome.domString "xref")
68   then
69    mathview#set_selection (Some element)
70   else
71    try
72      match element#get_parentNode with
73         None -> mathview#set_selection None
74       | Some p -> aux (new Gdome.element_of_node p)
75    with
76       GdomeInit.DOMCastException _ ->
77        prerr_endline "******* trying to select above the document root ********"
78  in
79  print_endline ("selection_changed: " ^
80   (match element with
81       None -> "selection_changed on nothing"
82     | Some element -> element#get_tagName#to_string
83   )
84  ) ;
85  match element with
86    None -> ()
87  | Some el -> aux el;
88  flush stdout
89 ;;
90
91 let element_over mathview (element : Gdome.element option) _ =
92  print_endline ("element_over: " ^
93   (match element with
94       None -> "element_over on nothing"
95     | Some element -> element#get_tagName#to_string
96   )
97  ) ;
98  flush stdout
99 ;;
100
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
104    if attr = "" then
105     match element#get_parentNode with
106        Some p ->
107         begin
108          try
109           jump (new Gdome.element_of_node p)
110          with
111           GdomeInit.DOMCastException _ ->
112            print_string "jump: NO HREF FOR THIS NODE\n" ;
113            flush stdout ;
114            false
115         end
116      | None -> assert false (* every element has a parent *)
117    else
118     begin
119      print_endline ("jump: " ^ attr) ;
120      flush stdout ;
121      true
122     end
123 ;;
124
125 let rec action mathview (element : Gdome.element) =
126  let module G = Gdome in
127   if element#get_tagName#to_string = "m:maction" then
128    let selection =
129     if element#hasAttribute ~name:(G.domString "selection") then
130      int_of_string (element#getAttribute ~name:(G.domString "selection"))#to_string
131     else
132      1
133    in
134     mathview#freeze ;
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))) ;
137     mathview#thaw ;
138     true
139   else
140    match element#get_parentNode with
141       Some p ->
142        begin
143         try
144          action mathview (new Gdome.element_of_node p)
145         with
146          GdomeInit.DOMCastException _ ->
147           print_string "action: NO MACTION FOUND\n" ;
148           flush stdout ;
149           false
150        end
151      | None -> assert false (* every element has a parent *)
152
153 let click mathview (element : Gdome.element option) _ =
154  let module G = Gdome in
155   match element with
156      None -> print_string "CLICKED ON NOTHING\n" ; flush stdout
157    | Some element ->
158       if not (jump element) then
159       if not (mathview#action_toggle element) then
160        ()
161 ;;
162
163
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" ;
167  flush stdout
168 ;;
169
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" ;
173  flush stdout
174 ;;
175
176 let get_font_manager_type mathview () =
177  print_string "CURRENT FONT MANAGER TYPE: ";
178  begin
179   match mathview#get_font_manager_type with
180   | `font_manager_t1 -> print_string "T1"
181   | `font_manager_gtk -> print_string "GTK"
182  end;
183  print_newline();
184  flush stdout
185 ;;
186
187 let get_transparency mathview () =
188  print_string ("CURRENT TRANSPARENCY: " ^
189   (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
190  flush stdout
191 ;;
192
193 let set_transparency mathview () =
194  mathview#set_transparency (not mathview#get_transparency) ;
195  print_string "TRANSPARENCY CHANGED\n" ;
196  flush stdout
197 ;;
198
199
200 let load_uri mathview () =
201  mathview#load_uri ~filename:"test.xml" ;
202  print_string "load: SEEMS TO WORK\n" ;
203  flush stdout
204 ;;
205
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" ;
209  flush stdout
210 ;;
211
212 let test_get_selection mathview () =
213  let selection =
214    match mathview#get_selection with
215       Some element -> element#get_tagName#to_string
216     | None -> "no selection!"
217  in
218   print_string ("get_selection: " ^ selection ^ "\n") ;
219   flush stdout
220 ;;
221
222 let test_set_selection mathview () =
223  begin
224    match mathview#get_selection with
225       Some element -> 
226        begin
227         match element#get_parentNode with
228            Some p ->
229             begin
230              try
231               mathview#set_selection (Some (new Gdome.element_of_node p));
232               print_string "set selection: SEEMS TO WORK\n"
233              with
234               GdomeInit.DOMCastException _ ->
235                print_string "EXCEPTION: no parent\n"
236             end
237          | None -> assert false (* every element has a parent *)
238        end
239     | None ->
240        mathview#set_selection None;
241        print_string "no selection\n"
242  end ;
243  flush stdout
244 ;;
245
246 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
247  match mathview#get_selection with
248     Some e -> mathview#add_selection e
249   | None ->
250      begin
251       print_string "no selection to add\n" ;
252       flush stdout
253      end
254 ;;
255
256 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
257  mathview#set_selection None ;
258  mathview#remove_selections
259
260 let unload mathview () =
261  mathview#unload ;
262  print_string "unload: SEEMS TO WORK\n" ;
263  flush stdout
264 ;;
265
266 let get_width mathview () =
267  print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
268  flush stdout
269 ;;
270
271 let get_height mathview () =
272  print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
273  flush stdout
274 ;;
275
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") ;
279   flush stdout
280 ;;
281
282 let set_top mathview () =
283  mathview#set_top 0 0;
284  print_string "set_top: SEEM TO WORK\n" ;
285  flush stdout
286 ;;
287
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" ;
295   flush stdout
296 ;;
297
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" ;
302   flush stdout
303 ;;
304
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" ;
309   flush stdout
310 ;;
311
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";
317   flush stdout
318 ;;
319
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" ;
324   flush stdout
325 ;;
326
327 let set_font_size mathview () =
328  mathview#set_font_size 24 ;
329  print_string "set_font_size: FONT IS NOW 24\n" ;
330  flush stdout
331 ;;
332  
333 let get_font_size mathview () =
334  print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
335  flush stdout
336 ;;
337  
338 let set_anti_aliasing mathview () =
339  mathview#set_anti_aliasing true ;
340  print_string "set_anti_aliasing: ON\n" ;
341  flush stdout
342 ;;
343  
344 let get_anti_aliasing mathview () =
345  print_string ("get_anti_aliasing: " ^
346   (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
347   "\n") ;
348  flush stdout
349 ;;
350  
351  (*
352 let set_kerning mathview () =
353  mathview#set_kerning true ;
354  print_string "set_kerning: ON\n" ;
355  flush stdout
356 ;;
357  
358 let get_kerning mathview () =
359  print_string ("get_kerning: " ^
360   (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
361   "\n") ;
362  flush stdout
363 ;;
364 *)
365
366 let set_log_verbosity mathview () =
367  mathview#set_log_verbosity 3 ;
368  print_string "set_log_verbosity: NOW IS 3\n" ;
369  flush stdout
370 ;;
371  
372 let get_log_verbosity mathview () =
373  print_string ("get_log_verbosity: " ^
374   string_of_int mathview#get_log_verbosity ^
375   "\n") ;
376  flush stdout
377 ;;
378
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";
382  flush stdout
383 ;;
384  
385 let x_coord = ref 0
386 ;;
387
388 let get_element_at mathview () =
389  begin
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")
393  end ;
394  x_coord := !x_coord + 10 ;
395  flush stdout
396 ;;
397
398 let get_drawing_area mathview () =
399  begin
400   let da = mathview#get_drawing_area in
401   print_string ("don't know what to do with the drawing area\n")
402  end ;
403  flush stdout
404 ;;
405
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)) ;
476 (* Main Loop *)
477 main_window#show () ;
478 GMain.Main.main ()
479 ;;