nautilus-3.6.3/eel/eel-stock-dialogs.c

No issues found

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