1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM 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.
12 * HELM 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.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
33 (* This is a simple gtk interface to the Coq-like pretty printer cicPp for *)
34 (* cic terms exported in xml. It uses directly the modules cicPp and *)
35 (* cicCcache and indirectly all the other modules (cicParser, cicParser2, *)
36 (* cicParser3, getter). *)
37 (* The syntax is "gtkInterface[.opt] filename1 ... filenamen" where *)
38 (* filenamei is the path-name of an xml file describing a cic term. *)
39 (* The terms are loaded in cache and then pretty-printed one at a time and *)
40 (* only once, when the user wants to look at it: if the user wants to look at *)
41 (* a term again, then the pretty-printed term is showed again, but not *)
44 (******************************************************************************)
46 (* GLOBAL REFERENCES (USED BY CALLBACKS) *)
48 let annotated_obj = ref None;; (* reference to a triple option where *)
49 (* the first component is the current *)
50 (* annotated object, the second is the *)
51 (* map from ids to annotated targets and *)
52 (* the third is the map from ids to *)
54 let current_id = ref None;; (* id of the element to annotate *)
55 let radio_some_status = ref false;; (* is the radio_some button selected? *)
56 let current_url = ref "";;
58 (* GLOBAL CONSTANTS *)
60 let helmns = Ominidom.o_mDOMString_of_string "http://www.cs.unibo.it/helm";;
64 let pathname_of_annuri uristring =
65 Configuration.annotations_dir ^
66 Str.replace_first (Str.regexp "^cic:") "" uristring
69 let make_dirs dirpath =
70 ignore (Unix.system ("mkdir -p " ^ dirpath))
73 module UrlManipulator =
75 exception No_param_dot_CICURI_or_param_dot_annotations_found_in of string;;
76 exception No_param_found_in of string * string;;
77 exception Bad_formed_url of string;;
79 let uri_from_url url =
80 let module N = Neturl in
81 let founduri = ref None in
82 let foundann = ref None in
85 [] -> raise (No_param_dot_CICURI_or_param_dot_annotations_found_in url)
87 match Str.split (Str.regexp "=") he with
88 ["param.CICURI";uri] ->
89 if !founduri <> None then
90 raise (Bad_formed_url url)
93 founduri := Some uri ;
94 if !foundann = None then
97 | ["param.annotations";ann] ->
98 if !foundann <> None then
99 raise (Bad_formed_url url)
107 | _ -> raise (Bad_formed_url url)
109 if !founduri = None then
115 (Str.split (Str.regexp "&")
116 (N.url_query ~encoded:true (N.url_of_string N.ip_url_syntax url))) ;
117 match !founduri,!foundann with
118 (Some uri),(Some ann) -> uri ^ ann
120 raise (No_param_dot_CICURI_or_param_dot_annotations_found_in url)
123 let extractParam param url =
124 let module N = Neturl in
127 [] -> raise (No_param_found_in (param,url))
129 match Str.split (Str.regexp "=") he with
130 [name;value] when name = param -> value
134 (Str.split (Str.regexp "&")
135 (N.url_query ~encoded:true (N.url_of_string N.ip_url_syntax url)))
138 let set_annotations_to_yes query url =
139 let found =ref false in
144 else raise (No_param_found_in ("param.annotations",url))
146 match Str.split (Str.regexp "=") he with
147 ["param.annotations" as s ; ann] ->
149 let auxtl = aux tl in
151 (if auxtl = "" then "" else "&" ^ auxtl)
153 let auxtl = aux tl in
155 (if auxtl = "" then "" else "&" ^ auxtl)
157 let auxtl = aux tl in
159 (if auxtl = "" then "" else "&" ^ auxtl)
160 | _ -> raise (Bad_formed_url url)
162 aux (Str.split (Str.regexp "&") query)
165 let annurl_of_url url =
166 let module N = Neturl in
167 let nurl = N.url_of_string N.ip_url_syntax url in
168 let query = N.url_query ~encoded:true nurl in
169 let newquery = set_annotations_to_yes query url in
170 N.string_of_url (N.modify_url ~encoded:true ~query:newquery nurl)
174 let get_current_uri () =
175 UriManager.uri_of_string (UrlManipulator.uri_from_url !current_url)
180 let get_annotated_obj () =
181 match !annotated_obj with
184 let (annobj,ids_to_annotations) =
185 match CicCache.get_annobj (get_current_uri ()) with
186 (annobj,None) -> annobj, Hashtbl.create 503
187 | (annobj, Some ids_to_annotations) -> (annobj,ids_to_annotations)
189 let ids_to_targets = CicXPath.get_ids_to_targets annobj in
190 (annobj,ids_to_targets,ids_to_annotations)
192 annotated_obj := Some annobj ;
194 | Some annobj -> annobj
197 let update_output rendering_window url =
198 rendering_window#label#set_text (UrlManipulator.uri_from_url url) ;
199 rendering_window#output#load url
202 let choose_selection rendering_window (node : Ominidom.o_mDOMNode option) =
203 let module O = Ominidom in
205 match node#get_attribute_ns (O.o_mDOMString_of_string "xref") helmns with
206 Some _ -> rendering_window#output#set_selection (Some node)
207 | None -> aux (node#get_parent)
211 | None -> rendering_window#output#set_selection None
214 let annotateb_pressed rendering_window annotation_window () =
215 let module O = Ominidom in
216 match rendering_window#output#get_selection with
219 match (node#get_attribute_ns (O.o_mDOMString_of_string "xref") helmns) with
221 let annobj = get_annotated_obj () in
222 let (anno, ids_to_targets, ids_to_annotations) = annobj in
223 let annotation = (annotation_window#annotation : GEdit.text) in
224 let id = xpath#get_string in
225 current_id := Some id ;
226 let ann = CicXPath.get_annotation ids_to_annotations id in
227 CicAnnotationHinter.create_hints annotation_window ids_to_targets
229 annotation#delete_text 0 annotation#length ;
233 annotation#misc#set_sensitive false ;
234 annotation_window#radio_none#set_active true ;
235 radio_some_status := false
237 annotation#insert ann' ;
238 annotation#misc#set_sensitive true ;
239 annotation_window#radio_some#set_active true ;
240 radio_some_status := true
242 GMain.Grab.add (annotation_window#window_to_annotate#coerce) ;
243 annotation_window#show () ;
244 | None -> rendering_window#label#set_text ("ERROR: No xref found!!!\n")
246 | None -> rendering_window#label#set_text ("ERROR: No selection!!!\n")
249 let change_annotation ids_to_annotations id ann =
252 Hashtbl.remove ids_to_annotations id
258 | Some ann' -> Hashtbl.add ids_to_annotations id ann'
261 (* called when the annotation is confirmed *)
262 let save_annotation annotation =
263 let module S = Str in
264 let module U = UriManager in
265 let (annobj,ids_to_annotations) =
266 match !annotated_obj with
268 | Some (annobj,_,ids_to_annotations) -> annobj,ids_to_annotations
270 change_annotation ids_to_annotations
271 (match !current_id with
273 | None -> assert false
275 (if !radio_some_status then
276 Some (annotation#get_chars 0 annotation#length)
280 let uri = get_current_uri () in
282 CicAnnotation2Xml.pp_annotation annobj ids_to_annotations uri
285 (pathname_of_annuri (U.buri_of_uri uri)) ;
286 Xml.pp ~quiet:true annxml
288 (pathname_of_annuri (U.string_of_uri (U.annuri_of_uri uri)) ^
294 (* STUFF TO BUILD THE GTK INTERFACE *)
296 (* Stuff for the widget settings *)
298 let export_to_postscript (output : GMathView.math_view) () =
299 output#export_to_postscript ~filename:"output.ps" ();
302 let activate_t1 output button_set_anti_aliasing button_set_kerning
303 button_set_transparency button_export_to_postscript button_t1 ()
305 let is_set = button_t1#active in
306 output#set_font_manager_type
307 (if is_set then `font_manager_t1 else `font_manager_gtk) ;
310 button_set_anti_aliasing#misc#set_sensitive true ;
311 button_set_kerning#misc#set_sensitive true ;
312 button_set_transparency#misc#set_sensitive true ;
313 button_export_to_postscript#misc#set_sensitive true ;
317 button_set_anti_aliasing#misc#set_sensitive false ;
318 button_set_kerning#misc#set_sensitive false ;
319 button_set_transparency#misc#set_sensitive false ;
320 button_export_to_postscript#misc#set_sensitive false ;
324 let set_anti_aliasing output button_set_anti_aliasing () =
325 output#set_anti_aliasing button_set_anti_aliasing#active
328 let set_kerning output button_set_kerning () =
329 output#set_kerning button_set_kerning#active
332 let set_transparency output button_set_transparency () =
333 output#set_transparency button_set_transparency#active
336 let changefont output font_size_spinb () =
337 output#set_font_size font_size_spinb#value_as_int
340 let set_log_verbosity output log_verbosity_spinb () =
341 output#set_log_verbosity log_verbosity_spinb#value_as_int
344 class settings_window output sw button_export_to_postscript
345 selection_changed_callback
347 let settings_window = GWindow.window ~title:"GtkMathView settings" () in
349 GPack.vbox ~packing:settings_window#add () in
352 ~rows:1 ~columns:3 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
353 ~border_width:5 ~packing:vbox#add () in
355 GButton.toggle_button ~label:"activate t1 fonts"
356 ~packing:(table#attach ~left:0 ~top:0) () in
357 let button_set_anti_aliasing =
358 GButton.toggle_button ~label:"set_anti_aliasing"
359 ~packing:(table#attach ~left:0 ~top:1) () in
360 let button_set_kerning =
361 GButton.toggle_button ~label:"set_kerning"
362 ~packing:(table#attach ~left:1 ~top:1) () in
363 let button_set_transparency =
364 GButton.toggle_button ~label:"set_transparency"
365 ~packing:(table#attach ~left:2 ~top:1) () in
368 ~rows:2 ~columns:2 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
369 ~border_width:5 ~packing:vbox#add () in
370 let font_size_label =
371 GMisc.label ~text:"font size:"
372 ~packing:(table#attach ~left:0 ~top:0 ~expand:`NONE) () in
373 let font_size_spinb =
375 GData.adjustment ~value:14.0 ~lower:5.0 ~upper:50.0 ~step_incr:1.0 ()
378 ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:0 ~fill:`NONE) () in
379 let log_verbosity_label =
380 GMisc.label ~text:"log verbosity:"
381 ~packing:(table#attach ~left:0 ~top:1) () in
382 let log_verbosity_spinb =
384 GData.adjustment ~value:0.0 ~lower:0.0 ~upper:3.0 ~step_incr:1.0 ()
387 ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:1) () in
389 GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
391 GButton.button ~label:"Close"
392 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
394 method show = settings_window#show
396 button_set_anti_aliasing#misc#set_sensitive false ;
397 button_set_kerning#misc#set_sensitive false ;
398 button_set_transparency#misc#set_sensitive false ;
399 (* Signals connection *)
400 ignore(button_t1#connect#clicked
401 (activate_t1 output button_set_anti_aliasing button_set_kerning
402 button_set_transparency button_export_to_postscript button_t1)) ;
403 ignore(font_size_spinb#connect#changed (changefont output font_size_spinb)) ;
404 ignore(button_set_anti_aliasing#connect#toggled
405 (set_anti_aliasing output button_set_anti_aliasing));
406 ignore(button_set_kerning#connect#toggled
407 (set_kerning output button_set_kerning)) ;
408 ignore(button_set_transparency#connect#toggled
409 (set_transparency output button_set_transparency)) ;
410 ignore(log_verbosity_spinb#connect#changed
411 (set_log_verbosity output log_verbosity_spinb)) ;
412 ignore(closeb#connect#clicked settings_window#misc#hide)
417 class annotation_window output label =
418 let window_to_annotate =
419 GWindow.window ~title:"Annotating environment" ~border_width:2 () in
421 GPack.hbox ~packing:window_to_annotate#add () in
423 GPack.vbox ~packing:(hbox1#pack ~padding:5) () in
425 GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
426 let radio_some = GButton.radio_button ~label:"Annotation below"
427 ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
428 let radio_none = GButton.radio_button ~label:"No annotation"
429 ~group:radio_some#group
430 ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5)
432 let annotation = GEdit.text ~editable:true ~width:400 ~height:180
433 ~packing:(vbox1#pack ~padding:5) () in
435 GPack.table ~rows:3 ~columns:3 ~packing:(vbox1#pack ~padding:5) () in
436 let annotation_hints =
439 GButton.button ~label:("Hint " ^ string_of_int i)
440 ~packing:(table#attach ~left:(i mod 3) ~top:(i / 3)) ()
443 GPack.vbox ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
445 GButton.button ~label:"O.K."
446 ~packing:(vbox2#pack ~expand:false ~fill:false ~padding:5) () in
448 GButton.button ~label:"Abort"
449 ~packing:(vbox2#pack ~expand:false ~fill:false ~padding:5) () in
451 method window_to_annotate = window_to_annotate
452 method annotation = annotation
453 method radio_some = radio_some
454 method radio_none = radio_none
455 method annotation_hints = annotation_hints
456 method output = (output : GMathView.math_view)
457 method show () = window_to_annotate#show ()
459 (* signal handlers here *)
460 ignore (window_to_annotate#event#connect#delete
462 window_to_annotate#misc#hide () ;
463 GMain.Grab.remove (window_to_annotate#coerce) ;
466 ignore (confirmb#connect#clicked
468 window_to_annotate#misc#hide () ;
469 save_annotation annotation ;
470 GMain.Grab.remove (window_to_annotate#coerce) ;
471 let new_current_uri = UriManager.annuri_of_uri (get_current_uri ()) in
472 Getter.register new_current_uri
473 (Configuration.annotations_url ^
474 Str.replace_first (Str.regexp "^cic:") ""
475 (UriManager.string_of_uri new_current_uri) ^ ".xml"
477 let new_current_url = UrlManipulator.annurl_of_url !current_url in
478 current_url := new_current_url ;
479 label#set_text (UriManager.string_of_uri new_current_uri) ;
480 output#load new_current_url
482 ignore (abortb#connect#clicked
484 window_to_annotate#misc#hide () ;
485 GMain.Grab.remove (window_to_annotate#coerce)
487 ignore (radio_some#connect#clicked
488 (fun () -> annotation#misc#set_sensitive true ; radio_some_status := true)) ;
489 ignore (radio_none #connect#clicked
491 annotation#misc#set_sensitive false;
492 radio_some_status := false)
496 class rendering_window annotation_window output (label : GMisc.label) =
498 GWindow.window ~title:"MathML viewer" ~border_width:2 () in
500 GPack.vbox ~packing:window#add () in
501 let _ = vbox#pack ~expand:false ~fill:false ~padding:5 label#coerce in
502 let scrolled_window0 =
503 GBin.scrolled_window ~border_width:10
504 ~packing:(vbox#pack ~expand:true ~padding:5) () in
505 let _ = scrolled_window0#add output#coerce in
507 GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
509 GButton.button ~label:"Annotate"
510 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
512 GButton.button ~label:"Settings"
513 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
514 let button_export_to_postscript =
515 GButton.button ~label:"export_to_postscript"
516 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
518 GButton.button ~label:"Close"
519 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
522 method output = (output : GMathView.math_view)
523 method show () = window#show ()
525 button_export_to_postscript#misc#set_sensitive false ;
527 (* signal handlers here *)
528 ignore(output#connect#selection_changed (choose_selection self)) ;
529 ignore(closeb#connect#clicked (fun _ -> GMain.Main.quit ())) ;
530 ignore(annotateb#connect#clicked (annotateb_pressed self annotation_window)) ;
531 let settings_window = new settings_window output scrolled_window0
532 button_export_to_postscript (choose_selection self) in
533 ignore(settingsb#connect#clicked settings_window#show) ;
534 ignore(button_export_to_postscript#connect#clicked (export_to_postscript output)) ;
535 ignore(window#event#connect#delete (fun _ -> GMain.Main.quit () ; true ))
540 let initialize_everything tmpfile url =
541 let module U = Unix in
542 let output = GMathView.math_view ~width:400 ~height:380 ()
543 and label = GMisc.label ~text:"???" () in
544 let annotation_window = new annotation_window output label in
545 let rendering_window =
546 new rendering_window annotation_window output label
548 rendering_window#show () ;
549 rendering_window#label#set_text (UrlManipulator.uri_from_url url) ;
550 rendering_window#output#load tmpfile ;
555 let filename = ref "" in
557 "\nusage: annotationHelper[.opt] file url\n\n List of options:"
561 if x = "" then raise (Arg.Bad "Empty filename or URL not allowed") ;
562 if !filename = "" then
564 else if !current_url = "" then
568 prerr_string "More than two arguments provided\n" ;
569 Arg.usage [] usage_msg ;
574 Netencoding.Url.decode
575 (UrlManipulator.extractParam "param.getterURL" !current_url) ;
576 initialize_everything !filename !current_url