]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/termViewer.ml
- sync with the new ApplyTransformation API
[helm.git] / helm / gTopLevel / termViewer.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 (***************************************************************************)
27 (*                                                                         *)
28 (*                             PROJECT HELM                                *)
29 (*                                                                         *)
30 (*                  29/01/2003 Claudio Sacerdoti Coen                      *)
31 (*                                                                         *)
32 (*                                                                         *)
33 (***************************************************************************)
34
35 let debug = false
36 let debug_print s = if debug then prerr_endline s
37
38 type mml_of_cic_sequent =
39  Cic.metasenv ->
40  int * Cic.context * Cic.term ->
41  Gdome.document * 
42  (Cic.annconjecture *
43   ((Cic.id, Cic.term) Hashtbl.t *
44    (Cic.id, Cic.id option) Hashtbl.t *
45    (string, Cic.hypothesis) Hashtbl.t *
46    (Cic.id, string) Hashtbl.t))
47    
48
49 type mml_of_cic_object =
50   UriManager.uri ->
51   Cic.obj ->
52   Gdome.document * 
53   (Cic.annobj * 
54    ((Cic.id, Cic.term) Hashtbl.t * 
55     (Cic.id, Cic.id option) Hashtbl.t *
56     (Cic.id, Cic.conjecture) Hashtbl.t * 
57     (Cic.id, Cic.hypothesis) Hashtbl.t *
58     (Cic.id, string) Hashtbl.t *   
59     (Cic.id, Cic2acic.anntypes) Hashtbl.t))
60   
61 (* List utility functions *)
62 exception Skip;;
63
64 let list_map_fail f =
65  let rec aux =
66   function
67      [] -> []
68    | he::tl ->
69       try
70        let he' = f he in
71         he'::(aux tl)
72       with Skip ->
73        (aux tl)
74  in
75   aux
76 ;;
77 (* End of the list utility functions *)
78
79 (** A widget to render sequents **)
80
81 class sequent_viewer ~(mml_of_cic_sequent:mml_of_cic_sequent) obj =
82  object(self)
83
84   inherit GMathViewAux.multi_selection_math_view obj
85
86   val mutable current_infos = None
87
88   (* returns the list of selected terms         *)
89   (* selections which are not terms are ignored *)
90   method get_selected_terms =
91    debug_print (string_of_int (List.length self#get_selections)) ;
92    let selections = self#get_selections in
93     list_map_fail
94      (function node ->
95        let xpath =
96         ((node : Gdome.element)#getAttributeNS
97           ~namespaceURI:Misc.helm_ns
98           ~localName:(Gdome.domString "xref"))#to_string
99        in
100         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
101         else
102          match current_infos with
103             Some (ids_to_terms,_,_) ->
104              let id = xpath in
105               (try
106                 Hashtbl.find ids_to_terms id
107                with _ -> raise Skip)
108           | None -> assert false (* "ERROR: No current term!!!" *)
109      ) selections
110
111   (* returns the list of selected hypotheses         *)
112   (* selections which are not hypotheses are ignored *)
113   method get_selected_hypotheses =
114    let selections = self#get_selections in
115     list_map_fail
116      (function node ->
117        let xpath =
118         ((node : Gdome.element)#getAttributeNS
119           ~namespaceURI:Misc.helm_ns
120           ~localName:(Gdome.domString "xref"))#to_string
121        in
122         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
123         else
124          match current_infos with
125             Some (_,_,ids_to_hypotheses) ->
126              let id = xpath in
127               (try
128                 Hashtbl.find ids_to_hypotheses id
129                with _ -> raise Skip)
130           | None -> assert false (* "ERROR: No current term!!!" *)
131      ) selections
132   
133   method load_sequent metasenv sequent =
134 (**** SIAM QUI ****)
135    let sequent_mml,(_,(ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)) =
136      mml_of_cic_sequent metasenv sequent
137    in
138     self#load_root ~root:sequent_mml#get_documentElement ;
139 ignore (Misc.domImpl#saveDocumentToFile ~name:"/tmp/pippo" ~doc:sequent_mml ());
140      current_infos <-
141       Some (ids_to_terms,ids_to_father_ids,ids_to_hypotheses)
142  end
143 ;;
144
145 let sequent_viewer ~(mml_of_cic_sequent: mml_of_cic_sequent)
146   ?hadjustment ?vadjustment ?font_size ?log_verbosity
147 =
148   GtkBase.Widget.size_params ~cont:(
149   OgtkMathViewProps.pack_return
150     (fun p ->
151       OgtkMathViewProps.set_params
152         (new sequent_viewer ~mml_of_cic_sequent
153           (GtkMathViewProps.MathView_GMetaDOM.create p))
154         ~font_size ~log_verbosity)) []
155 ;;
156
157 (*
158 let sequent_viewer ?adjustmenth ?adjustmentv ?font_size ?font_manager
159  ?border_width ?width ?height ?packing ?show () =
160  let w =
161    GtkMathView.MathView.create
162     ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
163     ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
164     ()
165  in
166   GtkBase.Container.set w ?border_width ?width ?height;
167  let mathview = GObj.pack_return (new sequent_viewer w) ~packing ~show in
168  begin
169     match font_size with
170     | Some size -> mathview#set_font_size size
171     | None      -> ()
172   end;
173   begin
174     match font_manager with
175     | Some manager -> mathview#set_font_manager_type ~fm_type:manager
176     | None         -> ()
177   end;
178   mathview
179 ;;
180 *)
181
182 (** A widget to render proofs **)
183
184 class proof_viewer ~(mml_of_cic_object:mml_of_cic_object) obj =
185  object(self)
186
187   inherit GMathViewAux.single_selection_math_view obj
188
189 (*   initializer self#set_font_size 10 *)
190
191   val mutable current_infos = None
192   val mutable current_mml = None
193
194   method make_sequent_of_selected_term =
195    match self#get_selection with
196       Some node ->
197        let xpath =
198         ((node : Gdome.element)#getAttributeNS
199           ~namespaceURI:Misc.helm_ns
200           ~localName:(Gdome.domString "xref"))#to_string
201        in
202         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
203         else
204          begin
205           match current_infos with
206              Some (ids_to_terms, ids_to_father_ids, _, _) ->
207               let id = xpath in
208                LogicalOperations.to_sequent id ids_to_terms ids_to_father_ids
209            | None -> assert false (* "ERROR: No current term!!!" *)
210          end
211     | None -> assert false (* "ERROR: No selection!!!" *)
212
213   method focus_sequent_of_selected_term =
214    match self#get_selection with
215       Some node ->
216        let xpath =
217         ((node : Gdome.element)#getAttributeNS
218           ~namespaceURI:Misc.helm_ns
219           ~localName:(Gdome.domString "xref"))#to_string
220        in
221         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
222         else
223          begin
224           match current_infos with
225              Some (ids_to_terms, ids_to_father_ids, _, _) ->
226               let id = xpath in
227                LogicalOperations.focus id ids_to_terms ids_to_father_ids
228            | None -> assert false (* "ERROR: No current term!!!" *)
229          end
230     | None -> assert false (* "ERROR: No selection!!!" *)
231
232   method load_proof uri currentproof =
233     let mml,
234          (acic,
235            (ids_to_terms,ids_to_father_ids,ids_to_conjectures,
236             ids_to_hypotheses,ids_to_inner_sorts,ids_to_inner_types)) =
237       mml_of_cic_object uri currentproof
238     in
239     current_infos <-
240       Some
241        (ids_to_terms,ids_to_father_ids,ids_to_conjectures,ids_to_hypotheses);
242     (* self#load_doc ~dom:mml ;
243        current_mml <- Some mml ; *)
244     ignore(Misc.domImpl#saveDocumentToFile ~name:"/tmp/prova" ~doc:mml ());
245     (match current_mml with
246       None ->
247         let time1 = Sys.time () in
248         self#load_root ~root:mml#get_documentElement ;
249         let time2 = Sys.time () in
250         debug_print ("Loading and displaying the proof took " ^
251           string_of_float (time2 -. time1) ^ "seconds") ;
252         current_mml <- Some mml
253     | Some current_mml' ->
254         self#freeze ;
255         let time1 = Sys.time () in
256         XmlDiff.update_dom ~from:current_mml' mml ;
257         let time2 = Sys.time () in
258         debug_print ("XMLDIFF took " ^
259           string_of_float (time2 -. time1) ^ "seconds") ;
260         self#thaw ;
261         let time3 = Sys.time () in
262         debug_print ("The refresh of the widget took " ^
263           string_of_float (time3 -. time2) ^ "seconds"));
264     (acic, ids_to_inner_types, ids_to_inner_sorts)
265   end
266 ;;
267
268
269 let proof_viewer ~(mml_of_cic_object: mml_of_cic_object)
270   ?hadjustment ?vadjustment ?font_size ?log_verbosity
271 =
272   GtkBase.Widget.size_params ~cont:(
273   OgtkMathViewProps.pack_return
274     (fun p ->
275       OgtkMathViewProps.set_params
276         (new proof_viewer ~mml_of_cic_object
277           (GtkMathViewProps.MathView_GMetaDOM.create p))
278         ~font_size ~log_verbosity)) []
279 ;;
280
281 (*
282 let proof_viewer ?adjustmenth ?adjustmentv ?font_size ?font_manager
283  ?border_width ?width ?height ?packing ?show () =
284  let w =
285    GtkMathView.MathView.create
286     ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
287     ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
288     ()
289  in
290   GtkBase.Container.set w ?border_width ?width ?height;
291  let mathview = GObj.pack_return (new proof_viewer w) ~packing ~show in
292  begin
293     match font_size with
294     | Some size -> mathview#set_font_size size
295     | None      -> ()
296   end;
297   begin
298     match font_manager with
299     | Some manager -> mathview#set_font_manager_type ~fm_type:manager
300     | None         -> ()
301   end;
302   mathview
303 ;;
304 *)
305