Moving away from GtkMessageDialog – How?

GtkMessageDialog was deprecated, so I would like to update my code accordingly. But what are the future plans? How should developers update their code? Is the CSS interface of GtkMessageDialog going to remain or will it be removed too? At the moment it is possible to make an exact clone of GtkMessageDialog by simply using GtkWindow and assigning the appropriate CSS classes ("dialog", "message", "dialog-action-area", and so on). But is that going to last or will these CSS rules be removed as well?

Deprecate API is guaranteed to exist for the entire duration of the same major API series; anything that gets deprecated in GTK4 can only be removed in GTK5. It’s the same rule for everything: GtkTreeView or GtkMessageDialog.

For message dialogs, the recommendation is to use the GtkAlertDialog API; if you have your own message dialog based on modifying the contents of a GtkMessageDialog then you should write a replacement with GtkWindow and your own handlers for buttons and actions. Libadwaita has a fairly comprehensive message dialog API that you can use. Once you decide what to do, you should also decide what kind of style to impose; you should never rely on what the system’s style provides, unless you’re the one also providing the system’s style—like libadwaita does.

Thank you, Emmanuele. So there is a replacement! But what is the equivalent of the old dialog with a "destructive-action" button? Or, in general, how can I add CSS classes to the buttons shown by GtkAlertDialog? In the past I could simply write

GtkWidget * const question_dialog = gtk_message_dialog_new(
	parent,
	0,
	GTK_MESSAGE_QUESTION,
	GTK_BUTTONS_OK_CANCEL,
	"Bla bla bla"
);

gtk_window_set_modal(GTK_WINDOW(question_dialog), true);

gtk_message_dialog_format_secondary_text(
	GTK_MESSAGE_DIALOG(question_dialog),
	"Bla bla bla"
);

GtkWidget * const destructive_button =
	gtk_dialog_get_widget_for_response(
		GTK_DIALOG(question_dialog),
		GTK_RESPONSE_OK
	);

gtk_dialog_set_default_response(GTK_DIALOG(question_dialog), GTK_RESPONSE_OK);
gtk_widget_add_css_class(destructive_button, "destructive-action");

and I would get:

destructive-ok-cancel

But now?

If you’re relying on “destructive-action” then you’re relying on the GNOME system theme, which means you should be using libadwaita.

If you’re using libadwaita, then you should be using AdwMessageDialog.

Alternatively, you can build your own message window with a destructive-action button.

Ok, thank you. I am already using libadwaita, so AdwMessageDialog seems to be the way (I assume it will let me set a button for a destructive action). I will have to study the code though, it seems to be a complex API.

adw_message_dialog_set_response_appearance (dialog,
                                            "ok",
                                            ADW_RESPONSE_DESTRUCTIVE);

It’s an string-based API, instead of using integers. Other than that, it follows the GTK4 dialog object API, with an async-based model:

  • create the object
  • set up the response identifiers for each button
  • call the choose() method with an async callback
  • in the callback, finish the async action and get the response identifier for the button

There’s a fairly comprehensive example in the documentation.

1 Like

The way I am using GtkMessageDialog right now is also async, so that will not be a big change.

Thank you, that is exactly what I needed.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.