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