]> Pileus Git - ~andy/gtk/blob - gtk/gtkdialog.c
c70b1541b4a5e1e7c4c15c15a55a535640e1cfd7
[~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 "gtkbutton.h"
28 #include "gtkdialog.h"
29 #include "gtkhbbox.h"
30 #include "gtkhseparator.h"
31 #include "gtkmarshalers.h"
32 #include "gtkvbox.h"
33 #include "gdkkeysyms.h"
34 #include "gtkmain.h"
35 #include "gtkintl.h"
36 #include "gtkbindings.h"
37
38 typedef struct _ResponseData ResponseData;
39
40 struct _ResponseData
41 {
42   gint response_id;
43 };
44
45 static void gtk_dialog_class_init (GtkDialogClass *klass);
46 static void gtk_dialog_init       (GtkDialog      *dialog);
47
48 static void gtk_dialog_add_buttons_valist (GtkDialog   *dialog,
49                                            const gchar *first_button_text,
50                                            va_list      args);
51
52 static gint gtk_dialog_delete_event_handler (GtkWidget   *widget,
53                                              GdkEventAny *event,
54                                              gpointer     user_data);
55
56 static void gtk_dialog_set_property      (GObject          *object,
57                                           guint             prop_id,
58                                           const GValue     *value,
59                                           GParamSpec       *pspec);
60 static void gtk_dialog_get_property      (GObject          *object,
61                                           guint             prop_id,
62                                           GValue           *value,
63                                           GParamSpec       *pspec);
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
72 enum {
73   PROP_0,
74   PROP_HAS_SEPARATOR
75 };
76
77 enum {
78   RESPONSE,
79   CLOSE,
80   LAST_SIGNAL
81 };
82
83 static gpointer parent_class;
84 static guint dialog_signals[LAST_SIGNAL];
85
86 GType
87 gtk_dialog_get_type (void)
88 {
89   static GType dialog_type = 0;
90
91   if (!dialog_type)
92     {
93       static const GTypeInfo dialog_info =
94       {
95         sizeof (GtkDialogClass),
96         NULL,           /* base_init */
97         NULL,           /* base_finalize */
98         (GClassInitFunc) gtk_dialog_class_init,
99         NULL,           /* class_finalize */
100         NULL,           /* class_data */
101         sizeof (GtkDialog),
102         0,              /* n_preallocs */
103         (GInstanceInitFunc) gtk_dialog_init,
104       };
105
106       dialog_type = g_type_register_static (GTK_TYPE_WINDOW, "GtkDialog",
107                                             &dialog_info, 0);
108     }
109
110   return dialog_type;
111 }
112
113 static void
114 gtk_dialog_class_init (GtkDialogClass *class)
115 {
116   GObjectClass *gobject_class;
117   GtkWidgetClass *widget_class;
118   GtkBindingSet *binding_set;
119   
120   gobject_class = G_OBJECT_CLASS (class);
121   widget_class = GTK_WIDGET_CLASS (class);
122   
123   parent_class = g_type_class_peek_parent (class);
124
125   gobject_class->set_property = gtk_dialog_set_property;
126   gobject_class->get_property = gtk_dialog_get_property;
127   
128   widget_class->map = gtk_dialog_map;
129   widget_class->style_set = gtk_dialog_style_set;
130
131   class->close = gtk_dialog_close;
132   
133   g_object_class_install_property (gobject_class,
134                                    PROP_HAS_SEPARATOR,
135                                    g_param_spec_boolean ("has_separator",
136                                                          _("Has separator"),
137                                                          _("The dialog has a separator bar above its buttons"),
138                                                          TRUE,
139                                                          G_PARAM_READWRITE));
140   
141   dialog_signals[RESPONSE] =
142     g_signal_new ("response",
143                   G_OBJECT_CLASS_TYPE (class),
144                   G_SIGNAL_RUN_LAST,
145                   G_STRUCT_OFFSET (GtkDialogClass, response),
146                   NULL, NULL,
147                   _gtk_marshal_NONE__INT,
148                   G_TYPE_NONE, 1,
149                   G_TYPE_INT);
150
151   dialog_signals[CLOSE] =
152     g_signal_new ("close",
153                   G_OBJECT_CLASS_TYPE (class),
154                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
155                   G_STRUCT_OFFSET (GtkDialogClass, close),
156                   NULL, NULL,
157                   _gtk_marshal_NONE__NONE,
158                   G_TYPE_NONE, 0);
159   
160   gtk_widget_class_install_style_property (widget_class,
161                                            g_param_spec_int ("content_area_border",
162                                                              _("Content area border"),
163                                                              _("Width of border around the main dialog area"),
164                                                              0,
165                                                              G_MAXINT,
166                                                              2,
167                                                              G_PARAM_READABLE));
168   gtk_widget_class_install_style_property (widget_class,
169                                            g_param_spec_int ("button_spacing",
170                                                              _("Button spacing"),
171                                                              _("Spacing between buttons"),
172                                                              0,
173                                                              G_MAXINT,
174                                                              10,
175                                                              G_PARAM_READABLE));
176   
177   gtk_widget_class_install_style_property (widget_class,
178                                            g_param_spec_int ("action_area_border",
179                                                              _("Action area border"),
180                                                              _("Width of border around the button area at the bottom of the dialog"),
181                                                              0,
182                                                              G_MAXINT,
183                                                              5,
184                                                              G_PARAM_READABLE));
185
186   binding_set = gtk_binding_set_by_class (class);
187   
188   gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
189                                 "close", 0);
190 }
191
192 static void
193 update_spacings (GtkDialog *dialog)
194 {
195   GtkWidget *widget;
196   gint content_area_border;
197   gint button_spacing;
198   gint action_area_border;
199   
200   widget = GTK_WIDGET (dialog);
201
202   gtk_widget_style_get (widget,
203                         "content_area_border",
204                         &content_area_border,
205                         "button_spacing",
206                         &button_spacing,
207                         "action_area_border",
208                         &action_area_border,
209                         NULL);
210
211   gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
212                                   content_area_border);
213   gtk_box_set_spacing (GTK_BOX (dialog->action_area),
214                        button_spacing);
215   gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
216                                   action_area_border);
217 }
218
219 static void
220 gtk_dialog_init (GtkDialog *dialog)
221 {
222   /* To avoid breaking old code that prevents destroy on delete event
223    * by connecting a handler, we have to have the FIRST signal
224    * connection on the dialog.
225    */
226   g_signal_connect (dialog,
227                     "delete_event",
228                     G_CALLBACK (gtk_dialog_delete_event_handler),
229                     NULL);
230   
231   dialog->vbox = gtk_vbox_new (FALSE, 0);
232   
233   gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
234   gtk_widget_show (dialog->vbox);
235
236   dialog->action_area = gtk_hbutton_box_new ();
237
238   gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
239                              GTK_BUTTONBOX_END);  
240
241   gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
242                     FALSE, TRUE, 0);
243   gtk_widget_show (dialog->action_area);
244
245   dialog->separator = gtk_hseparator_new ();
246   gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
247   gtk_widget_show (dialog->separator);
248
249   gtk_window_set_type_hint (GTK_WINDOW (dialog),
250                             GDK_WINDOW_TYPE_HINT_DIALOG);
251   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
252 }
253
254
255 static void 
256 gtk_dialog_set_property (GObject      *object,
257                          guint         prop_id,
258                          const GValue *value,
259                          GParamSpec   *pspec)
260 {
261   GtkDialog *dialog;
262   
263   dialog = GTK_DIALOG (object);
264   
265   switch (prop_id)
266     {
267     case PROP_HAS_SEPARATOR:
268       gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
269       break;
270
271     default:
272       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273       break;
274     }
275 }
276
277 static void 
278 gtk_dialog_get_property (GObject     *object,
279                          guint        prop_id,
280                          GValue      *value,
281                          GParamSpec  *pspec)
282 {
283   GtkDialog *dialog;
284   
285   dialog = GTK_DIALOG (object);
286   
287   switch (prop_id)
288     {
289     case PROP_HAS_SEPARATOR:
290       g_value_set_boolean (value, dialog->separator != NULL);
291       break;
292
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295       break;
296     }
297 }
298
299 static gint
300 gtk_dialog_delete_event_handler (GtkWidget   *widget,
301                                  GdkEventAny *event,
302                                  gpointer     user_data)
303 {
304   /* emit response signal */
305   gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
306
307   /* Do the destroy by default */
308   return FALSE;
309 }
310
311 /* A far too tricky heuristic for getting the right initial
312  * focus widget if none was set. What we do is we focus the first
313  * widget in the tab chain, but if this results in the focus
314  * ending up on one of the response widgets _other_ than the
315  * default response, we focus the default response instead.
316  */
317 static void
318 gtk_dialog_map (GtkWidget *widget)
319 {
320   GtkWindow *window = GTK_WINDOW (widget);
321   GtkDialog *dialog = GTK_DIALOG (widget);
322   
323   GTK_WIDGET_CLASS (parent_class)->map (widget);
324
325   if (!window->focus_widget)
326     {
327       GList *children, *tmp_list;
328       
329       g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
330
331       tmp_list = children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
332
333       while (tmp_list)
334         {
335           GtkWidget *child = tmp_list->data;
336
337           if (child == window->focus_widget && child != window->default_widget && window->default_widget)
338             {
339               gtk_widget_grab_focus (window->default_widget);
340               break;
341             }
342           
343           tmp_list = tmp_list->next;
344         }
345
346       g_list_free (children);
347     }
348 }
349
350 static void
351 gtk_dialog_style_set (GtkWidget *widget,
352                       GtkStyle  *prev_style)
353 {
354   update_spacings (GTK_DIALOG (widget));
355 }
356
357 static gboolean
358 dialog_has_cancel (GtkDialog *dialog)
359 {
360   GList *children, *tmp_list;
361   gboolean ret = FALSE;
362       
363   children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
364
365   for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
366     {
367       ResponseData *rd = get_response_data (tmp_list->data);
368       
369       if (rd && rd->response_id == GTK_RESPONSE_CANCEL)
370         {
371           ret = TRUE;
372           break;
373         }
374     }
375
376   g_list_free (children);
377
378   return ret;
379 }
380
381 static void
382 gtk_dialog_close (GtkDialog *dialog)
383 {
384   /* Synthesize delete_event to close dialog. */
385   
386   GtkWidget *widget = GTK_WIDGET (dialog);
387   GdkEvent *event;
388
389   if (!dialog_has_cancel (dialog))
390     return;
391
392   event = gdk_event_new (GDK_DELETE);
393   
394   event->any.window = g_object_ref (widget->window);
395   event->any.send_event = TRUE;
396   
397   gtk_main_do_event (event);
398   gdk_event_free (event);
399 }
400
401 GtkWidget*
402 gtk_dialog_new (void)
403 {
404   return g_object_new (GTK_TYPE_DIALOG, NULL);
405 }
406
407 static GtkWidget*
408 gtk_dialog_new_empty (const gchar     *title,
409                       GtkWindow       *parent,
410                       GtkDialogFlags   flags)
411 {
412   GtkDialog *dialog;
413
414   dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
415
416   if (title)
417     gtk_window_set_title (GTK_WINDOW (dialog), title);
418
419   if (parent)
420     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
421
422   if (flags & GTK_DIALOG_MODAL)
423     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
424   
425   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
426     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
427
428   if (flags & GTK_DIALOG_NO_SEPARATOR)
429     gtk_dialog_set_has_separator (dialog, FALSE);
430   
431   return GTK_WIDGET (dialog);
432 }
433
434 /**
435  * gtk_dialog_new_with_buttons:
436  * @title: Title of the dialog, or %NULL
437  * @parent: Transient parent of the dialog, or %NULL
438  * @flags: from #GtkDialogFlags
439  * @first_button_text: stock ID or text to go in first button, or %NULL
440  * @Varargs: response ID for first button, then additional buttons, ending with %NULL
441  * 
442  * Creates a new #GtkDialog with title @title (or %NULL for the default
443  * title; see gtk_window_set_title()) and transient parent @parent (or
444  * %NULL for none; see gtk_window_set_transient_for()). The @flags
445  * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
446  * and/or to have it destroyed along with its transient parent
447  * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
448  * text/response ID pairs should be listed, with a %NULL pointer ending
449  * the list. Button text can be either a stock ID such as
450  * #GTK_STOCK_OK, or some arbitrary text.  A response ID can be
451  * any positive number, or one of the values in the #GtkResponseType
452  * enumeration. If the user clicks one of these dialog buttons,
453  * #GtkDialog will emit the "response" signal with the corresponding
454  * response ID. If a #GtkDialog receives the "delete_event" signal, it
455  * will emit "response" with a response ID of #GTK_RESPONSE_DELETE_EVENT.
456  * However, destroying a dialog does not emit the "response" signal;
457  * so be careful relying on "response" when using
458  * the #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
459  * so the first button in the list will be the leftmost button in the dialog.
460  *
461  * Here's a simple example:
462  * <informalexample><programlisting>
463  *  GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
464  *                                                   main_app_window,
465  *                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
466  *                                                   GTK_STOCK_OK,
467  *                                                   GTK_RESPONSE_ACCEPT,
468  *                                                   GTK_STOCK_CANCEL,
469  *                                                   GTK_RESPONSE_REJECT,
470  *                                                   NULL);
471  * </programlisting></informalexample>
472  * 
473  * Return value: a new #GtkDialog
474  **/
475 GtkWidget*
476 gtk_dialog_new_with_buttons (const gchar    *title,
477                              GtkWindow      *parent,
478                              GtkDialogFlags  flags,
479                              const gchar    *first_button_text,
480                              ...)
481 {
482   GtkDialog *dialog;
483   va_list args;
484   
485   dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
486
487   va_start (args, first_button_text);
488
489   gtk_dialog_add_buttons_valist (dialog,
490                                  first_button_text,
491                                  args);
492   
493   va_end (args);
494
495   return GTK_WIDGET (dialog);
496 }
497
498 static ResponseData*
499 get_response_data (GtkWidget *widget)
500 {
501   ResponseData *ad = g_object_get_data (G_OBJECT (widget),
502                                         "gtk-dialog-response-data");
503
504   if (ad == NULL)
505     {
506       ad = g_new (ResponseData, 1);
507       
508       g_object_set_data_full (G_OBJECT (widget),
509                               "gtk-dialog-response-data",
510                               ad,
511                               g_free);
512     }
513
514   return ad;
515 }
516
517 static void
518 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
519 {
520   ResponseData *ad;
521   gint response_id;
522   
523   g_return_if_fail (GTK_IS_DIALOG (dialog));
524
525   response_id = GTK_RESPONSE_NONE;
526   
527   ad = get_response_data (widget);
528
529   g_assert (ad != NULL);
530   
531   response_id = ad->response_id;
532
533   gtk_dialog_response (dialog, response_id);
534 }
535
536 /**
537  * gtk_dialog_add_action_widget:
538  * @dialog: a #GtkDialog
539  * @child: an activatable widget
540  * @response_id: response ID for @child
541  * 
542  * Adds an activatable widget to the action area of a #GtkDialog,
543  * connecting a signal handler that will emit the "response" signal on
544  * the dialog when the widget is activated.  The widget is appended to
545  * the end of the dialog's action area.  If you want to add a
546  * non-activatable widget, simply pack it into the
547  * <literal>action_area</literal> field of the #GtkDialog struct.
548  **/
549 void
550 gtk_dialog_add_action_widget (GtkDialog *dialog,
551                               GtkWidget *child,
552                               gint       response_id)
553 {
554   ResponseData *ad;
555   gint signal_id = 0;
556   
557   g_return_if_fail (GTK_IS_DIALOG (dialog));
558   g_return_if_fail (GTK_IS_WIDGET (child));
559
560   ad = get_response_data (child);
561
562   ad->response_id = response_id;
563
564   if (GTK_IS_BUTTON (child))
565     signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
566   else
567     signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal != 0;
568
569   if (signal_id)
570     {
571       GClosure *closure;
572
573       closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
574                                        G_OBJECT (dialog));
575       g_signal_connect_closure_by_id (child,
576                                       signal_id,
577                                       0,
578                                       closure,
579                                       FALSE);
580     }
581   else
582     g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
583
584   gtk_box_pack_end (GTK_BOX (dialog->action_area),
585                     child,
586                     FALSE, TRUE, 0);
587   
588   if (response_id == GTK_RESPONSE_HELP)
589     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
590 }
591
592 /**
593  * gtk_dialog_add_button:
594  * @dialog: a #GtkDialog
595  * @button_text: text of button, or stock ID
596  * @response_id: response ID for the button
597  * 
598  * Adds a button with the given text (or a stock button, if @button_text is a
599  * stock ID) and sets things up so that clicking the button will emit the
600  * "response" signal with the given @response_id. The button is appended to the
601  * end of the dialog's action area. The button widget is returned, but usually
602  * you don't need it.
603  *
604  * Return value: the button widget that was added
605  **/
606 GtkWidget*
607 gtk_dialog_add_button (GtkDialog   *dialog,
608                        const gchar *button_text,
609                        gint         response_id)
610 {
611   GtkWidget *button;
612   
613   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
614   g_return_val_if_fail (button_text != NULL, NULL);
615
616   button = gtk_button_new_from_stock (button_text);
617
618   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
619   
620   gtk_widget_show (button);
621   
622   gtk_dialog_add_action_widget (dialog,
623                                 button,
624                                 response_id);
625
626   return button;
627 }
628
629 static void
630 gtk_dialog_add_buttons_valist(GtkDialog      *dialog,
631                               const gchar    *first_button_text,
632                               va_list         args)
633 {
634   const gchar* text;
635   gint response_id;
636
637   g_return_if_fail (GTK_IS_DIALOG (dialog));
638   
639   if (first_button_text == NULL)
640     return;
641   
642   text = first_button_text;
643   response_id = va_arg (args, gint);
644
645   while (text != NULL)
646     {
647       gtk_dialog_add_button (dialog, text, response_id);
648
649       text = va_arg (args, gchar*);
650       if (text == NULL)
651         break;
652       response_id = va_arg (args, int);
653     }
654 }
655
656 /**
657  * gtk_dialog_add_buttons:
658  * @dialog: a #GtkDialog
659  * @first_button_text: button text or stock ID
660  * @Varargs: response ID for first button, then more text-response_id pairs
661  * 
662  * Adds more buttons, same as calling gtk_dialog_add_button()
663  * repeatedly.  The variable argument list should be %NULL-terminated
664  * as with gtk_dialog_new_with_buttons(). Each button must have both
665  * text and response ID.
666  **/
667 void
668 gtk_dialog_add_buttons (GtkDialog   *dialog,
669                         const gchar *first_button_text,
670                         ...)
671 {  
672   va_list args;
673
674   va_start (args, first_button_text);
675
676   gtk_dialog_add_buttons_valist (dialog,
677                                  first_button_text,
678                                  args);
679   
680   va_end (args);
681 }
682
683 /**
684  * gtk_dialog_set_response_sensitive:
685  * @dialog: a #GtkDialog
686  * @response_id: a response ID
687  * @setting: %TRUE for sensitive
688  *
689  * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal> 
690  * for each widget in the dialog's action area with the given @response_id.
691  * A convenient way to sensitize/desensitize dialog buttons.
692  **/
693 void
694 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
695                                    gint       response_id,
696                                    gboolean   setting)
697 {
698   GList *children;
699   GList *tmp_list;
700
701   g_return_if_fail (GTK_IS_DIALOG (dialog));
702
703   children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
704
705   tmp_list = children;
706   while (tmp_list != NULL)
707     {
708       GtkWidget *widget = tmp_list->data;
709       ResponseData *rd = g_object_get_data (G_OBJECT (widget),
710                                             "gtk-dialog-response-data");
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 = g_object_get_data (G_OBJECT (widget),
746                                             "gtk-dialog-response-data");
747
748       if (rd && rd->response_id == response_id)
749         gtk_widget_grab_default (widget);
750             
751       tmp_list = g_list_next (tmp_list);
752     }
753
754   g_list_free (children);
755 }
756
757 /**
758  * gtk_dialog_set_has_separator:
759  * @dialog: a #GtkDialog
760  * @setting: %TRUE to have a separator
761  *
762  * Sets whether the dialog has a separator above the buttons.
763  * %TRUE by default.
764  **/
765 void
766 gtk_dialog_set_has_separator (GtkDialog *dialog,
767                               gboolean   setting)
768 {
769   g_return_if_fail (GTK_IS_DIALOG (dialog));
770
771   /* this might fail if we get called before _init() somehow */
772   g_assert (dialog->vbox != NULL);
773   
774   if (setting && dialog->separator == NULL)
775     {
776       dialog->separator = gtk_hseparator_new ();
777       gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
778
779       /* The app programmer could screw this up, but, their own fault.
780        * Moves the separator just above the action area.
781        */
782       gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
783       gtk_widget_show (dialog->separator);
784     }
785   else if (!setting && dialog->separator != NULL)
786     {
787       gtk_widget_destroy (dialog->separator);
788       dialog->separator = NULL;
789     }
790
791   g_object_notify (G_OBJECT (dialog), "has_separator");
792 }
793
794 /**
795  * gtk_dialog_get_has_separator:
796  * @dialog: a #GtkDialog
797  * 
798  * Accessor for whether the dialog has a separator.
799  * 
800  * Return value: %TRUE if the dialog has a separator
801  **/
802 gboolean
803 gtk_dialog_get_has_separator (GtkDialog *dialog)
804 {
805   g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
806
807   return dialog->separator != NULL;
808 }
809
810 /**
811  * gtk_dialog_response:
812  * @dialog: a #GtkDialog
813  * @response_id: response ID 
814  * 
815  * Emits the "response" signal with the given response ID. Used to
816  * indicate that the user has responded to the dialog in some way;
817  * typically either you or gtk_dialog_run() will be monitoring the
818  * "response" signal and take appropriate action.
819  **/
820 void
821 gtk_dialog_response (GtkDialog *dialog,
822                      gint       response_id)
823 {
824   g_return_if_fail (GTK_IS_DIALOG (dialog));
825
826   g_signal_emit (dialog,
827                  dialog_signals[RESPONSE],
828                  0,
829                  response_id);
830 }
831
832 typedef struct
833 {
834   GtkDialog *dialog;
835   gint response_id;
836   GMainLoop *loop;
837   gboolean destroyed;
838 } RunInfo;
839
840 static void
841 shutdown_loop (RunInfo *ri)
842 {
843   if (g_main_loop_is_running (ri->loop))
844     g_main_loop_quit (ri->loop);
845 }
846
847 static void
848 run_unmap_handler (GtkDialog *dialog, gpointer data)
849 {
850   RunInfo *ri = data;
851
852   shutdown_loop (ri);
853 }
854
855 static void
856 run_response_handler (GtkDialog *dialog,
857                       gint response_id,
858                       gpointer data)
859 {
860   RunInfo *ri;
861
862   ri = data;
863
864   ri->response_id = response_id;
865
866   shutdown_loop (ri);
867 }
868
869 static gint
870 run_delete_handler (GtkDialog *dialog,
871                     GdkEventAny *event,
872                     gpointer data)
873 {
874   RunInfo *ri = data;
875     
876   shutdown_loop (ri);
877   
878   return TRUE; /* Do not destroy */
879 }
880
881 static void
882 run_destroy_handler (GtkDialog *dialog, gpointer data)
883 {
884   RunInfo *ri = data;
885
886   /* shutdown_loop will be called by run_unmap_handler */
887   
888   ri->destroyed = TRUE;
889 }
890
891 /**
892  * gtk_dialog_run:
893  * @dialog: a #GtkDialog
894  * 
895  * Blocks in a recursive main loop until the @dialog either emits the
896  * response signal, or is destroyed. If the dialog is destroyed during the call
897  * to gtk_dialog_run(), gtk_dialog_returns #GTK_RESPONSE_NONE.
898  * Otherwise, it returns the response ID from the "response" signal emission.
899  * Before entering the recursive main loop, gtk_dialog_run() calls
900  * gtk_widget_show() on the dialog for you. Note that you still
901  * need to show any children of the dialog yourself.
902  *
903  * During gtk_dialog_run(), the default behavior of "delete_event" is
904  * disabled; if the dialog receives "delete_event", it will not be
905  * destroyed as windows usually are, and gtk_dialog_run() will return
906  * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be
907  * modal. You can force gtk_dialog_run() to return at any time by
908  * calling gtk_dialog_response() to emit the "response"
909  * signal. Destroying the dialog during gtk_dialog_run() is a very bad
910  * idea, because your post-run code won't know whether the dialog was
911  * destroyed or not.
912  *
913  * After gtk_dialog_run() returns, you are responsible for hiding or
914  * destroying the dialog if you wish to do so.
915  *
916  * Typical usage of this function might be:
917  * <informalexample><programlisting>
918  *   gint result = gtk_dialog_run (GTK_DIALOG (dialog));
919  *   switch (result)
920  *     {
921  *       case GTK_RESPONSE_ACCEPT:
922  *          do_application_specific_something (<!-- -->);
923  *          break;
924  *       default:
925  *          do_nothing_since_dialog_was_cancelled (<!-- -->);
926  *          break;
927  *     }
928  *   gtk_widget_destroy (dialog);
929  * </programlisting></informalexample>
930  * 
931  * Return value: response ID
932  **/
933 gint
934 gtk_dialog_run (GtkDialog *dialog)
935 {
936   RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
937   gboolean was_modal;
938   gulong response_handler;
939   gulong unmap_handler;
940   gulong destroy_handler;
941   gulong delete_handler;
942   
943   g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
944
945   g_object_ref (dialog);
946
947   if (!GTK_WIDGET_VISIBLE (dialog))
948     gtk_widget_show (GTK_WIDGET (dialog));
949   
950   was_modal = GTK_WINDOW (dialog)->modal;
951   if (!was_modal)
952     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
953
954   response_handler =
955     g_signal_connect (dialog,
956                       "response",
957                       G_CALLBACK (run_response_handler),
958                       &ri);
959   
960   unmap_handler =
961     g_signal_connect (dialog,
962                       "unmap",
963                       G_CALLBACK (run_unmap_handler),
964                       &ri);
965   
966   delete_handler =
967     g_signal_connect (dialog,
968                       "delete_event",
969                       G_CALLBACK (run_delete_handler),
970                       &ri);
971   
972   destroy_handler =
973     g_signal_connect (dialog,
974                       "destroy",
975                       G_CALLBACK (run_destroy_handler),
976                       &ri);
977   
978   ri.loop = g_main_loop_new (NULL, FALSE);
979
980   GDK_THREADS_LEAVE ();  
981   g_main_loop_run (ri.loop);
982   GDK_THREADS_ENTER ();  
983
984   g_main_loop_unref (ri.loop);
985
986   ri.loop = NULL;
987   ri.destroyed = FALSE;
988   
989   if (!ri.destroyed)
990     {
991       if (!was_modal)
992         gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
993       
994       g_signal_handler_disconnect (dialog, response_handler);
995       g_signal_handler_disconnect (dialog, unmap_handler);
996       g_signal_handler_disconnect (dialog, delete_handler);
997       g_signal_handler_disconnect (dialog, destroy_handler);
998     }
999
1000   g_object_unref (dialog);
1001
1002   return ri.response_id;
1003 }