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