]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/test/test.ml
First commit towards the 0.2.8 version.
[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 (node : Ominidom.o_mDOMNode option) =
33  let module O = Ominidom in
34   print_string ("selection_changed: " ^
35    (match node with
36        None -> "selection_changed on nothing"
37      | Some node ->
38        match node#get_name with
39        | Some x -> x#get_string
40        | None   -> "on element without name"
41    ) ^ "\n");
42   mathview#set_selection node;
43   flush stdout
44 ;;
45
46
47 let rec jump (node : Ominidom.o_mDOMNode) =
48  let module O = Ominidom in
49   match node#get_attribute (O.o_mDOMString_of_string "href") with
50      None ->
51       begin
52        try
53         let p = node#get_parent in
54          jump p
55        with
56           O.Node_has_no_parent ->
57            print_string "jump: NO HREF FOR THIS NODE\n" ;
58            flush stdout ;
59            false
60       end
61    | Some x ->
62       print_string ("jump: " ^ x#get_string ^ "\n") ;
63       flush stdout ;
64       true
65 ;;
66
67 let clicked mathview (node : Ominidom.o_mDOMNode) =
68  let module O = Ominidom in
69   if not (jump node) then
70    match mathview#get_action with
71       Some n ->
72        mathview#action_toggle ;
73        print_string ("current action selection: " ^
74         string_of_int mathview#action_get_selected ^ "\n") ;
75        flush stdout ;
76     | None ->
77        print_string ("mouse is on: " ^
78         (match (mathview#get_element : Ominidom.o_mDOMNode option) with
79             Some e ->
80              (match e#get_name with
81                  Some x -> x#get_string
82                | None   -> "no name"
83              )
84           | None -> "NO ELEMENT!\n"
85          ) ^ "\n") ;
86        print_string ("clicked: " ^
87         (match node#get_name with
88            Some x -> x#get_string
89          | None   -> "no name"
90         ) ^ "\n") ;
91        flush stdout
92 ;;
93
94
95 let activate_t1 mathview () =
96  mathview#set_font_manager_type `font_manager_t1;
97  print_string "WIDGET SET WITH T1 FONTS\n" ;
98  flush stdout
99 ;;
100
101 let activate_gtk mathview () =
102  mathview#set_font_manager_type `font_manager_gtk;
103  print_string "WIDGET SET WITH GTK FONTS\n" ;
104  flush stdout
105 ;;
106
107 let get_font_manager_type mathview () =
108  print_string "CURRENT FONT MANAGER TYPE: ";
109  begin
110   match mathview#get_font_manager_type with
111   | `font_manager_t1 -> print_string "T1"
112   | `font_manager_gtk -> print_string "GTK"
113  end;
114  print_newline();
115  flush stdout
116 ;;
117
118 let get_transparency mathview () =
119  print_string ("CURRENT TRANSPARENCY: " ^
120   (if mathview#get_transparency then "ON" else "OFF") ^ "\n") ;
121  flush stdout
122 ;;
123
124 let set_transparency mathview () =
125  mathview#set_transparency (not mathview#get_transparency) ;
126  print_string "TRANSPARENCY CHANGED\n" ;
127  flush stdout
128 ;;
129
130
131 let load mathview () =
132  mathview#load "test.xml" ;
133  print_string "load: SEEMS TO WORK\n" ;
134  flush stdout
135 ;;
136
137 let load_dom mathview () =
138  mathview#load_tree (new Ominidom.o_mDOMDoc (Minidom.doc_load "test.xml")) ;
139  print_string "load from DOM: SEEMS TO WORK\n" ;
140  flush stdout
141 ;;
142
143 let get_selection mathview () =
144  let module O = Ominidom in
145   let selection =
146     match mathview#get_selection with
147     | Some node ->
148       begin
149         match node#get_name with
150         | Some name -> name#get_string
151         | None      -> "element with no name!"
152       end
153     | None      -> "no selection!"
154   in
155    print_string ("get_selection: " ^ selection ^ "\n") ;
156    flush stdout
157 ;;
158
159 let set_selection mathview () =
160  let module O = Ominidom in
161   begin
162     match mathview#get_selection with
163     | Some node -> 
164       begin
165         try 
166           let parent_node = node#get_parent in
167           mathview#set_selection (Some parent_node);
168           print_string "set selection: SEEMS TO WORK\n"
169         with
170           _ -> print_string "EXCEPTION: no parent\n"
171       end
172     | None ->
173       mathview#set_selection None;
174       print_string "no selection\n"
175   end ;
176   flush stdout
177 ;;
178
179 let unload mathview () =
180  mathview#unload ;
181  print_string "unload: SEEMS TO WORK\n" ;
182  flush stdout
183 ;;
184
185 let get_width mathview () =
186  print_string ("get_width: " ^ string_of_int (mathview#get_width) ^ "\n") ;
187  flush stdout
188 ;;
189
190 let get_height mathview () =
191  print_string ("get_height: " ^ string_of_int (mathview#get_height) ^ "\n") ;
192  flush stdout
193 ;;
194
195 let get_top mathview () =
196  let (x,y) = mathview#get_top in
197   print_string ("get_top: ("^ string_of_int x ^ "," ^ string_of_int y ^ ")\n") ;
198   flush stdout
199 ;;
200
201 let set_top mathview () =
202  mathview#set_top 0 0;
203  print_string "set_top: SEEM TO WORK\n" ;
204  flush stdout
205 ;;
206
207 let set_adjustments mathview () =
208  let adj1 = GData.adjustment () in
209  let adj2 = GData.adjustment () in
210   mathview#set_adjustments adj1 adj2 ;
211   adj1#set_value ((adj1#lower +. adj1#upper) /. 2.0) ;
212   adj2#set_value ((adj2#lower +. adj2#upper) /. 2.0) ;
213   print_string "set_adjustments: SEEM TO WORK\n" ;
214   flush stdout
215 ;;
216
217 let get_hadjustment mathview () =
218  let adj = mathview#get_hadjustment in
219   adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
220   print_string "get_hadjustment: SEEM TO WORK\n" ;
221   flush stdout
222 ;;
223
224 let get_vadjustment mathview () =
225  let adj = mathview#get_vadjustment in
226   adj#set_value ((adj#lower +. adj#upper) /. 2.0) ;
227   print_string "get_vadjustment: SEEM TO WORK\n" ;
228   flush stdout
229 ;;
230
231 let get_buffer mathview () =
232  let buffer = mathview#get_buffer in
233   Gdk.Draw.rectangle buffer (Gdk.GC.create buffer) ~x:0 ~y:0
234    ~width:50 ~height:50 ~filled:true () ;
235   print_string "get_buffer: SEEMS TO WORK (hint: force the widget redrawing)\n";
236   flush stdout
237 ;;
238
239 let get_frame mathview () =
240  let frame = mathview#get_frame in
241   frame#set_shadow_type `NONE ;
242   print_string "get_frame: SEEMS TO WORK\n" ;
243   flush stdout
244 ;;
245
246 let set_font_size mathview () =
247  mathview#set_font_size 24 ;
248  print_string "set_font_size: FONT IS NOW 24\n" ;
249  flush stdout
250 ;;
251  
252 let get_font_size mathview () =
253  print_string ("get_font_size: " ^ string_of_int (mathview#get_font_size) ^ "\n") ;
254  flush stdout
255 ;;
256  
257 let set_anti_aliasing mathview () =
258  mathview#set_anti_aliasing true ;
259  print_string "set_anti_aliasing: ON\n" ;
260  flush stdout
261 ;;
262  
263 let get_anti_aliasing mathview () =
264  print_string ("get_anti_aliasing: " ^
265   (match mathview#get_anti_aliasing with true -> "ON" | false -> "OFF") ^
266   "\n") ;
267  flush stdout
268 ;;
269  
270 let set_kerning mathview () =
271  mathview#set_kerning true ;
272  print_string "set_kerning: ON\n" ;
273  flush stdout
274 ;;
275  
276 let get_kerning mathview () =
277  print_string ("get_kerning: " ^
278   (match mathview#get_kerning with true -> "ON" | false -> "OFF") ^
279   "\n") ;
280  flush stdout
281 ;;
282
283 let set_log_verbosity mathview () =
284  mathview#set_log_verbosity 3 ;
285  print_string "set_log_verbosity: NOW IS 3\n" ;
286  flush stdout
287 ;;
288  
289 let get_log_verbosity mathview () =
290  print_string ("get_log_verbosity: " ^
291   string_of_int mathview#get_log_verbosity ^
292   "\n") ;
293  flush stdout
294 ;;
295
296 let export_to_postscript (mathview : GMathView.math_view) () =
297  mathview#export_to_postscript ~filename:"test.ps" ();
298  print_string "expor_to_postscript: SEEMS TO WORK (hint: look at test.ps)\n";
299  flush stdout
300 ;;
301  
302 (* Widget creation *)
303 let main_window = GWindow.window ~title:"GtkMathView test" () in
304 let vbox = GPack.vbox ~packing:main_window#add () in
305 let sw = GBin.scrolled_window ~width:50 ~height:50 ~packing:vbox#pack () in
306 let mathview= GMathView.math_view ~packing:sw#add ~width:50 ~height:50 () in
307 let table = GPack.table ~rows:6 ~columns:5 ~packing:vbox#pack () in
308 let button_gtk=GButton.button ~label:"activate Gtk fonts" ~packing:(table#attach ~left:0 ~top:0) () in
309 let button_load = GButton.button ~label:"load" ~packing:(table#attach ~left:1 ~top:0) () in
310 let button_unload = GButton.button ~label:"unload" ~packing:(table#attach ~left:2 ~top:0) () in
311 let button_get_selection = GButton.button ~label:"get_selection" ~packing:(table#attach ~left:3 ~top:0) () in
312 let button_set_selection = GButton.button ~label:"set_selection" ~packing:(table#attach ~left:4 ~top:0) () in
313 let button_get_width = GButton.button ~label:"get_width" ~packing:(table#attach ~left:0 ~top:1) () in
314 let button_get_height = GButton.button ~label:"get_height" ~packing:(table#attach ~left:1 ~top:1) () in
315 let button_get_top = GButton.button ~label:"get_top" ~packing:(table#attach ~left:2 ~top:1) () in
316 let button_set_top = GButton.button ~label:"set_top" ~packing:(table#attach ~left:3 ~top:1) () in
317 let button_set_adjustments = GButton.button ~label:"set_adjustments" ~packing:(table#attach ~left:4 ~top:1) () in
318 let button_get_hadjustment = GButton.button ~label:"get_hadjustment" ~packing:(table#attach ~left:0 ~top:2) () in
319 let button_get_vadjustment = GButton.button ~label:"get_vadjustment" ~packing:(table#attach ~left:1 ~top:2) () in
320 let button_get_buffer = GButton.button ~label:"get_buffer" ~packing:(table#attach ~left:2 ~top:2) () in
321 let button_get_frame = GButton.button ~label:"get_frame" ~packing:(table#attach ~left:3 ~top:2) () in
322 let button_set_font_size = GButton.button ~label:"set_font_size" ~packing:(table#attach ~left:4 ~top:2) () in
323 let button_get_font_size = GButton.button ~label:"get_font_size" ~packing:(table#attach ~left:0 ~top:3) () in
324 let button_set_anti_aliasing = GButton.button ~label:"set_anti_aliasing" ~packing:(table#attach ~left:1 ~top:3) () in
325 let button_get_anti_aliasing = GButton.button ~label:"get_anti_aliasing" ~packing:(table#attach ~left:2 ~top:3) () in
326 let button_set_kerning = GButton.button ~label:"set_kerning" ~packing:(table#attach ~left:3 ~top:3) () in
327 let button_get_kerning = GButton.button ~label:"get_kerning" ~packing:(table#attach ~left:4 ~top:3) () in
328 let button_set_log_verbosity = GButton.button ~label:"set_log_verbosity" ~packing:(table#attach ~left:0 ~top:4) () in
329 let button_get_log_verbosity = GButton.button ~label:"get_log_verbosity" ~packing:(table#attach ~left:1 ~top:4) () in
330 let button_export_to_postscript = GButton.button ~label:"export_to_postscript" ~packing:(table#attach ~left:2 ~top:4) () in
331 let button_t1 = GButton.button ~label:"activate T1 fonts" ~packing:(table#attach ~left:3 ~top:4) () in
332 let button_get_font_manager_type = GButton.button ~label:"get_font_manager" ~packing:(table#attach ~left:4 ~top:4) () in
333 let button_get_transparency = GButton.button ~label:"get_transparency" ~packing:(table#attach ~left:0 ~top:5) () in
334 let button_set_transparency = GButton.button ~label:"set_transparency" ~packing:(table#attach ~left:1 ~top:5) () in
335 let button_load_dom = GButton.button ~label:"load from DOM" ~packing:(table#attach ~left:2 ~top:5) () in
336 (* Signals connection *)
337 ignore(button_gtk#connect#clicked (activate_gtk mathview)) ;
338 ignore(button_load#connect#clicked (load mathview)) ;
339 ignore(button_unload#connect#clicked (unload mathview)) ;
340 ignore(button_get_selection#connect#clicked (get_selection mathview)) ;
341 ignore(button_set_selection#connect#clicked (set_selection mathview)) ;
342 ignore(button_get_width#connect#clicked (get_width mathview)) ;
343 ignore(button_get_height#connect#clicked (get_height mathview)) ;
344 ignore(button_get_top#connect#clicked (get_top mathview)) ;
345 ignore(button_set_top#connect#clicked (set_top mathview)) ;
346 ignore(button_set_adjustments#connect#clicked (set_adjustments mathview)) ;
347 ignore(button_get_hadjustment#connect#clicked (get_hadjustment mathview)) ;
348 ignore(button_get_vadjustment#connect#clicked (get_vadjustment mathview)) ;
349 ignore(button_get_buffer#connect#clicked (get_buffer mathview)) ;
350 ignore(button_get_frame#connect#clicked (get_frame mathview)) ;
351 ignore(button_set_font_size#connect#clicked (set_font_size mathview)) ;
352 ignore(button_get_font_size#connect#clicked (get_font_size mathview)) ;
353 ignore(button_set_anti_aliasing#connect#clicked (set_anti_aliasing mathview)) ;
354 ignore(button_get_anti_aliasing#connect#clicked (get_anti_aliasing mathview)) ;
355 ignore(button_set_kerning#connect#clicked (set_kerning mathview)) ;
356 ignore(button_get_kerning#connect#clicked (get_kerning mathview)) ;
357 ignore(button_set_log_verbosity#connect#clicked (set_log_verbosity mathview)) ;
358 ignore(button_get_log_verbosity#connect#clicked (get_log_verbosity mathview)) ;
359 ignore(button_export_to_postscript#connect#clicked (export_to_postscript mathview)) ;
360 ignore(button_t1#connect#clicked (activate_t1 mathview)) ;
361 ignore(button_get_font_manager_type#connect#clicked (get_font_manager_type mathview)) ;
362 ignore(button_get_transparency#connect#clicked (get_transparency mathview)) ;
363 ignore(button_set_transparency#connect#clicked (set_transparency mathview)) ;
364 ignore(mathview#connect#clicked (clicked mathview)) ;
365 ignore(mathview#connect#selection_changed (selection_changed mathview)) ;
366 ignore(button_load_dom#connect#clicked (load_dom mathview)) ;
367 (* Main Loop *)
368 main_window#show () ;
369 GMain.Main.main ()
370 ;;