1 (******************************************************************************)
5 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
8 (* This is a simple gtk interface to the Coq-like pretty printer cicPp for *)
9 (* cic terms exported in xml. It uses directly the modules cicPp and *)
10 (* cicCcache and indirectly all the other modules (cicParser, cicParser2, *)
11 (* cicParser3, getter). *)
12 (* The syntax is "gtkInterface[.opt] filename1 ... filenamen" where *)
13 (* filenamei is the path-name of an xml file describing a cic term. *)
14 (* The terms are loaded in cache and then pretty-printed one at a time and *)
15 (* only once, when the user wants to look at it: if the user wants to look at *)
16 (* a term again, then the pretty-printed term is showed again, but not *)
19 (******************************************************************************)
21 (* DEFINITION OF THE URI TREE AND USEFUL FUNCTIONS ON IT *)
24 Dir of string * item list ref
25 | File of string * UriManager.uri
29 let theoryuritree = ref []
34 | File (name,_) -> name
40 | File (_,uri) -> Some uri
43 (* STUFF TO BUILD THE URI TREE *)
46 exception DuplicatedUri
47 exception ConflictingUris
49 let insert_in_uri_tree uri =
54 let _ = List.find (fun item -> name = get_name item) !l in
57 Not_found -> l := (File (name,uri))::!l
61 match List.find (fun item -> name = get_name item) !l with
62 Dir (_,children) -> aux children tl
63 | File _ -> raise ConflictingUris
66 let children = ref [] in
67 l := (Dir (name,children))::!l ;
70 | [] -> raise EmptyUri
75 (* Imperative procedure that builds the two uri trees *)
76 let build_uri_tree () =
77 let dbh = Dbm.opendbm Configuration.uris_dbm [Dbm.Dbm_rdonly] 0 in
80 let cicregexp = Str.regexp "cic:"
81 and theoryregexp = Str.regexp "theory:" in
82 if Str.string_match cicregexp uri 0 then
83 let s = Str.replace_first cicregexp "" uri in
84 let l = Str.split (Str.regexp "/") s in
85 insert_in_uri_tree (UriManager.uri_of_string uri) uritree l
86 else if Str.string_match theoryregexp uri 0 then
87 let s = Str.replace_first theoryregexp "" uri in
88 let l = Str.split (Str.regexp "/") s in
89 insert_in_uri_tree (UriManager.uri_of_string uri) theoryuritree l
94 (* GLOBAL REFERENCES (USED BY CALLBACKS) *)
96 let annotated_obj = ref None;; (* reference to a couple option where *)
97 (* the first component is the current *)
98 (* annotated object and the second is *)
99 (* the map from ids to annotated targets *)
100 let ann = ref (ref None);; (* current annotation *)
101 let radio_some_status = ref false;; (* is the radio_some button selected? *)
103 let theory_visited_uris = ref [];;
104 let theory_to_visit_uris = ref [];;
105 let visited_uris = ref [];;
106 let to_visit_uris = ref [];;
110 exception NoCurrentUri;;
111 exception NoNextOrPrevUri;;
112 exception GtkInterfaceInternalError;;
114 let theory_get_current_uri () =
115 match !theory_visited_uris with
116 [] -> raise NoCurrentUri
120 let get_current_uri () =
121 match !visited_uris with
122 [] -> raise NoCurrentUri
126 let get_annotated_obj () =
127 match !annotated_obj with
129 let (annobj, ids_to_targets,_) =
130 (CicCache.get_annobj (get_current_uri ()))
132 annotated_obj := Some (annobj, ids_to_targets) ;
133 (annobj, ids_to_targets)
134 | Some annobj -> annobj
137 let filename_of_uri uri =
141 let theory_update_output rendering_window uri =
142 rendering_window#label#set_text (UriManager.string_of_uri uri) ;
143 ignore (rendering_window#errors#delete_text 0 rendering_window#errors#length) ;
144 let mmlfile = XsltProcessor.process uri true "theory" in
145 rendering_window#output#load mmlfile
148 let update_output rendering_window uri =
149 rendering_window#label#set_text (UriManager.string_of_uri uri) ;
150 ignore (rendering_window#errors#delete_text 0 rendering_window#errors#length) ;
151 let mmlfile = XsltProcessor.process uri true "cic" in
152 rendering_window#output#load mmlfile
155 let theory_next rendering_window () =
156 match !theory_to_visit_uris with
157 [] -> raise NoNextOrPrevUri
159 theory_to_visit_uris := tl ;
160 theory_visited_uris := uri::!theory_visited_uris ;
161 theory_update_output rendering_window uri ;
162 rendering_window#prevb#misc#set_sensitive true ;
164 rendering_window#nextb#misc#set_sensitive false
167 let next rendering_window () =
168 match !to_visit_uris with
169 [] -> raise NoNextOrPrevUri
171 to_visit_uris := tl ;
172 visited_uris := uri::!visited_uris ;
173 annotated_obj := None ;
174 update_output rendering_window uri ;
175 rendering_window#prevb#misc#set_sensitive true ;
177 rendering_window#nextb#misc#set_sensitive false
180 let theory_prev rendering_window () =
181 match !theory_visited_uris with
182 [] -> raise NoCurrentUri
183 | [_] -> raise NoNextOrPrevUri
184 | uri::(uri'::tl as newvu) ->
185 theory_visited_uris := newvu ;
186 theory_to_visit_uris := uri::!theory_to_visit_uris ;
187 theory_update_output rendering_window uri' ;
188 rendering_window#nextb#misc#set_sensitive true ;
190 rendering_window#prevb#misc#set_sensitive false
193 let prev rendering_window () =
194 match !visited_uris with
195 [] -> raise NoCurrentUri
196 | [_] -> raise NoNextOrPrevUri
197 | uri::(uri'::tl as newvu) ->
198 visited_uris := newvu ;
199 to_visit_uris := uri::!to_visit_uris ;
200 annotated_obj := None ;
201 update_output rendering_window uri' ;
202 rendering_window#nextb#misc#set_sensitive true ;
204 rendering_window#prevb#misc#set_sensitive false
207 (* called when an hyperlink is clicked *)
208 let jump rendering_window node =
209 let module M = Minidom in
211 match M.node_get_attribute node (M.mDOMString_of_string "href") with
213 | Some s -> M.string_of_mDOMString s
215 let uri = UriManager.uri_of_string s in
216 rendering_window#show () ;
217 rendering_window#prevb#misc#set_sensitive true ;
218 rendering_window#nextb#misc#set_sensitive false ;
219 visited_uris := uri::!visited_uris ;
220 to_visit_uris := [] ;
221 annotated_obj := None ;
222 update_output rendering_window uri
225 let choose_selection rendering_window node =
226 let module M = Minidom in
227 let rec aux ~first_time node =
228 match M.node_get_attribute node (M.mDOMString_of_string "xref") with
231 match M.node_get_parent node with
233 | Some parent -> parent
235 aux ~first_time:false parent
237 if not first_time then
238 rendering_window#output#set_selection (Some node)
241 None -> () (* No element selected *)
242 | Some node -> aux ~first_time:true node
245 let changefont rendering_window () =
246 rendering_window#output#set_font_size rendering_window#spinb#value_as_int
250 let theory_selection_changed rendering_window uri () =
254 if !theory_visited_uris <> [] then
255 rendering_window#prevb#misc#set_sensitive true ;
256 rendering_window#nextb#misc#set_sensitive false ;
257 theory_visited_uris := uri'::!theory_visited_uris ;
258 theory_to_visit_uris := [] ;
259 rendering_window#show () ;
260 theory_update_output rendering_window uri'
263 let selection_changed rendering_window uri () =
267 if !visited_uris <> [] then
268 rendering_window#prevb#misc#set_sensitive true ;
269 rendering_window#nextb#misc#set_sensitive false ;
270 visited_uris := uri'::!visited_uris ;
271 to_visit_uris := [] ;
272 annotated_obj := None ;
273 rendering_window#show () ;
274 update_output rendering_window uri'
277 (* CSC: unificare con la creazione la prima volta *)
278 let rec updateb_pressed theory_rendering_window rendering_window
279 (sw1, sw ,(hbox : GPack.box)) mktree ()
282 (* let's empty the uri trees and rebuild them *)
284 theoryuritree := [] ;
286 hbox#remove !sw1#coerce ;
287 hbox#remove !sw#coerce ;
290 GBin.scrolled_window ~width:250 ~height:600
291 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
293 GTree.tree ~selection_mode:`BROWSE ~packing:sw3#add_with_viewport () in
294 let tree_item1 = GTree.tree_item ~label:"theory:/" ~packing:tree1#append () in
296 ignore(tree_item1#connect#select
297 (theory_selection_changed theory_rendering_window None)) ;
298 mktree theory_selection_changed theory_rendering_window tree_item1
299 (Dir ("theory:/",theoryuritree)) ;
302 GBin.scrolled_window ~width:250 ~height:600
303 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
305 GTree.tree ~selection_mode:`BROWSE ~packing:sw2#add_with_viewport () in
306 let tree_item = GTree.tree_item ~label:"cic:/" ~packing:tree#append () in
308 ignore(tree_item#connect#select (selection_changed rendering_window None)) ;
309 mktree selection_changed rendering_window tree_item (Dir ("cic:/",uritree))
312 let theory_check rendering_window () =
315 TheoryTypeChecker.typecheck (theory_get_current_uri ());
316 "Type Checking was successful"
318 TheoryTypeChecker.NotWellTyped s ->
319 "Type Checking was NOT successful:\n\t" ^ s
321 (* next "cast" can't got rid of, but I don't know why *)
322 let errors = (rendering_window#errors : GEdit.text) in
323 let _ = errors#delete_text 0 errors#length in
327 let check rendering_window () =
330 CicTypeChecker.typecheck (get_current_uri ());
331 "Type Checking was successful"
333 CicTypeChecker.NotWellTyped s -> "Type Checking was NOT successful:\n\t" ^ s
335 (* next "cast" can't got rid of, but I don't know why *)
336 let errors = (rendering_window#errors : GEdit.text) in
337 let _ = errors#delete_text 0 errors#length in
341 let annotateb_pressed rendering_window annotation_window () =
342 let module M = Minidom in
343 match rendering_window#output#get_selection with
344 None -> (rendering_window#errors : GEdit.text)#insert "\nNo selection!\n"
347 match M.node_get_attribute node (M.mDOMString_of_string "xref") with
349 | Some xpath -> M.string_of_mDOMString xpath
352 let annobj = get_annotated_obj ()
353 (* next "cast" can't got rid of, but I don't know why *)
354 and annotation = (annotation_window#annotation : GEdit.text) in
355 ann := CicXPath.get_annotation annobj xpath ;
356 CicAnnotationHinter.create_hints annotation_window annobj xpath ;
357 annotation#delete_text 0 annotation#length ;
361 annotation#misc#set_sensitive false ;
362 annotation_window#radio_none#set_active true ;
363 radio_some_status := false
365 annotation#insert ann' ;
366 annotation#misc#set_sensitive true ;
367 annotation_window#radio_some#set_active true ;
368 radio_some_status := true
370 GMain.Grab.add (annotation_window#window_to_annotate#coerce) ;
371 annotation_window#show () ;
374 (* next "cast" can't got rid of, but I don't know why *)
375 let errors = (rendering_window#errors : GEdit.text) in
376 errors#insert ("\n" ^ Printexc.to_string e ^ "\n")
379 (* called when the annotation is confirmed *)
380 let save_annotation annotation =
381 if !radio_some_status then
382 !ann := Some (annotation#get_chars 0 annotation#length)
385 match !annotated_obj with
386 None -> raise GtkInterfaceInternalError
388 let uri = get_current_uri () in
389 let annxml = Annotation2Xml.pp_annotation annobj uri in
390 Xml.pp annxml (Some (fst (Getter.get_ann_file_name_and_uri uri)))
393 let parse_no_cache uri =
394 let module U = UriManager in
395 XsltProcessor.process uri false "cic"
399 (* STUFF TO BUILD THE GTK INTERFACE *)
401 (* Stuff to build the tree window *)
403 (* selection_changed is actually selection_changed or theory_selection_changed*)
404 let mktree selection_changed rendering_window =
405 let rec aux treeitem =
407 Dir (dirname, content) ->
408 let subtree = GTree.tree () in
409 treeitem#set_subtree subtree ;
412 let label = get_name ti
413 and uri = get_uri ti in
414 let treeitem2 = GTree.tree_item ~label:label () in
415 subtree#append treeitem2 ;
416 ignore(treeitem2#connect#select
417 (selection_changed rendering_window uri)) ;
419 ) (List.sort compare !content)
425 class annotation_window output label =
426 let window_to_annotate =
427 GWindow.window ~title:"Annotating environment" ~border_width:2 () in
429 GPack.hbox ~packing:window_to_annotate#add () in
431 GPack.vbox ~packing:(hbox1#pack ~padding:5) () in
433 GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
434 let radio_some = GButton.radio_button ~label:"Annotation below"
435 ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
436 let radio_none = GButton.radio_button ~label:"No annotation"
437 ~group:radio_some#group
438 ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5)
440 let annotation = GEdit.text ~editable:true ~width:400 ~height:180
441 ~packing:(vbox1#pack ~padding:5) () in
443 GPack.table ~rows:3 ~columns:3 ~packing:(vbox1#pack ~padding:5) () in
444 let annotation_hints =
447 GButton.button ~label:("Hint " ^ string_of_int i)
448 ~packing:(table#attach ~left:(i mod 3) ~top:(i / 3)) ()
451 GPack.vbox ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
453 GButton.button ~label:"O.K."
454 ~packing:(vbox2#pack ~expand:false ~fill:false ~padding:5) () in
456 GButton.button ~label:"Abort"
457 ~packing:(vbox2#pack ~expand:false ~fill:false ~padding:5) () in
459 method window_to_annotate = window_to_annotate
460 method annotation = annotation
461 method radio_some = radio_some
462 method radio_none = radio_none
463 method annotation_hints = annotation_hints
464 method output = (output : GMathView.math_view)
465 method show () = window_to_annotate#show ()
467 (* signal handlers here *)
468 ignore (window_to_annotate#event#connect#delete
470 window_to_annotate#misc#hide () ;
471 GMain.Grab.remove (window_to_annotate#coerce) ;
474 ignore (confirmb#connect#clicked
476 window_to_annotate#misc#hide () ;
477 save_annotation annotation ;
478 GMain.Grab.remove (window_to_annotate#coerce) ;
479 let new_current_uri =
480 (snd (Getter.get_ann_file_name_and_uri (get_current_uri ())))
482 visited_uris := new_current_uri::(List.tl !visited_uris) ;
483 label#set_text (UriManager.string_of_uri new_current_uri) ;
484 output#load (parse_no_cache new_current_uri)
486 ignore (abortb#connect#clicked
488 window_to_annotate#misc#hide () ;
489 GMain.Grab.remove (window_to_annotate#coerce)
491 ignore (radio_some#connect#clicked
492 (fun () -> annotation#misc#set_sensitive true ; radio_some_status := true)) ;
493 ignore (radio_none #connect#clicked
495 annotation#misc#set_sensitive false;
496 radio_some_status := false)
500 class rendering_window annotation_window output (label : GMisc.label) =
502 GWindow.window ~title:"MathML viewer" ~border_width:2 () in
504 GPack.vbox ~packing:window#add () in
505 let _ = vbox#pack ~expand:false ~fill:false ~padding:5 label#coerce in
507 GPack.paned `HORIZONTAL ~packing:(vbox#pack ~padding:5) () in
508 let scrolled_window0 =
509 GBin.scrolled_window ~border_width:10 ~packing:paned#add1 () in
510 let _ = scrolled_window0#add output#coerce in
511 let scrolled_window =
513 ~border_width:10 ~packing:paned#add2 ~width:240 ~height:100 () in
514 let errors = GEdit.text ~packing:scrolled_window#add_with_viewport () in
516 GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
518 GButton.button ~label:"Prev"
519 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
521 GButton.button ~label:"Next"
522 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
524 GButton.button ~label:"Check"
525 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
527 GButton.button ~label:"Annotate"
528 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
531 GData.adjustment ~value:14.0 ~lower:5.0 ~upper:50.0 ~step_incr:1.0 ()
534 ~adjustment:sadj ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5)
537 GButton.button ~label:"Close"
538 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
544 method output = (output : GMathView.math_view)
545 method errors = errors
546 method show () = window#show ()
548 nextb#misc#set_sensitive false ;
549 prevb#misc#set_sensitive false ;
551 (* signal handlers here *)
552 ignore(output#connect#jump (jump self)) ;
553 ignore(output#connect#selection_changed (choose_selection self)) ;
554 ignore(nextb#connect#clicked (next self)) ;
555 ignore(prevb#connect#clicked (prev self)) ;
556 ignore(checkb#connect#clicked (check self)) ;
557 ignore(spinb#connect#changed (changefont self)) ;
558 ignore(closeb#connect#clicked window#misc#hide) ;
559 ignore(annotateb#connect#clicked (annotateb_pressed self annotation_window)) ;
560 ignore(window#event#connect#delete (fun _ -> window#misc#hide () ; true ))
563 class theory_rendering_window rendering_window =
565 GWindow.window ~title:"MathML theory viewer" ~border_width:2 () in
567 GPack.vbox ~packing:window#add () in
569 GMisc.label ~text:"???"
570 ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
572 GPack.paned `HORIZONTAL ~packing:(vbox#pack ~padding:5) () in
573 let scrolled_window0 =
574 GBin.scrolled_window ~border_width:10 ~packing:paned#add1 () in
576 GMathView.math_view ~width:400 ~height:380 ~packing:scrolled_window0#add () in
577 let scrolled_window =
579 ~border_width:10 ~packing:paned#add2 ~width:240 ~height:100 () in
580 let errors = GEdit.text ~packing:scrolled_window#add_with_viewport () in
582 GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
584 GButton.button ~label:"Prev"
585 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
587 GButton.button ~label:"Next"
588 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
590 GButton.button ~label:"Check"
591 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
594 GData.adjustment ~value:14.0 ~lower:5.0 ~upper:50.0 ~step_incr:1.0 ()
597 ~adjustment:sadj ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5)
600 GButton.button ~label:"Close"
601 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
606 method output = (output : GMathView.math_view)
607 method errors = errors
609 method show () = window#show ()
611 nextb#misc#set_sensitive false ;
612 prevb#misc#set_sensitive false ;
614 (* signal handlers here *)
615 ignore(output#connect#jump (jump rendering_window)) ;
616 ignore(output#connect#selection_changed (choose_selection self)) ;
617 ignore(nextb#connect#clicked (theory_next self)) ;
618 ignore(prevb#connect#clicked (theory_prev self)) ;
619 ignore(checkb#connect#clicked (theory_check self)) ;
620 ignore(spinb#connect#changed (changefont self)) ;
621 ignore(closeb#connect#clicked window#misc#hide) ;
622 ignore(window#event#connect#delete (fun _ -> window#misc#hide () ; true ))
625 (* CSC: fare in modo che i due alberi vengano svuotati invece che distrutti *)
626 class selection_window theory_rendering_window rendering_window =
627 let label = "cic:/" in
628 let theorylabel = "theory:/" in
629 let win = GWindow.window ~title:"Known uris" ~border_width:2 () in
630 let vbox = GPack.vbox ~packing:win#add () in
631 let hbox1 = GPack.hbox ~packing:(vbox#pack ~padding:5) () in
632 let sw1 = GBin.scrolled_window ~width:250 ~height:600
633 ~packing:(hbox1#pack ~padding:5) () in
635 GTree.tree ~selection_mode:`BROWSE ~packing:sw1#add_with_viewport () in
637 GTree.tree_item ~label:theorylabel ~packing:tree1#append () in
638 let sw = GBin.scrolled_window ~width:250 ~height:600
639 ~packing:(hbox1#pack ~padding:5) () in
641 GTree.tree ~selection_mode:`BROWSE ~packing:sw#add_with_viewport () in
643 GTree.tree_item ~label:label ~packing:tree#append () in
645 GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
647 GButton.button ~label:"Update"
648 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
650 GButton.button ~label:"Quit"
651 ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
653 method show () = win#show ()
655 mktree theory_selection_changed theory_rendering_window tree_item1
656 (Dir ("theory:/",theoryuritree));
657 mktree selection_changed rendering_window tree_item
658 (Dir ("cic:/",uritree));
660 (* signal handlers here *)
661 ignore (tree_item1#connect#select
662 ~callback:(theory_selection_changed theory_rendering_window None)) ;
663 ignore (tree_item#connect#select
664 ~callback:(selection_changed rendering_window None)) ;
665 ignore (win#connect#destroy ~callback:GMain.Main.quit) ;
666 ignore (quitb#connect#clicked GMain.Main.quit) ;
667 ignore(updateb#connect#clicked (updateb_pressed
668 theory_rendering_window rendering_window (ref sw1, ref sw, hbox1) mktree))
676 let output = GMathView.math_view ~width:400 ~height:380 ()
677 and label = GMisc.label ~text:"???" () in
678 let annotation_window = new annotation_window output label in
679 let rendering_window = new rendering_window annotation_window output label in
680 let theory_rendering_window = new theory_rendering_window rendering_window in
681 let selection_window =
682 new selection_window theory_rendering_window rendering_window
684 selection_window#show () ;