]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaGtkMisc.ml
snapshot, notably:
[helm.git] / helm / matita / matitaGtkMisc.ml
1 (* Copyright (C) 2004, 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://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 open MatitaTypes
29
30 let toggle_visibility ~(win: GWindow.window) ~(check: GMenu.check_menu_item) =
31   ignore (check#connect#toggled (fun _ ->
32     if check#active then win#show () else win#misc#hide ()));
33   ignore (win#event#connect#delete (fun _ ->
34     win#misc#hide ();
35     check#set_active false;
36     true))
37
38 let toggle_win ?(check: GMenu.check_menu_item option) (win: GWindow.window) () =
39   if win#is_active then win#misc#hide () else win#show ();
40   match check with
41   | None -> ()
42   | Some check -> check#set_active (not check#active)
43
44 let add_key_binding key callback (evbox: GBin.event_box) =
45   ignore (evbox#event#connect#key_press (function
46     | key' when GdkEvent.Key.keyval key' = key ->
47         callback ();
48         false
49     | _ -> false))
50
51 class stringListModel (tree_view: GTree.view) =
52   let column_list = new GTree.column_list in
53   let text_column = column_list#add Gobject.Data.string in
54   let list_store = GTree.list_store column_list in
55   object (self)
56
57     initializer
58       let renderer = (GTree.cell_renderer_text [], ["text", text_column]) in
59       let view_column = GTree.view_column ~renderer () in
60       tree_view#set_model (Some (list_store :> GTree.model));
61       ignore (tree_view#append_column view_column)
62
63     method list_store = list_store
64
65     method easy_append s =
66       let tree_iter = list_store#append () in
67       list_store#set ~row:tree_iter ~column:text_column s
68
69     method easy_insert pos s =
70       let tree_iter = list_store#insert pos in
71       list_store#set ~row:tree_iter ~column:text_column s
72
73     method easy_selection () =
74       List.map
75         (fun tree_path ->
76           let iter = list_store#get_iter tree_path in
77           list_store#get ~row:iter ~column:text_column)
78         tree_view#selection#get_selected_rows
79
80   end
81
82 class interpModel =
83   let cols = new GTree.column_list in
84   let id_col = cols#add Gobject.Data.string in
85   let dsc_col = cols#add Gobject.Data.string in
86   let interp_no_col = cols#add Gobject.Data.int in
87   let tree_store = GTree.tree_store cols in
88   let id_renderer = GTree.cell_renderer_text [], ["text", id_col] in
89   let dsc_renderer = GTree.cell_renderer_text [], ["text", dsc_col] in
90   let id_view_col = GTree.view_column ~renderer:id_renderer () in
91   let dsc_view_col = GTree.view_column ~renderer:dsc_renderer () in
92   fun tree_view choices ->
93     object
94       initializer
95         tree_view#set_model (Some (tree_store :> GTree.model));
96         ignore (tree_view#append_column id_view_col);
97         ignore (tree_view#append_column dsc_view_col);
98         let name_of_interp =
99           (* try to find a reasonable name for an interpretation *)
100           let idx = ref 0 in
101           fun interp ->
102             try
103               List.assoc "0" interp
104             with Not_found ->
105               incr idx; string_of_int !idx
106         in
107         tree_store#clear ();
108         let idx = ref ~-1 in
109         List.iter
110           (fun interp ->
111             incr idx;
112             let interp_row = tree_store#append () in
113             tree_store#set ~row:interp_row ~column:id_col
114               (name_of_interp interp);
115             tree_store#set ~row:interp_row ~column:interp_no_col !idx;
116             List.iter
117               (fun (id, dsc) ->
118                 let row = tree_store#append ~parent:interp_row () in
119                 tree_store#set ~row ~column:id_col id;
120                 tree_store#set ~row ~column:dsc_col dsc;
121                 tree_store#set ~row ~column:interp_no_col !idx)
122               interp)
123           choices
124
125       method get_interp_no tree_path =
126         let iter = tree_store#get_iter tree_path in
127         tree_store#get ~row:iter ~column:interp_no_col
128     end
129
130 let is_var_uri s =
131   try
132     String.sub s (String.length s - 4) 4 = ".var"
133   with Invalid_argument _ -> false
134
135 let non p x = not (p x)
136
137 class type gui =
138   object
139     method newUriDialog:          unit -> MatitaGeneratedGui.uriChoiceDialog
140     method newInterpDialog:       unit -> MatitaGeneratedGui.interpChoiceDialog
141     method newConfirmationDialog: unit -> MatitaGeneratedGui.confirmationDialog
142     method newEmptyDialog:        unit -> MatitaGeneratedGui.emptyDialog
143   end
144
145 exception Cancel
146
147 let interactive_user_uri_choice
148   ~(gui:#gui) ~(selection_mode:[`SINGLE|`MULTIPLE]) ?(title = "")
149   ?(msg = "") ?(nonvars_button=false) uris
150 =
151   let nonvars_uris = lazy (List.filter (non is_var_uri) uris) in
152   if (selection_mode <> `SINGLE) &&
153     (Helm_registry.get_bool "matita.auto_disambiguation")
154   then
155     Lazy.force nonvars_uris
156   else begin
157     let dialog = gui#newUriDialog () in
158     dialog#uriChoiceTreeView#selection#set_mode
159       (selection_mode :> Gtk.Tags.selection_mode);
160     let model = new stringListModel dialog#uriChoiceTreeView in
161     let choices = ref None in
162     let nonvars = ref false in
163     dialog#uriChoiceDialog#set_title title;
164     dialog#uriChoiceLabel#set_text msg;
165     List.iter model#easy_append uris;
166     dialog#uriChoiceConstantsButton#misc#set_sensitive nonvars_button;
167     let return v =
168       choices := v;
169       dialog#uriChoiceDialog#destroy ();
170       GMain.Main.quit ()
171     in
172     ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
173     ignore (dialog#uriChoiceConstantsButton#connect#clicked (fun _ ->
174       return (Some (Lazy.force nonvars_uris))));
175     ignore (dialog#uriChoiceAutoButton#connect#clicked (fun _ ->
176       Helm_registry.set_bool "matita.auto_disambiguation" true;
177       return (Some (Lazy.force nonvars_uris))));
178     ignore (dialog#uriChoiceSelectedButton#connect#clicked (fun _ ->
179       match model#easy_selection () with
180       | [] -> ()
181       | uris -> return (Some uris)));
182     ignore (dialog#uriChoiceAbortButton#connect#clicked (fun _ -> return None));
183     dialog#uriChoiceDialog#show ();
184     GtkThread.main ();
185     (match !choices with 
186     | None -> raise Cancel
187     | Some uris -> uris)
188   end
189
190 let interactive_interp_choice ~(gui:#gui) choices =
191   assert (choices <> []);
192   let dialog = gui#newInterpDialog () in
193   let model = new interpModel dialog#interpChoiceTreeView choices in
194   let interp_len = List.length (List.hd choices) in
195   dialog#interpChoiceDialog#set_title "Interpretation choice";
196   dialog#interpChoiceDialogLabel#set_label "Choose an interpretation:";
197   let interp_no = ref None in
198   let return _ =
199     dialog#interpChoiceDialog#destroy ();
200     GMain.Main.quit ()
201   in
202   ignore (dialog#interpChoiceDialog#event#connect#delete (fun _ -> true));
203   ignore (dialog#interpChoiceOkButton#connect#clicked (fun _ ->
204     match !interp_no with None -> () | Some _ -> return ()));
205   ignore (dialog#interpChoiceCancelButton#connect#clicked return);
206   ignore (dialog#interpChoiceTreeView#connect#row_activated (fun path _ ->
207     interp_no := Some (model#get_interp_no path);
208     return ()));
209   let selection = dialog#interpChoiceTreeView#selection in
210   ignore (selection#connect#changed (fun _ ->
211     match selection#get_selected_rows with
212     | [path] ->
213         debug_print (sprintf "selection: %d" (model#get_interp_no path));
214         interp_no := Some (model#get_interp_no path)
215     | _ -> assert false));
216   dialog#interpChoiceDialog#show ();
217   GtkThread.main ();
218   (match !interp_no with Some row -> [row] | _ -> raise Cancel)
219
220 let ask_confirmation ~(gui:#gui) ?(title = "") ?(msg = "") () =
221   let dialog = gui#newConfirmationDialog () in
222   dialog#confirmationDialog#set_title title;
223   dialog#confirmationDialogLabel#set_label msg;
224   let result = ref None in
225   let return r _ =
226     result := Some r;
227     dialog#confirmationDialog#destroy ();
228     GMain.Main.quit ()
229   in
230   ignore (dialog#confirmationDialog#event#connect#delete (fun _ -> true));
231   ignore (dialog#confirmationDialogOkButton#connect#clicked (return true));
232   ignore (dialog#confirmationDialogCancelButton#connect#clicked (return false));
233   dialog#confirmationDialog#show ();
234   GtkThread.main ();
235   (match !result with None -> assert false | Some r -> r)
236
237 let ask_text ~(gui:#gui) ?(title = "") ?(msg = "") ?(multiline = false) () =
238   let dialog = gui#newEmptyDialog () in
239   dialog#emptyDialog#set_title title;
240   dialog#emptyDialogLabel#set_label msg;
241   let result = ref None in
242   let return r =
243     result := r;
244     dialog#emptyDialog#destroy ();
245     GMain.Main.quit ()
246   in
247   ignore (dialog#emptyDialog#event#connect#delete (fun _ -> true));
248   if multiline then begin (* multiline input required: use a TextView widget *)
249     let win =
250       GBin.scrolled_window ~width:400 ~height:150 ~hpolicy:`NEVER
251         ~vpolicy:`ALWAYS ~packing:dialog#emptyDialogVBox#add ()
252     in
253     let view = GText.view ~wrap_mode:`CHAR ~packing:win#add () in
254     view#misc#grab_focus ();
255     ignore (dialog#emptyDialogOkButton#connect#clicked (fun _ ->
256       return (Some (view#buffer#get_text ()))))
257   end else begin (* monoline input required: use a TextEntry widget *)
258     let entry = GEdit.entry ~packing:dialog#emptyDialogVBox#add () in
259     entry#misc#grab_focus ();
260     ignore (dialog#emptyDialogOkButton#connect#clicked (fun _ ->
261       return (Some entry#text)))
262   end;
263   ignore (dialog#emptyDialogCancelButton#connect#clicked (fun _ ->return None));
264   dialog#emptyDialog#show ();
265   GtkThread.main ();
266   (match !result with None -> raise Cancel | Some r -> r)
267