[gimp/gtk3-port] app: add utility functions gimp_widget_get, set_image()



commit 7bd7a4ad7b9dde64d1dd60bba627f4b2306ba9d7
Author: Michael Natterer <mitch gimp org>
Date:   Wed May 2 14:50:22 2018 +0200

    app: add utility functions gimp_widget_get,set_image()
    
    as replacement for the deprecated GtkImageMenuItem, for the few cases
    where we really want an icon/image in a menu item.

 app/widgets/gimpwidgets-utils.c |   61 +++++++++++++++++++++++++++++++++++++++
 app/widgets/gimpwidgets-utils.h |    4 ++
 2 files changed, 65 insertions(+), 0 deletions(-)
---
diff --git a/app/widgets/gimpwidgets-utils.c b/app/widgets/gimpwidgets-utils.c
index 7ad4483..14f001f 100644
--- a/app/widgets/gimpwidgets-utils.c
+++ b/app/widgets/gimpwidgets-utils.c
@@ -57,6 +57,67 @@
 #define GIMP_TOOL_OPTIONS_GUI_KEY "gimp-tool-options-gui"
 
 
+GtkWidget *
+gimp_menu_item_get_image (GtkMenuItem *item)
+{
+  g_return_val_if_fail (GTK_IS_MENU_ITEM (item), NULL);
+
+  return g_object_get_data (G_OBJECT (item), "gimp-menu-item-image");
+}
+
+void
+gimp_menu_item_set_image (GtkMenuItem *item,
+                          GtkWidget   *image)
+{
+  GtkWidget *hbox;
+  GtkWidget *label;
+  GtkWidget *old_image;
+
+  g_return_if_fail (GTK_IS_MENU_ITEM (item));
+  g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
+
+  hbox = g_object_get_data (G_OBJECT (item), "gimp-menu-item-hbox");
+
+  if (! hbox)
+    {
+      if (! image)
+        return;
+
+      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
+      g_object_set_data (G_OBJECT (hbox), "gimp-menu-item-hbox", hbox);
+
+      label = gtk_bin_get_child (GTK_BIN (item));
+      g_object_set_data (G_OBJECT (hbox), "gimp-menu-item-label", label);
+
+      g_object_ref (label);
+      gtk_container_remove (GTK_CONTAINER (item), label);
+      gtk_container_add (GTK_CONTAINER (hbox), label);
+      g_object_unref (label);
+
+      gtk_container_add (GTK_CONTAINER (item), hbox);
+      gtk_widget_show (hbox);
+    }
+
+  old_image = g_object_get_data (G_OBJECT (item), "gimp-menu-item-image");
+
+  if (old_image != image)
+    {
+      if (old_image)
+        {
+          gtk_widget_destroy (old_image);
+          g_object_set_data (G_OBJECT (item), "gimp-menu-item-image", NULL);
+        }
+
+      if (image)
+        {
+          gtk_container_add (GTK_CONTAINER (hbox), image);
+          gtk_box_reorder_child (GTK_BOX (hbox), image, 0);
+          g_object_set_data (G_OBJECT (item), "gimp-menu-item-image", image);
+          gtk_widget_show (image);
+        }
+    }
+}
+
 /**
  * gimp_menu_position:
  * @menu: a #GtkMenu widget
diff --git a/app/widgets/gimpwidgets-utils.h b/app/widgets/gimpwidgets-utils.h
index 7bda22c..f2f6453 100644
--- a/app/widgets/gimpwidgets-utils.h
+++ b/app/widgets/gimpwidgets-utils.h
@@ -22,6 +22,10 @@
 #define __APP_GIMP_WIDGETS_UTILS_H__
 
 
+GtkWidget       * gimp_menu_item_get_image         (GtkMenuItem          *item);
+void              gimp_menu_item_set_image         (GtkMenuItem          *item,
+                                                    GtkWidget            *image);
+
 void              gimp_menu_position               (GtkMenu              *menu,
                                                     gint                 *x,
                                                     gint                 *y);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]