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"
48 GtkWidget *action_area;
52 guint ignore_separator : 1;
55 typedef struct _ResponseData ResponseData;
62 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
63 const gchar *first_button_text,
66 static gboolean gtk_dialog_delete_event_handler (GtkWidget *widget,
70 static void gtk_dialog_set_property (GObject *object,
74 static void gtk_dialog_get_property (GObject *object,
78 static void gtk_dialog_style_set (GtkWidget *widget,
79 GtkStyle *prev_style);
80 static void gtk_dialog_map (GtkWidget *widget);
82 static void gtk_dialog_close (GtkDialog *dialog);
84 static ResponseData * get_response_data (GtkWidget *widget,
87 static void gtk_dialog_buildable_interface_init (GtkBuildableIface *iface);
88 static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
90 const gchar *childname);
91 static gboolean gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
95 GMarkupParser *parser,
97 static void gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
100 const gchar *tagname,
115 static guint dialog_signals[LAST_SIGNAL];
117 G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
118 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
119 gtk_dialog_buildable_interface_init))
122 gtk_dialog_class_init (GtkDialogClass *class)
124 GObjectClass *gobject_class;
125 GtkWidgetClass *widget_class;
126 GtkBindingSet *binding_set;
128 gobject_class = G_OBJECT_CLASS (class);
129 widget_class = GTK_WIDGET_CLASS (class);
131 gobject_class->set_property = gtk_dialog_set_property;
132 gobject_class->get_property = gtk_dialog_get_property;
134 widget_class->map = gtk_dialog_map;
135 widget_class->style_set = gtk_dialog_style_set;
137 class->close = gtk_dialog_close;
139 g_type_class_add_private (gobject_class, sizeof (GtkDialogPriv));
142 * GtkDialog:has-separator:
144 * When %TRUE, the dialog has a separator bar above its buttons.
146 g_object_class_install_property (gobject_class,
148 g_param_spec_boolean ("has-separator",
150 P_("The dialog has a separator bar above its buttons"),
152 GTK_PARAM_READWRITE));
155 * GtkDialog::response:
156 * @dialog: the object on which the signal is emitted
157 * @response_id: the response ID
159 * Emitted when an action widget is clicked, the dialog receives a
160 * delete event, or the application programmer calls gtk_dialog_response().
161 * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
162 * Otherwise, it depends on which action widget was clicked.
164 dialog_signals[RESPONSE] =
165 g_signal_new (I_("response"),
166 G_OBJECT_CLASS_TYPE (class),
168 G_STRUCT_OFFSET (GtkDialogClass, response),
170 _gtk_marshal_VOID__INT,
177 * The ::close signal is a
178 * <link linkend="keybinding-signals">keybinding signal</link>
179 * which gets emitted when the user uses a keybinding to close
182 * The default binding for this signal is the Escape key.
184 dialog_signals[CLOSE] =
185 g_signal_new (I_("close"),
186 G_OBJECT_CLASS_TYPE (class),
187 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
188 G_STRUCT_OFFSET (GtkDialogClass, close),
190 _gtk_marshal_VOID__VOID,
193 gtk_widget_class_install_style_property (widget_class,
194 g_param_spec_int ("content-area-border",
195 P_("Content area border"),
196 P_("Width of border around the main dialog area"),
200 GTK_PARAM_READABLE));
202 * GtkDialog:content-area-spacing:
204 * The default spacing used between elements of the
205 * content area of the dialog, as returned by
206 * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
207 * was called on that widget directly.
211 gtk_widget_class_install_style_property (widget_class,
212 g_param_spec_int ("content-area-spacing",
213 P_("Content area spacing"),
214 P_("Spacing between elements of the main dialog area"),
218 GTK_PARAM_READABLE));
219 gtk_widget_class_install_style_property (widget_class,
220 g_param_spec_int ("button-spacing",
221 P_("Button spacing"),
222 P_("Spacing between buttons"),
226 GTK_PARAM_READABLE));
228 gtk_widget_class_install_style_property (widget_class,
229 g_param_spec_int ("action-area-border",
230 P_("Action area border"),
231 P_("Width of border around the button area at the bottom of the dialog"),
235 GTK_PARAM_READABLE));
237 binding_set = gtk_binding_set_by_class (class);
239 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0, "close", 0);
243 update_spacings (GtkDialog *dialog)
245 GtkDialogPriv *priv = dialog->priv;
246 gint content_area_border;
247 gint content_area_spacing;
249 gint action_area_border;
251 gtk_widget_style_get (GTK_WIDGET (dialog),
252 "content-area-border", &content_area_border,
253 "content-area-spacing", &content_area_spacing,
254 "button-spacing", &button_spacing,
255 "action-area-border", &action_area_border,
258 gtk_container_set_border_width (GTK_CONTAINER (priv->vbox),
259 content_area_border);
260 if (!_gtk_box_get_spacing_set (GTK_BOX (priv->vbox)))
262 gtk_box_set_spacing (GTK_BOX (priv->vbox), content_area_spacing);
263 _gtk_box_set_spacing_set (GTK_BOX (priv->vbox), FALSE);
265 gtk_box_set_spacing (GTK_BOX (priv->action_area),
267 gtk_container_set_border_width (GTK_CONTAINER (priv->action_area),
272 gtk_dialog_init (GtkDialog *dialog)
276 dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
281 priv->ignore_separator = FALSE;
283 /* To avoid breaking old code that prevents destroy on delete event
284 * by connecting a handler, we have to have the FIRST signal
285 * connection on the dialog.
287 g_signal_connect (dialog,
289 G_CALLBACK (gtk_dialog_delete_event_handler),
292 priv->vbox = gtk_vbox_new (FALSE, 0);
294 gtk_container_add (GTK_CONTAINER (dialog), priv->vbox);
295 gtk_widget_show (priv->vbox);
297 priv->action_area = gtk_hbutton_box_new ();
299 gtk_button_box_set_layout (GTK_BUTTON_BOX (priv->action_area),
302 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->action_area,
304 gtk_widget_show (priv->action_area);
306 priv->separator = gtk_hseparator_new ();
307 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->separator, FALSE, TRUE, 0);
308 gtk_widget_show (priv->separator);
310 gtk_window_set_type_hint (GTK_WINDOW (dialog),
311 GDK_WINDOW_TYPE_HINT_DIALOG);
312 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
315 static GtkBuildableIface *parent_buildable_iface;
318 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
320 parent_buildable_iface = g_type_interface_peek_parent (iface);
321 iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
322 iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
323 iface->custom_finished = gtk_dialog_buildable_custom_finished;
327 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
329 const gchar *childname)
331 GtkDialogPriv *priv = GTK_DIALOG (buildable)->priv;
333 if (strcmp (childname, "vbox") == 0)
334 return G_OBJECT (priv->vbox);
335 else if (strcmp (childname, "action_area") == 0)
336 return G_OBJECT (priv->action_area);
338 return parent_buildable_iface->get_internal_child (buildable,
344 gtk_dialog_set_property (GObject *object,
351 dialog = GTK_DIALOG (object);
355 case PROP_HAS_SEPARATOR:
356 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gtk_dialog_get_property (GObject *object,
371 GtkDialog *dialog = GTK_DIALOG (object);
372 GtkDialogPriv *priv = dialog->priv;
376 case PROP_HAS_SEPARATOR:
377 g_value_set_boolean (value, priv->separator != NULL);
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 gtk_dialog_delete_event_handler (GtkWidget *widget,
391 /* emit response signal */
392 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
394 /* Do the destroy by default */
398 /* A far too tricky heuristic for getting the right initial
399 * focus widget if none was set. What we do is we focus the first
400 * widget in the tab chain, but if this results in the focus
401 * ending up on one of the response widgets _other_ than the
402 * default response, we focus the default response instead.
404 * Additionally, skip selectable labels when looking for the
405 * right initial focus widget.
408 gtk_dialog_map (GtkWidget *widget)
410 GtkWidget *default_widget, *focus;
411 GtkWindow *window = GTK_WINDOW (widget);
412 GtkDialog *dialog = GTK_DIALOG (widget);
413 GtkDialogPriv *priv = dialog->priv;
415 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
417 focus = gtk_window_get_focus (window);
420 GList *children, *tmp_list;
421 GtkWidget *first_focus = NULL;
425 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
427 focus = gtk_window_get_focus (window);
428 if (first_focus == NULL)
430 else if (first_focus == focus)
432 if (!GTK_IS_LABEL (focus))
434 if (!gtk_label_get_current_uri (GTK_LABEL (focus)))
435 gtk_label_select_region (GTK_LABEL (focus), 0, 0);
439 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
443 GtkWidget *child = tmp_list->data;
445 default_widget = gtk_window_get_default_widget (window);
446 if ((focus == NULL || child == focus) &&
447 child != default_widget &&
450 gtk_widget_grab_focus (default_widget);
454 tmp_list = tmp_list->next;
457 g_list_free (children);
462 gtk_dialog_style_set (GtkWidget *widget,
463 GtkStyle *prev_style)
465 update_spacings (GTK_DIALOG (widget));
469 dialog_find_button (GtkDialog *dialog,
472 GtkDialogPriv *priv = dialog->priv;
473 GtkWidget *child = NULL;
474 GList *children, *tmp_list;
476 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
478 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
480 ResponseData *rd = get_response_data (tmp_list->data, FALSE);
482 if (rd && rd->response_id == response_id)
484 child = tmp_list->data;
489 g_list_free (children);
495 gtk_dialog_close (GtkDialog *dialog)
497 /* Synthesize delete_event to close dialog. */
499 GtkWidget *widget = GTK_WIDGET (dialog);
502 event = gdk_event_new (GDK_DELETE);
504 event->any.window = g_object_ref (gtk_widget_get_window (widget));
505 event->any.send_event = TRUE;
507 gtk_main_do_event (event);
508 gdk_event_free (event);
512 gtk_dialog_new (void)
514 return g_object_new (GTK_TYPE_DIALOG, NULL);
518 gtk_dialog_new_empty (const gchar *title,
520 GtkDialogFlags flags)
524 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
527 gtk_window_set_title (GTK_WINDOW (dialog), title);
530 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
532 if (flags & GTK_DIALOG_MODAL)
533 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
535 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
536 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
538 if (flags & GTK_DIALOG_NO_SEPARATOR)
539 gtk_dialog_set_has_separator (dialog, FALSE);
541 return GTK_WIDGET (dialog);
545 * gtk_dialog_new_with_buttons:
546 * @title: (allow-none): Title of the dialog, or %NULL
547 * @parent: (allow-none): Transient parent of the dialog, or %NULL
548 * @flags: from #GtkDialogFlags
549 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
550 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
552 * Creates a new #GtkDialog with title @title (or %NULL for the default
553 * title; see gtk_window_set_title()) and transient parent @parent (or
554 * %NULL for none; see gtk_window_set_transient_for()). The @flags
555 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
556 * and/or to have it destroyed along with its transient parent
557 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
558 * text/response ID pairs should be listed, with a %NULL pointer ending
559 * the list. Button text can be either a stock ID such as
560 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
561 * any positive number, or one of the values in the #GtkResponseType
562 * enumeration. If the user clicks one of these dialog buttons,
563 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
564 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
565 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
566 * However, destroying a dialog does not emit the ::response signal;
567 * so be careful relying on ::response when using the
568 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
569 * so the first button in the list will be the leftmost button in the dialog.
571 * Here's a simple example:
573 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
575 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
577 * GTK_RESPONSE_ACCEPT,
579 * GTK_RESPONSE_REJECT,
583 * Return value: a new #GtkDialog
586 gtk_dialog_new_with_buttons (const gchar *title,
588 GtkDialogFlags flags,
589 const gchar *first_button_text,
595 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
597 va_start (args, first_button_text);
599 gtk_dialog_add_buttons_valist (dialog,
605 return GTK_WIDGET (dialog);
609 response_data_free (gpointer data)
611 g_slice_free (ResponseData, data);
615 get_response_data (GtkWidget *widget,
618 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
619 "gtk-dialog-response-data");
621 if (ad == NULL && create)
623 ad = g_slice_new (ResponseData);
625 g_object_set_data_full (G_OBJECT (widget),
626 I_("gtk-dialog-response-data"),
635 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
639 response_id = gtk_dialog_get_response_for_widget (dialog, widget);
641 gtk_dialog_response (dialog, response_id);
645 * gtk_dialog_add_action_widget:
646 * @dialog: a #GtkDialog
647 * @child: an activatable widget
648 * @response_id: response ID for @child
650 * Adds an activatable widget to the action area of a #GtkDialog,
651 * connecting a signal handler that will emit the #GtkDialog::response
652 * signal on the dialog when the widget is activated. The widget is
653 * appended to the end of the dialog's action area. If you want to add a
654 * non-activatable widget, simply pack it into the @action_area field
655 * of the #GtkDialog struct.
658 gtk_dialog_add_action_widget (GtkDialog *dialog,
666 g_return_if_fail (GTK_IS_DIALOG (dialog));
667 g_return_if_fail (GTK_IS_WIDGET (child));
671 ad = get_response_data (child, TRUE);
673 ad->response_id = response_id;
675 if (GTK_IS_BUTTON (child))
676 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
678 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
684 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
686 g_signal_connect_closure_by_id (child,
693 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
695 gtk_box_pack_end (GTK_BOX (priv->action_area),
699 if (response_id == GTK_RESPONSE_HELP)
700 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
704 * gtk_dialog_add_button:
705 * @dialog: a #GtkDialog
706 * @button_text: text of button, or stock ID
707 * @response_id: response ID for the button
709 * Adds a button with the given text (or a stock button, if @button_text is a
710 * stock ID) and sets things up so that clicking the button will emit the
711 * #GtkDialog::response signal with the given @response_id. The button is
712 * appended to the end of the dialog's action area. The button widget is
713 * returned, but usually you don't need it.
715 * Return value: the button widget that was added
718 gtk_dialog_add_button (GtkDialog *dialog,
719 const gchar *button_text,
724 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
725 g_return_val_if_fail (button_text != NULL, NULL);
727 button = gtk_button_new_from_stock (button_text);
729 gtk_widget_set_can_default (button, TRUE);
731 gtk_widget_show (button);
733 gtk_dialog_add_action_widget (dialog,
741 gtk_dialog_add_buttons_valist (GtkDialog *dialog,
742 const gchar *first_button_text,
748 g_return_if_fail (GTK_IS_DIALOG (dialog));
750 if (first_button_text == NULL)
753 text = first_button_text;
754 response_id = va_arg (args, gint);
758 gtk_dialog_add_button (dialog, text, response_id);
760 text = va_arg (args, gchar*);
763 response_id = va_arg (args, int);
768 * gtk_dialog_add_buttons:
769 * @dialog: a #GtkDialog
770 * @first_button_text: button text or stock ID
771 * @Varargs: response ID for first button, then more text-response_id pairs
773 * Adds more buttons, same as calling gtk_dialog_add_button()
774 * repeatedly. The variable argument list should be %NULL-terminated
775 * as with gtk_dialog_new_with_buttons(). Each button must have both
776 * text and response ID.
779 gtk_dialog_add_buttons (GtkDialog *dialog,
780 const gchar *first_button_text,
785 va_start (args, first_button_text);
787 gtk_dialog_add_buttons_valist (dialog,
795 * gtk_dialog_set_response_sensitive:
796 * @dialog: a #GtkDialog
797 * @response_id: a response ID
798 * @setting: %TRUE for sensitive
800 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
801 * for each widget in the dialog's action area with the given @response_id.
802 * A convenient way to sensitize/desensitize dialog buttons.
805 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
813 g_return_if_fail (GTK_IS_DIALOG (dialog));
817 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
820 while (tmp_list != NULL)
822 GtkWidget *widget = tmp_list->data;
823 ResponseData *rd = get_response_data (widget, FALSE);
825 if (rd && rd->response_id == response_id)
826 gtk_widget_set_sensitive (widget, setting);
828 tmp_list = g_list_next (tmp_list);
831 g_list_free (children);
835 * gtk_dialog_set_default_response:
836 * @dialog: a #GtkDialog
837 * @response_id: a response ID
839 * Sets the last widget in the dialog's action area with the given @response_id
840 * as the default widget for the dialog. Pressing "Enter" normally activates
841 * the default widget.
844 gtk_dialog_set_default_response (GtkDialog *dialog,
851 g_return_if_fail (GTK_IS_DIALOG (dialog));
855 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
858 while (tmp_list != NULL)
860 GtkWidget *widget = tmp_list->data;
861 ResponseData *rd = get_response_data (widget, FALSE);
863 if (rd && rd->response_id == response_id)
864 gtk_widget_grab_default (widget);
866 tmp_list = g_list_next (tmp_list);
869 g_list_free (children);
873 * gtk_dialog_set_has_separator:
874 * @dialog: a #GtkDialog
875 * @setting: %TRUE to have a separator
877 * Sets whether the dialog has a separator above the buttons.
881 gtk_dialog_set_has_separator (GtkDialog *dialog,
886 g_return_if_fail (GTK_IS_DIALOG (dialog));
890 /* this might fail if we get called before _init() somehow */
891 g_assert (priv->vbox != NULL);
893 if (priv->ignore_separator)
895 g_warning ("Ignoring the separator setting");
899 if (setting && priv->separator == NULL)
901 priv->separator = gtk_hseparator_new ();
902 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->separator, FALSE, TRUE, 0);
904 /* The app programmer could screw this up, but, their own fault.
905 * Moves the separator just above the action area.
907 gtk_box_reorder_child (GTK_BOX (priv->vbox), priv->separator, 1);
908 gtk_widget_show (priv->separator);
910 else if (!setting && priv->separator != NULL)
912 gtk_widget_destroy (priv->separator);
913 priv->separator = NULL;
916 g_object_notify (G_OBJECT (dialog), "has-separator");
920 * gtk_dialog_get_has_separator:
921 * @dialog: a #GtkDialog
923 * Accessor for whether the dialog has a separator.
925 * Return value: %TRUE if the dialog has a separator
928 gtk_dialog_get_has_separator (GtkDialog *dialog)
930 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
932 return dialog->priv->separator != NULL;
936 * gtk_dialog_response:
937 * @dialog: a #GtkDialog
938 * @response_id: response ID
940 * Emits the #GtkDialog::response signal with the given response ID.
941 * Used to indicate that the user has responded to the dialog in some way;
942 * typically either you or gtk_dialog_run() will be monitoring the
943 * ::response signal and take appropriate action.
946 gtk_dialog_response (GtkDialog *dialog,
949 g_return_if_fail (GTK_IS_DIALOG (dialog));
951 g_signal_emit (dialog,
952 dialog_signals[RESPONSE],
966 shutdown_loop (RunInfo *ri)
968 if (g_main_loop_is_running (ri->loop))
969 g_main_loop_quit (ri->loop);
973 run_unmap_handler (GtkDialog *dialog, gpointer data)
981 run_response_handler (GtkDialog *dialog,
989 ri->response_id = response_id;
995 run_delete_handler (GtkDialog *dialog,
1003 return TRUE; /* Do not destroy */
1007 run_destroy_handler (GtkDialog *dialog, gpointer data)
1011 /* shutdown_loop will be called by run_unmap_handler */
1013 ri->destroyed = TRUE;
1018 * @dialog: a #GtkDialog
1020 * Blocks in a recursive main loop until the @dialog either emits the
1021 * #GtkDialog::response signal, or is destroyed. If the dialog is
1022 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
1023 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
1024 * ::response signal emission.
1026 * Before entering the recursive main loop, gtk_dialog_run() calls
1027 * gtk_widget_show() on the dialog for you. Note that you still
1028 * need to show any children of the dialog yourself.
1030 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1031 * is disabled; if the dialog receives ::delete_event, it will not be
1032 * destroyed as windows usually are, and gtk_dialog_run() will return
1033 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1034 * will be modal. You can force gtk_dialog_run() to return at any time by
1035 * calling gtk_dialog_response() to emit the ::response signal. Destroying
1036 * the dialog during gtk_dialog_run() is a very bad idea, because your
1037 * post-run code won't know whether the dialog was destroyed or not.
1039 * After gtk_dialog_run() returns, you are responsible for hiding or
1040 * destroying the dialog if you wish to do so.
1042 * Typical usage of this function might be:
1044 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1047 * case GTK_RESPONSE_ACCEPT:
1048 * do_application_specific_something ();
1051 * do_nothing_since_dialog_was_cancelled ();
1054 * gtk_widget_destroy (dialog);
1057 * Note that even though the recursive main loop gives the effect of a
1058 * modal dialog (it prevents the user from interacting with other
1059 * windows in the same window group while the dialog is run), callbacks
1060 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1061 * be triggered during a gtk_dialog_run() call.
1063 * Return value: response ID
1066 gtk_dialog_run (GtkDialog *dialog)
1068 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1070 gulong response_handler;
1071 gulong unmap_handler;
1072 gulong destroy_handler;
1073 gulong delete_handler;
1075 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1077 g_object_ref (dialog);
1079 was_modal = gtk_window_get_modal (GTK_WINDOW (dialog));
1081 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1083 if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1084 gtk_widget_show (GTK_WIDGET (dialog));
1087 g_signal_connect (dialog,
1089 G_CALLBACK (run_response_handler),
1093 g_signal_connect (dialog,
1095 G_CALLBACK (run_unmap_handler),
1099 g_signal_connect (dialog,
1101 G_CALLBACK (run_delete_handler),
1105 g_signal_connect (dialog,
1107 G_CALLBACK (run_destroy_handler),
1110 ri.loop = g_main_loop_new (NULL, FALSE);
1112 GDK_THREADS_LEAVE ();
1113 g_main_loop_run (ri.loop);
1114 GDK_THREADS_ENTER ();
1116 g_main_loop_unref (ri.loop);
1123 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1125 g_signal_handler_disconnect (dialog, response_handler);
1126 g_signal_handler_disconnect (dialog, unmap_handler);
1127 g_signal_handler_disconnect (dialog, delete_handler);
1128 g_signal_handler_disconnect (dialog, destroy_handler);
1131 g_object_unref (dialog);
1133 return ri.response_id;
1137 _gtk_dialog_set_ignore_separator (GtkDialog *dialog,
1138 gboolean ignore_separator)
1140 GtkDialogPriv *priv = dialog->priv;
1142 priv->ignore_separator = ignore_separator;
1146 * gtk_dialog_get_widget_for_response:
1147 * @dialog: a #GtkDialog
1148 * @response_id: the response ID used by the @dialog widget
1150 * Gets the widget button that uses the given response ID in the action area
1153 * Returns: the @widget button that uses the given @response_id, or %NULL.
1158 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1161 GtkDialogPriv *priv;
1165 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1167 priv = dialog->priv;
1169 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
1171 tmp_list = children;
1172 while (tmp_list != NULL)
1174 GtkWidget *widget = tmp_list->data;
1175 ResponseData *rd = get_response_data (widget, FALSE);
1177 if (rd && rd->response_id == response_id)
1179 g_list_free (children);
1183 tmp_list = g_list_next (tmp_list);
1186 g_list_free (children);
1192 * gtk_dialog_get_response_for_widget:
1193 * @dialog: a #GtkDialog
1194 * @widget: a widget in the action area of @dialog
1196 * Gets the response id of a widget in the action area
1199 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1200 * if @widget doesn't have a response id set.
1205 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1210 rd = get_response_data (widget, FALSE);
1212 return GTK_RESPONSE_NONE;
1214 return rd->response_id;
1218 * gtk_alternative_dialog_button_order:
1219 * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1221 * Returns %TRUE if dialogs are expected to use an alternative
1222 * button order on the screen @screen. See
1223 * gtk_dialog_set_alternative_button_order() for more details
1224 * about alternative button order.
1226 * If you need to use this function, you should probably connect
1227 * to the ::notify:gtk-alternative-button-order signal on the
1228 * #GtkSettings object associated to @screen, in order to be
1229 * notified if the button order setting changes.
1231 * Returns: Whether the alternative button order should be used
1236 gtk_alternative_dialog_button_order (GdkScreen *screen)
1238 GtkSettings *settings;
1242 settings = gtk_settings_get_for_screen (screen);
1244 settings = gtk_settings_get_default ();
1246 g_object_get (settings,
1247 "gtk-alternative-button-order", &result, NULL);
1253 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1254 gint first_response_id,
1257 GtkDialogPriv *priv = dialog->priv;
1262 response_id = first_response_id;
1264 while (response_id != -1)
1266 /* reorder child with response_id to position */
1267 child = dialog_find_button (dialog, response_id);
1268 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1270 response_id = va_arg (args, gint);
1276 * gtk_dialog_set_alternative_button_order:
1277 * @dialog: a #GtkDialog
1278 * @first_response_id: a response id used by one @dialog's buttons
1279 * @Varargs: a list of more response ids of @dialog's buttons, terminated by -1
1281 * Sets an alternative button order. If the
1282 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1283 * the dialog buttons are reordered according to the order of the
1284 * response ids passed to this function.
1286 * By default, GTK+ dialogs use the button order advocated by the Gnome
1287 * <ulink url="http://developer.gnome.org/projects/gup/hig/2.0/">Human
1288 * Interface Guidelines</ulink> with the affirmative button at the far
1289 * right, and the cancel button left of it. But the builtin GTK+ dialogs
1290 * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1291 * which is more suitable on some platforms, e.g. Windows.
1293 * Use this function after adding all the buttons to your dialog, as the
1294 * following example shows:
1296 * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1298 * GTK_RESPONSE_CANCEL);
1300 * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1304 * gtk_widget_grab_default (ok_button);
1306 * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1308 * GTK_RESPONSE_HELP);
1310 * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1312 * GTK_RESPONSE_CANCEL,
1313 * GTK_RESPONSE_HELP,
1320 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1321 gint first_response_id,
1327 g_return_if_fail (GTK_IS_DIALOG (dialog));
1329 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1330 if (!gtk_alternative_dialog_button_order (screen))
1333 va_start (args, first_response_id);
1335 gtk_dialog_set_alternative_button_order_valist (dialog,
1341 * gtk_dialog_set_alternative_button_order_from_array:
1342 * @dialog: a #GtkDialog
1343 * @n_params: the number of response ids in @new_order
1344 * @new_order: an array of response ids of @dialog's buttons
1346 * Sets an alternative button order. If the
1347 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1348 * the dialog buttons are reordered according to the order of the
1349 * response ids in @new_order.
1351 * See gtk_dialog_set_alternative_button_order() for more information.
1353 * This function is for use by language bindings.
1358 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1362 GtkDialogPriv *priv = dialog->priv;
1367 g_return_if_fail (GTK_IS_DIALOG (dialog));
1368 g_return_if_fail (new_order != NULL);
1370 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1371 if (!gtk_alternative_dialog_button_order (screen))
1374 for (position = 0; position < n_params; position++)
1376 /* reorder child with response_id to position */
1377 child = dialog_find_button (dialog, new_order[position]);
1378 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1389 GtkBuilder *builder;
1392 } ActionWidgetsSubParserData;
1395 attributes_start_element (GMarkupParseContext *context,
1396 const gchar *element_name,
1397 const gchar **names,
1398 const gchar **values,
1402 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1405 if (strcmp (element_name, "action-widget") == 0)
1407 for (i = 0; names[i]; i++)
1408 if (strcmp (names[i], "response") == 0)
1409 parser_data->response = g_strdup (values[i]);
1411 else if (strcmp (element_name, "action-widgets") == 0)
1414 g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1418 attributes_text_element (GMarkupParseContext *context,
1424 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1425 ActionWidgetInfo *item;
1427 if (!parser_data->response)
1430 item = g_new (ActionWidgetInfo, 1);
1431 item->widget_name = g_strndup (text, text_len);
1432 item->response_id = parser_data->response;
1433 parser_data->items = g_slist_prepend (parser_data->items, item);
1434 parser_data->response = NULL;
1437 static const GMarkupParser attributes_parser =
1439 attributes_start_element,
1441 attributes_text_element,
1445 gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
1446 GtkBuilder *builder,
1448 const gchar *tagname,
1449 GMarkupParser *parser,
1452 ActionWidgetsSubParserData *parser_data;
1457 if (strcmp (tagname, "action-widgets") == 0)
1459 parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1460 parser_data->dialog = GTK_DIALOG (buildable);
1461 parser_data->items = NULL;
1463 *parser = attributes_parser;
1464 *data = parser_data;
1468 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1469 tagname, parser, data);
1473 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1474 GtkBuilder *builder,
1476 const gchar *tagname,
1479 GtkDialog *dialog = GTK_DIALOG (buildable);
1480 GtkDialogPriv *priv = dialog->priv;
1482 ActionWidgetsSubParserData *parser_data;
1487 if (strcmp (tagname, "action-widgets"))
1489 parent_buildable_iface->custom_finished (buildable, builder, child,
1490 tagname, user_data);
1494 parser_data = (ActionWidgetsSubParserData*)user_data;
1495 parser_data->items = g_slist_reverse (parser_data->items);
1497 for (l = parser_data->items; l; l = l->next)
1499 ActionWidgetInfo *item = l->data;
1501 object = gtk_builder_get_object (builder, item->widget_name);
1504 g_warning ("Unknown object %s specified in action-widgets of %s",
1506 gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1510 ad = get_response_data (GTK_WIDGET (object), TRUE);
1511 ad->response_id = atoi (item->response_id);
1513 if (GTK_IS_BUTTON (object))
1514 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1516 signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1522 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1524 g_signal_connect_closure_by_id (object,
1531 if (ad->response_id == GTK_RESPONSE_HELP)
1532 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area),
1533 GTK_WIDGET (object), TRUE);
1535 g_free (item->widget_name);
1536 g_free (item->response_id);
1539 g_slist_free (parser_data->items);
1540 g_slice_free (ActionWidgetsSubParserData, parser_data);
1544 * gtk_dialog_get_action_area:
1545 * @dialog: a #GtkDialog
1547 * Returns the action area of @dialog.
1549 * Returns: (transfer none): the action area.
1554 gtk_dialog_get_action_area (GtkDialog *dialog)
1556 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1558 return dialog->priv->action_area;
1562 * gtk_dialog_get_content_area:
1563 * @dialog: a #GtkDialog
1565 * Returns the content area of @dialog.
1567 * Returns: (transfer none): the content area #GtkVBox.
1572 gtk_dialog_get_content_area (GtkDialog *dialog)
1574 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1576 return dialog->priv->vbox;