]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMathView.ml
snapshot, notably:
[helm.git] / helm / matita / matitaMathView.ml
1 (* Copyright (C) 2000-2002, 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 MatitaCicMisc
29 open MatitaTypes
30
31 (* List utility functions *)
32 exception Skip
33
34 let list_map_fail f =
35  let rec aux =
36   function
37      [] -> []
38    | he::tl ->
39       try
40        let he' = f he in
41         he'::(aux tl)
42       with Skip ->
43        (aux tl)
44  in
45   aux
46
47 class clickable_math_view obj =
48   let href = Gdome.domString "href" in
49   let xref = Gdome.domString "xref" in
50   object (self)
51     inherit GMathViewAux.multi_selection_math_view obj
52
53     val mutable href_callback: (UriManager.uri -> unit) option = None
54     method set_href_callback f = href_callback <- f
55
56     initializer
57       ignore (self#connect#selection_changed self#choose_selection);
58       ignore (self#connect#click (fun (gdome_elt, _, _, _) ->
59         match gdome_elt with
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
63             | None -> ()
64             | Some f ->
65                 let uri =
66                   elt#getAttributeNS ~namespaceURI:Misc.xlink_ns ~localName:href
67                 in
68                 f (UriManager.uri_of_string (uri#to_string)))
69         | Some elt -> ignore (self#action_toggle elt)
70         | None -> ()))
71     method private choose_selection gdome_elt =
72       let rec aux elt =
73         if elt#hasAttributeNS ~namespaceURI:Misc.helm_ns ~localName:xref then
74           self#set_selection (Some elt)
75         else
76           try
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" *)
82       in
83       match gdome_elt with
84       | Some elt -> aux elt
85       | None   -> self#set_selection None
86   end
87
88 class proof_viewer obj =
89   object(self)
90
91     inherit clickable_math_view obj
92
93     val mutable current_infos = None
94     val mutable current_mathml = None
95
96     method load_proof ((uri_opt, _, _, _) as proof, (goal_opt: int option)) =
97       let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
98           ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses) =
99         Cic2acic.acic_object_of_cic_object (cicCurrentProof proof)
100       in
101       current_infos <- Some (ids_to_terms, ids_to_father_ids,
102         ids_to_conjectures, ids_to_hypotheses);
103       let mathml =
104         ApplyTransformation.mml_of_cic_object ~explode_all:true
105           (unopt_uri uri_opt) annobj ids_to_inner_sorts ids_to_inner_types
106       in
107       debug_print "load_proof: dumping MathML to ./proof";
108       ignore (Misc.domImpl#saveDocumentToFile ~name:"./proof" ~doc:mathml ());
109       match current_mathml with
110       |  None ->
111           self#load_root ~root:mathml#get_documentElement ;
112           current_mathml <- Some mathml
113       | Some current_mathml ->
114           self#freeze;
115           XmlDiff.update_dom ~from:current_mathml mathml ;
116           self#thaw
117   end
118
119 class sequent_viewer obj =
120   object(self)
121
122     inherit clickable_math_view obj
123
124     val mutable current_infos = None
125
126     method get_selected_terms =
127       let selections = self#get_selections in
128       list_map_fail
129         (fun node ->
130           let xpath =
131             ((node : Gdome.element)#getAttributeNS
132               ~namespaceURI:Misc.helm_ns
133               ~localName:(Gdome.domString "xref"))#to_string
134           in
135           if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
136           else
137             match current_infos with
138             | Some (ids_to_terms,_,_) ->
139                 (try
140                   Hashtbl.find ids_to_terms xpath
141                  with _ -> raise Skip)
142             | None -> assert false) (* "ERROR: No current term!!!" *)
143         selections
144
145     method get_selected_hypotheses =
146       let selections = self#get_selections in
147       list_map_fail
148         (fun node ->
149           let xpath =
150             ((node : Gdome.element)#getAttributeNS
151               ~namespaceURI:Misc.helm_ns
152               ~localName:(Gdome.domString "xref"))#to_string
153           in
154           if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
155           else
156             match current_infos with
157             | Some (_,_,ids_to_hypotheses) ->
158                 (try
159                   Hashtbl.find ids_to_hypotheses xpath
160                 with _ -> raise Skip)
161             | None -> assert false) (* "ERROR: No current term!!!" *)
162         selections
163   
164   method load_sequent metasenv metano =
165 (*
166     let (annconjecture, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
167         ids_to_hypotheses) =
168       Cic2acic.asequent_of_sequent metasenv conjecture
169     in
170 *)
171     let sequent = CicUtil.lookup_meta metano metasenv in
172     let (mathml, (ids_to_terms, ids_to_father_ids, ids_to_hypotheses)) =
173       ApplyTransformation.mml_of_cic_sequent metasenv sequent
174     in
175     current_infos <- Some (ids_to_terms, ids_to_father_ids, ids_to_hypotheses);
176     debug_print "load_sequent: dumping MathML to ./prova";
177     ignore (Misc.domImpl#saveDocumentToFile ~name:"./prova" ~doc:mathml ());
178     self#load_root ~root:mathml#get_documentElement
179  end
180
181
182 class sequents_viewer ~(notebook:GPack.notebook)
183   ~(sequent_viewer:sequent_viewer) ~set_goal ()
184 =
185   let tab_label metano =
186     (GMisc.label ~text:(sprintf "?%d" metano) ~show:true ())#coerce
187   in
188   object (self)
189     val mutable pages = 0
190     val mutable switch_page_callback = None
191     val mutable page2goal = []  (* associative list: page no -> goal no *)
192     val mutable goal2page = []  (* the other way round *)
193     val mutable goal2win = []   (* associative list: goal no -> scrolled win *)
194     val mutable _metasenv = []
195
196     method reset =
197       for i = 1 to pages do notebook#remove_page 0 done;
198       pages <- 0;
199       page2goal <- [];
200       goal2page <- [];
201       goal2win <- [];
202       _metasenv <- [];
203       (match switch_page_callback with
204       | Some id ->
205           GtkSignal.disconnect notebook#as_widget id;
206           switch_page_callback <- None
207       | None -> ())
208
209     method load_sequents metasenv =
210       let sequents_no = List.length metasenv in
211       _metasenv <- metasenv;
212       pages <- sequents_no;
213       let win metano =
214         let w =
215           GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
216             ~shadow_type:`IN ~show:true ()
217         in
218         let reparent () =
219           match sequent_viewer#misc#parent with
220           | None -> w#add sequent_viewer#coerce
221           | Some _ -> sequent_viewer#misc#reparent w#coerce
222         in
223         goal2win <- (metano, reparent) :: goal2win;
224         w#coerce
225       in
226       let page = ref 0 in
227       List.iter
228         (fun (metano, _, _) ->
229           page2goal <- (!page, metano) :: page2goal;
230           goal2page <- (metano, !page) :: goal2page;
231           incr page;
232           notebook#append_page ~tab_label:(tab_label metano) (win metano))
233         metasenv;
234       switch_page_callback <-
235         Some (notebook#connect#switch_page ~callback:(fun page ->
236           let goal =
237             try
238               List.assoc page page2goal
239             with Not_found -> assert false
240           in
241           set_goal goal;
242           self#render_page ~page ~goal))
243
244     method private render_page ~page ~goal =
245       sequent_viewer#load_sequent _metasenv goal;
246       try
247         List.assoc goal goal2win ();
248         sequent_viewer#set_selection None
249       with Not_found -> assert false
250
251     method goto_sequent goal =
252       let page =
253         try
254           List.assoc goal goal2page
255         with Not_found -> assert false
256       in
257       notebook#goto_page page;
258       self#render_page page goal;
259
260   end
261
262  (** constructors *)
263
264 let sequent_viewer ?hadjustment ?vadjustment ?font_size ?log_verbosity =
265   GtkBase.Widget.size_params
266     ~cont:(OgtkMathViewProps.pack_return (fun p ->
267       OgtkMathViewProps.set_params
268         (new sequent_viewer (GtkMathViewProps.MathView_GMetaDOM.create p))
269         ~font_size ~log_verbosity))
270     []
271
272 let proof_viewer ?hadjustment ?vadjustment ?font_size ?log_verbosity =
273   GtkBase.Widget.size_params
274     ~cont:(OgtkMathViewProps.pack_return (fun p ->
275       OgtkMathViewProps.set_params
276         (new proof_viewer (GtkMathViewProps.MathView_GMetaDOM.create p))
277         ~font_size ~log_verbosity))
278     []
279
280 let proof_viewer_instance =
281   let instance = lazy (
282     let gui = MatitaGui.instance () in
283     proof_viewer ~show:true ~packing:gui#proof#scrolledProof#add ()
284   ) in
285   fun () -> Lazy.force instance
286
287 let sequents_viewer ~(notebook:GPack.notebook)
288   ~(sequent_viewer:sequent_viewer) ~set_goal ()
289 =
290   new sequents_viewer ~notebook ~sequent_viewer ~set_goal ()
291
292 class mathViewer =
293   let href_callback: (UriManager.uri -> unit) option ref = ref None in
294   object
295     val check_widget =
296       lazy
297         (let gui = MatitaGui.instance () in
298         let sequent_viewer =
299           sequent_viewer ~show:true ~packing:gui#check#scrolledCheck#add ()
300         in
301         sequent_viewer#set_href_callback !href_callback;
302         sequent_viewer)
303
304     method set_href_callback f = href_callback := f
305
306     method checkTerm sequent metasenv =
307       let (metano, context, expr) = sequent in
308       let widget = Lazy.force check_widget in
309       let gui = MatitaGui.instance () in
310       gui#check#checkWin#show ();
311       gui#main#showCheckMenuItem#set_active true;
312       widget#load_sequent (sequent :: metasenv) metano
313
314     method unload () = (proof_viewer_instance ())#unload
315   end
316
317 let mathViewer () = new mathViewer
318