GTK+ Reference Manual
<<< Previous Page | Home | Up | Next Page >>>

# GtkDialog

## Name

GtkDialog -- create popup windows.

## Synopsis

    #include <gtk/gtk.h>

    struct      GtkDialog;
    enum        GtkDialogFlags;
    enum        GtkResponseType;
    GtkWidget*  gtk_dialog_new                  (void);
    GtkWidget*  gtk_dialog_new_with_buttons     (const gchar *title,
                                                 GtkWindow *parent,
                                                 GtkDialogFlags flags,
                                                 const gchar *first_button_text,
                                                 ...);
    gint        gtk_dialog_run                  (GtkDialog *dialog);
    void        gtk_dialog_response             (GtkDialog *dialog,
                                                 gint response_id);
    GtkWidget*  gtk_dialog_add_button           (GtkDialog *dialog,
                                                 const gchar *button_text,
                                                 gint response_id);
    void        gtk_dialog_add_buttons          (GtkDialog *dialog,
                                                 const gchar *first_button_text,
                                                 ...);
    void        gtk_dialog_add_action_widget    (GtkDialog *dialog,
                                                 GtkWidget *child,
                                                 gint response_id);
    gboolean    gtk_dialog_get_has_separator    (GtkDialog *dialog);
    void        gtk_dialog_set_default_response (GtkDialog *dialog,
                                                 gint response_id);
    void        gtk_dialog_set_has_separator    (GtkDialog *dialog,
                                                 gboolean setting);
    void        gtk_dialog_set_response_sensitive
                                                (GtkDialog *dialog,
                                                 gint response_id,
                                                 gboolean setting);

## Object Hierarchy

      GObject
       +----GtkObject
             +----GtkWidget
                   +----GtkContainer
                         +----GtkBin
                               +----GtkWindow
                                     +----GtkDialog

## Properties

      "has-separator"        gboolean             : Read / Write

## Style Properties

      "button-spacing"       gint                 : Read
      "action-area-border"   gint                 : Read
      "content-area-border"  gint                 : Read

## Signal Prototypes

    "close"     void        user_function      (GtkDialog *dialog,
                                                gpointer user_data);
    "response"  void        user_function      (GtkDialog *dialog,
                                                gint arg1,
                                                gpointer user_data);

## Description

Dialog boxes are a convenient way to prompt the user for a small amount of input, eg. to display a message, ask a question, or anything else that does not require extensive effort on the user's part.

GTK+ treats a dialog as a window split vertically. The top section is a GtkVBox, and is where widgets such as a GtkLabel or a GtkEntry should be packed. The bottom area is known as the `action_area`. This is generally used for packing buttons into the dialog which may perform functions such as cancel, ok, or apply. The two areas are separated by a GtkHSeparator.

GtkDialog boxes are created with a call to gtk_dialog_new() or gtk_dialog_new_with_buttons(). gtk_dialog_new_with_buttons() is recommended; it allows you to set the dialog title, some convenient flags, and add simple buttons.

If 'dialog' is a newly created dialog, the two primary areas of the window can be accessed as `GTK_DIALOG(dialog)->vbox` and `GTK_DIALOG(dialog)->action_area`, as can be seen from the example, below.

A 'modal' dialog (that is, one which freezes the rest of the application from user input), can be created by calling gtk_window_set_modal() on the dialog. Use the GTK_WINDOW() macro to cast the widget returned from gtk_dialog_new() into a GtkWindow. When using gtk_dialog_new_with_buttons() you can also pass the GTK_DIALOG_MODAL flag to make a dialog modal.

If you add buttons to GtkDialog using gtk_dialog_new_with_buttons(), gtk_dialog_add_button(), gtk_dialog_add_buttons(), or gtk_dialog_add_action_widget(), clicking the button will emit a signal called "response" with a response ID that you specified. GTK+ will never assign a meaning to positive response IDs; these are entirely user-defined. But for convenience, you can use the response IDs in the GtkResponseType enumeration (these all have values less than zero). If a dialog receives a delete event, the "response" signal will be emitted with a response ID of GTK_RESPONSE_NONE.

