1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkbutton.h"
31 #include "gtkdialog.h"
34 #include "gtkhseparator.h"
35 #include "gtkmarshalers.h"
37 #include "gdkkeysyms.h"
40 #include "gtkbindings.h"
41 #include "gtkprivate.h"
42 #include "gtkbuildable.h"
48 GtkWidget *action_area;
52 guint ignore_separator : 1;
55 typedef struct _ResponseData ResponseData;
62 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
63 const gchar *first_button_text,
66 static gboolean gtk_dialog_delete_event_handler (GtkWidget *widget,
70 static void gtk_dialog_set_property (GObject *object,
74 static void gtk_dialog_get_property (GObject *object,
78 static void gtk_dialog_style_set (GtkWidget *widget,
79 GtkStyle *prev_style);
80 static void gtk_dialog_map (GtkWidget *widget);
82 static void gtk_dialog_close (GtkDialog *dialog);
84 static ResponseData * get_response_data (GtkWidget *widget,
87 static void gtk_dialog_buildable_interface_init (GtkBuildableIface *iface);
88 static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
90 const gchar *childname);
91 static gboolean gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
95 GMarkupParser *parser,
97 static void gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
100 const gchar *tagname,
115 static guint dialog_signals[LAST_SIGNAL];
117 G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
118 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
119 gtk_dialog_buildable_interface_init))
122 gtk_dialog_class_init (GtkDialogClass *class)
124 GObjectClass *gobject_class;
125 GtkWidgetClass *widget_class;
126 GtkBindingSet *binding_set;
128 gobject_class = G_OBJECT_CLASS (class);
129 widget_class = GTK_WIDGET_CLASS (class);
131 gobject_class->set_property = gtk_dialog_set_property;
132 gobject_class->get_property = gtk_dialog_get_property;
134 widget_class->map = gtk_dialog_map;
135 widget_class->style_set = gtk_dialog_style_set;
137 class->close = gtk_dialog_close;
139 g_type_class_add_private (gobject_class, sizeof (GtkDialogPriv));
142 * GtkDialog:has-separator:
144 * When %TRUE, the dialog has a separator bar above its buttons.
146 g_object_class_install_property (gobject_class,
148 g_param_spec_boolean ("has-separator",
150 P_("The dialog has a separator bar above its buttons"),
152 GTK_PARAM_READWRITE));
155 * GtkDialog::response:
156 * @dialog: the object on which the signal is emitted
157 * @response_id: the response ID
159 * Emitted when an action widget is clicked, the dialog receives a
160 * delete event, or the application programmer calls gtk_dialog_response().
161 * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
162 * Otherwise, it depends on which action widget was clicked.
164 dialog_signals[RESPONSE] =
165 g_signal_new (I_("response"),
166 G_OBJECT_CLASS_TYPE (class),
168 G_STRUCT_OFFSET (GtkDialogClass, response),
170 _gtk_marshal_VOID__INT,
177 * The ::close signal is a
178 * <link linkend="keybinding-signals">keybinding signal</link>
179 * which gets emitted when the user uses a keybinding to close
182 * The default binding for this signal is the Escape key.
184 dialog_signals[CLOSE] =
185 g_signal_new (I_("close"),
186 G_OBJECT_CLASS_TYPE (class),
187 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
188 G_STRUCT_OFFSET (GtkDialogClass, close),
190 _gtk_marshal_VOID__VOID,
193 gtk_widget_class_install_style_property (widget_class,
194 g_param_spec_int ("content-area-border",
195 P_("Content area border"),
196 P_("Width of border around the main dialog area"),
200 GTK_PARAM_READABLE));
202 * GtkDialog:content-area-spacing:
204 * The default spacing used between elements of the
205 * content area of the dialog, as returned by
206 * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
207 * was called on that widget directly.
211 gtk_widget_class_install_style_property (widget_class,
212 g_param_spec_int ("content-area-spacing",
213 P_("Content area spacing"),
214 P_("Spacing between elements of the main dialog area"),
218 GTK_PARAM_READABLE));
219 gtk_widget_class_install_style_property (widget_class,
220 g_param_spec_int ("button-spacing",
221 P_("Button spacing"),
222 P_("Spacing between buttons"),
226 GTK_PARAM_READABLE));
228 gtk_widget_class_install_style_property (widget_class,
229 g_param_spec_int ("action-area-border",
230 P_("Action area border"),
231 P_("Width of border around the button area at the bottom of the dialog"),
235 GTK_PARAM_READABLE));
237 binding_set = gtk_binding_set_by_class (class);
239 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0, "close", 0);
243 update_spacings (GtkDialog *dialog)
245 GtkDialogPriv *priv = dialog->priv;
246 gint content_area_border;
247 gint content_area_spacing;
249 gint action_area_border;
251 gtk_widget_style_get (GTK_WIDGET (dialog),
252 "content-area-border", &content_area_border,
253 "content-area-spacing", &content_area_spacing,
254 "button-spacing", &button_spacing,
255 "action-area-border", &action_area_border,
258 gtk_container_set_border_width (GTK_CONTAINER (priv->vbox),
259 content_area_border);
260 if (!_gtk_box_get_spacing_set (GTK_BOX (priv->vbox)))
262 gtk_box_set_spacing (GTK_BOX (priv->vbox), content_area_spacing);
263 _gtk_box_set_spacing_set (GTK_BOX (priv->vbox), FALSE);
265 gtk_box_set_spacing (GTK_BOX (priv->action_area),
267 gtk_container_set_border_width (GTK_CONTAINER (priv->action_area),
272 gtk_dialog_init (GtkDialog *dialog)
276 dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
281 priv->ignore_separator = FALSE;
283 /* To avoid breaking old code that prevents destroy on delete event
284 * by connecting a handler, we have to have the FIRST signal
285 * connection on the dialog.
287 g_signal_connect (dialog,
289 G_CALLBACK (gtk_dialog_delete_event_handler),
292 priv->vbox = gtk_vbox_new (FALSE, 0);
294 gtk_container_add (GTK_CONTAINER (dialog), priv->vbox);
295 gtk_widget_show (priv->vbox);
297 priv->action_area = gtk_hbutton_box_new ();
299 gtk_button_box_set_layout (GTK_BUTTON_BOX (priv->action_area),
302 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->action_area,
304 gtk_widget_show (priv->action_area);
306 priv->separator = gtk_hseparator_new ();
307 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->separator, FALSE, TRUE, 0);
308 gtk_widget_show (priv->separator);
310 gtk_window_set_type_hint (GTK_WINDOW (dialog),
311 GDK_WINDOW_TYPE_HINT_DIALOG);
312 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
315 static GtkBuildableIface *parent_buildable_iface;
318 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
320 parent_buildable_iface = g_type_interface_peek_parent (iface);
321 iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
322 iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
323 iface->custom_finished = gtk_dialog_buildable_custom_finished;
327 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
329 const gchar *childname)
331 GtkDialogPriv *priv = GTK_DIALOG (buildable)->priv;
333 if (strcmp (childname, "vbox") == 0)
334 return G_OBJECT (priv->vbox);
335 else if (strcmp (childname, "action_area") == 0)
336 return G_OBJECT (priv->action_area);
338 return parent_buildable_iface->get_internal_child (buildable,
344 gtk_dialog_set_property (GObject *object,
351 dialog = GTK_DIALOG (object);
355 case PROP_HAS_SEPARATOR:
356 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gtk_dialog_get_property (GObject *object,
371 GtkDialog *dialog = GTK_DIALOG (object);
372 GtkDialogPriv *priv = dialog->priv;
376 case PROP_HAS_SEPARATOR:
377 g_value_set_boolean (value, priv->separator != NULL);
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 gtk_dialog_delete_event_handler (GtkWidget *widget,
391 /* emit response signal */
392 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
394 /* Do the destroy by default */
398 /* A far too tricky heuristic for getting the right initial
399 * focus widget if none was set. What we do is we focus the first
400 * widget in the tab chain, but if this results in the focus
401 * ending up on one of the response widgets _other_ than the
402 * default response, we focus the default response instead.
404 * Additionally, skip selectable labels when looking for the
405 * right initial focus widget.
408 gtk_dialog_map (GtkWidget *widget)
410 GtkWidget *default_widget, *focus;
411 GtkWindow *window = GTK_WINDOW (widget);
412 GtkDialog *dialog = GTK_DIALOG (widget);
413 GtkDialogPriv *priv = dialog->priv;
415 GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
417 focus = gtk_window_get_focus (window);
420 GList *children, *tmp_list;
421 GtkWidget *first_focus = NULL;
425 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
427 focus = gtk_window_get_focus (window);
428 if (first_focus == NULL)
430 else if (first_focus == focus)
432 if (GTK_IS_LABEL (focus) &&
433 !gtk_label_get_current_uri (GTK_LABEL (focus)))
434 gtk_label_select_region (GTK_LABEL (focus), 0, 0);
437 if (!GTK_IS_LABEL (focus))
442 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
446 GtkWidget *child = tmp_list->data;
448 default_widget = gtk_window_get_default_widget (window);
449 if ((focus == NULL || child == focus) &&
450 child != default_widget &&
453 gtk_widget_grab_focus (default_widget);
457 tmp_list = tmp_list->next;
460 g_list_free (children);
465 gtk_dialog_style_set (GtkWidget *widget,
466 GtkStyle *prev_style)
468 update_spacings (GTK_DIALOG (widget));
472 dialog_find_button (GtkDialog *dialog,
475 GtkDialogPriv *priv = dialog->priv;
476 GtkWidget *child = NULL;
477 GList *children, *tmp_list;
479 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
481 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
483 ResponseData *rd = get_response_data (tmp_list->data, FALSE);
485 if (rd && rd->response_id == response_id)
487 child = tmp_list->data;
492 g_list_free (children);
498 gtk_dialog_close (GtkDialog *dialog)
500 /* Synthesize delete_event to close dialog. */
502 GtkWidget *widget = GTK_WIDGET (dialog);
505 event = gdk_event_new (GDK_DELETE);
507 event->any.window = g_object_ref (gtk_widget_get_window (widget));
508 event->any.send_event = TRUE;
510 gtk_main_do_event (event);
511 gdk_event_free (event);
515 gtk_dialog_new (void)
517 return g_object_new (GTK_TYPE_DIALOG, NULL);
521 gtk_dialog_new_empty (const gchar *title,
523 GtkDialogFlags flags)
527 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
530 gtk_window_set_title (GTK_WINDOW (dialog), title);
533 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
535 if (flags & GTK_DIALOG_MODAL)
536 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
538 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
539 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
541 if (flags & GTK_DIALOG_NO_SEPARATOR)
542 gtk_dialog_set_has_separator (dialog, FALSE);
544 return GTK_WIDGET (dialog);
548 * gtk_dialog_new_with_buttons:
549 * @title: (allow-none): Title of the dialog, or %NULL
550 * @parent: (allow-none): Transient parent of the dialog, or %NULL
551 * @flags: from #GtkDialogFlags
552 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
553 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
555 * Creates a new #GtkDialog with title @title (or %NULL for the default
556 * title; see gtk_window_set_title()) and transient parent @parent (or
557 * %NULL for none; see gtk_window_set_transient_for()). The @flags
558 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
559 * and/or to have it destroyed along with its transient parent
560 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
561 * text/response ID pairs should be listed, with a %NULL pointer ending
562 * the list. Button text can be either a stock ID such as
563 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
564 * any positive number, or one of the values in the #GtkResponseType
565 * enumeration. If the user clicks one of these dialog buttons,
566 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
567 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
568 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
569 * However, destroying a dialog does not emit the ::response signal;
570 * so be careful relying on ::response when using the
571 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
572 * so the first button in the list will be the leftmost button in the dialog.
574 * Here's a simple example:
576 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
578 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
580 * GTK_RESPONSE_ACCEPT,
582 * GTK_RESPONSE_REJECT,
586 * Return value: a new #GtkDialog
589 gtk_dialog_new_with_buttons (const gchar *title,
591 GtkDialogFlags flags,
592 const gchar *first_button_text,
598 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
600 va_start (args, first_button_text);
602 gtk_dialog_add_buttons_valist (dialog,
608 return GTK_WIDGET (dialog);
612 response_data_free (gpointer data)
614 g_slice_free (ResponseData, data);
618 get_response_data (GtkWidget *widget,
621 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
622 "gtk-dialog-response-data");
624 if (ad == NULL && create)
626 ad = g_slice_new (ResponseData);
628 g_object_set_data_full (G_OBJECT (widget),
629 I_("gtk-dialog-response-data"),
638 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
642 response_id = gtk_dialog_get_response_for_widget (dialog, widget);
644 gtk_dialog_response (dialog, response_id);
648 * gtk_dialog_add_action_widget:
649 * @dialog: a #GtkDialog
650 * @child: an activatable widget
651 * @response_id: response ID for @child
653 * Adds an activatable widget to the action area of a #GtkDialog,
654 * connecting a signal handler that will emit the #GtkDialog::response
655 * signal on the dialog when the widget is activated. The widget is
656 * appended to the end of the dialog's action area. If you want to add a
657 * non-activatable widget, simply pack it into the @action_area field
658 * of the #GtkDialog struct.
661 gtk_dialog_add_action_widget (GtkDialog *dialog,
669 g_return_if_fail (GTK_IS_DIALOG (dialog));
670 g_return_if_fail (GTK_IS_WIDGET (child));
674 ad = get_response_data (child, TRUE);
676 ad->response_id = response_id;
678 if (GTK_IS_BUTTON (child))
679 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
681 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
687 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
689 g_signal_connect_closure_by_id (child,
696 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
698 gtk_box_pack_end (GTK_BOX (priv->action_area),
702 if (response_id == GTK_RESPONSE_HELP)
703 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
707 * gtk_dialog_add_button:
708 * @dialog: a #GtkDialog
709 * @button_text: text of button, or stock ID
710 * @response_id: response ID for the button
712 * Adds a button with the given text (or a stock button, if @button_text is a
713 * stock ID) and sets things up so that clicking the button will emit the
714 * #GtkDialog::response signal with the given @response_id. The button is
715 * appended to the end of the dialog's action area. The button widget is
716 * returned, but usually you don't need it.
718 * Return value: the button widget that was added
721 gtk_dialog_add_button (GtkDialog *dialog,
722 const gchar *button_text,
727 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
728 g_return_val_if_fail (button_text != NULL, NULL);
730 button = gtk_button_new_from_stock (button_text);
732 gtk_widget_set_can_default (button, TRUE);
734 gtk_widget_show (button);
736 gtk_dialog_add_action_widget (dialog,
744 gtk_dialog_add_buttons_valist (GtkDialog *dialog,
745 const gchar *first_button_text,
751 g_return_if_fail (GTK_IS_DIALOG (dialog));
753 if (first_button_text == NULL)
756 text = first_button_text;
757 response_id = va_arg (args, gint);
761 gtk_dialog_add_button (dialog, text, response_id);
763 text = va_arg (args, gchar*);
766 response_id = va_arg (args, int);
771 * gtk_dialog_add_buttons:
772 * @dialog: a #GtkDialog
773 * @first_button_text: button text or stock ID
774 * @Varargs: response ID for first button, then more text-response_id pairs
776 * Adds more buttons, same as calling gtk_dialog_add_button()
777 * repeatedly. The variable argument list should be %NULL-terminated
778 * as with gtk_dialog_new_with_buttons(). Each button must have both
779 * text and response ID.
782 gtk_dialog_add_buttons (GtkDialog *dialog,
783 const gchar *first_button_text,
788 va_start (args, first_button_text);
790 gtk_dialog_add_buttons_valist (dialog,
798 * gtk_dialog_set_response_sensitive:
799 * @dialog: a #GtkDialog
800 * @response_id: a response ID
801 * @setting: %TRUE for sensitive
803 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
804 * for each widget in the dialog's action area with the given @response_id.
805 * A convenient way to sensitize/desensitize dialog buttons.
808 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
816 g_return_if_fail (GTK_IS_DIALOG (dialog));
820 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
823 while (tmp_list != NULL)
825 GtkWidget *widget = tmp_list->data;
826 ResponseData *rd = get_response_data (widget, FALSE);
828 if (rd && rd->response_id == response_id)
829 gtk_widget_set_sensitive (widget, setting);
831 tmp_list = g_list_next (tmp_list);
834 g_list_free (children);
838 * gtk_dialog_set_default_response:
839 * @dialog: a #GtkDialog
840 * @response_id: a response ID
842 * Sets the last widget in the dialog's action area with the given @response_id
843 * as the default widget for the dialog. Pressing "Enter" normally activates
844 * the default widget.
847 gtk_dialog_set_default_response (GtkDialog *dialog,
854 g_return_if_fail (GTK_IS_DIALOG (dialog));
858 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
861 while (tmp_list != NULL)
863 GtkWidget *widget = tmp_list->data;
864 ResponseData *rd = get_response_data (widget, FALSE);
866 if (rd && rd->response_id == response_id)
867 gtk_widget_grab_default (widget);
869 tmp_list = g_list_next (tmp_list);
872 g_list_free (children);
876 * gtk_dialog_set_has_separator:
877 * @dialog: a #GtkDialog
878 * @setting: %TRUE to have a separator
880 * Sets whether the dialog has a separator above the buttons.
884 gtk_dialog_set_has_separator (GtkDialog *dialog,
889 g_return_if_fail (GTK_IS_DIALOG (dialog));
893 /* this might fail if we get called before _init() somehow */
894 g_assert (priv->vbox != NULL);
896 if (priv->ignore_separator)
898 g_warning ("Ignoring the separator setting");
902 if (setting && priv->separator == NULL)
904 priv->separator = gtk_hseparator_new ();
905 gtk_box_pack_end (GTK_BOX (priv->vbox), priv->separator, FALSE, TRUE, 0);
907 /* The app programmer could screw this up, but, their own fault.
908 * Moves the separator just above the action area.
910 gtk_box_reorder_child (GTK_BOX (priv->vbox), priv->separator, 1);
911 gtk_widget_show (priv->separator);
913 else if (!setting && priv->separator != NULL)
915 gtk_widget_destroy (priv->separator);
916 priv->separator = NULL;
919 g_object_notify (G_OBJECT (dialog), "has-separator");
923 * gtk_dialog_get_has_separator:
924 * @dialog: a #GtkDialog
926 * Accessor for whether the dialog has a separator.
928 * Return value: %TRUE if the dialog has a separator
931 gtk_dialog_get_has_separator (GtkDialog *dialog)
933 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
935 return dialog->priv->separator != NULL;
939 * gtk_dialog_response:
940 * @dialog: a #GtkDialog
941 * @response_id: response ID
943 * Emits the #GtkDialog::response signal with the given response ID.
944 * Used to indicate that the user has responded to the dialog in some way;
945 * typically either you or gtk_dialog_run() will be monitoring the
946 * ::response signal and take appropriate action.
949 gtk_dialog_response (GtkDialog *dialog,
952 g_return_if_fail (GTK_IS_DIALOG (dialog));
954 g_signal_emit (dialog,
955 dialog_signals[RESPONSE],
969 shutdown_loop (RunInfo *ri)
971 if (g_main_loop_is_running (ri->loop))
972 g_main_loop_quit (ri->loop);
976 run_unmap_handler (GtkDialog *dialog, gpointer data)
984 run_response_handler (GtkDialog *dialog,
992 ri->response_id = response_id;
998 run_delete_handler (GtkDialog *dialog,
1006 return TRUE; /* Do not destroy */
1010 run_destroy_handler (GtkDialog *dialog, gpointer data)
1014 /* shutdown_loop will be called by run_unmap_handler */
1016 ri->destroyed = TRUE;
1021 * @dialog: a #GtkDialog
1023 * Blocks in a recursive main loop until the @dialog either emits the
1024 * #GtkDialog::response signal, or is destroyed. If the dialog is
1025 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
1026 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
1027 * ::response signal emission.
1029 * Before entering the recursive main loop, gtk_dialog_run() calls
1030 * gtk_widget_show() on the dialog for you. Note that you still
1031 * need to show any children of the dialog yourself.
1033 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1034 * is disabled; if the dialog receives ::delete_event, it will not be
1035 * destroyed as windows usually are, and gtk_dialog_run() will return
1036 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1037 * will be modal. You can force gtk_dialog_run() to return at any time by
1038 * calling gtk_dialog_response() to emit the ::response signal. Destroying
1039 * the dialog during gtk_dialog_run() is a very bad idea, because your
1040 * post-run code won't know whether the dialog was destroyed or not.
1042 * After gtk_dialog_run() returns, you are responsible for hiding or
1043 * destroying the dialog if you wish to do so.
1045 * Typical usage of this function might be:
1047 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1050 * case GTK_RESPONSE_ACCEPT:
1051 * do_application_specific_something ();
1054 * do_nothing_since_dialog_was_cancelled ();
1057 * gtk_widget_destroy (dialog);
1060 * Note that even though the recursive main loop gives the effect of a
1061 * modal dialog (it prevents the user from interacting with other
1062 * windows in the same window group while the dialog is run), callbacks
1063 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1064 * be triggered during a gtk_dialog_run() call.
1066 * Return value: response ID
1069 gtk_dialog_run (GtkDialog *dialog)
1071 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1073 gulong response_handler;
1074 gulong unmap_handler;
1075 gulong destroy_handler;
1076 gulong delete_handler;
1078 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1080 g_object_ref (dialog);
1082 was_modal = gtk_window_get_modal (GTK_WINDOW (dialog));
1084 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1086 if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1087 gtk_widget_show (GTK_WIDGET (dialog));
1090 g_signal_connect (dialog,
1092 G_CALLBACK (run_response_handler),
1096 g_signal_connect (dialog,
1098 G_CALLBACK (run_unmap_handler),
1102 g_signal_connect (dialog,
1104 G_CALLBACK (run_delete_handler),
1108 g_signal_connect (dialog,
1110 G_CALLBACK (run_destroy_handler),
1113 ri.loop = g_main_loop_new (NULL, FALSE);
1115 GDK_THREADS_LEAVE ();
1116 g_main_loop_run (ri.loop);
1117 GDK_THREADS_ENTER ();
1119 g_main_loop_unref (ri.loop);
1126 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1128 g_signal_handler_disconnect (dialog, response_handler);
1129 g_signal_handler_disconnect (dialog, unmap_handler);
1130 g_signal_handler_disconnect (dialog, delete_handler);
1131 g_signal_handler_disconnect (dialog, destroy_handler);
1134 g_object_unref (dialog);
1136 return ri.response_id;
1140 _gtk_dialog_set_ignore_separator (GtkDialog *dialog,
1141 gboolean ignore_separator)
1143 GtkDialogPriv *priv = dialog->priv;
1145 priv->ignore_separator = ignore_separator;
1149 * gtk_dialog_get_widget_for_response:
1150 * @dialog: a #GtkDialog
1151 * @response_id: the response ID used by the @dialog widget
1153 * Gets the widget button that uses the given response ID in the action area
1156 * Returns: the @widget button that uses the given @response_id, or %NULL.
1161 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1164 GtkDialogPriv *priv;
1168 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1170 priv = dialog->priv;
1172 children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
1174 tmp_list = children;
1175 while (tmp_list != NULL)
1177 GtkWidget *widget = tmp_list->data;
1178 ResponseData *rd = get_response_data (widget, FALSE);
1180 if (rd && rd->response_id == response_id)
1182 g_list_free (children);
1186 tmp_list = g_list_next (tmp_list);
1189 g_list_free (children);
1195 * gtk_dialog_get_response_for_widget:
1196 * @dialog: a #GtkDialog
1197 * @widget: a widget in the action area of @dialog
1199 * Gets the response id of a widget in the action area
1202 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1203 * if @widget doesn't have a response id set.
1208 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1213 rd = get_response_data (widget, FALSE);
1215 return GTK_RESPONSE_NONE;
1217 return rd->response_id;
1221 * gtk_alternative_dialog_button_order:
1222 * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1224 * Returns %TRUE if dialogs are expected to use an alternative
1225 * button order on the screen @screen. See
1226 * gtk_dialog_set_alternative_button_order() for more details
1227 * about alternative button order.
1229 * If you need to use this function, you should probably connect
1230 * to the ::notify:gtk-alternative-button-order signal on the
1231 * #GtkSettings object associated to @screen, in order to be
1232 * notified if the button order setting changes.
1234 * Returns: Whether the alternative button order should be used
1239 gtk_alternative_dialog_button_order (GdkScreen *screen)
1241 GtkSettings *settings;
1245 settings = gtk_settings_get_for_screen (screen);
1247 settings = gtk_settings_get_default ();
1249 g_object_get (settings,
1250 "gtk-alternative-button-order", &result, NULL);
1256 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1257 gint first_response_id,
1260 GtkDialogPriv *priv = dialog->priv;
1265 response_id = first_response_id;
1267 while (response_id != -1)
1269 /* reorder child with response_id to position */
1270 child = dialog_find_button (dialog, response_id);
1271 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1273 response_id = va_arg (args, gint);
1279 * gtk_dialog_set_alternative_button_order:
1280 * @dialog: a #GtkDialog
1281 * @first_response_id: a response id used by one @dialog's buttons
1282 * @Varargs: a list of more response ids of @dialog's buttons, terminated by -1
1284 * Sets an alternative button order. If the
1285 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1286 * the dialog buttons are reordered according to the order of the
1287 * response ids passed to this function.
1289 * By default, GTK+ dialogs use the button order advocated by the Gnome
1290 * <ulink url="http://developer.gnome.org/projects/gup/hig/2.0/">Human
1291 * Interface Guidelines</ulink> with the affirmative button at the far
1292 * right, and the cancel button left of it. But the builtin GTK+ dialogs
1293 * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1294 * which is more suitable on some platforms, e.g. Windows.
1296 * Use this function after adding all the buttons to your dialog, as the
1297 * following example shows:
1299 * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1301 * GTK_RESPONSE_CANCEL);
1303 * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1307 * gtk_widget_grab_default (ok_button);
1309 * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1311 * GTK_RESPONSE_HELP);
1313 * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1315 * GTK_RESPONSE_CANCEL,
1316 * GTK_RESPONSE_HELP,
1323 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1324 gint first_response_id,
1330 g_return_if_fail (GTK_IS_DIALOG (dialog));
1332 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1333 if (!gtk_alternative_dialog_button_order (screen))
1336 va_start (args, first_response_id);
1338 gtk_dialog_set_alternative_button_order_valist (dialog,
1344 * gtk_dialog_set_alternative_button_order_from_array:
1345 * @dialog: a #GtkDialog
1346 * @n_params: the number of response ids in @new_order
1347 * @new_order: an array of response ids of @dialog's buttons
1349 * Sets an alternative button order. If the
1350 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1351 * the dialog buttons are reordered according to the order of the
1352 * response ids in @new_order.
1354 * See gtk_dialog_set_alternative_button_order() for more information.
1356 * This function is for use by language bindings.
1361 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1365 GtkDialogPriv *priv = dialog->priv;
1370 g_return_if_fail (GTK_IS_DIALOG (dialog));
1371 g_return_if_fail (new_order != NULL);
1373 screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1374 if (!gtk_alternative_dialog_button_order (screen))
1377 for (position = 0; position < n_params; position++)
1379 /* reorder child with response_id to position */
1380 child = dialog_find_button (dialog, new_order[position]);
1381 gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1392 GtkBuilder *builder;
1395 } ActionWidgetsSubParserData;
1398 attributes_start_element (GMarkupParseContext *context,
1399 const gchar *element_name,
1400 const gchar **names,
1401 const gchar **values,
1405 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1408 if (strcmp (element_name, "action-widget") == 0)
1410 for (i = 0; names[i]; i++)
1411 if (strcmp (names[i], "response") == 0)
1412 parser_data->response = g_strdup (values[i]);
1414 else if (strcmp (element_name, "action-widgets") == 0)
1417 g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1421 attributes_text_element (GMarkupParseContext *context,
1427 ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1428 ActionWidgetInfo *item;
1430 if (!parser_data->response)
1433 item = g_new (ActionWidgetInfo, 1);
1434 item->widget_name = g_strndup (text, text_len);
1435 item->response_id = parser_data->response;
1436 parser_data->items = g_slist_prepend (parser_data->items, item);
1437 parser_data->response = NULL;
1440 static const GMarkupParser attributes_parser =
1442 attributes_start_element,
1444 attributes_text_element,
1448 gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
1449 GtkBuilder *builder,
1451 const gchar *tagname,
1452 GMarkupParser *parser,
1455 ActionWidgetsSubParserData *parser_data;
1460 if (strcmp (tagname, "action-widgets") == 0)
1462 parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1463 parser_data->dialog = GTK_DIALOG (buildable);
1464 parser_data->items = NULL;
1466 *parser = attributes_parser;
1467 *data = parser_data;
1471 return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1472 tagname, parser, data);
1476 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1477 GtkBuilder *builder,
1479 const gchar *tagname,
1482 GtkDialog *dialog = GTK_DIALOG (buildable);
1483 GtkDialogPriv *priv = dialog->priv;
1485 ActionWidgetsSubParserData *parser_data;
1490 if (strcmp (tagname, "action-widgets"))
1492 parent_buildable_iface->custom_finished (buildable, builder, child,
1493 tagname, user_data);
1497 parser_data = (ActionWidgetsSubParserData*)user_data;
1498 parser_data->items = g_slist_reverse (parser_data->items);
1500 for (l = parser_data->items; l; l = l->next)
1502 ActionWidgetInfo *item = l->data;
1504 object = gtk_builder_get_object (builder, item->widget_name);
1507 g_warning ("Unknown object %s specified in action-widgets of %s",
1509 gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1513 ad = get_response_data (GTK_WIDGET (object), TRUE);
1514 ad->response_id = atoi (item->response_id);
1516 if (GTK_IS_BUTTON (object))
1517 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1519 signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1525 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1527 g_signal_connect_closure_by_id (object,
1534 if (ad->response_id == GTK_RESPONSE_HELP)
1535 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area),
1536 GTK_WIDGET (object), TRUE);
1538 g_free (item->widget_name);
1539 g_free (item->response_id);
1542 g_slist_free (parser_data->items);
1543 g_slice_free (ActionWidgetsSubParserData, parser_data);
1547 * gtk_dialog_get_action_area:
1548 * @dialog: a #GtkDialog
1550 * Returns the action area of @dialog.
1552 * Returns: (transfer none): the action area.
1557 gtk_dialog_get_action_area (GtkDialog *dialog)
1559 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1561 return dialog->priv->action_area;
1565 * gtk_dialog_get_content_area:
1566 * @dialog: a #GtkDialog
1568 * Returns the content area of @dialog.
1570 * Returns: (transfer none): the content area #GtkVBox.
1575 gtk_dialog_get_content_area (GtkDialog *dialog)
1577 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1579 return dialog->priv->vbox;