-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguiutil.c
234 lines (195 loc) · 7.15 KB
/
guiutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2008 Department of Mathematical Sciences, New Mexico State University
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* DEPARTMENT OF MATHEMATICAL SCIENCES OR NEW MEXICO STATE UNIVERSITY BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "gbdfed.h"
/*
* The shared error dialog and the label for the message.
*/
static GtkWidget *errd;
static GtkWidget *errmsg;
/*
* The shared question dialog, the label for the question, and the
* value representing the answer.
*/
static GtkWidget *questd;
static GtkWidget *question;
static GtkWidget *yes;
static GtkWidget *no;
void
guiutil_show_dialog_centered(GtkWidget *dialog, GtkWidget *parent)
{
if (GTK_WINDOW(parent) != gtk_window_get_transient_for(GTK_WINDOW(dialog)))
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
gtk_widget_show(dialog);
}
void
guiutil_error_message(GtkWidget *parent, gchar *text)
{
GtkWidget *ok, *hbox, *image;
GtkStockItem item;
if (errd == 0) {
errd = gtk_dialog_new();
(void) g_signal_connect(G_OBJECT(errd), "delete_event",
G_CALLBACK(gtk_widget_hide), 0);
hbox = gtk_hbox_new(FALSE, 0);
/*
* Create the error icon.
*/
if (gtk_stock_lookup(GTK_STOCK_DIALOG_ERROR, &item)) {
/*
* Set the dialog title.
*/
gtk_window_set_title(GTK_WINDOW(errd), item.label);
image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_ERROR,
GTK_ICON_SIZE_DIALOG);
gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 2);
} else {
/*
* Probably not necessary, but use some default icon here.
*/
}
errmsg = gtk_label_new(text);
gtk_box_pack_start(GTK_BOX(hbox), errmsg, TRUE, TRUE, 2);
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(errd))),
hbox);
ok = gtk_dialog_add_button(GTK_DIALOG(errd), GTK_STOCK_CLOSE,
GTK_RESPONSE_CLOSE);
(void) g_signal_connect_object(G_OBJECT(ok), "clicked",
G_CALLBACK(gtk_widget_hide),
(gpointer) errd,
G_CONNECT_SWAPPED);
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(errd))),
ok);
gtk_widget_show_all(errd);
gtk_window_set_modal(GTK_WINDOW(errd), TRUE);
} else
gtk_label_set_text(GTK_LABEL(errmsg), text);
/*
* Center the dialog and display it.
*/
guiutil_show_dialog_centered(errd, parent);
/*
* Ring the bell.
*/
gdk_beep();
}
gboolean
guiutil_yes_or_no(GtkWidget *parent, gchar *text, gboolean default_answer)
{
GtkWidget *hbox, *image;
GList *kids;
gint ans;
GtkStockItem item;
if (questd == 0) {
questd = gtk_dialog_new();
gtk_window_set_resizable(GTK_WINDOW(questd), TRUE);
(void) g_signal_connect(G_OBJECT(questd), "delete_event",
G_CALLBACK(gtk_widget_hide), 0);
hbox = gtk_hbox_new(FALSE, 0);
/*
* Create the question icon.
*/
if (gtk_stock_lookup(GTK_STOCK_DIALOG_QUESTION, &item)) {
/*
* Set the dialog title.
*/
gtk_window_set_title(GTK_WINDOW(questd), item.label);
image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
GTK_ICON_SIZE_DIALOG);
gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 2);
} else {
/*
* Probably not necessary, but use some default icon here.
*/
}
question = gtk_label_new(text);
gtk_box_pack_start(GTK_BOX(hbox), question, TRUE, TRUE, 2);
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(questd))),
hbox);
/*
* Make sure the buttons are evenly distributed in the button box.
*/
gtk_button_box_set_layout(GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(questd))),
GTK_BUTTONBOX_SPREAD);
gtk_dialog_add_buttons(GTK_DIALOG(questd),
GTK_STOCK_YES, GTK_RESPONSE_ACCEPT,
GTK_STOCK_NO, GTK_RESPONSE_CANCEL, NULL);
/*
* Get the two children buttons out now so focus can be set on either
* when needed.
*/
kids = gtk_container_get_children(GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(questd))));
no = GTK_WIDGET(g_list_nth_data(kids, 0));
yes = GTK_WIDGET(g_list_nth_data(kids, 1));
g_list_free(kids);
gtk_widget_show_all(questd);
gtk_window_set_modal(GTK_WINDOW(questd), TRUE);
} else
gtk_label_set_text(GTK_LABEL(question), text);
/*
* Force the dialog to reset to its minimum size.
*/
if (gtk_widget_get_window(questd) != NULL)
gtk_window_resize(GTK_WINDOW(questd), 1, 1);
/*
* Center the dialog and display it.
*/
guiutil_show_dialog_centered(questd, parent);
/*
* Force the default answer button to have the focus.
*/
if (default_answer)
gtk_widget_grab_focus(yes);
else
gtk_widget_grab_focus(no);
ans = gtk_dialog_run(GTK_DIALOG(questd));
gtk_widget_hide(questd);
return (ans == GTK_RESPONSE_ACCEPT) ? TRUE : FALSE;
}
void
guiutil_util_set_tooltip(GtkWidget *w, gchar *text)
{
#if GTK_MAJOR_VERSION >= 2 && GTK_MINOR_VERSION >= 12
gtk_widget_set_tooltip_text(w, text);
#else
GtkTooltips *tt;
tt = gtk_tooltips_new();
gtk_tooltips_set_tip(tt, w, text, 0);
#endif
}
static GdkCursor *watch_cursor;
void
guiutil_busy_cursor(GtkWidget *w, gboolean on)
{
if (watch_cursor == 0)
watch_cursor = gdk_cursor_new(GDK_WATCH);
if (on)
gdk_window_set_cursor(gtk_widget_get_window(w), watch_cursor);
else
gdk_window_set_cursor(gtk_widget_get_window(w), 0);
}
void
guiutil_cursor_cleanup()
{
if (watch_cursor != 0)
gdk_cursor_unref(watch_cursor);
watch_cursor = 0;
}