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"
37 static void gtk_dialog_class_init (GtkDialogClass *klass);
38 static void gtk_dialog_init (GtkDialog *dialog);
39 static gint gtk_dialog_key_press (GtkWidget *widget,
42 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
43 const gchar *first_button_text,
46 static gint gtk_dialog_delete_event_handler (GtkWidget *widget,
50 static void gtk_dialog_set_property (GObject *object,
54 static void gtk_dialog_get_property (GObject *object,
58 static void gtk_dialog_style_set (GtkWidget *widget,
59 GtkStyle *prev_style);
71 static gpointer parent_class;
72 static guint dialog_signals[LAST_SIGNAL];
75 gtk_dialog_get_type (void)
77 static GtkType dialog_type = 0;
81 static const GtkTypeInfo dialog_info =
85 sizeof (GtkDialogClass),
86 (GtkClassInitFunc) gtk_dialog_class_init,
87 (GtkObjectInitFunc) gtk_dialog_init,
88 /* reserved_1 */ NULL,
89 /* reserved_2 */ NULL,
90 (GtkClassInitFunc) NULL,
93 dialog_type = gtk_type_unique (GTK_TYPE_WINDOW, &dialog_info);
100 gtk_dialog_class_init (GtkDialogClass *class)
102 GObjectClass *gobject_class;
103 GtkObjectClass *object_class;
104 GtkWidgetClass *widget_class;
106 gobject_class = G_OBJECT_CLASS (class);
107 object_class = GTK_OBJECT_CLASS (class);
108 widget_class = GTK_WIDGET_CLASS (class);
110 parent_class = g_type_class_peek_parent (class);
112 gobject_class->set_property = gtk_dialog_set_property;
113 gobject_class->get_property = gtk_dialog_get_property;
115 widget_class->key_press_event = gtk_dialog_key_press;
116 widget_class->style_set = gtk_dialog_style_set;
118 g_object_class_install_property (gobject_class,
120 g_param_spec_boolean ("has_separator",
122 _("The dialog has a separator bar above its buttons"),
126 dialog_signals[RESPONSE] =
127 gtk_signal_new ("response",
129 GTK_CLASS_TYPE (object_class),
130 GTK_SIGNAL_OFFSET (GtkDialogClass, response),
131 gtk_marshal_NONE__INT,
135 gtk_widget_class_install_style_property (widget_class,
136 g_param_spec_int ("content_area_border",
137 _("Content area border"),
138 _("Width of border around the main dialog area"),
143 gtk_widget_class_install_style_property (widget_class,
144 g_param_spec_int ("button_spacing",
146 _("Spacing between buttons"),
152 gtk_widget_class_install_style_property (widget_class,
153 g_param_spec_int ("action_area_border",
154 _("Action area border"),
155 _("Width of border around the button area at the bottom of the dialog"),
163 update_spacings (GtkDialog *dialog)
166 gint content_area_border;
168 gint action_area_border;
170 widget = GTK_WIDGET (dialog);
172 gtk_widget_style_get (widget,
173 "content_area_border",
174 &content_area_border,
177 "action_area_border",
181 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
182 content_area_border);
183 gtk_box_set_spacing (GTK_BOX (dialog->action_area),
185 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
190 gtk_dialog_init (GtkDialog *dialog)
192 /* To avoid breaking old code that prevents destroy on delete event
193 * by connecting a handler, we have to have the FIRST signal
194 * connection on the dialog.
196 gtk_signal_connect (GTK_OBJECT (dialog),
198 GTK_SIGNAL_FUNC (gtk_dialog_delete_event_handler),
201 dialog->vbox = gtk_vbox_new (FALSE, 0);
203 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
204 gtk_widget_show (dialog->vbox);
206 dialog->action_area = gtk_hbutton_box_new ();
208 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
211 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
213 gtk_widget_show (dialog->action_area);
215 dialog->separator = gtk_hseparator_new ();
216 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
217 gtk_widget_show (dialog->separator);
219 gtk_window_set_type_hint (GTK_WINDOW (dialog),
220 GDK_WINDOW_TYPE_HINT_DIALOG);
225 gtk_dialog_set_property (GObject *object,
232 dialog = GTK_DIALOG (object);
236 case PROP_HAS_SEPARATOR:
237 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 gtk_dialog_get_property (GObject *object,
254 dialog = GTK_DIALOG (object);
258 case PROP_HAS_SEPARATOR:
259 g_value_set_boolean (value, dialog->separator != NULL);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 gtk_dialog_delete_event_handler (GtkWidget *widget,
273 /* emit response signal */
274 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
276 /* Do the destroy by default */
281 gtk_dialog_key_press (GtkWidget *widget,
286 event.type = GDK_DELETE;
287 event.window = widget->window;
288 event.send_event = TRUE;
290 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, key))
293 if (key->keyval != GDK_Escape)
296 /* Synthesize delete_event to close dialog. */
297 g_object_ref (G_OBJECT (event.window));
299 gtk_main_do_event ((GdkEvent*)&event);
301 g_object_unref (G_OBJECT (event.window));
307 gtk_dialog_style_set (GtkWidget *widget,
308 GtkStyle *prev_style)
310 update_spacings (GTK_DIALOG (widget));
314 gtk_dialog_new (void)
316 return GTK_WIDGET (gtk_type_new (GTK_TYPE_DIALOG));
320 gtk_dialog_new_empty (const gchar *title,
322 GtkDialogFlags flags)
326 dialog = GTK_DIALOG (g_object_new (GTK_TYPE_DIALOG, NULL));
329 gtk_window_set_title (GTK_WINDOW (dialog), title);
332 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
334 if (flags & GTK_DIALOG_MODAL)
335 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
337 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
338 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
340 if (flags & GTK_DIALOG_NO_SEPARATOR)
341 gtk_dialog_set_has_separator (dialog, FALSE);
343 return GTK_WIDGET (dialog);
347 * gtk_dialog_new_with_buttons:
348 * @title: Title of the dialog, or NULL
349 * @parent: Transient parent of the dialog, or NULL
350 * @flags: from #GtkDialogFlags
351 * @first_button_text: stock ID or text to go in first button, or NULL
352 * @Varargs: response ID for first button, then additional buttons, ending with NULL
354 * Creates a new #GtkDialog with title @title (or NULL for the default
355 * title; see gtk_window_set_title()) and transient parent @parent (or
356 * NULL for none; see gtk_window_set_transient_for()). The @flags
357 * argument can be used to make the dialog modal (GTK_DIALOG_MODAL)
358 * and/or to have it destroyed along with its transient parent
359 * (GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
360 * text/response ID pairs should be listed, with a NULL pointer ending
361 * the list. Button text can be either a stock ID such as
362 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
363 * any positive number, or one of the values in the #GtkResponseType
364 * enumeration. If the user clicks one of these dialog buttons,
365 * #GtkDialog will emit the "response" signal with the corresponding
366 * response ID. If a #GtkDialog receives the "delete_event" signal, it
367 * will emit "response" with a response ID of GTK_RESPONSE_DELETE_EVENT.
368 * However, destroying a dialog does not emit the "response" signal;
369 * so be careful relying on "response" when using
370 * the GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
371 * so the first button in the list will be the leftmost button in the dialog.
373 * Here's a simple example:
375 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
377 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
379 * GTK_RESPONSE_ACCEPT,
381 * GTK_RESPONSE_REJECT,
385 * Return value: a new #GtkDialog
388 gtk_dialog_new_with_buttons (const gchar *title,
390 GtkDialogFlags flags,
391 const gchar *first_button_text,
397 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
399 va_start (args, first_button_text);
401 gtk_dialog_add_buttons_valist (dialog,
407 return GTK_WIDGET (dialog);
410 typedef struct _ResponseData ResponseData;
418 get_response_data (GtkWidget *widget)
420 ResponseData *ad = gtk_object_get_data (GTK_OBJECT (widget),
421 "gtk-dialog-response-data");
425 ad = g_new (ResponseData, 1);
427 gtk_object_set_data_full (GTK_OBJECT (widget),
428 "gtk-dialog-response-data",
437 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
442 g_return_if_fail (GTK_IS_DIALOG (dialog));
444 response_id = GTK_RESPONSE_NONE;
446 ad = get_response_data (widget);
448 g_assert (ad != NULL);
450 response_id = ad->response_id;
452 gtk_dialog_response (dialog, response_id);
455 * gtk_dialog_add_action_widget:
456 * @dialog: a #GtkDialog
457 * @child: an activatable widget
458 * @response_id: response ID for @child
460 * Adds an activatable widget to the action area of a #GtkDialog,
461 * connecting a signal handler that will emit the "response" signal on
462 * the dialog when the widget is activated. The widget is appended to
463 * the end of the dialog's action area. If you want to add a
464 * non-activatable widget, simply pack it into the
465 * <literal>action_area</literal> field of the #GtkDialog struct.
469 gtk_dialog_add_action_widget (GtkDialog *dialog,
476 g_return_if_fail (GTK_IS_DIALOG (dialog));
477 g_return_if_fail (GTK_IS_WIDGET (child));
479 ad = get_response_data (child);
481 ad->response_id = response_id;
483 if (GTK_IS_BUTTON (child))
485 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
488 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal != 0;
492 const gchar* name = gtk_signal_name (signal_id);
494 gtk_signal_connect_while_alive (GTK_OBJECT (child),
496 GTK_SIGNAL_FUNC (action_widget_activated),
498 GTK_OBJECT (dialog));
501 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
503 gtk_box_pack_end (GTK_BOX (dialog->action_area),
509 * gtk_dialog_add_button:
510 * @dialog: a #GtkDialog
511 * @button_text: text of button, or stock ID
512 * @response_id: response ID for the button
514 * Adds a button with the given text (or a stock button, if @button_text is a
515 * stock ID) and sets things up so that clicking the button will emit the
516 * "response" signal with the given @response_id. The button is appended to the
517 * end of the dialog's action area. The button widget is returned, but usually
520 * Return value: the button widget that was added
523 gtk_dialog_add_button (GtkDialog *dialog,
524 const gchar *button_text,
529 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
530 g_return_val_if_fail (button_text != NULL, NULL);
532 button = gtk_button_new_from_stock (button_text);
534 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
536 gtk_widget_show (button);
538 gtk_dialog_add_action_widget (dialog,
546 gtk_dialog_add_buttons_valist(GtkDialog *dialog,
547 const gchar *first_button_text,
553 g_return_if_fail (GTK_IS_DIALOG (dialog));
555 if (first_button_text == NULL)
558 text = first_button_text;
559 response_id = va_arg (args, gint);
563 gtk_dialog_add_button (dialog, text, response_id);
565 text = va_arg (args, gchar*);
568 response_id = va_arg (args, int);
573 * gtk_dialog_add_buttons:
574 * @dialog: a #GtkDialog
575 * @first_button_text: button text or stock ID
576 * @Varargs: response ID for first button, then more text-response_id pairs
578 * Adds more buttons, same as calling gtk_dialog_add_button()
579 * repeatedly. The variable argument list should be NULL-terminated
580 * as with gtk_dialog_new_with_buttons(). Each button must have both
581 * text and response ID.
585 gtk_dialog_add_buttons (GtkDialog *dialog,
586 const gchar *first_button_text,
591 va_start (args, first_button_text);
593 gtk_dialog_add_buttons_valist (dialog,
601 * gtk_dialog_set_response_sensitive:
602 * @dialog: a #GtkDialog
603 * @response_id: a response ID
604 * @setting: %TRUE for sensitive
606 * Calls gtk_widget_set_sensitive (widget, @setting) for each
607 * widget in the dialog's action area with the given @response_id.
608 * A convenient way to sensitize/desensitize dialog buttons.
612 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
619 children = gtk_container_children (GTK_CONTAINER (dialog));
622 while (tmp_list != NULL)
624 GtkWidget *widget = tmp_list->data;
625 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
626 "gtk-dialog-response-data");
628 if (rd && rd->response_id == response_id)
629 gtk_widget_set_sensitive (widget, setting);
631 tmp_list = g_list_next (tmp_list);
634 g_list_free (children);
638 * gtk_dialog_set_default_response:
639 * @dialog: a #GtkDialog
640 * @response_id: a response ID
642 * Sets the last widget in the dialog's action area with the given @response_id
643 * as the default widget for the dialog. Pressing "Enter" normally activates
644 * the default widget.
648 gtk_dialog_set_default_response (GtkDialog *dialog,
654 children = gtk_container_children (GTK_CONTAINER (dialog->action_area));
657 while (tmp_list != NULL)
659 GtkWidget *widget = tmp_list->data;
660 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
661 "gtk-dialog-response-data");
663 if (rd && rd->response_id == response_id)
665 gtk_widget_grab_default (widget);
667 if (!GTK_WINDOW (dialog)->focus_widget)
668 gtk_widget_grab_focus (widget);
671 tmp_list = g_list_next (tmp_list);
674 g_list_free (children);
678 gtk_dialog_set_has_separator (GtkDialog *dialog,
681 g_return_if_fail (GTK_IS_DIALOG (dialog));
683 /* this might fail if we get called before _init() somehow */
684 g_assert (dialog->vbox != NULL);
686 if (setting && dialog->separator == NULL)
688 dialog->separator = gtk_hseparator_new ();
689 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
691 /* The app programmer could screw this up, but, their own fault.
692 * Moves the separator just above the action area.
694 gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
695 gtk_widget_show (dialog->separator);
697 else if (!setting && dialog->separator != NULL)
699 gtk_widget_destroy (dialog->separator);
700 dialog->separator = NULL;
703 g_object_notify (G_OBJECT (dialog), "has_separator");
707 gtk_dialog_get_has_separator (GtkDialog *dialog)
709 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
711 return dialog->separator != NULL;
715 * gtk_dialog_response:
716 * @dialog: a #GtkDialog
717 * @response_id: response ID
719 * Emits the "response" signal with the given response ID. Used to
720 * indicate that the user has responded to the dialog in some way;
721 * typically either you or gtk_dialog_run() will be monitoring the
722 * "response" signal and take appropriate action.
725 gtk_dialog_response (GtkDialog *dialog,
728 g_return_if_fail (dialog != NULL);
729 g_return_if_fail (GTK_IS_DIALOG (dialog));
731 gtk_signal_emit (GTK_OBJECT (dialog),
732 dialog_signals[RESPONSE],
744 shutdown_loop (RunInfo *ri)
746 if (g_main_loop_is_running (ri->loop))
747 g_main_loop_quit (ri->loop);
751 run_unmap_handler (GtkDialog *dialog, gpointer data)
759 run_response_handler (GtkDialog *dialog,
767 ri->response_id = response_id;
773 run_delete_handler (GtkDialog *dialog,
781 return TRUE; /* Do not destroy */
786 * @dialog: a #GtkDialog
788 * Blocks in a recursive main loop until the @dialog either emits the
789 * response signal, or is destroyed. If the dialog is destroyed,
790 * gtk_dialog_run() returns GTK_RESPONSE_NONE. Otherwise, it returns
791 * the response ID from the "response" signal emission. Before
792 * entering the recursive main loop, gtk_dialog_run() calls
793 * gtk_widget_show() on the dialog for you. Note that you still
794 * need to show any children of the dialog yourself.
796 * During gtk_dialog_run(), the default behavior of "delete_event" is
797 * disabled; if the dialog receives "delete_event", it will not be
798 * destroyed as windows usually are, and gtk_dialog_run() will return
799 * GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be
800 * modal. You can force gtk_dialog_run() to return at any time by
801 * calling gtk_dialog_response() to emit the "response"
802 * signal. Destroying the dialog during gtk_dialog_run() is a very bad
803 * idea, because your post-run code won't know whether the dialog was
806 * After gtk_dialog_run() returns, you are responsible for hiding or
807 * destroying the dialog if you wish to do so.
809 * Typical usage of this function might be:
811 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
814 * case GTK_RESPONSE_ACCEPT:
815 * do_application_specific_something ();
818 * do_nothing_since_dialog_was_cancelled ();
821 * gtk_widget_destroy (dialog);
824 * Return value: response ID
827 gtk_dialog_run (GtkDialog *dialog)
829 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
831 guint response_handler;
832 guint destroy_handler;
833 guint delete_handler;
835 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
837 gtk_object_ref (GTK_OBJECT (dialog));
839 if (!GTK_WIDGET_VISIBLE (dialog))
840 gtk_widget_show (GTK_WIDGET (dialog));
842 was_modal = GTK_WINDOW (dialog)->modal;
844 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
847 gtk_signal_connect (GTK_OBJECT (dialog),
849 GTK_SIGNAL_FUNC (run_response_handler),
853 gtk_signal_connect (GTK_OBJECT (dialog),
855 GTK_SIGNAL_FUNC (run_unmap_handler),
859 gtk_signal_connect (GTK_OBJECT (dialog),
861 GTK_SIGNAL_FUNC (run_delete_handler),
864 ri.loop = g_main_new (FALSE);
866 g_main_loop_run (ri.loop);
868 g_main_loop_unref (ri.loop);
872 if (!GTK_OBJECT_DESTROYED (dialog))
875 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
877 gtk_signal_disconnect (GTK_OBJECT (dialog), destroy_handler);
878 gtk_signal_disconnect (GTK_OBJECT (dialog), response_handler);
879 gtk_signal_disconnect (GTK_OBJECT (dialog), delete_handler);
882 gtk_object_unref (GTK_OBJECT (dialog));
884 return ri.response_id;