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 static void gtk_dialog_class_init (GtkDialogClass *klass);
39 static void gtk_dialog_init (GtkDialog *dialog);
41 static void gtk_dialog_add_buttons_valist (GtkDialog *dialog,
42 const gchar *first_button_text,
45 static gint gtk_dialog_delete_event_handler (GtkWidget *widget,
49 static void gtk_dialog_set_property (GObject *object,
53 static void gtk_dialog_get_property (GObject *object,
57 static void gtk_dialog_style_set (GtkWidget *widget,
58 GtkStyle *prev_style);
59 static void gtk_dialog_map (GtkWidget *widget);
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 GType dialog_type = 0;
84 static const GTypeInfo dialog_info =
86 sizeof (GtkDialogClass),
88 NULL, /* base_finalize */
89 (GClassInitFunc) gtk_dialog_class_init,
90 NULL, /* class_finalize */
91 NULL, /* class_data */
94 (GInstanceInitFunc) gtk_dialog_init,
97 dialog_type = g_type_register_static (GTK_TYPE_WINDOW, "GtkDialog",
105 gtk_dialog_class_init (GtkDialogClass *class)
107 GObjectClass *gobject_class;
108 GtkWidgetClass *widget_class;
109 GtkBindingSet *binding_set;
111 gobject_class = G_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->map = gtk_dialog_map;
120 widget_class->style_set = gtk_dialog_style_set;
122 class->close = gtk_dialog_close;
124 g_object_class_install_property (gobject_class,
126 g_param_spec_boolean ("has_separator",
128 _("The dialog has a separator bar above its buttons"),
132 dialog_signals[RESPONSE] =
133 g_signal_new ("response",
134 G_OBJECT_CLASS_TYPE (class),
136 G_STRUCT_OFFSET (GtkDialogClass, response),
138 _gtk_marshal_NONE__INT,
142 dialog_signals[CLOSE] =
143 g_signal_new ("close",
144 G_OBJECT_CLASS_TYPE (class),
145 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
146 G_STRUCT_OFFSET (GtkDialogClass, close),
148 _gtk_marshal_NONE__NONE,
151 gtk_widget_class_install_style_property (widget_class,
152 g_param_spec_int ("content_area_border",
153 _("Content area border"),
154 _("Width of border around the main dialog area"),
159 gtk_widget_class_install_style_property (widget_class,
160 g_param_spec_int ("button_spacing",
162 _("Spacing between buttons"),
168 gtk_widget_class_install_style_property (widget_class,
169 g_param_spec_int ("action_area_border",
170 _("Action area border"),
171 _("Width of border around the button area at the bottom of the dialog"),
177 binding_set = gtk_binding_set_by_class (class);
179 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
184 update_spacings (GtkDialog *dialog)
187 gint content_area_border;
189 gint action_area_border;
191 widget = GTK_WIDGET (dialog);
193 gtk_widget_style_get (widget,
194 "content_area_border",
195 &content_area_border,
198 "action_area_border",
202 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox),
203 content_area_border);
204 gtk_box_set_spacing (GTK_BOX (dialog->action_area),
206 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area),
211 gtk_dialog_init (GtkDialog *dialog)
213 /* To avoid breaking old code that prevents destroy on delete event
214 * by connecting a handler, we have to have the FIRST signal
215 * connection on the dialog.
217 g_signal_connect (dialog,
219 G_CALLBACK (gtk_dialog_delete_event_handler),
222 dialog->vbox = gtk_vbox_new (FALSE, 0);
224 gtk_container_add (GTK_CONTAINER (dialog), dialog->vbox);
225 gtk_widget_show (dialog->vbox);
227 dialog->action_area = gtk_hbutton_box_new ();
229 gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
232 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->action_area,
234 gtk_widget_show (dialog->action_area);
236 dialog->separator = gtk_hseparator_new ();
237 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
238 gtk_widget_show (dialog->separator);
240 gtk_window_set_type_hint (GTK_WINDOW (dialog),
241 GDK_WINDOW_TYPE_HINT_DIALOG);
242 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
247 gtk_dialog_set_property (GObject *object,
254 dialog = GTK_DIALOG (object);
258 case PROP_HAS_SEPARATOR:
259 gtk_dialog_set_has_separator (dialog, g_value_get_boolean (value));
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 gtk_dialog_get_property (GObject *object,
276 dialog = GTK_DIALOG (object);
280 case PROP_HAS_SEPARATOR:
281 g_value_set_boolean (value, dialog->separator != NULL);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 gtk_dialog_delete_event_handler (GtkWidget *widget,
295 /* emit response signal */
296 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
298 /* Do the destroy by default */
302 /* A far too tricky heuristic for getting the right initial
303 * focus widget if none was set. What we do is we focus the first
304 * widget in the tab chain, but if this results in the focus
305 * ending up on one of the response widgets _other_ than the
306 * default response, we focus the default response instead.
309 gtk_dialog_map (GtkWidget *widget)
311 GtkWindow *window = GTK_WINDOW (widget);
312 GtkDialog *dialog = GTK_DIALOG (widget);
314 GTK_WIDGET_CLASS (parent_class)->map (widget);
316 if (!window->focus_widget)
318 GList *children, *tmp_list;
320 g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
322 tmp_list = children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
326 GtkWidget *child = tmp_list->data;
328 if (child == window->focus_widget && child != window->default_widget && window->default_widget)
330 gtk_widget_grab_focus (window->default_widget);
334 tmp_list = tmp_list->next;
337 g_list_free (children);
342 gtk_dialog_style_set (GtkWidget *widget,
343 GtkStyle *prev_style)
345 update_spacings (GTK_DIALOG (widget));
349 gtk_dialog_close (GtkDialog *dialog)
351 /* Synthesize delete_event to close dialog. */
353 GdkEvent *event = gdk_event_new (GDK_DELETE);
356 widget = GTK_WIDGET (dialog);
358 event->any.window = g_object_ref (widget->window);
359 event->any.send_event = TRUE;
361 gtk_main_do_event (event);
362 gdk_event_free (event);
366 gtk_dialog_new (void)
368 return g_object_new (GTK_TYPE_DIALOG, NULL);
372 gtk_dialog_new_empty (const gchar *title,
374 GtkDialogFlags flags)
378 dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
381 gtk_window_set_title (GTK_WINDOW (dialog), title);
384 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
386 if (flags & GTK_DIALOG_MODAL)
387 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
389 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
390 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
392 if (flags & GTK_DIALOG_NO_SEPARATOR)
393 gtk_dialog_set_has_separator (dialog, FALSE);
395 return GTK_WIDGET (dialog);
399 * gtk_dialog_new_with_buttons:
400 * @title: Title of the dialog, or %NULL
401 * @parent: Transient parent of the dialog, or %NULL
402 * @flags: from #GtkDialogFlags
403 * @first_button_text: stock ID or text to go in first button, or %NULL
404 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
406 * Creates a new #GtkDialog with title @title (or %NULL for the default
407 * title; see gtk_window_set_title()) and transient parent @parent (or
408 * %NULL for none; see gtk_window_set_transient_for()). The @flags
409 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
410 * and/or to have it destroyed along with its transient parent
411 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
412 * text/response ID pairs should be listed, with a %NULL pointer ending
413 * the list. Button text can be either a stock ID such as
414 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
415 * any positive number, or one of the values in the #GtkResponseType
416 * enumeration. If the user clicks one of these dialog buttons,
417 * #GtkDialog will emit the "response" signal with the corresponding
418 * response ID. If a #GtkDialog receives the "delete_event" signal, it
419 * will emit "response" with a response ID of #GTK_RESPONSE_DELETE_EVENT.
420 * However, destroying a dialog does not emit the "response" signal;
421 * so be careful relying on "response" when using
422 * the #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
423 * so the first button in the list will be the leftmost button in the dialog.
425 * Here's a simple example:
426 * <informalexample><programlisting>
427 * GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
429 * GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
431 * GTK_RESPONSE_ACCEPT,
433 * GTK_RESPONSE_REJECT,
435 * </programlisting></informalexample>
437 * Return value: a new #GtkDialog
440 gtk_dialog_new_with_buttons (const gchar *title,
442 GtkDialogFlags flags,
443 const gchar *first_button_text,
449 dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));
451 va_start (args, first_button_text);
453 gtk_dialog_add_buttons_valist (dialog,
459 return GTK_WIDGET (dialog);
462 typedef struct _ResponseData ResponseData;
470 get_response_data (GtkWidget *widget)
472 ResponseData *ad = g_object_get_data (G_OBJECT (widget),
473 "gtk-dialog-response-data");
477 ad = g_new (ResponseData, 1);
479 g_object_set_data_full (G_OBJECT (widget),
480 "gtk-dialog-response-data",
489 action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
494 g_return_if_fail (GTK_IS_DIALOG (dialog));
496 response_id = GTK_RESPONSE_NONE;
498 ad = get_response_data (widget);
500 g_assert (ad != NULL);
502 response_id = ad->response_id;
504 gtk_dialog_response (dialog, response_id);
508 * gtk_dialog_add_action_widget:
509 * @dialog: a #GtkDialog
510 * @child: an activatable widget
511 * @response_id: response ID for @child
513 * Adds an activatable widget to the action area of a #GtkDialog,
514 * connecting a signal handler that will emit the "response" signal on
515 * the dialog when the widget is activated. The widget is appended to
516 * the end of the dialog's action area. If you want to add a
517 * non-activatable widget, simply pack it into the
518 * <literal>action_area</literal> field of the #GtkDialog struct.
521 gtk_dialog_add_action_widget (GtkDialog *dialog,
528 g_return_if_fail (GTK_IS_DIALOG (dialog));
529 g_return_if_fail (GTK_IS_WIDGET (child));
531 ad = get_response_data (child);
533 ad->response_id = response_id;
535 if (GTK_IS_BUTTON (child))
536 signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
538 signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal != 0;
544 closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
546 g_signal_connect_closure_by_id (child,
553 g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");
555 gtk_box_pack_end (GTK_BOX (dialog->action_area),
559 if (response_id == GTK_RESPONSE_HELP)
560 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area), child, TRUE);
564 * gtk_dialog_add_button:
565 * @dialog: a #GtkDialog
566 * @button_text: text of button, or stock ID
567 * @response_id: response ID for the button
569 * Adds a button with the given text (or a stock button, if @button_text is a
570 * stock ID) and sets things up so that clicking the button will emit the
571 * "response" signal with the given @response_id. The button is appended to the
572 * end of the dialog's action area. The button widget is returned, but usually
575 * Return value: the button widget that was added
578 gtk_dialog_add_button (GtkDialog *dialog,
579 const gchar *button_text,
584 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
585 g_return_val_if_fail (button_text != NULL, NULL);
587 button = gtk_button_new_from_stock (button_text);
589 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
591 gtk_widget_show (button);
593 gtk_dialog_add_action_widget (dialog,
601 gtk_dialog_add_buttons_valist(GtkDialog *dialog,
602 const gchar *first_button_text,
608 g_return_if_fail (GTK_IS_DIALOG (dialog));
610 if (first_button_text == NULL)
613 text = first_button_text;
614 response_id = va_arg (args, gint);
618 gtk_dialog_add_button (dialog, text, response_id);
620 text = va_arg (args, gchar*);
623 response_id = va_arg (args, int);
628 * gtk_dialog_add_buttons:
629 * @dialog: a #GtkDialog
630 * @first_button_text: button text or stock ID
631 * @Varargs: response ID for first button, then more text-response_id pairs
633 * Adds more buttons, same as calling gtk_dialog_add_button()
634 * repeatedly. The variable argument list should be %NULL-terminated
635 * as with gtk_dialog_new_with_buttons(). Each button must have both
636 * text and response ID.
639 gtk_dialog_add_buttons (GtkDialog *dialog,
640 const gchar *first_button_text,
645 va_start (args, first_button_text);
647 gtk_dialog_add_buttons_valist (dialog,
655 * gtk_dialog_set_response_sensitive:
656 * @dialog: a #GtkDialog
657 * @response_id: a response ID
658 * @setting: %TRUE for sensitive
660 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
661 * for each widget in the dialog's action area with the given @response_id.
662 * A convenient way to sensitize/desensitize dialog buttons.
665 gtk_dialog_set_response_sensitive (GtkDialog *dialog,
672 g_return_if_fail (GTK_IS_DIALOG (dialog));
674 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
677 while (tmp_list != NULL)
679 GtkWidget *widget = tmp_list->data;
680 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
681 "gtk-dialog-response-data");
683 if (rd && rd->response_id == response_id)
684 gtk_widget_set_sensitive (widget, setting);
686 tmp_list = g_list_next (tmp_list);
689 g_list_free (children);
693 * gtk_dialog_set_default_response:
694 * @dialog: a #GtkDialog
695 * @response_id: a response ID
697 * Sets the last widget in the dialog's action area with the given @response_id
698 * as the default widget for the dialog. Pressing "Enter" normally activates
699 * the default widget.
702 gtk_dialog_set_default_response (GtkDialog *dialog,
708 g_return_if_fail (GTK_IS_DIALOG (dialog));
710 children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
713 while (tmp_list != NULL)
715 GtkWidget *widget = tmp_list->data;
716 ResponseData *rd = g_object_get_data (G_OBJECT (widget),
717 "gtk-dialog-response-data");
719 if (rd && rd->response_id == response_id)
721 gtk_widget_grab_default (widget);
723 if (!GTK_WINDOW (dialog)->focus_widget)
724 gtk_widget_grab_focus (widget);
727 tmp_list = g_list_next (tmp_list);
730 g_list_free (children);
734 * gtk_dialog_set_has_separator:
735 * @dialog: a #GtkDialog
736 * @setting: %TRUE to have a separator
738 * Sets whether the dialog has a separator above the buttons.
742 gtk_dialog_set_has_separator (GtkDialog *dialog,
745 g_return_if_fail (GTK_IS_DIALOG (dialog));
747 /* this might fail if we get called before _init() somehow */
748 g_assert (dialog->vbox != NULL);
750 if (setting && dialog->separator == NULL)
752 dialog->separator = gtk_hseparator_new ();
753 gtk_box_pack_end (GTK_BOX (dialog->vbox), dialog->separator, FALSE, TRUE, 0);
755 /* The app programmer could screw this up, but, their own fault.
756 * Moves the separator just above the action area.
758 gtk_box_reorder_child (GTK_BOX (dialog->vbox), dialog->separator, 1);
759 gtk_widget_show (dialog->separator);
761 else if (!setting && dialog->separator != NULL)
763 gtk_widget_destroy (dialog->separator);
764 dialog->separator = NULL;
767 g_object_notify (G_OBJECT (dialog), "has_separator");
771 * gtk_dialog_get_has_separator:
772 * @dialog: a #GtkDialog
774 * Accessor for whether the dialog has a separator.
776 * Return value: %TRUE if the dialog has a separator
779 gtk_dialog_get_has_separator (GtkDialog *dialog)
781 g_return_val_if_fail (GTK_IS_DIALOG (dialog), FALSE);
783 return dialog->separator != NULL;
787 * gtk_dialog_response:
788 * @dialog: a #GtkDialog
789 * @response_id: response ID
791 * Emits the "response" signal with the given response ID. Used to
792 * indicate that the user has responded to the dialog in some way;
793 * typically either you or gtk_dialog_run() will be monitoring the
794 * "response" signal and take appropriate action.
797 gtk_dialog_response (GtkDialog *dialog,
800 g_return_if_fail (GTK_IS_DIALOG (dialog));
802 g_signal_emit (dialog,
803 dialog_signals[RESPONSE],
817 shutdown_loop (RunInfo *ri)
819 if (g_main_loop_is_running (ri->loop))
820 g_main_loop_quit (ri->loop);
824 run_unmap_handler (GtkDialog *dialog, gpointer data)
832 run_response_handler (GtkDialog *dialog,
840 ri->response_id = response_id;
846 run_delete_handler (GtkDialog *dialog,
854 return TRUE; /* Do not destroy */
858 run_destroy_handler (GtkDialog *dialog, gpointer data)
862 /* shutdown_loop will be called by run_unmap_handler */
864 ri->destroyed = TRUE;
869 * @dialog: a #GtkDialog
871 * Blocks in a recursive main loop until the @dialog either emits the
872 * response signal, or is destroyed. If the dialog is destroyed during the call
873 * to gtk_dialog_run(), gtk_dialog_returns #GTK_RESPONSE_NONE.
874 * Otherwise, it returns the response ID from the "response" signal emission.
875 * Before entering the recursive main loop, gtk_dialog_run() calls
876 * gtk_widget_show() on the dialog for you. Note that you still
877 * need to show any children of the dialog yourself.
879 * During gtk_dialog_run(), the default behavior of "delete_event" is
880 * disabled; if the dialog receives "delete_event", it will not be
881 * destroyed as windows usually are, and gtk_dialog_run() will return
882 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be
883 * modal. You can force gtk_dialog_run() to return at any time by
884 * calling gtk_dialog_response() to emit the "response"
885 * signal. Destroying the dialog during gtk_dialog_run() is a very bad
886 * idea, because your post-run code won't know whether the dialog was
889 * After gtk_dialog_run() returns, you are responsible for hiding or
890 * destroying the dialog if you wish to do so.
892 * Typical usage of this function might be:
893 * <informalexample><programlisting>
894 * gint result = gtk_dialog_run (GTK_DIALOG (dialog));
897 * case GTK_RESPONSE_ACCEPT:
898 * do_application_specific_something (<!-- -->);
901 * do_nothing_since_dialog_was_cancelled (<!-- -->);
904 * gtk_widget_destroy (dialog);
905 * </programlisting></informalexample>
907 * Return value: response ID
910 gtk_dialog_run (GtkDialog *dialog)
912 RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL };
914 gulong response_handler;
915 gulong unmap_handler;
916 gulong destroy_handler;
917 gulong delete_handler;
919 g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);
921 g_object_ref (dialog);
923 if (!GTK_WIDGET_VISIBLE (dialog))
924 gtk_widget_show (GTK_WIDGET (dialog));
926 was_modal = GTK_WINDOW (dialog)->modal;
928 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
931 g_signal_connect (dialog,
933 G_CALLBACK (run_response_handler),
937 g_signal_connect (dialog,
939 G_CALLBACK (run_unmap_handler),
943 g_signal_connect (dialog,
945 G_CALLBACK (run_delete_handler),
949 g_signal_connect (dialog,
951 G_CALLBACK (run_destroy_handler),
954 ri.loop = g_main_loop_new (NULL, FALSE);
956 GDK_THREADS_LEAVE ();
957 g_main_loop_run (ri.loop);
958 GDK_THREADS_ENTER ();
960 g_main_loop_unref (ri.loop);
963 ri.destroyed = FALSE;
968 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
970 g_signal_handler_disconnect (dialog, response_handler);
971 g_signal_handler_disconnect (dialog, unmap_handler);
972 g_signal_handler_disconnect (dialog, delete_handler);
973 g_signal_handler_disconnect (dialog, destroy_handler);
976 g_object_unref (dialog);
978 return ri.response_id;