1 (* Copyright (C) 2004, 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://helm.cs.unibo.it/
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 _ ->
35 check#set_active false;
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 ();
42 | Some check -> check#set_active (not check#active)
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 ->
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
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)
63 method list_store = list_store
65 method easy_append s =
66 let tree_iter = list_store#append () in
67 list_store#set ~row:tree_iter ~column:text_column s
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
73 method easy_selection () =
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
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 ->
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);
99 (* try to find a reasonable name for an interpretation *)
103 List.assoc "0" interp
105 incr idx; string_of_int !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;
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)
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
132 String.sub s (String.length s - 4) 4 = ".var"
133 with Invalid_argument _ -> false
135 let non p x = not (p x)
139 method newUriDialog: unit -> MatitaGeneratedGui.uriChoiceDialog
140 method newInterpDialog: unit -> MatitaGeneratedGui.interpChoiceDialog
141 method newConfirmationDialog: unit -> MatitaGeneratedGui.confirmationDialog
142 method newEmptyDialog: unit -> MatitaGeneratedGui.emptyDialog
147 let interactive_user_uri_choice
148 ~(gui:#gui) ~(selection_mode:[`SINGLE|`MULTIPLE]) ?(title = "")
149 ?(msg = "") ?(nonvars_button=false) uris
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")
155 Lazy.force nonvars_uris
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;
169 dialog#uriChoiceDialog#destroy ();
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
181 | uris -> return (Some uris)));
182 ignore (dialog#uriChoiceAbortButton#connect#clicked (fun _ -> return None));
183 dialog#uriChoiceDialog#show ();
186 | None -> raise Cancel
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
199 dialog#interpChoiceDialog#destroy ();
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);
209 let selection = dialog#interpChoiceTreeView#selection in
210 ignore (selection#connect#changed (fun _ ->
211 match selection#get_selected_rows with
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 ();
218 (match !interp_no with Some row -> [row] | _ -> raise Cancel)
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
227 dialog#confirmationDialog#destroy ();
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 ();
235 (match !result with None -> assert false | Some r -> r)
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
244 dialog#emptyDialog#destroy ();
247 ignore (dialog#emptyDialog#event#connect#delete (fun _ -> true));
248 if multiline then begin (* multiline input required: use a TextView widget *)
250 GBin.scrolled_window ~width:400 ~height:150 ~hpolicy:`NEVER
251 ~vpolicy:`ALWAYS ~packing:dialog#emptyDialogVBox#add ()
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)))
263 ignore (dialog#emptyDialogCancelButton#connect#clicked (fun _ ->return None));
264 dialog#emptyDialog#show ();
266 (match !result with None -> raise Cancel | Some r -> r)