Adw.AlertDialog¶
- Subclasses:
None
Methods¶
- Inherited:
Adw.Dialog (24), Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1)
- Structs:
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Virtual Methods¶
- Inherited:
Adw.Dialog (2), Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9)
|
Properties¶
- Inherited:
Name |
Type |
Flags |
Short Description |
---|---|---|---|
r/w/en |
|||
r/w/en |
|||
r/w/en |
|||
r/w/en |
|||
r/w/en |
|||
r/w/en |
|||
r/w/en |
Signals¶
- Inherited:
Name |
Short Description |
---|---|
This signal is emitted when the dialog is closed. |
Fields¶
- Inherited:
Name |
Type |
Access |
Description |
---|---|---|---|
parent_instance |
r |
Class Details¶
- class Adw.AlertDialog(**kwargs)¶
- Bases:
- Abstract:
No
- Structure:
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) { conststr
*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); } ```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 theid
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
andcomments
attributes.The
<response>
elements can also haveenabled
and/orappearance
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:
- Returns:
the newly created
AdwAlertDialog
- Return type:
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 toNULL
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)¶
-
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:
parent (
Gtk.Widget
orNone
) – the parent widgetcancellable (
Gio.Cancellable
orNone
) – aGCancellable
to cancel the operationcallback (
Gio.AsyncReadyCallback
orNone
) – a callback to call when the operation is complete
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
) – aGAsyncResult
- 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:
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:
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:
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:
Gets the ID of the close response of self.
New in version 1.5.
- get_default_response()¶
-
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
orNone
Gets the child widget of self.
New in version 1.5.
- get_heading()¶
-
Gets the heading of self.
New in version 1.5.
- get_heading_use_markup()¶
- Returns:
whether self uses markup for heading
- Return type:
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:
Gets the appearance of response.
See [method`AlertDialog`.set_response_appearance].
New in version 1.5.
- get_response_enabled(response)¶
-
Gets whether response is enabled.
See [method`AlertDialog`.set_response_enabled].
New in version 1.5.
- get_response_label(response)¶
-
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:
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)¶
-
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
orNone
) – 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)¶
-
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:
response (
str
) – a response IDappearance (
Adw.ResponseAppearance
) – appearance for response
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)¶
-
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)¶
-
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.
Signal Details¶
- Adw.AlertDialog.signals.response(alert_dialog, response)¶
- Signal Name:
response
- Flags:
- Parameters:
alert_dialog (
Adw.AlertDialog
) – The object which received the signalresponse (
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:
- Default Value:
''
- Flags:
The body text of the dialog.
New in version 1.5.
- Adw.AlertDialog.props.body_use_markup¶
- Name:
body-use-markup
- Type:
- Default Value:
- Flags:
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:
- Default Value:
'close'
- Flags:
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:
- Default Value:
- Flags:
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:
- Default Value:
- Flags:
The child widget.
Displayed below the heading and body.
New in version 1.5.
- Adw.AlertDialog.props.heading¶
- Name:
heading
- Type:
- Default Value:
''
- Flags:
The heading of the dialog.
New in version 1.5.