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