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