1 (* Copyright (C) 2000-2002, 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/.
31 (* List utility functions *)
47 class clickable_math_view obj =
48 let href = Gdome.domString "href" in
49 let xref = Gdome.domString "xref" in
51 inherit GMathViewAux.multi_selection_math_view obj
53 val mutable href_callback: (UriManager.uri -> unit) option = None
54 method set_href_callback f = href_callback <- f
57 ignore (self#connect#selection_changed self#choose_selection);
58 ignore (self#connect#click (fun (gdome_elt, _, _, _) ->
60 | Some elt (* element is an hyperlink, use href_callback on it *)
61 when elt#hasAttributeNS ~namespaceURI:Misc.xlink_ns ~localName:href ->
62 (match href_callback with
66 elt#getAttributeNS ~namespaceURI:Misc.xlink_ns ~localName:href
68 f (UriManager.uri_of_string (uri#to_string)))
69 | Some elt -> ignore (self#action_toggle elt)
71 method private choose_selection gdome_elt =
73 if elt#hasAttributeNS ~namespaceURI:Misc.helm_ns ~localName:xref then
74 self#set_selection (Some elt)
77 (match elt#get_parentNode with
78 | None -> assert false
79 | Some p -> aux (new Gdome.element_of_node p))
80 with GdomeInit.DOMCastException _ -> ()
81 (* debug_print "trying to select above the document root" *)
85 | None -> self#set_selection None
88 let clickable_math_view ?hadjustment ?vadjustment ?font_size ?log_verbosity =
89 GtkBase.Widget.size_params
90 ~cont:(OgtkMathViewProps.pack_return (fun p ->
91 OgtkMathViewProps.set_params
92 (new clickable_math_view (GtkMathViewProps.MathView_GMetaDOM.create p))
93 ~font_size:None ~log_verbosity:None))
96 class sequent_viewer obj =
99 inherit clickable_math_view obj
101 val mutable current_infos = None
103 method get_selected_terms =
104 let selections = self#get_selections in
108 ((node : Gdome.element)#getAttributeNS
109 ~namespaceURI:Misc.helm_ns
110 ~localName:(Gdome.domString "xref"))#to_string
112 if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
114 match current_infos with
115 | Some (ids_to_terms,_,_) ->
117 Hashtbl.find ids_to_terms xpath
118 with _ -> raise Skip)
119 | None -> assert false) (* "ERROR: No current term!!!" *)
122 method get_selected_hypotheses =
123 let selections = self#get_selections in
127 ((node : Gdome.element)#getAttributeNS
128 ~namespaceURI:Misc.helm_ns
129 ~localName:(Gdome.domString "xref"))#to_string
131 if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
133 match current_infos with
134 | Some (_,_,ids_to_hypotheses) ->
136 Hashtbl.find ids_to_hypotheses xpath
137 with _ -> raise Skip)
138 | None -> assert false) (* "ERROR: No current term!!!" *)
141 method load_sequent metasenv metano =
143 let (annconjecture, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
145 Cic2acic.asequent_of_sequent metasenv conjecture
148 let sequent = CicUtil.lookup_meta metano metasenv in
149 let (mathml,(_,(ids_to_terms, ids_to_father_ids, ids_to_hypotheses,_))) =
150 ApplyTransformation.mml_of_cic_sequent metasenv sequent
152 current_infos <- Some (ids_to_terms, ids_to_father_ids, ids_to_hypotheses);
153 debug_print "load_sequent: dumping MathML to ./prova";
154 ignore (Misc.domImpl#saveDocumentToFile ~name:"./prova" ~doc:mathml ());
155 self#load_root ~root:mathml#get_documentElement
159 class sequents_viewer ~(notebook:GPack.notebook)
160 ~(sequent_viewer:sequent_viewer) ~set_goal ()
162 let tab_label metano =
163 (GMisc.label ~text:(sprintf "?%d" metano) ~show:true ())#coerce
166 val mutable pages = 0
167 val mutable switch_page_callback = None
168 val mutable page2goal = [] (* associative list: page no -> goal no *)
169 val mutable goal2page = [] (* the other way round *)
170 val mutable goal2win = [] (* associative list: goal no -> scrolled win *)
171 val mutable _metasenv = []
174 for i = 1 to pages do notebook#remove_page 0 done;
180 (match switch_page_callback with
182 GtkSignal.disconnect notebook#as_widget id;
183 switch_page_callback <- None
186 method load_sequents metasenv =
187 let sequents_no = List.length metasenv in
188 _metasenv <- metasenv;
189 pages <- sequents_no;
192 GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
193 ~shadow_type:`IN ~show:true ()
196 match sequent_viewer#misc#parent with
197 | None -> w#add sequent_viewer#coerce
198 | Some _ -> sequent_viewer#misc#reparent w#coerce
200 goal2win <- (metano, reparent) :: goal2win;
205 (fun (metano, _, _) ->
206 page2goal <- (!page, metano) :: page2goal;
207 goal2page <- (metano, !page) :: goal2page;
209 notebook#append_page ~tab_label:(tab_label metano) (win metano))
211 switch_page_callback <-
212 Some (notebook#connect#switch_page ~callback:(fun page ->
215 List.assoc page page2goal
216 with Not_found -> assert false
219 self#render_page ~page ~goal))
221 method private render_page ~page ~goal =
222 sequent_viewer#load_sequent _metasenv goal;
224 List.assoc goal goal2win ();
225 sequent_viewer#set_selection None
226 with Not_found -> assert false
228 method goto_sequent goal =
231 List.assoc goal goal2page
232 with Not_found -> assert false
234 notebook#goto_page page;
235 self#render_page page goal;
241 type 'widget constructor =
242 ?hadjustment:GData.adjustment ->
243 ?vadjustment:GData.adjustment ->
245 ?log_verbosity:int ->
248 ?packing:(GObj.widget -> unit) ->
253 let sequent_viewer ?hadjustment ?vadjustment ?font_size ?log_verbosity =
254 GtkBase.Widget.size_params
255 ~cont:(OgtkMathViewProps.pack_return (fun p ->
256 OgtkMathViewProps.set_params
257 (new sequent_viewer (GtkMathViewProps.MathView_GMetaDOM.create p))
258 ~font_size ~log_verbosity))
261 let blank_uri = BuildTimeConf.blank_uri
262 let current_proof_uri = BuildTimeConf.current_proof_uri
264 exception Browser_failure of string
266 let cicBrowsers = ref []
269 ~(disambiguator:MatitaTypes.disambiguator)
270 ~(history:string MatitaMisc.history)
273 let term_RE = Pcre.regexp "^term:(.*)" in
274 let gui = MatitaGui.instance () in
275 let win = gui#newBrowserWin () in
276 let toplevel = win#toplevel in
277 let mathView = sequent_viewer ~packing:win#scrolledBrowser#add () in
279 ignore (MatitaGtkMisc.ask_confirmation ~gui:(MatitaGui.instance ())
280 ~title:"Cic browser" ~msg ~cancel:false ());
286 fail (sprintf "Uncaught exception:\n%s" (Printexc.to_string exn))
288 let handle_error' f = fun () -> handle_error f in (* used in callbacks *)
291 ignore (win#browserUri#connect#activate (handle_error' (fun () ->
292 self#_loadUri win#browserUri#text)));
293 ignore (win#browserHomeButton#connect#clicked (handle_error' (fun () ->
294 self#_loadUri current_proof_uri)));
295 ignore (win#browserRefreshButton#connect#clicked
296 (handle_error' self#refresh));
297 ignore (win#browserBackButton#connect#clicked (handle_error' self#back));
298 ignore (win#browserForwardButton#connect#clicked
299 (handle_error' self#forward));
300 ignore (win#toplevel#event#connect#delete (fun _ ->
301 let my_id = Oo.id self in
302 cicBrowsers := List.filter (fun b -> Oo.id b <> my_id) !cicBrowsers;
303 if !cicBrowsers = [] &&
304 Helm_registry.get "matita.mode" = "cicbrowser"
308 mathView#set_href_callback (Some (fun uri ->
309 handle_error (fun () -> self#_loadUri (UriManager.string_of_uri uri))));
310 self#_loadUri ~add_to_history:false blank_uri;
313 val currentProof = MatitaProof.instance ()
315 val mutable current_uri = ""
316 val mutable current_infos = None
317 val mutable current_mathml = None
319 method private back () =
321 self#_loadUri ~add_to_history:false history#previous
322 with MatitaMisc.History_failure -> ()
324 method private forward () =
326 self#_loadUri ~add_to_history:false history#next
327 with MatitaMisc.History_failure -> ()
329 (* loads a uri which can be a cic uri or an about:* uri
330 * @param uri string *)
331 method private _loadUri ?(add_to_history = true) uri =
333 if current_uri <> uri || uri = current_proof_uri then begin
335 | uri when uri = blank_uri -> self#blank ()
336 | uri when uri = current_proof_uri -> self#home ()
337 | uri when Pcre.pmatch ~rex:term_RE uri ->
338 self#loadTerm (`String (Pcre.extract ~rex:term_RE uri).(1))
339 | _ -> self#loadUriManagerUri (UriManager.uri_of_string uri));
341 if add_to_history then history#add uri
344 | UriManager.IllFormedUri _ -> fail (sprintf "invalid uri: %s" uri)
345 | CicEnvironment.Object_not_found _ ->
346 fail (sprintf "object not found: %s" uri)
347 | Browser_failure msg -> fail msg
350 handle_error (fun () -> self#_loadUri ~add_to_history:true uri)
352 method private blank () = mathView#load_root MatitaMisc.empty_mathml
354 method private home () =
355 if currentProof#onGoing () then
356 self#loadObj (cicCurrentProof currentProof#proof#proof)
358 raise (Browser_failure "no on going proof")
360 (** loads a cic uri from the environment
361 * @param uri UriManager.uri *)
362 method private loadUriManagerUri uri =
363 let uri = UriManager.strip_xpointer uri in
364 let (obj, _) = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
367 method private setUri uri =
368 win#browserUri#set_text uri;
371 method private loadObj obj =
372 let use_diff = false in (* ZACK TODO use XmlDiff when re-rendering? *)
373 let (mathml, (_,(ids_to_terms, ids_to_father_ids, ids_to_conjectures,
374 ids_to_hypotheses,_,_))) =
375 ApplyTransformation.mml_of_cic_object obj
377 current_infos <- Some (ids_to_terms, ids_to_father_ids,
378 ids_to_conjectures, ids_to_hypotheses);
379 match current_mathml with
380 | Some current_mathml when use_diff ->
382 XmlDiff.update_dom ~from:current_mathml mathml;
385 mathView#load_root ~root:mathml#get_documentElement;
386 current_mathml <- Some mathml
388 method private _loadTerm s =
389 self#_loadTermAst (disambiguator#parserr#parseTerm (Stream.of_string s))
391 method private _loadTermAst ast =
392 let (_, _, term, _) =
393 MatitaCicMisc.disambiguate ~disambiguator ~currentProof ast
395 self#_loadTermCic term
397 method private _loadTermCic term =
398 let (context, metasenv) =
399 MatitaCicMisc.get_context_and_metasenv currentProof
401 let dummyno = CicMkImplicit.new_meta metasenv [] in
402 let sequent = (dummyno, context, term) in
403 mathView#load_sequent (sequent :: metasenv) dummyno
405 method loadTerm (src:MatitaTypes.term_source) =
406 handle_error (fun () ->
408 | `Ast ast -> self#_loadTermAst ast
409 | `Cic cic -> self#_loadTermCic cic
410 | `String s -> self#_loadTerm s);
413 (** {2 methods used by constructor only} *)
416 method history = history
417 method currentUri = current_uri
419 if current_uri = current_proof_uri then
420 self#_loadUri ~add_to_history:false current_proof_uri
424 let sequents_viewer ~(notebook:GPack.notebook)
425 ~(sequent_viewer:sequent_viewer) ~set_goal ()
427 new sequents_viewer ~notebook ~sequent_viewer ~set_goal ()
429 let cicBrowser ~disambiguator () =
430 let size = BuildTimeConf.browser_history_size in
431 let rec aux history =
432 let browser = new cicBrowser ~disambiguator ~history () in
433 let win = browser#win in
434 ignore (win#browserNewButton#connect#clicked (fun () ->
436 new MatitaMisc.browser_history ~memento:history#save size ""
438 let newBrowser = aux history in
439 newBrowser#loadUri browser#currentUri));
441 (* attempt (failed) to close windows on CTRL-W ... *)
442 MatitaGtkMisc.connect_key win#browserWinEventBox#event ~modifiers:[`CONTROL]
443 GdkKeysyms._W (fun () -> win#toplevel#destroy ());
445 cicBrowsers := browser :: !cicBrowsers;
446 (browser :> MatitaTypes.cicBrowser)
448 let history = new MatitaMisc.browser_history size blank_uri in
451 let refresh_all_browsers () = List.iter (fun b -> b#refresh ()) !cicBrowsers
453 class mathViewer ~disambiguator =
455 method checkTerm (src:MatitaTypes.term_source) =
456 let browser = cicBrowser ~disambiguator () in
460 let mathViewer ~disambiguator () = new mathViewer ~disambiguator