1 /* eel-stock-dialogs.c: Various standard dialogs for Eel.
2 *
3 * Copyright (C) 2000 Eazel, Inc.
4 *
5 * The Gnome Library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * The Gnome Library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with the Gnome Library; see the file COPYING.LIB. If not,
17 * see <http://www.gnu.org/licenses/>.
18 *
19 * Authors: Darin Adler <darin@eazel.com>
20 */
21
22 #include <config.h>
23 #include "eel-stock-dialogs.h"
24
25 #include "eel-glib-extensions.h"
26 #include "eel-gtk-extensions.h"
27
28 #include <glib/gi18n-lib.h>
29 #include <gtk/gtk.h>
30
31 #define TIMED_WAIT_STANDARD_DURATION 2000
32 #define TIMED_WAIT_MIN_TIME_UP 3000
33
34 #define TIMED_WAIT_MINIMUM_DIALOG_WIDTH 300
35
36 #define RESPONSE_DETAILS 1000
37
38 typedef struct
39 {
40 EelCancelCallback cancel_callback;
41 gpointer callback_data;
42
43 /* Parameters for creation of the window. */
44 char *wait_message;
45 GtkWindow *parent_window;
46
47 /* Timer to determine when we need to create the window. */
48 guint timeout_handler_id;
49
50 /* Window, once it's created. */
51 GtkDialog *dialog;
52
53 /* system time (microseconds) when dialog was created */
54 gint64 dialog_creation_time;
55 } TimedWait;
56
57 static GHashTable *timed_wait_hash_table;
58
59 static void timed_wait_dialog_destroy_callback (GtkWidget *object,
60 gpointer callback_data);
61
62 static guint
timed_wait_hash(gconstpointer value)63 timed_wait_hash (gconstpointer value)
64 {
65 const TimedWait *wait;
66
67 wait = value;
68
69 return GPOINTER_TO_UINT (wait->cancel_callback)
70 ^ GPOINTER_TO_UINT (wait->callback_data);
71 }
72
73 static gboolean
timed_wait_hash_equal(gconstpointer value1,gconstpointer value2)74 timed_wait_hash_equal (gconstpointer value1,
75 gconstpointer value2)
76 {
77 const TimedWait *wait1, *wait2;
78
79 wait1 = value1;
80 wait2 = value2;
81
82 return wait1->cancel_callback == wait2->cancel_callback
83 && wait1->callback_data == wait2->callback_data;
84 }
85
86 static void
timed_wait_delayed_close_destroy_dialog_callback(GtkWidget * object,gpointer callback_data)87 timed_wait_delayed_close_destroy_dialog_callback (GtkWidget *object,
88 gpointer callback_data)
89 {
90 g_source_remove (GPOINTER_TO_UINT (callback_data));
91 }
92
93 static gboolean
timed_wait_delayed_close_timeout_callback(gpointer callback_data)94 timed_wait_delayed_close_timeout_callback (gpointer callback_data)
95 {
96 guint handler_id;
97
98 handler_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (callback_data),
99 "eel-stock-dialogs/delayed_close_handler_timeout_id"));
100
101 g_signal_handlers_disconnect_by_func (G_OBJECT (callback_data),
102 G_CALLBACK (timed_wait_delayed_close_destroy_dialog_callback),
103 GUINT_TO_POINTER (handler_id));
104
105 gtk_widget_destroy (GTK_WIDGET (callback_data));
106
107 return FALSE;
108 }
109
110 static void
timed_wait_free(TimedWait * wait)111 timed_wait_free (TimedWait *wait)
112 {
113 guint delayed_close_handler_id;
114 guint64 time_up;
115
116 g_assert (g_hash_table_lookup (timed_wait_hash_table, wait) != NULL);
117
118 g_hash_table_remove (timed_wait_hash_table, wait);
119
120 g_free (wait->wait_message);
121 if (wait->parent_window != NULL)
122 {
123 g_object_unref (wait->parent_window);
124 }
125 if (wait->timeout_handler_id != 0)
126 {
127 g_source_remove (wait->timeout_handler_id);
128 }
129 if (wait->dialog != NULL)
130 {
131 /* Make sure to detach from the "destroy" signal, or we'll
132 * double-free.
133 */
134 g_signal_handlers_disconnect_by_func (G_OBJECT (wait->dialog),
135 G_CALLBACK (timed_wait_dialog_destroy_callback),
136 wait);
137
138 /* compute time up in milliseconds */
139 time_up = (g_get_monotonic_time () - wait->dialog_creation_time) / 1000;
140
141 if (time_up < TIMED_WAIT_MIN_TIME_UP)
142 {
143 delayed_close_handler_id = g_timeout_add (TIMED_WAIT_MIN_TIME_UP - time_up,
144 timed_wait_delayed_close_timeout_callback,
145 wait->dialog);
146 g_object_set_data (G_OBJECT (wait->dialog),
147 "eel-stock-dialogs/delayed_close_handler_timeout_id",
148 GUINT_TO_POINTER (delayed_close_handler_id));
149 g_signal_connect (wait->dialog, "destroy",
150 G_CALLBACK (timed_wait_delayed_close_destroy_dialog_callback),
151 GUINT_TO_POINTER (delayed_close_handler_id));
152 }
153 else
154 {
155 gtk_widget_destroy (GTK_WIDGET (wait->dialog));
156 }
157 }
158
159 /* And the wait object itself. */
160 g_free (wait);
161 }
162
163 static void
timed_wait_dialog_destroy_callback(GtkWidget * object,gpointer callback_data)164 timed_wait_dialog_destroy_callback (GtkWidget *object,
165 gpointer callback_data)
166 {
167 TimedWait *wait;
168
169 wait = callback_data;
170
171 g_assert (GTK_DIALOG (object) == wait->dialog);
172
173 wait->dialog = NULL;
174
175 /* When there's no cancel_callback, the originator will/must
176 * call eel_timed_wait_stop which will call timed_wait_free.
177 */
178
179 if (wait->cancel_callback != NULL)
180 {
181 (*wait->cancel_callback)(wait->callback_data);
182 timed_wait_free (wait);
183 }
184 }
185
186 static void
trash_dialog_response_callback(GtkDialog * dialog,int response_id,TimedWait * wait)187 trash_dialog_response_callback (GtkDialog *dialog,
188 int response_id,
189 TimedWait *wait)
190 {
191 gtk_widget_destroy (GTK_WIDGET (dialog));
192 }
193
194 static gboolean
timed_wait_callback(gpointer callback_data)195 timed_wait_callback (gpointer callback_data)
196 {
197 TimedWait *wait;
198 GtkDialog *dialog;
199 const char *button;
200
201 wait = callback_data;
202
203 /* Put up the timed wait window. */
204 button = wait->cancel_callback != NULL ? _("_Cancel") : ("_OK");
205 dialog = GTK_DIALOG (gtk_message_dialog_new (wait->parent_window,
206 0,
207 GTK_MESSAGE_INFO,
208 GTK_BUTTONS_NONE,
209 NULL));
210
211 g_object_set (dialog,
212 "text", wait->wait_message,
213 "secondary-text", _("You can stop this operation by clicking cancel."),
214 NULL);
215
216 gtk_dialog_add_button (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
217 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
218
219 /* The contents are often very small, causing tiny little
220 * dialogs with their titles clipped if you just let gtk
221 * sizing do its thing. This enforces a minimum width to
222 * make it more likely that the title won't be clipped.
223 */
224 gtk_window_set_default_size (GTK_WINDOW (dialog),
225 TIMED_WAIT_MINIMUM_DIALOG_WIDTH,
226 -1);
227 wait->dialog_creation_time = g_get_monotonic_time ();
228 gtk_widget_show (GTK_WIDGET (dialog));
229
230 /* FIXME bugzilla.eazel.com 2441:
231 * Could parent here, but it's complicated because we
232 * don't want this window to go away just because the parent
233 * would go away first.
234 */
235
236 /* Make the dialog cancel the timed wait when it goes away.
237 * Connect to "destroy" instead of "response" since we want
238 * to be called no matter how the dialog goes away.
239 */
240 g_signal_connect (dialog, "destroy",
241 G_CALLBACK (timed_wait_dialog_destroy_callback),
242 wait);
243 g_signal_connect (dialog, "response",
244 G_CALLBACK (trash_dialog_response_callback),
245 wait);
246
247 wait->timeout_handler_id = 0;
248 wait->dialog = dialog;
249
250 return FALSE;
251 }
252
253 void
eel_timed_wait_start_with_duration(int duration,EelCancelCallback cancel_callback,gpointer callback_data,const char * wait_message,GtkWindow * parent_window)254 eel_timed_wait_start_with_duration (int duration,
255 EelCancelCallback cancel_callback,
256 gpointer callback_data,
257 const char *wait_message,
258 GtkWindow *parent_window)
259 {
260 TimedWait *wait;
261
262 g_return_if_fail (callback_data != NULL);
263 g_return_if_fail (wait_message != NULL);
264 g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
265
266 /* Create the timed wait record. */
267 wait = g_new0 (TimedWait, 1);
268 wait->wait_message = g_strdup (wait_message);
269 wait->cancel_callback = cancel_callback;
270 wait->callback_data = callback_data;
271 wait->parent_window = parent_window;
272
273 if (parent_window != NULL)
274 {
275 g_object_ref (parent_window);
276 }
277
278 /* Start the timer. */
279 wait->timeout_handler_id = g_timeout_add (duration, timed_wait_callback, wait);
280
281 /* Put in the hash table so we can find it later. */
282 if (timed_wait_hash_table == NULL)
283 {
284 timed_wait_hash_table = g_hash_table_new (timed_wait_hash, timed_wait_hash_equal);
285 }
286 g_assert (g_hash_table_lookup (timed_wait_hash_table, wait) == NULL);
287 g_hash_table_insert (timed_wait_hash_table, wait, wait);
288 g_assert (g_hash_table_lookup (timed_wait_hash_table, wait) == wait);
289 }
290
291 void
eel_timed_wait_start(EelCancelCallback cancel_callback,gpointer callback_data,const char * wait_message,GtkWindow * parent_window)292 eel_timed_wait_start (EelCancelCallback cancel_callback,
293 gpointer callback_data,
294 const char *wait_message,
295 GtkWindow *parent_window)
296 {
297 eel_timed_wait_start_with_duration
298 (TIMED_WAIT_STANDARD_DURATION,
299 cancel_callback, callback_data,
300 wait_message, parent_window);
301 }
302
303 void
eel_timed_wait_stop(EelCancelCallback cancel_callback,gpointer callback_data)304 eel_timed_wait_stop (EelCancelCallback cancel_callback,
305 gpointer callback_data)
306 {
307 TimedWait key;
308 TimedWait *wait;
309
310 g_return_if_fail (callback_data != NULL);
311
312 key.cancel_callback = cancel_callback;
313 key.callback_data = callback_data;
314 wait = g_hash_table_lookup (timed_wait_hash_table, &key);
315
316 g_return_if_fail (wait != NULL);
317
318 timed_wait_free (wait);
319 }
320
321 int
eel_run_simple_dialog(GtkWidget * parent,gboolean ignore_close_box,GtkMessageType message_type,const char * primary_text,const char * secondary_text,...)322 eel_run_simple_dialog (GtkWidget *parent,
323 gboolean ignore_close_box,
324 GtkMessageType message_type,
325 const char *primary_text,
326 const char *secondary_text,
327 ...)
328 {
329 va_list button_title_args;
330 const char *button_title;
331 GtkWidget *dialog;
332 GtkWidget *top_widget, *chosen_parent;
333 int result;
334 int response_id;
335
336 /* Parent it if asked to. */
337 chosen_parent = NULL;
338 if (parent != NULL)
339 {
340 top_widget = gtk_widget_get_toplevel (parent);
341 if (GTK_IS_WINDOW (top_widget))
342 {
343 chosen_parent = top_widget;
344 }
345 }
346
347 /* Create the dialog. */
348 dialog = gtk_message_dialog_new (GTK_WINDOW (chosen_parent),
349 0,
350 message_type,
351 GTK_BUTTONS_NONE,
352 NULL);
353
354 g_object_set (dialog,
355 "text", primary_text,
356 "secondary-text", secondary_text,
357 NULL);
358
359 va_start (button_title_args, secondary_text);
360 response_id = 0;
361 while (1)
362 {
363 button_title = va_arg (button_title_args, const char *);
364 if (button_title == NULL)
365 {
366 break;
367 }
368 gtk_dialog_add_button (GTK_DIALOG (dialog), button_title, response_id);
369 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
370 response_id++;
371 }
372 va_end (button_title_args);
373
374 /* Run it. */
375 gtk_widget_show (dialog);
376 result = gtk_dialog_run (GTK_DIALOG (dialog));
377 while ((result == GTK_RESPONSE_NONE || result == GTK_RESPONSE_DELETE_EVENT) && ignore_close_box)
378 {
379 gtk_widget_show (GTK_WIDGET (dialog));
380 result = gtk_dialog_run (GTK_DIALOG (dialog));
381 }
382 gtk_widget_destroy (dialog);
383
384 return result;
385 }
386
387 static GtkDialog *
create_message_dialog(const char * primary_text,const char * secondary_text,GtkMessageType type,GtkButtonsType buttons_type,GtkWindow * parent)388 create_message_dialog (const char *primary_text,
389 const char *secondary_text,
390 GtkMessageType type,
391 GtkButtonsType buttons_type,
392 GtkWindow *parent)
393 {
394 GtkWidget *dialog;
395
396 dialog = gtk_message_dialog_new (parent,
397 0,
398 type,
399 buttons_type,
400 NULL);
401 if (parent)
402 {
403 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
404 }
405
406 g_object_set (dialog,
407 "text", primary_text,
408 "secondary-text", secondary_text,
409 NULL);
410
411 return GTK_DIALOG (dialog);
412 }
413
414 static GtkDialog *
show_message_dialog(const char * primary_text,const char * secondary_text,GtkMessageType type,GtkButtonsType buttons_type,GtkWindow * parent)415 show_message_dialog (const char *primary_text,
416 const char *secondary_text,
417 GtkMessageType type,
418 GtkButtonsType buttons_type,
419 GtkWindow *parent)
420 {
421 GtkDialog *dialog;
422
423 dialog = create_message_dialog (primary_text, secondary_text, type,
424 buttons_type, parent);
425 gtk_widget_show (GTK_WIDGET (dialog));
426
427 g_signal_connect (dialog, "response",
428 G_CALLBACK (gtk_widget_destroy), NULL);
429
430 return dialog;
431 }
432
433 static GtkDialog *
show_ok_dialog(const char * primary_text,const char * secondary_text,GtkMessageType type,GtkWindow * parent)434 show_ok_dialog (const char *primary_text,
435 const char *secondary_text,
436 GtkMessageType type,
437 GtkWindow *parent)
438 {
439 GtkDialog *dialog;
440
441 dialog = show_message_dialog (primary_text, secondary_text, type,
442 GTK_BUTTONS_OK, parent);
443 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
444
445 return dialog;
446 }
447
448 GtkDialog *
eel_show_info_dialog(const char * primary_text,const char * secondary_text,GtkWindow * parent)449 eel_show_info_dialog (const char *primary_text,
450 const char *secondary_text,
451 GtkWindow *parent)
452 {
453 return show_ok_dialog (primary_text,
454 secondary_text,
455 GTK_MESSAGE_INFO, parent);
456 }
457
458 GtkDialog *
eel_show_warning_dialog(const char * primary_text,const char * secondary_text,GtkWindow * parent)459 eel_show_warning_dialog (const char *primary_text,
460 const char *secondary_text,
461 GtkWindow *parent)
462 {
463 return show_ok_dialog (primary_text,
464 secondary_text,
465 GTK_MESSAGE_WARNING, parent);
466 }
467
468
469 GtkDialog *
eel_show_error_dialog(const char * primary_text,const char * secondary_text,GtkWindow * parent)470 eel_show_error_dialog (const char *primary_text,
471 const char *secondary_text,
472 GtkWindow *parent)
473 {
474 return show_ok_dialog (primary_text,
475 secondary_text,
476 GTK_MESSAGE_ERROR, parent);
477 }
478
479 /**
480 * eel_show_yes_no_dialog:
481 *
482 * Create and show a dialog asking a question with two choices.
483 * The caller needs to set up any necessary callbacks
484 * for the buttons. Use eel_create_question_dialog instead
485 * if any visual changes need to be made, to avoid flashiness.
486 * @question: The text of the question.
487 * @yes_label: The label of the "yes" button.
488 * @no_label: The label of the "no" button.
489 * @parent: The parent window for this dialog.
490 */
491 GtkDialog *
eel_show_yes_no_dialog(const char * primary_text,const char * secondary_text,const char * yes_label,const char * no_label,GtkWindow * parent)492 eel_show_yes_no_dialog (const char *primary_text,
493 const char *secondary_text,
494 const char *yes_label,
495 const char *no_label,
496 GtkWindow *parent)
497 {
498 GtkDialog *dialog = NULL;
499 dialog = eel_create_question_dialog (primary_text,
500 secondary_text,
501 no_label, GTK_RESPONSE_CANCEL,
502 yes_label, GTK_RESPONSE_YES,
503 GTK_WINDOW (parent));
504 gtk_widget_show (GTK_WIDGET (dialog));
505 return dialog;
506 }
507
508 /**
509 * eel_create_question_dialog:
510 *
511 * Create a dialog asking a question with at least two choices.
512 * The caller needs to set up any necessary callbacks
513 * for the buttons. The dialog is not yet shown, so that the
514 * caller can add additional buttons or make other visual changes
515 * without causing flashiness.
516 * @question: The text of the question.
517 * @answer_0: The label of the leftmost button (index 0)
518 * @answer_1: The label of the 2nd-to-leftmost button (index 1)
519 * @parent: The parent window for this dialog.
520 */
521 GtkDialog *
eel_create_question_dialog(const char * primary_text,const char * secondary_text,const char * answer_1,int response_1,const char * answer_2,int response_2,GtkWindow * parent)522 eel_create_question_dialog (const char *primary_text,
523 const char *secondary_text,
524 const char *answer_1,
525 int response_1,
526 const char *answer_2,
527 int response_2,
528 GtkWindow *parent)
529 {
530 GtkDialog *dialog;
531
532 dialog = create_message_dialog (primary_text,
533 secondary_text,
534 GTK_MESSAGE_QUESTION,
535 GTK_BUTTONS_NONE,
536 parent);
537 gtk_dialog_add_buttons (dialog, answer_1, response_1, answer_2, response_2, NULL);
538 return dialog;
539 }
540