]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaGtkMisc.ml
first moogle template checkin
[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 let toggle_visibility ~(win: GWindow.window) ~(check: GMenu.check_menu_item) =
27   ignore (check#connect#toggled (fun _ ->
28     if check#active then win#show () else win#misc#hide ()));
29   ignore (win#event#connect#delete (fun _ ->
30     win#misc#hide ();
31     check#set_active false;
32     true))
33
34 let toggle_win ?(check: GMenu.check_menu_item option) (win: GWindow.window) () =
35   if win#is_active then win#misc#hide () else win#show ();
36   match check with
37   | None -> ()
38   | Some check -> check#set_active (not check#active)
39
40 let add_key_binding key callback (evbox: GBin.event_box) =
41   ignore (evbox#event#connect#key_press (function
42     | key' when GdkEvent.Key.keyval key' = key ->
43         callback ();
44         false
45     | _ -> false))
46
47 class stringListModel (tree_view: GTree.view) =
48   let column_list = new GTree.column_list in
49   let text_column = column_list#add Gobject.Data.string in
50   let list_store = GTree.list_store column_list in
51   object (self)
52
53     initializer
54       let renderer = (GTree.cell_renderer_text [], ["text", text_column]) in
55       let view_column = GTree.view_column ~renderer () in
56       tree_view#set_model (Some (list_store :> GTree.model));
57       ignore (tree_view#append_column view_column)
58
59     method list_store = list_store
60
61     method easy_append s =
62       let tree_iter = list_store#append () in
63       list_store#set ~row:tree_iter ~column:text_column s
64
65     method easy_insert pos s =
66       let tree_iter = list_store#insert pos in
67       list_store#set ~row:tree_iter ~column:text_column s
68
69     method easy_selection () =
70       List.map
71         (fun tree_path ->
72           let iter = list_store#get_iter tree_path in
73           list_store#get ~row:iter ~column:text_column)
74         tree_view#selection#get_selected_rows
75
76   end
77
78 let is_var_uri s =
79   try
80     String.sub s (String.length s - 4) 4 = ".var"
81   with Invalid_argument _ -> false
82
83 let non p x = not (p x)
84
85 class type gui =
86   object
87     method newUriDialog: unit -> MatitaGeneratedGui.uriChoiceDialog
88     method newConfirmationDialog: unit -> MatitaGeneratedGui.confirmationDialog
89     method newEmptyDialog: unit -> MatitaGeneratedGui.emptyDialog
90   end
91
92 exception Cancel
93
94 let interactive_user_uri_choice
95   ~(gui:#gui) ~(selection_mode:[`SINGLE|`MULTIPLE]) ?(title = "")
96   ?(msg = "") ?(nonvars_button=false) uris
97 =
98   let nonvars_uris = lazy (List.filter (non is_var_uri) uris) in
99   if (selection_mode <> `SINGLE) &&
100     (Helm_registry.get_bool "matita.auto_disambiguation")
101   then
102     Lazy.force nonvars_uris
103   else begin
104     let dialog = gui#newUriDialog () in
105     dialog#uriChoiceTreeView#selection#set_mode
106       (selection_mode :> Gtk.Tags.selection_mode);
107     let model = new stringListModel dialog#uriChoiceTreeView in
108     let choices = ref None in
109     let nonvars = ref false in
110     dialog#uriChoiceDialog#set_title title;
111     dialog#uriChoiceLabel#set_text msg;
112     List.iter model#easy_append uris;
113     dialog#uriChoiceConstantsButton#misc#set_sensitive nonvars_button;
114     let return v =
115       choices := v;
116       dialog#uriChoiceDialog#destroy ();
117       GMain.Main.quit ()
118     in
119     ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
120     ignore (dialog#uriChoiceConstantsButton#connect#clicked (fun _ ->
121       return (Some (Lazy.force nonvars_uris))));
122     ignore (dialog#uriChoiceAutoButton#connect#clicked (fun _ ->
123       Helm_registry.set_bool "matita.auto_disambiguation" true;
124       return (Some (Lazy.force nonvars_uris))));
125     ignore (dialog#uriChoiceSelectedButton#connect#clicked (fun _ ->
126       match model#easy_selection () with
127       | [] -> ()
128       | uris -> return (Some uris)));
129     ignore (dialog#uriChoiceAbortButton#connect#clicked (fun _ -> return None));
130     dialog#uriChoiceDialog#show ();
131     GtkThread.main ();
132     (match !choices with 
133     | None -> raise Cancel
134     | Some uris -> uris)
135   end
136
137 let interactive_interp_choice ~(gui:#gui) choices =
138   (* TODO Zack implement interactive_interp_choice *)
139   MatitaTypes.warning
140     "'interactive_interp_choice' not implemented: returning 1st interpretation";
141   [0]
142
143 let ask_confirmation ~(gui:#gui) ?(title = "") ?(msg = "") () =
144   let dialog = gui#newConfirmationDialog () in
145   dialog#confirmationDialog#set_title title;
146   dialog#confirmationDialogLabel#set_label msg;
147   let result = ref None in
148   let return r _ =
149     result := Some r;
150     dialog#confirmationDialog#destroy ();
151     GMain.Main.quit ()
152   in
153   ignore (dialog#confirmationDialog#event#connect#delete (fun _ -> true));
154   ignore (dialog#confirmationDialogOkButton#connect#clicked (return true));
155   ignore (dialog#confirmationDialogCancelButton#connect#clicked (return false));
156   dialog#confirmationDialog#show ();
157   GtkThread.main ();
158   (match !result with None -> assert false | Some r -> r)
159
160 let ask_text ~(gui:#gui) ?(title = "") ?(msg = "") ?(multiline = false) () =
161   let dialog = gui#newEmptyDialog () in
162   dialog#emptyDialog#set_title title;
163   dialog#emptyDialogLabel#set_label msg;
164   let result = ref None in
165   let return r =
166     result := r;
167     dialog#emptyDialog#destroy ();
168     GMain.Main.quit ()
169   in
170   ignore (dialog#emptyDialog#event#connect#delete (fun _ -> true));
171   if multiline then begin (* multiline input required: use a TextView widget *)
172     let win =
173       GBin.scrolled_window ~width:400 ~height:150 ~hpolicy:`NEVER
174         ~vpolicy:`ALWAYS ~packing:dialog#emptyDialogVBox#add ()
175     in
176     let view = GText.view ~wrap_mode:`CHAR ~packing:win#add () in
177     view#misc#grab_focus ();
178     ignore (dialog#emptyDialogOkButton#connect#clicked (fun _ ->
179       return (Some (view#buffer#get_text ()))))
180   end else begin (* monoline input required: use a TextEntry widget *)
181     let entry = GEdit.entry ~packing:dialog#emptyDialogVBox#add () in
182     entry#misc#grab_focus ();
183     ignore (dialog#emptyDialogOkButton#connect#clicked (fun _ ->
184       return (Some entry#text)))
185   end;
186   ignore (dialog#emptyDialogCancelButton#connect#clicked (fun _ ->return None));
187   dialog#emptyDialog#show ();
188   GtkThread.main ();
189   (match !result with None -> raise Cancel | Some r -> r)
190