]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/test/test.ml
* click signal changed: now the element argument is optional
[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 (* Callbacks *)
33 let selection_changed mathview (element : Gdome.element option) =
34  print_endline ("selection_changed: " ^
35   (match element with
36       None -> "selection_changed on nothing"
37     | Some element -> element#get_tagName#to_string
38   )
39  ) ;
40  mathview#set_selection element;
41  flush stdout
42 ;;
43
44 let element_over mathview (element : Gdome.element option) _ =
45  print_endline ("element_over: " ^
46   (match element with
47       None -> "element_over on nothing"
48     | Some element -> element#get_tagName#to_string
49   )
50  ) ;
51  flush stdout
52 ;;
53
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
57    if attr = "" then
58     match element#get_parentNode with
59        Some p ->
60         begin
61          try
62           jump (new Gdome.element_of_node p)
63          with
64           GdomeInit.DOMCastException _ ->
65            print_string "jump: NO HREF FOR THIS NODE\n" ;
66            flush stdout ;
67            false
68         end
69      | None -> assert false (* every element has a parent *)
70    else
71     begin
72      print_endline ("jump: " ^ attr) ;
73      flush stdout ;
74      true
75     end
76 ;;
77
78 let rec action mathview (element : Gdome.element) =
79  let module G = Gdome in
80   if element#get_tagName#to_string = "m:maction" then
81    let selection =
82     if element#hasAttribute ~name:(G.domString "selection") then
83      int_of_string (element#getAttribute ~name:(G.domString "selection"))#to_string
84     else
85      1
86    in
87     mathview#freeze ;
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))) ;
90     mathview#thaw ;
91     true
92   else
93    match element#get_parentNode with
94       Some p ->
95        begin
96         try
97          action mathview (new Gdome.element_of_node p)
98         with
99          GdomeInit.DOMCastException _ ->
100           print_string "action: NO MACTION FOUND\n" ;
101           flush stdout ;
102           false
103        end
104      | None -> assert false (* every element has a parent *)
105
106 let click mathview (element : Gdome.element option) _ =
107  let module G = Gdome in
108   match element with
109      None -> print_string "CLICKED ON NOTHING\n" ; flush stdout
110    | Some element ->
111       if not (jump element) then
112       if not (mathview#action_toggle element) then
113        ()
114 ;;
115
116
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" ;
120  flush stdout
121 ;;
122
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" ;
126  flush stdout
127 ;;
128
129 let get_font_manager_type mathview () =
130  print_string "CURRENT FONT MANAGER TYPE: ";
131  begin
132   match mathview#get_font_manager_type with
133   | `font_manager_t1 -> print_string "T1"
134   | `font_manager_gtk -> print_string "GTK"
135  end;
136  print_newline();
137  flush stdout
138 ;;
139
140 let get_transparency mathview () =
141  print_string ("CURRENT TRANSPARENCY: " ^
142   (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
143  flush stdout
144 ;;
145
146 let set_transparency mathview () =
147  mathview#set_transparency (not mathview#get_transparency) ;
148  print_string "TRANSPARENCY CHANGED\n" ;
149  flush stdout
150 ;;
151
152
153 let load_uri mathview () =
154  mathview#load_uri ~filename:"test.xml" ;
155  print_string "load: SEEMS TO WORK\n" ;
156  flush stdout
157 ;;
158
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" ;
162  flush stdout
163 ;;
164
165 let test_get_selection mathview () =
166  let selection =
167    match mathview#get_selection with
168       Some element -> element#get_tagName#to_string
169     | None -> "no selection!"
170  in
171   print_string ("get_selection: " ^ selection ^ "\n") ;
172   flush stdout
173 ;;
174
175 let test_set_selection mathview () =
176  begin
177    match mathview#get_selection with
178       Some element -> 
179        begin
180         match element#get_parentNode with
181            Some p ->
182             begin
183              try
184               mathview#set_selection (Some (new Gdome.element_of_node p));
185               print_string "set selection: SEEMS TO WORK\n"
186              with
187               GdomeInit.DOMCastException _ ->
188                print_string "EXCEPTION: no parent\n"
189             end
190          | None -> assert false (* every element has a parent *)
191        end
192     | None ->
193        mathview#set_selection None;
194        print_string "no selection\n"
195  end ;
196  flush stdout
197 ;;
198
199 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
200  match mathview#get_selection with
201     Some e -> mathview#add_selection e
202   | None ->
203      begin
204       print_string "no selection to add\n" ;
205       flush stdout
206      end
207 ;;
208
209 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
210  mathview#set_selection None ;
211  mathview#remove_selections
212
213 let unload mathview () =
214  mathview#unload ;
215  print_string "unload: SEEMS TO WORK\n" ;
216  flush stdout
217 ;;
218
219 let get_width mathview () =
220  print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
221  flush stdout
222 ;;
223
224 let get_height mathview () =
225  print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
226  flush stdout
227 ;;
228
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") ;
232   flush stdout
233 ;;
234
235 let set_top mathview () =
236  mathview#set_top 0 0;
237  print_string "set_top: SEEM TO WORK\n" ;
238  flush stdout
239 ;;
240
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" ;
248   flush stdout
249 ;;
250
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" ;
255   flush stdout
256 ;;
257
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" ;
262   flush stdout
263 ;;
264
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";
270   flush stdout
271 ;;
272
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" ;
277   flush stdout
278 ;;
279
280 let set_font_size mathview () =
281  mathview#set_font_size 24 ;
282  print_string "set_font_size: FONT IS NOW 24\n" ;
283  flush stdout
284 ;;
285  
286 let get_font_size mathview () =
287  print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
288  flush stdout
289 ;;
290  
291 let set_anti_aliasing mathview () =
292  mathview#set_anti_aliasing true ;
293  print_string "set_anti_aliasing: ON\n" ;
294  flush stdout
295 ;;
296  
297 let get_anti_aliasing mathview () =
298  print_string ("get_anti_aliasing: " ^
299   (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
300   "\n") ;
301  flush stdout
302 ;;
303  
304  (*
305 let set_kerning mathview () =
306  mathview#set_kerning true ;
307  print_string "set_kerning: ON\n" ;
308  flush stdout
309 ;;
310  
311 let get_kerning mathview () =
312  print_string ("get_kerning: " ^
313   (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
314   "\n") ;
315  flush stdout
316 ;;
317 *)
318
319 let set_log_verbosity mathview () =
320  mathview#set_log_verbosity 3 ;
321  print_string "set_log_verbosity: NOW IS 3\n" ;
322  flush stdout
323 ;;
324  
325 let get_log_verbosity mathview () =
326  print_string ("get_log_verbosity: " ^
327   string_of_int mathview#get_log_verbosity ^
328   "\n") ;
329  flush stdout
330 ;;
331
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";
335  flush stdout
336 ;;
337  
338 let x_coord = ref 0
339 ;;
340
341 let get_element_at mathview () =
342  begin
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")
346  end ;
347  x_coord := !x_coord + 10 ;
348  flush stdout
349 ;;
350
351 let get_drawing_area mathview () =
352  begin
353   let da = mathview#get_drawing_area in
354   print_string ("don't know what to do with the drawing area\n")
355  end ;
356  flush stdout
357 ;;
358
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)) ;
429 (* Main Loop *)
430 main_window#show () ;
431 GMain.Main.main ()
432 ;;