GtkDialog

GtkDialog — Create popup windows

Properties

int use-header-bar Read / Write / Construct Only

Style Properties

Signals

void close Action
void response Run Last

Types and Values

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── GtkWidget
            ╰── GtkContainer
                ╰── GtkBin
                    ╰── GtkWindow
                        ╰── GtkDialog
                            ├── GtkAboutDialog
                            ├── GtkAppChooserDialog
                            ├── GtkColorChooserDialog
                            ├── GtkColorSelectionDialog
                            ├── GtkFileChooserDialog
                            ├── GtkFontChooserDialog
                            ├── GtkFontSelectionDialog
                            ├── GtkMessageDialog
                            ├── GtkPageSetupUnixDialog
                            ├── GtkPrintUnixDialog
                            ╰── GtkRecentChooserDialog

Implemented Interfaces

GtkDialog implements AtkImplementorIface and GtkBuildable.

Includes

#include <gtk/gtk.h>

Description

Dialog boxes are a convenient way to prompt the user for a small amount of input, e.g. 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.

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 through gtk_dialog_get_content_area() and gtk_dialog_get_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_DELETE_EVENT.

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.

An example for simple GtkDialog usage:

GtkDialog as GtkBuildable

The GtkDialog implementation of the GtkBuildable interface exposes the vbox and action_area as internal children with the names “vbox” and “action_area”.

GtkDialog supports a custom <action-widgets> element, which can contain multiple <action-widget> elements. The “response” attribute specifies a numeric response, and the content of the element is the id of widget (which should be a child of the dialogs action_area ). To mark a response as default, set the “default“ attribute of the <action-widget> element to true.

GtkDialog supports adding action widgets by specifying “action“ as the “type“ attribute of a <child> element. The widget will be added either to the action area or the headerbar of the dialog, depending on the “use-header-bar“ property. The response id has to be associated with the action widget using the <action-widgets> element.

An example of a GtkDialog UI definition fragment:

Functions

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

the new dialog as a GtkWidget

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 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:

Parameters

title

Title of the dialog, or NULL.

[allow-none]

parent

Transient parent of the dialog, or NULL.

[allow-none]

flags

from GtkDialogFlags

first_button_text

text to go in first button, or NULL.

[allow-none]

...

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 during the call to gtk_dialog_run(), 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:

Note that even though the recursive main loop gives the effect of a modal dialog (it prevents the user from interacting with other windows in the same window group while the dialog is run), callbacks such as timeouts, IO channel watches, DND drops, etc, will be triggered during a gtk_dialog_run() call.

Parameters

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.

Parameters

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 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.

Parameters

dialog

a GtkDialog

button_text

text of button

response_id

response ID for the button

Returns

the GtkButton widget that was added.

[transfer none]

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.

Parameters

dialog

a GtkDialog

first_button_text

button text

...

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.

Parameters

dialog

a GtkDialog

child

an activatable widget

response_id

response ID for child

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.

Parameters

dialog

a GtkDialog

response_id

a response ID

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.

Parameters

dialog

a GtkDialog

response_id

a response ID

setting

TRUE for sensitive

gtk_dialog_get_response_for_widget ()

gint
gtk_dialog_get_response_for_widget (GtkDialog *dialog,
                                    GtkWidget *widget);

Gets the response id of a widget in the action area of a dialog.

Parameters

dialog

a GtkDialog

widget

a widget in the action area of dialog

Returns

the response id of widget , or GTK_RESPONSE_NONE if widget doesn’t have a response id set.

Since: 2.8

gtk_dialog_get_widget_for_response ()

GtkWidget *
gtk_dialog_get_widget_for_response (GtkDialog *dialog,
                                    gint response_id);

Gets the widget button that uses the given response ID in the action area of a dialog.

Parameters

dialog

a GtkDialog

response_id

the response ID used by the dialog widget

Returns

the widget button that uses the given response_id , or NULL.

[nullable][transfer none]

Since: 2.20

gtk_dialog_get_action_area ()

GtkWidget *
gtk_dialog_get_action_area (GtkDialog *dialog);

gtk_dialog_get_action_area has been deprecated since version 3.12 and should not be used in newly-written code.

Direct access to the action area is discouraged; use gtk_dialog_add_button(), etc.

