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 gets 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));
198 * GtkDialog:content-area-spacing:
200 * The default spacing used between elements of the
201 * content area of the dialog, as returned by
202 * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
203 * was called on that widget directly.
207 gtk_widget_class_install_style_property (widget_class,
208 g_param_spec_int ("content-area-spacing",
209 P_("Content area spacing"),
210 P_("Spacing between elements of the main dialog area"),
214 GTK_PARAM_READABLE));
215 gtk_widget_class_install_style_property (widget_class,
216 g_param_spec_int ("button-spacing",
217 P_("Button spacing"),
218 P_("Spacing between buttons"),
222 GTK_PARAM_READABLE));
224 gtk_widget_class_install_style_property (widget_class,
225 g_param_spec_int ("action-area-border",
226 P_("Action area border"),
227 P_("Width of border around the button area at the bottom of the dialog"),
231 GTK_PARAM_READABLE));
233 binding_set = gtk_binding_set_by_class (class);
235 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0, "close", 0);
239 update_spacings (GtkDialog *dialog)
241 gint content_area_border;
242 gint content_area_spacing;
244 gint action_area_border;
246 gtk_widget_style_get (GTK_WIDGET (dialog),
247 "content-area-border", &content_area_border,
248 "content-area-spacing", &content_area_spacing,
249 "button-spacing", &button_spacing,
250 "action-area-border", &action_area_border,
253 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
254 content_area_border);
255 if (!_gtk_box_get_spacing_set (GTK_BOX (dialog->vbox)))
257 gtk_box_set_spacing (GTK_BOX (dialog->vbox), content_area_spacing);
258 _gtk_box_set_spacing_set (GTK_BOX (dialog->vbox), FALSE);
260 gtk_box_set_spacing (GTK_BOX (dialog->action_area),
262 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
267 gtk_dialog_init (GtkDialog *dialog)
269 GtkDialogPrivate *priv;
271 priv = GET_PRIVATE (dialog);
272 priv->ignore_separator = FALSE;
274 /* To avoid breaking old code that prevents destroy on delete event
275 * by connecting a handler, we have to have the FIRST signal
276 * connection on the dialog.
278 g_signal_connect (dialog,
280 G_CALLBACK (gtk_dialog_delete_event_handler),
283 dialog->vbox = gtk_vbox_new (FALSE, 0);
285 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
286 gtk_widget_show (dialog->vbox);
288 dialog->action_area = gtk_hbutton_box_new ();
290 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
293 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
295 gtk_widget_show (dialog->action_area);
297 dialog->separator = gtk_hseparator_new ();
298 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
299 gtk_widget_show (dialog->separator);
301 gtk_window_set_type_hint (GTK_WINDOW (dialog),
302 GDK_WINDOW_TYPE_HINT_DIALOG);
303 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
306 static GtkBuildableIface *parent_buildable_iface;
309 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
311 parent_buildable_iface = g_type_interface_peek_parent (iface);
312 iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
313 iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
314 iface->custom_finished = gtk_dialog_buildable_custom_finished;
318 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
320 const gchar *childname)
322 if (strcmp (childname, "vbox") == 0)
323 return G_OBJECT (GTK_DIALOG (buildable)->vbox);
324 else if (strcmp (childname, "action_area") == 0)
325 return G_OBJECT (GTK_DIALOG (buildable)->action_area);
327 return parent_buildable_iface->get_internal_child (buildable,
333 gtk_dialog_set_property (GObject *object,
340 dialog = GTK_DIALOG (object);
344 case PROP_HAS_SEPARATOR:
345 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
349 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
355 gtk_dialog_get_property (GObject *object,
362 dialog = GTK_DIALOG (object);
366 case PROP_HAS_SEPARATOR:
367 g_value_set_boolean (value, dialog->separator != NULL);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 gtk_dialog_delete_event_handler (GtkWidget *widget,
381 /* emit response signal */
382 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
384 /* Do the destroy by default */
388 /* A far too tricky heuristic for getting the right initial
389 * focus widget if none was set. What we do is we focus the first
390 * widget in the tab chain, but if this results in the focus
391 * ending up on one of the response widgets _other_ than the
392 * default response, we focus the default response instead.
394 * Additionally, skip selectable labels when looking for the
395 * right initial focus widget.
398 gtk_dialog_map (GtkWidget *widget)
400 GtkWindow *window = GTK_WINDOW (widget);
401 GtkDialog *dialog = GTK_DIALOG (widget);
403 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
405 if (!window->focus_widget)
407 GList *children, *tmp_list;
408 GtkWidget *first_focus = NULL;
412 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
414 if (first_focus == NULL)
415 first_focus = window->focus_widget;
416 else if (first_focus == window->focus_widget)
418 if (!GTK_IS_LABEL (window->focus_widget))
420 if (!gtk_label_get_current_uri (GTK_LABEL (window->focus_widget)))
421 gtk_label_select_region (GTK_LABEL (window->focus_widget), 0, 0);
425 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
429 GtkWidget *child = tmp_list->data;
431 if ((window->focus_widget == NULL ||
432 child == window->focus_widget) &&
433 child != window->default_widget &&
434 window->default_widget)
436 gtk_widget_grab_focus (window->default_widget);
440 tmp_list = tmp_list->next;
443 g_list_free (children);
448 gtk_dialog_style_set (GtkWidget *widget,
449 GtkStyle *prev_style)
451 update_spacings (GTK_DIALOG (widget));
455 dialog_find_button (GtkDialog *dialog,
458 GList *children, *tmp_list;
459 GtkWidget *child = NULL;
461 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
463 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
465 ResponseData *rd = get_response_data (tmp_list->data, FALSE);
467 if (rd && rd->response_id == response_id)
469 child = tmp_list->data;
474 g_list_free (children);
480 gtk_dialog_close (GtkDialog *dialog)
482 /* Synthesize delete_event to close dialog. */
484 GtkWidget *widget = GTK_WIDGET (dialog);
487 event = gdk_event_new (GDK_DELETE);
489 event->any.window = g_object_ref (widget->window);
490 event->any.send_event = TRUE;
492 gtk_main_do_event (event);
493 gdk_event_free (event);
497 gtk_dialog_new (void)
499 return g_object_new (GTK_TYPE_DIALOG, NULL);
503 gtk_dialog_new_empty (const gchar *title,
505 GtkDialogFlags flags)
509 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
512 gtk_window_set_title (GTK_WINDOW (dialog), title);
515 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
517 if (flags & GTK_DIALOG_MODAL)
518 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
520 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
521 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
523 if (flags & GTK_DIALOG_NO_SEPARATOR)
524 gtk_dialog_set_has_separator (dialog, FALSE);
526 return GTK_WIDGET (dialog);
530 * gtk_dialog_new_with_buttons:
531 * @title: (allow-none): Title of the dialog, or %NULL
532 * @parent: (allow-none): Transient parent of the dialog, or %NULL
533 * @flags: from #GtkDialogFlags
534 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
535 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
537 * Creates a new #GtkDialog with title @title (or %NULL for the default
538 * title; see gtk_window_set_title()) and transient parent @parent (or
539 * %NULL for none; see gtk_window_set_transient_for()). The @flags
540 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
541 * and/or to have it destroyed along with its transient parent
542 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
543 * text/response ID pairs should be listed, with a %NULL pointer ending
544 * the list. Button text can be either a stock ID such as
545 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
546 * any positive number, or one of the values in the #GtkResponseType
547 * enumeration. If the user clicks one of these dialog buttons,
548 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
549 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
550 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
551 * However, destroying a dialog does not emit the ::response signal;
552 * so be careful relying on ::response when using the
553 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
554 * so the first button in the list will be the leftmost button in the dialog.
556 * Here's a simple example:
558 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
560 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
562 * GTK_RESPONSE_ACCEPT,
564 * GTK_RESPONSE_REJECT,
568 * Return value: a new #GtkDialog
571 gtk_dialog_new_with_buttons (const gchar *title,
573 GtkDialogFlags flags,
574 const gchar *first_button_text,
580 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
582 va_start (args, first_button_text);
584 gtk_dialog_add_buttons_valist (dialog,
590 return GTK_WIDGET (dialog);
594 response_data_free (gpointer data)
596 g_slice_free (ResponseData, data);
600 get_response_data (GtkWidget *widget,
603 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
604 "gtk-dialog-response-data");
606 if (ad == NULL && create)
608 ad = g_slice_new (ResponseData);
610 g_object_set_data_full (G_OBJECT (widget),
611 I_("gtk-dialog-response-data"),
620 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
624 response_id = gtk_dialog_get_response_for_widget (dialog, widget);
626 gtk_dialog_response (dialog, response_id);
630 * gtk_dialog_add_action_widget:
631 * @dialog: a #GtkDialog
632 * @child: an activatable widget
633 * @response_id: response ID for @child
635 * Adds an activatable widget to the action area of a #GtkDialog,
636 * connecting a signal handler that will emit the #GtkDialog::response
637 * signal on the dialog when the widget is activated. The widget is
638 * appended to the end of the dialog's action area. If you want to add a
639 * non-activatable widget, simply pack it into the @action_area field
640 * of the #GtkDialog struct.
643 gtk_dialog_add_action_widget (GtkDialog *dialog,
650 g_return_if_fail (GTK_IS_DIALOG (dialog));
651 g_return_if_fail (GTK_IS_WIDGET (child));
653 ad = get_response_data (child, TRUE);
655 ad->response_id = response_id;
657 if (GTK_IS_BUTTON (child))
658 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
660 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
666 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
668 g_signal_connect_closure_by_id (child,
675 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
677 gtk_box_pack_end (GTK_BOX (dialog->action_area),
681 if (response_id == GTK_RESPONSE_HELP)
682 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
686 * gtk_dialog_add_button:
687 * @dialog: a #GtkDialog
688 * @button_text: text of button, or stock ID
689 * @response_id: response ID for the button
691 * Adds a button with the given text (or a stock button, if @button_text is a
692 * stock ID) and sets things up so that clicking the button will emit the
693 * #GtkDialog::response signal with the given @response_id. The button is
694 * appended to the end of the dialog's action area. The button widget is
695 * returned, but usually you don't need it.
697 * Return value: the button widget that was added
700 gtk_dialog_add_button (GtkDialog *dialog,
701 const gchar *button_text,
706 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
707 g_return_val_if_fail (button_text != NULL, NULL);
709 button = gtk_button_new_from_stock (button_text);
711 gtk_widget_set_can_default (button, TRUE);
713 gtk_widget_show (button);
715 gtk_dialog_add_action_widget (dialog,
723 gtk_dialog_add_buttons_valist (GtkDialog *dialog,
724 const gchar *first_button_text,
730 g_return_if_fail (GTK_IS_DIALOG (dialog));
732 if (first_button_text == NULL)
735 text = first_button_text;
736 response_id = va_arg (args, gint);
740 gtk_dialog_add_button (dialog, text, response_id);
742 text = va_arg (args, gchar*);
745 response_id = va_arg (args, int);
750 * gtk_dialog_add_buttons:
751 * @dialog: a #GtkDialog
752 * @first_button_text: button text or stock ID
753 * @Varargs: response ID for first button, then more text-response_id pairs
755 * Adds more buttons, same as calling gtk_dialog_add_button()
756 * repeatedly. The variable argument list should be %NULL-terminated
757 * as with gtk_dialog_new_with_buttons(). Each button must have both
758 * text and response ID.
761 gtk_dialog_add_buttons (GtkDialog *dialog,
762 const gchar *first_button_text,
767 va_start (args, first_button_text);
769 gtk_dialog_add_buttons_valist (dialog,
777 * gtk_dialog_set_response_sensitive:
778 * @dialog: a #GtkDialog
779 * @response_id: a response ID
780 * @setting: %TRUE for sensitive
782 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
783 * for each widget in the dialog's action area with the given @response_id.
784 * A convenient way to sensitize/desensitize dialog buttons.
787 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
794 g_return_if_fail (GTK_IS_DIALOG (dialog));
796 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
799 while (tmp_list != NULL)
801 GtkWidget *widget = tmp_list->data;
802 ResponseData *rd = get_response_data (widget, FALSE);
804 if (rd && rd->response_id == response_id)
805 gtk_widget_set_sensitive (widget, setting);
807 tmp_list = g_list_next (tmp_list);
810 g_list_free (children);
814 * gtk_dialog_set_default_response:
815 * @dialog: a #GtkDialog
816 * @response_id: a response ID
818 * Sets the last widget in the dialog's action area with the given @response_id
819 * as the default widget for the dialog. Pressing "Enter" normally activates
820 * the default widget.
823 gtk_dialog_set_default_response (GtkDialog *dialog,
829 g_return_if_fail (GTK_IS_DIALOG (dialog));
831 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
834 while (tmp_list != NULL)
836 GtkWidget *widget = tmp_list->data;
837 ResponseData *rd = get_response_data (widget, FALSE);
839 if (rd && rd->response_id == response_id)
840 gtk_widget_grab_default (widget);
842 tmp_list = g_list_next (tmp_list);
845 g_list_free (children);
849 * gtk_dialog_set_has_separator:
850 * @dialog: a #GtkDialog
851 * @setting: %TRUE to have a separator
853 * Sets whether the dialog has a separator above the buttons.
857 gtk_dialog_set_has_separator (GtkDialog *dialog,
860 GtkDialogPrivate *priv;
862 g_return_if_fail (GTK_IS_DIALOG (dialog));
864 priv = GET_PRIVATE (dialog);
866 /* this might fail if we get called before _init() somehow */
867 g_assert (dialog->vbox != NULL);
869 if (priv->ignore_separator)
871 g_warning ("Ignoring the separator setting");
875 if (setting && dialog->separator == NULL)
877 dialog->separator = gtk_hseparator_new ();
878 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
880 /* The app programmer could screw this up, but, their own fault.
881 * Moves the separator just above the action area.
883 gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
884 gtk_widget_show (dialog->separator);
886 else if (!setting && dialog->separator != NULL)
888 gtk_widget_destroy (dialog->separator);
889 dialog->separator = NULL;
892 g_object_notify (G_OBJECT (dialog), "has-separator");
896 * gtk_dialog_get_has_separator:
897 * @dialog: a #GtkDialog
899 * Accessor for whether the dialog has a separator.
901 * Return value: %TRUE if the dialog has a separator
904 gtk_dialog_get_has_separator (GtkDialog *dialog)
906 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
908 return dialog->separator != NULL;
912 * gtk_dialog_response:
913 * @dialog: a #GtkDialog
914 * @response_id: response ID
916 * Emits the #GtkDialog::response signal with the given response ID.
917 * Used to indicate that the user has responded to the dialog in some way;
918 * typically either you or gtk_dialog_run() will be monitoring the
919 * ::response signal and take appropriate action.
922 gtk_dialog_response (GtkDialog *dialog,
925 g_return_if_fail (GTK_IS_DIALOG (dialog));
927 g_signal_emit (dialog,
928 dialog_signals[RESPONSE],
942 shutdown_loop (RunInfo *ri)
944 if (g_main_loop_is_running (ri->loop))
945 g_main_loop_quit (ri->loop);
949 run_unmap_handler (GtkDialog *dialog, gpointer data)
957 run_response_handler (GtkDialog *dialog,
965 ri->response_id = response_id;
971 run_delete_handler (GtkDialog *dialog,
979 return TRUE; /* Do not destroy */
983 run_destroy_handler (GtkDialog *dialog, gpointer data)
987 /* shutdown_loop will be called by run_unmap_handler */
989 ri->destroyed = TRUE;
994 * @dialog: a #GtkDialog
996 * Blocks in a recursive main loop until the @dialog either emits the
997 * #GtkDialog::response signal, or is destroyed. If the dialog is
998 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
999 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
1000 * ::response signal emission.
1002 * Before entering the recursive main loop, gtk_dialog_run() calls
1003 * gtk_widget_show() on the dialog for you. Note that you still
1004 * need to show any children of the dialog yourself.
1006 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1007 * is disabled; if the dialog receives ::delete_event, it will not be
1008 * destroyed as windows usually are, and gtk_dialog_run() will return
1009 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1010 * will be modal. You can force gtk_dialog_run() to return at any time by
1011 * calling gtk_dialog_response() to emit the ::response signal. Destroying
1012 * the dialog during gtk_dialog_run() is a very bad idea, because your
1013 * post-run code won't know whether the dialog was destroyed or not.
1015 * After gtk_dialog_run() returns, you are responsible for hiding or
1016 * destroying the dialog if you wish to do so.
1018 * Typical usage of this function might be:
1020 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1023 * case GTK_RESPONSE_ACCEPT:
1024 * do_application_specific_something ();
1027 * do_nothing_since_dialog_was_cancelled ();
1030 * gtk_widget_destroy (dialog);
1033 * Note that even though the recursive main loop gives the effect of a
1034 * modal dialog (it prevents the user from interacting with other
1035 * windows in the same window group while the dialog is run), callbacks
1036 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1037 * be triggered during a gtk_dialog_run() call.
1039 * Return value: response ID
1042 gtk_dialog_run (GtkDialog *dialog)
1044 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1046 gulong response_handler;
1047 gulong unmap_handler;
1048 gulong destroy_handler;
1049 gulong delete_handler;
1051 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1053 g_object_ref (dialog);
1055 was_modal = GTK_WINDOW (dialog)->modal;
1057 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1059 if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1060 gtk_widget_show (GTK_WIDGET (dialog));
1063 g_signal_connect (dialog,
1065 G_CALLBACK (run_response_handler),
1069 g_signal_connect (dialog,
1071 G_CALLBACK (run_unmap_handler),
1075 g_signal_connect (dialog,
1077 G_CALLBACK (run_delete_handler),
1081 g_signal_connect (dialog,
1083 G_CALLBACK (run_destroy_handler),
1086 ri.loop = g_main_loop_new (NULL, FALSE);
1088 GDK_THREADS_LEAVE ();
1089 g_main_loop_run (ri.loop);
1090 GDK_THREADS_ENTER ();
1092 g_main_loop_unref (ri.loop);
1099 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1101 g_signal_handler_disconnect (dialog, response_handler);
1102 g_signal_handler_disconnect (dialog, unmap_handler);
1103 g_signal_handler_disconnect (dialog, delete_handler);
1104 g_signal_handler_disconnect (dialog, destroy_handler);
1107 g_object_unref (dialog);
1109 return ri.response_id;
1113 _gtk_dialog_set_ignore_separator (GtkDialog *dialog,
1114 gboolean ignore_separator)
1116 GtkDialogPrivate *priv;
1118 priv = GET_PRIVATE (dialog);
1119 priv->ignore_separator = ignore_separator;
1123 * gtk_dialog_get_widget_for_response:
1124 * @dialog: a #GtkDialog
1125 * @response_id: the response ID used by the @dialog widget
1127 * Gets the widget button that uses the given response ID in the action area
1130 * Returns: the @widget button that uses the given @response_id, or %NULL.
1135 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1141 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1143 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
1145 tmp_list = children;
1146 while (tmp_list != NULL)
1148 GtkWidget *widget = tmp_list->data;
1149 ResponseData *rd = get_response_data (widget, FALSE);
1151 if (rd && rd->response_id == response_id)
1153 g_list_free (children);
1157 tmp_list = g_list_next (tmp_list);
1160 g_list_free (children);
1166 * gtk_dialog_get_response_for_widget:
1167 * @dialog: a #GtkDialog
1168 * @widget: a widget in the action area of @dialog
1170 * Gets the response id of a widget in the action area
1173 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1174 * if @widget doesn't have a response id set.
1179 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1184 rd = get_response_data (widget, FALSE);
1186 return GTK_RESPONSE_NONE;
1188 return rd->response_id;
1192 * gtk_alternative_dialog_button_order:
1193 * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1195 * Returns %TRUE if dialogs are expected to use an alternative
1196 * button order on the screen @screen. See
1197 * gtk_dialog_set_alternative_button_order() for more details
1198 * about alternative button order.
1200 * If you need to use this function, you should probably connect
1201 * to the ::notify:gtk-alternative-button-order signal on the
1202 * #GtkSettings object associated to @screen, in order to be
1203 * notified if the button order setting changes.
1205 * Returns: Whether the alternative button order should be used
1210 gtk_alternative_dialog_button_order (GdkScreen *screen)
1212 GtkSettings *settings;
1216 settings = gtk_settings_get_for_screen (screen);
1218 settings = gtk_settings_get_default ();
1220 g_object_get (settings,
1221 "gtk-alternative-button-order", &result, NULL);
1227 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1228 gint first_response_id,
1235 response_id = first_response_id;
1237 while (response_id != -1)
1239 /* reorder child with response_id to position */
1240 child = dialog_find_button (dialog, response_id);
1241 gtk_box_reorder_child (GTK_BOX (dialog->action_area), child, position);
1243 response_id = va_arg (args, gint);
1249 * gtk_dialog_set_alternative_button_order:
1250 * @dialog: a #GtkDialog
1251 * @first_response_id: a response id used by one @dialog's buttons
1252 * @Varargs: a list of more response ids of @dialog's buttons, terminated by -1
1254 * Sets an alternative button order. If the
1255 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1256 * the dialog buttons are reordered according to the order of the
1257 * response ids passed to this function.
1259 * By default, GTK+ dialogs use the button order advocated by the Gnome
1260 * <ulink url="http://developer.gnome.org/projects/gup/hig/2.0/">Human
1261 * Interface Guidelines</ulink> with the affirmative button at the far
1262 * right, and the cancel button left of it. But the builtin GTK+ dialogs
1263 * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1264 * which is more suitable on some platforms, e.g. Windows.
1266 * Use this function after adding all the buttons to your dialog, as the
1267 * following example shows:
1269 * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1271 * GTK_RESPONSE_CANCEL);
1273 * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1277 * gtk_widget_grab_default (ok_button);
1279 * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1281 * GTK_RESPONSE_HELP);
1283 * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1285 * GTK_RESPONSE_CANCEL,
1286 * GTK_RESPONSE_HELP,
1293 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1294 gint first_response_id,
1300 g_return_if_fail (GTK_IS_DIALOG (dialog));
1302 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1303 if (!gtk_alternative_dialog_button_order (screen))
1306 va_start (args, first_response_id);
1308 gtk_dialog_set_alternative_button_order_valist (dialog,
1314 * gtk_dialog_set_alternative_button_order_from_array:
1315 * @dialog: a #GtkDialog
1316 * @n_params: the number of response ids in @new_order
1317 * @new_order: an array of response ids of @dialog's buttons
1319 * Sets an alternative button order. If the
1320 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1321 * the dialog buttons are reordered according to the order of the
1322 * response ids in @new_order.
1324 * See gtk_dialog_set_alternative_button_order() for more information.
1326 * This function is for use by language bindings.
1331 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1339 g_return_if_fail (GTK_IS_DIALOG (dialog));
1340 g_return_if_fail (new_order != NULL);
1342 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1343 if (!gtk_alternative_dialog_button_order (screen))
1346 for (position = 0; position < n_params; position++)
1348 /* reorder child with response_id to position */
1349 child = dialog_find_button (dialog, new_order[position]);
1350 gtk_box_reorder_child (GTK_BOX (dialog->action_area), child, position);
1361 GtkBuilder *builder;
1364 } ActionWidgetsSubParserData;
1367 attributes_start_element (GMarkupParseContext *context,
1368 const gchar *element_name,
1369 const gchar **names,
1370 const gchar **values,
1374 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1377 if (strcmp (element_name, "action-widget") == 0)
1379 for (i = 0; names[i]; i++)
1380 if (strcmp (names[i], "response") == 0)
1381 parser_data->response = g_strdup (values[i]);
1383 else if (strcmp (element_name, "action-widgets") == 0)
1386 g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1390 attributes_text_element (GMarkupParseContext *context,
1396 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1397 ActionWidgetInfo *item;
1399 if (!parser_data->response)
1402 item = g_new (ActionWidgetInfo, 1);
1403 item->widget_name = g_strndup (text, text_len);
1404 item->response_id = parser_data->response;
1405 parser_data->items = g_slist_prepend (parser_data->items, item);
1406 parser_data->response = NULL;
1409 static const GMarkupParser attributes_parser =
1411 attributes_start_element,
1413 attributes_text_element,
1417 gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
1418 GtkBuilder *builder,
1420 const gchar *tagname,
1421 GMarkupParser *parser,
1424 ActionWidgetsSubParserData *parser_data;
1429 if (strcmp (tagname, "action-widgets") == 0)
1431 parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1432 parser_data->dialog = GTK_DIALOG (buildable);
1433 parser_data->items = NULL;
1435 *parser = attributes_parser;
1436 *data = parser_data;
1440 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1441 tagname, parser, data);
1445 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1446 GtkBuilder *builder,
1448 const gchar *tagname,
1452 ActionWidgetsSubParserData *parser_data;
1458 if (strcmp (tagname, "action-widgets"))
1460 parent_buildable_iface->custom_finished (buildable, builder, child,
1461 tagname, user_data);
1465 dialog = GTK_DIALOG (buildable);
1466 parser_data = (ActionWidgetsSubParserData*)user_data;
1467 parser_data->items = g_slist_reverse (parser_data->items);
1469 for (l = parser_data->items; l; l = l->next)
1471 ActionWidgetInfo *item = l->data;
1473 object = gtk_builder_get_object (builder, item->widget_name);
1476 g_warning ("Unknown object %s specified in action-widgets of %s",
1478 gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1482 ad = get_response_data (GTK_WIDGET (object), TRUE);
1483 ad->response_id = atoi (item->response_id);
1485 if (GTK_IS_BUTTON (object))
1486 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1488 signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1494 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1496 g_signal_connect_closure_by_id (object,
1503 if (ad->response_id == GTK_RESPONSE_HELP)
1504 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area),
1505 GTK_WIDGET (object), TRUE);
1507 g_free (item->widget_name);
1508 g_free (item->response_id);
1511 g_slist_free (parser_data->items);
1512 g_slice_free (ActionWidgetsSubParserData, parser_data);
1516 * gtk_dialog_get_action_area:
1517 * @dialog: a #GtkDialog
1519 * Returns the action area of @dialog.
1521 * Returns: (transfer none): the action area.
1526 gtk_dialog_get_action_area (GtkDialog *dialog)
1528 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1530 return dialog->action_area;
1534 * gtk_dialog_get_content_area:
1535 * @dialog: a #GtkDialog
1537 * Returns the content area of @dialog.
1539 * Returns: (transfer none): the content area #GtkVBox.
1544 gtk_dialog_get_content_area (GtkDialog *dialog)
1546 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1548 return dialog->vbox;
1551 #define __GTK_DIALOG_C__
1552 #include "gtkaliasdef.c"