]> 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 proof_viewer obj =
48  object(self)
49
50   inherit GMathViewAux.single_selection_math_view obj
51
52 (*   initializer self#set_log_verbosity 3 *)
53
54   initializer
55     ignore (self#connect#click (fun (gdome_elt, _, _, _) ->
56       match gdome_elt with
57       | Some gdome_elt ->
58           prerr_endline (gdome_elt#get_nodeName#to_string);
59           ignore (self#action_toggle gdome_elt)
60       | None -> ()))
61
62   val mutable current_infos = None
63   val mutable current_mathml = None
64
65   method load_proof ((uri_opt, _, _, _) as proof, (goal_opt: int option)) =
66     let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
67         ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses) =
68       Cic2acic.acic_object_of_cic_object (cicCurrentProof proof)
69     in
70     current_infos <- Some (ids_to_terms, ids_to_father_ids, ids_to_conjectures,
71       ids_to_hypotheses);
72     let mathml =
73       ApplyTransformation.mml_of_cic_object ~explode_all:true
74         (unopt_uri uri_opt) annobj ids_to_inner_sorts ids_to_inner_types
75     in
76     debug_print "load_proof: dumping MathML to /tmp/proof";
77     ignore (Misc.domImpl#saveDocumentToFile ~name:"/tmp/proof" ~doc:mathml ());
78     match current_mathml with
79     |  None ->
80         debug_print "no previous MathML";
81         self#load_root ~root:mathml#get_documentElement ;
82         current_mathml <- Some mathml
83     | Some current_mathml ->
84         debug_print "Previous MathML available: xmldiffing ...";
85         self#freeze ;
86         XmlDiff.update_dom ~from:current_mathml mathml ;
87         self#thaw
88   end
89
90 class sequent_viewer obj =
91  object(self)
92
93   inherit GMathViewAux.multi_selection_math_view obj
94
95   val mutable current_infos = None
96
97   method get_selected_terms =
98    let selections = self#get_selections in
99     list_map_fail
100      (function node ->
101        let xpath =
102         ((node : Gdome.element)#getAttributeNS
103           ~namespaceURI:Misc.helmns
104           ~localName:(Gdome.domString "xref"))#to_string
105        in
106         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
107         else
108          match current_infos with
109             Some (ids_to_terms,_,_) ->
110              let id = xpath in
111               (try
112                 Hashtbl.find ids_to_terms id
113                with _ -> raise Skip)
114           | None -> assert false (* "ERROR: No current term!!!" *)
115      ) selections
116
117   method get_selected_hypotheses =
118    let selections = self#get_selections in
119     list_map_fail
120      (function node ->
121        let xpath =
122         ((node : Gdome.element)#getAttributeNS
123           ~namespaceURI:Misc.helmns
124           ~localName:(Gdome.domString "xref"))#to_string
125        in
126         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
127         else
128          match current_infos with
129             Some (_,_,ids_to_hypotheses) ->
130              let id = xpath in
131               (try
132                 Hashtbl.find ids_to_hypotheses id
133                with _ -> raise Skip)
134           | None -> assert false (* "ERROR: No current term!!!" *)
135      ) selections
136   
137   method load_sequent metasenv metano =
138 (*
139     let (annconjecture, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
140         ids_to_hypotheses) =
141       Cic2acic.asequent_of_sequent metasenv conjecture
142     in
143 *)
144     let sequent = CicUtil.lookup_meta metano metasenv in
145     let (mathml, (ids_to_terms, ids_to_father_ids, ids_to_hypotheses)) =
146       ApplyTransformation.mml_of_cic_sequent metasenv sequent
147     in
148     current_infos <- Some (ids_to_terms, ids_to_father_ids, ids_to_hypotheses);
149     debug_print "load_sequent: dumping MathML to /tmp/prova";
150     ignore (Misc.domImpl#saveDocumentToFile ~name:"/tmp/prova" ~doc:mathml ());
151     self#load_root ~root:mathml#get_documentElement
152  end
153
154
155 class sequents_viewer ~(notebook:GPack.notebook)
156   ~(sequent_viewer:MatitaTypes.sequent_viewer) ~set_goal ()
157 =
158   object (self)
159     val mutable pages = 0
160     val mutable switch_page_callback = None
161     val mutable page2goal = []  (* associative list: page no -> goal no *)
162     val mutable goal2page = []  (* the other way round *)
163     val mutable _metasenv = []
164
165     method reset =
166       for i = 1 to pages do notebook#remove_page 0 done;
167       pages <- 0;
168       page2goal <- [];
169       goal2page <- [];
170       _metasenv <- [];
171       (match switch_page_callback with
172       | Some id ->
173           GtkSignal.disconnect notebook#as_widget id;
174           switch_page_callback <- None
175       | None -> ())
176
177     method load_sequents metasenv =
178       let sequents_no = List.length metasenv in
179       _metasenv <- metasenv;
180       debug_print (sprintf "sequents no: %d" sequents_no);
181       pages <- sequents_no;
182       let widget = sequent_viewer#coerce in
183       let page = ref 0 in
184       List.iter
185         (fun (metano, _, _) ->
186           page2goal <- (!page, metano) :: page2goal;
187           goal2page <- (metano, !page) :: goal2page;
188           incr page;
189           let tab_label =
190             (GMisc.label ~text:(sprintf "?%d" metano) ~show:true ())#coerce
191           in
192           notebook#append_page ~tab_label widget)
193         metasenv;
194       switch_page_callback <-
195         (* TODO Zack the "#after" may probably be removed after Luca's fix for
196         * widget not loading documents before being realized *)
197         Some (notebook#connect#after#switch_page ~callback:(fun page ->
198           debug_print "switch_page callback";
199           let goal =
200             try
201               List.assoc page page2goal
202             with Not_found -> assert false
203           in
204           set_goal goal;
205           self#render_page goal))
206
207     method private render_page goal =
208       sequent_viewer#load_sequent _metasenv goal
209
210     method goto_sequent goal =
211       let page =
212         try
213           List.assoc goal goal2page
214         with Not_found -> assert false
215       in
216       notebook#goto_page page;
217       self#render_page goal
218
219   end
220
221  (** constructors *)
222
223 let sequent_viewer ?hadjustment ?vadjustment ?font_size ?log_verbosity =
224   GtkBase.Widget.size_params
225     ~cont:(OgtkMathViewProps.pack_return (fun p ->
226       OgtkMathViewProps.set_params
227         (new sequent_viewer (GtkMathViewProps.MathView_GMetaDOM.create p))
228         ~font_size ~log_verbosity))
229     []
230
231 let proof_viewer ?hadjustment ?vadjustment ?font_size ?log_verbosity =
232   GtkBase.Widget.size_params
233     ~cont:(OgtkMathViewProps.pack_return (fun p ->
234       OgtkMathViewProps.set_params
235         (new proof_viewer (GtkMathViewProps.MathView_GMetaDOM.create p))
236         ~font_size ~log_verbosity))
237     []
238
239 let sequents_viewer ~(notebook:GPack.notebook)
240   ~(sequent_viewer:MatitaTypes.sequent_viewer) ~set_goal ()
241 =
242   new sequents_viewer ~notebook ~sequent_viewer ~set_goal ()
243