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