Adw.AlertDialog

g Adw.AlertDialog Adw.AlertDialog Adw.Dialog Adw.Dialog Adw.Dialog->Adw.AlertDialog GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Buildable->Gtk.Widget Gtk.ConstraintTarget->Gtk.Widget Gtk.Widget->Adw.Dialog

Subclasses:

None

Methods

Inherited:

Adw.Dialog (24), Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new (heading, body)

add_response (id, label)

choose (parent, cancellable, callback, *user_data)

choose_finish (result)

get_body ()

get_body_use_markup ()

get_close_response ()

get_default_response ()

get_extra_child ()

get_heading ()

get_heading_use_markup ()

get_response_appearance (response)

get_response_enabled (response)

get_response_label (response)

has_response (response)

remove_response (id)

set_body (body)

set_body_use_markup (use_markup)

set_close_response (response)

set_default_response (response)

set_extra_child (child)

set_heading (heading)

set_heading_use_markup (use_markup)

set_response_appearance (response, appearance)

set_response_enabled (response, enabled)

set_response_label (response, label)

Virtual Methods

Inherited:

Adw.Dialog (2), Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9)

do_response (response)

Properties

Inherited:

Adw.Dialog (10), Gtk.Widget (34), Gtk.Accessible (1)

Name

Type

Flags

Short Description

body

str

r/w/en

body-use-markup

bool

r/w/en

close-response

str

r/w/en

default-response

str

r/w/en

extra-child

Gtk.Widget

r/w/en

heading

str

r/w/en

heading-use-markup

bool

r/w/en

Signals

Inherited:

Adw.Dialog (2), Gtk.Widget (13), GObject.Object (1)

Name

Short Description

response

This signal is emitted when the dialog is closed.

Fields

Inherited:

Adw.Dialog (2), Gtk.Widget (13), GObject.Object (1)

Name

Type

Access

Description

parent_instance

Adw.Dialog

r

Class Details

class Adw.AlertDialog(**kwargs)
Bases:

Adw.Dialog

Abstract:

No

Structure:

Adw.AlertDialogClass

A dialog presenting a message or a question.

