]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/texTermEditor.ml
b8375b3eab0a9a861d8841c35a8cef17f4c714ed
[helm.git] / helm / gTopLevel / texTermEditor.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 (*                                 06/01/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36 (* A WIDGET TO ENTER CIC TERMS *)
37
38 class type term_editor =
39  object
40    method coerce : GObj.widget
41    method get_as_string : string
42    method get_metasenv_and_term :
43      context:Cic.context ->
44      metasenv:Cic.metasenv -> Cic.metasenv * Cic.term
45    method reset : unit
46    method set_term : string -> unit
47    method id_to_uris : Disambiguate.domain_and_interpretation ref
48  end
49 ;;
50
51 let empty_id_to_uris = ([],function _ -> None);;
52
53 module Make(C:Disambiguate.Callbacks) =
54   struct
55
56    module Disambiguate' = Disambiguate.Make(C);;
57
58    class term_editor_impl
59     mqi_handle
60     ?packing ?width ?height
61     ?isnotempty_callback ?share_id_to_uris_with () : term_editor
62    =
63     let mmlwidget =
64      GMathViewAux.single_selection_math_view
65       ?packing ?width ?height () in
66     let drawing_area = mmlwidget#get_drawing_area in
67     let _ = drawing_area#misc#set_can_focus true in
68     let _ = drawing_area#misc#grab_focus () in
69     let logger =
70      fun l s -> prerr_endline ("TERM_EDITOR (" ^ string_of_int l ^ "): " ^ s) in
71     let tex_editor =
72      Mathml_editor.create
73       Mathml_editor.default_dictionary_path
74       Mathml_editor.default_mathml_stylesheet_path
75       Mathml_editor.default_tex_stylesheet_path
76       logger
77     in
78     let _ =
79      (new GObj.event_ops mmlwidget#coerce#as_widget)#connect#button_press
80       ~callback:(fun _ -> drawing_area#misc#grab_focus () ; true) in
81     let _ =
82      (new GObj.event_ops drawing_area#coerce#as_widget)#connect#focus_in
83        ~callback:
84          (fun _ ->
85            mmlwidget#freeze ;
86            Mathml_editor.cursor_show ~editor:tex_editor ;
87            mmlwidget#thaw ;
88            true) in
89     let _ =
90      (new GObj.event_ops drawing_area#coerce#as_widget)#connect#focus_out
91        ~callback:
92          (fun _ ->
93            mmlwidget#freeze ;
94            Mathml_editor.cursor_hide ~editor:tex_editor ;
95            mmlwidget#thaw ;
96            true) in
97     let _ = Mathml_editor.push tex_editor '$' in
98     let dom_tree = Mathml_editor.get_mml tex_editor in
99     let _ = mmlwidget#load_doc dom_tree in
100     let _ = 
101      drawing_area#event#connect#key_press
102       (function e ->
103         let key = GdkEvent.Key.keyval e in
104          mmlwidget#freeze ;
105 (*CSC: pre-lexer *)
106          if key = GdkKeysyms._space && GdkEvent.Key.state e = [] then
107           begin
108            ignore (Mathml_editor.freeze tex_editor) ;
109            Mathml_editor.push tex_editor '\\' ;
110            Mathml_editor.push tex_editor ';' ;
111            ignore (Mathml_editor.thaw tex_editor)
112           end
113 (*CSC: end of pre-lexer *)
114          else if
115           key >= 32 && key < 256 &&
116            (GdkEvent.Key.state e = [] || GdkEvent.Key.state e = [`SHIFT])
117          then
118           Mathml_editor.push tex_editor (Char.chr key)
119          else if key = GdkKeysyms._u then
120           begin
121            mmlwidget#freeze ;
122            ignore (Mathml_editor.freeze tex_editor) ;
123            Mathml_editor.reset tex_editor ;
124            Mathml_editor.push tex_editor '$' ;
125            ignore (Mathml_editor.thaw tex_editor) ;
126            mmlwidget#thaw
127           end
128          else if key = GdkKeysyms._BackSpace then
129           Mathml_editor.drop tex_editor false ;
130          mmlwidget#thaw ;
131          false) in
132     let id_to_uris =
133      match share_id_to_uris_with with
134         None -> ref empty_id_to_uris
135       | Some obj -> obj#id_to_uris
136     in
137     let _ =
138      match isnotempty_callback with
139         None -> ()
140       | Some callback ->
141          (* This approximation of the test that checks if the tree is empty *)
142          (* is utterly unprecise. We assume a tree to look as an empty tree *)
143          (* iff it is made of just one node m:mtext (which should be the    *)
144          (* cursor).                                                        *)
145          let is_empty_tree () =
146           let root = dom_tree#get_documentElement in
147            match root#get_firstChild with
148               None -> true
149             | Some n -> n#get_nodeName#to_string = "m:mtext"
150          in
151           dom_tree#addEventListener
152            ~typ:(Gdome.domString "DOMSubtreeModified")
153            ~listener:
154              (Gdome.eventListener
155                (function _ -> callback (not (is_empty_tree ()))))
156            ~useCapture:false
157     in
158      object(self)
159       method coerce = mmlwidget#coerce
160       method reset =
161        mmlwidget#freeze ;
162        ignore (Mathml_editor.freeze tex_editor) ;
163        Mathml_editor.reset tex_editor ;
164        Mathml_editor.push tex_editor '$' ;
165        ignore (Mathml_editor.thaw tex_editor) ;
166        mmlwidget#thaw
167
168       method set_term txt =
169        mmlwidget#freeze ;
170        ignore (Mathml_editor.freeze tex_editor) ;
171        self#reset ;
172        (* we need to remove the initial and final '$' *)
173        let txt' = String.sub txt 1 (String.length txt - 2) in
174         String.iter (fun ch -> Mathml_editor.push tex_editor ch) txt' ;
175         ignore (Mathml_editor.thaw tex_editor) ;
176         mmlwidget#thaw
177
178       method get_as_string =
179        Mathml_editor.get_tex tex_editor
180
181       method get_metasenv_and_term ~context ~metasenv =
182        let name_context =
183         List.map
184          (function
185              Some (n,_) -> Some n
186            | None -> None
187          ) context
188        in
189         let lexbuf = Lexing.from_string self#get_as_string in
190          let dom,mk_metasenv_and_expr =
191           TexCicTextualParserContext.main
192            ~context:name_context ~metasenv TexCicTextualLexer.token lexbuf
193          in
194           let id_to_uris',metasenv,expr =
195            Disambiguate'.disambiguate_input mqi_handle 
196             context metasenv dom mk_metasenv_and_expr ~id_to_uris:!id_to_uris
197           in
198            id_to_uris := id_to_uris' ;
199            metasenv,expr
200       method id_to_uris = id_to_uris
201    end
202
203    let term_editor = new term_editor_impl
204
205 end
206 ;;