AlertDialog#

Added in version 1.5.

class AlertDialog(**properties: Any)#

Superclasses: Dialog, Widget, InitiallyUnowned, Object

Implemented Interfaces: Accessible, Buildable, ConstraintTarget

A dialog presenting a message or a question.

https://gnome.pages.gitlab.gnome.org/libadwaita/doc/1-latest/alert-dialog.png

Alert dialogs have a heading, a body, an optional child widget, and one or multiple responses, each presented as a button.

Each response has a unique string ID, and a button label. Additionally, each response can be enabled or disabled, and can have a suggested or destructive appearance.

When one of the responses is activated, or the dialog is closed, the response signal will be emitted. This signal is detailed, and the detail, as well as the response parameter will be set to the ID of the activated response, or to the value of the close_response property if the dialog had been closed without activating any of the responses.

Response buttons can be presented horizontally or vertically depending on available space.

When a response is activated, AdwAlertDialog is closed automatically.

An example of using an alert dialog:

AdwDialog *dialog;

dialog = adw_alert_dialog_new (_("Replace File?"), NULL);

adw_alert_dialog_format_body (ADW_ALERT_DIALOG (dialog),
                              _("A file named “%s” already exists. Do you want to replace it?"),
                              filename);

adw_alert_dialog_add_responses (ADW_ALERT_DIALOG (dialog),
                                "cancel",  _("_Cancel"),
                                "replace", _("_Replace"),
                                NULL);

adw_alert_dialog_set_response_appearance (ADW_ALERT_DIALOG (dialog),
                                          "replace",
                                          ADW_RESPONSE_DESTRUCTIVE);

adw_alert_dialog_set_default_response (ADW_ALERT_DIALOG (dialog), "cancel");
adw_alert_dialog_set_close_response (ADW_ALERT_DIALOG (dialog), "cancel");

g_signal_connect (dialog, "response", G_CALLBACK (response_cb), self);

adw_dialog_present (dialog, parent);

Async API#

AdwAlertDialog can also be used via the choose method. This API follows the GIO async pattern, for example:

static void
dialog_cb (AdwAlertDialog *dialog,
           GAsyncResult   *result,
           MyWindow       *self)
{
  const char *response = adw_alert_dialog_choose_finish (dialog, result);

  // ...
}

static void
show_dialog (MyWindow *self)
{
  AdwDialog *dialog;

  dialog = adw_alert_dialog_new (_("Replace File?"), NULL);

  adw_alert_dialog_format_body (ADW_ALERT_DIALOG (dialog),
                                _("A file named “%s” already exists. Do you want to replace it?"),
                                filename);

  adw_alert_dialog_add_responses (ADW_ALERT_DIALOG (dialog),
                                  "cancel",  _("_Cancel"),
                                  "replace", _("_Replace"),
                                  NULL);

  adw_alert_dialog_set_response_appearance (ADW_ALERT_DIALOG (dialog),
                                            "replace",
                                            ADW_RESPONSE_DESTRUCTIVE);

  adw_alert_dialog_set_default_response (ADW_ALERT_DIALOG (dialog), "cancel");
  adw_alert_dialog_set_close_response (ADW_ALERT_DIALOG (dialog), "cancel");

  adw_alert_dialog_choose (ADW_ALERT_DIALOG (dialog), GTK_WIDGET (self),
                           NULL, (GAsyncReadyCallback) dialog_cb, self);
}

AdwAlertDialog as GtkBuildable#

AdwAlertDialog supports adding responses in UI definitions by via the <responses> element that may contain multiple <response> elements, each representing a response.

Each of the <response> elements must have the id attribute specifying the response ID. The contents of the element are used as the response label.

Response labels can be translated with the usual translatable, context and comments attributes.

The <response> elements can also have enabled and/or appearance attributes. See set_response_enabled and set_response_appearance for details.

Example of an AdwAlertDialog UI definition:

<object class="AdwAlertDialog" id="dialog">
  <property name="heading" translatable="yes">Save Changes?</property>
  <property name="body" translatable="yes">Open documents contain unsaved changes. Changes which are not saved will be permanently lost.</property>
  <property name="default-response">save</property>
  <property name="close-response">cancel</property>
  <signal name="response" handler="response_cb"/>
  <responses>
    <response id="cancel" translatable="yes">_Cancel</response>
    <response id="discard" translatable="yes" appearance="destructive">_Discard</response>
    <response id="save" translatable="yes" appearance="suggested" enabled="false">_Save</response>
  </responses>
</object>

Constructors#

class AlertDialog
classmethod new(heading: str | None = None, body: str | None = None) Dialog#

Creates a new AdwAlertDialog.

heading and body can be set to NULL. This can be useful if they need to be formatted or use markup. In that case, set them to NULL and call format_body or similar methods afterwards:

AdwDialog *dialog;

dialog = adw_alert_dialog_new (_("Replace File?"), NULL);
adw_alert_dialog_format_body (ADW_ALERT_DIALOG (dialog),
                              _("A file named “%s” already exists.  Do you want to replace it?"),
                              filename);

Added in version 1.5.

Parameters:
  • heading – the heading

  • body – the body text

Methods#

class AlertDialog
add_response(id: str, label: str) None#

Adds a response with id and label to self.

Responses are represented as buttons in the dialog.

Response ID must be unique. It will be used in response to tell which response had been activated, as well as to inspect and modify the response later.

An embedded underline in label indicates a mnemonic.

set_response_label can be used to change the response label after it had been added.

set_response_enabled and set_response_appearance can be used to customize the responses further.

Added in version 1.5.

Parameters:
  • id – the response ID

  • label – the response label

