]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/test/test.ml
* some small bug fixes
[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) _ =
107  let module G = Gdome in
108   if not (jump element) then
109   if not (mathview#action_toggle element) then
110    ()
111 ;;
112
113
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" ;
117  flush stdout
118 ;;
119
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" ;
123  flush stdout
124 ;;
125
126 let get_font_manager_type mathview () =
127  print_string "CURRENT FONT MANAGER TYPE: ";
128  begin
129   match mathview#get_font_manager_type with
130   | `font_manager_t1 -> print_string "T1"
131   | `font_manager_gtk -> print_string "GTK"
132  end;
133  print_newline();
134  flush stdout
135 ;;
136
137 let get_transparency mathview () =
138  print_string ("CURRENT TRANSPARENCY: " ^
139   (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
140  flush stdout
141 ;;
142
143 let set_transparency mathview () =
144  mathview#set_transparency (not mathview#get_transparency) ;
145  print_string "TRANSPARENCY CHANGED\n" ;
146  flush stdout
147 ;;
148
149
150 let load_uri mathview () =
151  mathview#load_uri ~filename:"test.xml" ;
152  print_string "load: SEEMS TO WORK\n" ;
153  flush stdout
154 ;;
155
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" ;
159  flush stdout
160 ;;
161
162 let test_get_selection mathview () =
163  let selection =
164    match mathview#get_selection with
165       Some element -> element#get_tagName#to_string
166     | None -> "no selection!"
167  in
168   print_string ("get_selection: " ^ selection ^ "\n") ;
169   flush stdout
170 ;;
171
172 let test_set_selection mathview () =
173  begin
174    match mathview#get_selection with
175       Some element -> 
176        begin
177         match element#get_parentNode with
178            Some p ->
179             begin
180              try
181               mathview#set_selection (Some (new Gdome.element_of_node p));
182               print_string "set selection: SEEMS TO WORK\n"
183              with
184               GdomeInit.DOMCastException _ ->
185                print_string "EXCEPTION: no parent\n"
186             end
187          | None -> assert false (* every element has a parent *)
188        end
189     | None ->
190        mathview#set_selection None;
191        print_string "no selection\n"
192  end ;
193  flush stdout
194 ;;
195
196 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
197  match mathview#get_selection with
198     Some e -> mathview#add_selection e
199   | None ->
200      begin
201       print_string "no selection to add\n" ;
202       flush stdout
203      end
204 ;;
205
206 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
207  mathview#set_selection None ;
208  mathview#remove_selections
209
210 let unload mathview () =
211  mathview#unload ;
212  print_string "unload: SEEMS TO WORK\n" ;
213  flush stdout
214 ;;
215
216 let get_width mathview () =
217  print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
218  flush stdout
219 ;;
220
221 let get_height mathview () =
222  print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
223  flush stdout
224 ;;
225
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") ;
229   flush stdout
230 ;;
231
232 let set_top mathview () =
233  mathview#set_top 0 0;
234  print_string "set_top: SEEM TO WORK\n" ;
235  flush stdout
236 ;;
237
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" ;
245   flush stdout
246 ;;
247
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" ;
252   flush stdout
253 ;;
254
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" ;
259   flush stdout
260 ;;
261
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";
267   flush stdout
268 ;;
269
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" ;
274   flush stdout
275 ;;
276
277 let set_font_size mathview () =
278  mathview#set_font_size 24 ;
279  print_string "set_font_size: FONT IS NOW 24\n" ;
280  flush stdout
281 ;;
282  
283 let get_font_size mathview () =
284  print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
285  flush stdout
286 ;;
287  
288 let set_anti_aliasing mathview () =
289  mathview#set_anti_aliasing true ;
290  print_string "set_anti_aliasing: ON\n" ;
291  flush stdout
292 ;;
293  
294 let get_anti_aliasing mathview () =
295  print_string ("get_anti_aliasing: " ^
296   (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
297   "\n") ;
298  flush stdout
299 ;;
300  
301  (*
302 let set_kerning mathview () =
303  mathview#set_kerning true ;
304  print_string "set_kerning: ON\n" ;
305  flush stdout
306 ;;
307  
308 let get_kerning mathview () =
309  print_string ("get_kerning: " ^
310   (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
311   "\n") ;
312  flush stdout
313 ;;
314 *)
315
316 let set_log_verbosity mathview () =
317  mathview#set_log_verbosity 3 ;
318  print_string "set_log_verbosity: NOW IS 3\n" ;
319  flush stdout
320 ;;
321  
322 let get_log_verbosity mathview () =
323  print_string ("get_log_verbosity: " ^
324   string_of_int mathview#get_log_verbosity ^
325   "\n") ;
326  flush stdout
327 ;;
328
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";
332  flush stdout
333 ;;
334  
335 let x_coord = ref 0
336 ;;
337
338 let get_element_at mathview () =
339  begin
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")
343  end ;
344  x_coord := !x_coord + 10 ;
345  flush stdout
346 ;;
347
348 (* Widget creation *)
349 let main_window = GWindow.window ~title:"GtkMathView test" () in
350 let vbox = GPack.vbox ~packing:main_window#add () in
351 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
352 let mathview= GMathViewAux.multi_selection_math_view ~packing:sw#add ~width:50 ~height:50 () in
353 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
354 let button_gtk=GButton.button ~label:"activate Gtk fonts" ~packing:(table#attach ~left:0 ~top:0) () in
355 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
356 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
357 let button_get_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
358 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
359 let button_add_selection = GButton.button ~label:"add_selection" ~packing:(table#attach ~left:3 ~top:3) () in
360 let button_reset_selections = GButton.button ~label:"reset_selections" ~packing:(table#attach ~left:4 ~top:3) () in
361 let button_get_width = GButton.button ~label:"get_width" ~packing:(table#attach ~left:0 ~top:1) () in
362 let button_get_height = GButton.button ~label:"get_height" ~packing:(table#attach ~left:1 ~top:1) () in
363 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
364 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
365 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
366 let button_get_hadjustment = GButton.button ~label:"get_hadjustment" ~packing:(table#attach ~left:0 ~top:2) () in
367 let button_get_vadjustment = GButton.button ~label:"get_vadjustment" ~packing:(table#attach ~left:1 ~top:2) () in
368 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
369 let button_get_frame = GButton.button ~label:"get_frame" ~packing:(table#attach ~left:3 ~top:2) () in
370 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
371 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
372 let button_set_anti_aliasing = GButton.button ~label:"set_anti_aliasing" ~packing:(table#attach ~left:1 ~top:3) () in
373 let button_get_anti_aliasing = GButton.button ~label:"get_anti_aliasing" ~packing:(table#attach ~left:2 ~top:3) () in
374 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
375 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
376 let button_export_to_postscript = GButton.button ~label:"export_to_postscript" ~packing:(table#attach ~left:2 ~top:4) () in
377 let button_t1 = GButton.button ~label:"activate T1 fonts" ~packing:(table#attach ~left:3 ~top:4) () in
378 let button_get_font_manager_type = GButton.button ~label:"get_font_manager" ~packing:(table#attach ~left:4 ~top:4) () in
379 let button_get_transparency = GButton.button ~label:"get_transparency" ~packing:(table#attach ~left:0 ~top:5) () in
380 let button_set_transparency = GButton.button ~label:"set_transparency" ~packing:(table#attach ~left:1 ~top:5) () in
381 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
382 let button_get_element_at = GButton.button ~label:"get_element_at" ~packing:(table#attach ~left:3 ~top:5) () in
383 (* Signals connection *)
384 ignore(button_gtk#connect#clicked (activate_gtk mathview)) ;
385 ignore(button_load#connect#clicked (load_uri mathview)) ;
386 ignore(button_unload#connect#clicked (unload mathview)) ;
387 ignore(button_get_selection#connect#clicked (test_get_selection mathview)) ;
388 ignore(button_set_selection#connect#clicked (test_set_selection mathview)) ;
389 ignore(button_add_selection#connect#clicked (test_add_selection mathview)) ;
390 ignore(button_reset_selections#connect#clicked (test_reset_selections mathview)) ;
391 ignore(button_get_width#connect#clicked (get_width mathview)) ;
392 ignore(button_get_height#connect#clicked (get_height mathview)) ;
393 ignore(button_get_top#connect#clicked (get_top mathview)) ;
394 ignore(button_set_top#connect#clicked (set_top mathview)) ;
395 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
396 ignore(button_get_hadjustment#connect#clicked (get_hadjustment mathview)) ;
397 ignore(button_get_vadjustment#connect#clicked (get_vadjustment mathview)) ;
398 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
399 ignore(button_get_frame#connect#clicked (get_frame mathview)) ;
400 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
401 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
402 ignore(button_set_anti_aliasing#connect#clicked (set_anti_aliasing mathview)) ;
403 ignore(button_get_anti_aliasing#connect#clicked (get_anti_aliasing mathview)) ;
404 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
405 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
406 ignore(button_export_to_postscript#connect#clicked (export_to_postscript mathview)) ;
407 ignore(button_t1#connect#clicked (activate_t1 mathview)) ;
408 ignore(button_get_font_manager_type#connect#clicked (get_font_manager_type mathview)) ;
409 ignore(button_get_transparency#connect#clicked (get_transparency mathview)) ;
410 ignore(button_set_transparency#connect#clicked (set_transparency mathview)) ;
411 ignore(mathview#connect#click (click mathview)) ;
412 ignore(mathview#connect#selection_changed (selection_changed mathview));
413 ignore(mathview#connect#element_over (element_over mathview)) ;
414 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
415 ignore(button_get_element_at#connect#clicked (get_element_at mathview)) ;
416 (* Main Loop *)
417 main_window#show () ;
418 GMain.Main.main ()
419 ;;