Returns the action area of dialog .

Parameters

dialog

a GtkDialog

Returns

the action area.

[type Gtk.Box][transfer none]

Since: 2.14

gtk_dialog_get_content_area ()

GtkWidget *
gtk_dialog_get_content_area (GtkDialog *dialog);

Returns the content area of dialog .

Parameters

dialog

a GtkDialog

Returns

the content area GtkBox.

[type Gtk.Box][transfer none]

Since: 2.14

gtk_dialog_get_header_bar ()

GtkWidget *
gtk_dialog_get_header_bar (GtkDialog *dialog);

Returns the header bar of dialog . Note that the headerbar is only used by the dialog if the “use-header-bar” property is TRUE.

Parameters

dialog

a GtkDialog

Returns

the header bar.

[type Gtk.HeaderBar][transfer none]

Since: 3.12

gtk_alternative_dialog_button_order ()

gboolean
gtk_alternative_dialog_button_order (GdkScreen *screen);

gtk_alternative_dialog_button_order has been deprecated since version 3.10 and should not be used in newly-written code.

Deprecated

Returns TRUE if dialogs are expected to use an alternative button order on the screen screen . See gtk_dialog_set_alternative_button_order() for more details about alternative button order.

If you need to use this function, you should probably connect to the ::notify:gtk-alternative-button-order signal on the GtkSettings object associated to screen , in order to be notified if the button order setting changes.

Parameters

screen

a GdkScreen, or NULL to use the default screen.

[allow-none]

Returns

Whether the alternative button order should be used

Since: 2.6

gtk_dialog_set_alternative_button_order ()

void
gtk_dialog_set_alternative_button_order
                               (GtkDialog *dialog,
                                gint first_response_id,
                                ...);

gtk_dialog_set_alternative_button_order has been deprecated since version 3.10 and should not be used in newly-written code.

Deprecated

Sets an alternative button order. If the “gtk-alternative-button-order” setting is set to TRUE, the dialog buttons are reordered according to the order of the response ids passed to this function.

By default, GTK+ dialogs use the button order advocated by the GNOME Human Interface Guidelines with the affirmative button at the far right, and the cancel button left of it. But the builtin GTK+ dialogs and GtkMessageDialogs do provide an alternative button order, which is more suitable on some platforms, e.g. Windows.

Use this function after adding all the buttons to your dialog, as the following example shows:

// Function to open a dialog box with a message
void
quick_message (GtkWindow *parent, gchar *message)
{
 GtkWidget *dialog, *label, *content_area;
 GtkDialogFlags flags;

 // Create the widgets
 flags = GTK_DIALOG_DESTROY_WITH_PARENT;
 dialog = gtk_dialog_new_with_buttons ("Message",
                                       parent,
                                       flags,
                                       _("_OK"),
                                       GTK_RESPONSE_NONE,
                                       NULL);
 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
 label = gtk_label_new (message);

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

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

 // Add the label, and show everything we’ve added

 gtk_container_add (GTK_CONTAINER (content_area), label);
 gtk_widget_show_all (dialog);
}

Parameters

dialog

a GtkDialog

first_response_id

a response id used by one dialog ’s buttons

...

a list of more response ids of dialog ’s buttons, terminated by -1

Since: 2.6

gtk_dialog_set_alternative_button_order_from_array ()

void
gtk_dialog_set_alternative_button_order_from_array
                               (GtkDialog *dialog,
                                gint n_params,
                                gint *new_order);

gtk_dialog_set_alternative_button_order_from_array has been deprecated since version 3.10 and should not be used in newly-written code.

Deprecated

Sets an alternative button order. If the “gtk-alternative-button-order” setting is set to TRUE, the dialog buttons are reordered according to the order of the response ids in new_order .

See gtk_dialog_set_alternative_button_order() for more information.

This function is for use by language bindings.

Parameters

dialog

a GtkDialog

n_params

the number of response ids in new_order

new_order

an array of response ids of dialog ’s buttons.

[array length=n_params]

Since: 2.6

Types and Values

struct GtkDialog

struct GtkDialog;

The GtkDialog contains only private fields and should not be directly accessed.

struct GtkDialogClass

struct GtkDialogClass {
  GtkWindowClass parent_class;

