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