1 (* Copyright (C) 2004-2005, 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/
31 let wrap_callback f = f
33 let connect_button (button: #GButton.button) callback =
34 ignore (button#connect#clicked (wrap_callback callback))
36 let connect_toggle_button (button: #GButton.toggle_button) callback =
37 ignore (button#connect#toggled (wrap_callback callback))
39 let connect_menu_item (menu_item: #GMenu.menu_item) callback =
40 ignore (menu_item#connect#activate (wrap_callback callback))
42 let connect_key (ev:GObj.event_ops) ?(modifiers = []) ?(stop = false) key
45 ignore (ev#connect#key_press (fun key' ->
46 let modifiers' = GdkEvent.Key.state key' in
48 | key' when GdkEvent.Key.keyval key' = key
49 && List.for_all (fun m -> List.mem m modifiers') modifiers ->
54 let toggle_widget_visibility ~(widget: GObj.widget)
55 ~(check: GMenu.check_menu_item)
57 ignore (check#connect#toggled (fun _ ->
58 if check#active then widget#misc#show () else widget#misc#hide ()))
60 let toggle_window_visibility ~(window: GWindow.window)
61 ~(check: GMenu.check_menu_item)
63 ignore (check#connect#toggled (fun _ ->
64 if check#active then window#show () else window#misc#hide ()));
65 ignore (window#event#connect#delete (fun _ ->
67 check#set_active false;
70 let toggle_win ?(check: GMenu.check_menu_item option) (win: GWindow.window) () =
71 if win#is_active then win#misc#hide () else win#show ();
74 | Some check -> check#set_active (not check#active)
76 let toggle_callback ~callback ~(check: GMenu.check_menu_item) =
77 ignore (check#connect#toggled (fun _ -> callback check#active))
79 let add_key_binding key callback (evbox: GBin.event_box) =
80 ignore (evbox#event#connect#key_press (function
81 | key' when GdkEvent.Key.keyval key' = key ->
86 class stringListModel (tree_view: GTree.view) =
87 let column_list = new GTree.column_list in
88 let text_column = column_list#add Gobject.Data.string in
89 let list_store = GTree.list_store column_list in
90 let renderer = (GTree.cell_renderer_text [], ["text", text_column]) in
91 let view_column = GTree.view_column ~renderer () in
94 tree_view#set_model (Some (list_store :> GTree.model));
95 ignore (tree_view#append_column view_column)
97 method list_store = list_store
99 method easy_append s =
100 let tree_iter = list_store#append () in
101 list_store#set ~row:tree_iter ~column:text_column s
103 method easy_insert pos s =
104 let tree_iter = list_store#insert pos in
105 list_store#set ~row:tree_iter ~column:text_column s
107 method easy_selection () =
110 let iter = list_store#get_iter tree_path in
111 list_store#get ~row:iter ~column:text_column)
112 tree_view#selection#get_selected_rows
115 class taggedStringListModel ~(tags:(string * GdkPixbuf.pixbuf) list)
116 (tree_view: GTree.view)
118 let column_list = new GTree.column_list in
119 let tag_column = column_list#add Gobject.Data.gobject in
120 let text_column = column_list#add Gobject.Data.string in
121 let list_store = GTree.list_store column_list in
122 let text_renderer = (GTree.cell_renderer_text [], ["text", text_column]) in
123 let tag_renderer = (GTree.cell_renderer_pixbuf [], ["pixbuf", tag_column]) in
124 let text_vcolumn = GTree.view_column ~renderer:text_renderer () in
125 let tag_vcolumn = GTree.view_column ~renderer:tag_renderer () in
126 let lookup_pixbuf tag =
127 try List.assoc tag tags with Not_found -> assert false
131 tree_view#set_model (Some (list_store :> GTree.model));
132 ignore (tree_view#append_column tag_vcolumn);
133 ignore (tree_view#append_column text_vcolumn)
135 method list_store = list_store
137 method easy_append ~tag s =
138 let tree_iter = list_store#append () in
139 list_store#set ~row:tree_iter ~column:text_column s;
140 list_store#set ~row:tree_iter ~column:tag_column (lookup_pixbuf tag)
142 method easy_insert pos ~tag s =
143 let tree_iter = list_store#insert pos in
144 list_store#set ~row:tree_iter ~column:text_column s;
145 list_store#set ~row:tree_iter ~column:tag_column (lookup_pixbuf tag)
147 method easy_selection () =
150 let iter = list_store#get_iter tree_path in
151 list_store#get ~row:iter ~column:text_column)
152 tree_view#selection#get_selected_rows
157 method newUriDialog: unit -> MatitaGeneratedGui.uriChoiceDialog
158 method newInterpDialog: unit -> MatitaGeneratedGui.interpChoiceDialog
159 method newConfirmationDialog: unit -> MatitaGeneratedGui.confirmationDialog
160 method newEmptyDialog: unit -> MatitaGeneratedGui.emptyDialog
164 ~title ~message ~buttons ~callback
165 ?(message_type=`QUESTION) ?parent ?(use_markup=true)
166 ?(destroy_with_parent=true) ?(allow_grow=false) ?(allow_shrink=false)
167 ?icon ?(modal=true) ?(resizable=false) ?screen ?type_hint
168 ?(position=`CENTER_ON_PARENT) ?wm_name ?wm_class ?border_width ?width
169 ?height ?(show=true) ()
172 GWindow.message_dialog
173 ~message ~use_markup ~message_type ~buttons ?parent ~destroy_with_parent
174 ~title ~allow_grow ~allow_shrink ?icon ~modal ~resizable ?screen
175 ?type_hint ~position ?wm_name ?wm_class ?border_width ?width ?height
178 ignore(m#connect#response
179 ~callback:(fun a -> GMain.Main.quit ();callback a));
180 ignore(m#connect#close
181 ~callback:(fun _ -> GMain.Main.quit ();raise PopupClosed));
185 let popup_message_lowlevel
186 ~title ~message ?(no_separator=true) ~callback ~message_type ~buttons
187 ?parent ?(destroy_with_parent=true) ?(allow_grow=false) ?(allow_shrink=false)
188 ?icon ?(modal=true) ?(resizable=false) ?screen ?type_hint
189 ?(position=`CENTER_ON_PARENT) ?wm_name ?wm_class ?border_width ?width
190 ?height ?(show=true) ()
195 ?parent ~destroy_with_parent
196 ~title ~allow_grow ~allow_shrink ?icon ~modal ~resizable ?screen
197 ?type_hint ~position ?wm_name ?wm_class ?border_width ?width ?height
201 match message_type with
202 | `WARNING -> `DIALOG_WARNING
203 | `INFO -> `DIALOG_INFO
204 | `ERROR ->`DIALOG_ERROR
205 | `QUESTION -> `DIALOG_QUESTION
207 let image = GMisc.image ~stock ~icon_size:`DIALOG () in
208 let label = GMisc.label ~markup:message () in
209 let hbox = GPack.hbox ~spacing:10 () in
210 hbox#pack ~from:`START ~expand:true ~fill:true (image:>GObj.widget);
211 hbox#pack ~from:`START ~expand:true ~fill:true (label:>GObj.widget);
212 m#vbox#pack ~from:`START
213 ~padding:20 ~expand:true ~fill:true (hbox:>GObj.widget);
214 List.iter (fun (x, y) ->
215 m#add_button_stock x y;
217 m#set_default_response y
219 ignore(m#connect#response
220 ~callback:(fun a -> GMain.Main.quit ();callback a));
221 ignore(m#connect#close
222 ~callback:(fun _ -> GMain.Main.quit ();callback `POPUPCLOSED));
227 let ask_confirmation ~title ~message ?parent () =
233 | `CANCEL -> rc := `CANCEL
234 | `DELETE_EVENT -> rc := `CANCEL
235 | `POPUPCLOSED -> rc := `CANCEL
237 let buttons = [`YES,`YES ; `NO,`NO ; `CANCEL,`CANCEL] in
238 popup_message_lowlevel
239 ~title ~message ~message_type:`WARNING ~callback ~buttons ?parent ();
242 let report_error ~title ~message ?parent () =
243 let rc = ref false in
244 let callback _ = () in
245 let buttons = GWindow.Buttons.ok in
248 ~title ~message ~message_type:`ERROR ~callback ~buttons ?parent ()
253 let ask_text ~(gui:#gui) ?(title = "") ?(msg = "") ?(multiline = false) () =
254 let dialog = gui#newEmptyDialog () in
255 dialog#emptyDialog#set_title title;
256 dialog#emptyDialogLabel#set_label msg;
257 let result = ref None in
260 dialog#emptyDialog#destroy ();
263 ignore (dialog#emptyDialog#event#connect#delete (fun _ -> true));
264 if multiline then begin (* multiline input required: use a TextView widget *)
266 GBin.scrolled_window ~width:400 ~height:150 ~hpolicy:`NEVER
267 ~vpolicy:`ALWAYS ~packing:dialog#emptyDialogVBox#add ()
269 let view = GText.view ~wrap_mode:`CHAR ~packing:win#add () in
270 view#misc#grab_focus ();
271 connect_button dialog#emptyDialogOkButton (fun _ ->
272 return (Some (view#buffer#get_text ())))
273 end else begin (* monoline input required: use a TextEntry widget *)
274 let entry = GEdit.entry ~packing:dialog#emptyDialogVBox#add () in
275 entry#misc#grab_focus ();
276 connect_button dialog#emptyDialogOkButton (fun _ ->
277 return (Some entry#text))
279 connect_button dialog#emptyDialogCancelButton (fun _ ->return None);
280 dialog#emptyDialog#show ();
282 (match !result with None -> raise Cancel | Some r -> r)