  void (* response) (GtkDialog *dialog, gint response_id);

  /* Keybinding signals */

  void (* close)    (GtkDialog *dialog);
};

Members

response ()

Signal emitted when an action widget is activated.

close ()

Signal emitted when the user uses a keybinding to close the dialog.

enum GtkDialogFlags

Flags used to influence dialog construction.

Members

GTK_DIALOG_MODAL

Make the constructed dialog modal, see gtk_window_set_modal()

GTK_DIALOG_DESTROY_WITH_PARENT

Destroy the dialog when its parent is destroyed, see gtk_window_set_destroy_with_parent()

GTK_DIALOG_USE_HEADER_BAR

Create dialog with actions in header bar instead of action area. Since 3.12.

enum GtkResponseType

Predefined values for use as response ids in gtk_dialog_add_button(). All predefined values are negative; GTK+ leaves values of 0 or greater for application-defined response ids.

Members

GTK_RESPONSE_NONE

Returned if an action widget has no response id, or if the dialog gets programmatically hidden or destroyed

GTK_RESPONSE_REJECT

Generic response id, not used by GTK+ dialogs

GTK_RESPONSE_ACCEPT

Generic response id, not used by GTK+ dialogs

GTK_RESPONSE_DELETE_EVENT

Returned if the dialog is deleted

GTK_RESPONSE_OK

Returned by OK buttons in GTK+ dialogs

GTK_RESPONSE_CANCEL

Returned by Cancel buttons in GTK+ dialogs

GTK_RESPONSE_CLOSE

Returned by Close buttons in GTK+ dialogs

GTK_RESPONSE_YES

Returned by Yes buttons in GTK+ dialogs

GTK_RESPONSE_NO

Returned by No buttons in GTK+ dialogs

GTK_RESPONSE_APPLY

Returned by Apply buttons in GTK+ dialogs

GTK_RESPONSE_HELP

Returned by Help buttons in GTK+ dialogs

Property Details

The “use-header-bar” property

  “use-header-bar”           int

TRUE if the dialog uses a GtkHeaderBar for action buttons instead of the action-area.

For technical reasons, this property is declared as an integer property, but you should only set it to TRUE or FALSE.

Owner: GtkDialog

Flags: Read / Write / Construct Only

Allowed values: [-1,1]

Default value: -1

Since: 3.12

Style Property Details

The “action-area-border” style property

  “action-area-border”       int

The default border width used around the action area of the dialog, as returned by gtk_dialog_get_action_area(), unless gtk_container_set_border_width() was called on that widget directly.

Owner: GtkDialog

Flags: Read

Allowed values: >= 0

Default value: 5

The “button-spacing” style property

  “button-spacing”           int

Spacing between buttons.

Owner: GtkDialog

Flags: Read

Allowed values: >= 0

Default value: 6

The “content-area-border” style property

  “content-area-border”      int

The default border width used around the content area of the dialog, as returned by gtk_dialog_get_content_area(), unless gtk_container_set_border_width() was called on that widget directly.

Owner: GtkDialog

Flags: Read

Allowed values: >= 0

Default value: 2

The “content-area-spacing” style property

  “content-area-spacing”     int

The default spacing used between elements of the content area of the dialog, as returned by gtk_dialog_get_content_area(), unless gtk_box_set_spacing() was called on that widget directly.

Owner: GtkDialog

Flags: Read

Allowed values: >= 0

Default value: 0

Since: 2.16

Signal Details

The “close” signal

void
user_function (GtkDialog *dialog,
               gpointer   user_data)

The ::close signal is a keybinding signal which gets emitted when the user uses a keybinding to close the dialog.

The default binding for this signal is the Escape key.

Parameters

user_data

user data set when the signal handler was connected.

Flags: Action

The “response” signal

void
user_function (GtkDialog *dialog,
               int        response_id,
               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_DELETE_EVENT. Otherwise, it depends on which action widget was clicked.

Parameters

dialog

the object on which the signal is emitted

response_id

the response ID

user_data

user data set when the signal handler was connected.

Flags: Run Last

© 2005–2020 The GNOME Project
Licensed under the GNU Lesser General Public License version 2.1 or later.
https://developer.gnome.org/gtk3/3.24/GtkDialog.html