1 (* Copyright (C) 2000-2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
34 (******************************************************************************)
36 (* A WIDGET TO ENTER CIC TERMS *)
38 class type term_editor =
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
47 (* The input of set_term is unquoted *)
48 method set_term : string -> unit
49 method id_to_uris : Disambiguate.domain_and_interpretation ref
53 let empty_id_to_uris = ([],function _ -> None);;
55 module Make(C:Disambiguate.Callbacks) =
58 module Disambiguate' = Disambiguate.Make(C);;
60 class term_editor_impl
62 ?packing ?width ?height
63 ?isnotempty_callback ?share_id_to_uris_with () : term_editor
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
72 fun l s -> prerr_endline ("TERM_EDITOR (" ^ string_of_int l ^ "): " ^ s) in
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
81 ~tml_uri:"/usr/share/editex/tml-litex.xsl"
85 (new GObj.event_ops mmlwidget#coerce#as_widget)#connect#button_press
86 ~callback:(fun _ -> drawing_area#misc#grab_focus () ; true) in
88 (new GObj.event_ops drawing_area#coerce#as_widget)#connect#focus_in
92 Mathml_editor.cursor_show ~editor:tex_editor ;
96 (new GObj.event_ops drawing_area#coerce#as_widget)#connect#focus_out
100 Mathml_editor.cursor_hide ~editor:tex_editor ;
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
107 drawing_area#event#connect#key_press
109 let key = GdkEvent.Key.keyval e in
112 key >= 32 && key < 256 &&
113 (GdkEvent.Key.state e = [] || GdkEvent.Key.state e = [`SHIFT])
115 Mathml_editor.push tex_editor (Char.chr key)
116 else if key = GdkKeysyms._u then
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) ;
125 else if key = GdkKeysyms._BackSpace then
126 Mathml_editor.drop tex_editor
127 (List.mem `CONTROL (GdkEvent.Key.state e))
128 else if key = GdkKeysyms._v then
129 ignore (mmlwidget#misc#convert_selection "STRING" Gdk.Atom.primary);
130 let adj = mmlwidget#get_hadjustment in
132 adj#set_value adj#upper ;
135 match share_id_to_uris_with with
136 None -> ref empty_id_to_uris
137 | Some obj -> obj#id_to_uris
140 match isnotempty_callback with
143 (* This approximation of the test that checks if the tree is empty *)
144 (* is utterly unprecise. We assume a tree to look as an empty tree *)
145 (* iff it is made of just one node m:mtext (which should be the *)
147 let is_empty_tree () =
148 let root = dom_tree#get_documentElement in
149 match root#get_firstChild with
151 | Some n -> n#get_nodeName#to_string = "m:mtext"
153 dom_tree#addEventListener
154 ~typ:(Gdome.domString "DOMSubtreeModified")
157 (function _ -> callback (not (is_empty_tree ()))))
163 ignore (mmlwidget#misc#connect#selection_received
164 ~callback: (fun selection_data ~time ->
165 let input = try selection_data#data with Gpointer.Null -> "" in
167 ignore (Mathml_editor.freeze tex_editor) ;
168 for i = 0 to String.length input - 1 do
169 Mathml_editor.push tex_editor input.[i]
171 ignore (Mathml_editor.thaw tex_editor) ;
174 method coerce = mmlwidget#coerce
177 ignore (Mathml_editor.freeze tex_editor) ;
178 Mathml_editor.reset tex_editor ;
179 Mathml_editor.push tex_editor '$' ;
180 ignore (Mathml_editor.thaw tex_editor) ;
183 (* The input of set_term is unquoted *)
184 method set_term txt =
186 ignore (Mathml_editor.freeze tex_editor) ;
188 let txt' = Str.global_replace (Str.regexp "_") "\\_" txt in
189 String.iter (fun ch -> Mathml_editor.push tex_editor ch) txt' ;
190 ignore (Mathml_editor.thaw tex_editor) ;
193 (* get_as_string returns the unquoted string *)
194 method get_as_string =
195 let term = Mathml_editor.get_tex tex_editor in
196 Str.global_replace (Str.regexp "^\\$\\$?") ""
197 (Str.global_replace (Str.regexp "\\$\\$?$") ""
198 (Str.global_replace (Str.regexp "\\\\_") "_" term))
200 method get_metasenv_and_term ~context ~metasenv =
208 prerr_endline ("###CSC: " ^ (Mathml_editor.get_tex tex_editor)) ;
209 let lexbuf = Lexing.from_string (Mathml_editor.get_tex tex_editor) in
210 let dom,mk_metasenv_and_expr =
211 TexCicTextualParserContext.main
212 ~context:name_context ~metasenv TexCicTextualLexer.token lexbuf
214 let id_to_uris',metasenv,expr =
215 Disambiguate'.disambiguate_input mqi_handle
216 context metasenv dom mk_metasenv_and_expr ~id_to_uris:!id_to_uris
218 id_to_uris := id_to_uris' ;
220 method id_to_uris = id_to_uris
223 let term_editor = new term_editor_impl