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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkbutton.h"
31 #include "gtkdialog.h"
34 #include "gtkmarshalers.h"
36 #include "gtkboxprivate.h"
39 #include "gtkbindings.h"
40 #include "gtkprivate.h"
41 #include "gtkbuildable.h"
45 * @Short_description: Create popup windows
47 * @See_also: #GtkVBox, #GtkWindow, #GtkButton
49 * Dialog boxes are a convenient way to prompt the user for a small amount
50 * of input, e.g. to display a message, ask a question, or anything else
51 * that does not require extensive effort on the user's part.
53 * GTK+ treats a dialog as a window split vertically. The top section is a
54 * #GtkVBox, and is where widgets such as a #GtkLabel or a #GtkEntry should
55 * be packed. The bottom area is known as the
56 * <structfield>action_area</structfield>. This is generally used for
57 * packing buttons into the dialog which may perform functions such as
58 * cancel, ok, or apply.
60 * #GtkDialog boxes are created with a call to gtk_dialog_new() or
61 * gtk_dialog_new_with_buttons(). gtk_dialog_new_with_buttons() is
62 * recommended; it allows you to set the dialog title, some convenient flags,
63 * and add simple buttons.
65 * If 'dialog' is a newly created dialog, the two primary areas of the
66 * window can be accessed through gtk_dialog_get_content_area() and
67 * gtk_dialog_get_action_area(), as can be seen from the example below.
69 * A 'modal' dialog (that is, one which freezes the rest of the application
70 * from user input), can be created by calling gtk_window_set_modal() on the
71 * dialog. Use the GTK_WINDOW() macro to cast the widget returned from
72 * gtk_dialog_new() into a #GtkWindow. When using gtk_dialog_new_with_buttons()
73 * you can also pass the #GTK_DIALOG_MODAL flag to make a dialog modal.
75 * If you add buttons to #GtkDialog using gtk_dialog_new_with_buttons(),
76 * gtk_dialog_add_button(), gtk_dialog_add_buttons(), or
77 * gtk_dialog_add_action_widget(), clicking the button will emit a signal
78 * called #GtkDialog::response with a response ID that you specified. GTK+
79 * will never assign a meaning to positive response IDs; these are entirely
80 * user-defined. But for convenience, you can use the response IDs in the
81 * #GtkResponseType enumeration (these all have values less than zero). If
82 * a dialog receives a delete event, the #GtkDialog::response signal will
83 * be emitted with a response ID of #GTK_RESPONSE_DELETE_EVENT.
85 * If you want to block waiting for a dialog to return before returning
86 * control flow to your code, you can call gtk_dialog_run(). This function
87 * enters a recursive main loop and waits for the user to respond to the
88 * dialog, returning the response ID corresponding to the button the user
91 * For the simple dialog in the following example, in reality you'd probably
92 * use #GtkMessageDialog to save yourself some effort. But you'd need to
93 * create the dialog contents manually if you had more than a simple message
96 * <title>Simple GtkDialog usage</title>
98 * /* Function to open a dialog box displaying the message provided. */
100 * quick_message (gchar *message)
102 * GtkWidget *dialog, *label, *content_area;
104 * /* Create the widgets */
105 * dialog = gtk_dialog_new_with_buttons ("Message",
106 * main_application_window,
107 * GTK_DIALOG_DESTROY_WITH_PARENT,
111 * content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
112 * label = gtk_label_new (message);
114 * /* Ensure that the dialog box is destroyed when the user responds */
115 * g_signal_connect_swapped (dialog,
117 * G_CALLBACK (gtk_widget_destroy),
120 * /* Add the label, and show everything we've added to the dialog */
122 * gtk_container_add (GTK_CONTAINER (content_area), label);
123 * gtk_widget_show_all (dialog);
128 * <refsect2 id="GtkDialog-BUILDER-UI"><title>GtkDialog as GtkBuildable</title>
130 * The GtkDialog implementation of the #GtkBuildable interface exposes the
131 * @vbox and @action_area as internal children with the names "vbox" and
135 * GtkDialog supports a custom <action-widgets> element, which
136 * can contain multiple <action-widget> elements. The "response"
137 * attribute specifies a numeric response, and the content of the element
138 * is the id of widget (which should be a child of the dialogs @action_area).
141 * <title>A <structname>GtkDialog</structname> UI definition fragment.</title>
142 * <programlisting><![CDATA[
143 * <object class="GtkDialog" id="dialog1">
144 * <child internal-child="vbox">"
145 * <object class="GtkVBox" id="vbox">
146 * <child internal-child="action_area">
147 * <object class="GtkHButtonBox" id="button_box">
149 * <object class="GtkButton" id="button_cancel"/>
152 * <object class="GtkButton" id="button_ok"/>
159 * <action-widget response="3">button_ok</action-widget>
160 * <action-widget response="-5">button_cancel</action-widget>
163 * ]]></programlisting>
168 struct _GtkDialogPrivate
171 GtkWidget *action_area;
174 typedef struct _ResponseData ResponseData;
181 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
182 const gchar *first_button_text,
185 static gboolean gtk_dialog_delete_event_handler (GtkWidget *widget,
188 static void gtk_dialog_style_updated (GtkWidget *widget);
189 static void gtk_dialog_map (GtkWidget *widget);
191 static void gtk_dialog_close (GtkDialog *dialog);
193 static ResponseData * get_response_data (GtkWidget *widget,
196 static void gtk_dialog_buildable_interface_init (GtkBuildableIface *iface);
197 static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
199 const gchar *childname);
200 static gboolean gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
203 const gchar *tagname,
204 GMarkupParser *parser,
206 static void gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
209 const gchar *tagname,
224 static guint dialog_signals[LAST_SIGNAL];
226 G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
227 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
228 gtk_dialog_buildable_interface_init))
231 gtk_dialog_class_init (GtkDialogClass *class)
233 GObjectClass *gobject_class;
234 GtkWidgetClass *widget_class;
235 GtkBindingSet *binding_set;
237 gobject_class = G_OBJECT_CLASS (class);
238 widget_class = GTK_WIDGET_CLASS (class);
240 widget_class->map = gtk_dialog_map;
241 widget_class->style_updated = gtk_dialog_style_updated;
243 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_DIALOG);
245 class->close = gtk_dialog_close;
247 g_type_class_add_private (gobject_class, sizeof (GtkDialogPrivate));
250 * GtkDialog::response:
251 * @dialog: the object on which the signal is emitted
252 * @response_id: the response ID
254 * Emitted when an action widget is clicked, the dialog receives a
255 * delete event, or the application programmer calls gtk_dialog_response().
256 * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
257 * Otherwise, it depends on which action widget was clicked.
259 dialog_signals[RESPONSE] =
260 g_signal_new (I_("response"),
261 G_OBJECT_CLASS_TYPE (class),
263 G_STRUCT_OFFSET (GtkDialogClass, response),
265 _gtk_marshal_VOID__INT,
272 * The ::close signal is a
273 * <link linkend="keybinding-signals">keybinding signal</link>
274 * which gets emitted when the user uses a keybinding to close
277 * The default binding for this signal is the Escape key.
279 dialog_signals[CLOSE] =
280 g_signal_new (I_("close"),
281 G_OBJECT_CLASS_TYPE (class),
282 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
283 G_STRUCT_OFFSET (GtkDialogClass, close),
285 _gtk_marshal_VOID__VOID,
288 gtk_widget_class_install_style_property (widget_class,
289 g_param_spec_int ("content-area-border",
290 P_("Content area border"),
291 P_("Width of border around the main dialog area"),
295 GTK_PARAM_READABLE));
297 * GtkDialog:content-area-spacing:
299 * The default spacing used between elements of the
300 * content area of the dialog, as returned by
301 * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
302 * was called on that widget directly.
306 gtk_widget_class_install_style_property (widget_class,
307 g_param_spec_int ("content-area-spacing",
308 P_("Content area spacing"),
309 P_("Spacing between elements of the main dialog area"),
313 GTK_PARAM_READABLE));
314 gtk_widget_class_install_style_property (widget_class,
315 g_param_spec_int ("button-spacing",
316 P_("Button spacing"),
317 P_("Spacing between buttons"),
321 GTK_PARAM_READABLE));
323 gtk_widget_class_install_style_property (widget_class,
324 g_param_spec_int ("action-area-border",
325 P_("Action area border"),
326 P_("Width of border around the button area at the bottom of the dialog"),
330 GTK_PARAM_READABLE));
332 binding_set = gtk_binding_set_by_class (class);
334 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
338 update_spacings (GtkDialog *dialog)
340 GtkDialogPrivate *priv = dialog->priv;
341 gint content_area_border;
342 gint content_area_spacing;
344 gint action_area_border;
346 gtk_widget_style_get (GTK_WIDGET (dialog),
347 "content-area-border", &content_area_border,
348 "content-area-spacing", &content_area_spacing,
349 "button-spacing", &button_spacing,
350 "action-area-border", &action_area_border,
354 gtk_container_set_border_width (GTK_CONTAINER (priv->vbox),
355 content_area_border);
356 if (!_gtk_box_get_spacing_set (GTK_BOX (priv->vbox)))
358 gtk_box_set_spacing (GTK_BOX (priv->vbox), content_area_spacing);
359 _gtk_box_set_spacing_set (GTK_BOX (priv->vbox), FALSE);
361 gtk_box_set_spacing (GTK_BOX (priv->action_area),
363 gtk_container_set_border_width (GTK_CONTAINER (priv->action_area),
368 gtk_dialog_init (GtkDialog *dialog)
370 GtkDialogPrivate *priv;
372 dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
377 /* To avoid breaking old code that prevents destroy on delete event
378 * by connecting a handler, we have to have the FIRST signal
379 * connection on the dialog.
381 g_signal_connect (dialog,
383 G_CALLBACK (gtk_dialog_delete_event_handler),
386 priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
387 gtk_container_add (GTK_CONTAINER (dialog), priv->vbox);
388 gtk_widget_show (priv->vbox);
390 priv->action_area = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
392 gtk_button_box_set_layout (GTK_BUTTON_BOX (priv->action_area),
395 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->action_area,
397 gtk_widget_show (priv->action_area);
399 gtk_window_set_type_hint (GTK_WINDOW (dialog),
400 GDK_WINDOW_TYPE_HINT_DIALOG);
401 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
403 update_spacings (dialog);
406 static GtkBuildableIface *parent_buildable_iface;
409 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
411 parent_buildable_iface = g_type_interface_peek_parent (iface);
412 iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
413 iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
414 iface->custom_finished = gtk_dialog_buildable_custom_finished;
418 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
420 const gchar *childname)
422 GtkDialogPrivate *priv = GTK_DIALOG (buildable)->priv;
424 if (strcmp (childname, "vbox") == 0)
425 return G_OBJECT (priv->vbox);
426 else if (strcmp (childname, "action_area") == 0)
427 return G_OBJECT (priv->action_area);
429 return parent_buildable_iface->get_internal_child (buildable,
435 gtk_dialog_delete_event_handler (GtkWidget *widget,
439 /* emit response signal */
440 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
442 /* Do the destroy by default */
446 /* A far too tricky heuristic for getting the right initial
447 * focus widget if none was set. What we do is we focus the first
448 * widget in the tab chain, but if this results in the focus
449 * ending up on one of the response widgets _other_ than the
450 * default response, we focus the default response instead.
452 * Additionally, skip selectable labels when looking for the
453 * right initial focus widget.
456 gtk_dialog_map (GtkWidget *widget)
458 GtkWidget *default_widget, *focus;
459 GtkWindow *window = GTK_WINDOW (widget);
460 GtkDialog *dialog = GTK_DIALOG (widget);
461 GtkDialogPrivate *priv = dialog->priv;
463 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
465 focus = gtk_window_get_focus (window);
468 GList *children, *tmp_list;
469 GtkWidget *first_focus = NULL;
473 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
475 focus = gtk_window_get_focus (window);
476 if (GTK_IS_LABEL (focus) &&
477 !gtk_label_get_current_uri (GTK_LABEL (focus)))
478 gtk_label_select_region (GTK_LABEL (focus), 0, 0);
480 if (first_focus == NULL)
482 else if (first_focus == focus)
485 if (!GTK_IS_LABEL (focus))
490 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
494 GtkWidget *child = tmp_list->data;
496 default_widget = gtk_window_get_default_widget (window);
497 if ((focus == NULL || child == focus) &&
498 child != default_widget &&
501 gtk_widget_grab_focus (default_widget);
505 tmp_list = tmp_list->next;
508 g_list_free (children);
513 gtk_dialog_style_updated (GtkWidget *widget)
515 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->style_updated (widget);
517 update_spacings (GTK_DIALOG (widget));
521 dialog_find_button (GtkDialog *dialog,
524 GtkDialogPrivate *priv = dialog->priv;
525 GtkWidget *child = NULL;
526 GList *children, *tmp_list;
528 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
530 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
532 ResponseData *rd = get_response_data (tmp_list->data, FALSE);
534 if (rd && rd->response_id == response_id)
536 child = tmp_list->data;
541 g_list_free (children);
547 gtk_dialog_close (GtkDialog *dialog)
549 /* Synthesize delete_event to close dialog. */
551 GtkWidget *widget = GTK_WIDGET (dialog);
554 event = gdk_event_new (GDK_DELETE);
556 event->any.window = g_object_ref (gtk_widget_get_window (widget));
557 event->any.send_event = TRUE;
559 gtk_main_do_event (event);
560 gdk_event_free (event);
566 * Creates a new dialog box.
568 * Widgets should not be packed into this #GtkWindow
569 * directly, but into the @vbox and @action_area, as described above.
571 * Returns: the new dialog as a #GtkWidget
574 gtk_dialog_new (void)
576 return g_object_new (GTK_TYPE_DIALOG, NULL);
580 gtk_dialog_new_empty (const gchar *title,
582 GtkDialogFlags flags)
586 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
589 gtk_window_set_title (GTK_WINDOW (dialog), title);
592 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
594 if (flags & GTK_DIALOG_MODAL)
595 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
597 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
598 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
600 return GTK_WIDGET (dialog);
604 * gtk_dialog_new_with_buttons:
605 * @title: (allow-none): Title of the dialog, or %NULL
606 * @parent: (allow-none): Transient parent of the dialog, or %NULL
607 * @flags: from #GtkDialogFlags
608 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
609 * @...: response ID for first button, then additional buttons, ending with %NULL
611 * Creates a new #GtkDialog with title @title (or %NULL for the default
612 * title; see gtk_window_set_title()) and transient parent @parent (or
613 * %NULL for none; see gtk_window_set_transient_for()). The @flags
614 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
615 * and/or to have it destroyed along with its transient parent
616 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
617 * text/response ID pairs should be listed, with a %NULL pointer ending
618 * the list. Button text can be either a stock ID such as
619 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
620 * any positive number, or one of the values in the #GtkResponseType
621 * enumeration. If the user clicks one of these dialog buttons,
622 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
623 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
624 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
625 * However, destroying a dialog does not emit the ::response signal;
626 * so be careful relying on ::response when using the
627 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
628 * so the first button in the list will be the leftmost button in the dialog.
630 * Here's a simple example:
632 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
634 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
636 * GTK_RESPONSE_ACCEPT,
638 * GTK_RESPONSE_REJECT,
642 * Return value: a new #GtkDialog
645 gtk_dialog_new_with_buttons (const gchar *title,
647 GtkDialogFlags flags,
648 const gchar *first_button_text,
654 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
656 va_start (args, first_button_text);
658 gtk_dialog_add_buttons_valist (dialog,
664 return GTK_WIDGET (dialog);
668 response_data_free (gpointer data)
670 g_slice_free (ResponseData, data);
674 get_response_data (GtkWidget *widget,
677 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
678 "gtk-dialog-response-data");
680 if (ad == NULL && create)
682 ad = g_slice_new (ResponseData);
684 g_object_set_data_full (G_OBJECT (widget),
685 I_("gtk-dialog-response-data"),
694 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
698 response_id = gtk_dialog_get_response_for_widget (dialog, widget);
700 gtk_dialog_response (dialog, response_id);
704 * gtk_dialog_add_action_widget:
705 * @dialog: a #GtkDialog
706 * @child: an activatable widget
707 * @response_id: response ID for @child
709 * Adds an activatable widget to the action area of a #GtkDialog,
710 * connecting a signal handler that will emit the #GtkDialog::response
711 * signal on the dialog when the widget is activated. The widget is
712 * appended to the end of the dialog's action area. If you want to add a
713 * non-activatable widget, simply pack it into the @action_area field
714 * of the #GtkDialog struct.
717 gtk_dialog_add_action_widget (GtkDialog *dialog,
721 GtkDialogPrivate *priv;
725 g_return_if_fail (GTK_IS_DIALOG (dialog));
726 g_return_if_fail (GTK_IS_WIDGET (child));
730 ad = get_response_data (child, TRUE);
732 ad->response_id = response_id;
734 if (GTK_IS_BUTTON (child))
735 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
737 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
743 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
745 g_signal_connect_closure_by_id (child,
752 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
754 gtk_box_pack_end (GTK_BOX (priv->action_area),
758 if (response_id == GTK_RESPONSE_HELP)
759 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
763 * gtk_dialog_add_button:
764 * @dialog: a #GtkDialog
765 * @button_text: text of button, or stock ID
766 * @response_id: response ID for the button
768 * Adds a button with the given text (or a stock button, if @button_text is a
769 * stock ID) and sets things up so that clicking the button will emit the
770 * #GtkDialog::response signal with the given @response_id. The button is
771 * appended to the end of the dialog's action area. The button widget is
772 * returned, but usually you don't need it.
774 * Return value: (transfer none): the #GtkButton widget that was added
777 gtk_dialog_add_button (GtkDialog *dialog,
778 const gchar *button_text,
783 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
784 g_return_val_if_fail (button_text != NULL, NULL);
786 button = gtk_button_new_from_stock (button_text);
788 gtk_widget_set_can_default (button, TRUE);
790 gtk_widget_show (button);
792 gtk_dialog_add_action_widget (dialog,
800 gtk_dialog_add_buttons_valist (GtkDialog *dialog,
801 const gchar *first_button_text,
807 g_return_if_fail (GTK_IS_DIALOG (dialog));
809 if (first_button_text == NULL)
812 text = first_button_text;
813 response_id = va_arg (args, gint);
817 gtk_dialog_add_button (dialog, text, response_id);
819 text = va_arg (args, gchar*);
822 response_id = va_arg (args, int);
827 * gtk_dialog_add_buttons:
828 * @dialog: a #GtkDialog
829 * @first_button_text: button text or stock ID
830 * @...: response ID for first button, then more text-response_id pairs
832 * Adds more buttons, same as calling gtk_dialog_add_button()
833 * repeatedly. The variable argument list should be %NULL-terminated
834 * as with gtk_dialog_new_with_buttons(). Each button must have both
835 * text and response ID.
838 gtk_dialog_add_buttons (GtkDialog *dialog,
839 const gchar *first_button_text,
844 va_start (args, first_button_text);
846 gtk_dialog_add_buttons_valist (dialog,
854 * gtk_dialog_set_response_sensitive:
855 * @dialog: a #GtkDialog
856 * @response_id: a response ID
857 * @setting: %TRUE for sensitive
859 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
860 * for each widget in the dialog's action area with the given @response_id.
861 * A convenient way to sensitize/desensitize dialog buttons.
864 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
868 GtkDialogPrivate *priv;
872 g_return_if_fail (GTK_IS_DIALOG (dialog));
876 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
879 while (tmp_list != NULL)
881 GtkWidget *widget = tmp_list->data;
882 ResponseData *rd = get_response_data (widget, FALSE);
884 if (rd && rd->response_id == response_id)
885 gtk_widget_set_sensitive (widget, setting);
887 tmp_list = g_list_next (tmp_list);
890 g_list_free (children);
894 * gtk_dialog_set_default_response:
895 * @dialog: a #GtkDialog
896 * @response_id: a response ID
898 * Sets the last widget in the dialog's action area with the given @response_id
899 * as the default widget for the dialog. Pressing "Enter" normally activates
900 * the default widget.
903 gtk_dialog_set_default_response (GtkDialog *dialog,
906 GtkDialogPrivate *priv;
910 g_return_if_fail (GTK_IS_DIALOG (dialog));
914 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
917 while (tmp_list != NULL)
919 GtkWidget *widget = tmp_list->data;
920 ResponseData *rd = get_response_data (widget, FALSE);
922 if (rd && rd->response_id == response_id)
923 gtk_widget_grab_default (widget);
925 tmp_list = g_list_next (tmp_list);
928 g_list_free (children);
932 * gtk_dialog_response:
933 * @dialog: a #GtkDialog
934 * @response_id: response ID
936 * Emits the #GtkDialog::response signal with the given response ID.
937 * Used to indicate that the user has responded to the dialog in some way;
938 * typically either you or gtk_dialog_run() will be monitoring the
939 * ::response signal and take appropriate action.
942 gtk_dialog_response (GtkDialog *dialog,
945 g_return_if_fail (GTK_IS_DIALOG (dialog));
947 g_signal_emit (dialog,
948 dialog_signals[RESPONSE],
962 shutdown_loop (RunInfo *ri)
964 if (g_main_loop_is_running (ri->loop))
965 g_main_loop_quit (ri->loop);
969 run_unmap_handler (GtkDialog *dialog, gpointer data)
977 run_response_handler (GtkDialog *dialog,
985 ri->response_id = response_id;
991 run_delete_handler (GtkDialog *dialog,
999 return TRUE; /* Do not destroy */
1003 run_destroy_handler (GtkDialog *dialog, gpointer data)
1007 /* shutdown_loop will be called by run_unmap_handler */
1009 ri->destroyed = TRUE;
1014 * @dialog: a #GtkDialog
1016 * Blocks in a recursive main loop until the @dialog either emits the
1017 * #GtkDialog::response signal, or is destroyed. If the dialog is
1018 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
1019 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
1020 * ::response signal emission.
1022 * Before entering the recursive main loop, gtk_dialog_run() calls
1023 * gtk_widget_show() on the dialog for you. Note that you still
1024 * need to show any children of the dialog yourself.
1026 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1027 * is disabled; if the dialog receives ::delete_event, it will not be
1028 * destroyed as windows usually are, and gtk_dialog_run() will return
1029 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1030 * will be modal. You can force gtk_dialog_run() to return at any time by
1031 * calling gtk_dialog_response() to emit the ::response signal. Destroying
1032 * the dialog during gtk_dialog_run() is a very bad idea, because your
1033 * post-run code won't know whether the dialog was destroyed or not.
1035 * After gtk_dialog_run() returns, you are responsible for hiding or
1036 * destroying the dialog if you wish to do so.
1038 * Typical usage of this function might be:
1040 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1043 * case GTK_RESPONSE_ACCEPT:
1044 * do_application_specific_something ();
1047 * do_nothing_since_dialog_was_cancelled ();
1050 * gtk_widget_destroy (dialog);
1053 * Note that even though the recursive main loop gives the effect of a
1054 * modal dialog (it prevents the user from interacting with other
1055 * windows in the same window group while the dialog is run), callbacks
1056 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1057 * be triggered during a gtk_dialog_run() call.
1059 * Return value: response ID
1062 gtk_dialog_run (GtkDialog *dialog)
1064 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1066 gulong response_handler;
1067 gulong unmap_handler;
1068 gulong destroy_handler;
1069 gulong delete_handler;
1071 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1073 g_object_ref (dialog);
1075 was_modal = gtk_window_get_modal (GTK_WINDOW (dialog));
1077 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1079 if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1080 gtk_widget_show (GTK_WIDGET (dialog));
1083 g_signal_connect (dialog,
1085 G_CALLBACK (run_response_handler),
1089 g_signal_connect (dialog,
1091 G_CALLBACK (run_unmap_handler),
1095 g_signal_connect (dialog,
1097 G_CALLBACK (run_delete_handler),
1101 g_signal_connect (dialog,
1103 G_CALLBACK (run_destroy_handler),
1106 ri.loop = g_main_loop_new (NULL, FALSE);
1108 GDK_THREADS_LEAVE ();
1109 g_main_loop_run (ri.loop);
1110 GDK_THREADS_ENTER ();
1112 g_main_loop_unref (ri.loop);
1119 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1121 g_signal_handler_disconnect (dialog, response_handler);
1122 g_signal_handler_disconnect (dialog, unmap_handler);
1123 g_signal_handler_disconnect (dialog, delete_handler);
1124 g_signal_handler_disconnect (dialog, destroy_handler);
1127 g_object_unref (dialog);
1129 return ri.response_id;
1133 * gtk_dialog_get_widget_for_response:
1134 * @dialog: a #GtkDialog
1135 * @response_id: the response ID used by the @dialog widget
1137 * Gets the widget button that uses the given response ID in the action area
1140 * Returns: (transfer none): the @widget button that uses the given
1141 * @response_id, or %NULL.
1146 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1149 GtkDialogPrivate *priv;
1153 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1155 priv = dialog->priv;
1157 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
1159 tmp_list = children;
1160 while (tmp_list != NULL)
1162 GtkWidget *widget = tmp_list->data;
1163 ResponseData *rd = get_response_data (widget, FALSE);
1165 if (rd && rd->response_id == response_id)
1167 g_list_free (children);
1171 tmp_list = g_list_next (tmp_list);
1174 g_list_free (children);
1180 * gtk_dialog_get_response_for_widget:
1181 * @dialog: a #GtkDialog
1182 * @widget: a widget in the action area of @dialog
1184 * Gets the response id of a widget in the action area
1187 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1188 * if @widget doesn't have a response id set.
1193 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1198 rd = get_response_data (widget, FALSE);
1200 return GTK_RESPONSE_NONE;
1202 return rd->response_id;
1206 * gtk_alternative_dialog_button_order:
1207 * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1209 * Returns %TRUE if dialogs are expected to use an alternative
1210 * button order on the screen @screen. See
1211 * gtk_dialog_set_alternative_button_order() for more details
1212 * about alternative button order.
1214 * If you need to use this function, you should probably connect
1215 * to the ::notify:gtk-alternative-button-order signal on the
1216 * #GtkSettings object associated to @screen, in order to be
1217 * notified if the button order setting changes.
1219 * Returns: Whether the alternative button order should be used
1224 gtk_alternative_dialog_button_order (GdkScreen *screen)
1226 GtkSettings *settings;
1230 settings = gtk_settings_get_for_screen (screen);
1232 settings = gtk_settings_get_default ();
1234 g_object_get (settings,
1235 "gtk-alternative-button-order", &result, NULL);
1241 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1242 gint first_response_id,
1245 GtkDialogPrivate *priv = dialog->priv;
1250 response_id = first_response_id;
1252 while (response_id != -1)
1254 /* reorder child with response_id to position */
1255 child = dialog_find_button (dialog, response_id);
1256 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1258 response_id = va_arg (args, gint);
1264 * gtk_dialog_set_alternative_button_order:
1265 * @dialog: a #GtkDialog
1266 * @first_response_id: a response id used by one @dialog's buttons
1267 * @...: a list of more response ids of @dialog's buttons, terminated by -1
1269 * Sets an alternative button order. If the
1270 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1271 * the dialog buttons are reordered according to the order of the
1272 * response ids passed to this function.
1274 * By default, GTK+ dialogs use the button order advocated by the Gnome
1275 * <ulink url="http://library.gnome.org/devel/hig-book/stable/">Human
1276 * Interface Guidelines</ulink> with the affirmative button at the far
1277 * right, and the cancel button left of it. But the builtin GTK+ dialogs
1278 * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1279 * which is more suitable on some platforms, e.g. Windows.
1281 * Use this function after adding all the buttons to your dialog, as the
1282 * following example shows:
1284 * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1286 * GTK_RESPONSE_CANCEL);
1288 * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1292 * gtk_widget_grab_default (ok_button);
1294 * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1296 * GTK_RESPONSE_HELP);
1298 * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1300 * GTK_RESPONSE_CANCEL,
1301 * GTK_RESPONSE_HELP,
1308 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1309 gint first_response_id,
1315 g_return_if_fail (GTK_IS_DIALOG (dialog));
1317 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1318 if (!gtk_alternative_dialog_button_order (screen))
1321 va_start (args, first_response_id);
1323 gtk_dialog_set_alternative_button_order_valist (dialog,
1329 * gtk_dialog_set_alternative_button_order_from_array:
1330 * @dialog: a #GtkDialog
1331 * @n_params: the number of response ids in @new_order
1332 * @new_order: (array length=n_params): an array of response ids of
1335 * Sets an alternative button order. If the
1336 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1337 * the dialog buttons are reordered according to the order of the
1338 * response ids in @new_order.
1340 * See gtk_dialog_set_alternative_button_order() for more information.
1342 * This function is for use by language bindings.
1347 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1351 GtkDialogPrivate *priv = dialog->priv;
1356 g_return_if_fail (GTK_IS_DIALOG (dialog));
1357 g_return_if_fail (new_order != NULL);
1359 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1360 if (!gtk_alternative_dialog_button_order (screen))
1363 for (position = 0; position < n_params; position++)
1365 /* reorder child with response_id to position */
1366 child = dialog_find_button (dialog, new_order[position]);
1367 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1378 GtkBuilder *builder;
1381 } ActionWidgetsSubParserData;
1384 attributes_start_element (GMarkupParseContext *context,
1385 const gchar *element_name,
1386 const gchar **names,
1387 const gchar **values,
1391 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1394 if (strcmp (element_name, "action-widget") == 0)
1396 for (i = 0; names[i]; i++)
1397 if (strcmp (names[i], "response") == 0)
1398 parser_data->response = g_strdup (values[i]);
1400 else if (strcmp (element_name, "action-widgets") == 0)
1403 g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1407 attributes_text_element (GMarkupParseContext *context,
1413 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1414 ActionWidgetInfo *item;
1416 if (!parser_data->response)
1419 item = g_new (ActionWidgetInfo, 1);
1420 item->widget_name = g_strndup (text, text_len);
1421 item->response_id = parser_data->response;
1422 parser_data->items = g_slist_prepend (parser_data->items, item);
1423 parser_data->response = NULL;
1426 static const GMarkupParser attributes_parser =
1428 attributes_start_element,
1430 attributes_text_element,
1434 gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
1435 GtkBuilder *builder,
1437 const gchar *tagname,
1438 GMarkupParser *parser,
1441 ActionWidgetsSubParserData *parser_data;
1446 if (strcmp (tagname, "action-widgets") == 0)
1448 parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1449 parser_data->dialog = GTK_DIALOG (buildable);
1450 parser_data->items = NULL;
1452 *parser = attributes_parser;
1453 *data = parser_data;
1457 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1458 tagname, parser, data);
1462 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1463 GtkBuilder *builder,
1465 const gchar *tagname,
1468 GtkDialog *dialog = GTK_DIALOG (buildable);
1469 GtkDialogPrivate *priv = dialog->priv;
1471 ActionWidgetsSubParserData *parser_data;
1476 if (strcmp (tagname, "action-widgets"))
1478 parent_buildable_iface->custom_finished (buildable, builder, child,
1479 tagname, user_data);
1483 parser_data = (ActionWidgetsSubParserData*)user_data;
1484 parser_data->items = g_slist_reverse (parser_data->items);
1486 for (l = parser_data->items; l; l = l->next)
1488 ActionWidgetInfo *item = l->data;
1490 object = gtk_builder_get_object (builder, item->widget_name);
1493 g_warning ("Unknown object %s specified in action-widgets of %s",
1495 gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1499 ad = get_response_data (GTK_WIDGET (object), TRUE);
1500 ad->response_id = g_ascii_strtoll (item->response_id, NULL, 10);
1502 if (GTK_IS_BUTTON (object))
1503 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1505 signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1511 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1513 g_signal_connect_closure_by_id (object,
1520 if (ad->response_id == GTK_RESPONSE_HELP)
1521 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area),
1522 GTK_WIDGET (object), TRUE);
1524 g_free (item->widget_name);
1525 g_free (item->response_id);
1528 g_slist_free (parser_data->items);
1529 g_slice_free (ActionWidgetsSubParserData, parser_data);
1533 * gtk_dialog_get_action_area:
1534 * @dialog: a #GtkDialog
1536 * Returns the action area of @dialog.
1538 * Returns: (transfer none): the action area.
1543 gtk_dialog_get_action_area (GtkDialog *dialog)
1545 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1547 return dialog->priv->action_area;
1551 * gtk_dialog_get_content_area:
1552 * @dialog: a #GtkDialog
1554 * Returns the content area of @dialog.
1556 * Returns: (transfer none): the content area #GtkBox.
1561 gtk_dialog_get_content_area (GtkDialog *dialog)
1563 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1565 return dialog->priv->vbox;