]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/termViewer.ml
cb3b23a007be0a687bc0fd49e4b9d5f123ca8db1
[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 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 29/01/2003                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36 (* List utility functions *)
37 exception Skip;;
38
39 let list_map_fail f =
40  let rec aux =
41   function
42      [] -> []
43    | he::tl ->
44       try
45        let he' = f he in
46         he'::(aux tl)
47       with Skip ->
48        (aux tl)
49  in
50   aux
51 ;;
52 (* End of the list utility functions *)
53
54 (** A widget to render sequents **)
55
56 class sequent_viewer obj =
57  object(self)
58
59   inherit GMathViewAux.multi_selection_math_view obj
60
61   val mutable current_infos = None
62
63   (* returns the list of selected terms         *)
64   (* selections which are not terms are ignored *)
65   method get_selected_terms =
66    let selections = self#get_selections in
67     list_map_fail
68      (function node ->
69        let xpath =
70         ((node : Gdome.element)#getAttributeNS
71           ~namespaceURI:Misc.helmns
72           ~localName:(Gdome.domString "xref"))#to_string
73        in
74         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
75         else
76          match current_infos with
77             Some (ids_to_terms,_,_) ->
78              let id = xpath in
79               (try
80                 Hashtbl.find ids_to_terms id
81                with _ -> raise Skip)
82           | None -> assert false (* "ERROR: No current term!!!" *)
83      ) selections
84
85   (* returns the list of selected hypotheses         *)
86   (* selections which are not hypotheses are ignored *)
87   method get_selected_hypotheses =
88    let selections = self#get_selections in
89     list_map_fail
90      (function node ->
91        let xpath =
92         ((node : Gdome.element)#getAttributeNS
93           ~namespaceURI:Misc.helmns
94           ~localName:(Gdome.domString "xref"))#to_string
95        in
96         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
97         else
98          match current_infos with
99             Some (_,_,ids_to_hypotheses) ->
100              let id = xpath in
101               (try
102                 Hashtbl.find ids_to_hypotheses id
103                with _ -> raise Skip)
104           | None -> assert false (* "ERROR: No current term!!!" *)
105      ) selections
106   
107   method load_sequent metasenv sequent =
108    let sequent_gdome,ids_to_terms,ids_to_father_ids,ids_to_hypotheses =
109     SequentPp.XmlPp.print_sequent metasenv sequent in
110    let sequent_doc =
111     Xml2Gdome.document_of_xml Misc.domImpl sequent_gdome in
112    let sequent_mml =
113     ApplyStylesheets.apply_sequent_stylesheets sequent_doc
114    in
115     self#load_doc ~dom:sequent_mml ;
116     current_infos <-
117      Some (ids_to_terms,ids_to_father_ids,ids_to_hypotheses)
118  end
119 ;;
120
121 let sequent_viewer ?adjustmenth ?adjustmentv ?font_size ?font_manager
122  ?border_width ?width ?height ?packing ?show () =
123  let w =
124    GtkMathView.MathView.create
125     ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
126     ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
127     ()
128  in
129   GtkBase.Container.set w ?border_width ?width ?height;
130  let mathview = GObj.pack_return (new sequent_viewer w) ~packing ~show in
131  begin
132     match font_size with
133     | Some size -> mathview#set_font_size size
134     | None      -> ()
135   end;
136   begin
137     match font_manager with
138     | Some manager -> mathview#set_font_manager_type ~fm_type:manager
139     | None         -> ()
140   end;
141   mathview
142 ;;
143
144
145 (** A widget to render proofs **)
146
147 class proof_viewer obj =
148  object(self)
149
150   inherit GMathViewAux.single_selection_math_view obj
151
152   val mutable current_infos = None
153
154   method make_sequent_of_selected_term =
155    match self#get_selection with
156       Some node ->
157        let xpath =
158         ((node : Gdome.element)#getAttributeNS
159           ~namespaceURI:Misc.helmns
160           ~localName:(Gdome.domString "xref"))#to_string
161        in
162         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
163         else
164          begin
165           match current_infos with
166              Some (ids_to_terms, ids_to_father_ids, _, _) ->
167               let id = xpath in
168                LogicalOperations.to_sequent id ids_to_terms ids_to_father_ids
169            | None -> assert false (* "ERROR: No current term!!!" *)
170          end
171     | None -> assert false (* "ERROR: No selection!!!" *)
172
173   method focus_sequent_of_selected_term =
174    match self#get_selection with
175       Some node ->
176        let xpath =
177         ((node : Gdome.element)#getAttributeNS
178           ~namespaceURI:Misc.helmns
179           ~localName:(Gdome.domString "xref"))#to_string
180        in
181         if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
182         else
183          begin
184           match current_infos with
185              Some (ids_to_terms, ids_to_father_ids, _, _) ->
186               let id = xpath in
187                LogicalOperations.focus id ids_to_terms ids_to_father_ids
188            | None -> assert false (* "ERROR: No current term!!!" *)
189          end
190     | None -> assert false (* "ERROR: No selection!!!" *)
191
192   method load_proof uri currentproof =
193    let
194     (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,
195      ids_to_inner_types,ids_to_conjectures,ids_to_hypotheses)
196    =
197     Cic2acic.acic_object_of_cic_object currentproof
198    in
199     let mml =
200      ApplyStylesheets.mml_of_cic_object
201       ~explode_all:true uri acic ids_to_inner_sorts ids_to_inner_types
202     in
203      self#load_doc ~dom:mml ;
204      current_infos <-
205       Some
206        (ids_to_terms,ids_to_father_ids,ids_to_conjectures,ids_to_hypotheses) ;
207      (acic, ids_to_inner_types, ids_to_inner_sorts)
208  end
209 ;;
210
211 let proof_viewer ?adjustmenth ?adjustmentv ?font_size ?font_manager
212  ?border_width ?width ?height ?packing ?show () =
213  let w =
214    GtkMathView.MathView.create
215     ?adjustmenth:(Gaux.may_map ~f:GData.as_adjustment adjustmenth)
216     ?adjustmentv:(Gaux.may_map ~f:GData.as_adjustment adjustmentv)
217     ()
218  in
219   GtkBase.Container.set w ?border_width ?width ?height;
220  let mathview = GObj.pack_return (new proof_viewer w) ~packing ~show in
221  begin
222     match font_size with
223     | Some size -> mathview#set_font_size size
224     | None      -> ()
225   end;
226   begin
227     match font_manager with
228     | Some manager -> mathview#set_font_manager_type ~fm_type:manager
229     | None         -> ()
230   end;
231   mathview
232 ;;