]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMathView.ml
Code simplification.
[helm.git] / helm / matita / matitaMathView.ml
1 (* Copyright (C) 2004-2005, 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 open Printf
27
28 open MatitaTypes
29
30 let add_trailing_slash =
31   let rex = Pcre.regexp "/$" in
32   fun s ->
33     if Pcre.pmatch ~rex s then s
34     else s ^ "/"
35
36 let strip_blanks =
37   let rex = Pcre.regexp "^\\s*([^\\s]*)\\s*$" in
38   fun s ->
39     (Pcre.extract ~rex s).(1)
40
41 (** inherit from this class if you want to access current script *)
42 class scriptAccessor =
43 object (self)
44   method private script = MatitaScript.instance ()
45 end
46
47 let cicBrowsers = ref []
48 let gui_instance = ref None
49 let set_gui gui = gui_instance := Some gui
50 let get_gui () =
51   match !gui_instance with
52   | None -> assert false
53   | Some gui -> gui
54
55 let default_font_size () =
56   Helm_registry.get_opt_default Helm_registry.int
57     ~default:BuildTimeConf.default_font_size "matita.font_size"
58 let current_font_size = ref ~-1
59 let increase_font_size () = incr current_font_size
60 let decrease_font_size () = decr current_font_size
61 let reset_font_size () = current_font_size := default_font_size ()
62
63   (* is there any lablgtk2 constant corresponding to the left mouse button??? *)
64 let left_button = 1
65
66 let near (x1, y1) (x2, y2) =
67   let distance = sqrt (((x2 -. x1) ** 2.) +. ((y2 -. y1) ** 2.)) in
68   (distance < 4.)
69
70 let href_ds = Gdome.domString "href"
71 let xref_ds = Gdome.domString "xref"
72
73 class clickableMathView obj =
74   let text_width = 80 in
75   object (self)
76     inherit GMathViewAux.multi_selection_math_view obj
77
78     val mutable href_callback: (string -> unit) option = None
79     method set_href_callback f = href_callback <- f
80
81     val mutable _cic_info = None
82     method private set_cic_info info = _cic_info <- info
83     method private cic_info =
84       match _cic_info with
85       | Some info -> info
86       | None -> assert false
87
88     initializer
89       self#set_font_size !current_font_size;
90       ignore (self#connect#selection_changed self#choose_selection);
91       ignore (self#event#connect#button_press self#button_press);
92       ignore (self#event#connect#button_release self#button_release);
93 (*       ignore (self#connect#click (fun (gdome_elt, _, _, _) ->
94         match gdome_elt with
95         | Some elt  |+ element is an hyperlink, use href_callback on it +|
96           when elt#hasAttributeNS ~namespaceURI:DomMisc.xlink_ns ~localName:href ->
97             (match href_callback with
98             | None -> ()
99             | Some f ->
100                 let uri =
101                   elt#getAttributeNS ~namespaceURI:DomMisc.xlink_ns ~localName:href
102                 in
103                 f (uri#to_string))
104         | Some elt -> ignore (self#action_toggle elt)
105         | None -> ())) *)
106
107     val mutable button_press_x = -1.
108     val mutable button_press_y = -1.
109     val mutable selection_changed = false
110
111     method private button_press gdk_button =
112       if GdkEvent.Button.button gdk_button = left_button then begin
113         button_press_x <- GdkEvent.Button.x gdk_button;
114         button_press_y <- GdkEvent.Button.y gdk_button;
115         selection_changed <- false
116       end;
117       false
118
119     method private button_release gdk_button =
120       if GdkEvent.Button.button gdk_button = left_button then begin
121         let button_release_x = GdkEvent.Button.x gdk_button in
122         let button_release_y = GdkEvent.Button.y gdk_button in
123         (if near (button_press_x, button_press_y)
124           (button_release_x, button_release_y)
125           && not selection_changed
126         then
127           let x = int_of_float button_press_x in
128           let y = int_of_float button_press_y in
129           (match self#get_element_at x y with
130           | None -> ()
131           | Some elt ->
132               let namespaceURI = DomMisc.xlink_ns in
133               let localName = href_ds in
134               if elt#hasAttributeNS ~namespaceURI ~localName then
135                 self#invoke_href_callback
136                   (elt#getAttributeNS ~namespaceURI ~localName)#to_string
137                   gdk_button
138               else
139                 ignore (self#action_toggle elt)));
140       end;
141       false
142
143     method private invoke_href_callback href_value gdk_button =
144       let button = GdkEvent.Button.button gdk_button in
145       if button = left_button then
146         let time = GdkEvent.Button.time gdk_button in
147         match href_callback with
148         | None -> ()
149         | Some f ->
150             (match MatitaMisc.split href_value with
151             | [ uri ] ->  f uri
152             | uris ->
153                 let menu = GMenu.menu () in
154                 List.iter
155                   (fun uri ->
156                     let menu_item =
157                       GMenu.menu_item ~label:uri ~packing:menu#append ()
158                     in
159                     ignore (menu_item#connect#activate (fun () -> f uri)))
160                   uris;
161                 menu#popup ~button ~time)
162
163     method private choose_selection gdome_elt =
164       let rec aux elt =
165         if (elt#getAttributeNS ~namespaceURI:DomMisc.helm_ns
166               ~localName:xref_ds)#to_string <> ""
167 (*         if elt#hasAttributeNS ~namespaceURI:DomMisc.helm_ns ~localName:xref_ds
168           && (elt#getAttributeNS ~namespaceURI:DomMisc.helm_ns
169               ~localName:xref_ds)#to_string <> "" *)
170         then
171           self#set_selection (Some elt)
172         else
173           try
174             (match elt#get_parentNode with
175             | None -> assert false
176             | Some p -> aux (new Gdome.element_of_node p))
177           with GdomeInit.DOMCastException _ -> ()
178 (*             debug_print "trying to select above the document root" *)
179       in
180       (match gdome_elt with
181       | Some elt -> aux elt
182       | None   -> self#set_selection None);
183       selection_changed <- true
184
185     method update_font_size = 
186       self#set_font_size !current_font_size
187
188     method private get_term_by_id context id =
189       let ids_to_terms, ids_to_hypotheses = self#cic_info in
190       try
191         `Term (Hashtbl.find ids_to_terms id)
192       with Not_found ->
193         try
194           let hyp = Hashtbl.find ids_to_hypotheses id in
195           let context' = MatitaMisc.list_tl_at hyp context in
196           `Hyp context'
197         with Not_found -> assert false
198       
199     method string_of_selected_terms =
200       let get_id (node: Gdome.element) =
201         let xref_attr =
202           node#getAttributeNS ~namespaceURI:DomMisc.helm_ns ~localName:xref_ds
203         in
204         xref_attr#to_string
205       in
206       let script = MatitaScript.instance () in
207       let metasenv = script#proofMetasenv in
208       let context = script#proofContext in
209       let conclusion = script#proofConclusion in
210       let cic_terms =
211         List.map
212           (fun node -> self#get_term_by_id context (get_id node))
213           self#get_selections
214       in
215 (* TODO: code for patterns
216       let conclusion = (MatitaScript.instance ())#proofConclusion in
217       let conclusion_pattern =
218         ProofEngineHelpers.pattern_of ~term:conclusion cic_terms
219       in
220 *)
221       let dummy_goal = ~-1 in
222       let cic_sequent =
223         match cic_terms with
224         | [] -> assert false
225         | `Term t :: _ ->
226             let context' =
227               ProofEngineHelpers.locate_in_conjecture t
228                 (dummy_goal, context, conclusion)
229             in
230             dummy_goal, context', t
231         | `Hyp context :: _ -> dummy_goal, context, Cic.Rel 1
232       in
233 (* TODO: code for patterns
234       (* TODO context shouldn't be empty *)
235       let cic_sequent = ~-1, [], conclusion_pattern in
236 *)
237       let acic_sequent, _, _, ids_to_inner_sorts, _ =
238         Cic2acic.asequent_of_sequent metasenv cic_sequent
239       in
240       let _, _, _, annterm = acic_sequent in
241       let ast, ids_to_uris =
242         CicNotationRew.ast_of_acic ids_to_inner_sorts annterm
243       in
244       let pped_ast = CicNotationRew.pp_ast ast in
245       let markup = CicNotationPres.render ids_to_uris pped_ast in
246       BoxPp.render_to_string text_width markup
247
248   end
249
250 let clickableMathView ?hadjustment ?vadjustment ?font_size ?log_verbosity =
251   GtkBase.Widget.size_params
252     ~cont:(OgtkMathViewProps.pack_return (fun p ->
253       OgtkMathViewProps.set_params
254         (new clickableMathView (GtkMathViewProps.MathView_GMetaDOM.create p))
255         ~font_size:None ~log_verbosity:None))
256     []
257
258 class sequentViewer obj =
259 object (self)
260   inherit clickableMathView obj
261
262   method load_sequent metasenv metano =
263     let sequent = CicUtil.lookup_meta metano metasenv in
264     let (mathml, (_, (ids_to_terms, _, ids_to_hypotheses,_ ))) =
265       ApplyTransformation.mml_of_cic_sequent metasenv sequent
266     in
267     self#set_cic_info (Some (ids_to_terms, ids_to_hypotheses));
268     let name = "sequent_viewer.xml" in
269     prerr_endline ("load_sequent: dumping MathML to ./" ^ name);
270     ignore (DomMisc.domImpl#saveDocumentToFile ~name ~doc:mathml ());
271     self#load_root ~root:mathml#get_documentElement
272  end
273
274 class sequentsViewer ~(notebook:GPack.notebook)
275   ~(sequentViewer:sequentViewer) ()
276 =
277   object (self)
278     inherit scriptAccessor
279
280     val mutable pages = 0
281     val mutable switch_page_callback = None
282     val mutable page2goal = []  (* associative list: page no -> goal no *)
283     val mutable goal2page = []  (* the other way round *)
284     val mutable goal2win = []   (* associative list: goal no -> scrolled win *)
285     val mutable _metasenv = []
286     val mutable scrolledWin: GBin.scrolled_window option = None
287       (* scrolled window to which the sequentViewer is currently attached *)
288
289     method private tab_label metano =
290       (GMisc.label ~text:(sprintf "?%d" metano) ~show:true ())#coerce
291
292     method reset =
293       (match scrolledWin with
294       | Some w ->
295           (* removing page from the notebook will destroy all contained widget,
296           * we do not want the sequentViewer to be destroyed as well *)
297           w#remove sequentViewer#coerce;
298           scrolledWin <- None
299       | None -> ());
300       for i = 1 to pages do notebook#remove_page 0 done;
301       pages <- 0;
302       page2goal <- [];
303       goal2page <- [];
304       goal2win <- [];
305       _metasenv <- [];
306       self#script#setGoal ~-1;
307       (match switch_page_callback with
308       | Some id ->
309           GtkSignal.disconnect notebook#as_widget id;
310           switch_page_callback <- None
311       | None -> ())
312
313     method load_sequents (status: ProofEngineTypes.status) =
314       let ((_, metasenv, _, _), goal) = status in
315       let sequents_no = List.length metasenv in
316       _metasenv <- metasenv;
317       pages <- sequents_no;
318       self#script#setGoal goal;
319       let win metano =
320         let w =
321           GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
322             ~shadow_type:`IN ~show:true ()
323         in
324         let reparent () =
325           scrolledWin <- Some w;
326           match sequentViewer#misc#parent with
327           | None -> w#add sequentViewer#coerce
328           | Some parent ->
329              let parent =
330               match sequentViewer#misc#parent with
331                  None -> assert false
332                | Some p -> GContainer.cast_container p
333              in
334               parent#remove sequentViewer#coerce;
335               w#add sequentViewer#coerce
336         in
337         goal2win <- (metano, reparent) :: goal2win;
338         w#coerce
339       in
340       let page = ref 0 in
341       List.iter
342         (fun (metano, _, _) ->
343           page2goal <- (!page, metano) :: page2goal;
344           goal2page <- (metano, !page) :: goal2page;
345           incr page;
346           notebook#append_page ~tab_label:(self#tab_label metano) (win metano))
347         metasenv;
348       switch_page_callback <-
349         Some (notebook#connect#switch_page ~callback:(fun page ->
350           let goal =
351             try
352               List.assoc page page2goal
353             with Not_found -> assert false
354           in
355           self#script#setGoal goal;
356           self#render_page ~page ~goal))
357
358     method private render_page ~page ~goal =
359       sequentViewer#load_sequent _metasenv goal;
360       try
361         List.assoc goal goal2win ();
362         sequentViewer#set_selection None
363       with Not_found -> assert false
364
365     method goto_sequent goal =
366       let page =
367         try
368           List.assoc goal goal2page
369         with Not_found -> assert false
370       in
371       notebook#goto_page page;
372       self#render_page page goal
373
374   end
375
376  (** constructors *)
377
378 type 'widget constructor =
379   ?hadjustment:GData.adjustment ->
380   ?vadjustment:GData.adjustment ->
381   ?font_size:int ->
382   ?log_verbosity:int ->
383   ?width:int ->
384   ?height:int ->
385   ?packing:(GObj.widget -> unit) ->
386   ?show:bool ->
387   unit ->
388     'widget
389
390 let sequentViewer ?hadjustment ?vadjustment ?font_size ?log_verbosity =
391   GtkBase.Widget.size_params
392     ~cont:(OgtkMathViewProps.pack_return (fun p ->
393       OgtkMathViewProps.set_params
394         (new sequentViewer (GtkMathViewProps.MathView_GMetaDOM.create p))
395         ~font_size ~log_verbosity))
396     []
397
398 let blank_uri = BuildTimeConf.blank_uri
399 let current_proof_uri = BuildTimeConf.current_proof_uri
400
401 type term_source =
402   [ `Ast of DisambiguateTypes.term
403   | `Cic of Cic.term * Cic.metasenv
404   | `String of string
405   ]
406
407 class type cicBrowser =
408 object
409   method load: MatitaTypes.mathViewer_entry -> unit
410   (* method loadList: string list -> MatitaTypes.mathViewer_entry-> unit *)
411   method loadInput: string -> unit
412 end
413
414 let reloadable = function
415   | `About `Current_proof
416   | `Dir _ ->
417       true
418   | _ -> false
419
420 class cicBrowser_impl ~(history:MatitaTypes.mathViewer_entry MatitaMisc.history)
421   ()
422 =
423   let term_RE = Pcre.regexp "^term:(.*)" in
424   let whelp_RE = Pcre.regexp "^\\s*whelp" in
425   let uri_RE =
426     Pcre.regexp
427       "^cic:/([^/]+/)*[^/]+\\.(con|ind|var)(#xpointer\\(\\d+(/\\d+)+\\))?$"
428   in
429   let dir_RE = Pcre.regexp "^cic:((/([^/]+/)*[^/]+(/)?)|/|)$" in
430   let whelp_query_RE = Pcre.regexp "^\\s*whelp\\s+([^\\s]+)\\s+(.*)$" in
431   let trailing_slash_RE = Pcre.regexp "/$" in
432   let has_xpointer_RE = Pcre.regexp "#xpointer\\(\\d+/\\d+(/\\d+)?\\)$" in
433   let is_whelp txt = Pcre.pmatch ~rex:whelp_RE txt in
434   let is_uri txt = Pcre.pmatch ~rex:uri_RE txt in
435   let is_dir txt = Pcre.pmatch ~rex:dir_RE txt in
436   let gui = get_gui () in
437   let (win: MatitaGuiTypes.browserWin) = gui#newBrowserWin () in
438   let queries = ["Locate";"Hint";"Match";"Elim";"Instance"] in
439   let combo,_ = GEdit.combo_box_text ~strings:queries () in
440   let activate_combo_query input q =
441     let q' = String.lowercase q in
442     let rec aux i = function
443       | [] -> failwith ("Whelp query '" ^ q ^ "' not found")
444       | h::_ when String.lowercase h = q' -> i
445       | _::tl -> aux (i+1) tl
446     in
447     combo#set_active (aux 0 queries);
448     win#queryInputText#set_text input
449   in
450   let set_whelp_query txt =
451     let query, arg = 
452       try
453         let q = Pcre.extract ~rex:whelp_query_RE txt in
454         q.(1), q.(2)
455       with Invalid_argument _ -> failwith "Malformed Whelp query"
456     in
457     activate_combo_query arg query
458   in
459   let toplevel = win#toplevel in
460   let mathView = sequentViewer ~packing:win#scrolledBrowser#add () in
461   let fail message = 
462     MatitaGtkMisc.report_error ~title:"Cic browser" ~message 
463       ~parent:toplevel ()  
464   in
465   let tags =
466     [ "dir", GdkPixbuf.from_file (MatitaMisc.image_path "matita-folder.png");
467       "obj", GdkPixbuf.from_file (MatitaMisc.image_path "matita-object.png") ]
468   in
469   let handle_error f =
470     try
471       f ()
472     with exn -> fail (MatitaExcPp.to_string exn)
473   in
474   let handle_error' f = (fun () -> handle_error (fun () -> f ())) in
475   object (self)
476     inherit scriptAccessor
477     
478     (* Whelp bar queries *)
479
480     initializer
481       activate_combo_query "" "locate";
482       win#whelpBarComboVbox#add combo#coerce;
483       let start_query () = 
484         let query = String.lowercase (List.nth queries combo#active) in
485         let input = win#queryInputText#text in
486         let statement = "whelp " ^ query ^ " " ^ input ^ "." in
487         (MatitaScript.instance ())#advance ~statement ()
488       in
489       ignore(win#queryInputText#connect#activate ~callback:start_query);
490       ignore(combo#connect#changed ~callback:start_query);
491       win#whelpBarImage#set_file (MatitaMisc.image_path "whelp.png");
492       win#mathOrListNotebook#set_show_tabs false;
493
494       win#browserForwardButton#misc#set_sensitive false;
495       win#browserBackButton#misc#set_sensitive false;
496       ignore (win#browserUri#entry#connect#activate (handle_error' (fun () ->
497         self#loadInput win#browserUri#entry#text)));
498       ignore (win#browserHomeButton#connect#clicked (handle_error' (fun () ->
499         self#load (`About `Current_proof))));
500       ignore (win#browserRefreshButton#connect#clicked
501         (handle_error' self#refresh));
502       ignore (win#browserBackButton#connect#clicked (handle_error' self#back));
503       ignore (win#browserForwardButton#connect#clicked
504         (handle_error' self#forward));
505       ignore (win#toplevel#event#connect#delete (fun _ ->
506         let my_id = Oo.id self in
507         cicBrowsers := List.filter (fun b -> Oo.id b <> my_id) !cicBrowsers;
508         if !cicBrowsers = [] &&
509           Helm_registry.get "matita.mode" = "cicbrowser"
510         then
511           GMain.quit ();
512         false));
513       ignore(win#whelpResultTreeview#connect#row_activated 
514         ~callback:(fun _ _ ->
515           handle_error (fun () -> self#loadInput (self#_getSelectedUri ()))));
516       mathView#set_href_callback (Some (fun uri ->
517         handle_error (fun () ->
518           self#load (`Uri (UriManager.uri_of_string uri)))));
519       self#_load (`About `Blank);
520       toplevel#show ()
521
522     val mutable current_entry = `About `Blank 
523     val mutable current_infos = None
524     val mutable current_mathml = None
525
526     val model =
527       new MatitaGtkMisc.taggedStringListModel tags win#whelpResultTreeview
528
529     val mutable lastDir = ""  (* last loaded "directory" *)
530
531     method private _getSelectedUri () =
532       match model#easy_selection () with
533       | [sel] when is_uri sel -> sel  (* absolute URI selected *)
534 (*       | [sel] -> win#browserUri#entry#text ^ sel  |+ relative URI selected +| *)
535       | [sel] -> lastDir ^ sel
536       | _ -> assert false
537
538     (** history RATIONALE 
539      *
540      * All operations about history are done using _historyFoo.
541      * Only toplevel functions (ATM load and loadInput) call _historyAdd.
542      *)
543           
544     method private _historyAdd item = 
545       history#add item;
546       win#browserBackButton#misc#set_sensitive true;
547       win#browserForwardButton#misc#set_sensitive false
548
549     method private _historyPrev () =
550       let item = history#previous in
551       if history#is_begin then win#browserBackButton#misc#set_sensitive false;
552       win#browserForwardButton#misc#set_sensitive true;
553       item
554     
555     method private _historyNext () =
556       let item = history#next in
557       if history#is_end then win#browserForwardButton#misc#set_sensitive false;
558       win#browserBackButton#misc#set_sensitive true;
559       item
560
561     (** notebook RATIONALE 
562      * 
563      * Use only these functions to switch between the tabs
564      *)
565     method private _showList = win#mathOrListNotebook#goto_page 1
566     method private _showMath = win#mathOrListNotebook#goto_page 0
567     
568     method private back () =
569       try
570         self#_load (self#_historyPrev ())
571       with MatitaMisc.History_failure -> ()
572
573     method private forward () =
574       try
575         self#_load (self#_historyNext ())
576       with MatitaMisc.History_failure -> ()
577
578       (* loads a uri which can be a cic uri or an about:* uri
579       * @param uri string *)
580     method private _load entry =
581       try
582         if entry <> current_entry || reloadable entry then begin
583           (match entry with
584           | `About `Current_proof -> self#home ()
585           | `About `Blank -> self#blank ()
586           | `About `Us -> () (* TODO implement easter egg here :-] *)
587           | `Check term -> self#_loadCheck term
588           | `Cic (term, metasenv) -> self#_loadTermCic term metasenv
589           | `Dir dir -> self#_loadDir dir
590           | `Uri uri -> self#_loadUriManagerUri uri
591           | `Whelp (query, results) -> 
592               set_whelp_query query;
593               self#_loadList (List.map (fun r -> "obj",
594                 UriManager.string_of_uri r) results));
595           self#setEntry entry
596         end
597       with exn -> fail (MatitaExcPp.to_string exn)
598
599     method private blank () =
600       self#_showMath;
601       mathView#load_root (MatitaMisc.empty_mathml ())#get_documentElement
602
603     method private _loadCheck term =
604       failwith "not implemented _loadCheck";
605       self#_showMath
606
607     method private home () =
608       self#_showMath;
609       match self#script#status.proof_status with
610       | Proof  (uri, metasenv, bo, ty) ->
611           let name = UriManager.name_of_uri (MatitaMisc.unopt uri) in
612           let obj = Cic.CurrentProof (name, metasenv, bo, ty, [], []) in
613           self#_loadObj obj
614       | Incomplete_proof ((uri, metasenv, bo, ty), _) -> 
615           let name = UriManager.name_of_uri (MatitaMisc.unopt uri) in
616           let obj = Cic.CurrentProof (name, metasenv, bo, ty, [], []) in
617           self#_loadObj obj
618       | _ -> self#blank ()
619
620       (** loads a cic uri from the environment
621       * @param uri UriManager.uri *)
622     method private _loadUriManagerUri uri =
623       let uri = UriManager.strip_xpointer uri in
624       let (obj, _) = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
625       self#_loadObj obj
626       
627     method private _loadDir dir = 
628       let content = Http_getter.ls dir in
629       let l =
630         List.fast_sort
631           Pervasives.compare
632           (List.map
633             (function 
634               | Http_getter_types.Ls_section s -> "dir", s
635               | Http_getter_types.Ls_object o -> "obj", o.Http_getter_types.uri)
636             content)
637       in
638       lastDir <- dir;
639       self#_loadList l
640
641     method private setEntry entry =
642       win#browserUri#entry#set_text (string_of_entry entry);
643       current_entry <- entry
644
645     method private _loadObj obj =
646       self#_showMath; 
647       (* this must be _before_ loading the document, since 
648        * if the widget is not mapped (hidden by the notebook)
649        * the document is not rendered *)
650       let use_diff = false in (* ZACK TODO use XmlDiff when re-rendering? *)
651       let (mathml, (_,((ids_to_terms, ids_to_father_ids, ids_to_conjectures,
652            ids_to_hypotheses, ids_to_inner_sorts, ids_to_inner_types) as info)))
653       =
654         ApplyTransformation.mml_of_cic_object obj
655       in
656       current_infos <- Some info;
657       (match current_mathml with
658       | Some current_mathml when use_diff ->
659           mathView#freeze;
660           XmlDiff.update_dom ~from:current_mathml mathml;
661           mathView#thaw
662       |  _ ->
663           let name = "cic_browser.xml" in
664           prerr_endline ("cic_browser: dumping MathML to ./" ^ name);
665           ignore (DomMisc.domImpl#saveDocumentToFile ~name ~doc:mathml ());
666           mathView#load_root ~root:mathml#get_documentElement;
667           current_mathml <- Some mathml);
668
669     method private _loadTermCic term metasenv =
670       let context = self#script#proofContext in
671       let dummyno = CicMkImplicit.new_meta metasenv [] in
672       let sequent = (dummyno, context, term) in
673       mathView#load_sequent (sequent :: metasenv) dummyno;
674       self#_showMath
675
676     method private _loadList l =
677       model#list_store#clear ();
678       List.iter (fun (tag, s) -> model#easy_append ~tag s) l;
679       self#_showList
680     
681     (** { public methods, all must call _load!! } *)
682       
683     method load entry =
684       handle_error (fun () -> self#_load entry; self#_historyAdd entry)
685
686     (**  this is what the browser does when you enter a string an hit enter *)
687     method loadInput txt =
688       let txt = strip_blanks txt in
689       let fix_uri txt =
690         UriManager.string_of_uri
691           (UriManager.strip_xpointer (UriManager.uri_of_string txt))
692       in
693       if is_whelp txt then begin
694         set_whelp_query txt;  
695         (MatitaScript.instance ())#advance ~statement:(txt ^ ".") ()
696       end else begin
697         let entry =
698           match txt with
699           | txt when is_uri txt -> `Uri (UriManager.uri_of_string (fix_uri txt))
700           | txt when is_dir txt -> `Dir (add_trailing_slash txt)
701           | txt ->
702               (try
703                 entry_of_string txt
704               with Invalid_argument _ ->
705                 command_error (sprintf "unsupported uri: %s" txt))
706         in
707         self#_load entry;
708         self#_historyAdd entry
709       end
710
711       (** {2 methods accessing underlying GtkMathView} *)
712
713     method updateFontSize = mathView#set_font_size !current_font_size
714
715       (** {2 methods used by constructor only} *)
716
717     method win = win
718     method history = history
719     method currentEntry = current_entry
720     method refresh () =
721       if reloadable current_entry then self#_load current_entry
722
723   end
724   
725 let sequentsViewer ~(notebook:GPack.notebook)
726   ~(sequentViewer:sequentViewer) ()
727 =
728   new sequentsViewer ~notebook ~sequentViewer ()
729
730 let cicBrowser () =
731   let size = BuildTimeConf.browser_history_size in
732   let rec aux history =
733     let browser = new cicBrowser_impl ~history () in
734     let win = browser#win in
735     ignore (win#browserNewButton#connect#clicked (fun () ->
736       let history =
737         new MatitaMisc.browser_history ~memento:history#save size
738           (`About `Blank)
739       in
740       let newBrowser = aux history in
741       newBrowser#load browser#currentEntry));
742 (*
743       (* attempt (failed) to close windows on CTRL-W ... *)
744     MatitaGtkMisc.connect_key win#browserWinEventBox#event ~modifiers:[`CONTROL]
745       GdkKeysyms._W (fun () -> win#toplevel#destroy ());
746 *)
747     cicBrowsers := browser :: !cicBrowsers;
748     (browser :> cicBrowser)
749   in
750   let history = new MatitaMisc.browser_history size (`About `Blank) in
751   aux history
752
753 let default_sequentViewer () = sequentViewer ~show:true ()
754 let sequentViewer_instance = MatitaMisc.singleton default_sequentViewer
755
756 let default_sequentsViewer () =
757   let gui = get_gui () in
758   let sequentViewer = sequentViewer_instance () in
759   sequentsViewer ~notebook:gui#main#sequentsNotebook ~sequentViewer ()
760 let sequentsViewer_instance = MatitaMisc.singleton default_sequentsViewer
761
762 let mathViewer () = 
763   object(self)
764     method private get_browser reuse = 
765       if reuse then
766         (match !cicBrowsers with
767         | [] -> cicBrowser ()
768         | b :: _ -> (b :> cicBrowser))
769       else
770         (cicBrowser ())
771           
772     method show_entry ?(reuse=false) t = (self#get_browser reuse)#load t
773       
774     method show_uri_list ?(reuse=false) ~entry l =
775       (self#get_browser reuse)#load entry
776   end
777
778 let refresh_all_browsers () = List.iter (fun b -> b#refresh ()) !cicBrowsers
779
780 let update_font_sizes () =
781   List.iter (fun b -> b#updateFontSize) !cicBrowsers;
782   (sequentViewer_instance ())#update_font_size
783