I have this code to display a message dialog box:
void mensajeVentana (GtkWidget *wid, GtkWidget *win, gchar *mensaje) {
GtkWidget *dialog = NULL;
GtkWidget *image;
dialog = gtk_message_dialog_new(GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, mensaje);
image = gtk_image_new_from_icon_name ("computer-fail", GTK_ICON_SIZE_DIALOG);
gtk_widget_show (image);
gtk_message_dialog_set_image(GTK_MESSAGE_DIALOG(dialog), image);
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
When compile it, I have the following message warning:
‘gtk_message_dialog_set_image’ is deprecated [-Wdeprecated-declarations]
I’m using the following versions of gcc and GTK:
- gcc version 8.3.0 (Debian 8.3.0-6)
- GTK3, libgtk-3-0:amd64 3.24.5-1
I have researched about the replacement to ‘gtk_message_dialog_set_image’, the only help I found was the documentation that says:
gtk_message_dialog_get_image has been deprecated since version 3.12 and should not be used in newly-written code.
Use GtkDialog for dialogs with images
Gets the dialog’s image.
But apart from that I didn’t find any example about how to display an icon with a message dialog box using GTK3 not using the function: ‘gtk_message_dialog_set_image’.
Any idea?
Thanks!
UPDATE.-
I wrote the following code thanks to Alexander Dmitriev’s sugestions:
void mensajeVentana (GtkWidget *wid, GtkWidget *win, gchar *mensaje) {
GtkWidget *dialog = NULL;
GtkWidget *image, *content_area, *box, *label;
dialog = gtk_dialog_new_with_buttons ("Alerta",
GTK_WINDOW(win),
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
"Cerrar",
GTK_BUTTONS_CLOSE,
NULL);
content_area = gtk_dialog_get_content_area (dialog);
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
label = gtk_label_new (mensaje);
image = gtk_image_new();
gtk_image_set_from_icon_name (image, "computer-fail", GTK_ICON_SIZE_DIALOG);
gtk_widget_show (image);
gtk_box_pack_start (box, image, TRUE, TRUE, 1);
gtk_container_add (content_area, box);
gtk_container_add (content_area, label);
gtk_widget_show_all (dialog);
}
Nevertheless, there is two issues:
- The attached image is on the top instead of the middle left.
- The close button doesn’t close de dialog box, it does nothing.
How can I solve these two issues?
2
Answers
I finally got the results that I was expecting:
GTK devs want message dialogs to be text-only. If you want message dialog with an icon, they force you to do it manually:
gtk_dialog_get_content_area()
returns aGtkBox
, thus you can usegtk_box_pack...
methods on it.Alternatively,
gtk_message_dialog_get_message_area ()
and pack an icon there, but it’s a vertical GtkBox.