‘gtk_message_dialog_set_image’ 已弃用。

huangapple go评论143阅读模式
英文:

‘gtk_message_dialog_set_image’ is deprecated

问题

以下是代码部分的中文翻译:

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

以下是更新后的代码的中文翻译:

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

请注意,代码中的一些 HTML 实体已被更正为原始字符,并且用于引号的字符已被更改。如果您有任何问题或需要进一步的帮助,请告诉我。

英文:

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!
‘gtk_message_dialog_set_image’ 已弃用。

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

‘gtk_message_dialog_set_image’ 已弃用。

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?

答案1

得分: 2

我终于得到了我期望的结果:

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

‘gtk_message_dialog_set_image’ 已弃用。

英文:

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

‘gtk_message_dialog_set_image’ 已弃用。

答案2

得分: 1

使用 GtkDialog 来创建带有图像的对话框

GTK 开发者希望消息对话框只包含文本。如果你想要带有图标的消息对话框,他们强制你手动实现:

GtkWidget *dialog, *content_area, *box, *image;
dialog = gtk_dialog_new_with_buttons () // 创建对话框
content_area = gtk_dialog_get_content_area (dialog) // 获取容器以放置你自己的小部件
// 创建一个 GtkGrid 或 GtkBox,将它们打包并手动添加 GtkImage 和 GtkLabel
box = gtk_box_new (...);
image = gtk_image_new (...);
gtk_box_pack... (box, image);
gtk_container_add (content_area, box); // 请参考下面的注释

gtk_dialog_get_content_area() 返回一个 GtkBox,因此你可以在其上使用 gtk_box_pack... 方法。

或者,可以使用 gtk_message_dialog_get_message_area () 并在其中添加图标,但它是一个垂直的 GtkBox。

英文:

> 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.

huangapple
  • 本文由 发表于 2020年1月7日 02:42:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定