Class AlertDialog

All Implemented Interfaces:
Accessible, Buildable, ConstraintTarget, ShortcutManager, Proxy

@Generated("org.javagi.JavaGI") public class AlertDialog extends Dialog implements Accessible, Buildable, ConstraintTarget, ShortcutManager

A dialog presenting a message or a question.

alert-dialog

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 AlertDialog::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 AlertDialog: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(Widget, Cancellable, AsyncReadyCallback) 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 setResponseEnabled(String, boolean) and setResponseAppearance(String, ResponseAppearance) 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>
Since:
1.5
  • Constructor Details

    • AlertDialog

      public AlertDialog(MemorySegment address)
      Create a AlertDialog instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • AlertDialog

      public AlertDialog(@Nullable String heading, @Nullable String body)

      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 formatBody(String, Object...) 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);
      
      Parameters:
      heading - the heading
      body - the body text
      Since:
      1.5
    • AlertDialog

      public AlertDialog()
      Create a new AlertDialog.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the AlertDialog class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected AlertDialog asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Dialog
      Returns:
      the instance as if it were its parent type
    • addResponse

      public void addResponse(String id, String 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 AlertDialog::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.

      setResponseLabel(String, String) can be used to change the response label after it had been added.

      setResponseEnabled(String, boolean) and setResponseAppearance(String, ResponseAppearance) can be used to customize the responses further.

      Parameters:
      id - the response ID
      label - the response label
      Since:
      1.5
    • addResponses

      public void addResponses(String firstId, Object... varargs)

      Adds multiple responses to self.

      This is the same as calling addResponse(String, String) repeatedly. The variable argument list should be NULL-terminated list of response IDs and labels.

      Example:

      adw_alert_dialog_add_responses (dialog,
                                      "cancel",  _("_Cancel"),
                                      "discard", _("_Discard"),
                                      "save",    _("_Save"),
                                      NULL);
      
      Parameters:
      firstId - response id
      varargs - label for first response, then more id-label pairs
      Since:
      1.5
    • choose

      public void choose(@Nullable Widget parent, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      This function shows this AlertDialog 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.

      Parameters:
      parent - the parent widget
      cancellable - a GCancellable to cancel the operation
      callback - a callback to call when the operation is complete
      Since:
      1.5
    • chooseFinish

      public String chooseFinish(AsyncResult result)
      Finishes the choose(Widget, Cancellable, AsyncReadyCallback) call and returns the response ID.
      Parameters:
      result - a GAsyncResult
      Returns:
      the ID of the response that was selected, or AlertDialog:close-response if the call was cancelled.
      Since:
      1.5
    • formatBody

      public void formatBody(String format, Object... varargs)

      Sets the formatted body text of self.

      See AlertDialog:body.

      Parameters:
      format - the formatted string for the body text
      varargs - the parameters to insert into format
      Since:
      1.5
    • formatBodyMarkup

      public void formatBodyMarkup(String format, Object... varargs)

      Sets the formatted body text of this AlertDialog with Pango markup.

      The format is assumed to contain Pango markup.

      Special XML characters in the printf() arguments passed to this function will automatically be escaped as necessary, see GLib#markupPrintfEscaped.

      See AlertDialog:body.

      Parameters:
      format - the formatted string for the body text with Pango markup
      varargs - the parameters to insert into format
      Since:
      1.5
    • formatHeading

      public void formatHeading(String format, Object... varargs)

      Sets the formatted heading of self.

      See AlertDialog:heading.

      Parameters:
      format - the formatted string for the heading
      varargs - the parameters to insert into format
      Since:
      1.5
    • formatHeadingMarkup

      public void formatHeadingMarkup(String format, Object... varargs)

      Sets the formatted heading of this AlertDialog with Pango markup.

      The format is assumed to contain Pango markup.

      Special XML characters in the printf() arguments passed to this function will automatically be escaped as necessary, see GLib#markupPrintfEscaped.

      See AlertDialog:heading.

      Parameters:
      format - the formatted string for the heading with Pango markup
      varargs - the parameters to insert into format
      Since:
      1.5
    • getBody

      public String getBody()
      Gets the body text of self.
      Returns:
      the body of self.
      Since:
      1.5
    • getBodyUseMarkup

      public boolean getBodyUseMarkup()
      Gets whether the body text of this AlertDialog includes Pango markup.
      Returns:
      whether this AlertDialog uses markup for body text
      Since:
      1.5
    • getCloseResponse

      public String getCloseResponse()
      Gets the ID of the close response of self.
      Returns:
      the close response ID
      Since:
      1.5
    • getDefaultResponse

      public @Nullable String getDefaultResponse()
      Gets the ID of the default response of self.
      Returns:
      the default response ID
      Since:
      1.5
    • getExtraChild

      public @Nullable Widget getExtraChild()
      Gets the child widget of self.
      Returns:
      the child widget of self.
      Since:
      1.5
    • getHeading

      public @Nullable String getHeading()
      Gets the heading of self.
      Returns:
      the heading of self.
      Since:
      1.5
    • getHeadingUseMarkup

      public boolean getHeadingUseMarkup()
      Gets whether the heading of this AlertDialog includes Pango markup.
      Returns:
      whether this AlertDialog uses markup for heading
      Since:
      1.5
    • getPreferWideLayout

      public boolean getPreferWideLayout()
      Gets whether this AlertDialog prefers horizontal button layout.
      Returns:
      whether to prefer wide layout
      Since:
      1.5
    • getResponseAppearance

      public ResponseAppearance getResponseAppearance(String response)

      Gets the appearance of response.

      See setResponseAppearance(String, ResponseAppearance).

      Parameters:
      response - a response ID
      Returns:
      the appearance of response
      Since:
      1.5
    • getResponseEnabled

      public boolean getResponseEnabled(String response)

      Gets whether response is enabled.

      See setResponseEnabled(String, boolean).

      Parameters:
      response - a response ID
      Returns:
      whether response is enabled
      Since:
      1.5
    • getResponseLabel

      public String getResponseLabel(String response)

      Gets the label of response.

      See setResponseLabel(String, String).

      Parameters:
      response - a response ID
      Returns:
      the label of response
      Since:
      1.5
    • hasResponse

      public boolean hasResponse(String response)
      Gets whether this AlertDialog has a response with the ID response.
      Parameters:
      response - response ID
      Returns:
      whether this AlertDialog has a response with the ID response.
      Since:
      1.5
    • removeResponse

      public void removeResponse(String id)
      Removes a response from self.
      Parameters:
      id - the response ID
      Since:
      1.5
    • setBody

      public void setBody(String body)
      Sets the body text of self.
      Parameters:
      body - the body of this AlertDialog
      Since:
      1.5
    • setBodyUseMarkup

      public void setBodyUseMarkup(boolean useMarkup)

      Sets whether the body text of this AlertDialog includes Pango markup.

      See Pango#parseMarkup.

      Parameters:
      useMarkup - whether to use markup for body text
      Since:
      1.5
    • setCloseResponse

      public void setCloseResponse(String response)

      Sets the ID of the close response of self.

      It will be passed to AlertDialog::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.

      Parameters:
      response - the close response ID
      Since:
      1.5
    • setDefaultResponse

      public void setDefaultResponse(@Nullable String response)

      Sets the ID of the default response of self.

      The button corresponding to this response will be set as the default widget of self.

      If not set, the default widget will not be set, and the last added response will be focused by default.

      See Dialog:default-widget.

      Parameters:
      response - the default response ID
      Since:
      1.5
    • setExtraChild

      public void setExtraChild(@Nullable Widget child)

      Sets the child widget of self.

      The child widget is displayed below the heading and body.

      Parameters:
      child - the child widget
      Since:
      1.5
    • setHeading

      public void setHeading(@Nullable String heading)
      Sets the heading of self.
      Parameters:
      heading - the heading of this AlertDialog
      Since:
      1.5
    • setHeadingUseMarkup

      public void setHeadingUseMarkup(boolean useMarkup)

      Sets whether the heading of this AlertDialog includes Pango markup.

      See Pango#parseMarkup.

      Parameters:
      useMarkup - whether to use markup for heading
      Since:
      1.5
    • setPreferWideLayout

      public void setPreferWideLayout(boolean preferWideLayout)

      Whether to prefer horizontal button layout.

      AdwAlertDialog can present buttons horizontally or vertically depending on available space, how many buttons there are and how wide they are.

      By default it will prefer to stack buttons vertically at medium sizes.

      Set to TRUE to prefer horizontal layout in these cases instead. This will make the dialog slightly wider as well.

      Vertical layout may still be used if the dialog would get too wide otherwise.

      Does nothing with just one button, or when the buttons are already horizontal.

      Parameters:
      preferWideLayout - whether to prefer wide layout
      Since:
      1.6
    • setResponseAppearance

      public void setResponseAppearance(String response, ResponseAppearance appearance)

      Sets the appearance for response.

      alert-dialog-appearance

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

      Use Adw.ResponseAppearance.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.ResponseAppearance.default.

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

      Parameters:
      response - a response ID
      appearance - appearance for response
      Since:
      1.5
    • setResponseEnabled

      public void setResponseEnabled(String response, boolean enabled)

      Sets whether response is enabled.

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

      response can still be used as AlertDialog:close-response while it's not enabled.

      Responses are enabled by default.

      Parameters:
      response - a response ID
      enabled - whether to enable response
      Since:
      1.5
    • setResponseLabel

      public void setResponseLabel(String response, String label)

      Sets the label of response to label.

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

      Parameters:
      response - a response ID
      label - the label of response
      Since:
      1.5
    • response

      protected void response(String response)
    • onResponse

      public SignalConnection<AlertDialog.ResponseCallback> onResponse(@Nullable String detail, AlertDialog.ResponseCallback handler)

      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 Escape or with a system action, response will be set to the value of AlertDialog:close-response.

      Parameters:
      detail - the signal detail
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      1.5
      See Also:
    • emitResponse

      public void emitResponse(@Nullable String detail, String response)
      Emits the "response" signal. See onResponse(String, AlertDialog.ResponseCallback).
    • builder

      public static AlertDialog.Builder<? extends AlertDialog.Builder> builder()
      A AlertDialog.Builder object constructs a AlertDialog with the specified properties. Use the various set...() methods to set properties, and finish construction with AlertDialog.Builder.build().
      Returns:
      the builder object