choose(parent: Widget | None = None, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#

This function shows self to the user.

If the window is an Window or ApplicationWindow, the dialog will be shown within it. Otherwise, it will be a separate window.

Added in version 1.5.

Parameters:
  • parent – the parent widget

  • cancellable – a GCancellable to cancel the operation

  • callback – a callback to call when the operation is complete

  • user_data – data to pass to callback

choose_finish(result: AsyncResult) str#

Finishes the choose call and returns the response ID.

Added in version 1.5.

Parameters:

result – a GAsyncResult

do_response(self, response: str) None#
Parameters:

response

get_body() str#

Gets the body text of self.

Added in version 1.5.

get_body_use_markup() bool#

Gets whether the body text of self includes Pango markup.

Added in version 1.5.

get_close_response() str#

Gets the ID of the close response of self.

Added in version 1.5.

get_default_response() str | None#

Gets the ID of the default response of self.

Added in version 1.5.

get_extra_child() Widget | None#

Gets the child widget of self.

Added in version 1.5.

get_heading() str | None#

Gets the heading of self.

Added in version 1.5.

get_heading_use_markup() bool#

Gets whether the heading of self includes Pango markup.

Added in version 1.5.

get_prefer_wide_layout() bool#

Gets whether self prefers wide layout.

Added in version 1.5.

get_response_appearance(response: str) ResponseAppearance#

Gets the appearance of response.

See set_response_appearance.

Added in version 1.5.

Parameters:

response – a response ID

get_response_enabled(response: str) bool#

Gets whether response is enabled.

See set_response_enabled.

Added in version 1.5.

Parameters:

response – a response ID

get_response_label(response: str) str#

Gets the label of response.

See set_response_label.

Added in version 1.5.

Parameters:

response – a response ID

has_response(response: str) bool#

Gets whether self has a response with the ID response.

Added in version 1.5.

Parameters:

response – response ID

remove_response(id: str) None#

Removes a response from self.

Added in version 1.5.

Parameters:

id – the response ID

set_body(body: str) None#

Sets the body text of self.

Added in version 1.5.

Parameters:

body – the body of self

set_body_use_markup(use_markup: bool) None#

Sets whether the body text of self includes Pango markup.

See parse_markup.

Added in version 1.5.

Parameters:

use_markup – whether to use markup for body text

set_close_response(response: str) None#

Sets the ID of the close response of self.

It will be passed to response if the dialog is closed by pressing Escape or with a system action.

It doesn’t have to correspond to any of the responses in the dialog.

The default close response is close.

Added in version 1.5.

Parameters:

response – the close response ID

set_default_response(response: str | None = None) None#

Sets the ID of the default response of self.

If set, pressing Enter will activate the corresponding button.

If set to NULL or to a non-existent response ID, pressing Enter will do nothing.

Added in version 1.5.

Parameters:

response – the default response ID

set_extra_child(child: Widget | None = None) None#

Sets the child widget of self.

The child widget is displayed below the heading and body.

Added in version 1.5.

Parameters:

child – the child widget

set_heading(heading: str | None = None) None#

Sets the heading of self.

Added in version 1.5.

Parameters:

heading – the heading of self

set_heading_use_markup(use_markup: bool) None#

Sets whether the heading of self includes Pango markup.

See parse_markup.

Added in version 1.5.

Parameters:

use_markup – whether to use markup for heading

set_prefer_wide_layout(prefer_wide_layout: bool) None#

Sets whether self prefers wide layout.

Prefer horizontal button layout when possible, and wider dialog width otherwise.

Added in version 1.6.

Parameters:

prefer_wide_layout – whether to prefer wide layout

set_response_appearance(response: str, appearance: ResponseAppearance) None#

Sets the appearance for response.

https://gnome.pages.gitlab.gnome.org/libadwaita/doc/1-latest/alert-dialog-appearance.png

Use ADW_RESPONSE_SUGGESTED to mark important responses such as the affirmative action, like the Save button in the example.

Use ADW_RESPONSE_DESTRUCTIVE to draw attention to the potentially damaging consequences of using response. This appearance acts as a warning to the user. The Discard button in the example is using this appearance.

The default appearance is ADW_RESPONSE_DEFAULT.

Negative responses like Cancel or Close should use the default appearance.

Added in version 1.5.

Parameters:
  • response – a response ID

  • appearance – appearance for response

set_response_enabled(response: str, enabled: bool) None#

Sets whether response is enabled.

If response is not enabled, the corresponding button will have sensitive set to FALSE and it can’t be activated as a default response.

response can still be used as close_response while it’s not enabled.

Responses are enabled by default.

Added in version 1.5.

Parameters:
  • response – a response ID

  • enabled – whether to enable response

set_response_label(response: str, label: str) None#

Sets the label of response to label.

Labels are displayed on the dialog buttons. An embedded underline in label indicates a mnemonic.

Added in version 1.5.

Parameters:
  • response – a response ID

  • label – the label of response

Properties#

class AlertDialog
props.body: str#

The type of the None singleton.

Added in version 1.5.

props.body_use_markup: bool#

The type of the None singleton.

Added in version 1.5.

props.close_response: str#

The type of the None singleton.

Added in version 1.5.

props.default_response: str#

The type of the None singleton.

Added in version 1.5.

props.extra_child: Widget#

The type of the None singleton.

Added in version 1.5.

props.heading: str#

The type of the None singleton.

Added in version 1.5.

props.heading_use_markup: bool#

The type of the None singleton.

Added in version 1.5.

props.prefer_wide_layout: bool#

The type of the None singleton.

Added in version 1.6.

Signals#

class AlertDialog.signals
response(response: str) None#

The type of the None singleton.

Added in version 1.5.

Parameters:

response – the response ID

Virtual Methods#

class AlertDialog
do_response(response: str) None#

The type of the None singleton.

Parameters:

response

Fields#

class AlertDialog
parent_instance#