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 "gdkkeysyms.h"
36 #include "gtkbindings.h"
38 typedef struct _ResponseData ResponseData;
45 static void gtk_dialog_class_init (GtkDialogClass *klass);
46 static void gtk_dialog_init (GtkDialog *dialog);
48 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
49 const gchar *first_button_text,
52 static gint gtk_dialog_delete_event_handler (GtkWidget *widget,
56 static void gtk_dialog_set_property (GObject *object,
60 static void gtk_dialog_get_property (GObject *object,
64 static void gtk_dialog_style_set (GtkWidget *widget,
65 GtkStyle *prev_style);
66 static void gtk_dialog_map (GtkWidget *widget);
68 static void gtk_dialog_close (GtkDialog *dialog);
70 static ResponseData* get_response_data (GtkWidget *widget);
83 static gpointer parent_class;
84 static guint dialog_signals[LAST_SIGNAL];
87 gtk_dialog_get_type (void)
89 static GType dialog_type = 0;
93 static const GTypeInfo dialog_info =
95 sizeof (GtkDialogClass),
97 NULL, /* base_finalize */
98 (GClassInitFunc) gtk_dialog_class_init,
99 NULL, /* class_finalize */
100 NULL, /* class_data */
103 (GInstanceInitFunc) gtk_dialog_init,
106 dialog_type = g_type_register_static (GTK_TYPE_WINDOW, "GtkDialog",
114 gtk_dialog_class_init (GtkDialogClass *class)
116 GObjectClass *gobject_class;
117 GtkWidgetClass *widget_class;
118 GtkBindingSet *binding_set;
120 gobject_class = G_OBJECT_CLASS (class);
121 widget_class = GTK_WIDGET_CLASS (class);
123 parent_class = g_type_class_peek_parent (class);
125 gobject_class->set_property = gtk_dialog_set_property;
126 gobject_class->get_property = gtk_dialog_get_property;
128 widget_class->map = gtk_dialog_map;
129 widget_class->style_set = gtk_dialog_style_set;
131 class->close = gtk_dialog_close;
133 g_object_class_install_property (gobject_class,
135 g_param_spec_boolean ("has_separator",
137 _("The dialog has a separator bar above its buttons"),
141 dialog_signals[RESPONSE] =
142 g_signal_new ("response",
143 G_OBJECT_CLASS_TYPE (class),
145 G_STRUCT_OFFSET (GtkDialogClass, response),
147 _gtk_marshal_NONE__INT,
151 dialog_signals[CLOSE] =
152 g_signal_new ("close",
153 G_OBJECT_CLASS_TYPE (class),
154 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
155 G_STRUCT_OFFSET (GtkDialogClass, close),
157 _gtk_marshal_NONE__NONE,
160 gtk_widget_class_install_style_property (widget_class,
161 g_param_spec_int ("content_area_border",
162 _("Content area border"),
163 _("Width of border around the main dialog area"),
168 gtk_widget_class_install_style_property (widget_class,
169 g_param_spec_int ("button_spacing",
171 _("Spacing between buttons"),
177 gtk_widget_class_install_style_property (widget_class,
178 g_param_spec_int ("action_area_border",
179 _("Action area border"),
180 _("Width of border around the button area at the bottom of the dialog"),
186 binding_set = gtk_binding_set_by_class (class);
188 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
193 update_spacings (GtkDialog *dialog)
196 gint content_area_border;
198 gint action_area_border;
200 widget = GTK_WIDGET (dialog);
202 gtk_widget_style_get (widget,
203 "content_area_border",
204 &content_area_border,
207 "action_area_border",
211 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
212 content_area_border);
213 gtk_box_set_spacing (GTK_BOX (dialog->action_area),
215 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
220 gtk_dialog_init (GtkDialog *dialog)
222 /* To avoid breaking old code that prevents destroy on delete event
223 * by connecting a handler, we have to have the FIRST signal
224 * connection on the dialog.
226 g_signal_connect (dialog,
228 G_CALLBACK (gtk_dialog_delete_event_handler),
231 dialog->vbox = gtk_vbox_new (FALSE, 0);
233 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
234 gtk_widget_show (dialog->vbox);
236 dialog->action_area = gtk_hbutton_box_new ();
238 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
241 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
243 gtk_widget_show (dialog->action_area);
245 dialog->separator = gtk_hseparator_new ();
246 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
247 gtk_widget_show (dialog->separator);
249 gtk_window_set_type_hint (GTK_WINDOW (dialog),
250 GDK_WINDOW_TYPE_HINT_DIALOG);
251 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
256 gtk_dialog_set_property (GObject *object,
263 dialog = GTK_DIALOG (object);
267 case PROP_HAS_SEPARATOR:
268 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gtk_dialog_get_property (GObject *object,
285 dialog = GTK_DIALOG (object);
289 case PROP_HAS_SEPARATOR:
290 g_value_set_boolean (value, dialog->separator != NULL);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 gtk_dialog_delete_event_handler (GtkWidget *widget,
304 /* emit response signal */
305 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
307 /* Do the destroy by default */
311 /* A far too tricky heuristic for getting the right initial
312 * focus widget if none was set. What we do is we focus the first
313 * widget in the tab chain, but if this results in the focus
314 * ending up on one of the response widgets _other_ than the
315 * default response, we focus the default response instead.
318 gtk_dialog_map (GtkWidget *widget)
320 GtkWindow *window = GTK_WINDOW (widget);
321 GtkDialog *dialog = GTK_DIALOG (widget);
323 GTK_WIDGET_CLASS (parent_class)->map (widget);
325 if (!window->focus_widget)
327 GList *children, *tmp_list;
329 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
331 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
335 GtkWidget *child = tmp_list->data;
337 if (child == window->focus_widget && child != window->default_widget && window->default_widget)
339 gtk_widget_grab_focus (window->default_widget);
343 tmp_list = tmp_list->next;
346 g_list_free (children);
351 gtk_dialog_style_set (GtkWidget *widget,
352 GtkStyle *prev_style)
354 update_spacings (GTK_DIALOG (widget));
358 dialog_has_cancel (GtkDialog *dialog)
360 GList *children, *tmp_list;
361 gboolean ret = FALSE;
363 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
365 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
367 ResponseData *rd = get_response_data (tmp_list->data);
369 if (rd && rd->response_id == GTK_RESPONSE_CANCEL)
376 g_list_free (children);
382 gtk_dialog_close (GtkDialog *dialog)
384 /* Synthesize delete_event to close dialog. */
386 GtkWidget *widget = GTK_WIDGET (dialog);
389 if (!dialog_has_cancel (dialog))
392 event = gdk_event_new (GDK_DELETE);
394 event->any.window = g_object_ref (widget->window);
395 event->any.send_event = TRUE;
397 gtk_main_do_event (event);
398 gdk_event_free (event);
402 gtk_dialog_new (void)
404 return g_object_new (GTK_TYPE_DIALOG, NULL);
408 gtk_dialog_new_empty (const gchar *title,
410 GtkDialogFlags flags)
414 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
417 gtk_window_set_title (GTK_WINDOW (dialog), title);
420 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
422 if (flags & GTK_DIALOG_MODAL)
423 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
425 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
426 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
428 if (flags & GTK_DIALOG_NO_SEPARATOR)
429 gtk_dialog_set_has_separator (dialog, FALSE);
431 return GTK_WIDGET (dialog);
435 * gtk_dialog_new_with_buttons:
436 * @title: Title of the dialog, or %NULL
437 * @parent: Transient parent of the dialog, or %NULL
438 * @flags: from #GtkDialogFlags
439 * @first_button_text: stock ID or text to go in first button, or %NULL
440 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
442 * Creates a new #GtkDialog with title @title (or %NULL for the default
443 * title; see gtk_window_set_title()) and transient parent @parent (or
444 * %NULL for none; see gtk_window_set_transient_for()). The @flags
445 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
446 * and/or to have it destroyed along with its transient parent
447 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
448 * text/response ID pairs should be listed, with a %NULL pointer ending
449 * the list. Button text can be either a stock ID such as
450 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
451 * any positive number, or one of the values in the #GtkResponseType
452 * enumeration. If the user clicks one of these dialog buttons,
453 * #GtkDialog will emit the "response" signal with the corresponding
454 * response ID. If a #GtkDialog receives the "delete_event" signal, it
455 * will emit "response" with a response ID of #GTK_RESPONSE_DELETE_EVENT.
456 * However, destroying a dialog does not emit the "response" signal;
457 * so be careful relying on "response" when using
458 * the #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
459 * so the first button in the list will be the leftmost button in the dialog.
461 * Here's a simple example:
462 * <informalexample><programlisting>
463 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
465 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
467 * GTK_RESPONSE_ACCEPT,
469 * GTK_RESPONSE_REJECT,
471 * </programlisting></informalexample>
473 * Return value: a new #GtkDialog
476 gtk_dialog_new_with_buttons (const gchar *title,
478 GtkDialogFlags flags,
479 const gchar *first_button_text,
485 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
487 va_start (args, first_button_text);
489 gtk_dialog_add_buttons_valist (dialog,
495 return GTK_WIDGET (dialog);
499 get_response_data (GtkWidget *widget)
501 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
502 "gtk-dialog-response-data");
506 ad = g_new (ResponseData, 1);
508 g_object_set_data_full (G_OBJECT (widget),
509 "gtk-dialog-response-data",
518 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
523 g_return_if_fail (GTK_IS_DIALOG (dialog));
525 response_id = GTK_RESPONSE_NONE;
527 ad = get_response_data (widget);
529 g_assert (ad != NULL);
531 response_id = ad->response_id;
533 gtk_dialog_response (dialog, response_id);
537 * gtk_dialog_add_action_widget:
538 * @dialog: a #GtkDialog
539 * @child: an activatable widget
540 * @response_id: response ID for @child
542 * Adds an activatable widget to the action area of a #GtkDialog,
543 * connecting a signal handler that will emit the "response" signal on
544 * the dialog when the widget is activated. The widget is appended to
545 * the end of the dialog's action area. If you want to add a
546 * non-activatable widget, simply pack it into the
547 * <literal>action_area</literal> field of the #GtkDialog struct.
550 gtk_dialog_add_action_widget (GtkDialog *dialog,
557 g_return_if_fail (GTK_IS_DIALOG (dialog));
558 g_return_if_fail (GTK_IS_WIDGET (child));
560 ad = get_response_data (child);
562 ad->response_id = response_id;
564 if (GTK_IS_BUTTON (child))
565 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
567 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal != 0;
573 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
575 g_signal_connect_closure_by_id (child,
582 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
584 gtk_box_pack_end (GTK_BOX (dialog->action_area),
588 if (response_id == GTK_RESPONSE_HELP)
589 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
593 * gtk_dialog_add_button:
594 * @dialog: a #GtkDialog
595 * @button_text: text of button, or stock ID
596 * @response_id: response ID for the button
598 * Adds a button with the given text (or a stock button, if @button_text is a
599 * stock ID) and sets things up so that clicking the button will emit the
600 * "response" signal with the given @response_id. The button is appended to the
601 * end of the dialog's action area. The button widget is returned, but usually
604 * Return value: the button widget that was added
607 gtk_dialog_add_button (GtkDialog *dialog,
608 const gchar *button_text,
613 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
614 g_return_val_if_fail (button_text != NULL, NULL);
616 button = gtk_button_new_from_stock (button_text);
618 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
620 gtk_widget_show (button);
622 gtk_dialog_add_action_widget (dialog,
630 gtk_dialog_add_buttons_valist(GtkDialog *dialog,
631 const gchar *first_button_text,
637 g_return_if_fail (GTK_IS_DIALOG (dialog));
639 if (first_button_text == NULL)
642 text = first_button_text;
643 response_id = va_arg (args, gint);
647 gtk_dialog_add_button (dialog, text, response_id);
649 text = va_arg (args, gchar*);
652 response_id = va_arg (args, int);
657 * gtk_dialog_add_buttons:
658 * @dialog: a #GtkDialog
659 * @first_button_text: button text or stock ID
660 * @Varargs: response ID for first button, then more text-response_id pairs
662 * Adds more buttons, same as calling gtk_dialog_add_button()
663 * repeatedly. The variable argument list should be %NULL-terminated
664 * as with gtk_dialog_new_with_buttons(). Each button must have both
665 * text and response ID.
668 gtk_dialog_add_buttons (GtkDialog *dialog,
669 const gchar *first_button_text,
674 va_start (args, first_button_text);
676 gtk_dialog_add_buttons_valist (dialog,
684 * gtk_dialog_set_response_sensitive:
685 * @dialog: a #GtkDialog
686 * @response_id: a response ID
687 * @setting: %TRUE for sensitive
689 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
690 * for each widget in the dialog's action area with the given @response_id.
691 * A convenient way to sensitize/desensitize dialog buttons.
694 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
701 g_return_if_fail (GTK_IS_DIALOG (dialog));
703 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
706 while (tmp_list != NULL)
708 GtkWidget *widget = tmp_list->data;
709 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
710 "gtk-dialog-response-data");
712 if (rd && rd->response_id == response_id)
713 gtk_widget_set_sensitive (widget, setting);
715 tmp_list = g_list_next (tmp_list);
718 g_list_free (children);
722 * gtk_dialog_set_default_response:
723 * @dialog: a #GtkDialog
724 * @response_id: a response ID
726 * Sets the last widget in the dialog's action area with the given @response_id
727 * as the default widget for the dialog. Pressing "Enter" normally activates
728 * the default widget.
731 gtk_dialog_set_default_response (GtkDialog *dialog,
737 g_return_if_fail (GTK_IS_DIALOG (dialog));
739 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
742 while (tmp_list != NULL)
744 GtkWidget *widget = tmp_list->data;
745 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
746 "gtk-dialog-response-data");
748 if (rd && rd->response_id == response_id)
749 gtk_widget_grab_default (widget);
751 tmp_list = g_list_next (tmp_list);
754 g_list_free (children);
758 * gtk_dialog_set_has_separator:
759 * @dialog: a #GtkDialog
760 * @setting: %TRUE to have a separator
762 * Sets whether the dialog has a separator above the buttons.
766 gtk_dialog_set_has_separator (GtkDialog *dialog,
769 g_return_if_fail (GTK_IS_DIALOG (dialog));
771 /* this might fail if we get called before _init() somehow */
772 g_assert (dialog->vbox != NULL);
774 if (setting && dialog->separator == NULL)
776 dialog->separator = gtk_hseparator_new ();
777 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
779 /* The app programmer could screw this up, but, their own fault.
780 * Moves the separator just above the action area.
782 gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
783 gtk_widget_show (dialog->separator);
785 else if (!setting && dialog->separator != NULL)
787 gtk_widget_destroy (dialog->separator);
788 dialog->separator = NULL;
791 g_object_notify (G_OBJECT (dialog), "has_separator");
795 * gtk_dialog_get_has_separator:
796 * @dialog: a #GtkDialog
798 * Accessor for whether the dialog has a separator.
800 * Return value: %TRUE if the dialog has a separator
803 gtk_dialog_get_has_separator (GtkDialog *dialog)
805 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
807 return dialog->separator != NULL;
811 * gtk_dialog_response:
812 * @dialog: a #GtkDialog
813 * @response_id: response ID
815 * Emits the "response" signal with the given response ID. Used to
816 * indicate that the user has responded to the dialog in some way;
817 * typically either you or gtk_dialog_run() will be monitoring the
818 * "response" signal and take appropriate action.
821 gtk_dialog_response (GtkDialog *dialog,
824 g_return_if_fail (GTK_IS_DIALOG (dialog));
826 g_signal_emit (dialog,
827 dialog_signals[RESPONSE],
841 shutdown_loop (RunInfo *ri)
843 if (g_main_loop_is_running (ri->loop))
844 g_main_loop_quit (ri->loop);
848 run_unmap_handler (GtkDialog *dialog, gpointer data)
856 run_response_handler (GtkDialog *dialog,
864 ri->response_id = response_id;
870 run_delete_handler (GtkDialog *dialog,
878 return TRUE; /* Do not destroy */
882 run_destroy_handler (GtkDialog *dialog, gpointer data)
886 /* shutdown_loop will be called by run_unmap_handler */
888 ri->destroyed = TRUE;
893 * @dialog: a #GtkDialog
895 * Blocks in a recursive main loop until the @dialog either emits the
896 * response signal, or is destroyed. If the dialog is destroyed during the call
897 * to gtk_dialog_run(), gtk_dialog_returns #GTK_RESPONSE_NONE.
898 * Otherwise, it returns the response ID from the "response" signal emission.
899 * Before entering the recursive main loop, gtk_dialog_run() calls
900 * gtk_widget_show() on the dialog for you. Note that you still
901 * need to show any children of the dialog yourself.
903 * During gtk_dialog_run(), the default behavior of "delete_event" is
904 * disabled; if the dialog receives "delete_event", it will not be
905 * destroyed as windows usually are, and gtk_dialog_run() will return
906 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be
907 * modal. You can force gtk_dialog_run() to return at any time by
908 * calling gtk_dialog_response() to emit the "response"
909 * signal. Destroying the dialog during gtk_dialog_run() is a very bad
910 * idea, because your post-run code won't know whether the dialog was
913 * After gtk_dialog_run() returns, you are responsible for hiding or
914 * destroying the dialog if you wish to do so.
916 * Typical usage of this function might be:
917 * <informalexample><programlisting>
918 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
921 * case GTK_RESPONSE_ACCEPT:
922 * do_application_specific_something (<!-- -->);
925 * do_nothing_since_dialog_was_cancelled (<!-- -->);
928 * gtk_widget_destroy (dialog);
929 * </programlisting></informalexample>
931 * Return value: response ID
934 gtk_dialog_run (GtkDialog *dialog)
936 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
938 gulong response_handler;
939 gulong unmap_handler;
940 gulong destroy_handler;
941 gulong delete_handler;
943 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
945 g_object_ref (dialog);
947 if (!GTK_WIDGET_VISIBLE (dialog))
948 gtk_widget_show (GTK_WIDGET (dialog));
950 was_modal = GTK_WINDOW (dialog)->modal;
952 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
955 g_signal_connect (dialog,
957 G_CALLBACK (run_response_handler),
961 g_signal_connect (dialog,
963 G_CALLBACK (run_unmap_handler),
967 g_signal_connect (dialog,
969 G_CALLBACK (run_delete_handler),
973 g_signal_connect (dialog,
975 G_CALLBACK (run_destroy_handler),
978 ri.loop = g_main_loop_new (NULL, FALSE);
980 GDK_THREADS_LEAVE ();
981 g_main_loop_run (ri.loop);
982 GDK_THREADS_ENTER ();
984 g_main_loop_unref (ri.loop);
987 ri.destroyed = FALSE;
992 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
994 g_signal_handler_disconnect (dialog, response_handler);
995 g_signal_handler_disconnect (dialog, unmap_handler);
996 g_signal_handler_disconnect (dialog, delete_handler);
997 g_signal_handler_disconnect (dialog, destroy_handler);
1000 g_object_unref (dialog);
1002 return ri.response_id;