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