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"
44 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_DIALOG, GtkDialogPrivate))
47 guint ignore_separator : 1;
50 typedef struct _ResponseData ResponseData;
57 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
58 const gchar *first_button_text,
61 static gboolean gtk_dialog_delete_event_handler (GtkWidget *widget,
65 static void gtk_dialog_set_property (GObject *object,
69 static void gtk_dialog_get_property (GObject *object,
73 static void gtk_dialog_style_set (GtkWidget *widget,
74 GtkStyle *prev_style);
75 static void gtk_dialog_map (GtkWidget *widget);
77 static void gtk_dialog_close (GtkDialog *dialog);
79 static ResponseData * get_response_data (GtkWidget *widget,
82 static void gtk_dialog_buildable_interface_init (GtkBuildableIface *iface);
83 static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
85 const gchar *childname);
86 static gboolean gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
90 GMarkupParser *parser,
92 static void gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
110 static guint dialog_signals[LAST_SIGNAL];
112 G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
113 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
114 gtk_dialog_buildable_interface_init))
117 gtk_dialog_class_init (GtkDialogClass *class)
119 GObjectClass *gobject_class;
120 GtkWidgetClass *widget_class;
121 GtkBindingSet *binding_set;
123 gobject_class = G_OBJECT_CLASS (class);
124 widget_class = GTK_WIDGET_CLASS (class);
126 gobject_class->set_property = gtk_dialog_set_property;
127 gobject_class->get_property = gtk_dialog_get_property;
129 widget_class->map = gtk_dialog_map;
130 widget_class->style_set = gtk_dialog_style_set;
132 class->close = gtk_dialog_close;
134 g_type_class_add_private (gobject_class, sizeof (GtkDialogPrivate));
137 * GtkDialog:has-separator:
139 * When %TRUE, the dialog has a separator bar above its buttons.
141 g_object_class_install_property (gobject_class,
143 g_param_spec_boolean ("has-separator",
145 P_("The dialog has a separator bar above its buttons"),
147 GTK_PARAM_READWRITE));
150 * GtkDialog::response:
151 * @dialog: the object on which the signal is emitted
152 * @response_id: the response ID
154 * Emitted when an action widget is clicked, the dialog receives a
155 * delete event, or the application programmer calls gtk_dialog_response().
156 * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
157 * Otherwise, it depends on which action widget was clicked.
159 dialog_signals[RESPONSE] =
160 g_signal_new (I_("response"),
161 G_OBJECT_CLASS_TYPE (class),
163 G_STRUCT_OFFSET (GtkDialogClass, response),
165 _gtk_marshal_VOID__INT,
172 * The ::close signal is a
173 * <link linkend="keybinding-signals">keybinding signal</link>
174 * which gets emitted when the user uses a keybinding to close
177 * The default binding for this signal is the Escape key.
179 dialog_signals[CLOSE] =
180 g_signal_new (I_("close"),
181 G_OBJECT_CLASS_TYPE (class),
182 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
183 G_STRUCT_OFFSET (GtkDialogClass, close),
185 _gtk_marshal_VOID__VOID,
188 gtk_widget_class_install_style_property (widget_class,
189 g_param_spec_int ("content-area-border",
190 P_("Content area border"),
191 P_("Width of border around the main dialog area"),
195 GTK_PARAM_READABLE));
197 * GtkDialog:content-area-spacing:
199 * The default spacing used between elements of the
200 * content area of the dialog, as returned by
201 * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
202 * was called on that widget directly.
206 gtk_widget_class_install_style_property (widget_class,
207 g_param_spec_int ("content-area-spacing",
208 P_("Content area spacing"),
209 P_("Spacing between elements of the main dialog area"),
213 GTK_PARAM_READABLE));
214 gtk_widget_class_install_style_property (widget_class,
215 g_param_spec_int ("button-spacing",
216 P_("Button spacing"),
217 P_("Spacing between buttons"),
221 GTK_PARAM_READABLE));
223 gtk_widget_class_install_style_property (widget_class,
224 g_param_spec_int ("action-area-border",
225 P_("Action area border"),
226 P_("Width of border around the button area at the bottom of the dialog"),
230 GTK_PARAM_READABLE));
232 binding_set = gtk_binding_set_by_class (class);
234 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0, "close", 0);
238 update_spacings (GtkDialog *dialog)
240 gint content_area_border;
241 gint content_area_spacing;
243 gint action_area_border;
245 gtk_widget_style_get (GTK_WIDGET (dialog),
246 "content-area-border", &content_area_border,
247 "content-area-spacing", &content_area_spacing,
248 "button-spacing", &button_spacing,
249 "action-area-border", &action_area_border,
252 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
253 content_area_border);
254 if (!_gtk_box_get_spacing_set (GTK_BOX (dialog->vbox)))
256 gtk_box_set_spacing (GTK_BOX (dialog->vbox), content_area_spacing);
257 _gtk_box_set_spacing_set (GTK_BOX (dialog->vbox), FALSE);
259 gtk_box_set_spacing (GTK_BOX (dialog->action_area),
261 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
266 gtk_dialog_init (GtkDialog *dialog)
268 GtkDialogPrivate *priv;
270 priv = GET_PRIVATE (dialog);
271 priv->ignore_separator = FALSE;
273 /* To avoid breaking old code that prevents destroy on delete event
274 * by connecting a handler, we have to have the FIRST signal
275 * connection on the dialog.
277 g_signal_connect (dialog,
279 G_CALLBACK (gtk_dialog_delete_event_handler),
282 dialog->vbox = gtk_vbox_new (FALSE, 0);
284 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
285 gtk_widget_show (dialog->vbox);
287 dialog->action_area = gtk_hbutton_box_new ();
289 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
292 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
294 gtk_widget_show (dialog->action_area);
296 dialog->separator = gtk_hseparator_new ();
297 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
298 gtk_widget_show (dialog->separator);
300 gtk_window_set_type_hint (GTK_WINDOW (dialog),
301 GDK_WINDOW_TYPE_HINT_DIALOG);
302 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
305 static GtkBuildableIface *parent_buildable_iface;
308 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
310 parent_buildable_iface = g_type_interface_peek_parent (iface);
311 iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
312 iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
313 iface->custom_finished = gtk_dialog_buildable_custom_finished;
317 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
319 const gchar *childname)
321 if (strcmp (childname, "vbox") == 0)
322 return G_OBJECT (GTK_DIALOG (buildable)->vbox);
323 else if (strcmp (childname, "action_area") == 0)
324 return G_OBJECT (GTK_DIALOG (buildable)->action_area);
326 return parent_buildable_iface->get_internal_child (buildable,
332 gtk_dialog_set_property (GObject *object,
339 dialog = GTK_DIALOG (object);
343 case PROP_HAS_SEPARATOR:
344 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gtk_dialog_get_property (GObject *object,
361 dialog = GTK_DIALOG (object);
365 case PROP_HAS_SEPARATOR:
366 g_value_set_boolean (value, dialog->separator != NULL);
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376 gtk_dialog_delete_event_handler (GtkWidget *widget,
380 /* emit response signal */
381 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
383 /* Do the destroy by default */
387 /* A far too tricky heuristic for getting the right initial
388 * focus widget if none was set. What we do is we focus the first
389 * widget in the tab chain, but if this results in the focus
390 * ending up on one of the response widgets _other_ than the
391 * default response, we focus the default response instead.
393 * Additionally, skip selectable labels when looking for the
394 * right initial focus widget.
397 gtk_dialog_map (GtkWidget *widget)
399 GtkWindow *window = GTK_WINDOW (widget);
400 GtkDialog *dialog = GTK_DIALOG (widget);
402 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
404 if (!window->focus_widget)
406 GList *children, *tmp_list;
407 GtkWidget *first_focus = NULL;
411 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
413 if (first_focus == NULL)
414 first_focus = window->focus_widget;
415 else if (first_focus == window->focus_widget)
417 if (!GTK_IS_LABEL (window->focus_widget))
419 if (!gtk_label_get_current_uri (GTK_LABEL (window->focus_widget)))
420 gtk_label_select_region (GTK_LABEL (window->focus_widget), 0, 0);
424 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
428 GtkWidget *child = tmp_list->data;
430 if ((window->focus_widget == NULL ||
431 child == window->focus_widget) &&
432 child != window->default_widget &&
433 window->default_widget)
435 gtk_widget_grab_focus (window->default_widget);
439 tmp_list = tmp_list->next;
442 g_list_free (children);
447 gtk_dialog_style_set (GtkWidget *widget,
448 GtkStyle *prev_style)
450 update_spacings (GTK_DIALOG (widget));
454 dialog_find_button (GtkDialog *dialog,
457 GList *children, *tmp_list;
458 GtkWidget *child = NULL;
460 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
462 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
464 ResponseData *rd = get_response_data (tmp_list->data, FALSE);
466 if (rd && rd->response_id == response_id)
468 child = tmp_list->data;
473 g_list_free (children);
479 gtk_dialog_close (GtkDialog *dialog)
481 /* Synthesize delete_event to close dialog. */
483 GtkWidget *widget = GTK_WIDGET (dialog);
486 event = gdk_event_new (GDK_DELETE);
488 event->any.window = g_object_ref (widget->window);
489 event->any.send_event = TRUE;
491 gtk_main_do_event (event);
492 gdk_event_free (event);
496 gtk_dialog_new (void)
498 return g_object_new (GTK_TYPE_DIALOG, NULL);
502 gtk_dialog_new_empty (const gchar *title,
504 GtkDialogFlags flags)
508 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
511 gtk_window_set_title (GTK_WINDOW (dialog), title);
514 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
516 if (flags & GTK_DIALOG_MODAL)
517 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
519 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
520 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
522 if (flags & GTK_DIALOG_NO_SEPARATOR)
523 gtk_dialog_set_has_separator (dialog, FALSE);
525 return GTK_WIDGET (dialog);
529 * gtk_dialog_new_with_buttons:
530 * @title: (allow-none): Title of the dialog, or %NULL
531 * @parent: (allow-none): Transient parent of the dialog, or %NULL
532 * @flags: from #GtkDialogFlags
533 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
534 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
536 * Creates a new #GtkDialog with title @title (or %NULL for the default
537 * title; see gtk_window_set_title()) and transient parent @parent (or
538 * %NULL for none; see gtk_window_set_transient_for()). The @flags
539 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
540 * and/or to have it destroyed along with its transient parent
541 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
542 * text/response ID pairs should be listed, with a %NULL pointer ending
543 * the list. Button text can be either a stock ID such as
544 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
545 * any positive number, or one of the values in the #GtkResponseType
546 * enumeration. If the user clicks one of these dialog buttons,
547 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
548 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
549 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
550 * However, destroying a dialog does not emit the ::response signal;
551 * so be careful relying on ::response when using the
552 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
553 * so the first button in the list will be the leftmost button in the dialog.
555 * Here's a simple example:
557 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
559 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
561 * GTK_RESPONSE_ACCEPT,
563 * GTK_RESPONSE_REJECT,
567 * Return value: a new #GtkDialog
570 gtk_dialog_new_with_buttons (const gchar *title,
572 GtkDialogFlags flags,
573 const gchar *first_button_text,
579 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
581 va_start (args, first_button_text);
583 gtk_dialog_add_buttons_valist (dialog,
589 return GTK_WIDGET (dialog);
593 response_data_free (gpointer data)
595 g_slice_free (ResponseData, data);
599 get_response_data (GtkWidget *widget,
602 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
603 "gtk-dialog-response-data");
605 if (ad == NULL && create)
607 ad = g_slice_new (ResponseData);
609 g_object_set_data_full (G_OBJECT (widget),
610 I_("gtk-dialog-response-data"),
619 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
623 response_id = gtk_dialog_get_response_for_widget (dialog, widget);
625 gtk_dialog_response (dialog, response_id);
629 * gtk_dialog_add_action_widget:
630 * @dialog: a #GtkDialog
631 * @child: an activatable widget
632 * @response_id: response ID for @child
634 * Adds an activatable widget to the action area of a #GtkDialog,
635 * connecting a signal handler that will emit the #GtkDialog::response
636 * signal on the dialog when the widget is activated. The widget is
637 * appended to the end of the dialog's action area. If you want to add a
638 * non-activatable widget, simply pack it into the @action_area field
639 * of the #GtkDialog struct.
642 gtk_dialog_add_action_widget (GtkDialog *dialog,
649 g_return_if_fail (GTK_IS_DIALOG (dialog));
650 g_return_if_fail (GTK_IS_WIDGET (child));
652 ad = get_response_data (child, TRUE);
654 ad->response_id = response_id;
656 if (GTK_IS_BUTTON (child))
657 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
659 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
665 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
667 g_signal_connect_closure_by_id (child,
674 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
676 gtk_box_pack_end (GTK_BOX (dialog->action_area),
680 if (response_id == GTK_RESPONSE_HELP)
681 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
685 * gtk_dialog_add_button:
686 * @dialog: a #GtkDialog
687 * @button_text: text of button, or stock ID
688 * @response_id: response ID for the button
690 * Adds a button with the given text (or a stock button, if @button_text is a
691 * stock ID) and sets things up so that clicking the button will emit the
692 * #GtkDialog::response signal with the given @response_id. The button is
693 * appended to the end of the dialog's action area. The button widget is
694 * returned, but usually you don't need it.
696 * Return value: the button widget that was added
699 gtk_dialog_add_button (GtkDialog *dialog,
700 const gchar *button_text,
705 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
706 g_return_val_if_fail (button_text != NULL, NULL);
708 button = gtk_button_new_from_stock (button_text);
710 gtk_widget_set_can_default (button, TRUE);
712 gtk_widget_show (button);
714 gtk_dialog_add_action_widget (dialog,
722 gtk_dialog_add_buttons_valist (GtkDialog *dialog,
723 const gchar *first_button_text,
729 g_return_if_fail (GTK_IS_DIALOG (dialog));
731 if (first_button_text == NULL)
734 text = first_button_text;
735 response_id = va_arg (args, gint);
739 gtk_dialog_add_button (dialog, text, response_id);
741 text = va_arg (args, gchar*);
744 response_id = va_arg (args, int);
749 * gtk_dialog_add_buttons:
750 * @dialog: a #GtkDialog
751 * @first_button_text: button text or stock ID
752 * @Varargs: response ID for first button, then more text-response_id pairs
754 * Adds more buttons, same as calling gtk_dialog_add_button()
755 * repeatedly. The variable argument list should be %NULL-terminated
756 * as with gtk_dialog_new_with_buttons(). Each button must have both
757 * text and response ID.
760 gtk_dialog_add_buttons (GtkDialog *dialog,
761 const gchar *first_button_text,
766 va_start (args, first_button_text);
768 gtk_dialog_add_buttons_valist (dialog,
776 * gtk_dialog_set_response_sensitive:
777 * @dialog: a #GtkDialog
778 * @response_id: a response ID
779 * @setting: %TRUE for sensitive
781 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
782 * for each widget in the dialog's action area with the given @response_id.
783 * A convenient way to sensitize/desensitize dialog buttons.
786 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
793 g_return_if_fail (GTK_IS_DIALOG (dialog));
795 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
798 while (tmp_list != NULL)
800 GtkWidget *widget = tmp_list->data;
801 ResponseData *rd = get_response_data (widget, FALSE);
803 if (rd && rd->response_id == response_id)
804 gtk_widget_set_sensitive (widget, setting);
806 tmp_list = g_list_next (tmp_list);
809 g_list_free (children);
813 * gtk_dialog_set_default_response:
814 * @dialog: a #GtkDialog
815 * @response_id: a response ID
817 * Sets the last widget in the dialog's action area with the given @response_id
818 * as the default widget for the dialog. Pressing "Enter" normally activates
819 * the default widget.
822 gtk_dialog_set_default_response (GtkDialog *dialog,
828 g_return_if_fail (GTK_IS_DIALOG (dialog));
830 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
833 while (tmp_list != NULL)
835 GtkWidget *widget = tmp_list->data;
836 ResponseData *rd = get_response_data (widget, FALSE);
838 if (rd && rd->response_id == response_id)
839 gtk_widget_grab_default (widget);
841 tmp_list = g_list_next (tmp_list);
844 g_list_free (children);
848 * gtk_dialog_set_has_separator:
849 * @dialog: a #GtkDialog
850 * @setting: %TRUE to have a separator
852 * Sets whether the dialog has a separator above the buttons.
856 gtk_dialog_set_has_separator (GtkDialog *dialog,
859 GtkDialogPrivate *priv;
861 g_return_if_fail (GTK_IS_DIALOG (dialog));
863 priv = GET_PRIVATE (dialog);
865 /* this might fail if we get called before _init() somehow */
866 g_assert (dialog->vbox != NULL);
868 if (priv->ignore_separator)
870 g_warning ("Ignoring the separator setting");
874 if (setting && dialog->separator == NULL)
876 dialog->separator = gtk_hseparator_new ();
877 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
879 /* The app programmer could screw this up, but, their own fault.
880 * Moves the separator just above the action area.
882 gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
883 gtk_widget_show (dialog->separator);
885 else if (!setting && dialog->separator != NULL)
887 gtk_widget_destroy (dialog->separator);
888 dialog->separator = NULL;
891 g_object_notify (G_OBJECT (dialog), "has-separator");
895 * gtk_dialog_get_has_separator:
896 * @dialog: a #GtkDialog
898 * Accessor for whether the dialog has a separator.
900 * Return value: %TRUE if the dialog has a separator
903 gtk_dialog_get_has_separator (GtkDialog *dialog)
905 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
907 return dialog->separator != NULL;
911 * gtk_dialog_response:
912 * @dialog: a #GtkDialog
913 * @response_id: response ID
915 * Emits the #GtkDialog::response signal with the given response ID.
916 * Used to indicate that the user has responded to the dialog in some way;
917 * typically either you or gtk_dialog_run() will be monitoring the
918 * ::response signal and take appropriate action.
921 gtk_dialog_response (GtkDialog *dialog,
924 g_return_if_fail (GTK_IS_DIALOG (dialog));
926 g_signal_emit (dialog,
927 dialog_signals[RESPONSE],
941 shutdown_loop (RunInfo *ri)
943 if (g_main_loop_is_running (ri->loop))
944 g_main_loop_quit (ri->loop);
948 run_unmap_handler (GtkDialog *dialog, gpointer data)
956 run_response_handler (GtkDialog *dialog,
964 ri->response_id = response_id;
970 run_delete_handler (GtkDialog *dialog,
978 return TRUE; /* Do not destroy */
982 run_destroy_handler (GtkDialog *dialog, gpointer data)
986 /* shutdown_loop will be called by run_unmap_handler */
988 ri->destroyed = TRUE;
993 * @dialog: a #GtkDialog
995 * Blocks in a recursive main loop until the @dialog either emits the
996 * #GtkDialog::response signal, or is destroyed. If the dialog is
997 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
998 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
999 * ::response signal emission.
1001 * Before entering the recursive main loop, gtk_dialog_run() calls
1002 * gtk_widget_show() on the dialog for you. Note that you still
1003 * need to show any children of the dialog yourself.
1005 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1006 * is disabled; if the dialog receives ::delete_event, it will not be
1007 * destroyed as windows usually are, and gtk_dialog_run() will return
1008 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1009 * will be modal. You can force gtk_dialog_run() to return at any time by
1010 * calling gtk_dialog_response() to emit the ::response signal. Destroying
1011 * the dialog during gtk_dialog_run() is a very bad idea, because your
1012 * post-run code won't know whether the dialog was destroyed or not.
1014 * After gtk_dialog_run() returns, you are responsible for hiding or
1015 * destroying the dialog if you wish to do so.
1017 * Typical usage of this function might be:
1019 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1022 * case GTK_RESPONSE_ACCEPT:
1023 * do_application_specific_something ();
1026 * do_nothing_since_dialog_was_cancelled ();
1029 * gtk_widget_destroy (dialog);
1032 * Note that even though the recursive main loop gives the effect of a
1033 * modal dialog (it prevents the user from interacting with other
1034 * windows in the same window group while the dialog is run), callbacks
1035 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1036 * be triggered during a gtk_dialog_run() call.
1038 * Return value: response ID
1041 gtk_dialog_run (GtkDialog *dialog)
1043 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1045 gulong response_handler;
1046 gulong unmap_handler;
1047 gulong destroy_handler;
1048 gulong delete_handler;
1050 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1052 g_object_ref (dialog);
1054 was_modal = GTK_WINDOW (dialog)->modal;
1056 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1058 if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1059 gtk_widget_show (GTK_WIDGET (dialog));
1062 g_signal_connect (dialog,
1064 G_CALLBACK (run_response_handler),
1068 g_signal_connect (dialog,
1070 G_CALLBACK (run_unmap_handler),
1074 g_signal_connect (dialog,
1076 G_CALLBACK (run_delete_handler),
1080 g_signal_connect (dialog,
1082 G_CALLBACK (run_destroy_handler),
1085 ri.loop = g_main_loop_new (NULL, FALSE);
1087 GDK_THREADS_LEAVE ();
1088 g_main_loop_run (ri.loop);
1089 GDK_THREADS_ENTER ();
1091 g_main_loop_unref (ri.loop);
1098 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1100 g_signal_handler_disconnect (dialog, response_handler);
1101 g_signal_handler_disconnect (dialog, unmap_handler);
1102 g_signal_handler_disconnect (dialog, delete_handler);
1103 g_signal_handler_disconnect (dialog, destroy_handler);
1106 g_object_unref (dialog);
1108 return ri.response_id;
1112 _gtk_dialog_set_ignore_separator (GtkDialog *dialog,
1113 gboolean ignore_separator)
1115 GtkDialogPrivate *priv;
1117 priv = GET_PRIVATE (dialog);
1118 priv->ignore_separator = ignore_separator;
1122 * gtk_dialog_get_widget_for_response:
1123 * @dialog: a #GtkDialog
1124 * @response_id: the response ID used by the @dialog widget
1126 * Gets the widget button that uses the given response ID in the action area
1129 * Returns: the @widget button that uses the given @response_id, or %NULL.
1134 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1140 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1142 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
1144 tmp_list = children;
1145 while (tmp_list != NULL)
1147 GtkWidget *widget = tmp_list->data;
1148 ResponseData *rd = get_response_data (widget, FALSE);
1150 if (rd && rd->response_id == response_id)
1152 g_list_free (children);
1156 tmp_list = g_list_next (tmp_list);
1159 g_list_free (children);
1165 * gtk_dialog_get_response_for_widget:
1166 * @dialog: a #GtkDialog
1167 * @widget: a widget in the action area of @dialog
1169 * Gets the response id of a widget in the action area
1172 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1173 * if @widget doesn't have a response id set.
1178 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1183 rd = get_response_data (widget, FALSE);
1185 return GTK_RESPONSE_NONE;
1187 return rd->response_id;
1191 * gtk_alternative_dialog_button_order:
1192 * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1194 * Returns %TRUE if dialogs are expected to use an alternative
1195 * button order on the screen @screen. See
1196 * gtk_dialog_set_alternative_button_order() for more details
1197 * about alternative button order.
1199 * If you need to use this function, you should probably connect
1200 * to the ::notify:gtk-alternative-button-order signal on the
1201 * #GtkSettings object associated to @screen, in order to be
1202 * notified if the button order setting changes.
1204 * Returns: Whether the alternative button order should be used
1209 gtk_alternative_dialog_button_order (GdkScreen *screen)
1211 GtkSettings *settings;
1215 settings = gtk_settings_get_for_screen (screen);
1217 settings = gtk_settings_get_default ();
1219 g_object_get (settings,
1220 "gtk-alternative-button-order", &result, NULL);
1226 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1227 gint first_response_id,
1234 response_id = first_response_id;
1236 while (response_id != -1)
1238 /* reorder child with response_id to position */
1239 child = dialog_find_button (dialog, response_id);
1240 gtk_box_reorder_child (GTK_BOX (dialog->action_area), child, position);
1242 response_id = va_arg (args, gint);
1248 * gtk_dialog_set_alternative_button_order:
1249 * @dialog: a #GtkDialog
1250 * @first_response_id: a response id used by one @dialog's buttons
1251 * @Varargs: a list of more response ids of @dialog's buttons, terminated by -1
1253 * Sets an alternative button order. If the
1254 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1255 * the dialog buttons are reordered according to the order of the
1256 * response ids passed to this function.
1258 * By default, GTK+ dialogs use the button order advocated by the Gnome
1259 * <ulink url="http://developer.gnome.org/projects/gup/hig/2.0/">Human
1260 * Interface Guidelines</ulink> with the affirmative button at the far
1261 * right, and the cancel button left of it. But the builtin GTK+ dialogs
1262 * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1263 * which is more suitable on some platforms, e.g. Windows.
1265 * Use this function after adding all the buttons to your dialog, as the
1266 * following example shows:
1268 * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1270 * GTK_RESPONSE_CANCEL);
1272 * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1276 * gtk_widget_grab_default (ok_button);
1278 * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1280 * GTK_RESPONSE_HELP);
1282 * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1284 * GTK_RESPONSE_CANCEL,
1285 * GTK_RESPONSE_HELP,
1292 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1293 gint first_response_id,
1299 g_return_if_fail (GTK_IS_DIALOG (dialog));
1301 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1302 if (!gtk_alternative_dialog_button_order (screen))
1305 va_start (args, first_response_id);
1307 gtk_dialog_set_alternative_button_order_valist (dialog,
1313 * gtk_dialog_set_alternative_button_order_from_array:
1314 * @dialog: a #GtkDialog
1315 * @n_params: the number of response ids in @new_order
1316 * @new_order: an array of response ids of @dialog's buttons
1318 * Sets an alternative button order. If the
1319 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1320 * the dialog buttons are reordered according to the order of the
1321 * response ids in @new_order.
1323 * See gtk_dialog_set_alternative_button_order() for more information.
1325 * This function is for use by language bindings.
1330 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1338 g_return_if_fail (GTK_IS_DIALOG (dialog));
1339 g_return_if_fail (new_order != NULL);
1341 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1342 if (!gtk_alternative_dialog_button_order (screen))
1345 for (position = 0; position < n_params; position++)
1347 /* reorder child with response_id to position */
1348 child = dialog_find_button (dialog, new_order[position]);
1349 gtk_box_reorder_child (GTK_BOX (dialog->action_area), child, position);
1360 GtkBuilder *builder;
1363 } ActionWidgetsSubParserData;
1366 attributes_start_element (GMarkupParseContext *context,
1367 const gchar *element_name,
1368 const gchar **names,
1369 const gchar **values,
1373 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1376 if (strcmp (element_name, "action-widget") == 0)
1378 for (i = 0; names[i]; i++)
1379 if (strcmp (names[i], "response") == 0)
1380 parser_data->response = g_strdup (values[i]);
1382 else if (strcmp (element_name, "action-widgets") == 0)
1385 g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1389 attributes_text_element (GMarkupParseContext *context,
1395 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1396 ActionWidgetInfo *item;
1398 if (!parser_data->response)
1401 item = g_new (ActionWidgetInfo, 1);
1402 item->widget_name = g_strndup (text, text_len);
1403 item->response_id = parser_data->response;
1404 parser_data->items = g_slist_prepend (parser_data->items, item);
1405 parser_data->response = NULL;
1408 static const GMarkupParser attributes_parser =
1410 attributes_start_element,
1412 attributes_text_element,
1416 gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
1417 GtkBuilder *builder,
1419 const gchar *tagname,
1420 GMarkupParser *parser,
1423 ActionWidgetsSubParserData *parser_data;
1428 if (strcmp (tagname, "action-widgets") == 0)
1430 parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1431 parser_data->dialog = GTK_DIALOG (buildable);
1432 parser_data->items = NULL;
1434 *parser = attributes_parser;
1435 *data = parser_data;
1439 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1440 tagname, parser, data);
1444 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1445 GtkBuilder *builder,
1447 const gchar *tagname,
1451 ActionWidgetsSubParserData *parser_data;
1457 if (strcmp (tagname, "action-widgets"))
1459 parent_buildable_iface->custom_finished (buildable, builder, child,
1460 tagname, user_data);
1464 dialog = GTK_DIALOG (buildable);
1465 parser_data = (ActionWidgetsSubParserData*)user_data;
1466 parser_data->items = g_slist_reverse (parser_data->items);
1468 for (l = parser_data->items; l; l = l->next)
1470 ActionWidgetInfo *item = l->data;
1472 object = gtk_builder_get_object (builder, item->widget_name);
1475 g_warning ("Unknown object %s specified in action-widgets of %s",
1477 gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1481 ad = get_response_data (GTK_WIDGET (object), TRUE);
1482 ad->response_id = atoi (item->response_id);
1484 if (GTK_IS_BUTTON (object))
1485 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1487 signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1493 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1495 g_signal_connect_closure_by_id (object,
1502 if (ad->response_id == GTK_RESPONSE_HELP)
1503 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area),
1504 GTK_WIDGET (object), TRUE);
1506 g_free (item->widget_name);
1507 g_free (item->response_id);
1510 g_slist_free (parser_data->items);
1511 g_slice_free (ActionWidgetsSubParserData, parser_data);
1515 * gtk_dialog_get_action_area:
1516 * @dialog: a #GtkDialog
1518 * Returns the action area of @dialog.
1520 * Returns: (transfer none): the action area.
1525 gtk_dialog_get_action_area (GtkDialog *dialog)
1527 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1529 return dialog->action_area;
1533 * gtk_dialog_get_content_area:
1534 * @dialog: a #GtkDialog
1536 * Returns the content area of @dialog.
1538 * Returns: (transfer none): the content area #GtkVBox.
1543 gtk_dialog_get_content_area (GtkDialog *dialog)
1545 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1547 return dialog->vbox;