]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/termViewer.ml
helmns -> helm_ns
[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.helm_ns
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.helm_ns
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_root ~root:sequent_mml#get_documentElement ;
131 ignore (Misc.domImpl#saveDocumentToFile ~name:"/tmp/pippo" ~doc:sequent_mml ());
132      current_infos <-
133       Some (ids_to_terms,ids_to_father_ids,ids_to_hypotheses)
134  end
135 ;;
136
137 let sequent_viewer ~(mml_of_cic_sequent: mml_of_cic_sequent)
138   ?hadjustment ?vadjustment ?font_size ?log_verbosity
139 =
140   GtkBase.Widget.size_params ~cont:(
141   OgtkMathViewProps.pack_return
142     (fun p ->
143       OgtkMathViewProps.set_params
144         (new sequent_viewer ~mml_of_cic_sequent
145           (GtkMathViewProps.MathView_GMetaDOM.create p))
146         ~font_size ~log_verbosity)) []
147 ;;
148
149 (*
150 let sequent_viewer ?adjustmenth ?adjustmentv ?font_size ?font_manager
151  ?border_width ?width ?height ?packing ?show () =
152  let w =
153    GtkMathView.MathView.create
154     ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
155     ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
156     ()
157  in
158   GtkBase.Container.set w ?border_width ?width ?height;
159  let mathview = GObj.pack_return (new sequent_viewer w) ~packing ~show in
160  begin
161     match font_size with
162     | Some size -> mathview#set_font_size size
163     | None      -> ()
164   end;
165   begin
166     match font_manager with
167     | Some manager -> mathview#set_font_manager_type ~fm_type:manager
168     | None         -> ()
169   end;
170   mathview
171 ;;
172 *)
173
174 (** A widget to render proofs **)
175
176 class proof_viewer ~(mml_of_cic_object:mml_of_cic_object) obj =
177  object(self)
178
179   inherit GMathViewAux.single_selection_math_view obj
180
181 (*   initializer self#set_font_size 10 *)
182
183   val mutable current_infos = None
184   val mutable current_mml = None
185
186   method make_sequent_of_selected_term =
187    match self#get_selection with
188       Some node ->
189        let xpath =
190         ((node : Gdome.element)#getAttributeNS
191           ~namespaceURI:Misc.helm_ns
192           ~localName:(Gdome.domString "xref"))#to_string
193        in
194         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
195         else
196          begin
197           match current_infos with
198              Some (ids_to_terms, ids_to_father_ids, _, _) ->
199               let id = xpath in
200                LogicalOperations.to_sequent id ids_to_terms ids_to_father_ids
201            | None -> assert false (* "ERROR: No current term!!!" *)
202          end
203     | None -> assert false (* "ERROR: No selection!!!" *)
204
205   method focus_sequent_of_selected_term =
206    match self#get_selection with
207       Some node ->
208        let xpath =
209         ((node : Gdome.element)#getAttributeNS
210           ~namespaceURI:Misc.helm_ns
211           ~localName:(Gdome.domString "xref"))#to_string
212        in
213         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
214         else
215          begin
216           match current_infos with
217              Some (ids_to_terms, ids_to_father_ids, _, _) ->
218               let id = xpath in
219                LogicalOperations.focus id ids_to_terms ids_to_father_ids
220            | None -> assert false (* "ERROR: No current term!!!" *)
221          end
222     | None -> assert false (* "ERROR: No selection!!!" *)
223
224   method load_proof uri currentproof =
225     let
226       (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,
227        ids_to_inner_types,ids_to_conjectures,ids_to_hypotheses)
228       = Cic2acic.acic_object_of_cic_object currentproof
229     in
230     let mml =
231       mml_of_cic_object
232         ~explode_all:true uri acic ids_to_inner_sorts ids_to_inner_types
233     in
234     current_infos <-
235       Some
236        (ids_to_terms,ids_to_father_ids,ids_to_conjectures,ids_to_hypotheses);
237     (* self#load_doc ~dom:mml ;
238        current_mml <- Some mml ; *)
239     ignore(Misc.domImpl#saveDocumentToFile ~name:"/tmp/prova" ~doc:mml ());
240     (match current_mml with
241       None ->
242         let time1 = Sys.time () in
243         self#load_root ~root:mml#get_documentElement ;
244         let time2 = Sys.time () in
245         debug_print ("Loading and displaying the proof took " ^
246           string_of_float (time2 -. time1) ^ "seconds") ;
247         current_mml <- Some mml
248     | Some current_mml' ->
249         self#freeze ;
250         let time1 = Sys.time () in
251         XmlDiff.update_dom ~from:current_mml' mml ;
252         let time2 = Sys.time () in
253         debug_print ("XMLDIFF took " ^
254           string_of_float (time2 -. time1) ^ "seconds") ;
255         self#thaw ;
256         let time3 = Sys.time () in
257         debug_print ("The refresh of the widget took " ^
258           string_of_float (time3 -. time2) ^ "seconds"));
259     (acic, ids_to_inner_types, ids_to_inner_sorts)
260   end
261 ;;
262
263
264 let proof_viewer ~(mml_of_cic_object: mml_of_cic_object)
265   ?hadjustment ?vadjustment ?font_size ?log_verbosity
266 =
267   GtkBase.Widget.size_params ~cont:(
268   OgtkMathViewProps.pack_return
269     (fun p ->
270       OgtkMathViewProps.set_params
271         (new proof_viewer ~mml_of_cic_object
272           (GtkMathViewProps.MathView_GMetaDOM.create p))
273         ~font_size ~log_verbosity)) []
274 ;;
275
276 (*
277 let proof_viewer ?adjustmenth ?adjustmentv ?font_size ?font_manager
278  ?border_width ?width ?height ?packing ?show () =
279  let w =
280    GtkMathView.MathView.create
281     ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
282     ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
283     ()
284  in
285   GtkBase.Container.set w ?border_width ?width ?height;
286  let mathview = GObj.pack_return (new proof_viewer w) ~packing ~show in
287  begin
288     match font_size with
289     | Some size -> mathview#set_font_size size
290     | None      -> ()
291   end;
292   begin
293     match font_manager with
294     | Some manager -> mathview#set_font_manager_type ~fm_type:manager
295     | None         -> ()
296   end;
297   mathview
298 ;;
299 *)
300