]> Pileus Git - ~andy/gtk/blob - gtk/gtkdialog.c
Merge branch 'gtk-3-0' into broadway
[~andy/gtk] / gtk / gtkdialog.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #include "config.h"
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "gtkbutton.h"
33 #include "gtkdialog.h"
34 #include "gtkhbbox.h"
35 #include "gtklabel.h"
36 #include "gtkmarshalers.h"
37 #include "gtkvbox.h"
38 #include "gtkmain.h"
39 #include "gtkintl.h"
40 #include "gtkbindings.h"
41 #include "gtkprivate.h"
42 #include "gtkbuildable.h"
43
44 /**
45  * SECTION:gtkdialog
46  * @Short_description: Create popup windows
47  * @Title: GtkDialog
48  * @See_also: #GtkVBox, #GtkWindow, #GtkButton
49  *
50  * Dialog boxes are a convenient way to prompt the user for a small amount
51  * of input, e.g. to display a message, ask a question, or anything else
52  * that does not require extensive effort on the user's part.
53  *
54  * GTK+ treats a dialog as a window split vertically. The top section is a
55  * #GtkVBox, and is where widgets such as a #GtkLabel or a #GtkEntry should
56  * be packed. The bottom area is known as the
57  * <structfield>action_area</structfield>. This is generally used for
58  * packing buttons into the dialog which may perform functions such as
59  * cancel, ok, or apply.
60  *
61  * #GtkDialog boxes are created with a call to gtk_dialog_new() or
62  * gtk_dialog_new_with_buttons(). gtk_dialog_new_with_buttons() is
63  * recommended; it allows you to set the dialog title, some convenient flags,
64  * and add simple buttons.
65  *
66  * If 'dialog' is a newly created dialog, the two primary areas of the
67  * window can be accessed through gtk_dialog_get_content_area() and
68  * gtk_dialog_get_action_area(), as can be seen from the example below.
69  *
70  * A 'modal' dialog (that is, one which freezes the rest of the application
71  * from user input), can be created by calling gtk_window_set_modal() on the
72  * dialog. Use the GTK_WINDOW() macro to cast the widget returned from
73  * gtk_dialog_new() into a #GtkWindow. When using gtk_dialog_new_with_buttons()
74  * you can also pass the #GTK_DIALOG_MODAL flag to make a dialog modal.
75  *
76  * If you add buttons to #GtkDialog using gtk_dialog_new_with_buttons(),
77  * gtk_dialog_add_button(), gtk_dialog_add_buttons(), or
78  * gtk_dialog_add_action_widget(), clicking the button will emit a signal
79  * called #GtkDialog::response with a response ID that you specified. GTK+
80  * will never assign a meaning to positive response IDs; these are entirely
81  * user-defined. But for convenience, you can use the response IDs in the
82  * #GtkResponseType enumeration (these all have values less than zero). If
83  * a dialog receives a delete event, the #GtkDialog::response signal will
84  * be emitted with a response ID of #GTK_RESPONSE_DELETE_EVENT.
85  *
86  * If you want to block waiting for a dialog to return before returning
87  * control flow to your code, you can call gtk_dialog_run(). This function
88  * enters a recursive main loop and waits for the user to respond to the
89  * dialog, returning the response ID corresponding to the button the user
90  * clicked.
91  *
92  * For the simple dialog in the following example, in reality you'd probably
93  * use #GtkMessageDialog to save yourself some effort. But you'd need to
94  * create the dialog contents manually if you had more than a simple message
95  * in the dialog.
96  * <example>
97  * <title>Simple GtkDialog usage</title>
98  * <programlisting>
99  * /&ast; Function to open a dialog box displaying the message provided. &ast;/
100  * void
101  * quick_message (gchar *message)
102  * {
103  *    GtkWidget *dialog, *label, *content_area;
104  *
105  *    /&ast; Create the widgets &ast;/
106  *    dialog = gtk_dialog_new_with_buttons ("Message",
107  *                                          main_application_window,
108  *                                          GTK_DIALOG_DESTROY_WITH_PARENT,
109  *                                          GTK_STOCK_OK,
110  *                                          GTK_RESPONSE_NONE,
111  *                                          NULL);
112  *    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
113  *    label = gtk_label_new (message);
114  *
115  *    /&ast; Ensure that the dialog box is destroyed when the user responds &ast;/
116  *    g_signal_connect_swapped (dialog,
117  *                              "response",
118  *                              G_CALLBACK (gtk_widget_destroy),
119  *                              dialog);
120  *
121  *    /&ast; Add the label, and show everything we've added to the dialog &ast;/
122  *
123  *    gtk_container_add (GTK_CONTAINER (content_area), label);
124  *    gtk_widget_show_all (dialog);
125  * }
126  * </programlisting>
127  * </example>
128  *
129  * <refsect2 id="GtkDialog-BUILDER-UI"><title>GtkDialog as GtkBuildable</title>
130  * <para>
131  * The GtkDialog implementation of the #GtkBuildable interface exposes the
132  * @vbox and @action_area as internal children with the names "vbox" and
133  * "action_area".
134  * </para>
135  * <para>
136  * GtkDialog supports a custom &lt;action-widgets&gt; element, which
137  * can contain multiple &lt;action-widget&gt; elements. The "response"
138  * attribute specifies a numeric response, and the content of the element
139  * is the id of widget (which should be a child of the dialogs @action_area).
140  * </para>
141  * <example>
142  * <title>A <structname>GtkDialog</structname> UI definition fragment.</title>
143  * <programlisting><![CDATA[
144  * <object class="GtkDialog" id="dialog1">
145  *   <child internal-child="vbox">"
146  *     <object class="GtkVBox" id="vbox">
147  *       <child internal-child="action_area">
148  *         <object class="GtkHButtonBox" id="button_box">
149  *           <child>
150  *             <object class="GtkButton" id="button_cancel"/>
151  *           </child>
152  *           <child>
153  *             <object class="GtkButton" id="button_ok"/>
154  *           </child>
155  *         </object>
156  *       </child>
157  *     </object>
158  *   </child>
159  *   <action-widgets>
160  *     <action-widget response="3">button_ok</action-widget>
161  *     <action-widget response="-5">button_cancel</action-widget>
162  *   </action-widgets>
163  * </object>
164  * ]]></programlisting>
165  * </example>
166  * </refsect2>
167  */
168
169 struct _GtkDialogPrivate
170 {
171   GtkWidget *vbox;
172   GtkWidget *action_area;
173 };
174
175 typedef struct _ResponseData ResponseData;
176
177 struct _ResponseData
178 {
179   gint response_id;
180 };
181
182 static void      gtk_dialog_add_buttons_valist   (GtkDialog    *dialog,
183                                                   const gchar  *first_button_text,
184                                                   va_list       args);
185
186 static gboolean  gtk_dialog_delete_event_handler (GtkWidget    *widget,
187                                                   GdkEventAny  *event,
188                                                   gpointer      user_data);
189 static void      gtk_dialog_style_updated        (GtkWidget    *widget);
190 static void      gtk_dialog_map                  (GtkWidget    *widget);
191
192 static void      gtk_dialog_close                (GtkDialog    *dialog);
193
194 static ResponseData * get_response_data          (GtkWidget    *widget,
195                                                   gboolean      create);
196
197 static void      gtk_dialog_buildable_interface_init     (GtkBuildableIface *iface);
198 static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable  *buildable,
199                                                           GtkBuilder    *builder,
200                                                           const gchar   *childname);
201 static gboolean  gtk_dialog_buildable_custom_tag_start   (GtkBuildable  *buildable,
202                                                           GtkBuilder    *builder,
203                                                           GObject       *child,
204                                                           const gchar   *tagname,
205                                                           GMarkupParser *parser,
206                                                           gpointer      *data);
207 static void      gtk_dialog_buildable_custom_finished    (GtkBuildable  *buildable,
208                                                           GtkBuilder    *builder,
209                                                           GObject       *child,
210                                                           const gchar   *tagname,
211                                                           gpointer       user_data);
212
213
214 enum {
215   PROP_0,
216   PROP_HAS_SEPARATOR
217 };
218
219 enum {
220   RESPONSE,
221   CLOSE,
222   LAST_SIGNAL
223 };
224
225 static guint dialog_signals[LAST_SIGNAL];
226
227 G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
228                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
229                                                 gtk_dialog_buildable_interface_init))
230
231 static void
232 gtk_dialog_class_init (GtkDialogClass *class)
233 {
234   GObjectClass *gobject_class;
235   GtkWidgetClass *widget_class;
236   GtkBindingSet *binding_set;
237
238   gobject_class = G_OBJECT_CLASS (class);
239   widget_class = GTK_WIDGET_CLASS (class);
240
241   widget_class->map = gtk_dialog_map;
242   widget_class->style_updated = gtk_dialog_style_updated;
243
244   class->close = gtk_dialog_close;
245
246   g_type_class_add_private (gobject_class, sizeof (GtkDialogPrivate));
247
248   /**
249    * GtkDialog::response:
250    * @dialog: the object on which the signal is emitted
251    * @response_id: the response ID
252    *
253    * Emitted when an action widget is clicked, the dialog receives a
254    * delete event, or the application programmer calls gtk_dialog_response().
255    * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
256    * Otherwise, it depends on which action widget was clicked.
257    */
258   dialog_signals[RESPONSE] =
259     g_signal_new (I_("response"),
260                   G_OBJECT_CLASS_TYPE (class),
261                   G_SIGNAL_RUN_LAST,
262                   G_STRUCT_OFFSET (GtkDialogClass, response),
263                   NULL, NULL,
264                   _gtk_marshal_VOID__INT,
265                   G_TYPE_NONE, 1,
266                   G_TYPE_INT);
267
268   /**
269    * GtkDialog::close:
270    *
271    * The ::close signal is a
272    * <link linkend="keybinding-signals">keybinding signal</link>
273    * which gets emitted when the user uses a keybinding to close
274    * the dialog.
275    *
276    * The default binding for this signal is the Escape key.
277    */
278   dialog_signals[CLOSE] =
279     g_signal_new (I_("close"),
280                   G_OBJECT_CLASS_TYPE (class),
281                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
282                   G_STRUCT_OFFSET (GtkDialogClass, close),
283                   NULL, NULL,
284                   _gtk_marshal_VOID__VOID,
285                   G_TYPE_NONE, 0);
286
287   gtk_widget_class_install_style_property (widget_class,
288                                            g_param_spec_int ("content-area-border",
289                                                              P_("Content area border"),
290                                                              P_("Width of border around the main dialog area"),
291                                                              0,
292                                                              G_MAXINT,
293                                                              2,
294                                                              GTK_PARAM_READABLE));
295   /**
296    * GtkDialog:content-area-spacing:
297    *
298    * The default spacing used between elements of the
299    * content area of the dialog, as returned by
300    * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
301    * was called on that widget directly.
302    *
303    * Since: 2.16
304    */
305   gtk_widget_class_install_style_property (widget_class,
306                                            g_param_spec_int ("content-area-spacing",
307                                                              P_("Content area spacing"),
308                                                              P_("Spacing between elements of the main dialog area"),
309                                                              0,
310                                                              G_MAXINT,
311                                                              0,
312                                                              GTK_PARAM_READABLE));
313   gtk_widget_class_install_style_property (widget_class,
314                                            g_param_spec_int ("button-spacing",
315                                                              P_("Button spacing"),
316                                                              P_("Spacing between buttons"),
317                                                              0,
318                                                              G_MAXINT,
319                                                              6,
320                                                              GTK_PARAM_READABLE));
321
322   gtk_widget_class_install_style_property (widget_class,
323                                            g_param_spec_int ("action-area-border",
324                                                              P_("Action area border"),
325                                                              P_("Width of border around the button area at the bottom of the dialog"),
326                                                              0,
327                                                              G_MAXINT,
328                                                              5,
329                                                              GTK_PARAM_READABLE));
330
331   binding_set = gtk_binding_set_by_class (class);
332
333   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
334 }
335
336 static void
337 update_spacings (GtkDialog *dialog)
338 {
339   GtkDialogPrivate *priv = dialog->priv;
340   gint content_area_border;
341   gint content_area_spacing;
342   gint button_spacing;
343   gint action_area_border;
344
345   gtk_widget_style_get (GTK_WIDGET (dialog),
346                         "content-area-border", &content_area_border,
347                         "content-area-spacing", &content_area_spacing,
348                         "button-spacing", &button_spacing,
349                         "action-area-border", &action_area_border,
350                         NULL);
351
352   gtk_container_set_border_width (GTK_CONTAINER (priv->vbox),
353                                   content_area_border);
354   if (!_gtk_box_get_spacing_set (GTK_BOX (priv->vbox)))
355     {
356       gtk_box_set_spacing (GTK_BOX (priv->vbox), content_area_spacing);
357       _gtk_box_set_spacing_set (GTK_BOX (priv->vbox), FALSE);
358     }
359   gtk_box_set_spacing (GTK_BOX (priv->action_area),
360                        button_spacing);
361   gtk_container_set_border_width (GTK_CONTAINER (priv->action_area),
362                                   action_area_border);
363 }
364
365 static void
366 gtk_dialog_init (GtkDialog *dialog)
367 {
368   GtkDialogPrivate *priv;
369
370   dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
371                                               GTK_TYPE_DIALOG,
372                                               GtkDialogPrivate);
373   priv = dialog->priv;
374
375   /* To avoid breaking old code that prevents destroy on delete event
376    * by connecting a handler, we have to have the FIRST signal
377    * connection on the dialog.
378    */
379   g_signal_connect (dialog,
380                     "delete-event",
381                     G_CALLBACK (gtk_dialog_delete_event_handler),
382                     NULL);
383
384   priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
385   gtk_container_add (GTK_CONTAINER (dialog), priv->vbox);
386   gtk_widget_show (priv->vbox);
387
388   priv->action_area = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
389
390   gtk_button_box_set_layout (GTK_BUTTON_BOX (priv->action_area),
391                              GTK_BUTTONBOX_END);
392
393   gtk_box_pack_end (GTK_BOX (priv->vbox), priv->action_area,
394                     FALSE, TRUE, 0);
395   gtk_widget_show (priv->action_area);
396
397   gtk_window_set_type_hint (GTK_WINDOW (dialog),
398                             GDK_WINDOW_TYPE_HINT_DIALOG);
399   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
400 }
401
402 static GtkBuildableIface *parent_buildable_iface;
403
404 static void
405 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
406 {
407   parent_buildable_iface = g_type_interface_peek_parent (iface);
408   iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
409   iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
410   iface->custom_finished = gtk_dialog_buildable_custom_finished;
411 }
412
413 static GObject *
414 gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
415                                          GtkBuilder   *builder,
416                                          const gchar  *childname)
417 {
418   GtkDialogPrivate *priv = GTK_DIALOG (buildable)->priv;
419
420   if (strcmp (childname, "vbox") == 0)
421     return G_OBJECT (priv->vbox);
422   else if (strcmp (childname, "action_area") == 0)
423     return G_OBJECT (priv->action_area);
424
425   return parent_buildable_iface->get_internal_child (buildable,
426                                                      builder,
427                                                      childname);
428 }
429
430 static gboolean
431 gtk_dialog_delete_event_handler (GtkWidget   *widget,
432                                  GdkEventAny *event,
433                                  gpointer     user_data)
434 {
435   /* emit response signal */
436   gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
437
438   /* Do the destroy by default */
439   return FALSE;
440 }
441
442 /* A far too tricky heuristic for getting the right initial
443  * focus widget if none was set. What we do is we focus the first
444  * widget in the tab chain, but if this results in the focus
445  * ending up on one of the response widgets _other_ than the
446  * default response, we focus the default response instead.
447  *
448  * Additionally, skip selectable labels when looking for the
449  * right initial focus widget.
450  */
451 static void
452 gtk_dialog_map (GtkWidget *widget)
453 {
454   GtkWidget *default_widget, *focus;
455   GtkWindow *window = GTK_WINDOW (widget);
456   GtkDialog *dialog = GTK_DIALOG (widget);
457   GtkDialogPrivate *priv = dialog->priv;
458
459   GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
460
461   focus = gtk_window_get_focus (window);
462   if (!focus)
463     {
464       GList *children, *tmp_list;
465       GtkWidget *first_focus = NULL;
466
467       do
468         {
469           g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
470
471           focus = gtk_window_get_focus (window);
472           if (GTK_IS_LABEL (focus) &&
473               !gtk_label_get_current_uri (GTK_LABEL (focus)))
474             gtk_label_select_region (GTK_LABEL (focus), 0, 0);
475
476           if (first_focus == NULL)
477             first_focus = focus;
478           else if (first_focus == focus)
479             break;
480
481           if (!GTK_IS_LABEL (focus))
482             break;
483         }
484       while (TRUE);
485
486       tmp_list = children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
487
488       while (tmp_list)
489         {
490           GtkWidget *child = tmp_list->data;
491
492           default_widget = gtk_window_get_default_widget (window);
493           if ((focus == NULL || child == focus) &&
494               child != default_widget &&
495               default_widget)
496             {
497               gtk_widget_grab_focus (default_widget);
498               break;
499             }
500
501           tmp_list = tmp_list->next;
502         }
503
504       g_list_free (children);
505     }
506 }
507
508 static void
509 gtk_dialog_style_updated (GtkWidget *widget)
510 {
511   GTK_WIDGET_CLASS (gtk_dialog_parent_class)->style_updated (widget);
512
513   update_spacings (GTK_DIALOG (widget));
514 }
515
516 static GtkWidget *
517 dialog_find_button (GtkDialog *dialog,
518                     gint       response_id)
519 {
520   GtkDialogPrivate *priv = dialog->priv;
521   GtkWidget *child = NULL;
522   GList *children, *tmp_list;
523
524   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
525
526   for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
527     {
528       ResponseData *rd = get_response_data (tmp_list->data, FALSE);
529
530       if (rd && rd->response_id == response_id)
531         {
532           child = tmp_list->data;
533           break;
534         }
535     }
536
537   g_list_free (children);
538
539   return child;
540 }
541
542 static void
543 gtk_dialog_close (GtkDialog *dialog)
544 {
545   /* Synthesize delete_event to close dialog. */
546
547   GtkWidget *widget = GTK_WIDGET (dialog);
548   GdkEvent *event;
549
550   event = gdk_event_new (GDK_DELETE);
551
552   event->any.window = g_object_ref (gtk_widget_get_window (widget));
553   event->any.send_event = TRUE;
554
555   gtk_main_do_event (event);
556   gdk_event_free (event);
557 }
558
559 /**
560  * gtk_dialog_new:
561  *
562  * Creates a new dialog box.
563  *
564  * Widgets should not be packed into this #GtkWindow
565  * directly, but into the @vbox and @action_area, as described above.
566  *
567  * Returns: the new dialog as a #GtkWidget
568  */
569 GtkWidget*
570 gtk_dialog_new (void)
571 {
572   return g_object_new (GTK_TYPE_DIALOG, NULL);
573 }
574
575 static GtkWidget*
576 gtk_dialog_new_empty (const gchar     *title,
577                       GtkWindow       *parent,
578                       GtkDialogFlags   flags)
579 {
580   GtkDialog *dialog;
581
582   dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
583
584   if (title)
585     gtk_window_set_title (GTK_WINDOW (dialog), title);
586
587   if (parent)
588     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
589
590   if (flags & GTK_DIALOG_MODAL)
591     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
592
593   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
594     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
595
596   return GTK_WIDGET (dialog);
597 }
598
599 /**
600  * gtk_dialog_new_with_buttons:
601  * @title: (allow-none): Title of the dialog, or %NULL
602  * @parent: (allow-none): Transient parent of the dialog, or %NULL
603  * @flags: from #GtkDialogFlags
604  * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
605  * @Varargs: response ID for first button, then additional buttons, ending with %NULL
606  *
607  * Creates a new #GtkDialog with title @title (or %NULL for the default
608  * title; see gtk_window_set_title()) and transient parent @parent (or
609  * %NULL for none; see gtk_window_set_transient_for()). The @flags
610  * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
611  * and/or to have it destroyed along with its transient parent
612  * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
613  * text/response ID pairs should be listed, with a %NULL pointer ending
614  * the list. Button text can be either a stock ID such as
615  * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
616  * any positive number, or one of the values in the #GtkResponseType
617  * enumeration. If the user clicks one of these dialog buttons,
618  * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
619  * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
620  * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
621  * However, destroying a dialog does not emit the ::response signal;
622  * so be careful relying on ::response when using the
623  * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
624  * so the first button in the list will be the leftmost button in the dialog.
625  *
626  * Here's a simple example:
627  * |[
628  *  GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
629  *                                                   main_app_window,
630  *                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
631  *                                                   GTK_STOCK_OK,
632  *                                                   GTK_RESPONSE_ACCEPT,
633  *                                                   GTK_STOCK_CANCEL,
634  *                                                   GTK_RESPONSE_REJECT,
635  *                                                   NULL);
636  * ]|
637  *
638  * Return value: a new #GtkDialog
639  **/
640 GtkWidget*
641 gtk_dialog_new_with_buttons (const gchar    *title,
642                              GtkWindow      *parent,
643                              GtkDialogFlags  flags,
644                              const gchar    *first_button_text,
645                              ...)
646 {
647   GtkDialog *dialog;
648   va_list args;
649
650   dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
651
652   va_start (args, first_button_text);
653
654   gtk_dialog_add_buttons_valist (dialog,
655                                  first_button_text,
656                                  args);
657
658   va_end (args);
659
660   return GTK_WIDGET (dialog);
661 }
662
663 static void
664 response_data_free (gpointer data)
665 {
666   g_slice_free (ResponseData, data);
667 }
668
669 static ResponseData*
670 get_response_data (GtkWidget *widget,
671                    gboolean   create)
672 {
673   ResponseData *ad = g_object_get_data (G_OBJECT (widget),
674                                         "gtk-dialog-response-data");
675
676   if (ad == NULL && create)
677     {
678       ad = g_slice_new (ResponseData);
679
680       g_object_set_data_full (G_OBJECT (widget),
681                               I_("gtk-dialog-response-data"),
682                               ad,
683                               response_data_free);
684     }
685
686   return ad;
687 }
688
689 static void
690 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
691 {
692   gint response_id;
693
694   response_id = gtk_dialog_get_response_for_widget (dialog, widget);
695
696   gtk_dialog_response (dialog, response_id);
697 }
698
699 /**
700  * gtk_dialog_add_action_widget:
701  * @dialog: a #GtkDialog
702  * @child: an activatable widget
703  * @response_id: response ID for @child
704  *
705  * Adds an activatable widget to the action area of a #GtkDialog,
706  * connecting a signal handler that will emit the #GtkDialog::response
707  * signal on the dialog when the widget is activated. The widget is
708  * appended to the end of the dialog's action area. If you want to add a
709  * non-activatable widget, simply pack it into the @action_area field
710  * of the #GtkDialog struct.
711  **/
712 void
713 gtk_dialog_add_action_widget (GtkDialog *dialog,
714                               GtkWidget *child,
715                               gint       response_id)
716 {
717   GtkDialogPrivate *priv;
718   ResponseData *ad;
719   guint signal_id;
720
721   g_return_if_fail (GTK_IS_DIALOG (dialog));
722   g_return_if_fail (GTK_IS_WIDGET (child));
723
724   priv = dialog->priv;
725
726   ad = get_response_data (child, TRUE);
727
728   ad->response_id = response_id;
729
730   if (GTK_IS_BUTTON (child))
731     signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
732   else
733     signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
734
735   if (signal_id)
736     {
737       GClosure *closure;
738
739       closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
740                                        G_OBJECT (dialog));
741       g_signal_connect_closure_by_id (child,
742                                       signal_id,
743                                       0,
744                                       closure,
745                                       FALSE);
746     }
747   else
748     g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
749
750   gtk_box_pack_end (GTK_BOX (priv->action_area),
751                     child,
752                     FALSE, TRUE, 0);
753
754   if (response_id == GTK_RESPONSE_HELP)
755     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
756 }
757
758 /**
759  * gtk_dialog_add_button:
760  * @dialog: a #GtkDialog
761  * @button_text: text of button, or stock ID
762  * @response_id: response ID for the button
763  *
764  * Adds a button with the given text (or a stock button, if @button_text is a
765  * stock ID) and sets things up so that clicking the button will emit the
766  * #GtkDialog::response signal with the given @response_id. The button is
767  * appended to the end of the dialog's action area. The button widget is
768  * returned, but usually you don't need it.
769  *
770  * Return value: (transfer none): the #GtkButton widget that was added
771  **/
772 GtkWidget*
773 gtk_dialog_add_button (GtkDialog   *dialog,
774                        const gchar *button_text,
775                        gint         response_id)
776 {
777   GtkWidget *button;
778
779   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
780   g_return_val_if_fail (button_text != NULL, NULL);
781
782   button = gtk_button_new_from_stock (button_text);
783
784   gtk_widget_set_can_default (button, TRUE);
785
786   gtk_widget_show (button);
787
788   gtk_dialog_add_action_widget (dialog,
789                                 button,
790                                 response_id);
791
792   return button;
793 }
794
795 static void
796 gtk_dialog_add_buttons_valist (GtkDialog      *dialog,
797                                const gchar    *first_button_text,
798                                va_list         args)
799 {
800   const gchar* text;
801   gint response_id;
802
803   g_return_if_fail (GTK_IS_DIALOG (dialog));
804
805   if (first_button_text == NULL)
806     return;
807
808   text = first_button_text;
809   response_id = va_arg (args, gint);
810
811   while (text != NULL)
812     {
813       gtk_dialog_add_button (dialog, text, response_id);
814
815       text = va_arg (args, gchar*);
816       if (text == NULL)
817         break;
818       response_id = va_arg (args, int);
819     }
820 }
821
822 /**
823  * gtk_dialog_add_buttons:
824  * @dialog: a #GtkDialog
825  * @first_button_text: button text or stock ID
826  * @Varargs: response ID for first button, then more text-response_id pairs
827  *
828  * Adds more buttons, same as calling gtk_dialog_add_button()
829  * repeatedly.  The variable argument list should be %NULL-terminated
830  * as with gtk_dialog_new_with_buttons(). Each button must have both
831  * text and response ID.
832  **/
833 void
834 gtk_dialog_add_buttons (GtkDialog   *dialog,
835                         const gchar *first_button_text,
836                         ...)
837 {
838   va_list args;
839
840   va_start (args, first_button_text);
841
842   gtk_dialog_add_buttons_valist (dialog,
843                                  first_button_text,
844                                  args);
845
846   va_end (args);
847 }
848
849 /**
850  * gtk_dialog_set_response_sensitive:
851  * @dialog: a #GtkDialog
852  * @response_id: a response ID
853  * @setting: %TRUE for sensitive
854  *
855  * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
856  * for each widget in the dialog's action area with the given @response_id.
857  * A convenient way to sensitize/desensitize dialog buttons.
858  **/
859 void
860 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
861                                    gint       response_id,
862                                    gboolean   setting)
863 {
864   GtkDialogPrivate *priv;
865   GList *children;
866   GList *tmp_list;
867
868   g_return_if_fail (GTK_IS_DIALOG (dialog));
869
870   priv = dialog->priv;
871
872   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
873
874   tmp_list = children;
875   while (tmp_list != NULL)
876     {
877       GtkWidget *widget = tmp_list->data;
878       ResponseData *rd = get_response_data (widget, FALSE);
879
880       if (rd && rd->response_id == response_id)
881         gtk_widget_set_sensitive (widget, setting);
882
883       tmp_list = g_list_next (tmp_list);
884     }
885
886   g_list_free (children);
887 }
888
889 /**
890  * gtk_dialog_set_default_response:
891  * @dialog: a #GtkDialog
892  * @response_id: a response ID
893  *
894  * Sets the last widget in the dialog's action area with the given @response_id
895  * as the default widget for the dialog. Pressing "Enter" normally activates
896  * the default widget.
897  **/
898 void
899 gtk_dialog_set_default_response (GtkDialog *dialog,
900                                  gint       response_id)
901 {
902   GtkDialogPrivate *priv;
903   GList *children;
904   GList *tmp_list;
905
906   g_return_if_fail (GTK_IS_DIALOG (dialog));
907
908   priv = dialog->priv;
909
910   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
911
912   tmp_list = children;
913   while (tmp_list != NULL)
914     {
915       GtkWidget *widget = tmp_list->data;
916       ResponseData *rd = get_response_data (widget, FALSE);
917
918       if (rd && rd->response_id == response_id)
919         gtk_widget_grab_default (widget);
920
921       tmp_list = g_list_next (tmp_list);
922     }
923
924   g_list_free (children);
925 }
926
927 /**
928  * gtk_dialog_response:
929  * @dialog: a #GtkDialog
930  * @response_id: response ID
931  *
932  * Emits the #GtkDialog::response signal with the given response ID.
933  * Used to indicate that the user has responded to the dialog in some way;
934  * typically either you or gtk_dialog_run() will be monitoring the
935  * ::response signal and take appropriate action.
936  **/
937 void
938 gtk_dialog_response (GtkDialog *dialog,
939                      gint       response_id)
940 {
941   g_return_if_fail (GTK_IS_DIALOG (dialog));
942
943   g_signal_emit (dialog,
944                  dialog_signals[RESPONSE],
945                  0,
946                  response_id);
947 }
948
949 typedef struct
950 {
951   GtkDialog *dialog;
952   gint response_id;
953   GMainLoop *loop;
954   gboolean destroyed;
955 } RunInfo;
956
957 static void
958 shutdown_loop (RunInfo *ri)
959 {
960   if (g_main_loop_is_running (ri->loop))
961     g_main_loop_quit (ri->loop);
962 }
963
964 static void
965 run_unmap_handler (GtkDialog *dialog, gpointer data)
966 {
967   RunInfo *ri = data;
968
969   shutdown_loop (ri);
970 }
971
972 static void
973 run_response_handler (GtkDialog *dialog,
974                       gint response_id,
975                       gpointer data)
976 {
977   RunInfo *ri;
978
979   ri = data;
980
981   ri->response_id = response_id;
982
983   shutdown_loop (ri);
984 }
985
986 static gint
987 run_delete_handler (GtkDialog *dialog,
988                     GdkEventAny *event,
989                     gpointer data)
990 {
991   RunInfo *ri = data;
992
993   shutdown_loop (ri);
994
995   return TRUE; /* Do not destroy */
996 }
997
998 static void
999 run_destroy_handler (GtkDialog *dialog, gpointer data)
1000 {
1001   RunInfo *ri = data;
1002
1003   /* shutdown_loop will be called by run_unmap_handler */
1004
1005   ri->destroyed = TRUE;
1006 }
1007
1008 /**
1009  * gtk_dialog_run:
1010  * @dialog: a #GtkDialog
1011  *
1012  * Blocks in a recursive main loop until the @dialog either emits the
1013  * #GtkDialog::response signal, or is destroyed. If the dialog is
1014  * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
1015  * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
1016  * ::response signal emission.
1017  *
1018  * Before entering the recursive main loop, gtk_dialog_run() calls
1019  * gtk_widget_show() on the dialog for you. Note that you still
1020  * need to show any children of the dialog yourself.
1021  *
1022  * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1023  * is disabled; if the dialog receives ::delete_event, it will not be
1024  * destroyed as windows usually are, and gtk_dialog_run() will return
1025  * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1026  * will be modal. You can force gtk_dialog_run() to return at any time by
1027  * calling gtk_dialog_response() to emit the ::response signal. Destroying
1028  * the dialog during gtk_dialog_run() is a very bad idea, because your
1029  * post-run code won't know whether the dialog was destroyed or not.
1030  *
1031  * After gtk_dialog_run() returns, you are responsible for hiding or
1032  * destroying the dialog if you wish to do so.
1033  *
1034  * Typical usage of this function might be:
1035  * |[
1036  *   gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1037  *   switch (result)
1038  *     {
1039  *       case GTK_RESPONSE_ACCEPT:
1040  *          do_application_specific_something ();
1041  *          break;
1042  *       default:
1043  *          do_nothing_since_dialog_was_cancelled ();
1044  *          break;
1045  *     }
1046  *   gtk_widget_destroy (dialog);
1047  * ]|
1048  *
1049  * Note that even though the recursive main loop gives the effect of a
1050  * modal dialog (it prevents the user from interacting with other
1051  * windows in the same window group while the dialog is run), callbacks
1052  * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1053  * be triggered during a gtk_dialog_run() call.
1054  *
1055  * Return value: response ID
1056  **/
1057 gint
1058 gtk_dialog_run (GtkDialog *dialog)
1059 {
1060   RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1061   gboolean was_modal;
1062   gulong response_handler;
1063   gulong unmap_handler;
1064   gulong destroy_handler;
1065   gulong delete_handler;
1066
1067   g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1068
1069   g_object_ref (dialog);
1070
1071   was_modal = gtk_window_get_modal (GTK_WINDOW (dialog));
1072   if (!was_modal)
1073     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1074
1075   if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1076     gtk_widget_show (GTK_WIDGET (dialog));
1077
1078   response_handler =
1079     g_signal_connect (dialog,
1080                       "response",
1081                       G_CALLBACK (run_response_handler),
1082                       &ri);
1083
1084   unmap_handler =
1085     g_signal_connect (dialog,
1086                       "unmap",
1087                       G_CALLBACK (run_unmap_handler),
1088                       &ri);
1089
1090   delete_handler =
1091     g_signal_connect (dialog,
1092                       "delete-event",
1093                       G_CALLBACK (run_delete_handler),
1094                       &ri);
1095
1096   destroy_handler =
1097     g_signal_connect (dialog,
1098                       "destroy",
1099                       G_CALLBACK (run_destroy_handler),
1100                       &ri);
1101
1102   ri.loop = g_main_loop_new (NULL, FALSE);
1103
1104   GDK_THREADS_LEAVE ();
1105   g_main_loop_run (ri.loop);
1106   GDK_THREADS_ENTER ();
1107
1108   g_main_loop_unref (ri.loop);
1109
1110   ri.loop = NULL;
1111
1112   if (!ri.destroyed)
1113     {
1114       if (!was_modal)
1115         gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1116
1117       g_signal_handler_disconnect (dialog, response_handler);
1118       g_signal_handler_disconnect (dialog, unmap_handler);
1119       g_signal_handler_disconnect (dialog, delete_handler);
1120       g_signal_handler_disconnect (dialog, destroy_handler);
1121     }
1122
1123   g_object_unref (dialog);
1124
1125   return ri.response_id;
1126 }
1127
1128 /**
1129  * gtk_dialog_get_widget_for_response:
1130  * @dialog: a #GtkDialog
1131  * @response_id: the response ID used by the @dialog widget
1132  *
1133  * Gets the widget button that uses the given response ID in the action area
1134  * of a dialog.
1135  *
1136  * Returns: (transfer none): the @widget button that uses the given
1137  *     @response_id, or %NULL.
1138  *
1139  * Since: 2.20
1140  */
1141 GtkWidget*
1142 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1143                                     gint       response_id)
1144 {
1145   GtkDialogPrivate *priv;
1146   GList *children;
1147   GList *tmp_list;
1148
1149   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1150
1151   priv = dialog->priv;
1152
1153   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
1154
1155   tmp_list = children;
1156   while (tmp_list != NULL)
1157     {
1158       GtkWidget *widget = tmp_list->data;
1159       ResponseData *rd = get_response_data (widget, FALSE);
1160
1161       if (rd && rd->response_id == response_id)
1162         {
1163           g_list_free (children);
1164           return widget;
1165         }
1166
1167       tmp_list = g_list_next (tmp_list);
1168     }
1169
1170   g_list_free (children);
1171
1172   return NULL;
1173 }
1174
1175 /**
1176  * gtk_dialog_get_response_for_widget:
1177  * @dialog: a #GtkDialog
1178  * @widget: a widget in the action area of @dialog
1179  *
1180  * Gets the response id of a widget in the action area
1181  * of a dialog.
1182  *
1183  * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1184  *  if @widget doesn't have a response id set.
1185  *
1186  * Since: 2.8
1187  */
1188 gint
1189 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1190                                     GtkWidget *widget)
1191 {
1192   ResponseData *rd;
1193
1194   rd = get_response_data (widget, FALSE);
1195   if (!rd)
1196     return GTK_RESPONSE_NONE;
1197   else
1198     return rd->response_id;
1199 }
1200
1201 /**
1202  * gtk_alternative_dialog_button_order:
1203  * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1204  *
1205  * Returns %TRUE if dialogs are expected to use an alternative
1206  * button order on the screen @screen. See
1207  * gtk_dialog_set_alternative_button_order() for more details
1208  * about alternative button order.
1209  *
1210  * If you need to use this function, you should probably connect
1211  * to the ::notify:gtk-alternative-button-order signal on the
1212  * #GtkSettings object associated to @screen, in order to be
1213  * notified if the button order setting changes.
1214  *
1215  * Returns: Whether the alternative button order should be used
1216  *
1217  * Since: 2.6
1218  */
1219 gboolean
1220 gtk_alternative_dialog_button_order (GdkScreen *screen)
1221 {
1222   GtkSettings *settings;
1223   gboolean result;
1224
1225   if (screen)
1226     settings = gtk_settings_get_for_screen (screen);
1227   else
1228     settings = gtk_settings_get_default ();
1229
1230   g_object_get (settings,
1231                 "gtk-alternative-button-order", &result, NULL);
1232
1233   return result;
1234 }
1235
1236 static void
1237 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1238                                                 gint       first_response_id,
1239                                                 va_list    args)
1240 {
1241   GtkDialogPrivate *priv = dialog->priv;
1242   GtkWidget *child;
1243   gint response_id;
1244   gint position;
1245
1246   response_id = first_response_id;
1247   position = 0;
1248   while (response_id != -1)
1249     {
1250       /* reorder child with response_id to position */
1251       child = dialog_find_button (dialog, response_id);
1252       gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1253
1254       response_id = va_arg (args, gint);
1255       position++;
1256     }
1257 }
1258
1259 /**
1260  * gtk_dialog_set_alternative_button_order:
1261  * @dialog: a #GtkDialog
1262  * @first_response_id: a response id used by one @dialog's buttons
1263  * @Varargs: a list of more response ids of @dialog's buttons, terminated by -1
1264  *
1265  * Sets an alternative button order. If the
1266  * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1267  * the dialog buttons are reordered according to the order of the
1268  * response ids passed to this function.
1269  *
1270  * By default, GTK+ dialogs use the button order advocated by the Gnome
1271  * <ulink url="http://library.gnome.org/devel/hig-book/stable/">Human
1272  * Interface Guidelines</ulink> with the affirmative button at the far
1273  * right, and the cancel button left of it. But the builtin GTK+ dialogs
1274  * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1275  * which is more suitable on some platforms, e.g. Windows.
1276  *
1277  * Use this function after adding all the buttons to your dialog, as the
1278  * following example shows:
1279  * |[
1280  * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1281  *                                        GTK_STOCK_CANCEL,
1282  *                                        GTK_RESPONSE_CANCEL);
1283  *
1284  * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1285  *                                    GTK_STOCK_OK,
1286  *                                    GTK_RESPONSE_OK);
1287  *
1288  * gtk_widget_grab_default (ok_button);
1289  *
1290  * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1291  *                                      GTK_STOCK_HELP,
1292  *                                      GTK_RESPONSE_HELP);
1293  *
1294  * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1295  *                                          GTK_RESPONSE_OK,
1296  *                                          GTK_RESPONSE_CANCEL,
1297  *                                          GTK_RESPONSE_HELP,
1298  *                                          -1);
1299  * ]|
1300  *
1301  * Since: 2.6
1302  */
1303 void
1304 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1305                                          gint       first_response_id,
1306                                          ...)
1307 {
1308   GdkScreen *screen;
1309   va_list args;
1310
1311   g_return_if_fail (GTK_IS_DIALOG (dialog));
1312
1313   screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1314   if (!gtk_alternative_dialog_button_order (screen))
1315       return;
1316
1317   va_start (args, first_response_id);
1318
1319   gtk_dialog_set_alternative_button_order_valist (dialog,
1320                                                   first_response_id,
1321                                                   args);
1322   va_end (args);
1323 }
1324 /**
1325  * gtk_dialog_set_alternative_button_order_from_array:
1326  * @dialog: a #GtkDialog
1327  * @n_params: the number of response ids in @new_order
1328  * @new_order: (array length=n_params): an array of response ids of
1329  *     @dialog's buttons
1330  *
1331  * Sets an alternative button order. If the
1332  * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1333  * the dialog buttons are reordered according to the order of the
1334  * response ids in @new_order.
1335  *
1336  * See gtk_dialog_set_alternative_button_order() for more information.
1337  *
1338  * This function is for use by language bindings.
1339  *
1340  * Since: 2.6
1341  */
1342 void
1343 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1344                                                     gint       n_params,
1345                                                     gint      *new_order)
1346 {
1347   GtkDialogPrivate *priv = dialog->priv;
1348   GdkScreen *screen;
1349   GtkWidget *child;
1350   gint position;
1351
1352   g_return_if_fail (GTK_IS_DIALOG (dialog));
1353   g_return_if_fail (new_order != NULL);
1354
1355   screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1356   if (!gtk_alternative_dialog_button_order (screen))
1357       return;
1358
1359   for (position = 0; position < n_params; position++)
1360   {
1361       /* reorder child with response_id to position */
1362       child = dialog_find_button (dialog, new_order[position]);
1363       gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1364     }
1365 }
1366
1367 typedef struct {
1368   gchar *widget_name;
1369   gchar *response_id;
1370 } ActionWidgetInfo;
1371
1372 typedef struct {
1373   GtkDialog *dialog;
1374   GtkBuilder *builder;
1375   GSList *items;
1376   gchar *response;
1377 } ActionWidgetsSubParserData;
1378
1379 static void
1380 attributes_start_element (GMarkupParseContext *context,
1381                           const gchar         *element_name,
1382                           const gchar        **names,
1383                           const gchar        **values,
1384                           gpointer             user_data,
1385                           GError             **error)
1386 {
1387   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1388   guint i;
1389
1390   if (strcmp (element_name, "action-widget") == 0)
1391     {
1392       for (i = 0; names[i]; i++)
1393         if (strcmp (names[i], "response") == 0)
1394           parser_data->response = g_strdup (values[i]);
1395     }
1396   else if (strcmp (element_name, "action-widgets") == 0)
1397     return;
1398   else
1399     g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1400 }
1401
1402 static void
1403 attributes_text_element (GMarkupParseContext *context,
1404                          const gchar         *text,
1405                          gsize                text_len,
1406                          gpointer             user_data,
1407                          GError             **error)
1408 {
1409   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1410   ActionWidgetInfo *item;
1411
1412   if (!parser_data->response)
1413     return;
1414
1415   item = g_new (ActionWidgetInfo, 1);
1416   item->widget_name = g_strndup (text, text_len);
1417   item->response_id = parser_data->response;
1418   parser_data->items = g_slist_prepend (parser_data->items, item);
1419   parser_data->response = NULL;
1420 }
1421
1422 static const GMarkupParser attributes_parser =
1423   {
1424     attributes_start_element,
1425     NULL,
1426     attributes_text_element,
1427   };
1428
1429 static gboolean
1430 gtk_dialog_buildable_custom_tag_start (GtkBuildable  *buildable,
1431                                        GtkBuilder    *builder,
1432                                        GObject       *child,
1433                                        const gchar   *tagname,
1434                                        GMarkupParser *parser,
1435                                        gpointer      *data)
1436 {
1437   ActionWidgetsSubParserData *parser_data;
1438
1439   if (child)
1440     return FALSE;
1441
1442   if (strcmp (tagname, "action-widgets") == 0)
1443     {
1444       parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1445       parser_data->dialog = GTK_DIALOG (buildable);
1446       parser_data->items = NULL;
1447
1448       *parser = attributes_parser;
1449       *data = parser_data;
1450       return TRUE;
1451     }
1452
1453   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1454                                                    tagname, parser, data);
1455 }
1456
1457 static void
1458 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1459                                       GtkBuilder   *builder,
1460                                       GObject      *child,
1461                                       const gchar  *tagname,
1462                                       gpointer      user_data)
1463 {
1464   GtkDialog *dialog = GTK_DIALOG (buildable);
1465   GtkDialogPrivate *priv = dialog->priv;
1466   GSList *l;
1467   ActionWidgetsSubParserData *parser_data;
1468   GObject *object;
1469   ResponseData *ad;
1470   guint signal_id;
1471
1472   if (strcmp (tagname, "action-widgets"))
1473     {
1474     parent_buildable_iface->custom_finished (buildable, builder, child,
1475                                              tagname, user_data);
1476     return;
1477     }
1478
1479   parser_data = (ActionWidgetsSubParserData*)user_data;
1480   parser_data->items = g_slist_reverse (parser_data->items);
1481
1482   for (l = parser_data->items; l; l = l->next)
1483     {
1484       ActionWidgetInfo *item = l->data;
1485
1486       object = gtk_builder_get_object (builder, item->widget_name);
1487       if (!object)
1488         {
1489           g_warning ("Unknown object %s specified in action-widgets of %s",
1490                      item->widget_name,
1491                      gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1492           continue;
1493         }
1494
1495       ad = get_response_data (GTK_WIDGET (object), TRUE);
1496       ad->response_id = g_ascii_strtoll (item->response_id, NULL, 10);
1497
1498       if (GTK_IS_BUTTON (object))
1499         signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1500       else
1501         signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1502
1503       if (signal_id)
1504         {
1505           GClosure *closure;
1506
1507           closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1508                                            G_OBJECT (dialog));
1509           g_signal_connect_closure_by_id (object,
1510                                           signal_id,
1511                                           0,
1512                                           closure,
1513                                           FALSE);
1514         }
1515
1516       if (ad->response_id == GTK_RESPONSE_HELP)
1517         gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area),
1518                                             GTK_WIDGET (object), TRUE);
1519
1520       g_free (item->widget_name);
1521       g_free (item->response_id);
1522       g_free (item);
1523     }
1524   g_slist_free (parser_data->items);
1525   g_slice_free (ActionWidgetsSubParserData, parser_data);
1526 }
1527
1528 /**
1529  * gtk_dialog_get_action_area:
1530  * @dialog: a #GtkDialog
1531  *
1532  * Returns the action area of @dialog.
1533  *
1534  * Returns: (transfer none): the action area.
1535  *
1536  * Since: 2.14
1537  **/
1538 GtkWidget *
1539 gtk_dialog_get_action_area (GtkDialog *dialog)
1540 {
1541   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1542
1543   return dialog->priv->action_area;
1544 }
1545
1546 /**
1547  * gtk_dialog_get_content_area:
1548  * @dialog: a #GtkDialog
1549  *
1550  * Returns the content area of @dialog.
1551  *
1552  * Returns: (transfer none): the content area #GtkBox.
1553  *
1554  * Since: 2.14
1555  **/
1556 GtkWidget *
1557 gtk_dialog_get_content_area (GtkDialog *dialog)
1558 {
1559   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1560
1561   return dialog->priv->vbox;
1562 }