]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaGtkMisc.ml
fixed some errers in the save/cancel ...
[helm.git] / helm / matita / matitaGtkMisc.ml
1 (* Copyright (C) 2004-2005, 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 exception PopupClosed
27 open Printf
28
29 open MatitaTypes
30
31 let wrap_callback f = f
32 (*
33 let wrap_callback f () =
34   try
35     f ()
36   with exn ->
37     MatitaLog.error (sprintf "Uncaught exception: %s" (Printexc.to_string exn))
38 *)
39
40 let connect_button (button: #GButton.button) callback =
41   ignore (button#connect#clicked (wrap_callback callback))
42
43 let connect_toggle_button (button: #GButton.toggle_button) callback =
44   ignore (button#connect#toggled (wrap_callback callback))
45
46 let connect_menu_item (menu_item: #GMenu.menu_item) callback =
47   ignore (menu_item#connect#activate (wrap_callback callback))
48
49 let connect_key (ev:GObj.event_ops) ?(modifiers = []) ?(stop = false) key
50   callback
51 =
52   ignore (ev#connect#key_press (fun key' ->
53     let modifiers' = GdkEvent.Key.state key' in
54     (match key' with
55     | key' when GdkEvent.Key.keyval key' = key
56           && List.for_all (fun m -> List.mem m modifiers') modifiers ->
57         callback ();
58         stop
59     | _ -> false)))
60
61 let toggle_widget_visibility ~(widget: GObj.widget) 
62                               ~(check: GMenu.check_menu_item) 
63 =
64   ignore (check#connect#toggled (fun _ ->
65     if check#active then widget#misc#show () else widget#misc#hide ()))
66
67 let toggle_window_visibility ~(window: GWindow.window) 
68                               ~(check: GMenu.check_menu_item) 
69 =
70   ignore (check#connect#toggled (fun _ ->
71     if check#active then window#show () else window#misc#hide ()));
72   ignore (window#event#connect#delete (fun _ ->
73     window#misc#hide ();
74     check#set_active false;
75     true))
76
77 let toggle_win ?(check: GMenu.check_menu_item option) (win: GWindow.window) () =
78   if win#is_active then win#misc#hide () else win#show ();
79   match check with
80   | None -> ()
81   | Some check -> check#set_active (not check#active)
82
83 let toggle_callback ~callback ~(check: GMenu.check_menu_item) =
84   ignore (check#connect#toggled (fun _ -> callback check#active))
85   
86 let add_key_binding key callback (evbox: GBin.event_box) =
87   ignore (evbox#event#connect#key_press (function
88     | key' when GdkEvent.Key.keyval key' = key ->
89         callback ();
90         false
91     | _ -> false))
92
93 class stringListModel (tree_view: GTree.view) =
94   let column_list = new GTree.column_list in
95   let text_column = column_list#add Gobject.Data.string in
96   let list_store = GTree.list_store column_list in
97   let renderer = (GTree.cell_renderer_text [], ["text", text_column]) in
98   let view_column = GTree.view_column ~renderer () in
99   object (self)
100     initializer
101       tree_view#set_model (Some (list_store :> GTree.model));
102       ignore (tree_view#append_column view_column)
103
104     method list_store = list_store
105
106     method easy_append s =
107       let tree_iter = list_store#append () in
108       list_store#set ~row:tree_iter ~column:text_column s
109
110     method easy_insert pos s =
111       let tree_iter = list_store#insert pos in
112       list_store#set ~row:tree_iter ~column:text_column s
113
114     method easy_selection () =
115       List.map
116         (fun tree_path ->
117           let iter = list_store#get_iter tree_path in
118           list_store#get ~row:iter ~column:text_column)
119         tree_view#selection#get_selected_rows
120   end
121
122 class taggedStringListModel ~(tags:(string * GdkPixbuf.pixbuf) list)
123   (tree_view: GTree.view)
124 =
125   let column_list = new GTree.column_list in
126   let tag_column = column_list#add Gobject.Data.gobject in
127   let text_column = column_list#add Gobject.Data.string in
128   let list_store = GTree.list_store column_list in
129   let text_renderer = (GTree.cell_renderer_text [], ["text", text_column]) in
130   let tag_renderer = (GTree.cell_renderer_pixbuf [], ["pixbuf", tag_column]) in
131   let text_vcolumn = GTree.view_column ~renderer:text_renderer () in
132   let tag_vcolumn = GTree.view_column ~renderer:tag_renderer () in
133   let lookup_pixbuf tag =
134     try List.assoc tag tags with Not_found -> assert false
135   in
136   object (self)
137     initializer
138       tree_view#set_model (Some (list_store :> GTree.model));
139       ignore (tree_view#append_column tag_vcolumn);
140       ignore (tree_view#append_column text_vcolumn)
141
142     method list_store = list_store
143
144     method easy_append ~tag s =
145       let tree_iter = list_store#append () in
146       list_store#set ~row:tree_iter ~column:text_column s;
147       list_store#set ~row:tree_iter ~column:tag_column (lookup_pixbuf tag)
148
149     method easy_insert pos ~tag s =
150       let tree_iter = list_store#insert pos in
151       list_store#set ~row:tree_iter ~column:text_column s;
152       list_store#set ~row:tree_iter ~column:tag_column (lookup_pixbuf tag)
153
154     method easy_selection () =
155       List.map
156         (fun tree_path ->
157           let iter = list_store#get_iter tree_path in
158           list_store#get ~row:iter ~column:text_column)
159         tree_view#selection#get_selected_rows
160   end
161
162 class type gui =
163   object
164     method newUriDialog:          unit -> MatitaGeneratedGui.uriChoiceDialog
165     method newInterpDialog:       unit -> MatitaGeneratedGui.interpChoiceDialog
166     method newConfirmationDialog: unit -> MatitaGeneratedGui.confirmationDialog
167     method newEmptyDialog:        unit -> MatitaGeneratedGui.emptyDialog
168   end
169
170 let popup_message 
171   ~title ~message ~buttons ~callback
172   ?(message_type=`QUESTION) ?parent ?(use_markup=true)
173   ?(destroy_with_parent=true) ?(allow_grow=false) ?(allow_shrink=false)
174   ?icon ?(modal=true) ?(resizable=false) ?screen ?type_hint
175   ?(position=`CENTER_ON_PARENT) ?wm_name ?wm_class ?border_width ?width 
176   ?height ?(show=true) ()
177 =
178   let m = 
179     GWindow.message_dialog 
180       ~message ~use_markup ~message_type ~buttons ?parent ~destroy_with_parent 
181       ~title ~allow_grow ~allow_shrink ?icon ~modal ~resizable ?screen 
182       ?type_hint ~position ?wm_name ?wm_class ?border_width ?width ?height 
183       ~show () 
184   in 
185   ignore(m#connect#response 
186     ~callback:(fun a ->  GMain.Main.quit ();callback a));
187   ignore(m#connect#close 
188     ~callback:(fun _ -> GMain.Main.quit ();raise PopupClosed));
189   GtkThread.main ();
190   m#destroy () 
191
192 let popup_message_lowlevel
193   ~title ~message ?(no_separator=true) ~callback ~message_type ~buttons
194   ?parent  ?(destroy_with_parent=true) ?(allow_grow=false) ?(allow_shrink=false)
195   ?icon ?(modal=true) ?(resizable=false) ?screen ?type_hint
196   ?(position=`CENTER_ON_PARENT) ?wm_name ?wm_class ?border_width ?width 
197   ?height ?(show=true) ()
198 =
199   let m = 
200     GWindow.dialog 
201       ~no_separator 
202       ?parent ~destroy_with_parent 
203       ~title ~allow_grow ~allow_shrink ?icon ~modal ~resizable ?screen 
204       ?type_hint ~position ?wm_name ?wm_class ?border_width ?width ?height 
205       ~show () 
206   in
207   let stock = 
208     match message_type with
209     | `WARNING -> `DIALOG_WARNING
210     | `INFO -> `DIALOG_INFO
211     | `ERROR ->`DIALOG_ERROR 
212     | `QUESTION -> `DIALOG_QUESTION
213   in
214   let image = GMisc.image ~stock ~icon_size:`DIALOG () in
215   let label = GMisc.label ~markup:message () in
216   let hbox = GPack.hbox ~spacing:10 () in
217   hbox#pack ~from:`START ~expand:true ~fill:true (image:>GObj.widget);
218   hbox#pack ~from:`START ~expand:true ~fill:true (label:>GObj.widget);
219   m#vbox#pack ~from:`START 
220     ~padding:20 ~expand:true ~fill:true (hbox:>GObj.widget);
221   List.iter (fun (x, y) -> 
222     m#add_button_stock x y;
223     if y = `CANCEL then 
224       m#set_default_response y
225   ) buttons;
226   ignore(m#connect#response 
227     ~callback:(fun a ->  GMain.Main.quit ();callback a));
228   ignore(m#connect#close 
229     ~callback:(fun _ -> GMain.Main.quit ();callback `POPUPCLOSED));
230   GtkThread.main ();
231   m#destroy () 
232
233
234 let ask_confirmation ~title ~message ?parent () =
235   let rc = ref `YES in
236   let callback = 
237     function 
238     | `YES -> rc := `YES
239     | `NO -> rc := `NO
240     | `CANCEL -> rc := `CANCEL
241     | `DELETE_EVENT -> rc := `CANCEL
242     | `POPUPCLOSED -> rc := `CANCEL
243   in
244   let buttons = [`YES,`YES ; `NO,`NO ; `CANCEL,`CANCEL] in
245     popup_message_lowlevel 
246       ~title ~message ~message_type:`WARNING ~callback ~buttons ?parent ();
247     !rc
248
249 let report_error ~title ~message ?parent () =
250   let rc = ref false in
251   let callback _ = () in
252   let buttons = GWindow.Buttons.ok in
253   try 
254     popup_message 
255       ~title ~message ~message_type:`ERROR ~callback ~buttons ?parent ()
256   with
257   | PopupClosed -> ()
258
259
260 let ask_text ~(gui:#gui) ?(title = "") ?(msg = "") ?(multiline = false) () =
261   let dialog = gui#newEmptyDialog () in
262   dialog#emptyDialog#set_title title;
263   dialog#emptyDialogLabel#set_label msg;
264   let result = ref None in
265   let return r =
266     result := r;
267     dialog#emptyDialog#destroy ();
268     GMain.Main.quit ()
269   in
270   ignore (dialog#emptyDialog#event#connect#delete (fun _ -> true));
271   if multiline then begin (* multiline input required: use a TextView widget *)
272     let win =
273       GBin.scrolled_window ~width:400 ~height:150 ~hpolicy:`NEVER
274         ~vpolicy:`ALWAYS ~packing:dialog#emptyDialogVBox#add ()
275     in
276     let view = GText.view ~wrap_mode:`CHAR ~packing:win#add () in
277     view#misc#grab_focus ();
278     connect_button dialog#emptyDialogOkButton (fun _ ->
279       return (Some (view#buffer#get_text ())))
280   end else begin (* monoline input required: use a TextEntry widget *)
281     let entry = GEdit.entry ~packing:dialog#emptyDialogVBox#add () in
282     entry#misc#grab_focus ();
283     connect_button dialog#emptyDialogOkButton (fun _ ->
284       return (Some entry#text))
285   end;
286   connect_button dialog#emptyDialogCancelButton (fun _ ->return None);
287   dialog#emptyDialog#show ();
288   GtkThread.main ();
289   (match !result with None -> raise Cancel | Some r -> r)
290