]> Pileus Git - ~andy/gtk/blob - gtk/gtkdialog.c
Patch from Matthias Clasen to remove remove all instances of
[~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 }
239
240
241 static void 
242 gtk_dialog_set_property (GObject      *object,
243                          guint         prop_id,
244                          const GValue *value,
245                          GParamSpec   *pspec)
246 {
247   GtkDialog *dialog;
248   
249   dialog = GTK_DIALOG (object);
250   
251   switch (prop_id)
252     {
253     case PROP_HAS_SEPARATOR:
254       gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
255       break;
256
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260     }
261 }
262
263 static void 
264 gtk_dialog_get_property (GObject     *object,
265                          guint        prop_id,
266                          GValue      *value,
267                          GParamSpec  *pspec)
268 {
269   GtkDialog *dialog;
270   
271   dialog = GTK_DIALOG (object);
272   
273   switch (prop_id)
274     {
275     case PROP_HAS_SEPARATOR:
276       g_value_set_boolean (value, dialog->separator != NULL);
277       break;
278
279     default:
280       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281       break;
282     }
283 }
284
285 static gint
286 gtk_dialog_delete_event_handler (GtkWidget   *widget,
287                                  GdkEventAny *event,
288                                  gpointer     user_data)
289 {
290   /* emit response signal */
291   gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
292
293   /* Do the destroy by default */
294   return FALSE;
295 }
296
297 static void
298 gtk_dialog_style_set (GtkWidget *widget,
299                       GtkStyle  *prev_style)
300 {
301   update_spacings (GTK_DIALOG (widget));
302 }
303
304 static void
305 gtk_dialog_close (GtkDialog *dialog)
306 {
307   /* Synthesize delete_event to close dialog. */
308   
309   GdkEventAny event;
310   GtkWidget *widget;
311
312   widget = GTK_WIDGET (dialog);
313   
314   event.type = GDK_DELETE;
315   event.window = widget->window;
316   event.send_event = TRUE;
317   
318   g_object_ref (G_OBJECT (event.window));
319   
320   gtk_main_do_event ((GdkEvent*)&event);
321   
322   g_object_unref (G_OBJECT (event.window));
323 }
324
325 GtkWidget*
326 gtk_dialog_new (void)
327 {
328   return GTK_WIDGET (gtk_type_new (GTK_TYPE_DIALOG));
329 }
330
331 static GtkWidget*
332 gtk_dialog_new_empty (const gchar     *title,
333                       GtkWindow       *parent,
334                       GtkDialogFlags   flags)
335 {
336   GtkDialog *dialog;
337
338   dialog = GTK_DIALOG (g_object_new (GTK_TYPE_DIALOG, NULL));
339
340   if (title)
341     gtk_window_set_title (GTK_WINDOW (dialog), title);
342
343   if (parent)
344     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
345
346   if (flags & GTK_DIALOG_MODAL)
347     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
348   
349   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
350     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
351
352   if (flags & GTK_DIALOG_NO_SEPARATOR)
353     gtk_dialog_set_has_separator (dialog, FALSE);
354   
355   return GTK_WIDGET (dialog);
356 }
357
358 /**
359  * gtk_dialog_new_with_buttons:
360  * @title: Title of the dialog, or NULL
361  * @parent: Transient parent of the dialog, or NULL
362  * @flags: from #GtkDialogFlags
363  * @first_button_text: stock ID or text to go in first button, or NULL
364  * @Varargs: response ID for first button, then additional buttons, ending with NULL
365  * 
366  * Creates a new #GtkDialog with title @title (or NULL for the default
367  * title; see gtk_window_set_title()) and transient parent @parent (or
368  * NULL for none; see gtk_window_set_transient_for()). The @flags
369  * argument can be used to make the dialog modal (GTK_DIALOG_MODAL)
370  * and/or to have it destroyed along with its transient parent
371  * (GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
372  * text/response ID pairs should be listed, with a NULL pointer ending
373  * the list. Button text can be either a stock ID such as
374  * #GTK_STOCK_OK, or some arbitrary text.  A response ID can be
375  * any positive number, or one of the values in the #GtkResponseType
376  * enumeration. If the user clicks one of these dialog buttons,
377  * #GtkDialog will emit the "response" signal with the corresponding
378  * response ID. If a #GtkDialog receives the "delete_event" signal, it
379  * will emit "response" with a response ID of GTK_RESPONSE_DELETE_EVENT.
380  * However, destroying a dialog does not emit the "response" signal;
381  * so be careful relying on "response" when using
382  * the GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
383  * so the first button in the list will be the leftmost button in the dialog.
384  *
385  * Here's a simple example:
386  * <programlisting>
387  *  GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
388  *                                                   main_app_window,
389  *                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
390  *                                                   GTK_STOCK_OK,
391  *                                                   GTK_RESPONSE_ACCEPT,
392  *                                                   GTK_STOCK_CANCEL,
393  *                                                   GTK_RESPONSE_REJECT,
394  *                                                   NULL);
395  * </programlisting>
396  * 
397  * Return value: a new #GtkDialog
398  **/
399 GtkWidget*
400 gtk_dialog_new_with_buttons (const gchar    *title,
401                              GtkWindow      *parent,
402                              GtkDialogFlags  flags,
403                              const gchar    *first_button_text,
404                              ...)
405 {
406   GtkDialog *dialog;
407   va_list args;
408   
409   dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
410
411   va_start (args, first_button_text);
412
413   gtk_dialog_add_buttons_valist (dialog,
414                                  first_button_text,
415                                  args);
416   
417   va_end (args);
418
419   return GTK_WIDGET (dialog);
420 }
421
422 typedef struct _ResponseData ResponseData;
423
424 struct _ResponseData
425 {
426   gint response_id;
427 };
428
429 static ResponseData*
430 get_response_data (GtkWidget *widget)
431 {
432   ResponseData *ad = gtk_object_get_data (GTK_OBJECT (widget),
433                                           "gtk-dialog-response-data");
434
435   if (ad == NULL)
436     {
437       ad = g_new (ResponseData, 1);
438       
439       gtk_object_set_data_full (GTK_OBJECT (widget),
440                                 "gtk-dialog-response-data",
441                                 ad,
442                                 g_free);
443     }
444
445   return ad;
446 }
447
448 static void
449 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
450 {
451   ResponseData *ad;
452   gint response_id;
453   
454   g_return_if_fail (GTK_IS_DIALOG (dialog));
455
456   response_id = GTK_RESPONSE_NONE;
457   
458   ad = get_response_data (widget);
459
460   g_assert (ad != NULL);
461   
462   response_id = ad->response_id;
463
464   gtk_dialog_response (dialog, response_id);
465 }
466 /**
467  * gtk_dialog_add_action_widget:
468  * @dialog: a #GtkDialog
469  * @child: an activatable widget
470  * @response_id: response ID for @child
471  * 
472  * Adds an activatable widget to the action area of a #GtkDialog,
473  * connecting a signal handler that will emit the "response" signal on
474  * the dialog when the widget is activated.  The widget is appended to
475  * the end of the dialog's action area.  If you want to add a
476  * non-activatable widget, simply pack it into the
477  * <literal>action_area</literal> field of the #GtkDialog struct.
478  * 
479  **/
480 void
481 gtk_dialog_add_action_widget  (GtkDialog *dialog,
482                                GtkWidget *child,
483                                gint       response_id)
484 {
485   ResponseData *ad;
486   gint signal_id = 0;
487   
488   g_return_if_fail (GTK_IS_DIALOG (dialog));
489   g_return_if_fail (GTK_IS_WIDGET (child));
490
491   ad = get_response_data (child);
492
493   ad->response_id = response_id;
494
495   if (GTK_IS_BUTTON (child))
496     {
497       signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
498     }
499   else
500     signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal != 0;
501
502   if (signal_id)
503     {
504       const gchar* name = gtk_signal_name (signal_id);
505
506       gtk_signal_connect_while_alive (GTK_OBJECT (child),
507                                       name,
508                                       GTK_SIGNAL_FUNC (action_widget_activated),
509                                       dialog,
510                                       GTK_OBJECT (dialog));
511     }
512   else
513     g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
514
515   gtk_box_pack_end (GTK_BOX (dialog->action_area),
516                     child,
517                     FALSE, TRUE, 0);
518   
519   if (response_id == GTK_RESPONSE_HELP)
520     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
521 }
522
523 /**
524  * gtk_dialog_add_button:
525  * @dialog: a #GtkDialog
526  * @button_text: text of button, or stock ID
527  * @response_id: response ID for the button
528  * 
529  * Adds a button with the given text (or a stock button, if @button_text is a
530  * stock ID) and sets things up so that clicking the button will emit the
531  * "response" signal with the given @response_id. The button is appended to the
532  * end of the dialog's action area. The button widget is returned, but usually
533  * you don't need it.
534  *
535  * Return value: the button widget that was added
536  **/
537 GtkWidget*
538 gtk_dialog_add_button (GtkDialog   *dialog,
539                        const gchar *button_text,
540                        gint         response_id)
541 {
542   GtkWidget *button;
543   
544   g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
545   g_return_val_if_fail (button_text != NULL, NULL);
546
547   button = gtk_button_new_from_stock (button_text);
548
549   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
550   
551   gtk_widget_show (button);
552   
553   gtk_dialog_add_action_widget (dialog,
554                                 button,
555                                 response_id);
556
557   return button;
558 }
559
560 static void
561 gtk_dialog_add_buttons_valist(GtkDialog      *dialog,
562                               const gchar    *first_button_text,
563                               va_list         args)
564 {
565   const gchar* text;
566   gint response_id;
567
568   g_return_if_fail (GTK_IS_DIALOG (dialog));
569   
570   if (first_button_text == NULL)
571     return;
572   
573   text = first_button_text;
574   response_id = va_arg (args, gint);
575
576   while (text != NULL)
577     {
578       gtk_dialog_add_button (dialog, text, response_id);
579
580       text = va_arg (args, gchar*);
581       if (text == NULL)
582         break;
583       response_id = va_arg (args, int);
584     }
585 }
586
587 /**
588  * gtk_dialog_add_buttons:
589  * @dialog: a #GtkDialog
590  * @first_button_text: button text or stock ID
591  * @Varargs: response ID for first button, then more text-response_id pairs
592  * 
593  * Adds more buttons, same as calling gtk_dialog_add_button()
594  * repeatedly.  The variable argument list should be NULL-terminated
595  * as with gtk_dialog_new_with_buttons(). Each button must have both
596  * text and response ID.
597  * 
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 gtk_widget_set_sensitive (widget, @setting) for each
622  * widget in the dialog's action area with the given @response_id.
623  * A convenient way to sensitize/desensitize dialog buttons.
624  * 
625  **/
626 void
627 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
628                                    gint       response_id,
629                                    gboolean   setting)
630 {
631   GList *children;
632   GList *tmp_list;
633
634   children = gtk_container_children (GTK_CONTAINER (dialog));
635
636   tmp_list = children;
637   while (tmp_list != NULL)
638     {
639       GtkWidget *widget = tmp_list->data;
640       ResponseData *rd = g_object_get_data (G_OBJECT (widget),
641                                             "gtk-dialog-response-data");
642
643       if (rd && rd->response_id == response_id)
644         gtk_widget_set_sensitive (widget, setting);
645
646       tmp_list = g_list_next (tmp_list);
647     }
648
649   g_list_free (children);
650 }
651
652 /**
653  * gtk_dialog_set_default_response:
654  * @dialog: a #GtkDialog
655  * @response_id: a response ID
656  * 
657  * Sets the last widget in the dialog's action area with the given @response_id
658  * as the default widget for the dialog. Pressing "Enter" normally activates
659  * the default widget.
660  * 
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   children = gtk_container_children (GTK_CONTAINER (dialog->action_area));
670
671   tmp_list = children;
672   while (tmp_list != NULL)
673     {
674       GtkWidget *widget = tmp_list->data;
675       ResponseData *rd = g_object_get_data (G_OBJECT (widget),
676                                             "gtk-dialog-response-data");
677
678       if (rd && rd->response_id == response_id)
679         {
680           gtk_widget_grab_default (widget);
681           
682           if (!GTK_WINDOW (dialog)->focus_widget)
683             gtk_widget_grab_focus (widget);
684         }
685             
686       tmp_list = g_list_next (tmp_list);
687     }
688
689   g_list_free (children);
690 }
691
692 void
693 gtk_dialog_set_has_separator (GtkDialog *dialog,
694                               gboolean   setting)
695 {
696   g_return_if_fail (GTK_IS_DIALOG (dialog));
697
698   /* this might fail if we get called before _init() somehow */
699   g_assert (dialog->vbox != NULL);
700   
701   if (setting && dialog->separator == NULL)
702     {
703       dialog->separator = gtk_hseparator_new ();
704       gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
705
706       /* The app programmer could screw this up, but, their own fault.
707        * Moves the separator just above the action area.
708        */
709       gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
710       gtk_widget_show (dialog->separator);
711     }
712   else if (!setting && dialog->separator != NULL)
713     {
714       gtk_widget_destroy (dialog->separator);
715       dialog->separator = NULL;
716     }
717
718   g_object_notify (G_OBJECT (dialog), "has_separator");
719 }
720
721 gboolean
722 gtk_dialog_get_has_separator (GtkDialog *dialog)
723 {
724   g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
725
726   return dialog->separator != NULL;
727 }
728
729 /**
730  * gtk_dialog_response:
731  * @dialog: a #GtkDialog
732  * @response_id: response ID 
733  * 
734  * Emits the "response" signal with the given response ID. Used to
735  * indicate that the user has responded to the dialog in some way;
736  * typically either you or gtk_dialog_run() will be monitoring the
737  * "response" signal and take appropriate action.
738  **/
739 void
740 gtk_dialog_response (GtkDialog *dialog,
741                      gint       response_id)
742 {
743   g_return_if_fail (GTK_IS_DIALOG (dialog));
744
745   gtk_signal_emit (GTK_OBJECT (dialog),
746                    dialog_signals[RESPONSE],
747                    response_id);
748 }
749
750 typedef struct
751 {
752   GtkDialog *dialog;
753   gint response_id;
754   GMainLoop *loop;
755 } RunInfo;
756
757 static void
758 shutdown_loop (RunInfo *ri)
759 {
760   if (g_main_loop_is_running (ri->loop))
761     g_main_loop_quit (ri->loop);
762 }
763
764 static void
765 run_unmap_handler (GtkDialog *dialog, gpointer data)
766 {
767   RunInfo *ri = data;
768
769   shutdown_loop (ri);
770 }
771
772 static void
773 run_response_handler (GtkDialog *dialog,
774                       gint response_id,
775                       gpointer data)
776 {
777   RunInfo *ri;
778
779   ri = data;
780
781   ri->response_id = response_id;
782
783   shutdown_loop (ri);
784 }
785
786 static gint
787 run_delete_handler (GtkDialog *dialog,
788                     GdkEventAny *event,
789                     gpointer data)
790 {
791   RunInfo *ri = data;
792     
793   shutdown_loop (ri);
794   
795   return TRUE; /* Do not destroy */
796 }
797
798 /**
799  * gtk_dialog_run:
800  * @dialog: a #GtkDialog
801  * 
802  * Blocks in a recursive main loop until the @dialog either emits the
803  * response signal, or is destroyed. If the dialog is destroyed,
804  * gtk_dialog_run() returns GTK_RESPONSE_NONE. Otherwise, it returns
805  * the response ID from the "response" signal emission. Before
806  * entering the recursive main loop, gtk_dialog_run() calls
807  * gtk_widget_show() on the dialog for you. Note that you still
808  * need to show any children of the dialog yourself.
809  *
810  * During gtk_dialog_run(), the default behavior of "delete_event" is
811  * disabled; if the dialog receives "delete_event", it will not be
812  * destroyed as windows usually are, and gtk_dialog_run() will return
813  * GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be
814  * modal. You can force gtk_dialog_run() to return at any time by
815  * calling gtk_dialog_response() to emit the "response"
816  * signal. Destroying the dialog during gtk_dialog_run() is a very bad
817  * idea, because your post-run code won't know whether the dialog was
818  * destroyed or not.
819  *
820  * After gtk_dialog_run() returns, you are responsible for hiding or
821  * destroying the dialog if you wish to do so.
822  *
823  * Typical usage of this function might be:
824  * <programlisting>
825  *   gint result = gtk_dialog_run (GTK_DIALOG (dialog));
826  *   switch (result)
827  *     {
828  *       case GTK_RESPONSE_ACCEPT:
829  *          do_application_specific_something ();
830  *          break;
831  *       default:
832  *          do_nothing_since_dialog_was_cancelled ();
833  *          break;
834  *     }
835  *   gtk_widget_destroy (dialog);
836  * </programlisting>
837  * 
838  * Return value: response ID
839  **/
840 gint
841 gtk_dialog_run (GtkDialog *dialog)
842 {
843   RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
844   gboolean was_modal;
845   guint response_handler;
846   guint destroy_handler;
847   guint delete_handler;
848   
849   g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
850
851   gtk_object_ref (GTK_OBJECT (dialog));
852
853   if (!GTK_WIDGET_VISIBLE (dialog))
854     gtk_widget_show (GTK_WIDGET (dialog));
855   
856   was_modal = GTK_WINDOW (dialog)->modal;
857   if (!was_modal)
858     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
859
860   response_handler =
861     gtk_signal_connect (GTK_OBJECT (dialog),
862                         "response",
863                         GTK_SIGNAL_FUNC (run_response_handler),
864                         &ri);
865   
866   destroy_handler =
867     gtk_signal_connect (GTK_OBJECT (dialog),
868                         "unmap",
869                         GTK_SIGNAL_FUNC (run_unmap_handler),
870                         &ri);
871   
872   delete_handler =
873     gtk_signal_connect (GTK_OBJECT (dialog),
874                         "delete_event",
875                         GTK_SIGNAL_FUNC (run_delete_handler),
876                         &ri);
877   
878   ri.loop = g_main_new (FALSE);
879
880   GDK_THREADS_LEAVE ();  
881   g_main_loop_run (ri.loop);
882   GDK_THREADS_ENTER ();  
883
884   g_main_loop_unref (ri.loop);
885
886   ri.loop = NULL;
887   
888   if (!GTK_OBJECT_DESTROYED (dialog))
889     {
890       if (!was_modal)
891         gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
892       
893       gtk_signal_disconnect (GTK_OBJECT (dialog), destroy_handler);
894       gtk_signal_disconnect (GTK_OBJECT (dialog), response_handler);
895       gtk_signal_disconnect (GTK_OBJECT (dialog), delete_handler);
896     }
897
898   gtk_object_unref (GTK_OBJECT (dialog));
899
900   return ri.response_id;
901 }
902
903
904
905