]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaGtkMisc.ml
added comments, fixed history, added loadList to browser
[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 open Printf
27
28 open MatitaTypes
29
30 let wrap_callback f = f
31 (*
32 let wrap_callback f () =
33   try
34     f ()
35   with exn ->
36     MatitaLog.error (sprintf "Uncaught exception: %s" (Printexc.to_string exn))
37 *)
38
39 let connect_button (button: #GButton.button) callback =
40   ignore (button#connect#clicked (wrap_callback callback))
41
42 let connect_toggle_button (button: #GButton.toggle_button) callback =
43   ignore (button#connect#toggled (wrap_callback callback))
44
45 let connect_menu_item (menu_item: #GMenu.menu_item) callback =
46   ignore (menu_item#connect#activate (wrap_callback callback))
47
48 let connect_key (ev:GObj.event_ops) ?(modifiers = []) ?(stop = false) key
49   callback
50 =
51   ignore (ev#connect#key_press (fun key' ->
52     let modifiers' = GdkEvent.Key.state key' in
53     (match key' with
54     | key' when GdkEvent.Key.keyval key' = key
55           && List.for_all (fun m -> List.mem m modifiers') modifiers ->
56         callback ();
57         stop
58     | _ -> false)))
59
60 let toggle_visibility ~(win: GWindow.window) ~(check: GMenu.check_menu_item) =
61   ignore (check#connect#toggled (fun _ ->
62     if check#active then win#show () else win#misc#hide ()));
63   ignore (win#event#connect#delete (fun _ ->
64     win#misc#hide ();
65     check#set_active false;
66     true))
67
68 let toggle_win ?(check: GMenu.check_menu_item option) (win: GWindow.window) () =
69   if win#is_active then win#misc#hide () else win#show ();
70   match check with
71   | None -> ()
72   | Some check -> check#set_active (not check#active)
73
74 let add_key_binding key callback (evbox: GBin.event_box) =
75   ignore (evbox#event#connect#key_press (function
76     | key' when GdkEvent.Key.keyval key' = key ->
77         callback ();
78         false
79     | _ -> false))
80
81 class stringListModel (tree_view: GTree.view) =
82   let column_list = new GTree.column_list in
83   let text_column = column_list#add Gobject.Data.string in
84   let list_store = GTree.list_store column_list in
85   object (self)
86
87     initializer
88       let renderer = (GTree.cell_renderer_text [], ["text", text_column]) in
89       let view_column = GTree.view_column ~renderer () in
90       tree_view#set_model (Some (list_store :> GTree.model));
91       ignore (tree_view#append_column view_column)
92
93     method list_store = list_store
94
95     method easy_append s =
96       let tree_iter = list_store#append () in
97       list_store#set ~row:tree_iter ~column:text_column s
98
99     method easy_insert pos s =
100       let tree_iter = list_store#insert pos in
101       list_store#set ~row:tree_iter ~column:text_column s
102
103     method easy_selection () =
104       List.map
105         (fun tree_path ->
106           let iter = list_store#get_iter tree_path in
107           list_store#get ~row:iter ~column:text_column)
108         tree_view#selection#get_selected_rows
109
110   end
111
112 class type gui =
113   object
114     method newUriDialog:          unit -> MatitaGeneratedGui.uriChoiceDialog
115     method newInterpDialog:       unit -> MatitaGeneratedGui.interpChoiceDialog
116     method newConfirmationDialog: unit -> MatitaGeneratedGui.confirmationDialog
117     method newEmptyDialog:        unit -> MatitaGeneratedGui.emptyDialog
118   end
119
120 let ask_confirmation ~(gui:#gui) ?(cancel = true) ?(title = "") ?(msg = "") () =
121   let dialog = gui#newConfirmationDialog () in
122   dialog#confirmationDialog#set_title title;
123   dialog#confirmationDialogLabel#set_label msg;
124   let result = ref None in
125   let return r _ =
126     result := Some r;
127     dialog#confirmationDialog#destroy ();
128     GMain.Main.quit ()
129   in
130   ignore (dialog#confirmationDialog#event#connect#delete (fun _ -> true));
131   connect_button dialog#confirmationDialogOkButton (return true);
132   connect_button dialog#confirmationDialogCancelButton (return false);
133   if not cancel then dialog#confirmationDialogCancelButton#misc#hide ();
134   dialog#confirmationDialog#show ();
135   GtkThread.main ();
136   (match !result with None -> assert false | Some r -> r)
137
138 let ask_text ~(gui:#gui) ?(title = "") ?(msg = "") ?(multiline = false) () =
139   let dialog = gui#newEmptyDialog () in
140   dialog#emptyDialog#set_title title;
141   dialog#emptyDialogLabel#set_label msg;
142   let result = ref None in
143   let return r =
144     result := r;
145     dialog#emptyDialog#destroy ();
146     GMain.Main.quit ()
147   in
148   ignore (dialog#emptyDialog#event#connect#delete (fun _ -> true));
149   if multiline then begin (* multiline input required: use a TextView widget *)
150     let win =
151       GBin.scrolled_window ~width:400 ~height:150 ~hpolicy:`NEVER
152         ~vpolicy:`ALWAYS ~packing:dialog#emptyDialogVBox#add ()
153     in
154     let view = GText.view ~wrap_mode:`CHAR ~packing:win#add () in
155     view#misc#grab_focus ();
156     connect_button dialog#emptyDialogOkButton (fun _ ->
157       return (Some (view#buffer#get_text ())))
158   end else begin (* monoline input required: use a TextEntry widget *)
159     let entry = GEdit.entry ~packing:dialog#emptyDialogVBox#add () in
160     entry#misc#grab_focus ();
161     connect_button dialog#emptyDialogOkButton (fun _ ->
162       return (Some entry#text))
163   end;
164   connect_button dialog#emptyDialogCancelButton (fun _ ->return None);
165   dialog#emptyDialog#show ();
166   GtkThread.main ();
167   (match !result with None -> raise Cancel | Some r -> r)
168