If you want to block waiting for a dialog to return before returning control flow to your code, you can call gtk_dialog_run(). This function enters a recursive main loop and waits for the user to respond to the dialog, returning the response ID corresponding to the button the user clicked.

For the simple dialog in the following example, in reality you'd probably use GtkMessageDialog to save yourself some effort. But you'd need to create the dialog contents manually if you had more than a simple message in the dialog.

Example 1. Simple GtkDialog usage.

    /* Function to open a dialog box displaying the message provided. */

    void quick_message (gchar *message) {

       GtkWidget *dialog, *label;

       /* Create the widgets */

       dialog = gtk_dialog_new_with_buttons ("Message",
                                             main_application_window,
                                             GTK_DIALOG_DESTROY_WITH_PARENT,
                                             GTK_STOCK_OK,
                                             GTK_RESPONSE_NONE,
                                             NULL);
       label = gtk_label_new (message);

       /* Ensure that the dialog box is destroyed when the user responds. */

       g_signal_connect_swapped (GTK_OBJECT (dialog),
                                 "response",
                                 G_CALLBACK (gtk_widget_destroy),
                                 GTK_OBJECT (dialog));

       /* Add the label, and show everything we've added to the dialog. */

       gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
                          label);
       gtk_widget_show_all (dialog);
    }

## Details

### struct GtkDialog

    struct GtkDialog;

`window` is a GtkWindow, but should not be modified directly, (use the functions provided, such as gtk_window_set_title(). See the GtkWindow section for more).

`vbox` is a GtkVBox - the main part of the dialog box.

`action_area` is a GtkHBox packed below the dividing GtkHSeparator in the dialog. It is treated exactly the same as any other GtkHBox.

### enum GtkDialogFlags

    typedef enum
    {
      GTK_DIALOG_MODAL               = 1 << 0, /* call gtk_window_set_modal (win, TRUE) */
      GTK_DIALOG_DESTROY_WITH_PARENT = 1 << 1, /* call gtk_window_set_destroy_with_parent () */
      GTK_DIALOG_NO_SEPARATOR        = 1 << 2  /* no separator bar above buttons */
    } GtkDialogFlags;

### enum GtkResponseType

    typedef enum
    {
      /* GTK returns this if a response widget has no response_id,
       * or if the dialog gets programmatically hidden or destroyed.
       */
      GTK_RESPONSE_NONE = -1,

      /* GTK won't return these unless you pass them in
       * as the response for an action widget. They are
       * for your convenience.
       */
      GTK_RESPONSE_REJECT = -2,
      GTK_RESPONSE_ACCEPT = -3,

      /* If the dialog is deleted. */
      GTK_RESPONSE_DELETE_EVENT = -4,

      /* These are returned from GTK dialogs, and you can also use them
       * yourself if you like.
       */
      GTK_RESPONSE_OK     = -5,
      GTK_RESPONSE_CANCEL = -6,
      GTK_RESPONSE_CLOSE  = -7,
      GTK_RESPONSE_YES    = -8,
      GTK_RESPONSE_NO     = -9,
      GTK_RESPONSE_APPLY  = -10,
      GTK_RESPONSE_HELP   = -11
    } GtkResponseType;

### gtk_dialog_new ()

    GtkWidget*  gtk_dialog_new                  (void);

Creates a new dialog box. Widgets should not be packed into this GtkWindow directly, but into the `vbox` and `action_area`, as described above.

Returns : | a new GtkDialog.

### gtk_dialog_new_with_buttons ()

    GtkWidget*  gtk_dialog_new_with_buttons     (const gchar *title,
                                                 GtkWindow *parent,
                                                 GtkDialogFlags flags,
                                                 const gchar *first_button_text,
                                                 ...);

