2 * Copyright 2008 Department of Mathematical Sciences, New Mexico State University
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * DEPARTMENT OF MATHEMATICAL SCIENCES OR NEW MEXICO STATE UNIVERSITY BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * The shared error dialog and the label for the message.
28 static GtkWidget *errd;
29 static GtkWidget *errmsg;
32 * The shared question dialog, the label for the question, and the
33 * value representing the answer.
35 static GtkWidget *questd;
36 static GtkWidget *question;
37 static GtkWidget *yes;
41 guiutil_show_dialog_centered(GtkWidget *dialog, GtkWidget *parent)
43 if (GTK_WINDOW(parent) != gtk_window_get_transient_for(GTK_WINDOW(dialog)))
44 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
45 gtk_widget_show(dialog);
49 guiutil_error_message(GtkWidget *parent, gchar *text)
51 GtkWidget *ok, *hbox, *image;
55 errd = gtk_dialog_new();
56 (void) g_signal_connect(G_OBJECT(errd), "delete_event",
57 G_CALLBACK(gtk_widget_hide), 0);
59 hbox = gtk_hbox_new(FALSE, 0);
62 * Create the error icon.
64 if (gtk_stock_lookup(GTK_STOCK_DIALOG_ERROR, &item)) {
66 * Set the dialog title.
68 gtk_window_set_title(GTK_WINDOW(errd), item.label);
69 image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_ERROR,
70 GTK_ICON_SIZE_DIALOG);
71 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 2);
74 * Probably not necessary, but use some default icon here.
78 errmsg = gtk_label_new(text);
79 gtk_box_pack_start(GTK_BOX(hbox), errmsg, TRUE, TRUE, 2);
80 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(errd)->vbox), hbox);
82 ok = gtk_dialog_add_button(GTK_DIALOG(errd), GTK_STOCK_CLOSE,
84 (void) g_signal_connect_object(G_OBJECT(ok), "clicked",
85 G_CALLBACK(gtk_widget_hide),
89 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(errd)->action_area), ok);
91 gtk_widget_show_all(errd);
93 gtk_window_set_modal(GTK_WINDOW(errd), TRUE);
95 gtk_label_set_text(GTK_LABEL(errmsg), text);
98 * Center the dialog and display it.
100 guiutil_show_dialog_centered(errd, parent);
109 guiutil_yes_or_no(GtkWidget *parent, gchar *text, gboolean default_answer)
111 GtkWidget *hbox, *image;
117 questd = gtk_dialog_new();
118 gtk_window_set_resizable(GTK_WINDOW(questd), TRUE);
120 (void) g_signal_connect(G_OBJECT(questd), "delete_event",
121 G_CALLBACK(gtk_widget_hide), 0);
123 hbox = gtk_hbox_new(FALSE, 0);
126 * Create the question icon.
128 if (gtk_stock_lookup(GTK_STOCK_DIALOG_QUESTION, &item)) {
130 * Set the dialog title.
132 gtk_window_set_title(GTK_WINDOW(questd), item.label);
133 image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
134 GTK_ICON_SIZE_DIALOG);
135 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 2);
138 * Probably not necessary, but use some default icon here.
142 question = gtk_label_new(text);
144 gtk_box_pack_start(GTK_BOX(hbox), question, TRUE, TRUE, 2);
145 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(questd)->vbox), hbox);
148 * Make sure the buttons are evenly distributed in the button box.
150 gtk_button_box_set_layout(GTK_BUTTON_BOX(GTK_DIALOG(questd)->action_area), GTK_BUTTONBOX_SPREAD);
152 gtk_dialog_add_buttons(GTK_DIALOG(questd),
153 GTK_STOCK_YES, GTK_RESPONSE_ACCEPT,
154 GTK_STOCK_NO, GTK_RESPONSE_CANCEL, NULL);
157 * Get the two children buttons out now so focus can be set on either
160 kids = gtk_container_get_children(GTK_CONTAINER(GTK_DIALOG(questd)->action_area));
161 no = GTK_WIDGET(g_list_nth_data(kids, 0));
162 yes = GTK_WIDGET(g_list_nth_data(kids, 1));
165 gtk_widget_show_all(questd);
167 gtk_window_set_modal(GTK_WINDOW(questd), TRUE);
169 gtk_label_set_text(GTK_LABEL(question), text);
172 * Force the dialog to reset to its minimum size.
174 if (questd->window != NULL)
175 gtk_window_resize(GTK_WINDOW(questd), 1, 1);
178 * Center the dialog and display it.
180 guiutil_show_dialog_centered(questd, parent);
183 * Force the default answer button to have the focus.
186 gtk_widget_grab_focus(yes);
188 gtk_widget_grab_focus(no);
190 ans = gtk_dialog_run(GTK_DIALOG(questd));
192 gtk_widget_hide(questd);
194 return (ans == GTK_RESPONSE_ACCEPT) ? TRUE : FALSE;
198 guiutil_util_set_tooltip(GtkWidget *w, gchar *text)
200 #if GTK_MAJOR_VERSION >= 2 && GTK_MINOR_VERSION >= 12
201 gtk_widget_set_tooltip_text(w, text);
205 tt = gtk_tooltips_new();
206 gtk_tooltips_set_tip(tt, w, text, 0);
210 static GdkCursor *watch_cursor;
213 guiutil_busy_cursor(GtkWidget *w, gboolean on)
215 if (watch_cursor == 0)
216 watch_cursor = gdk_cursor_new(GDK_WATCH);
219 gdk_window_set_cursor(w->window, watch_cursor);
221 gdk_window_set_cursor(w->window, 0);
225 guiutil_cursor_cleanup()
227 if (watch_cursor != 0)
228 gdk_cursor_unref(watch_cursor);