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