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