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