<picture> <source srcset=”alert-dialog-dark.png” media=”(prefers-color-scheme: dark)”> <img src=”alert-dialog.png” alt=”alert-dialog”> </picture>

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 [signal`AlertDialog`:py:func:::response<Adw.AlertDialog.signals.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 [property`AlertDialog`:py:data::close-response<Adw.AlertDialog.props.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:

```c Adw.Dialog *dialog;

dialog = Adw.AlertDialog.new (_(“Replace File?”), None);

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”), None);

Adw.AlertDialog.set_response_appearance (ADW_ALERT_DIALOG (dialog), “replace”, Adw.ResponseAppearance.DESTRUCTIVE);

Adw.AlertDialog.set_default_response (ADW_ALERT_DIALOG (dialog), “cancel”); Adw.AlertDialog.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 [method`AlertDialog`.choose] method. This API follows the GIO async pattern, and the result can be obtained by calling [method`AlertDialog`.choose_finish], for example:

```c static void dialog_cb (Adw.AlertDialog *dialog, Gio.AsyncResult *result, MyWindow *self) { const str *response = Adw.AlertDialog.choose_finish (dialog, result);

// … }

static void show_dialog (MyWindow *self) { Adw.Dialog *dialog;

dialog = Adw.AlertDialog.new (_(“Replace File?”), None);

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”), None);

Adw.AlertDialog.set_response_appearance (ADW_ALERT_DIALOG (dialog), “replace”, Adw.ResponseAppearance.DESTRUCTIVE);

Adw.AlertDialog.set_default_response (ADW_ALERT_DIALOG (dialog), “cancel”); Adw.AlertDialog.set_close_response (ADW_ALERT_DIALOG (dialog), “cancel”);

Adw.AlertDialog.choose (ADW_ALERT_DIALOG (dialog), GTK_WIDGET (self), None, (Gio.AsyncReadyCallback) dialog_cb, self); } ```

Adw.AlertDialog as Gtk.Buildable

AdwAlertDialog supports adding responses in UI definitions by via the <responses> element that may contain multiple <response> elements, each respresenting 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 [method`AlertDialog`.set_response_enabled] and [method`AlertDialog`.set_response_appearance] for details.

Example of an AdwAlertDialog UI definition:

``xml <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> ``

New in version 1.5.

classmethod new(heading, body)
Parameters:
  • heading (str or None) – the heading

  • body (str or None) – the body text

Returns:

the newly created AdwAlertDialog

Return type:

Adw.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 [method`AlertDialog`.format_body] or similar methods afterwards:

```c Adw.Dialog *dialog;

dialog = Adw.AlertDialog.new (_(“Replace File?”), None); adw_alert_dialog_format_body (ADW_ALERT_DIALOG (dialog), _(“A file named “%s” already exists. Do you want to replace it?”), filename); ```

New in version 1.5.

add_response(id, label)
Parameters:
  • id (str) – the response ID

  • label (str) – the response label

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 [signal`AlertDialog`:py:func:::response<Adw.AlertDialog.signals.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.

[method`AlertDialog`.set_response_label] can be used to change the response label after it had been added.

[method`AlertDialog`.set_response_enabled] and [method`AlertDialog`.set_response_appearance] can be used to customize the responses further.

New in version 1.5.

choose(parent, cancellable, callback, *user_data)
Parameters:

This function shows self to the user.

The callback will be called when the alert is dismissed. It should call [method`AlertDialog`.choose_finish] to obtain the result.

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

New in version 1.5.

choose_finish(result)
Parameters:

result (Gio.AsyncResult) – a GAsyncResult

Returns:

the ID of the response that was selected, or [property`AlertDialog`:py:data::close-response<Adw.AlertDialog.props.close_response>] if the call was cancelled.

Return type:

str

Finishes the [method`AlertDialog`.choose] call and returns the response ID.

New in version 1.5.

get_body()
Returns:

the body of self.

Return type:

str

Gets the body text of self.

New in version 1.5.

get_body_use_markup()
Returns:

whether self uses markup for body text

Return type:

bool

Gets whether the body text of self includes Pango markup.

New in version 1.5.

get_close_response()
Returns:

the close response ID

Return type:

str

Gets the ID of the close response of self.

New in version 1.5.

get_default_response()
Returns:

the default response ID

Return type:

str or None

Gets the ID of the default response of self.

New in version 1.5.

get_extra_child()
Returns:

the child widget of self.

Return type:

Gtk.Widget or None

Gets the child widget of self.

New in version 1.5.

get_heading()
Returns:

the heading of self.

Return type:

str or None

Gets the heading of self.

New in version 1.5.

get_heading_use_markup()
Returns:

whether self uses markup for heading

Return type:

bool

Gets whether the heading of self includes Pango markup.

New in version 1.5.

get_response_appearance(response)
Parameters:

response (str) – a response ID

Returns:

the appearance of response

Return type:

Adw.ResponseAppearance

Gets the appearance of response.

See [method`AlertDialog`.set_response_appearance].

New in version 1.5.

get_response_enabled(response)
Parameters:

response (str) – a response ID

Returns:

whether response is enabled

Return type:

bool

Gets whether response is enabled.

See [method`AlertDialog`.set_response_enabled].

New in version 1.5.

get_response_label(response)
Parameters:

response (str) – a response ID

Returns:

the label of response

Return type:

str

Gets the label of response.

See [method`AlertDialog`.set_response_label].

New in version 1.5.

has_response(response)
Parameters:

response (str) – response ID

Returns:

whether self has a response with the ID response.

Return type:

bool

Gets whether self has a response with the ID response.

New in version 1.5.

remove_response(id)
Parameters:

id (str) – the response ID

Removes a response from self.

New in version 1.5.

set_body(body)
Parameters:

body (str) – the body of self

Sets the body text of self.

New in version 1.5.

set_body_use_markup(use_markup)
Parameters:

use_markup (bool) – whether to use markup for body text

Sets whether the body text of self includes Pango markup.

See [func`Pango`.parse_markup].

New in version 1.5.

set_close_response(response)
Parameters:

response (str) – the close response ID

Sets the ID of the close response of self.

It will be passed to [signal`AlertDialog`:py:func:::response<Adw.AlertDialog.signals.response>] if the dialog is closed by pressing <kbd>Escape</kbd> 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.

New in version 1.5.

set_default_response(response)
Parameters:

response (str or None) – the default response ID

Sets the ID of the default response of self.

If set, pressing <kbd>Enter</kbd> will activate the corresponding button.

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

New in version 1.5.

set_extra_child(child)
Parameters:

child (Gtk.Widget or None) – the child widget

Sets the child widget of self.

The child widget is displayed below the heading and body.

New in version 1.5.

set_heading(heading)
Parameters:

heading (str or None) – the heading of self

Sets the heading of self.

New in version 1.5.

set_heading_use_markup(use_markup)
Parameters:

use_markup (bool) – whether to use markup for heading

Sets whether the heading of self includes Pango markup.

See [func`Pango`.parse_markup].

New in version 1.5.

set_response_appearance(response, appearance)
Parameters:

Sets the appearance for response.

<picture> <source srcset=”alert-dialog-appearance-dark.png” media=”(prefers-color-scheme: dark)”> <img src=”alert-dialog-appearance.png” alt=”alert-dialog-appearance”> </picture>

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.

New in version 1.5.

set_response_enabled(response, enabled)
Parameters:
  • response (str) – a response ID

  • enabled (bool) – whether to enable response

Sets whether response is enabled.

If response is not enabled, the corresponding button will have [property`Gtk`.Widget:sensitive] set to FALSE and it can’t be activated as a default response.

response can still be used as [property`AlertDialog`:py:data::close-response<Adw.AlertDialog.props.close_response>] while it’s not enabled.

Responses are enabled by default.

New in version 1.5.

set_response_label(response, label)
Parameters:
  • response (str) – a response ID

  • label (str) – the label of response

Sets the label of response to label.

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

New in version 1.5.

do_response(response) virtual
Parameters:

response (str) –

Signal Details

Adw.AlertDialog.signals.response(alert_dialog, response)
Signal Name:

response

Flags:

RUN_LAST, DETAILED

Parameters:
  • alert_dialog (Adw.AlertDialog) – The object which received the signal

  • response (str) – the response ID

This signal is emitted when the dialog is closed.

response will be set to the response ID of the button that had been activated.

if the dialog was closed by pressing <kbd>Escape</kbd> or with a system action, response will be set to the value of [property`AlertDialog`:py:data::close-response<Adw.AlertDialog.props.close_response>].

New in version 1.5.

Property Details

Adw.AlertDialog.props.body
Name:

body

Type:

str

Default Value:

''

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The body text of the dialog.

New in version 1.5.

Adw.AlertDialog.props.body_use_markup
Name:

body-use-markup

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the body text includes Pango markup.

See [func`Pango`.parse_markup].

New in version 1.5.

Adw.AlertDialog.props.close_response
Name:

close-response

Type:

str

Default Value:

'close'

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The ID of the close response.

It will be passed to [signal`AlertDialog`:py:func:::response<Adw.AlertDialog.signals.response>] if the dialog is closed by pressing <kbd>Escape</kbd> 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.

New in version 1.5.

Adw.AlertDialog.props.default_response
Name:

default-response

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The response ID of the default response.

If set, pressing <kbd>Enter</kbd> will activate the corresponding button.

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

New in version 1.5.

Adw.AlertDialog.props.extra_child
Name:

extra-child

Type:

Gtk.Widget

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The child widget.

Displayed below the heading and body.

New in version 1.5.

Adw.AlertDialog.props.heading
Name:

heading

Type:

str

Default Value:

''

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The heading of the dialog.

New in version 1.5.

Adw.AlertDialog.props.heading_use_markup
Name:

heading-use-markup

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the heading includes Pango markup.

See [func`Pango`.parse_markup].

New in version 1.5.