Creates a new GtkDialog with title `title` (or `NULL` for the default title; see gtk_window_set_title()) and transient parent `parent` (or `NULL` for none; see gtk_window_set_transient_for()). The `flags` argument can be used to make the dialog modal (GTK_DIALOG_MODAL) and/or to have it destroyed along with its transient parent (GTK_DIALOG_DESTROY_WITH_PARENT). After `flags`, button text/response ID pairs should be listed, with a `NULL` pointer ending the list. Button text can be either a stock ID such as GTK_STOCK_OK, or some arbitrary text. A response ID can be any positive number, or one of the values in the GtkResponseType enumeration. If the user clicks one of these dialog buttons, GtkDialog will emit the "response" signal with the corresponding response ID. If a GtkDialog receives the "delete_event" signal, it will emit "response" with a response ID of GTK_RESPONSE_DELETE_EVENT. However, destroying a dialog does not emit the "response" signal; so be careful relying on "response" when using the GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right, so the first button in the list will be the leftmost button in the dialog.

Here's a simple example:

     GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
                                                      main_app_window,
                                                      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                                      GTK_STOCK_OK,
                                                      GTK_RESPONSE_ACCEPT,
                                                      GTK_STOCK_CANCEL,
                                                      GTK_RESPONSE_REJECT,
                                                      NULL);

`title` : | Title of the dialog, or `NULL`
`parent` : | Transient parent of the dialog, or `NULL`
`flags` : | from GtkDialogFlags
`first_button_text` : | stock ID or text to go in first button, or `NULL`
`...` : | response ID for first button, then additional buttons, ending with `NULL`
Returns : | a new GtkDialog

### gtk_dialog_run ()

    gint        gtk_dialog_run                  (GtkDialog *dialog);

Blocks in a recursive main loop until the `dialog` either emits the response signal, or is destroyed. If the dialog is destroyed, gtk_dialog_run() returns GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the "response" signal emission. Before entering the recursive main loop, gtk_dialog_run() calls gtk_widget_show() on the dialog for you. Note that you still need to show any children of the dialog yourself.

During gtk_dialog_run(), the default behavior of "delete_event" is disabled; if the dialog receives "delete_event", it will not be destroyed as windows usually are, and gtk_dialog_run() will return GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog will be modal. You can force gtk_dialog_run() to return at any time by calling gtk_dialog_response() to emit the "response" signal. Destroying the dialog during gtk_dialog_run() is a very bad idea, because your post-run code won't know whether the dialog was destroyed or not.

After gtk_dialog_run() returns, you are responsible for hiding or destroying the dialog if you wish to do so.

Typical usage of this function might be:

      gint result = gtk_dialog_run (GTK_DIALOG (dialog));
      switch (result)
        {
          case GTK_RESPONSE_ACCEPT:
             do_application_specific_something ();
             break;
          default:
             do_nothing_since_dialog_was_cancelled ();
             break;
        }
      gtk_widget_destroy (dialog);

`dialog` : | a GtkDialog
Returns : | response ID

### gtk_dialog_response ()

    void        gtk_dialog_response             (GtkDialog *dialog,
                                                 gint response_id);

Emits the "response" signal with the given response ID. Used to indicate that the user has responded to the dialog in some way; typically either you or gtk_dialog_run() will be monitoring the "response" signal and take appropriate action.

`dialog` : | a GtkDialog
`response_id` : | response ID

### gtk_dialog_add_button ()

    GtkWidget*  gtk_dialog_add_button           (GtkDialog *dialog,
                                                 const gchar *button_text,
                                                 gint response_id);

Adds a button with the given text (or a stock button, if `button_text` is a stock ID) and sets things up so that clicking the button will emit the "response" signal with the given `response_id`. The button is appended to the end of the dialog's action area. The button widget is returned, but usually you don't need it.

