skip to Main Content

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);
}

Dialog box Image deprecated!
Dialog box deprecated

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);
}

enter image description here

Nevertheless, there is two issues:

  1. The attached image is on the top instead of the middle left.
  2. The close button doesn’t close de dialog box, it does nothing.

How can I solve these two issues?

2

Answers


  1. Chosen as BEST ANSWER

    I finally got the results that I was expecting:

    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 (GTK_DIALOG(dialog));
      box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
      label = gtk_label_new (mensaje);
    
      image = gtk_image_new();
      gtk_image_set_from_icon_name (GTK_IMAGE(image), "computer-fail", GTK_ICON_SIZE_DIALOG);
      gtk_widget_show (image);
      gtk_widget_set_halign (image, GTK_ALIGN_START);
    
      g_signal_connect_swapped (dialog,
                               "response",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog);
    
      gtk_box_pack_start (GTK_BOX(box), image, TRUE, TRUE, 10);
      gtk_box_pack_start (GTK_BOX(box), label, TRUE, TRUE, 10);
      gtk_container_add (GTK_CONTAINER(content_area), box);
    
      gtk_widget_show_all (dialog);
    }
    

    enter image description here


  2. Use GtkDialog for dialogs with images

    GTK devs want message dialogs to be text-only. If you want message dialog with an icon, they force you to do it manually:

    GtkWidget *dialog, *content_area, *box, *image;
    dialog = gtk_dialog_new_with_buttons () // create dialog
    content_area = gtk_dialog_get_content_area (dialog) // get a container to place your own widgets
    // make a GtkGrid or GtkBox, pack it there and manually with GtkImage and GtkLabel
    box = gtk_box_new (...)
    image = gtk_image_new (...)
    gtk_box_pack... (box, image)
    gtk_container_add (content_area, box) // see note below
    

    gtk_dialog_get_content_area() returns a GtkBox, thus you can use gtk_box_pack... methods on it.

    Alternatively, gtk_message_dialog_get_message_area () and pack an icon there, but it’s a vertical GtkBox.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search