]> Pileus Git - ~andy/gtk/blob - gtk/gtkdialog.c
[GI] Cosmetic cleanups of annotations and doc comments
[~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. The two areas are separated by a #GtkHSeparator.
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   update_spacings (GTK_DIALOG (widget));
512 }
513
514 static GtkWidget *
515 dialog_find_button (GtkDialog *dialog,
516                     gint       response_id)
517 {
518   GtkDialogPrivate *priv = dialog->priv;
519   GtkWidget *child = NULL;
520   GList *children, *tmp_list;
521
522   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
523
524   for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
525     {
526       ResponseData *rd = get_response_data (tmp_list->data, FALSE);
527
528       if (rd && rd->response_id == response_id)
529         {
530           child = tmp_list->data;
531           break;
532         }
533     }
534
535   g_list_free (children);
536
537   return child;
538 }
539
540 static void
541 gtk_dialog_close (GtkDialog *dialog)
542 {
543   /* Synthesize delete_event to close dialog. */
544
545   GtkWidget *widget = GTK_WIDGET (dialog);
546   GdkEvent *event;
547
548   event = gdk_event_new (GDK_DELETE);
549
550   event->any.window = g_object_ref (gtk_widget_get_window (widget));
551   event->any.send_event = TRUE;
552
553   gtk_main_do_event (event);
554   gdk_event_free (event);
555 }
556
557 /**
558  * gtk_dialog_new:
559  *
560  * Creates a new dialog box.
561  *
562  * Widgets should not be packed into this #GtkWindow
563  * directly, but into the @vbox and @action_area, as described above.
564  *
565  * Returns: the new dialog as a #GtkWidget
566  */
567 GtkWidget*
568 gtk_dialog_new (void)
569 {
570   return g_object_new (GTK_TYPE_DIALOG, NULL);
571 }
572
573 static GtkWidget*
574 gtk_dialog_new_empty (const gchar     *title,
575                       GtkWindow       *parent,
576                       GtkDialogFlags   flags)
577 {
578   GtkDialog *dialog;
579
580   dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
581
582   if (title)
583     gtk_window_set_title (GTK_WINDOW (dialog), title);
584
585   if (parent)
586     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
587
588   if (flags & GTK_DIALOG_MODAL)
589     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
590
591   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
592     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
593
594   return GTK_WIDGET (dialog);
595 }
596
597 /**
598  * gtk_dialog_new_with_buttons:
599  * @title: (allow-none): Title of the dialog, or %NULL
600  * @parent: (allow-none): Transient parent of the dialog, or %NULL
601  * @flags: from #GtkDialogFlags
602  * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
603  * @Varargs: response ID for first button, then additional buttons, ending with %NULL
604  *
605  * Creates a new #GtkDialog with title @title (or %NULL for the default
606  * title; see gtk_window_set_title()) and transient parent @parent (or
607  * %NULL for none; see gtk_window_set_transient_for()). The @flags
608  * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
609  * and/or to have it destroyed along with its transient parent
610  * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
611  * text/response ID pairs should be listed, with a %NULL pointer ending
612  * the list. Button text can be either a stock ID such as
613  * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
614  * any positive number, or one of the values in the #GtkResponseType
615  * enumeration. If the user clicks one of these dialog buttons,
616  * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
617  * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
618  * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
619  * However, destroying a dialog does not emit the ::response signal;
620  * so be careful relying on ::response when using the
621  * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
622  * so the first button in the list will be the leftmost button in the dialog.
623  *
624  * Here's a simple example:
625  * |[
626  *  GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
627  *                                                   main_app_window,
628  *                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
629  *                                                   GTK_STOCK_OK,
630  *                                                   GTK_RESPONSE_ACCEPT,
631  *                                                   GTK_STOCK_CANCEL,
632  *                                                   GTK_RESPONSE_REJECT,
633  *                                                   NULL);
634  * ]|
635  *
636  * Return value: a new #GtkDialog
637  **/
638 GtkWidget*
639 gtk_dialog_new_with_buttons (const gchar    *title,
640                              GtkWindow      *parent,
641                              GtkDialogFlags  flags,
642                              const gchar    *first_button_text,
643                              ...)
644 {
645   GtkDialog *dialog;
646   va_list args;
647
648   dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
649
650   va_start (args, first_button_text);
651
652   gtk_dialog_add_buttons_valist (dialog,
653                                  first_button_text,
654                                  args);
655
656   va_end (args);
657
658   return GTK_WIDGET (dialog);
659 }
660
661 static void
662 response_data_free (gpointer data)
663 {
664   g_slice_free (ResponseData, data);
665 }
666
667 static ResponseData*
668 get_response_data (GtkWidget *widget,
669                    gboolean   create)
670 {
671   ResponseData *ad = g_object_get_data (G_OBJECT (widget),
672                                         "gtk-dialog-response-data");
673
674   if (ad == NULL && create)
675     {
676       ad = g_slice_new (ResponseData);
677
678       g_object_set_data_full (G_OBJECT (widget),
679                               I_("gtk-dialog-response-data"),
680                               ad,
681                               response_data_free);
682     }
683
684   return ad;
685 }
686
687 static void
688 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
689 {
690   gint response_id;
691
692   response_id = gtk_dialog_get_response_for_widget (dialog, widget);
693
694   gtk_dialog_response (dialog, response_id);
695 }
696
697 /**
698  * gtk_dialog_add_action_widget:
699  * @dialog: a #GtkDialog
700  * @child: an activatable widget
701  * @response_id: response ID for @child
702  *
703  * Adds an activatable widget to the action area of a #GtkDialog,
704  * connecting a signal handler that will emit the #GtkDialog::response
705  * signal on the dialog when the widget is activated. The widget is
706  * appended to the end of the dialog's action area. If you want to add a
707  * non-activatable widget, simply pack it into the @action_area field
708  * of the #GtkDialog struct.
709  **/
710 void
711 gtk_dialog_add_action_widget (GtkDialog *dialog,
712                               GtkWidget *child,
713                               gint       response_id)
714 {
715   GtkDialogPrivate *priv;
716   ResponseData *ad;
717   guint signal_id;
718
719   g_return_if_fail (GTK_IS_DIALOG (dialog));
720   g_return_if_fail (GTK_IS_WIDGET (child));
721
722   priv = dialog->priv;
723
724   ad = get_response_data (child, TRUE);
725
726   ad->response_id = response_id;
727
728   if (GTK_IS_BUTTON (child))
729     signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
730   else
731     signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
732
733   if (signal_id)
734     {
735       GClosure *closure;
736
737       closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
738                                        G_OBJECT (dialog));
739       g_signal_connect_closure_by_id (child,
740                                       signal_id,
741                                       0,
742                                       closure,
743                                       FALSE);
744     }
745   else
746     g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
747
748   gtk_box_pack_end (GTK_BOX (priv->action_area),
749                     child,
750                     FALSE, TRUE, 0);
751
752   if (response_id == GTK_RESPONSE_HELP)
753     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
754 }
755
756 /**
757  * gtk_dialog_add_button:
758  * @dialog: a #GtkDialog
759  * @button_text: text of button, or stock ID
760  * @response_id: response ID for the button
761  *
762  * Adds a button with the given text (or a stock button, if @button_text is a
763  * stock ID) and sets things up so that clicking the button will emit the
764  * #GtkDialog::response signal with the given @response_id. The button is
765  * appended to the end of the dialog's action area. The button widget is
766  * returned, but usually you don't need it.
767  *
768  * Return value: (transfer full): the button widget that was added
769  **/
770 GtkWidget*
771 gtk_dialog_add_button (GtkDialog   *dialog,
772                        const gchar *button_text,
773                        gint         response_id)
774 {
775   GtkWidget *button;
776
777   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
778   g_return_val_if_fail (button_text != NULL, NULL);
779
780   button = gtk_button_new_from_stock (button_text);
781
782   gtk_widget_set_can_default (button, TRUE);
783
784   gtk_widget_show (button);
785
786   gtk_dialog_add_action_widget (dialog,
787                                 button,
788                                 response_id);
789
790   return button;
791 }
792
793 static void
794 gtk_dialog_add_buttons_valist (GtkDialog      *dialog,
795                                const gchar    *first_button_text,
796                                va_list         args)
797 {
798   const gchar* text;
799   gint response_id;
800
801   g_return_if_fail (GTK_IS_DIALOG (dialog));
802
803   if (first_button_text == NULL)
804     return;
805
806   text = first_button_text;
807   response_id = va_arg (args, gint);
808
809   while (text != NULL)
810     {
811       gtk_dialog_add_button (dialog, text, response_id);
812
813       text = va_arg (args, gchar*);
814       if (text == NULL)
815         break;
816       response_id = va_arg (args, int);
817     }
818 }
819
820 /**
821  * gtk_dialog_add_buttons:
822  * @dialog: a #GtkDialog
823  * @first_button_text: button text or stock ID
824  * @Varargs: response ID for first button, then more text-response_id pairs
825  *
826  * Adds more buttons, same as calling gtk_dialog_add_button()
827  * repeatedly.  The variable argument list should be %NULL-terminated
828  * as with gtk_dialog_new_with_buttons(). Each button must have both
829  * text and response ID.
830  **/
831 void
832 gtk_dialog_add_buttons (GtkDialog   *dialog,
833                         const gchar *first_button_text,
834                         ...)
835 {
836   va_list args;
837
838   va_start (args, first_button_text);
839
840   gtk_dialog_add_buttons_valist (dialog,
841                                  first_button_text,
842                                  args);
843
844   va_end (args);
845 }
846
847 /**
848  * gtk_dialog_set_response_sensitive:
849  * @dialog: a #GtkDialog
850  * @response_id: a response ID
851  * @setting: %TRUE for sensitive
852  *
853  * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
854  * for each widget in the dialog's action area with the given @response_id.
855  * A convenient way to sensitize/desensitize dialog buttons.
856  **/
857 void
858 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
859                                    gint       response_id,
860                                    gboolean   setting)
861 {
862   GtkDialogPrivate *priv;
863   GList *children;
864   GList *tmp_list;
865
866   g_return_if_fail (GTK_IS_DIALOG (dialog));
867
868   priv = dialog->priv;
869
870   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
871
872   tmp_list = children;
873   while (tmp_list != NULL)
874     {
875       GtkWidget *widget = tmp_list->data;
876       ResponseData *rd = get_response_data (widget, FALSE);
877
878       if (rd && rd->response_id == response_id)
879         gtk_widget_set_sensitive (widget, setting);
880
881       tmp_list = g_list_next (tmp_list);
882     }
883
884   g_list_free (children);
885 }
886
887 /**
888  * gtk_dialog_set_default_response:
889  * @dialog: a #GtkDialog
890  * @response_id: a response ID
891  *
892  * Sets the last widget in the dialog's action area with the given @response_id
893  * as the default widget for the dialog. Pressing "Enter" normally activates
894  * the default widget.
895  **/
896 void
897 gtk_dialog_set_default_response (GtkDialog *dialog,
898                                  gint       response_id)
899 {
900   GtkDialogPrivate *priv;
901   GList *children;
902   GList *tmp_list;
903
904   g_return_if_fail (GTK_IS_DIALOG (dialog));
905
906   priv = dialog->priv;
907
908   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
909
910   tmp_list = children;
911   while (tmp_list != NULL)
912     {
913       GtkWidget *widget = tmp_list->data;
914       ResponseData *rd = get_response_data (widget, FALSE);
915
916       if (rd && rd->response_id == response_id)
917         gtk_widget_grab_default (widget);
918
919       tmp_list = g_list_next (tmp_list);
920     }
921
922   g_list_free (children);
923 }
924
925 /**
926  * gtk_dialog_response:
927  * @dialog: a #GtkDialog
928  * @response_id: response ID
929  *
930  * Emits the #GtkDialog::response signal with the given response ID.
931  * Used to indicate that the user has responded to the dialog in some way;
932  * typically either you or gtk_dialog_run() will be monitoring the
933  * ::response signal and take appropriate action.
934  **/
935 void
936 gtk_dialog_response (GtkDialog *dialog,
937                      gint       response_id)
938 {
939   g_return_if_fail (GTK_IS_DIALOG (dialog));
940
941   g_signal_emit (dialog,
942                  dialog_signals[RESPONSE],
943                  0,
944                  response_id);
945 }
946
947 typedef struct
948 {
949   GtkDialog *dialog;
950   gint response_id;
951   GMainLoop *loop;
952   gboolean destroyed;
953 } RunInfo;
954
955 static void
956 shutdown_loop (RunInfo *ri)
957 {
958   if (g_main_loop_is_running (ri->loop))
959     g_main_loop_quit (ri->loop);
960 }
961
962 static void
963 run_unmap_handler (GtkDialog *dialog, gpointer data)
964 {
965   RunInfo *ri = data;
966
967   shutdown_loop (ri);
968 }
969
970 static void
971 run_response_handler (GtkDialog *dialog,
972                       gint response_id,
973                       gpointer data)
974 {
975   RunInfo *ri;
976
977   ri = data;
978
979   ri->response_id = response_id;
980
981   shutdown_loop (ri);
982 }
983
984 static gint
985 run_delete_handler (GtkDialog *dialog,
986                     GdkEventAny *event,
987                     gpointer data)
988 {
989   RunInfo *ri = data;
990
991   shutdown_loop (ri);
992
993   return TRUE; /* Do not destroy */
994 }
995
996 static void
997 run_destroy_handler (GtkDialog *dialog, gpointer data)
998 {
999   RunInfo *ri = data;
1000
1001   /* shutdown_loop will be called by run_unmap_handler */
1002
1003   ri->destroyed = TRUE;
1004 }
1005
1006 /**
1007  * gtk_dialog_run:
1008  * @dialog: a #GtkDialog
1009  *
1010  * Blocks in a recursive main loop until the @dialog either emits the
1011  * #GtkDialog::response signal, or is destroyed. If the dialog is
1012  * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
1013  * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
1014  * ::response signal emission.
1015  *
1016  * Before entering the recursive main loop, gtk_dialog_run() calls
1017  * gtk_widget_show() on the dialog for you. Note that you still
1018  * need to show any children of the dialog yourself.
1019  *
1020  * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
1021  * is disabled; if the dialog receives ::delete_event, it will not be
1022  * destroyed as windows usually are, and gtk_dialog_run() will return
1023  * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
1024  * will be modal. You can force gtk_dialog_run() to return at any time by
1025  * calling gtk_dialog_response() to emit the ::response signal. Destroying
1026  * the dialog during gtk_dialog_run() is a very bad idea, because your
1027  * post-run code won't know whether the dialog was destroyed or not.
1028  *
1029  * After gtk_dialog_run() returns, you are responsible for hiding or
1030  * destroying the dialog if you wish to do so.
1031  *
1032  * Typical usage of this function might be:
1033  * |[
1034  *   gint result = gtk_dialog_run (GTK_DIALOG (dialog));
1035  *   switch (result)
1036  *     {
1037  *       case GTK_RESPONSE_ACCEPT:
1038  *          do_application_specific_something ();
1039  *          break;
1040  *       default:
1041  *          do_nothing_since_dialog_was_cancelled ();
1042  *          break;
1043  *     }
1044  *   gtk_widget_destroy (dialog);
1045  * ]|
1046  *
1047  * Note that even though the recursive main loop gives the effect of a
1048  * modal dialog (it prevents the user from interacting with other
1049  * windows in the same window group while the dialog is run), callbacks
1050  * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
1051  * be triggered during a gtk_dialog_run() call.
1052  *
1053  * Return value: response ID
1054  **/
1055 gint
1056 gtk_dialog_run (GtkDialog *dialog)
1057 {
1058   RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
1059   gboolean was_modal;
1060   gulong response_handler;
1061   gulong unmap_handler;
1062   gulong destroy_handler;
1063   gulong delete_handler;
1064
1065   g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
1066
1067   g_object_ref (dialog);
1068
1069   was_modal = gtk_window_get_modal (GTK_WINDOW (dialog));
1070   if (!was_modal)
1071     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1072
1073   if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
1074     gtk_widget_show (GTK_WIDGET (dialog));
1075
1076   response_handler =
1077     g_signal_connect (dialog,
1078                       "response",
1079                       G_CALLBACK (run_response_handler),
1080                       &ri);
1081
1082   unmap_handler =
1083     g_signal_connect (dialog,
1084                       "unmap",
1085                       G_CALLBACK (run_unmap_handler),
1086                       &ri);
1087
1088   delete_handler =
1089     g_signal_connect (dialog,
1090                       "delete-event",
1091                       G_CALLBACK (run_delete_handler),
1092                       &ri);
1093
1094   destroy_handler =
1095     g_signal_connect (dialog,
1096                       "destroy",
1097                       G_CALLBACK (run_destroy_handler),
1098                       &ri);
1099
1100   ri.loop = g_main_loop_new (NULL, FALSE);
1101
1102   GDK_THREADS_LEAVE ();
1103   g_main_loop_run (ri.loop);
1104   GDK_THREADS_ENTER ();
1105
1106   g_main_loop_unref (ri.loop);
1107
1108   ri.loop = NULL;
1109
1110   if (!ri.destroyed)
1111     {
1112       if (!was_modal)
1113         gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
1114
1115       g_signal_handler_disconnect (dialog, response_handler);
1116       g_signal_handler_disconnect (dialog, unmap_handler);
1117       g_signal_handler_disconnect (dialog, delete_handler);
1118       g_signal_handler_disconnect (dialog, destroy_handler);
1119     }
1120
1121   g_object_unref (dialog);
1122
1123   return ri.response_id;
1124 }
1125
1126 /**
1127  * gtk_dialog_get_widget_for_response:
1128  * @dialog: a #GtkDialog
1129  * @response_id: the response ID used by the @dialog widget
1130  *
1131  * Gets the widget button that uses the given response ID in the action area
1132  * of a dialog.
1133  *
1134  * Returns: (transfer none): the @widget button that uses the given
1135  *     @response_id, or %NULL.
1136  *
1137  * Since: 2.20
1138  */
1139 GtkWidget*
1140 gtk_dialog_get_widget_for_response (GtkDialog *dialog,
1141                                     gint       response_id)
1142 {
1143   GtkDialogPrivate *priv;
1144   GList *children;
1145   GList *tmp_list;
1146
1147   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1148
1149   priv = dialog->priv;
1150
1151   children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
1152
1153   tmp_list = children;
1154   while (tmp_list != NULL)
1155     {
1156       GtkWidget *widget = tmp_list->data;
1157       ResponseData *rd = get_response_data (widget, FALSE);
1158
1159       if (rd && rd->response_id == response_id)
1160         {
1161           g_list_free (children);
1162           return widget;
1163         }
1164
1165       tmp_list = g_list_next (tmp_list);
1166     }
1167
1168   g_list_free (children);
1169
1170   return NULL;
1171 }
1172
1173 /**
1174  * gtk_dialog_get_response_for_widget:
1175  * @dialog: a #GtkDialog
1176  * @widget: a widget in the action area of @dialog
1177  *
1178  * Gets the response id of a widget in the action area
1179  * of a dialog.
1180  *
1181  * Returns: the response id of @widget, or %GTK_RESPONSE_NONE
1182  *  if @widget doesn't have a response id set.
1183  *
1184  * Since: 2.8
1185  */
1186 gint
1187 gtk_dialog_get_response_for_widget (GtkDialog *dialog,
1188                                     GtkWidget *widget)
1189 {
1190   ResponseData *rd;
1191
1192   rd = get_response_data (widget, FALSE);
1193   if (!rd)
1194     return GTK_RESPONSE_NONE;
1195   else
1196     return rd->response_id;
1197 }
1198
1199 /**
1200  * gtk_alternative_dialog_button_order:
1201  * @screen: (allow-none): a #GdkScreen, or %NULL to use the default screen
1202  *
1203  * Returns %TRUE if dialogs are expected to use an alternative
1204  * button order on the screen @screen. See
1205  * gtk_dialog_set_alternative_button_order() for more details
1206  * about alternative button order.
1207  *
1208  * If you need to use this function, you should probably connect
1209  * to the ::notify:gtk-alternative-button-order signal on the
1210  * #GtkSettings object associated to @screen, in order to be
1211  * notified if the button order setting changes.
1212  *
1213  * Returns: Whether the alternative button order should be used
1214  *
1215  * Since: 2.6
1216  */
1217 gboolean
1218 gtk_alternative_dialog_button_order (GdkScreen *screen)
1219 {
1220   GtkSettings *settings;
1221   gboolean result;
1222
1223   if (screen)
1224     settings = gtk_settings_get_for_screen (screen);
1225   else
1226     settings = gtk_settings_get_default ();
1227
1228   g_object_get (settings,
1229                 "gtk-alternative-button-order", &result, NULL);
1230
1231   return result;
1232 }
1233
1234 static void
1235 gtk_dialog_set_alternative_button_order_valist (GtkDialog *dialog,
1236                                                 gint       first_response_id,
1237                                                 va_list    args)
1238 {
1239   GtkDialogPrivate *priv = dialog->priv;
1240   GtkWidget *child;
1241   gint response_id;
1242   gint position;
1243
1244   response_id = first_response_id;
1245   position = 0;
1246   while (response_id != -1)
1247     {
1248       /* reorder child with response_id to position */
1249       child = dialog_find_button (dialog, response_id);
1250       gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1251
1252       response_id = va_arg (args, gint);
1253       position++;
1254     }
1255 }
1256
1257 /**
1258  * gtk_dialog_set_alternative_button_order:
1259  * @dialog: a #GtkDialog
1260  * @first_response_id: a response id used by one @dialog's buttons
1261  * @Varargs: a list of more response ids of @dialog's buttons, terminated by -1
1262  *
1263  * Sets an alternative button order. If the
1264  * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1265  * the dialog buttons are reordered according to the order of the
1266  * response ids passed to this function.
1267  *
1268  * By default, GTK+ dialogs use the button order advocated by the Gnome
1269  * <ulink url="http://library.gnome.org/devel/hig-book/stable/">Human
1270  * Interface Guidelines</ulink> with the affirmative button at the far
1271  * right, and the cancel button left of it. But the builtin GTK+ dialogs
1272  * and #GtkMessageDialog<!-- -->s do provide an alternative button order,
1273  * which is more suitable on some platforms, e.g. Windows.
1274  *
1275  * Use this function after adding all the buttons to your dialog, as the
1276  * following example shows:
1277  * |[
1278  * cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1279  *                                        GTK_STOCK_CANCEL,
1280  *                                        GTK_RESPONSE_CANCEL);
1281  *
1282  * ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1283  *                                    GTK_STOCK_OK,
1284  *                                    GTK_RESPONSE_OK);
1285  *
1286  * gtk_widget_grab_default (ok_button);
1287  *
1288  * help_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1289  *                                      GTK_STOCK_HELP,
1290  *                                      GTK_RESPONSE_HELP);
1291  *
1292  * gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1293  *                                          GTK_RESPONSE_OK,
1294  *                                          GTK_RESPONSE_CANCEL,
1295  *                                          GTK_RESPONSE_HELP,
1296  *                                          -1);
1297  * ]|
1298  *
1299  * Since: 2.6
1300  */
1301 void
1302 gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
1303                                          gint       first_response_id,
1304                                          ...)
1305 {
1306   GdkScreen *screen;
1307   va_list args;
1308
1309   g_return_if_fail (GTK_IS_DIALOG (dialog));
1310
1311   screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1312   if (!gtk_alternative_dialog_button_order (screen))
1313       return;
1314
1315   va_start (args, first_response_id);
1316
1317   gtk_dialog_set_alternative_button_order_valist (dialog,
1318                                                   first_response_id,
1319                                                   args);
1320   va_end (args);
1321 }
1322 /**
1323  * gtk_dialog_set_alternative_button_order_from_array:
1324  * @dialog: a #GtkDialog
1325  * @n_params: the number of response ids in @new_order
1326  * @new_order: an array of response ids of @dialog's buttons
1327  *
1328  * Sets an alternative button order. If the
1329  * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE,
1330  * the dialog buttons are reordered according to the order of the
1331  * response ids in @new_order.
1332  *
1333  * See gtk_dialog_set_alternative_button_order() for more information.
1334  *
1335  * This function is for use by language bindings.
1336  *
1337  * Since: 2.6
1338  */
1339 void
1340 gtk_dialog_set_alternative_button_order_from_array (GtkDialog *dialog,
1341                                                     gint       n_params,
1342                                                     gint      *new_order)
1343 {
1344   GtkDialogPrivate *priv = dialog->priv;
1345   GdkScreen *screen;
1346   GtkWidget *child;
1347   gint position;
1348
1349   g_return_if_fail (GTK_IS_DIALOG (dialog));
1350   g_return_if_fail (new_order != NULL);
1351
1352   screen = gtk_widget_get_screen (GTK_WIDGET (dialog));
1353   if (!gtk_alternative_dialog_button_order (screen))
1354       return;
1355
1356   for (position = 0; position < n_params; position++)
1357   {
1358       /* reorder child with response_id to position */
1359       child = dialog_find_button (dialog, new_order[position]);
1360       gtk_box_reorder_child (GTK_BOX (priv->action_area), child, position);
1361     }
1362 }
1363
1364 typedef struct {
1365   gchar *widget_name;
1366   gchar *response_id;
1367 } ActionWidgetInfo;
1368
1369 typedef struct {
1370   GtkDialog *dialog;
1371   GtkBuilder *builder;
1372   GSList *items;
1373   gchar *response;
1374 } ActionWidgetsSubParserData;
1375
1376 static void
1377 attributes_start_element (GMarkupParseContext *context,
1378                           const gchar         *element_name,
1379                           const gchar        **names,
1380                           const gchar        **values,
1381                           gpointer             user_data,
1382                           GError             **error)
1383 {
1384   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1385   guint i;
1386
1387   if (strcmp (element_name, "action-widget") == 0)
1388     {
1389       for (i = 0; names[i]; i++)
1390         if (strcmp (names[i], "response") == 0)
1391           parser_data->response = g_strdup (values[i]);
1392     }
1393   else if (strcmp (element_name, "action-widgets") == 0)
1394     return;
1395   else
1396     g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
1397 }
1398
1399 static void
1400 attributes_text_element (GMarkupParseContext *context,
1401                          const gchar         *text,
1402                          gsize                text_len,
1403                          gpointer             user_data,
1404                          GError             **error)
1405 {
1406   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1407   ActionWidgetInfo *item;
1408
1409   if (!parser_data->response)
1410     return;
1411
1412   item = g_new (ActionWidgetInfo, 1);
1413   item->widget_name = g_strndup (text, text_len);
1414   item->response_id = parser_data->response;
1415   parser_data->items = g_slist_prepend (parser_data->items, item);
1416   parser_data->response = NULL;
1417 }
1418
1419 static const GMarkupParser attributes_parser =
1420   {
1421     attributes_start_element,
1422     NULL,
1423     attributes_text_element,
1424   };
1425
1426 static gboolean
1427 gtk_dialog_buildable_custom_tag_start (GtkBuildable  *buildable,
1428                                        GtkBuilder    *builder,
1429                                        GObject       *child,
1430                                        const gchar   *tagname,
1431                                        GMarkupParser *parser,
1432                                        gpointer      *data)
1433 {
1434   ActionWidgetsSubParserData *parser_data;
1435
1436   if (child)
1437     return FALSE;
1438
1439   if (strcmp (tagname, "action-widgets") == 0)
1440     {
1441       parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1442       parser_data->dialog = GTK_DIALOG (buildable);
1443       parser_data->items = NULL;
1444
1445       *parser = attributes_parser;
1446       *data = parser_data;
1447       return TRUE;
1448     }
1449
1450   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1451                                                    tagname, parser, data);
1452 }
1453
1454 static void
1455 gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
1456                                       GtkBuilder   *builder,
1457                                       GObject      *child,
1458                                       const gchar  *tagname,
1459                                       gpointer      user_data)
1460 {
1461   GtkDialog *dialog = GTK_DIALOG (buildable);
1462   GtkDialogPrivate *priv = dialog->priv;
1463   GSList *l;
1464   ActionWidgetsSubParserData *parser_data;
1465   GObject *object;
1466   ResponseData *ad;
1467   guint signal_id;
1468
1469   if (strcmp (tagname, "action-widgets"))
1470     {
1471     parent_buildable_iface->custom_finished (buildable, builder, child,
1472                                              tagname, user_data);
1473     return;
1474     }
1475
1476   parser_data = (ActionWidgetsSubParserData*)user_data;
1477   parser_data->items = g_slist_reverse (parser_data->items);
1478
1479   for (l = parser_data->items; l; l = l->next)
1480     {
1481       ActionWidgetInfo *item = l->data;
1482
1483       object = gtk_builder_get_object (builder, item->widget_name);
1484       if (!object)
1485         {
1486           g_warning ("Unknown object %s specified in action-widgets of %s",
1487                      item->widget_name,
1488                      gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1489           continue;
1490         }
1491
1492       ad = get_response_data (GTK_WIDGET (object), TRUE);
1493       ad->response_id = g_ascii_strtoll (item->response_id, NULL, 10);
1494
1495       if (GTK_IS_BUTTON (object))
1496         signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1497       else
1498         signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1499
1500       if (signal_id)
1501         {
1502           GClosure *closure;
1503
1504           closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1505                                            G_OBJECT (dialog));
1506           g_signal_connect_closure_by_id (object,
1507                                           signal_id,
1508                                           0,
1509                                           closure,
1510                                           FALSE);
1511         }
1512
1513       if (ad->response_id == GTK_RESPONSE_HELP)
1514         gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area),
1515                                             GTK_WIDGET (object), TRUE);
1516
1517       g_free (item->widget_name);
1518       g_free (item->response_id);
1519       g_free (item);
1520     }
1521   g_slist_free (parser_data->items);
1522   g_slice_free (ActionWidgetsSubParserData, parser_data);
1523 }
1524
1525 /**
1526  * gtk_dialog_get_action_area:
1527  * @dialog: a #GtkDialog
1528  *
1529  * Returns the action area of @dialog.
1530  *
1531  * Returns: (transfer none): the action area.
1532  *
1533  * Since: 2.14
1534  **/
1535 GtkWidget *
1536 gtk_dialog_get_action_area (GtkDialog *dialog)
1537 {
1538   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1539
1540   return dialog->priv->action_area;
1541 }
1542
1543 /**
1544  * gtk_dialog_get_content_area:
1545  * @dialog: a #GtkDialog
1546  *
1547  * Returns the content area of @dialog.
1548  *
1549  * Returns: (transfer none): the content area #GtkBox.
1550  *
1551  * Since: 2.14
1552  **/
1553 GtkWidget *
1554 gtk_dialog_get_content_area (GtkDialog *dialog)
1555 {
1556   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
1557
1558   return dialog->priv->vbox;
1559 }