1 (* Copyright (C) 2004-2005, 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 let rec aux = function
42 (** inherit from this class if you want to access current script *)
43 class scriptAccessor =
45 method private script = MatitaScript.instance ()
48 class clickableMathView obj =
49 let href = Gdome.domString "href" in
50 let xref = Gdome.domString "xref" in
52 inherit GMathViewAux.multi_selection_math_view obj
54 val mutable href_callback: (string -> unit) option = None
55 method set_href_callback f = href_callback <- f
58 ignore (self#connect#selection_changed self#choose_selection);
59 ignore (self#connect#click (fun (gdome_elt, _, _, _) ->
61 | Some elt (* element is an hyperlink, use href_callback on it *)
62 when elt#hasAttributeNS ~namespaceURI:Misc.xlink_ns ~localName:href ->
63 (match href_callback with
67 elt#getAttributeNS ~namespaceURI:Misc.xlink_ns ~localName:href
70 | Some elt -> ignore (self#action_toggle elt)
72 method private choose_selection gdome_elt =
74 if elt#hasAttributeNS ~namespaceURI:Misc.helm_ns ~localName:xref then
75 self#set_selection (Some elt)
78 (match elt#get_parentNode with
79 | None -> assert false
80 | Some p -> aux (new Gdome.element_of_node p))
81 with GdomeInit.DOMCastException _ -> ()
82 (* debug_print "trying to select above the document root" *)
86 | None -> self#set_selection None
89 let clickableMathView ?hadjustment ?vadjustment ?font_size ?log_verbosity =
90 GtkBase.Widget.size_params
91 ~cont:(OgtkMathViewProps.pack_return (fun p ->
92 OgtkMathViewProps.set_params
93 (new clickableMathView (GtkMathViewProps.MathView_GMetaDOM.create p))
94 ~font_size:None ~log_verbosity:None))
97 class sequentViewer obj =
100 inherit clickableMathView obj
102 val mutable current_infos = None
104 method get_selected_terms =
105 let selections = self#get_selections in
109 ((node : Gdome.element)#getAttributeNS
110 ~namespaceURI:Misc.helm_ns
111 ~localName:(Gdome.domString "xref"))#to_string
113 if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
115 match current_infos with
116 | Some (ids_to_terms,_,_) ->
117 (try Hashtbl.find ids_to_terms xpath with _ -> raise Exit)
118 | None -> assert false) (* "ERROR: No current term!!!" *)
121 method get_selected_hypotheses =
122 let selections = self#get_selections in
126 ((node : Gdome.element)#getAttributeNS
127 ~namespaceURI:Misc.helm_ns
128 ~localName:(Gdome.domString "xref"))#to_string
130 if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
132 match current_infos with
133 | Some (_,_,ids_to_hypotheses) ->
134 (try Hashtbl.find ids_to_hypotheses xpath with _ -> raise Exit)
135 | None -> assert false) (* "ERROR: No current term!!!" *)
138 method load_sequent metasenv metano =
139 let sequent = CicUtil.lookup_meta metano metasenv in
140 let (mathml,(_,(ids_to_terms, ids_to_father_ids, ids_to_hypotheses,_))) =
141 ApplyTransformation.mml_of_cic_sequent metasenv sequent
143 current_infos <- Some (ids_to_terms, ids_to_father_ids, ids_to_hypotheses);
145 debug_print "load_sequent: dumping MathML to ./prova";
146 ignore (Misc.domImpl#saveDocumentToFile ~name:"./prova" ~doc:mathml ());
148 self#load_root ~root:mathml#get_documentElement
151 class sequentsViewer ~(notebook:GPack.notebook)
152 ~(sequentViewer:sequentViewer) ()
155 inherit scriptAccessor
157 val mutable pages = 0
158 val mutable switch_page_callback = None
159 val mutable page2goal = [] (* associative list: page no -> goal no *)
160 val mutable goal2page = [] (* the other way round *)
161 val mutable goal2win = [] (* associative list: goal no -> scrolled win *)
162 val mutable _metasenv = []
163 val mutable scrolledWin: GBin.scrolled_window option = None
164 (* scrolled window to which the sequentViewer is currently attached *)
166 method private tab_label metano =
167 (GMisc.label ~text:(sprintf "?%d" metano) ~show:true ())#coerce
170 (match scrolledWin with
172 (* removing page from the notebook will destroy all contained widget,
173 * we do not want the sequentViewer to be destroyed as well *)
174 w#remove sequentViewer#coerce;
177 for i = 1 to pages do notebook#remove_page 0 done;
183 self#script#setGoal ~-1;
184 (match switch_page_callback with
186 GtkSignal.disconnect notebook#as_widget id;
187 switch_page_callback <- None
190 method load_sequents (status: ProofEngineTypes.status) =
191 let ((_, metasenv, _, _), goal) = status in
192 let sequents_no = List.length metasenv in
193 _metasenv <- metasenv;
194 pages <- sequents_no;
195 self#script#setGoal goal;
198 GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
199 ~shadow_type:`IN ~show:true ()
202 scrolledWin <- Some w;
203 match sequentViewer#misc#parent with
204 | None -> w#add sequentViewer#coerce
205 | Some _ -> sequentViewer#misc#reparent w#coerce
207 goal2win <- (metano, reparent) :: goal2win;
212 (fun (metano, _, _) ->
213 page2goal <- (!page, metano) :: page2goal;
214 goal2page <- (metano, !page) :: goal2page;
216 notebook#append_page ~tab_label:(self#tab_label metano) (win metano))
218 switch_page_callback <-
219 Some (notebook#connect#switch_page ~callback:(fun page ->
222 List.assoc page page2goal
223 with Not_found -> assert false
225 self#script#setGoal goal;
226 self#render_page ~page ~goal))
228 method private render_page ~page ~goal =
229 sequentViewer#load_sequent _metasenv goal;
231 List.assoc goal goal2win ();
232 sequentViewer#set_selection None
233 with Not_found -> assert false
235 method goto_sequent goal =
238 List.assoc goal goal2page
239 with Not_found -> assert false
241 notebook#goto_page page;
242 self#render_page page goal
248 type 'widget constructor =
249 ?hadjustment:GData.adjustment ->
250 ?vadjustment:GData.adjustment ->
252 ?log_verbosity:int ->
255 ?packing:(GObj.widget -> unit) ->
260 let sequentViewer ?hadjustment ?vadjustment ?font_size ?log_verbosity =
261 GtkBase.Widget.size_params
262 ~cont:(OgtkMathViewProps.pack_return (fun p ->
263 OgtkMathViewProps.set_params
264 (new sequentViewer (GtkMathViewProps.MathView_GMetaDOM.create p))
265 ~font_size ~log_verbosity))
268 let blank_uri = BuildTimeConf.blank_uri
269 let current_proof_uri = BuildTimeConf.current_proof_uri
272 [ `Ast of DisambiguateTypes.term
273 | `Cic of Cic.term * Cic.metasenv
277 exception Browser_failure of string
279 let cicBrowsers = ref []
281 class type cicBrowser =
283 method loadUri: string -> unit
284 method loadTerm: term_source -> unit
287 class cicBrowser_impl ~(history:string MatitaMisc.history) () =
288 let term_RE = Pcre.regexp "^term:(.*)" in
289 let trailing_slash_RE = Pcre.regexp "/$" in
290 let gui = MatitaGui.instance () in
291 let win = gui#newBrowserWin () in
292 let toplevel = win#toplevel in
293 let mathView = sequentViewer ~packing:win#scrolledBrowser#add () in
295 ignore (MatitaGtkMisc.ask_confirmation ~gui:(MatitaGui.instance ())
296 ~title:"Cic browser" ~msg ~cancel:false ());
302 fail (sprintf "Uncaught exception:\n%s" (Printexc.to_string exn))
304 let handle_error' f = fun () -> handle_error f in (* used in callbacks *)
306 inherit scriptAccessor
309 ignore (win#browserUri#connect#activate (handle_error' (fun () ->
310 self#_loadUri win#browserUri#text)));
311 ignore (win#browserHomeButton#connect#clicked (handle_error' (fun () ->
312 self#_loadUri current_proof_uri)));
313 ignore (win#browserRefreshButton#connect#clicked
314 (handle_error' self#refresh));
315 ignore (win#browserBackButton#connect#clicked (handle_error' self#back));
316 ignore (win#browserForwardButton#connect#clicked
317 (handle_error' self#forward));
318 ignore (win#toplevel#event#connect#delete (fun _ ->
319 let my_id = Oo.id self in
320 cicBrowsers := List.filter (fun b -> Oo.id b <> my_id) !cicBrowsers;
321 if !cicBrowsers = [] &&
322 Helm_registry.get "matita.mode" = "cicbrowser"
326 mathView#set_href_callback (Some (fun uri ->
327 handle_error (fun () -> self#_loadUri uri)));
328 self#_loadUri ~add_to_history:false blank_uri;
331 val mutable current_uri = ""
332 val mutable current_infos = None
333 val mutable current_mathml = None
335 method private back () =
337 self#_loadUri ~add_to_history:false history#previous
338 with MatitaMisc.History_failure -> ()
340 method private forward () =
342 self#_loadUri ~add_to_history:false history#next
343 with MatitaMisc.History_failure -> ()
345 (* loads a uri which can be a cic uri or an about:* uri
346 * @param uri string *)
347 method private _loadUri ?(add_to_history = true) uri =
349 if current_uri <> uri || uri = current_proof_uri then begin
351 | uri when uri = blank_uri -> self#blank ()
352 | uri when uri = current_proof_uri -> self#home ()
353 | uri when Pcre.pmatch ~rex:term_RE uri ->
354 self#loadTerm (`String (Pcre.extract ~rex:term_RE uri).(1))
355 | uri when Pcre.pmatch ~rex:trailing_slash_RE uri -> self#loadDir uri
356 | _ -> self#loadUriManagerUri (UriManager.uri_of_string uri));
358 if add_to_history then history#add uri
361 | UriManager.IllFormedUri _ -> fail (sprintf "invalid uri: %s" uri)
362 | CicEnvironment.Object_not_found _ ->
363 fail (sprintf "object not found: %s" uri)
364 | Browser_failure msg -> fail msg
367 handle_error (fun () -> self#_loadUri ~add_to_history:true uri)
369 method private blank () =
370 mathView#load_root (MatitaMisc.empty_mathml ())#get_documentElement
372 method private home () =
373 match self#script#status.proof_status with
374 | Proof (uri, metasenv, bo, ty) ->
375 let name = UriManager.name_of_uri (MatitaMisc.unopt uri) in
376 let obj = Cic.CurrentProof (name, metasenv, bo, ty, [], []) in
378 | Incomplete_proof ((uri, metasenv, bo, ty), _) ->
379 let name = UriManager.name_of_uri (MatitaMisc.unopt uri) in
380 let obj = Cic.CurrentProof (name, metasenv, bo, ty, [], []) in
384 (** loads a cic uri from the environment
385 * @param uri UriManager.uri *)
386 method private loadUriManagerUri uri =
387 let uri = UriManager.strip_xpointer uri in
388 let (obj, _) = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
391 method private loadDir dir =
392 let mathml = MatitaMisc.empty_boxml () in
393 let content = Http_getter.ls dir in
394 let root = mathml#get_documentElement in
395 let new_box_elt name =
396 mathml#createElementNS ~namespaceURI:(Some Misc.boxml_ns)
397 ~qualifiedName:(Gdome.domString ("b:" ^ name))
399 let new_text content = mathml#createTextNode (Gdome.domString content) in
400 let b_v = new_box_elt "v" in
403 let b_text = new_box_elt "text" in
406 | Http_getter_types.Ls_section subdir ->
407 (dir ^ subdir ^ "/"), (new_text (subdir ^ "/"))
408 | Http_getter_types.Ls_object obj ->
409 (dir ^ obj.Http_getter_types.uri),
410 (new_text obj.Http_getter_types.uri)
412 b_text#setAttributeNS ~namespaceURI:(Some Misc.xlink_ns)
413 ~qualifiedName:(Gdome.domString "xlink:href")
414 ~value:(Gdome.domString uri);
415 ignore (b_v#appendChild ~newChild:(b_text :> Gdome.node));
416 ignore (b_text#appendChild ~newChild:(elt :> Gdome.node)))
418 ignore (root#appendChild ~newChild:(b_v :> Gdome.node));
419 (* Misc.domImpl#saveDocumentToFile ~doc:mathml ~name:"pippo" (); *)
420 mathView#load_root ~root:root
422 method private setUri uri =
423 win#browserUri#set_text uri;
426 method private loadObj obj =
427 let use_diff = false in (* ZACK TODO use XmlDiff when re-rendering? *)
428 let (mathml, (_,(ids_to_terms, ids_to_father_ids, ids_to_conjectures,
429 ids_to_hypotheses,_,_))) =
430 ApplyTransformation.mml_of_cic_object obj
432 current_infos <- Some (ids_to_terms, ids_to_father_ids,
433 ids_to_conjectures, ids_to_hypotheses);
434 match current_mathml with
435 | Some current_mathml when use_diff ->
437 XmlDiff.update_dom ~from:current_mathml mathml;
440 mathView#load_root ~root:mathml#get_documentElement;
441 current_mathml <- Some mathml
443 method private _loadTerm s = failwith "not implemented _loadTerm"
444 (* TODO self#_loadTermAst (disambiguator#parserr#parseTerm (Stream.of_string s)) *)
446 method private _loadTermAst ast = failwith "not implemented _loadTermAst"
448 let (_, metasenv, term, _) =
449 MatitaCicMisc.disambiguate ~disambiguator ~currentProof ast
451 self#_loadTermCic term metasenv
454 method private _loadTermCic term metasenv =
455 let context = self#script#proofContext in
456 let dummyno = CicMkImplicit.new_meta metasenv [] in
457 let sequent = (dummyno, context, term) in
458 mathView#load_sequent (sequent :: metasenv) dummyno
460 method loadTerm (src:term_source) =
461 handle_error (fun () ->
463 | `Ast ast -> self#_loadTermAst ast
464 | `Cic (cic, metasenv) -> self#_loadTermCic cic metasenv
465 | `String s -> self#_loadTerm s);
468 (** {2 methods used by constructor only} *)
471 method history = history
472 method currentUri = current_uri
474 if current_uri = current_proof_uri then
475 self#_loadUri ~add_to_history:false current_proof_uri
479 let sequentsViewer ~(notebook:GPack.notebook)
480 ~(sequentViewer:sequentViewer) ()
482 new sequentsViewer ~notebook ~sequentViewer ()
485 let size = BuildTimeConf.browser_history_size in
486 let rec aux history =
487 let browser = new cicBrowser_impl ~history () in
488 let win = browser#win in
489 ignore (win#browserNewButton#connect#clicked (fun () ->
491 new MatitaMisc.browser_history ~memento:history#save size ""
493 let newBrowser = aux history in
494 newBrowser#loadUri browser#currentUri));
496 (* attempt (failed) to close windows on CTRL-W ... *)
497 MatitaGtkMisc.connect_key win#browserWinEventBox#event ~modifiers:[`CONTROL]
498 GdkKeysyms._W (fun () -> win#toplevel#destroy ());
500 cicBrowsers := browser :: !cicBrowsers;
501 (browser :> cicBrowser)
503 let history = new MatitaMisc.browser_history size blank_uri in
506 let refresh_all_browsers () = List.iter (fun b -> b#refresh ()) !cicBrowsers
508 let default_sequentViewer () = sequentViewer ~show:true ()
509 let sequentViewer_instance = MatitaMisc.singleton default_sequentViewer
511 let default_sequentsViewer () =
512 let gui = MatitaGui.instance () in
513 let sequentViewer = sequentViewer_instance () in
514 sequentsViewer ~notebook:gui#main#sequentsNotebook ~sequentViewer ()
515 let sequentsViewer_instance = MatitaMisc.singleton default_sequentsViewer
520 method show_term t = (cicBrowser ()) # loadTerm t