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/.
30 #include "gtkbutton.h"
31 #include "gtkdialog.h"
34 #include "gtkhseparator.h"
35 #include "gtkmarshalers.h"
37 #include "gdkkeysyms.h"
40 #include "gtkbindings.h"
41 #include "gtkprivate.h"
42 #include "gtkbuildable.h"
45 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_DIALOG, GtkDialogPrivate))
48 guint ignore_separator : 1;
51 typedef struct _ResponseData ResponseData;
58 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
59 const gchar *first_button_text,
62 static gboolean gtk_dialog_delete_event_handler (GtkWidget *widget,
66 static void gtk_dialog_set_property (GObject *object,
70 static void gtk_dialog_get_property (GObject *object,
74 static void gtk_dialog_style_set (GtkWidget *widget,
75 GtkStyle *prev_style);
76 static void gtk_dialog_map (GtkWidget *widget);
78 static void gtk_dialog_close (GtkDialog *dialog);
80 static ResponseData * get_response_data (GtkWidget *widget,
83 static void gtk_dialog_buildable_interface_init (GtkBuildableIface *iface);
84 static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
86 const gchar *childname);
87 static gboolean gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
91 GMarkupParser *parser,
93 static void gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
111 static guint dialog_signals[LAST_SIGNAL];
113 G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
114 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
115 gtk_dialog_buildable_interface_init))
118 gtk_dialog_class_init (GtkDialogClass *class)
120 GObjectClass *gobject_class;
121 GtkWidgetClass *widget_class;
122 GtkBindingSet *binding_set;
124 gobject_class = G_OBJECT_CLASS (class);
125 widget_class = GTK_WIDGET_CLASS (class);
127 gobject_class->set_property = gtk_dialog_set_property;
128 gobject_class->get_property = gtk_dialog_get_property;
130 widget_class->map = gtk_dialog_map;
131 widget_class->style_set = gtk_dialog_style_set;
133 class->close = gtk_dialog_close;
135 g_type_class_add_private (gobject_class, sizeof (GtkDialogPrivate));
138 * GtkDialog:has-separator:
140 * When %TRUE, the dialog has a separator bar above its buttons.
142 g_object_class_install_property (gobject_class,
144 g_param_spec_boolean ("has-separator",
146 P_("The dialog has a separator bar above its buttons"),
148 GTK_PARAM_READWRITE));
151 * GtkDialog::response:
152 * @dialog: the object on which the signal is emitted
153 * @response_id: the response ID
155 * Emitted when an action widget is clicked, the dialog receives a
156 * delete event, or the application programmer calls gtk_dialog_response().
157 * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
158 * Otherwise, it depends on which action widget was clicked.
160 dialog_signals[RESPONSE] =
161 g_signal_new (I_("response"),
162 G_OBJECT_CLASS_TYPE (class),
164 G_STRUCT_OFFSET (GtkDialogClass, response),
166 _gtk_marshal_VOID__INT,
173 * The ::close signal is a
174 * <link linkend="keybinding-signals">keybinding signal</link>
175 * which getrs emitted when the user uses a keybinding to close
178 * The default binding for this signal is the Escape key.
180 dialog_signals[CLOSE] =
181 g_signal_new (I_("close"),
182 G_OBJECT_CLASS_TYPE (class),
183 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
184 G_STRUCT_OFFSET (GtkDialogClass, close),
186 _gtk_marshal_VOID__VOID,
189 gtk_widget_class_install_style_property (widget_class,
190 g_param_spec_int ("content-area-border",
191 P_("Content area border"),
192 P_("Width of border around the main dialog area"),
196 GTK_PARAM_READABLE));
197 gtk_widget_class_install_style_property (widget_class,
198 g_param_spec_int ("button-spacing",
199 P_("Button spacing"),
200 P_("Spacing between buttons"),
204 GTK_PARAM_READABLE));
206 gtk_widget_class_install_style_property (widget_class,
207 g_param_spec_int ("action-area-border",
208 P_("Action area border"),
209 P_("Width of border around the button area at the bottom of the dialog"),
213 GTK_PARAM_READABLE));
215 binding_set = gtk_binding_set_by_class (class);
217 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0, "close", 0);
221 update_spacings (GtkDialog *dialog)
224 gint content_area_border;
226 gint action_area_border;
228 widget = GTK_WIDGET (dialog);
230 gtk_widget_style_get (widget,
231 "content-area-border", &content_area_border,
232 "button-spacing", &button_spacing,
233 "action-area-border", &action_area_border,
236 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
237 content_area_border);
238 gtk_box_set_spacing (GTK_BOX (dialog->action_area),
240 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
245 gtk_dialog_init (GtkDialog *dialog)
247 GtkDialogPrivate *priv;
249 priv = GET_PRIVATE (dialog);
250 priv->ignore_separator = FALSE;
252 /* To avoid breaking old code that prevents destroy on delete event
253 * by connecting a handler, we have to have the FIRST signal
254 * connection on the dialog.
256 g_signal_connect (dialog,
258 G_CALLBACK (gtk_dialog_delete_event_handler),
261 dialog->vbox = gtk_vbox_new (FALSE, 0);
263 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
264 gtk_widget_show (dialog->vbox);
266 dialog->action_area = gtk_hbutton_box_new ();
268 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
271 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
273 gtk_widget_show (dialog->action_area);
275 dialog->separator = gtk_hseparator_new ();
276 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
277 gtk_widget_show (dialog->separator);
279 gtk_window_set_type_hint (GTK_WINDOW (dialog),
280 GDK_WINDOW_TYPE_HINT_DIALOG);
281 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
284 static GtkBuildableIface *parent_buildable_iface;
287 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
289 parent_buildable_iface = g_type_interface_peek_parent (iface);
290 iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
291 iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
292 iface->custom_finished = gtk_dialog_buildable_custom_finished;
296 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
298 const gchar *childname)
300 if (strcmp (childname, "vbox") == 0)
301 return G_OBJECT (GTK_DIALOG (buildable)->vbox);
302 else if (strcmp (childname, "action_area") == 0)
303 return G_OBJECT (GTK_DIALOG (buildable)->action_area);
305 return parent_buildable_iface->get_internal_child (buildable,
311 gtk_dialog_set_property (GObject *object,
318 dialog = GTK_DIALOG (object);
322 case PROP_HAS_SEPARATOR:
323 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 gtk_dialog_get_property (GObject *object,
340 dialog = GTK_DIALOG (object);
344 case PROP_HAS_SEPARATOR:
345 g_value_set_boolean (value, dialog->separator != NULL);
349 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
355 gtk_dialog_delete_event_handler (GtkWidget *widget,
359 /* emit response signal */
360 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
362 /* Do the destroy by default */
366 /* A far too tricky heuristic for getting the right initial
367 * focus widget if none was set. What we do is we focus the first
368 * widget in the tab chain, but if this results in the focus
369 * ending up on one of the response widgets _other_ than the
370 * default response, we focus the default response instead.
372 * Additionally, skip selectable labels when looking for the
373 * right initial focus widget.
376 gtk_dialog_map (GtkWidget *widget)
378 GtkWindow *window = GTK_WINDOW (widget);
379 GtkDialog *dialog = GTK_DIALOG (widget);
381 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
383 if (!window->focus_widget)
385 GList *children, *tmp_list;
386 GtkWidget *first_focus = NULL;
390 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
392 if (first_focus == NULL)
393 first_focus = window->focus_widget;
394 else if (first_focus == window->focus_widget)
397 if (!GTK_IS_LABEL (window->focus_widget))
400 gtk_label_select_region (GTK_LABEL (window->focus_widget), 0, 0);
404 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
408 GtkWidget *child = tmp_list->data;
410 if ((window->focus_widget == NULL ||
411 child == window->focus_widget) &&
412 child != window->default_widget &&
413 window->default_widget)
415 gtk_widget_grab_focus (window->default_widget);
419 tmp_list = tmp_list->next;
422 g_list_free (children);
427 gtk_dialog_style_set (GtkWidget *widget,
428 GtkStyle *prev_style)
430 update_spacings (GTK_DIALOG (widget));
434 dialog_find_button (GtkDialog *dialog,
437 GList *children, *tmp_list;
438 GtkWidget *child = NULL;
440 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
442 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
444 ResponseData *rd = get_response_data (tmp_list->data, FALSE);
446 if (rd && rd->response_id == response_id)
448 child = tmp_list->data;
453 g_list_free (children);
459 gtk_dialog_close (GtkDialog *dialog)
461 /* Synthesize delete_event to close dialog. */
463 GtkWidget *widget = GTK_WIDGET (dialog);
466 event = gdk_event_new (GDK_DELETE);
468 event->any.window = g_object_ref (widget->window);
469 event->any.send_event = TRUE;
471 gtk_main_do_event (event);
472 gdk_event_free (event);
476 gtk_dialog_new (void)
478 return g_object_new (GTK_TYPE_DIALOG, NULL);
482 gtk_dialog_new_empty (const gchar *title,
484 GtkDialogFlags flags)
488 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
491 gtk_window_set_title (GTK_WINDOW (dialog), title);
494 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
496 if (flags & GTK_DIALOG_MODAL)
497 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
499 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
500 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
502 if (flags & GTK_DIALOG_NO_SEPARATOR)
503 gtk_dialog_set_has_separator (dialog, FALSE);
505 return GTK_WIDGET (dialog);
509 * gtk_dialog_new_with_buttons:
510 * @title: Title of the dialog, or %NULL
511 * @parent: Transient parent of the dialog, or %NULL
512 * @flags: from #GtkDialogFlags
513 * @first_button_text: stock ID or text to go in first button, or %NULL
514 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
516 * Creates a new #GtkDialog with title @title (or %NULL for the default
517 * title; see gtk_window_set_title()) and transient parent @parent (or
518 * %NULL for none; see gtk_window_set_transient_for()). The @flags
519 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
520 * and/or to have it destroyed along with its transient parent
521 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
522 * text/response ID pairs should be listed, with a %NULL pointer ending
523 * the list. Button text can be either a stock ID such as
524 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
525 * any positive number, or one of the values in the #GtkResponseType
526 * enumeration. If the user clicks one of these dialog buttons,
527 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
528 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
529 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
530 * However, destroying a dialog does not emit the ::response signal;
531 * so be careful relying on ::response when using the
532 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
533 * so the first button in the list will be the leftmost button in the dialog.
535 * Here's a simple example:
537 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
539 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
541 * GTK_RESPONSE_ACCEPT,
543 * GTK_RESPONSE_REJECT,
547 * Return value: a new #GtkDialog
550 gtk_dialog_new_with_buttons (const gchar *title,
552 GtkDialogFlags flags,
553 const gchar *first_button_text,
559 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
561 va_start (args, first_button_text);
563 gtk_dialog_add_buttons_valist (dialog,
569 return GTK_WIDGET (dialog);
573 response_data_free (gpointer data)
575 g_slice_free (ResponseData, data);
579 get_response_data (GtkWidget *widget,
582 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
583 "gtk-dialog-response-data");
585 if (ad == NULL && create)
587 ad = g_slice_new (ResponseData);
589 g_object_set_data_full (G_OBJECT (widget),
590 I_("gtk-dialog-response-data"),
599 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
603 response_id = gtk_dialog_get_response_for_widget (dialog, widget);
605 gtk_dialog_response (dialog, response_id);
609 * gtk_dialog_add_action_widget:
610 * @dialog: a #GtkDialog
611 * @child: an activatable widget
612 * @response_id: response ID for @child
614 * Adds an activatable widget to the action area of a #GtkDialog,
615 * connecting a signal handler that will emit the #GtkDialog::response
616 * signal on the dialog when the widget is activated. The widget is
617 * appended to the end of the dialog's action area. If you want to add a
618 * non-activatable widget, simply pack it into the @action_area field
619 * of the #GtkDialog struct.
622 gtk_dialog_add_action_widget (GtkDialog *dialog,
629 g_return_if_fail (GTK_IS_DIALOG (dialog));
630 g_return_if_fail (GTK_IS_WIDGET (child));
632 ad = get_response_data (child, TRUE);
634 ad->response_id = response_id;
636 if (GTK_IS_BUTTON (child))
637 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
639 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
645 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
647 g_signal_connect_closure_by_id (child,
654 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
656 gtk_box_pack_end (GTK_BOX (dialog->action_area),
660 if (response_id == GTK_RESPONSE_HELP)
661 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
665 * gtk_dialog_add_button:
666 * @dialog: a #GtkDialog
667 * @button_text: text of button, or stock ID
668 * @response_id: response ID for the button
670 * Adds a button with the given text (or a stock button, if @button_text is a
671 * stock ID) and sets things up so that clicking the button will emit the
672 * #GtkDialog::response signal with the given @response_id. The button is
673 * appended to the end of the dialog's action area. The button widget is
674 * returned, but usually you don't need it.
676 * Return value: the button widget that was added
679 gtk_dialog_add_button (GtkDialog *dialog,
680 const gchar *button_text,
685 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
686 g_return_val_if_fail (button_text != NULL, NULL);
688 button = gtk_button_new_from_stock (button_text);
690 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
692 gtk_widget_show (button);
694 gtk_dialog_add_action_widget (dialog,
702 gtk_dialog_add_buttons_valist (GtkDialog *dialog,
703 const gchar *first_button_text,
709 g_return_if_fail (GTK_IS_DIALOG (dialog));
711 if (first_button_text == NULL)
714 text = first_button_text;
715 response_id = va_arg (args, gint);
719 gtk_dialog_add_button (dialog, text, response_id);
721 text = va_arg (args, gchar*);
724 response_id = va_arg (args, int);
729 * gtk_dialog_add_buttons:
730 * @dialog: a #GtkDialog
731 * @first_button_text: button text or stock ID
732 * @Varargs: response ID for first button, then more text-response_id pairs
734 * Adds more buttons, same as calling gtk_dialog_add_button()
735 * repeatedly. The variable argument list should be %NULL-terminated
736 * as with gtk_dialog_new_with_buttons(). Each button must have both
737 * text and response ID.
740 gtk_dialog_add_buttons (GtkDialog *dialog,
741 const gchar *first_button_text,
746 va_start (args, first_button_text);
748 gtk_dialog_add_buttons_valist (dialog,
756 * gtk_dialog_set_response_sensitive:
757 * @dialog: a #GtkDialog
758 * @response_id: a response ID
759 * @setting: %TRUE for sensitive
761 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
762 * for each widget in the dialog's action area with the given @response_id.
763 * A convenient way to sensitize/desensitize dialog buttons.
766 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
773 g_return_if_fail (GTK_IS_DIALOG (dialog));
775 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
778 while (tmp_list != NULL)
780 GtkWidget *widget = tmp_list->data;
781 ResponseData *rd = get_response_data (widget, FALSE);
783 if (rd && rd->response_id == response_id)
784 gtk_widget_set_sensitive (widget, setting);
786 tmp_list = g_list_next (tmp_list);
789 g_list_free (children);
793 * gtk_dialog_set_default_response:
794 * @dialog: a #GtkDialog
795 * @response_id: a response ID
797 * Sets the last widget in the dialog's action area with the given @response_id
798 * as the default widget for the dialog. Pressing "Enter" normally activates
799 * the default widget.
802 gtk_dialog_set_default_response (GtkDialog *dialog,
808 g_return_if_fail (GTK_IS_DIALOG (dialog));
810 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
813 while (tmp_list != NULL)
815 GtkWidget *widget = tmp_list->data;
816 ResponseData *rd = get_response_data (widget, FALSE);
818 if (rd && rd->response_id == response_id)
819 gtk_widget_grab_default (widget);
821 tmp_list = g_list_next (tmp_list);
824 g_list_free (children);
828 * gtk_dialog_set_has_separator:
829 * @dialog: a #GtkDialog
830 * @setting: %TRUE to have a separator
832 * Sets whether the dialog has a separator above the buttons.
836 gtk_dialog_set_has_separator (GtkDialog *dialog,
839 GtkDialogPrivate *priv;
841 g_return_if_fail (GTK_IS_DIALOG (dialog));
843 priv = GET_PRIVATE (dialog);
845 /* this might fail if we get called before _init() somehow */
846 g_assert (dialog->vbox != NULL);
848 if (priv->ignore_separator)
850 g_warning ("Ignoring the separator setting");
854 if (setting && dialog->separator == NULL)
856 dialog->separator = gtk_hseparator_new ();
857 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
859 /* The app programmer could screw this up, but, their own fault.
860 * Moves the separator just above the action area.
862 gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
863 gtk_widget_show (dialog->separator);
865 else if (!setting && dialog->separator != NULL)
867 gtk_widget_destroy (dialog->separator);
868 dialog->separator = NULL;
871 g_object_notify (G_OBJECT (dialog), "has-separator");
875 * gtk_dialog_get_has_separator:
876 * @dialog: a #GtkDialog
878 * Accessor for whether the dialog has a separator.
880 * Return value: %TRUE if the dialog has a separator
883 gtk_dialog_get_has_separator (GtkDialog *dialog)
885 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
887 return dialog->separator != NULL;
891 * gtk_dialog_response:
892 * @dialog: a #GtkDialog
893 * @response_id: response ID
895 * Emits the #GtkDialog::response signal with the given response ID.
896 * Used to indicate that the user has responded to the dialog in some way;
897 * typically either you or gtk_dialog_run() will be monitoring the
898 * ::response signal and take appropriate action.
901 gtk_dialog_response (GtkDialog *dialog,
904 g_return_if_fail (GTK_IS_DIALOG (dialog));
906 g_signal_emit (dialog,
907 dialog_signals[RESPONSE],
921 shutdown_loop (RunInfo *ri)
923 if (g_main_loop_is_running (ri->loop))
924 g_main_loop_quit (ri->loop);
928 run_unmap_handler (GtkDialog *dialog, gpointer data)
936 run_response_handler (GtkDialog *dialog,
944 ri->response_id = response_id;
950 run_delete_handler (GtkDialog *dialog,
958 return TRUE; /* Do not destroy */
962 run_destroy_handler (GtkDialog *dialog, gpointer data)
966 /* shutdown_loop will be called by run_unmap_handler */
968 ri->destroyed = TRUE;
973 * @dialog: a #GtkDialog
975 * Blocks in a recursive main loop until the @dialog either emits the
976 * #GtkDialog::response signal, or is destroyed. If the dialog is
977 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
978 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
979 * ::response signal emission.
981 * Before entering the recursive main loop, gtk_dialog_run() calls
982 * gtk_widget_show() on the dialog for you. Note that you still
983 * need to show any children of the dialog yourself.
985 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
986 * is disabled; if the dialog receives ::delete_event, it will not be
987 * destroyed as windows usually are, and gtk_dialog_run() will return
988 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
989 * will be modal. You can force gtk_dialog_run() to return at any time by
990 * calling gtk_dialog_response() to emit the ::response signal. Destroying
991 * the dialog during gtk_dialog_run() is a very bad idea, because your
992 * post-run code won't know whether the dialog was destroyed or not.
994 * After gtk_dialog_run() returns, you are responsible for hiding or
995 * destroying the dialog if you wish to do so.
997 * Typical usage of this function might be:
999 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1002 * case GTK_RESPONSE_ACCEPT:
1003 * do_application_specific_something ();
1006 * do_nothing_since_dialog_was_cancelled ();
1009 * gtk_widget_destroy (dialog);
1012 * Note that even though the recursive main loop gives the effect of a
1013 * modal dialog (it prevents the user from interacting with other
1014 * windows in the same window group while the dialog is run), callbacks
1015 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1016 * be triggered during a gtk_dialog_run() call.
1018 * Return value: response ID
1021 gtk_dialog_run (GtkDialog *dialog)
1023 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1025 gulong response_handler;
1026 gulong unmap_handler;
1027 gulong destroy_handler;
1028 gulong delete_handler;
1030 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1032 g_object_ref (dialog);
1034 was_modal = GTK_WINDOW (dialog)->modal;
1036 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1038 if (!GTK_WIDGET_VISIBLE (dialog))
1039 gtk_widget_show (GTK_WIDGET (dialog));
1042 g_signal_connect (dialog,
1044 G_CALLBACK (run_response_handler),
1048 g_signal_connect (dialog,
1050 G_CALLBACK (run_unmap_handler),
1054 g_signal_connect (dialog,
1056 G_CALLBACK (run_delete_handler),
1060 g_signal_connect (dialog,
1062 G_CALLBACK (run_destroy_handler),
1065 ri.loop = g_main_loop_new (NULL, FALSE);
1067 GDK_THREADS_LEAVE ();
1068 g_main_loop_run (ri.loop);
1069 GDK_THREADS_ENTER ();
1071 g_main_loop_unref (ri.loop);
1078 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1080 g_signal_handler_disconnect (dialog, response_handler);
1081 g_signal_handler_disconnect (dialog, unmap_handler);
1082 g_signal_handler_disconnect (dialog, delete_handler);
1083 g_signal_handler_disconnect (dialog, destroy_handler);
1086 g_object_unref (dialog);
1088 return ri.response_id;
1092 _gtk_dialog_set_ignore_separator (GtkDialog *dialog,
1093 gboolean ignore_separator)
1095 GtkDialogPrivate *priv;
1097 priv = GET_PRIVATE (dialog);
1098 priv->ignore_separator = ignore_separator;
1102 * gtk_dialog_get_response_for_widget:
1103 * @dialog: a #GtkDialog
1104 * @widget: a widget in the action area of @dialog
1106 * Gets the response id of a widget in the action area
1109 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1110 * if @widget doesn't have a response id set.
1115 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1120 rd = get_response_data (widget, FALSE);
1122 return GTK_RESPONSE_NONE;
1124 return rd->response_id;
1128 * gtk_alternative_dialog_button_order:
1129 * @screen: a #GdkScreen, or %NULL to use the default screen
1131 * Returns %TRUE if dialogs are expected to use an alternative
1132 * button order on the screen @screen. See
1133 * gtk_dialog_set_alternative_button_order() for more details
1134 * about alternative button order.
1136 * If you need to use this function, you should probably connect
1137 * to the ::notify:gtk-alternative-button-order signal on the
1138 * #GtkSettings object associated to @screen, in order to be
1139 * notified if the button order setting changes.
1141 * Returns: Whether the alternative button order should be used
1146 gtk_alternative_dialog_button_order (GdkScreen *screen)
1148 GtkSettings *settings;
1152 settings = gtk_settings_get_for_screen (screen);
1154 settings = gtk_settings_get_default ();
1156 g_object_get (settings,
1157 "gtk-alternative-button-order", &result, NULL);
1163 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1164 gint first_response_id,
1171 response_id = first_response_id;
1173 while (response_id != -1)
1175 /* reorder child with response_id to position */
1176 child = dialog_find_button (dialog, response_id);
1177 gtk_box_reorder_child (GTK_BOX (dialog->action_area), child, position);
1179 response_id = va_arg (args, gint);
1185 * gtk_dialog_set_alternative_button_order:
1186 * @dialog: a #GtkDialog
1187 * @first_response_id: a response id used by one @dialog's buttons
1188 * @Varargs: a list of more response ids of @dialog's buttons, terminated by -1
1190 * Sets an alternative button order. If the
1191 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1192 * the dialog buttons are reordered according to the order of the
1193 * response ids passed to this function.
1195 * By default, GTK+ dialogs use the button order advocated by the Gnome
1196 * <ulink url="http://developer.gnome.org/projects/gup/hig/2.0/">Human
1197 * Interface Guidelines</ulink> with the affirmative button at the far
1198 * right, and the cancel button left of it. But the builtin GTK+ dialogs
1199 * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1200 * which is more suitable on some platforms, e.g. Windows.
1202 * Use this function after adding all the buttons to your dialog, as the
1203 * following example shows:
1205 * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1207 * GTK_RESPONSE_CANCEL);
1209 * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1213 * gtk_widget_grab_default (ok_button);
1215 * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1217 * GTK_RESPONSE_HELP);
1219 * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1221 * GTK_RESPONSE_CANCEL,
1222 * GTK_RESPONSE_HELP,
1229 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1230 gint first_response_id,
1236 g_return_if_fail (GTK_IS_DIALOG (dialog));
1238 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1239 if (!gtk_alternative_dialog_button_order (screen))
1242 va_start (args, first_response_id);
1244 gtk_dialog_set_alternative_button_order_valist (dialog,
1250 * gtk_dialog_set_alternative_button_order_from_array:
1251 * @dialog: a #GtkDialog
1252 * @n_params: the number of response ids in @new_order
1253 * @new_order: an array of response ids of @dialog's buttons
1255 * Sets an alternative button order. If the
1256 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1257 * the dialog buttons are reordered according to the order of the
1258 * response ids in @new_order.
1260 * See gtk_dialog_set_alternative_button_order() for more information.
1262 * This function is for use by language bindings.
1267 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1275 g_return_if_fail (GTK_IS_DIALOG (dialog));
1276 g_return_if_fail (new_order != NULL);
1278 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1279 if (!gtk_alternative_dialog_button_order (screen))
1282 for (position = 0; position < n_params; position++)
1284 /* reorder child with response_id to position */
1285 child = dialog_find_button (dialog, new_order[position]);
1286 gtk_box_reorder_child (GTK_BOX (dialog->action_area), child, position);
1297 GtkBuilder *builder;
1300 } ActionWidgetsSubParserData;
1303 attributes_start_element (GMarkupParseContext *context,
1304 const gchar *element_name,
1305 const gchar **names,
1306 const gchar **values,
1310 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1313 if (strcmp (element_name, "action-widget") == 0)
1315 for (i = 0; names[i]; i++)
1316 if (strcmp (names[i], "response") == 0)
1317 parser_data->response = g_strdup (values[i]);
1319 else if (strcmp (element_name, "action-widgets") == 0)
1322 g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1326 attributes_text_element (GMarkupParseContext *context,
1332 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1333 ActionWidgetInfo *item;
1335 if (!parser_data->response)
1338 item = g_new (ActionWidgetInfo, 1);
1339 item->widget_name = g_strndup (text, text_len);
1340 item->response_id = parser_data->response;
1341 parser_data->items = g_slist_prepend (parser_data->items, item);
1342 parser_data->response = NULL;
1345 static const GMarkupParser attributes_parser =
1347 attributes_start_element,
1349 attributes_text_element,
1353 gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
1354 GtkBuilder *builder,
1356 const gchar *tagname,
1357 GMarkupParser *parser,
1360 ActionWidgetsSubParserData *parser_data;
1365 if (strcmp (tagname, "action-widgets") == 0)
1367 parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1368 parser_data->dialog = GTK_DIALOG (buildable);
1369 parser_data->items = NULL;
1371 *parser = attributes_parser;
1372 *data = parser_data;
1376 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1377 tagname, parser, data);
1381 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1382 GtkBuilder *builder,
1384 const gchar *tagname,
1388 ActionWidgetsSubParserData *parser_data;
1394 if (strcmp (tagname, "action-widgets"))
1396 parent_buildable_iface->custom_finished (buildable, builder, child,
1397 tagname, user_data);
1401 dialog = GTK_DIALOG (buildable);
1402 parser_data = (ActionWidgetsSubParserData*)user_data;
1403 parser_data->items = g_slist_reverse (parser_data->items);
1405 for (l = parser_data->items; l; l = l->next)
1407 ActionWidgetInfo *item = l->data;
1409 object = gtk_builder_get_object (builder, item->widget_name);
1412 g_warning ("Unknown object %s specified in action-widgets of %s",
1414 gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1418 ad = get_response_data (GTK_WIDGET (object), TRUE);
1419 ad->response_id = atoi (item->response_id);
1421 if (GTK_IS_BUTTON (object))
1422 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1424 signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1430 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1432 g_signal_connect_closure_by_id (object,
1439 if (ad->response_id == GTK_RESPONSE_HELP)
1440 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area),
1441 GTK_WIDGET (object), TRUE);
1443 g_free (item->widget_name);
1444 g_free (item->response_id);
1447 g_slist_free (parser_data->items);
1448 g_slice_free (ActionWidgetsSubParserData, parser_data);
1452 * gtk_dialog_get_action_area:
1453 * @dialog: a #GtkDialog
1455 * Returns the action area of @dialog.
1457 * Returns: the action area.
1462 gtk_dialog_get_action_area (GtkDialog *dialog)
1464 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1466 return dialog->action_area;
1470 * gtk_dialog_get_content_area:
1471 * @dialog: a #GtkDialog
1473 * Returns the content area of @dialog.
1475 * Returns: the content area #GtkVBox.
1480 gtk_dialog_get_content_area (GtkDialog *dialog)
1482 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1484 return dialog->vbox;
1487 #define __GTK_DIALOG_C__
1488 #include "gtkaliasdef.c"