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