1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "gtkbutton.h"
28 #include "gtkdialog.h"
30 #include "gtkhseparator.h"
31 #include "gtkmarshalers.h"
33 #include "gtksignal.h"
34 #include "gdkkeysyms.h"
37 #include "gtkbindings.h"
39 static void gtk_dialog_class_init (GtkDialogClass *klass);
40 static void gtk_dialog_init (GtkDialog *dialog);
42 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
43 const gchar *first_button_text,
46 static gint gtk_dialog_delete_event_handler (GtkWidget *widget,
50 static void gtk_dialog_set_property (GObject *object,
54 static void gtk_dialog_get_property (GObject *object,
58 static void gtk_dialog_style_set (GtkWidget *widget,
59 GtkStyle *prev_style);
61 static void gtk_dialog_close (GtkDialog *dialog);
74 static gpointer parent_class;
75 static guint dialog_signals[LAST_SIGNAL];
78 gtk_dialog_get_type (void)
80 static GtkType dialog_type = 0;
84 static const GtkTypeInfo dialog_info =
88 sizeof (GtkDialogClass),
89 (GtkClassInitFunc) gtk_dialog_class_init,
90 (GtkObjectInitFunc) gtk_dialog_init,
91 /* reserved_1 */ NULL,
92 /* reserved_2 */ NULL,
93 (GtkClassInitFunc) NULL,
96 dialog_type = gtk_type_unique (GTK_TYPE_WINDOW, &dialog_info);
103 gtk_dialog_class_init (GtkDialogClass *class)
105 GObjectClass *gobject_class;
106 GtkObjectClass *object_class;
107 GtkWidgetClass *widget_class;
108 GtkBindingSet *binding_set;
110 gobject_class = G_OBJECT_CLASS (class);
111 object_class = GTK_OBJECT_CLASS (class);
112 widget_class = GTK_WIDGET_CLASS (class);
114 parent_class = g_type_class_peek_parent (class);
116 gobject_class->set_property = gtk_dialog_set_property;
117 gobject_class->get_property = gtk_dialog_get_property;
119 widget_class->style_set = gtk_dialog_style_set;
121 class->close = gtk_dialog_close;
123 g_object_class_install_property (gobject_class,
125 g_param_spec_boolean ("has_separator",
127 _("The dialog has a separator bar above its buttons"),
131 dialog_signals[RESPONSE] =
132 gtk_signal_new ("response",
134 GTK_CLASS_TYPE (object_class),
135 GTK_SIGNAL_OFFSET (GtkDialogClass, response),
136 _gtk_marshal_NONE__INT,
140 dialog_signals[CLOSE] =
141 gtk_signal_new ("close",
142 GTK_RUN_LAST | GTK_RUN_ACTION,
143 GTK_CLASS_TYPE (object_class),
144 GTK_SIGNAL_OFFSET (GtkDialogClass, close),
145 _gtk_marshal_NONE__NONE,
148 gtk_widget_class_install_style_property (widget_class,
149 g_param_spec_int ("content_area_border",
150 _("Content area border"),
151 _("Width of border around the main dialog area"),
156 gtk_widget_class_install_style_property (widget_class,
157 g_param_spec_int ("button_spacing",
159 _("Spacing between buttons"),
165 gtk_widget_class_install_style_property (widget_class,
166 g_param_spec_int ("action_area_border",
167 _("Action area border"),
168 _("Width of border around the button area at the bottom of the dialog"),
174 binding_set = gtk_binding_set_by_class (class);
176 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
181 update_spacings (GtkDialog *dialog)
184 gint content_area_border;
186 gint action_area_border;
188 widget = GTK_WIDGET (dialog);
190 gtk_widget_style_get (widget,
191 "content_area_border",
192 &content_area_border,
195 "action_area_border",
199 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
200 content_area_border);
201 gtk_box_set_spacing (GTK_BOX (dialog->action_area),
203 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
208 gtk_dialog_init (GtkDialog *dialog)
210 /* To avoid breaking old code that prevents destroy on delete event
211 * by connecting a handler, we have to have the FIRST signal
212 * connection on the dialog.
214 gtk_signal_connect (GTK_OBJECT (dialog),
216 GTK_SIGNAL_FUNC (gtk_dialog_delete_event_handler),
219 dialog->vbox = gtk_vbox_new (FALSE, 0);
221 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
222 gtk_widget_show (dialog->vbox);
224 dialog->action_area = gtk_hbutton_box_new ();
226 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
229 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
231 gtk_widget_show (dialog->action_area);
233 dialog->separator = gtk_hseparator_new ();
234 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
235 gtk_widget_show (dialog->separator);
237 gtk_window_set_type_hint (GTK_WINDOW (dialog),
238 GDK_WINDOW_TYPE_HINT_DIALOG);
239 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
244 gtk_dialog_set_property (GObject *object,
251 dialog = GTK_DIALOG (object);
255 case PROP_HAS_SEPARATOR:
256 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 gtk_dialog_get_property (GObject *object,
273 dialog = GTK_DIALOG (object);
277 case PROP_HAS_SEPARATOR:
278 g_value_set_boolean (value, dialog->separator != NULL);
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 gtk_dialog_delete_event_handler (GtkWidget *widget,
292 /* emit response signal */
293 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
295 /* Do the destroy by default */
300 gtk_dialog_style_set (GtkWidget *widget,
301 GtkStyle *prev_style)
303 update_spacings (GTK_DIALOG (widget));
307 gtk_dialog_close (GtkDialog *dialog)
309 /* Synthesize delete_event to close dialog. */
314 widget = GTK_WIDGET (dialog);
316 event.type = GDK_DELETE;
317 event.window = widget->window;
318 event.send_event = TRUE;
320 g_object_ref (G_OBJECT (event.window));
322 gtk_main_do_event ((GdkEvent*)&event);
324 g_object_unref (G_OBJECT (event.window));
328 gtk_dialog_new (void)
330 return GTK_WIDGET (gtk_type_new (GTK_TYPE_DIALOG));
334 gtk_dialog_new_empty (const gchar *title,
336 GtkDialogFlags flags)
340 dialog = GTK_DIALOG (g_object_new (GTK_TYPE_DIALOG, NULL));
343 gtk_window_set_title (GTK_WINDOW (dialog), title);
346 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
348 if (flags & GTK_DIALOG_MODAL)
349 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
351 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
352 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
354 if (flags & GTK_DIALOG_NO_SEPARATOR)
355 gtk_dialog_set_has_separator (dialog, FALSE);
357 return GTK_WIDGET (dialog);
361 * gtk_dialog_new_with_buttons:
362 * @title: Title of the dialog, or %NULL
363 * @parent: Transient parent of the dialog, or %NULL
364 * @flags: from #GtkDialogFlags
365 * @first_button_text: stock ID or text to go in first button, or %NULL
366 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
368 * Creates a new #GtkDialog with title @title (or %NULL for the default
369 * title; see gtk_window_set_title()) and transient parent @parent (or
370 * %NULL for none; see gtk_window_set_transient_for()). The @flags
371 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
372 * and/or to have it destroyed along with its transient parent
373 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
374 * text/response ID pairs should be listed, with a %NULL pointer ending
375 * the list. Button text can be either a stock ID such as
376 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
377 * any positive number, or one of the values in the #GtkResponseType
378 * enumeration. If the user clicks one of these dialog buttons,
379 * #GtkDialog will emit the "response" signal with the corresponding
380 * response ID. If a #GtkDialog receives the "delete_event" signal, it
381 * will emit "response" with a response ID of #GTK_RESPONSE_DELETE_EVENT.
382 * However, destroying a dialog does not emit the "response" signal;
383 * so be careful relying on "response" when using
384 * the #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
385 * so the first button in the list will be the leftmost button in the dialog.
387 * Here's a simple example:
388 * <informalexample><programlisting>
389 * <!>GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
390 * <!> main_app_window,
391 * <!> GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
393 * <!> GTK_RESPONSE_ACCEPT,
394 * <!> GTK_STOCK_CANCEL,
395 * <!> GTK_RESPONSE_REJECT,
397 * </programlisting></informalexample>
399 * Return value: a new #GtkDialog
402 gtk_dialog_new_with_buttons (const gchar *title,
404 GtkDialogFlags flags,
405 const gchar *first_button_text,
411 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
413 va_start (args, first_button_text);
415 gtk_dialog_add_buttons_valist (dialog,
421 return GTK_WIDGET (dialog);
424 typedef struct _ResponseData ResponseData;
432 get_response_data (GtkWidget *widget)
434 ResponseData *ad = gtk_object_get_data (GTK_OBJECT (widget),
435 "gtk-dialog-response-data");
439 ad = g_new (ResponseData, 1);
441 gtk_object_set_data_full (GTK_OBJECT (widget),
442 "gtk-dialog-response-data",
451 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
456 g_return_if_fail (GTK_IS_DIALOG (dialog));
458 response_id = GTK_RESPONSE_NONE;
460 ad = get_response_data (widget);
462 g_assert (ad != NULL);
464 response_id = ad->response_id;
466 gtk_dialog_response (dialog, response_id);
469 * gtk_dialog_add_action_widget:
470 * @dialog: a #GtkDialog
471 * @child: an activatable widget
472 * @response_id: response ID for @child
474 * Adds an activatable widget to the action area of a #GtkDialog,
475 * connecting a signal handler that will emit the "response" signal on
476 * the dialog when the widget is activated. The widget is appended to
477 * the end of the dialog's action area. If you want to add a
478 * non-activatable widget, simply pack it into the
479 * <literal>action_area</literal> field of the #GtkDialog struct.
482 gtk_dialog_add_action_widget (GtkDialog *dialog,
489 g_return_if_fail (GTK_IS_DIALOG (dialog));
490 g_return_if_fail (GTK_IS_WIDGET (child));
492 ad = get_response_data (child);
494 ad->response_id = response_id;
496 if (GTK_IS_BUTTON (child))
498 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
501 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal != 0;
505 const gchar* name = gtk_signal_name (signal_id);
507 gtk_signal_connect_while_alive (GTK_OBJECT (child),
509 GTK_SIGNAL_FUNC (action_widget_activated),
511 GTK_OBJECT (dialog));
514 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
516 gtk_box_pack_end (GTK_BOX (dialog->action_area),
520 if (response_id == GTK_RESPONSE_HELP)
521 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
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
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
536 * Return value: the button widget that was added
539 gtk_dialog_add_button (GtkDialog *dialog,
540 const gchar *button_text,
545 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
546 g_return_val_if_fail (button_text != NULL, NULL);
548 button = gtk_button_new_from_stock (button_text);
550 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
552 gtk_widget_show (button);
554 gtk_dialog_add_action_widget (dialog,
562 gtk_dialog_add_buttons_valist(GtkDialog *dialog,
563 const gchar *first_button_text,
569 g_return_if_fail (GTK_IS_DIALOG (dialog));
571 if (first_button_text == NULL)
574 text = first_button_text;
575 response_id = va_arg (args, gint);
579 gtk_dialog_add_button (dialog, text, response_id);
581 text = va_arg (args, gchar*);
584 response_id = va_arg (args, int);
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
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.
600 gtk_dialog_add_buttons (GtkDialog *dialog,
601 const gchar *first_button_text,
606 va_start (args, first_button_text);
608 gtk_dialog_add_buttons_valist (dialog,
616 * gtk_dialog_set_response_sensitive:
617 * @dialog: a #GtkDialog
618 * @response_id: a response ID
619 * @setting: %TRUE for sensitive
621 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
622 * for each widget in the dialog's action area with the given @response_id.
623 * A convenient way to sensitize/desensitize dialog buttons.
626 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
633 g_return_if_fail (GTK_IS_DIALOG (dialog));
635 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
638 while (tmp_list != NULL)
640 GtkWidget *widget = tmp_list->data;
641 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
642 "gtk-dialog-response-data");
644 if (rd && rd->response_id == response_id)
645 gtk_widget_set_sensitive (widget, setting);
647 tmp_list = g_list_next (tmp_list);
650 g_list_free (children);
654 * gtk_dialog_set_default_response:
655 * @dialog: a #GtkDialog
656 * @response_id: a response ID
658 * Sets the last widget in the dialog's action area with the given @response_id
659 * as the default widget for the dialog. Pressing "Enter" normally activates
660 * the default widget.
663 gtk_dialog_set_default_response (GtkDialog *dialog,
669 g_return_if_fail (GTK_IS_DIALOG (dialog));
671 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
674 while (tmp_list != NULL)
676 GtkWidget *widget = tmp_list->data;
677 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
678 "gtk-dialog-response-data");
680 if (rd && rd->response_id == response_id)
682 gtk_widget_grab_default (widget);
684 if (!GTK_WINDOW (dialog)->focus_widget)
685 gtk_widget_grab_focus (widget);
688 tmp_list = g_list_next (tmp_list);
691 g_list_free (children);
695 * gtk_dialog_set_has_separator:
696 * @dialog: a #GtkDialog
697 * @setting: %TRUE to have a separator
699 * Sets whether the dialog has a separator above the buttons.
703 gtk_dialog_set_has_separator (GtkDialog *dialog,
706 g_return_if_fail (GTK_IS_DIALOG (dialog));
708 /* this might fail if we get called before _init() somehow */
709 g_assert (dialog->vbox != NULL);
711 if (setting && dialog->separator == NULL)
713 dialog->separator = gtk_hseparator_new ();
714 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
716 /* The app programmer could screw this up, but, their own fault.
717 * Moves the separator just above the action area.
719 gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
720 gtk_widget_show (dialog->separator);
722 else if (!setting && dialog->separator != NULL)
724 gtk_widget_destroy (dialog->separator);
725 dialog->separator = NULL;
728 g_object_notify (G_OBJECT (dialog), "has_separator");
732 * gtk_dialog_get_has_separator:
733 * @dialog: a #GtkDialog
735 * Accessor for whether the dialog has a separator.
737 * Return value: %TRUE if the dialog has a separator
740 gtk_dialog_get_has_separator (GtkDialog *dialog)
742 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
744 return dialog->separator != NULL;
748 * gtk_dialog_response:
749 * @dialog: a #GtkDialog
750 * @response_id: response ID
752 * Emits the "response" signal with the given response ID. Used to
753 * indicate that the user has responded to the dialog in some way;
754 * typically either you or gtk_dialog_run() will be monitoring the
755 * "response" signal and take appropriate action.
758 gtk_dialog_response (GtkDialog *dialog,
761 g_return_if_fail (GTK_IS_DIALOG (dialog));
763 gtk_signal_emit (GTK_OBJECT (dialog),
764 dialog_signals[RESPONSE],
777 shutdown_loop (RunInfo *ri)
779 if (g_main_loop_is_running (ri->loop))
780 g_main_loop_quit (ri->loop);
784 run_unmap_handler (GtkDialog *dialog, gpointer data)
792 run_response_handler (GtkDialog *dialog,
800 ri->response_id = response_id;
806 run_delete_handler (GtkDialog *dialog,
814 return TRUE; /* Do not destroy */
818 run_destroy_handler (GtkDialog *dialog, gpointer data)
822 /* shutdown_loop will be called by run_unmap_handler */
824 ri->destroyed = TRUE;
829 * @dialog: a #GtkDialog
831 * Blocks in a recursive main loop until the @dialog either emits the
832 * response signal, or is destroyed. If the dialog is destroyed,
833 * gtk_dialog_run() returns #GTK_RESPONSE_NONE. Otherwise, it returns
834 * the response ID from the "response" signal emission. Before
835 * entering the recursive main loop, gtk_dialog_run() calls
836 * gtk_widget_show() on the dialog for you. Note that you still
837 * need to show any children of the dialog yourself.
839 * During gtk_dialog_run(), the default behavior of "delete_event" is
840 * disabled; if the dialog receives "delete_event", it will not be
841 * destroyed as windows usually are, and gtk_dialog_run() will return
842 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be
843 * modal. You can force gtk_dialog_run() to return at any time by
844 * calling gtk_dialog_response() to emit the "response"
845 * signal. Destroying the dialog during gtk_dialog_run() is a very bad
846 * idea, because your post-run code won't know whether the dialog was
849 * After gtk_dialog_run() returns, you are responsible for hiding or
850 * destroying the dialog if you wish to do so.
852 * Typical usage of this function might be:
853 * <informalexample><programlisting>
854 * <!> gint result = gtk_dialog_run (GTK_DIALOG (dialog));
855 * <!> switch (result)
857 * <!> case GTK_RESPONSE_ACCEPT:
858 * <!> do_application_specific_something (<!-- -->);
861 * <!> do_nothing_since_dialog_was_cancelled (<!-- -->);
864 * <!> gtk_widget_destroy (dialog);
865 * </programlisting></informalexample>
867 * Return value: response ID
870 gtk_dialog_run (GtkDialog *dialog)
872 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
874 guint response_handler;
876 guint destroy_handler;
877 guint delete_handler;
879 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
881 gtk_object_ref (GTK_OBJECT (dialog));
883 if (!GTK_WIDGET_VISIBLE (dialog))
884 gtk_widget_show (GTK_WIDGET (dialog));
886 was_modal = GTK_WINDOW (dialog)->modal;
888 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
891 gtk_signal_connect (GTK_OBJECT (dialog),
893 GTK_SIGNAL_FUNC (run_response_handler),
897 gtk_signal_connect (GTK_OBJECT (dialog),
899 GTK_SIGNAL_FUNC (run_unmap_handler),
903 gtk_signal_connect (GTK_OBJECT (dialog),
905 GTK_SIGNAL_FUNC (run_delete_handler),
909 gtk_signal_connect (GTK_OBJECT (dialog),
911 GTK_SIGNAL_FUNC (run_destroy_handler),
914 ri.loop = g_main_new (FALSE);
916 GDK_THREADS_LEAVE ();
917 g_main_loop_run (ri.loop);
918 GDK_THREADS_ENTER ();
920 g_main_loop_unref (ri.loop);
923 ri.destroyed = FALSE;
928 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
930 gtk_signal_disconnect (GTK_OBJECT (dialog), response_handler);
931 gtk_signal_disconnect (GTK_OBJECT (dialog), unmap_handler);
932 gtk_signal_disconnect (GTK_OBJECT (dialog), delete_handler);
933 gtk_signal_disconnect (GTK_OBJECT (dialog), destroy_handler);
936 gtk_object_unref (GTK_OBJECT (dialog));
938 return ri.response_id;