`dialog` : | a GtkDialog
`button_text` : | text of button, or stock ID
`response_id` : | response ID for the button
Returns : | the button widget that was added

### gtk_dialog_add_buttons ()

    void        gtk_dialog_add_buttons          (GtkDialog *dialog,
                                                 const gchar *first_button_text,
                                                 ...);

Adds more buttons, same as calling gtk_dialog_add_button() repeatedly. The variable argument list should be `NULL`-terminated as with gtk_dialog_new_with_buttons(). Each button must have both text and response ID.

`dialog` : | a GtkDialog
`first_button_text` : | button text or stock ID
`...` : | response ID for first button, then more text-response_id pairs

### gtk_dialog_add_action_widget ()

    void        gtk_dialog_add_action_widget    (GtkDialog *dialog,
                                                 GtkWidget *child,
                                                 gint response_id);

Adds an activatable widget to the action area of a GtkDialog, connecting a signal handler that will emit the "response" signal on the dialog when the widget is activated. The widget is appended to the end of the dialog's action area. If you want to add a non-activatable widget, simply pack it into the `action_area` field of the GtkDialog struct.

`dialog` : | a GtkDialog
`child` : | an activatable widget
`response_id` : | response ID for `child`

### gtk_dialog_get_has_separator ()

    gboolean    gtk_dialog_get_has_separator    (GtkDialog *dialog);

Accessor for whether the dialog has a separator.

`dialog` : | a GtkDialog
Returns : | `TRUE` if the dialog has a separator

### gtk_dialog_set_default_response ()

    void        gtk_dialog_set_default_response (GtkDialog *dialog,
                                                 gint response_id);

Sets the last widget in the dialog's action area with the given `response_id` as the default widget for the dialog. Pressing "Enter" normally activates the default widget.

`dialog` : | a GtkDialog
`response_id` : | a response ID

### gtk_dialog_set_has_separator ()

    void        gtk_dialog_set_has_separator    (GtkDialog *dialog,
                                                 gboolean setting);

Sets whether the dialog has a separator above the buttons. `TRUE` by default.

`dialog` : | a GtkDialog
`setting` : | `TRUE` to have a separator

### gtk_dialog_set_response_sensitive ()

    void        gtk_dialog_set_response_sensitive
                                                (GtkDialog *dialog,
                                                 gint response_id,
                                                 gboolean setting);

Calls `gtk_widget_set_sensitive (widget, `setting`)` for each widget in the dialog's action area with the given `response_id`. A convenient way to sensitize/desensitize dialog buttons.

`dialog` : | a GtkDialog
`response_id` : | a response ID
`setting` : | `TRUE` for sensitive

## Properties

"`has-separator`" (gboolean : Read / Write)

  The dialog has a separator bar above its buttons

## Style Properties

"`button-spacing`" (gint : Read)

  Spacing between buttons

"`action-area-border`" (gint : Read)

  Width of border around the button area at the bottom of the dialog

"`content-area-border`" (gint : Read)

  Width of border around the main dialog area

## Signals

### The "close" signal

    void        user_function                  (GtkDialog *dialog,
                                                gpointer user_data);

`dialog` : | the object which received the signal.
`user_data` : | user data set when the signal handler was connected.

### The "response" signal

    void        user_function                  (GtkDialog *dialog,
                                                gint arg1,
                                                gpointer user_data);

Emitted when an action widget is clicked, the dialog receives a delete event, or the application programmer calls gtk_dialog_response(). On a delete event, the response ID is GTK_RESPONSE_NONE. Otherwise, it depends on which action widget was clicked.

`dialog` : | the object which received the signal.
`arg1` : | the response ID
`user_data` : | user data set when the signal handler was connected.

## See Also

GtkVBox

  Pack widgets vertically.

GtkWindow

  Alter the properties of your dialog box.

GtkButton

  Add them to the `action_area` to get a response from the user.

<<< Previous Page | Home | Up | Next Page >>>
Windows | GtkInvisible