]> matita.cs.unibo.it Git - helm.git/blob - helm/annotationHelper/cicAnnotationHelper.ml
Main code clean-up.
[helm.git] / helm / annotationHelper / cicAnnotationHelper.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 03/04/2001                                 *)
32 (*                                                                            *)
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        *)
42 (* recomputed                                                                 *)
43 (*                                                                            *)
44 (******************************************************************************)
45
46 (* GLOBAL REFERENCES (USED BY CALLBACKS) *)
47
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      *)
53                                     (* annotations.                          *)
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 "";;
57
58 (* GLOBAL CONSTANTS *)
59
60 let helmns = Ominidom.o_mDOMString_of_string "http://www.cs.unibo.it/helm";;
61
62 (* MISC FUNCTIONS *)
63
64 let pathname_of_annuri uristring =
65  Configuration.annotations_dir ^
66   Str.replace_first (Str.regexp "^cic:") "" uristring
67 ;;
68
69 let make_dirs dirpath =
70  ignore (Unix.system ("mkdir -p " ^ dirpath))
71 ;;
72
73 module UrlManipulator =
74  struct
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;;
78
79   let uri_from_url url =
80    let module N = Neturl in
81    let founduri = ref None in
82    let foundann = ref None in
83     let rec find_uri =
84      function
85         [] -> raise (No_param_dot_CICURI_or_param_dot_annotations_found_in url)
86       | he::tl ->
87          match Str.split (Str.regexp "=") he with
88             ["param.CICURI";uri] ->
89               if !founduri <> None then
90                raise (Bad_formed_url url)
91               else
92                begin
93                 founduri := Some uri ;
94                 if !foundann = None then
95                  find_uri tl
96                end
97           | ["param.annotations";ann] ->
98               if !foundann <> None then
99                raise (Bad_formed_url url)
100               else
101                begin
102                 foundann :=
103                  Some
104                   (match ann with
105                       "yes" -> ".ann"
106                     | "no"  -> ""
107                     | _     -> raise (Bad_formed_url url)
108                   ) ;
109                 if !founduri = None then
110                  find_uri tl
111                end
112           | _ -> find_uri tl
113     in
114      find_uri
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
119       | _         , _         ->
120          raise (No_param_dot_CICURI_or_param_dot_annotations_found_in url)
121   ;;
122
123   let extractParam param url =
124    let module N = Neturl in
125     let rec find_param =
126      function
127         [] -> raise (No_param_found_in (param,url))
128       | he::tl ->
129          match Str.split (Str.regexp "=") he with
130             [name;value] when name = param -> value
131           | _ -> find_param tl
132     in
133      find_param
134       (Str.split (Str.regexp "&")
135        (N.url_query ~encoded:true (N.url_of_string N.ip_url_syntax url)))
136   ;;
137
138   let set_annotations_to_yes query url =
139    let found =ref false in
140    let rec aux =
141     function
142        [] ->
143         if !found then ""
144         else raise (No_param_found_in ("param.annotations",url))
145      | he::tl ->
146         match Str.split (Str.regexp "=") he with
147            ["param.annotations" as s ; ann] ->
148              found := true ;
149              let auxtl = aux tl in
150               s ^ "=yes" ^
151                (if auxtl = "" then "" else "&" ^ auxtl)
152          | [name ; value] ->
153             let auxtl = aux tl in
154              name ^ "=" ^ value ^
155               (if auxtl = "" then "" else "&" ^ auxtl)
156          | [name] ->
157             let auxtl = aux tl in
158              name ^ "=" ^
159               (if auxtl = "" then "" else "&" ^ auxtl)
160          | _ -> raise (Bad_formed_url url)
161    in
162     aux (Str.split (Str.regexp "&") query)
163   ;;
164
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)
171   ;;
172 end
173
174 let get_current_uri () =
175  UriManager.uri_of_string (UrlManipulator.uri_from_url !current_url)
176 ;;
177
178 (* CALLBACKS *)
179
180 let get_annotated_obj () =
181  match !annotated_obj with
182     None   ->
183      let annobj =
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)
188       in
189        let ids_to_targets = CicXPath.get_ids_to_targets annobj in
190         (annobj,ids_to_targets,ids_to_annotations)
191      in
192       annotated_obj := Some annobj ;
193       annobj
194   | Some annobj -> annobj
195 ;;
196
197 let update_output rendering_window url =
198  rendering_window#label#set_text (UrlManipulator.uri_from_url url) ;
199  rendering_window#output#load url
200 ;;
201
202 let choose_selection rendering_window (node : Ominidom.o_mDOMNode option) =
203  let module O = Ominidom in
204   let rec aux node =
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)
208   in
209    match node with
210      Some x -> aux x
211    | None   -> rendering_window#output#set_selection None
212 ;;
213
214 let annotateb_pressed rendering_window annotation_window () =
215  let module O = Ominidom in
216  match rendering_window#output#get_selection with
217    Some node ->
218     begin
219      match (node#get_attribute_ns (O.o_mDOMString_of_string "xref") helmns) with
220         Some xpath ->
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
228             (xpath#get_string) ;
229            annotation#delete_text 0 annotation#length ;
230            begin
231             match ann with
232                 None      ->
233                  annotation#misc#set_sensitive false ;
234                  annotation_window#radio_none#set_active true ;
235                  radio_some_status := false
236               | Some ann' ->
237                  annotation#insert ann' ;
238                  annotation#misc#set_sensitive true ;
239                  annotation_window#radio_some#set_active true ;
240                  radio_some_status := true
241            end ;
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")
245     end
246  | None -> rendering_window#label#set_text ("ERROR: No selection!!!\n")
247 ;;
248
249 let change_annotation ids_to_annotations id ann =
250  begin
251   try
252    Hashtbl.remove ids_to_annotations id
253   with
254    Not_found -> ()
255  end ;
256  match ann with
257     None -> ()
258   | Some ann' -> Hashtbl.add ids_to_annotations id ann'
259 ;;
260
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
267       None -> assert false
268     | Some (annobj,_,ids_to_annotations) -> annobj,ids_to_annotations
269   in
270    change_annotation ids_to_annotations
271     (match !current_id with
272         Some id -> id
273       | None -> assert false
274     )
275     (if !radio_some_status then
276       Some (annotation#get_chars 0 annotation#length)
277      else
278       None
279     ) ;
280    let uri = get_current_uri () in
281     let annxml =
282      CicAnnotation2Xml.pp_annotation annobj ids_to_annotations uri
283     in
284      make_dirs
285        (pathname_of_annuri (U.buri_of_uri uri)) ;
286      Xml.pp ~quiet:true annxml
287       (Some
288        (pathname_of_annuri (U.string_of_uri (U.annuri_of_uri uri)) ^
289         ".xml"
290        )
291       )
292 ;;
293
294 (* STUFF TO BUILD THE GTK INTERFACE *)
295
296 (* Stuff for the widget settings *)
297
298 let export_to_postscript (output : GMathView.math_view) () =
299  output#export_to_postscript ~filename:"output.ps" ();
300 ;;
301
302 let activate_t1 output button_set_anti_aliasing button_set_kerning 
303  button_set_transparency button_export_to_postscript button_t1 ()
304 =
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) ;
308   if is_set then
309    begin
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 ;
314    end
315   else
316    begin
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 ;
321    end
322 ;;
323
324 let set_anti_aliasing output button_set_anti_aliasing () =
325  output#set_anti_aliasing button_set_anti_aliasing#active
326 ;;
327
328 let set_kerning output button_set_kerning () =
329  output#set_kerning button_set_kerning#active
330 ;;
331
332 let set_transparency output button_set_transparency () =
333  output#set_transparency button_set_transparency#active
334 ;;
335
336 let changefont output font_size_spinb () =
337  output#set_font_size font_size_spinb#value_as_int
338 ;;
339
340 let set_log_verbosity output log_verbosity_spinb () =
341  output#set_log_verbosity log_verbosity_spinb#value_as_int
342 ;;
343
344 class settings_window output sw button_export_to_postscript
345  selection_changed_callback
346 =
347  let settings_window = GWindow.window ~title:"GtkMathView settings" () in
348  let vbox =
349   GPack.vbox ~packing:settings_window#add () in
350  let table =
351   GPack.table
352    ~rows:1 ~columns:3 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
353    ~border_width:5 ~packing:vbox#add () in
354  let button_t1 =
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
366  let table =
367   GPack.table
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 =
374   let sadj =
375    GData.adjustment ~value:14.0 ~lower:5.0 ~upper:50.0 ~step_incr:1.0 ()
376   in
377    GEdit.spin_button 
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 =
383   let sadj =
384    GData.adjustment ~value:0.0 ~lower:0.0 ~upper:3.0 ~step_incr:1.0 ()
385   in
386    GEdit.spin_button 
387     ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:1) () in
388  let hbox =
389   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
390  let closeb =
391   GButton.button ~label:"Close"
392    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
393 object(self)
394  method show = settings_window#show
395  initializer
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)
413 end;;
414
415 (* Main windows *)
416
417 class annotation_window output label =
418  let window_to_annotate =
419   GWindow.window ~title:"Annotating environment" ~border_width:2 () in
420  let hbox1 =
421   GPack.hbox ~packing:window_to_annotate#add () in
422  let vbox1 =
423   GPack.vbox ~packing:(hbox1#pack ~padding:5) () in
424  let hbox2 =
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)
431   ~active:true () in
432  let annotation = GEdit.text ~editable:true ~width:400 ~height:180
433   ~packing:(vbox1#pack ~padding:5) () in
434  let table =
435   GPack.table ~rows:3 ~columns:3 ~packing:(vbox1#pack ~padding:5) () in
436  let annotation_hints =
437   Array.init 9
438    (function i ->
439      GButton.button ~label:("Hint " ^ string_of_int i)
440       ~packing:(table#attach ~left:(i mod 3) ~top:(i / 3)) ()
441    ) in
442  let vbox2 =
443   GPack.vbox ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
444  let confirmb =
445   GButton.button ~label:"O.K."
446    ~packing:(vbox2#pack ~expand:false ~fill:false ~padding:5) () in
447  let abortb =
448   GButton.button ~label:"Abort"
449    ~packing:(vbox2#pack ~expand:false ~fill:false ~padding:5) () in
450 object (self)
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 ()
458  initializer
459   (* signal handlers here *)
460   ignore (window_to_annotate#event#connect#delete
461    (fun _ ->
462      window_to_annotate#misc#hide () ;
463      GMain.Grab.remove (window_to_annotate#coerce) ; 
464      true
465    )) ;
466   ignore (confirmb#connect#clicked
467    (fun () ->
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"
476        ) ;
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
481    )) ;
482   ignore (abortb#connect#clicked
483    (fun () ->
484      window_to_annotate#misc#hide () ;
485      GMain.Grab.remove (window_to_annotate#coerce)
486    ));
487   ignore (radio_some#connect#clicked
488    (fun () -> annotation#misc#set_sensitive true ; radio_some_status := true)) ;
489   ignore (radio_none #connect#clicked
490    (fun () ->
491      annotation#misc#set_sensitive false;
492      radio_some_status := false)
493    )
494 end;;
495
496 class rendering_window annotation_window output (label : GMisc.label) =
497  let window =
498   GWindow.window ~title:"MathML viewer" ~border_width:2 () in
499  let vbox =
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
506  let hbox =
507   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
508  let annotateb =
509   GButton.button ~label:"Annotate"
510    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
511  let settingsb =
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
517  let closeb =
518   GButton.button ~label:"Close"
519    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
520 object(self)
521  method label = label
522  method output = (output : GMathView.math_view)
523  method show () = window#show ()
524  initializer
525   button_export_to_postscript#misc#set_sensitive false ;
526
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 ))
536 end;;
537
538 (* MAIN *)
539
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
547     in
548      rendering_window#show () ;
549      rendering_window#label#set_text (UrlManipulator.uri_from_url url) ;
550      rendering_window#output#load tmpfile ;
551      GMain.Main.main ()
552 ;;
553
554 let _ =
555  let filename = ref "" in
556  let usage_msg =
557    "\nusage: annotationHelper[.opt] file url\n\n List of options:"
558  in
559   Arg.parse []
560    (fun x ->
561      if x = "" then raise (Arg.Bad "Empty filename or URL not allowed") ;
562      if !filename = "" then
563       filename := x
564      else if !current_url = "" then
565       current_url := x
566      else
567       begin
568        prerr_string "More than two arguments provided\n" ;
569        Arg.usage [] usage_msg ;
570        exit (-1)
571       end
572    ) usage_msg ;
573    Getter.getter_url :=
574     Netencoding.Url.decode
575      (UrlManipulator.extractParam "param.getterURL" !current_url) ;
576    initialize_everything !filename !current_url
577 ;;