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/.
32 #include "gtkbutton.h"
33 #include "gtkdialog.h"
36 #include "gtkmarshalers.h"
38 #include "gtkboxprivate.h"
41 #include "gtkbindings.h"
42 #include "gtkprivate.h"
43 #include "gtkbuildable.h"
47 * @Short_description: Create popup windows
49 * @See_also: #GtkVBox, #GtkWindow, #GtkButton
51 * Dialog boxes are a convenient way to prompt the user for a small amount
52 * of input, e.g. to display a message, ask a question, or anything else
53 * that does not require extensive effort on the user's part.
55 * GTK+ treats a dialog as a window split vertically. The top section is a
56 * #GtkVBox, and is where widgets such as a #GtkLabel or a #GtkEntry should
57 * be packed. The bottom area is known as the
58 * <structfield>action_area</structfield>. This is generally used for
59 * packing buttons into the dialog which may perform functions such as
60 * cancel, ok, or apply.
62 * #GtkDialog boxes are created with a call to gtk_dialog_new() or
63 * gtk_dialog_new_with_buttons(). gtk_dialog_new_with_buttons() is
64 * recommended; it allows you to set the dialog title, some convenient flags,
65 * and add simple buttons.
67 * If 'dialog' is a newly created dialog, the two primary areas of the
68 * window can be accessed through gtk_dialog_get_content_area() and
69 * gtk_dialog_get_action_area(), as can be seen from the example below.
71 * A 'modal' dialog (that is, one which freezes the rest of the application
72 * from user input), can be created by calling gtk_window_set_modal() on the
73 * dialog. Use the GTK_WINDOW() macro to cast the widget returned from
74 * gtk_dialog_new() into a #GtkWindow. When using gtk_dialog_new_with_buttons()
75 * you can also pass the #GTK_DIALOG_MODAL flag to make a dialog modal.
77 * If you add buttons to #GtkDialog using gtk_dialog_new_with_buttons(),
78 * gtk_dialog_add_button(), gtk_dialog_add_buttons(), or
79 * gtk_dialog_add_action_widget(), clicking the button will emit a signal
80 * called #GtkDialog::response with a response ID that you specified. GTK+
81 * will never assign a meaning to positive response IDs; these are entirely
82 * user-defined. But for convenience, you can use the response IDs in the
83 * #GtkResponseType enumeration (these all have values less than zero). If
84 * a dialog receives a delete event, the #GtkDialog::response signal will
85 * be emitted with a response ID of #GTK_RESPONSE_DELETE_EVENT.
87 * If you want to block waiting for a dialog to return before returning
88 * control flow to your code, you can call gtk_dialog_run(). This function
89 * enters a recursive main loop and waits for the user to respond to the
90 * dialog, returning the response ID corresponding to the button the user
93 * For the simple dialog in the following example, in reality you'd probably
94 * use #GtkMessageDialog to save yourself some effort. But you'd need to
95 * create the dialog contents manually if you had more than a simple message
98 * <title>Simple GtkDialog usage</title>
100 * /* Function to open a dialog box displaying the message provided. */
102 * quick_message (gchar *message)
104 * GtkWidget *dialog, *label, *content_area;
106 * /* Create the widgets */
107 * dialog = gtk_dialog_new_with_buttons ("Message",
108 * main_application_window,
109 * GTK_DIALOG_DESTROY_WITH_PARENT,
113 * content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
114 * label = gtk_label_new (message);
116 * /* Ensure that the dialog box is destroyed when the user responds */
117 * g_signal_connect_swapped (dialog,
119 * G_CALLBACK (gtk_widget_destroy),
122 * /* Add the label, and show everything we've added to the dialog */
124 * gtk_container_add (GTK_CONTAINER (content_area), label);
125 * gtk_widget_show_all (dialog);
130 * <refsect2 id="GtkDialog-BUILDER-UI"><title>GtkDialog as GtkBuildable</title>
132 * The GtkDialog implementation of the #GtkBuildable interface exposes the
133 * @vbox and @action_area as internal children with the names "vbox" and
137 * GtkDialog supports a custom <action-widgets> element, which
138 * can contain multiple <action-widget> elements. The "response"
139 * attribute specifies a numeric response, and the content of the element
140 * is the id of widget (which should be a child of the dialogs @action_area).
143 * <title>A <structname>GtkDialog</structname> UI definition fragment.</title>
144 * <programlisting><![CDATA[
145 * <object class="GtkDialog" id="dialog1">
146 * <child internal-child="vbox">"
147 * <object class="GtkVBox" id="vbox">
148 * <child internal-child="action_area">
149 * <object class="GtkHButtonBox" id="button_box">
151 * <object class="GtkButton" id="button_cancel"/>
154 * <object class="GtkButton" id="button_ok"/>
161 * <action-widget response="3">button_ok</action-widget>
162 * <action-widget response="-5">button_cancel</action-widget>
165 * ]]></programlisting>
170 struct _GtkDialogPrivate
173 GtkWidget *action_area;
176 typedef struct _ResponseData ResponseData;
183 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
184 const gchar *first_button_text,
187 static gboolean gtk_dialog_delete_event_handler (GtkWidget *widget,
190 static void gtk_dialog_style_updated (GtkWidget *widget);
191 static void gtk_dialog_map (GtkWidget *widget);
193 static void gtk_dialog_close (GtkDialog *dialog);
195 static ResponseData * get_response_data (GtkWidget *widget,
198 static void gtk_dialog_buildable_interface_init (GtkBuildableIface *iface);
199 static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
201 const gchar *childname);
202 static gboolean gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
205 const gchar *tagname,
206 GMarkupParser *parser,
208 static void gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
211 const gchar *tagname,
226 static guint dialog_signals[LAST_SIGNAL];
228 G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
229 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
230 gtk_dialog_buildable_interface_init))
233 gtk_dialog_class_init (GtkDialogClass *class)
235 GObjectClass *gobject_class;
236 GtkWidgetClass *widget_class;
237 GtkBindingSet *binding_set;
239 gobject_class = G_OBJECT_CLASS (class);
240 widget_class = GTK_WIDGET_CLASS (class);
242 widget_class->map = gtk_dialog_map;
243 widget_class->style_updated = gtk_dialog_style_updated;
245 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_DIALOG);
247 class->close = gtk_dialog_close;
249 g_type_class_add_private (gobject_class, sizeof (GtkDialogPrivate));
252 * GtkDialog::response:
253 * @dialog: the object on which the signal is emitted
254 * @response_id: the response ID
256 * Emitted when an action widget is clicked, the dialog receives a
257 * delete event, or the application programmer calls gtk_dialog_response().
258 * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
259 * Otherwise, it depends on which action widget was clicked.
261 dialog_signals[RESPONSE] =
262 g_signal_new (I_("response"),
263 G_OBJECT_CLASS_TYPE (class),
265 G_STRUCT_OFFSET (GtkDialogClass, response),
267 _gtk_marshal_VOID__INT,
274 * The ::close signal is a
275 * <link linkend="keybinding-signals">keybinding signal</link>
276 * which gets emitted when the user uses a keybinding to close
279 * The default binding for this signal is the Escape key.
281 dialog_signals[CLOSE] =
282 g_signal_new (I_("close"),
283 G_OBJECT_CLASS_TYPE (class),
284 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
285 G_STRUCT_OFFSET (GtkDialogClass, close),
287 _gtk_marshal_VOID__VOID,
290 gtk_widget_class_install_style_property (widget_class,
291 g_param_spec_int ("content-area-border",
292 P_("Content area border"),
293 P_("Width of border around the main dialog area"),
297 GTK_PARAM_READABLE));
299 * GtkDialog:content-area-spacing:
301 * The default spacing used between elements of the
302 * content area of the dialog, as returned by
303 * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
304 * was called on that widget directly.
308 gtk_widget_class_install_style_property (widget_class,
309 g_param_spec_int ("content-area-spacing",
310 P_("Content area spacing"),
311 P_("Spacing between elements of the main dialog area"),
315 GTK_PARAM_READABLE));
316 gtk_widget_class_install_style_property (widget_class,
317 g_param_spec_int ("button-spacing",
318 P_("Button spacing"),
319 P_("Spacing between buttons"),
323 GTK_PARAM_READABLE));
325 gtk_widget_class_install_style_property (widget_class,
326 g_param_spec_int ("action-area-border",
327 P_("Action area border"),
328 P_("Width of border around the button area at the bottom of the dialog"),
332 GTK_PARAM_READABLE));
334 binding_set = gtk_binding_set_by_class (class);
336 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
340 update_spacings (GtkDialog *dialog)
342 GtkDialogPrivate *priv = dialog->priv;
343 gint content_area_border;
344 gint content_area_spacing;
346 gint action_area_border;
348 gtk_widget_style_get (GTK_WIDGET (dialog),
349 "content-area-border", &content_area_border,
350 "content-area-spacing", &content_area_spacing,
351 "button-spacing", &button_spacing,
352 "action-area-border", &action_area_border,
356 gtk_container_set_border_width (GTK_CONTAINER (priv->vbox),
357 content_area_border);
358 if (!_gtk_box_get_spacing_set (GTK_BOX (priv->vbox)))
360 gtk_box_set_spacing (GTK_BOX (priv->vbox), content_area_spacing);
361 _gtk_box_set_spacing_set (GTK_BOX (priv->vbox), FALSE);
363 gtk_box_set_spacing (GTK_BOX (priv->action_area),
365 gtk_container_set_border_width (GTK_CONTAINER (priv->action_area),
370 gtk_dialog_init (GtkDialog *dialog)
372 GtkDialogPrivate *priv;
374 dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
379 /* To avoid breaking old code that prevents destroy on delete event
380 * by connecting a handler, we have to have the FIRST signal
381 * connection on the dialog.
383 g_signal_connect (dialog,
385 G_CALLBACK (gtk_dialog_delete_event_handler),
388 priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
389 gtk_container_add (GTK_CONTAINER (dialog), priv->vbox);
390 gtk_widget_show (priv->vbox);
392 priv->action_area = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
394 gtk_button_box_set_layout (GTK_BUTTON_BOX (priv->action_area),
397 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->action_area,
399 gtk_widget_show (priv->action_area);
401 gtk_window_set_type_hint (GTK_WINDOW (dialog),
402 GDK_WINDOW_TYPE_HINT_DIALOG);
403 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
405 update_spacings (dialog);
408 static GtkBuildableIface *parent_buildable_iface;
411 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
413 parent_buildable_iface = g_type_interface_peek_parent (iface);
414 iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
415 iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
416 iface->custom_finished = gtk_dialog_buildable_custom_finished;
420 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
422 const gchar *childname)
424 GtkDialogPrivate *priv = GTK_DIALOG (buildable)->priv;
426 if (strcmp (childname, "vbox") == 0)
427 return G_OBJECT (priv->vbox);
428 else if (strcmp (childname, "action_area") == 0)
429 return G_OBJECT (priv->action_area);
431 return parent_buildable_iface->get_internal_child (buildable,
437 gtk_dialog_delete_event_handler (GtkWidget *widget,
441 /* emit response signal */
442 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
444 /* Do the destroy by default */
448 /* A far too tricky heuristic for getting the right initial
449 * focus widget if none was set. What we do is we focus the first
450 * widget in the tab chain, but if this results in the focus
451 * ending up on one of the response widgets _other_ than the
452 * default response, we focus the default response instead.
454 * Additionally, skip selectable labels when looking for the
455 * right initial focus widget.
458 gtk_dialog_map (GtkWidget *widget)
460 GtkWidget *default_widget, *focus;
461 GtkWindow *window = GTK_WINDOW (widget);
462 GtkDialog *dialog = GTK_DIALOG (widget);
463 GtkDialogPrivate *priv = dialog->priv;
465 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
467 focus = gtk_window_get_focus (window);
470 GList *children, *tmp_list;
471 GtkWidget *first_focus = NULL;
475 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
477 focus = gtk_window_get_focus (window);
478 if (GTK_IS_LABEL (focus) &&
479 !gtk_label_get_current_uri (GTK_LABEL (focus)))
480 gtk_label_select_region (GTK_LABEL (focus), 0, 0);
482 if (first_focus == NULL)
484 else if (first_focus == focus)
487 if (!GTK_IS_LABEL (focus))
492 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
496 GtkWidget *child = tmp_list->data;
498 default_widget = gtk_window_get_default_widget (window);
499 if ((focus == NULL || child == focus) &&
500 child != default_widget &&
503 gtk_widget_grab_focus (default_widget);
507 tmp_list = tmp_list->next;
510 g_list_free (children);
515 gtk_dialog_style_updated (GtkWidget *widget)
517 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->style_updated (widget);
519 update_spacings (GTK_DIALOG (widget));
523 dialog_find_button (GtkDialog *dialog,
526 GtkDialogPrivate *priv = dialog->priv;
527 GtkWidget *child = NULL;
528 GList *children, *tmp_list;
530 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
532 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
534 ResponseData *rd = get_response_data (tmp_list->data, FALSE);
536 if (rd && rd->response_id == response_id)
538 child = tmp_list->data;
543 g_list_free (children);
549 gtk_dialog_close (GtkDialog *dialog)
551 /* Synthesize delete_event to close dialog. */
553 GtkWidget *widget = GTK_WIDGET (dialog);
556 event = gdk_event_new (GDK_DELETE);
558 event->any.window = g_object_ref (gtk_widget_get_window (widget));
559 event->any.send_event = TRUE;
561 gtk_main_do_event (event);
562 gdk_event_free (event);
568 * Creates a new dialog box.
570 * Widgets should not be packed into this #GtkWindow
571 * directly, but into the @vbox and @action_area, as described above.
573 * Returns: the new dialog as a #GtkWidget
576 gtk_dialog_new (void)
578 return g_object_new (GTK_TYPE_DIALOG, NULL);
582 gtk_dialog_new_empty (const gchar *title,
584 GtkDialogFlags flags)
588 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
591 gtk_window_set_title (GTK_WINDOW (dialog), title);
594 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
596 if (flags & GTK_DIALOG_MODAL)
597 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
599 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
600 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
602 return GTK_WIDGET (dialog);
606 * gtk_dialog_new_with_buttons:
607 * @title: (allow-none): Title of the dialog, or %NULL
608 * @parent: (allow-none): Transient parent of the dialog, or %NULL
609 * @flags: from #GtkDialogFlags
610 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
611 * @...: response ID for first button, then additional buttons, ending with %NULL
613 * Creates a new #GtkDialog with title @title (or %NULL for the default
614 * title; see gtk_window_set_title()) and transient parent @parent (or
615 * %NULL for none; see gtk_window_set_transient_for()). The @flags
616 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
617 * and/or to have it destroyed along with its transient parent
618 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
619 * text/response ID pairs should be listed, with a %NULL pointer ending
620 * the list. Button text can be either a stock ID such as
621 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
622 * any positive number, or one of the values in the #GtkResponseType
623 * enumeration. If the user clicks one of these dialog buttons,
624 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
625 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
626 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
627 * However, destroying a dialog does not emit the ::response signal;
628 * so be careful relying on ::response when using the
629 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
630 * so the first button in the list will be the leftmost button in the dialog.
632 * Here's a simple example:
634 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
636 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
638 * GTK_RESPONSE_ACCEPT,
640 * GTK_RESPONSE_REJECT,
644 * Return value: a new #GtkDialog
647 gtk_dialog_new_with_buttons (const gchar *title,
649 GtkDialogFlags flags,
650 const gchar *first_button_text,
656 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
658 va_start (args, first_button_text);
660 gtk_dialog_add_buttons_valist (dialog,
666 return GTK_WIDGET (dialog);
670 response_data_free (gpointer data)
672 g_slice_free (ResponseData, data);
676 get_response_data (GtkWidget *widget,
679 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
680 "gtk-dialog-response-data");
682 if (ad == NULL && create)
684 ad = g_slice_new (ResponseData);
686 g_object_set_data_full (G_OBJECT (widget),
687 I_("gtk-dialog-response-data"),
696 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
700 response_id = gtk_dialog_get_response_for_widget (dialog, widget);
702 gtk_dialog_response (dialog, response_id);
706 * gtk_dialog_add_action_widget:
707 * @dialog: a #GtkDialog
708 * @child: an activatable widget
709 * @response_id: response ID for @child
711 * Adds an activatable widget to the action area of a #GtkDialog,
712 * connecting a signal handler that will emit the #GtkDialog::response
713 * signal on the dialog when the widget is activated. The widget is
714 * appended to the end of the dialog's action area. If you want to add a
715 * non-activatable widget, simply pack it into the @action_area field
716 * of the #GtkDialog struct.
719 gtk_dialog_add_action_widget (GtkDialog *dialog,
723 GtkDialogPrivate *priv;
727 g_return_if_fail (GTK_IS_DIALOG (dialog));
728 g_return_if_fail (GTK_IS_WIDGET (child));
732 ad = get_response_data (child, TRUE);
734 ad->response_id = response_id;
736 if (GTK_IS_BUTTON (child))
737 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
739 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
745 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
747 g_signal_connect_closure_by_id (child,
754 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
756 gtk_box_pack_end (GTK_BOX (priv->action_area),
760 if (response_id == GTK_RESPONSE_HELP)
761 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
765 * gtk_dialog_add_button:
766 * @dialog: a #GtkDialog
767 * @button_text: text of button, or stock ID
768 * @response_id: response ID for the button
770 * Adds a button with the given text (or a stock button, if @button_text is a
771 * stock ID) and sets things up so that clicking the button will emit the
772 * #GtkDialog::response signal with the given @response_id. The button is
773 * appended to the end of the dialog's action area. The button widget is
774 * returned, but usually you don't need it.
776 * Return value: (transfer none): the #GtkButton widget that was added
779 gtk_dialog_add_button (GtkDialog *dialog,
780 const gchar *button_text,
785 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
786 g_return_val_if_fail (button_text != NULL, NULL);
788 button = gtk_button_new_from_stock (button_text);
790 gtk_widget_set_can_default (button, TRUE);
792 gtk_widget_show (button);
794 gtk_dialog_add_action_widget (dialog,
802 gtk_dialog_add_buttons_valist (GtkDialog *dialog,
803 const gchar *first_button_text,
809 g_return_if_fail (GTK_IS_DIALOG (dialog));
811 if (first_button_text == NULL)
814 text = first_button_text;
815 response_id = va_arg (args, gint);
819 gtk_dialog_add_button (dialog, text, response_id);
821 text = va_arg (args, gchar*);
824 response_id = va_arg (args, int);
829 * gtk_dialog_add_buttons:
830 * @dialog: a #GtkDialog
831 * @first_button_text: button text or stock ID
832 * @...: response ID for first button, then more text-response_id pairs
834 * Adds more buttons, same as calling gtk_dialog_add_button()
835 * repeatedly. The variable argument list should be %NULL-terminated
836 * as with gtk_dialog_new_with_buttons(). Each button must have both
837 * text and response ID.
840 gtk_dialog_add_buttons (GtkDialog *dialog,
841 const gchar *first_button_text,
846 va_start (args, first_button_text);
848 gtk_dialog_add_buttons_valist (dialog,
856 * gtk_dialog_set_response_sensitive:
857 * @dialog: a #GtkDialog
858 * @response_id: a response ID
859 * @setting: %TRUE for sensitive
861 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
862 * for each widget in the dialog's action area with the given @response_id.
863 * A convenient way to sensitize/desensitize dialog buttons.
866 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
870 GtkDialogPrivate *priv;
874 g_return_if_fail (GTK_IS_DIALOG (dialog));
878 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
881 while (tmp_list != NULL)
883 GtkWidget *widget = tmp_list->data;
884 ResponseData *rd = get_response_data (widget, FALSE);
886 if (rd && rd->response_id == response_id)
887 gtk_widget_set_sensitive (widget, setting);
889 tmp_list = g_list_next (tmp_list);
892 g_list_free (children);
896 * gtk_dialog_set_default_response:
897 * @dialog: a #GtkDialog
898 * @response_id: a response ID
900 * Sets the last widget in the dialog's action area with the given @response_id
901 * as the default widget for the dialog. Pressing "Enter" normally activates
902 * the default widget.
905 gtk_dialog_set_default_response (GtkDialog *dialog,
908 GtkDialogPrivate *priv;
912 g_return_if_fail (GTK_IS_DIALOG (dialog));
916 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
919 while (tmp_list != NULL)
921 GtkWidget *widget = tmp_list->data;
922 ResponseData *rd = get_response_data (widget, FALSE);
924 if (rd && rd->response_id == response_id)
925 gtk_widget_grab_default (widget);
927 tmp_list = g_list_next (tmp_list);
930 g_list_free (children);
934 * gtk_dialog_response:
935 * @dialog: a #GtkDialog
936 * @response_id: response ID
938 * Emits the #GtkDialog::response signal with the given response ID.
939 * Used to indicate that the user has responded to the dialog in some way;
940 * typically either you or gtk_dialog_run() will be monitoring the
941 * ::response signal and take appropriate action.
944 gtk_dialog_response (GtkDialog *dialog,
947 g_return_if_fail (GTK_IS_DIALOG (dialog));
949 g_signal_emit (dialog,
950 dialog_signals[RESPONSE],
964 shutdown_loop (RunInfo *ri)
966 if (g_main_loop_is_running (ri->loop))
967 g_main_loop_quit (ri->loop);
971 run_unmap_handler (GtkDialog *dialog, gpointer data)
979 run_response_handler (GtkDialog *dialog,
987 ri->response_id = response_id;
993 run_delete_handler (GtkDialog *dialog,
1001 return TRUE; /* Do not destroy */
1005 run_destroy_handler (GtkDialog *dialog, gpointer data)
1009 /* shutdown_loop will be called by run_unmap_handler */
1011 ri->destroyed = TRUE;
1016 * @dialog: a #GtkDialog
1018 * Blocks in a recursive main loop until the @dialog either emits the
1019 * #GtkDialog::response signal, or is destroyed. If the dialog is
1020 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
1021 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
1022 * ::response signal emission.
1024 * Before entering the recursive main loop, gtk_dialog_run() calls
1025 * gtk_widget_show() on the dialog for you. Note that you still
1026 * need to show any children of the dialog yourself.
1028 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1029 * is disabled; if the dialog receives ::delete_event, it will not be
1030 * destroyed as windows usually are, and gtk_dialog_run() will return
1031 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1032 * will be modal. You can force gtk_dialog_run() to return at any time by
1033 * calling gtk_dialog_response() to emit the ::response signal. Destroying
1034 * the dialog during gtk_dialog_run() is a very bad idea, because your
1035 * post-run code won't know whether the dialog was destroyed or not.
1037 * After gtk_dialog_run() returns, you are responsible for hiding or
1038 * destroying the dialog if you wish to do so.
1040 * Typical usage of this function might be:
1042 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1045 * case GTK_RESPONSE_ACCEPT:
1046 * do_application_specific_something ();
1049 * do_nothing_since_dialog_was_cancelled ();
1052 * gtk_widget_destroy (dialog);
1055 * Note that even though the recursive main loop gives the effect of a
1056 * modal dialog (it prevents the user from interacting with other
1057 * windows in the same window group while the dialog is run), callbacks
1058 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1059 * be triggered during a gtk_dialog_run() call.
1061 * Return value: response ID
1064 gtk_dialog_run (GtkDialog *dialog)
1066 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1068 gulong response_handler;
1069 gulong unmap_handler;
1070 gulong destroy_handler;
1071 gulong delete_handler;
1073 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1075 g_object_ref (dialog);
1077 was_modal = gtk_window_get_modal (GTK_WINDOW (dialog));
1079 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1081 if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1082 gtk_widget_show (GTK_WIDGET (dialog));
1085 g_signal_connect (dialog,
1087 G_CALLBACK (run_response_handler),
1091 g_signal_connect (dialog,
1093 G_CALLBACK (run_unmap_handler),
1097 g_signal_connect (dialog,
1099 G_CALLBACK (run_delete_handler),
1103 g_signal_connect (dialog,
1105 G_CALLBACK (run_destroy_handler),
1108 ri.loop = g_main_loop_new (NULL, FALSE);
1110 GDK_THREADS_LEAVE ();
1111 g_main_loop_run (ri.loop);
1112 GDK_THREADS_ENTER ();
1114 g_main_loop_unref (ri.loop);
1121 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1123 g_signal_handler_disconnect (dialog, response_handler);
1124 g_signal_handler_disconnect (dialog, unmap_handler);
1125 g_signal_handler_disconnect (dialog, delete_handler);
1126 g_signal_handler_disconnect (dialog, destroy_handler);
1129 g_object_unref (dialog);
1131 return ri.response_id;
1135 * gtk_dialog_get_widget_for_response:
1136 * @dialog: a #GtkDialog
1137 * @response_id: the response ID used by the @dialog widget
1139 * Gets the widget button that uses the given response ID in the action area
1142 * Returns: (transfer none): the @widget button that uses the given
1143 * @response_id, or %NULL.
1148 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1151 GtkDialogPrivate *priv;
1155 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1157 priv = dialog->priv;
1159 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
1161 tmp_list = children;
1162 while (tmp_list != NULL)
1164 GtkWidget *widget = tmp_list->data;
1165 ResponseData *rd = get_response_data (widget, FALSE);
1167 if (rd && rd->response_id == response_id)
1169 g_list_free (children);
1173 tmp_list = g_list_next (tmp_list);
1176 g_list_free (children);
1182 * gtk_dialog_get_response_for_widget:
1183 * @dialog: a #GtkDialog
1184 * @widget: a widget in the action area of @dialog
1186 * Gets the response id of a widget in the action area
1189 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1190 * if @widget doesn't have a response id set.
1195 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1200 rd = get_response_data (widget, FALSE);
1202 return GTK_RESPONSE_NONE;
1204 return rd->response_id;
1208 * gtk_alternative_dialog_button_order:
1209 * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1211 * Returns %TRUE if dialogs are expected to use an alternative
1212 * button order on the screen @screen. See
1213 * gtk_dialog_set_alternative_button_order() for more details
1214 * about alternative button order.
1216 * If you need to use this function, you should probably connect
1217 * to the ::notify:gtk-alternative-button-order signal on the
1218 * #GtkSettings object associated to @screen, in order to be
1219 * notified if the button order setting changes.
1221 * Returns: Whether the alternative button order should be used
1226 gtk_alternative_dialog_button_order (GdkScreen *screen)
1228 GtkSettings *settings;
1232 settings = gtk_settings_get_for_screen (screen);
1234 settings = gtk_settings_get_default ();
1236 g_object_get (settings,
1237 "gtk-alternative-button-order", &result, NULL);
1243 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1244 gint first_response_id,
1247 GtkDialogPrivate *priv = dialog->priv;
1252 response_id = first_response_id;
1254 while (response_id != -1)
1256 /* reorder child with response_id to position */
1257 child = dialog_find_button (dialog, response_id);
1258 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1260 response_id = va_arg (args, gint);
1266 * gtk_dialog_set_alternative_button_order:
1267 * @dialog: a #GtkDialog
1268 * @first_response_id: a response id used by one @dialog's buttons
1269 * @...: a list of more response ids of @dialog's buttons, terminated by -1
1271 * Sets an alternative button order. If the
1272 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1273 * the dialog buttons are reordered according to the order of the
1274 * response ids passed to this function.
1276 * By default, GTK+ dialogs use the button order advocated by the Gnome
1277 * <ulink url="http://library.gnome.org/devel/hig-book/stable/">Human
1278 * Interface Guidelines</ulink> with the affirmative button at the far
1279 * right, and the cancel button left of it. But the builtin GTK+ dialogs
1280 * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1281 * which is more suitable on some platforms, e.g. Windows.
1283 * Use this function after adding all the buttons to your dialog, as the
1284 * following example shows:
1286 * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1288 * GTK_RESPONSE_CANCEL);
1290 * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1294 * gtk_widget_grab_default (ok_button);
1296 * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1298 * GTK_RESPONSE_HELP);
1300 * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1302 * GTK_RESPONSE_CANCEL,
1303 * GTK_RESPONSE_HELP,
1310 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1311 gint first_response_id,
1317 g_return_if_fail (GTK_IS_DIALOG (dialog));
1319 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1320 if (!gtk_alternative_dialog_button_order (screen))
1323 va_start (args, first_response_id);
1325 gtk_dialog_set_alternative_button_order_valist (dialog,
1331 * gtk_dialog_set_alternative_button_order_from_array:
1332 * @dialog: a #GtkDialog
1333 * @n_params: the number of response ids in @new_order
1334 * @new_order: (array length=n_params): an array of response ids of
1337 * Sets an alternative button order. If the
1338 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1339 * the dialog buttons are reordered according to the order of the
1340 * response ids in @new_order.
1342 * See gtk_dialog_set_alternative_button_order() for more information.
1344 * This function is for use by language bindings.
1349 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1353 GtkDialogPrivate *priv = dialog->priv;
1358 g_return_if_fail (GTK_IS_DIALOG (dialog));
1359 g_return_if_fail (new_order != NULL);
1361 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1362 if (!gtk_alternative_dialog_button_order (screen))
1365 for (position = 0; position < n_params; position++)
1367 /* reorder child with response_id to position */
1368 child = dialog_find_button (dialog, new_order[position]);
1369 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1380 GtkBuilder *builder;
1383 } ActionWidgetsSubParserData;
1386 attributes_start_element (GMarkupParseContext *context,
1387 const gchar *element_name,
1388 const gchar **names,
1389 const gchar **values,
1393 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1396 if (strcmp (element_name, "action-widget") == 0)
1398 for (i = 0; names[i]; i++)
1399 if (strcmp (names[i], "response") == 0)
1400 parser_data->response = g_strdup (values[i]);
1402 else if (strcmp (element_name, "action-widgets") == 0)
1405 g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1409 attributes_text_element (GMarkupParseContext *context,
1415 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1416 ActionWidgetInfo *item;
1418 if (!parser_data->response)
1421 item = g_new (ActionWidgetInfo, 1);
1422 item->widget_name = g_strndup (text, text_len);
1423 item->response_id = parser_data->response;
1424 parser_data->items = g_slist_prepend (parser_data->items, item);
1425 parser_data->response = NULL;
1428 static const GMarkupParser attributes_parser =
1430 attributes_start_element,
1432 attributes_text_element,
1436 gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
1437 GtkBuilder *builder,
1439 const gchar *tagname,
1440 GMarkupParser *parser,
1443 ActionWidgetsSubParserData *parser_data;
1448 if (strcmp (tagname, "action-widgets") == 0)
1450 parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1451 parser_data->dialog = GTK_DIALOG (buildable);
1452 parser_data->items = NULL;
1454 *parser = attributes_parser;
1455 *data = parser_data;
1459 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1460 tagname, parser, data);
1464 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1465 GtkBuilder *builder,
1467 const gchar *tagname,
1470 GtkDialog *dialog = GTK_DIALOG (buildable);
1471 GtkDialogPrivate *priv = dialog->priv;
1473 ActionWidgetsSubParserData *parser_data;
1478 if (strcmp (tagname, "action-widgets"))
1480 parent_buildable_iface->custom_finished (buildable, builder, child,
1481 tagname, user_data);
1485 parser_data = (ActionWidgetsSubParserData*)user_data;
1486 parser_data->items = g_slist_reverse (parser_data->items);
1488 for (l = parser_data->items; l; l = l->next)
1490 ActionWidgetInfo *item = l->data;
1492 object = gtk_builder_get_object (builder, item->widget_name);
1495 g_warning ("Unknown object %s specified in action-widgets of %s",
1497 gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1501 ad = get_response_data (GTK_WIDGET (object), TRUE);
1502 ad->response_id = g_ascii_strtoll (item->response_id, NULL, 10);
1504 if (GTK_IS_BUTTON (object))
1505 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1507 signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1513 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1515 g_signal_connect_closure_by_id (object,
1522 if (ad->response_id == GTK_RESPONSE_HELP)
1523 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area),
1524 GTK_WIDGET (object), TRUE);
1526 g_free (item->widget_name);
1527 g_free (item->response_id);
1530 g_slist_free (parser_data->items);
1531 g_slice_free (ActionWidgetsSubParserData, parser_data);
1535 * gtk_dialog_get_action_area:
1536 * @dialog: a #GtkDialog
1538 * Returns the action area of @dialog.
1540 * Returns: (transfer none): the action area.
1545 gtk_dialog_get_action_area (GtkDialog *dialog)
1547 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1549 return dialog->priv->action_area;
1553 * gtk_dialog_get_content_area:
1554 * @dialog: a #GtkDialog
1556 * Returns the content area of @dialog.
1558 * Returns: (transfer none): the content area #GtkBox.
1563 gtk_dialog_get_content_area (GtkDialog *dialog)
1565 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1567 return dialog->priv->vbox;