]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/test/test.ml
* binding update to recognize the new event scheme generated by
[helm.git] / helm / DEVEL / lablgtkmathview / test / test.ml
1 (* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
2  *
3  * This file is part of lablgtkmathview, the Ocaml binding
4  * for the GtkMathView widget.
5  * 
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.
10  *
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.
15  *
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.
19  * 
20  * For details, send a mail to the author.
21  *)
22
23 (******************************************************************************)
24 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
25 (*                                 25/09/2000                                 *)
26 (*                                                                            *)
27 (*     This is a simple test for the OCaml (LablGtk indeed) binding of the    *)
28 (*                             MathView widget                                *)
29 (******************************************************************************)
30
31 (* Callbacks *)
32 let selection_changed mathview (element : Gdome.element option) =
33  print_endline ("selection_changed: " ^
34   (match element with
35       None -> "selection_changed on nothing"
36     | Some element -> element#get_tagName#to_string
37   )
38  ) ;
39  mathview#set_selection element;
40  flush stdout
41 ;;
42
43 let element_over mathview (element : Gdome.element option) _ =
44  print_endline ("element_over: " ^
45   (match element with
46       None -> "element_over on nothing"
47     | Some element -> element#get_tagName#to_string
48   )
49  ) ;
50  flush stdout
51 ;;
52
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
56    if attr = "" then
57     match element#get_parentNode with
58        Some p ->
59         begin
60          try
61           jump (new Gdome.element_of_node p)
62          with
63           GdomeInit.DOMCastException _ ->
64            print_string "jump: NO HREF FOR THIS NODE\n" ;
65            flush stdout ;
66            false
67         end
68      | None -> assert false (* every element has a parent *)
69    else
70     begin
71      print_endline ("jump: " ^ attr) ;
72      flush stdout ;
73      true
74     end
75 ;;
76
77 let rec action mathview (element : Gdome.element) =
78  let module G = Gdome in
79   if element#get_tagName#to_string = "m:maction" then
80    let selection =
81     if element#hasAttribute ~name:(G.domString "selection") then
82      int_of_string (element#getAttribute ~name:(G.domString "selection"))#to_string
83     else
84      1
85    in
86     mathview#freeze ;
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))) ;
89     mathview#thaw ;
90     true
91   else
92    match element#get_parentNode with
93       Some p ->
94        begin
95         try
96          action mathview (new Gdome.element_of_node p)
97         with
98          GdomeInit.DOMCastException _ ->
99           print_string "action: NO MACTION FOUND\n" ;
100           flush stdout ;
101           false
102        end
103      | None -> assert false (* every element has a parent *)
104
105 let click mathview (element : Gdome.element) _ =
106  let module G = Gdome in
107   if not (jump element) then
108   if not (mathview#action_toggle element) then
109   (*
110    match mathview#get_action with
111       Some n ->
112        mathview#action_toggle ;
113        print_string ("current action selection: " ^
114         string_of_int mathview#action_get_selected ^ "\n") ;
115        flush stdout ;
116     | None ->
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"
121          ) ^ "\n") ;
122        print_endline ("clicked: " ^ element#get_tagName#to_string) ;
123        flush stdout
124    *)
125    ()
126 ;;
127
128
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" ;
132  flush stdout
133 ;;
134
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" ;
138  flush stdout
139 ;;
140
141 let get_font_manager_type mathview () =
142  print_string "CURRENT FONT MANAGER TYPE: ";
143  begin
144   match mathview#get_font_manager_type with
145   | `font_manager_t1 -> print_string "T1"
146   | `font_manager_gtk -> print_string "GTK"
147  end;
148  print_newline();
149  flush stdout
150 ;;
151
152 let get_transparency mathview () =
153  print_string ("CURRENT TRANSPARENCY: " ^
154   (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
155  flush stdout
156 ;;
157
158 let set_transparency mathview () =
159  mathview#set_transparency (not mathview#get_transparency) ;
160  print_string "TRANSPARENCY CHANGED\n" ;
161  flush stdout
162 ;;
163
164
165 let load_uri mathview () =
166  mathview#load_uri ~filename:"test.xml" ;
167  print_string "load: SEEMS TO WORK\n" ;
168  flush stdout
169 ;;
170
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" ;
174  flush stdout
175 ;;
176
177 let test_get_selection mathview () =
178  let selection =
179    match mathview#get_selection with
180       Some element -> element#get_tagName#to_string
181     | None -> "no selection!"
182  in
183   print_string ("get_selection: " ^ selection ^ "\n") ;
184   flush stdout
185 ;;
186
187 let test_set_selection mathview () =
188  begin
189    match mathview#get_selection with
190       Some element -> 
191        begin
192         match element#get_parentNode with
193            Some p ->
194             begin
195              try
196               mathview#set_selection (Some (new Gdome.element_of_node p));
197               print_string "set selection: SEEMS TO WORK\n"
198              with
199               GdomeInit.DOMCastException _ ->
200                print_string "EXCEPTION: no parent\n"
201             end
202          | None -> assert false (* every element has a parent *)
203        end
204     | None ->
205        mathview#set_selection None;
206        print_string "no selection\n"
207  end ;
208  flush stdout
209 ;;
210
211 let test_add_selection (mathview : GMathViewAux.multi_selection_math_view) () =
212  match mathview#get_selection with
213     Some e -> mathview#add_selection e
214   | None ->
215      begin
216       print_string "no selection to add\n" ;
217       flush stdout
218      end
219 ;;
220
221 let test_reset_selections (mathview : GMathViewAux.multi_selection_math_view) () =
222  mathview#set_selection None ;
223  mathview#remove_selections
224
225 let unload mathview () =
226  mathview#unload ;
227  print_string "unload: SEEMS TO WORK\n" ;
228  flush stdout
229 ;;
230
231 let get_width mathview () =
232  print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
233  flush stdout
234 ;;
235
236 let get_height mathview () =
237  print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
238  flush stdout
239 ;;
240
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") ;
244   flush stdout
245 ;;
246
247 let set_top mathview () =
248  mathview#set_top 0 0;
249  print_string "set_top: SEEM TO WORK\n" ;
250  flush stdout
251 ;;
252
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" ;
260   flush stdout
261 ;;
262
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" ;
267   flush stdout
268 ;;
269
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" ;
274   flush stdout
275 ;;
276
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";
282   flush stdout
283 ;;
284
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" ;
289   flush stdout
290 ;;
291
292 let set_font_size mathview () =
293  mathview#set_font_size 24 ;
294  print_string "set_font_size: FONT IS NOW 24\n" ;
295  flush stdout
296 ;;
297  
298 let get_font_size mathview () =
299  print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
300  flush stdout
301 ;;
302  
303 let set_anti_aliasing mathview () =
304  mathview#set_anti_aliasing true ;
305  print_string "set_anti_aliasing: ON\n" ;
306  flush stdout
307 ;;
308  
309 let get_anti_aliasing mathview () =
310  print_string ("get_anti_aliasing: " ^
311   (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
312   "\n") ;
313  flush stdout
314 ;;
315  
316  (*
317 let set_kerning mathview () =
318  mathview#set_kerning true ;
319  print_string "set_kerning: ON\n" ;
320  flush stdout
321 ;;
322  
323 let get_kerning mathview () =
324  print_string ("get_kerning: " ^
325   (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
326   "\n") ;
327  flush stdout
328 ;;
329 *)
330
331 let set_log_verbosity mathview () =
332  mathview#set_log_verbosity 3 ;
333  print_string "set_log_verbosity: NOW IS 3\n" ;
334  flush stdout
335 ;;
336  
337 let get_log_verbosity mathview () =
338  print_string ("get_log_verbosity: " ^
339   string_of_int mathview#get_log_verbosity ^
340   "\n") ;
341  flush stdout
342 ;;
343
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";
347  flush stdout
348 ;;
349  
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#click (click mathview)) ;
413 ignore(mathview#connect#selection_changed (selection_changed mathview));
414 ignore(mathview#connect#element_over (element_over mathview)) ;
415 ignore(button_load_dom#connect#clicked (load_doc mathview)) ;
416 (* Main Loop *)
417 main_window#show () ;
418 GMain.Main.main ()
419 ;;