1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkbutton.h"
28 #include "gtkdialog.h"
30 #include "gtkhseparator.h"
32 #include "gtksignal.h"
33 #include "gdkkeysyms.h"
36 static void gtk_dialog_class_init (GtkDialogClass *klass);
37 static void gtk_dialog_init (GtkDialog *dialog);
38 static gint gtk_dialog_key_press (GtkWidget *widget,
41 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
42 const gchar *first_button_text,
45 static gint gtk_dialog_delete_event_handler (GtkWidget *widget,
55 static gpointer parent_class;
56 static guint dialog_signals[LAST_SIGNAL];
59 gtk_dialog_get_type (void)
61 static GtkType dialog_type = 0;
65 static const GtkTypeInfo dialog_info =
69 sizeof (GtkDialogClass),
70 (GtkClassInitFunc) gtk_dialog_class_init,
71 (GtkObjectInitFunc) gtk_dialog_init,
72 /* reserved_1 */ NULL,
73 /* reserved_2 */ NULL,
74 (GtkClassInitFunc) NULL,
77 dialog_type = gtk_type_unique (GTK_TYPE_WINDOW, &dialog_info);
84 gtk_dialog_class_init (GtkDialogClass *class)
86 GtkObjectClass *object_class;
87 GtkWidgetClass *widget_class;
89 object_class = (GtkObjectClass*) class;
90 widget_class = (GtkWidgetClass*) class;
92 parent_class = g_type_class_peek_parent (class);
94 widget_class->key_press_event = gtk_dialog_key_press;
96 dialog_signals[RESPONSE] =
97 gtk_signal_new ("response",
99 GTK_CLASS_TYPE (object_class),
100 GTK_SIGNAL_OFFSET (GtkDialogClass, response),
101 gtk_marshal_NONE__INT,
107 gtk_dialog_init (GtkDialog *dialog)
109 GtkWidget *separator;
111 /* To avoid breaking old code that prevents destroy on delete event
112 * by connecting a handler, we have to have the FIRST signal
113 * connection on the dialog.
115 gtk_signal_connect (GTK_OBJECT (dialog),
117 GTK_SIGNAL_FUNC (gtk_dialog_delete_event_handler),
120 dialog->vbox = gtk_vbox_new (FALSE, 0);
122 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), 2);
124 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
125 gtk_widget_show (dialog->vbox);
127 dialog->action_area = gtk_hbutton_box_new ();
129 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
132 gtk_button_box_set_spacing (GTK_BUTTON_BOX (dialog->action_area), 5);
134 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 5);
135 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
137 gtk_widget_show (dialog->action_area);
139 separator = gtk_hseparator_new ();
140 gtk_box_pack_end (GTK_BOX (dialog->vbox), separator, FALSE, TRUE, 0);
141 gtk_widget_show (separator);
143 gtk_window_set_type_hint (GTK_WINDOW (dialog),
144 GDK_WINDOW_TYPE_HINT_DIALOG);
148 gtk_dialog_delete_event_handler (GtkWidget *widget,
152 /* emit response signal */
153 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
155 /* Do the destroy by default */
160 gtk_dialog_key_press (GtkWidget *widget,
165 event.type = GDK_DELETE;
166 event.window = widget->window;
167 event.send_event = TRUE;
169 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, key))
172 if (key->keyval != GDK_Escape)
175 /* Synthesize delete_event on key press. */
176 g_object_ref (G_OBJECT (event.window));
178 gtk_main_do_event ((GdkEvent*)&event);
180 g_object_unref (G_OBJECT (event.window));
186 gtk_dialog_new (void)
188 return GTK_WIDGET (gtk_type_new (GTK_TYPE_DIALOG));
192 gtk_dialog_new_empty (const gchar *title,
194 GtkDialogFlags flags)
198 dialog = GTK_DIALOG (g_object_new (GTK_TYPE_DIALOG, NULL));
201 gtk_window_set_title (GTK_WINDOW (dialog), title);
204 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
206 if (flags & GTK_DIALOG_MODAL)
207 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
209 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
210 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
212 return GTK_WIDGET (dialog);
216 * gtk_dialog_new_with_buttons:
217 * @title: Title of the dialog, or NULL
218 * @parent: Transient parent of the dialog, or NULL
219 * @flags: from #GtkDialogFlags
220 * @first_button_text: stock ID or text to go in first button, or NULL
221 * @Varargs: response ID for first button, then additional buttons, ending with NULL
223 * Creates a new #GtkDialog with title @title (or NULL for the default
224 * title; see gtk_window_set_title()) and transient parent @parent (or
225 * NULL for none; see gtk_window_set_transient_for()). The @flags
226 * argument can be used to make the dialog modal (GTK_DIALOG_MODAL)
227 * and/or to have it destroyed along with its transient parent
228 * (GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
229 * text/response ID pairs should be listed, with a NULL pointer ending
230 * the list. Button text can be either a stock ID such as
231 * GTK_STOCK_BUTTON_OK, or some arbitrary text. A response ID can be
232 * any positive number, or one of the values in the #GtkResponseType
233 * enumeration. If the user clicks one of these dialog buttons,
234 * #GtkDialog will emit the "response" signal with the corresponding
235 * response ID. If a #GtkDialog receives the "delete_event" signal, it
236 * will emit "response" with a response ID of GTK_RESPONSE_NONE.
237 * However, destroying a dialog does not emit the "response" signal;
238 * so be careful relying on "response" when using
239 * the GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
240 * so the first button in the list will be the leftmost button in the dialog.
242 * Here's a simple example:
244 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
246 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
247 * GTK_STOCK_BUTTON_OK,
248 * GTK_RESPONSE_ACCEPT,
249 * GTK_STOCK_BUTTON_CANCEL,
250 * GTK_RESPONSE_REJECT,
254 * Return value: a new #GtkDialog
257 gtk_dialog_new_with_buttons (const gchar *title,
259 GtkDialogFlags flags,
260 const gchar *first_button_text,
266 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
268 va_start (args, first_button_text);
270 gtk_dialog_add_buttons_valist (dialog,
276 return GTK_WIDGET (dialog);
279 typedef struct _ResponseData ResponseData;
287 get_response_data (GtkWidget *widget)
289 ResponseData *ad = gtk_object_get_data (GTK_OBJECT (widget),
290 "gtk-dialog-response-data");
294 ad = g_new (ResponseData, 1);
296 gtk_object_set_data_full (GTK_OBJECT (widget),
297 "gtk-dialog-response-data",
306 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
311 g_return_if_fail (GTK_IS_DIALOG (dialog));
313 response_id = GTK_RESPONSE_NONE;
315 ad = get_response_data (widget);
317 g_assert (ad != NULL);
319 response_id = ad->response_id;
321 gtk_dialog_response (dialog, response_id);
324 * gtk_dialog_add_action_widget:
325 * @dialog: a #GtkDialog
326 * @child: an activatable widget
327 * @response_id: response ID for @child
329 * Adds an activatable widget to the action area of a #GtkDialog,
330 * connecting a signal handler that will emit the "response" signal on
331 * the dialog when the widget is activated. The widget is appended to
332 * the end of the dialog's action area. If you want to add a
333 * non-activatable widget, simply pack it into the
334 * <literal>action_area</literal> field of the #GtkDialog struct.
338 gtk_dialog_add_action_widget (GtkDialog *dialog,
344 g_return_if_fail (GTK_IS_DIALOG (dialog));
345 g_return_if_fail (GTK_IS_WIDGET (child));
347 ad = get_response_data (child);
349 ad->response_id = response_id;
351 if (GTK_WIDGET_GET_CLASS (child)->activate_signal != 0)
354 gtk_signal_name (GTK_WIDGET_GET_CLASS (child)->activate_signal);
356 gtk_signal_connect_while_alive (GTK_OBJECT (child),
358 GTK_SIGNAL_FUNC (action_widget_activated),
360 GTK_OBJECT (dialog));
363 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
365 gtk_box_pack_end (GTK_BOX (dialog->action_area),
371 * gtk_dialog_add_button:
372 * @dialog: a #GtkDialog
373 * @button_text: text of button, or stock ID
374 * @response_id: response ID for the button
376 * Adds a button with the given text (or a stock button, if @button_text is a
377 * stock ID) and sets things up so that clicking the button will emit the
378 * "response" signal with the given @response_id. The button is appended to the
379 * end of the dialog's action area. The button widget is returned, but usually
382 * Return value: the button widget that was added
385 gtk_dialog_add_button (GtkDialog *dialog,
386 const gchar *button_text,
391 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
392 g_return_val_if_fail (button_text != NULL, NULL);
394 button = gtk_button_new_stock (button_text,
395 gtk_window_get_default_accel_group (GTK_WINDOW (dialog)));
397 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
399 gtk_widget_show (button);
401 gtk_dialog_add_action_widget (dialog,
409 gtk_dialog_add_buttons_valist(GtkDialog *dialog,
410 const gchar *first_button_text,
416 g_return_if_fail (GTK_IS_DIALOG (dialog));
418 if (first_button_text == NULL)
421 text = first_button_text;
422 response_id = va_arg (args, gint);
426 gtk_dialog_add_button (dialog, text, response_id);
428 text = va_arg (args, gchar*);
431 response_id = va_arg (args, int);
436 * gtk_dialog_add_buttons:
437 * @dialog: a #GtkDialog
438 * @first_button_text: button text or stock ID
439 * @Varargs: response ID for first button, then more text-response_id pairs
441 * Adds more buttons, same as calling gtk_dialog_add_button()
442 * repeatedly. The variable argument list should be NULL-terminated
443 * as with gtk_dialog_new_with_buttons(). Each button must have both
444 * text and response ID.
448 gtk_dialog_add_buttons (GtkDialog *dialog,
449 const gchar *first_button_text,
454 va_start (args, first_button_text);
456 gtk_dialog_add_buttons_valist (dialog,
464 * gtk_dialog_set_response_sensitive:
465 * @dialog: a #GtkDialog
466 * @response_id: a response ID
467 * @setting: %TRUE for sensitive
469 * Calls gtk_widget_set_sensitive (widget, @setting) for each
470 * widget in the dialog's action area with the given @response_id.
471 * A convenient way to sensitize/desensitize dialog buttons.
475 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
482 children = gtk_container_children (GTK_CONTAINER (dialog));
485 while (tmp_list != NULL)
487 GtkWidget *widget = tmp_list->data;
488 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
489 "gtk-dialog-response-data");
491 if (rd && rd->response_id == response_id)
492 gtk_widget_set_sensitive (widget, setting);
494 tmp_list = g_list_next (tmp_list);
497 g_list_free (children);
501 * gtk_dialog_set_default_response:
502 * @dialog: a #GtkDialog
503 * @response_id: a response ID
505 * Sets the last widget in the dialog's action area with the given @response_id
506 * as the default widget for the dialog. Pressing "Enter" normally activates
507 * the default widget.
511 gtk_dialog_set_default_response (GtkDialog *dialog,
517 children = gtk_container_children (GTK_CONTAINER (dialog->action_area));
520 while (tmp_list != NULL)
522 GtkWidget *widget = tmp_list->data;
523 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
524 "gtk-dialog-response-data");
526 if (rd && rd->response_id == response_id)
528 gtk_widget_grab_default (widget);
530 if (!GTK_WINDOW (dialog)->focus_widget)
531 gtk_widget_grab_focus (widget);
534 tmp_list = g_list_next (tmp_list);
537 g_list_free (children);
541 * gtk_dialog_response:
542 * @dialog: a #GtkDialog
543 * @response_id: response ID
545 * Emits the "response" signal with the given response ID. Used to
546 * indicate that the user has responded to the dialog in some way;
547 * typically either you or gtk_dialog_run() will be monitoring the
548 * "response" signal and take appropriate action.
551 gtk_dialog_response (GtkDialog *dialog,
554 g_return_if_fail (dialog != NULL);
555 g_return_if_fail (GTK_IS_DIALOG (dialog));
557 gtk_signal_emit (GTK_OBJECT (dialog),
558 dialog_signals[RESPONSE],
570 shutdown_loop (RunInfo *ri)
572 if (g_main_loop_is_running (ri->loop))
573 g_main_loop_quit (ri->loop);
577 run_unmap_handler (GtkDialog *dialog, gpointer data)
585 run_response_handler (GtkDialog *dialog,
593 ri->response_id = response_id;
599 run_delete_handler (GtkDialog *dialog,
607 /* emit response signal */
608 gtk_dialog_response (dialog, GTK_RESPONSE_NONE);
610 return TRUE; /* Do not destroy */
615 * @dialog: a #GtkDialog
617 * Blocks in a recursive main loop until the @dialog either emits the
618 * response signal, or is destroyed. If the dialog is destroyed,
619 * gtk_dialog_run() returns GTK_RESPONSE_NONE. Otherwise, it returns
620 * the response ID from the "response" signal emission. Before
621 * entering the recursive main loop, gtk_dialog_run() calls
622 * gtk_widget_show() on the dialog for you. Note that you still
623 * need to show any children of the dialog yourself.
625 * During gtk_dialog_run(), the default behavior of "delete_event" is
626 * disabled; if the dialog receives "delete_event", it will not be
627 * destroyed as windows usually are, and gtk_dialog_run() will return
628 * GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be
629 * modal. You can force gtk_dialog_run() to return at any time by
630 * calling gtk_dialog_response() to emit the "response"
631 * signal. Destroying the dialog during gtk_dialog_run() is a very bad
632 * idea, because your post-run code won't know whether the dialog was
635 * After gtk_dialog_run() returns, you are responsible for hiding or
636 * destroying the dialog if you wish to do so.
638 * Typical usage of this function might be:
640 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
643 * case GTK_RESPONSE_ACCEPT:
644 * do_application_specific_something ();
647 * do_nothing_since_dialog_was_cancelled ();
650 * gtk_widget_destroy (dialog);
653 * Return value: response ID
656 gtk_dialog_run (GtkDialog *dialog)
658 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
660 guint response_handler;
661 guint destroy_handler;
662 guint delete_handler;
664 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
666 gtk_object_ref (GTK_OBJECT (dialog));
668 if (!GTK_WIDGET_VISIBLE (dialog))
669 gtk_widget_show (GTK_WIDGET (dialog));
671 was_modal = GTK_WINDOW (dialog)->modal;
673 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
676 gtk_signal_connect (GTK_OBJECT (dialog),
678 GTK_SIGNAL_FUNC (run_response_handler),
682 gtk_signal_connect (GTK_OBJECT (dialog),
684 GTK_SIGNAL_FUNC (run_unmap_handler),
688 gtk_signal_connect (GTK_OBJECT (dialog),
690 GTK_SIGNAL_FUNC (run_delete_handler),
693 ri.loop = g_main_new (FALSE);
695 g_main_loop_run (ri.loop);
697 g_main_loop_unref (ri.loop);
701 if (!GTK_OBJECT_DESTROYED (dialog))
704 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
706 gtk_signal_disconnect (GTK_OBJECT (dialog), destroy_handler);
707 gtk_signal_disconnect (GTK_OBJECT (dialog), response_handler);
708 gtk_signal_disconnect (GTK_OBJECT (dialog), delete_handler);
711 gtk_object_unref (GTK_OBJECT (dialog));
713 return ri.response_id;