[gcr] eggimagemenubutton: Copy and un-deprecate GtkImageMenuitem



commit e7cc9b071312fb241905330344ee60562185910e
Author: Stef Walter <stefw gnome org>
Date:   Sun Jan 5 20:09:04 2014 +0100

    eggimagemenubutton: Copy and un-deprecate GtkImageMenuitem
    
    This was deprecated without reference to a bug or reasoning,
    and we need it for our importer drop down button. We want
    to show the device image in the drop down menu. So copy it
    from GTK+, clean it up, and keep using it.
    
    Add a tool to test GcrImportButton

 docs/reference/gcr/Makefile.am |    1 +
 ui/Makefile.am                 |    1 +
 ui/eggimagemenuitem.c          |  735 ++++++++++++++++++++++++++++++++++++++++
 ui/eggimagemenuitem.h          |   77 +++++
 ui/gcr-import-button.c         |    7 +-
 ui/tests/Makefile.am           |    1 +
 ui/tests/frob-import-button.c  |  253 ++++++++++++++
 7 files changed, 1072 insertions(+), 3 deletions(-)
---
diff --git a/docs/reference/gcr/Makefile.am b/docs/reference/gcr/Makefile.am
index 4e56eb8..37c1fc9 100644
--- a/docs/reference/gcr/Makefile.am
+++ b/docs/reference/gcr/Makefile.am
@@ -59,6 +59,7 @@ EXTRA_HFILES=$(top_srcdir)/ui/*.h
 # e.g. IGNORE_HFILES=gtkdebug.h gtkintl.h
 IGNORE_HFILES= \
        console-interaction.h \
+       eggimagemenuitem.h \
        gcr-callback-output-stream.h \
        gcr-certificate-exporter.h \
        gcr-certificate-basics-widget.h \
diff --git a/ui/Makefile.am b/ui/Makefile.am
index 4216d5c..67ecea9 100644
--- a/ui/Makefile.am
+++ b/ui/Makefile.am
@@ -113,6 +113,7 @@ PUBLIC_FILES = \
        $(NULL)
 
 PRIVATE_FILES = \
+       eggimagemenuitem.c eggimagemenuitem.h \
        gcr-certificate-basics-widget.c gcr-certificate-basics-widget.h \
        gcr-certificate-details-widget.c gcr-certificate-details-widget.h \
        gcr-certificate-exporter.c gcr-certificate-exporter.h \
diff --git a/ui/eggimagemenuitem.c b/ui/eggimagemenuitem.c
new file mode 100644
index 0000000..1e49c7e
--- /dev/null
+++ b/ui/eggimagemenuitem.c
@@ -0,0 +1,735 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * EggImageMenuItem was was deprecated from GTK+ without a bug containing
+ * reasoning or such. Gcr wants to use image menu items in button drop downs
+ * using the device icons (for the import button). So just copy it here :(
+ * ... with the stock and activatable sutff removed
+ */
+
+/*
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#include "config.h"
+
+#include "eggimagemenuitem.h"
+
+struct _EggImageMenuItemPrivate
+{
+  GtkWidget     *image;
+
+  gchar         *label;
+  guint          always_show_image : 1;
+  guint16        toggle_size;
+};
+
+enum {
+  PROP_0,
+  PROP_IMAGE,
+  PROP_ALWAYS_SHOW_IMAGE
+};
+
+static void egg_image_menu_item_destroy              (GtkWidget        *widget);
+static void egg_image_menu_item_get_preferred_width  (GtkWidget        *widget,
+                                                      gint             *minimum,
+                                                      gint             *natural);
+static void egg_image_menu_item_get_preferred_height (GtkWidget        *widget,
+                                                      gint             *minimum,
+                                                      gint             *natural);
+static void egg_image_menu_item_get_preferred_height_for_width (GtkWidget *widget,
+                                                                gint       width,
+                                                                gint      *minimum,
+                                                                gint      *natural);
+static void egg_image_menu_item_size_allocate        (GtkWidget        *widget,
+                                                      GtkAllocation    *allocation);
+static void egg_image_menu_item_map                  (GtkWidget        *widget);
+static void egg_image_menu_item_remove               (GtkContainer     *container,
+                                                      GtkWidget        *child);
+static void egg_image_menu_item_toggle_size_request  (GtkMenuItem      *menu_item,
+                                                      gint             *requisition);
+static void egg_image_menu_item_toggle_size_allocate (GtkMenuItem      *menu_item,
+                                                      gint              allocation);
+static void egg_image_menu_item_set_label            (GtkMenuItem      *menu_item,
+                                                      const gchar      *label);
+static const gchar * egg_image_menu_item_get_label   (GtkMenuItem *menu_item);
+
+static void egg_image_menu_item_forall               (GtkContainer    *container,
+                                                      gboolean         include_internals,
+                                                      GtkCallback      callback,
+                                                      gpointer         callback_data);
+
+static void egg_image_menu_item_finalize             (GObject         *object);
+static void egg_image_menu_item_set_property         (GObject         *object,
+                                                      guint            prop_id,
+                                                      const GValue    *value,
+                                                      GParamSpec      *pspec);
+static void egg_image_menu_item_get_property         (GObject         *object,
+                                                      guint            prop_id,
+                                                      GValue          *value,
+                                                      GParamSpec      *pspec);
+static void egg_image_menu_item_screen_changed       (GtkWidget       *widget,
+                                                      GdkScreen       *previous_screen);
+
+static void egg_image_menu_item_recalculate          (EggImageMenuItem *image_menu_item);
+
+G_DEFINE_TYPE (EggImageMenuItem, egg_image_menu_item, GTK_TYPE_MENU_ITEM);
+
+static void
+egg_image_menu_item_class_init (EggImageMenuItemClass *klass)
+{
+  GObjectClass *gobject_class = (GObjectClass*) klass;
+  GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
+  GtkMenuItemClass *menu_item_class = (GtkMenuItemClass*) klass;
+  GtkContainerClass *container_class = (GtkContainerClass*) klass;
+
+  widget_class->destroy = egg_image_menu_item_destroy;
+  widget_class->screen_changed = egg_image_menu_item_screen_changed;
+  widget_class->get_preferred_width = egg_image_menu_item_get_preferred_width;
+  widget_class->get_preferred_height = egg_image_menu_item_get_preferred_height;
+  widget_class->get_preferred_height_for_width = egg_image_menu_item_get_preferred_height_for_width;
+  widget_class->size_allocate = egg_image_menu_item_size_allocate;
+  widget_class->map = egg_image_menu_item_map;
+
+  container_class->forall = egg_image_menu_item_forall;
+  container_class->remove = egg_image_menu_item_remove;
+
+  menu_item_class->toggle_size_request = egg_image_menu_item_toggle_size_request;
+  menu_item_class->toggle_size_allocate = egg_image_menu_item_toggle_size_allocate;
+  menu_item_class->set_label           = egg_image_menu_item_set_label;
+  menu_item_class->get_label           = egg_image_menu_item_get_label;
+
+  gobject_class->finalize     = egg_image_menu_item_finalize;
+  gobject_class->set_property = egg_image_menu_item_set_property;
+  gobject_class->get_property = egg_image_menu_item_get_property;
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_IMAGE,
+                                   g_param_spec_object ("image",
+                                                        "Image widget",
+                                                        "Child widget to appear next to the menu text",
+                                                        GTK_TYPE_WIDGET,
+                                                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_ALWAYS_SHOW_IMAGE,
+                                   g_param_spec_boolean ("always-show-image",
+                                                         "Always show image",
+                                                         "Whether the image will always be shown",
+                                                         FALSE,
+                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | 
G_PARAM_CONSTRUCT));
+
+  g_type_class_add_private (gobject_class, sizeof (EggImageMenuItemPrivate));
+}
+
+static void
+egg_image_menu_item_init (EggImageMenuItem *image_menu_item)
+{
+  EggImageMenuItemPrivate *priv;
+
+  image_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (image_menu_item, EGG_TYPE_IMAGE_MENU_ITEM, 
EggImageMenuItemPrivate);
+  priv = image_menu_item->priv;
+
+  priv->image = NULL;
+  priv->label  = NULL;
+}
+
+static void
+egg_image_menu_item_finalize (GObject *object)
+{
+  EggImageMenuItemPrivate *priv = EGG_IMAGE_MENU_ITEM (object)->priv;
+
+  g_free (priv->label);
+  priv->label  = NULL;
+
+  G_OBJECT_CLASS (egg_image_menu_item_parent_class)->finalize (object);
+}
+
+static void
+egg_image_menu_item_set_property (GObject         *object,
+                                  guint            prop_id,
+                                  const GValue    *value,
+                                  GParamSpec      *pspec)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (object);
+
+  switch (prop_id)
+    {
+    case PROP_IMAGE:
+      egg_image_menu_item_set_image (image_menu_item, (GtkWidget *) g_value_get_object (value));
+      break;
+    case PROP_ALWAYS_SHOW_IMAGE:
+      egg_image_menu_item_set_always_show_image (image_menu_item, g_value_get_boolean (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+egg_image_menu_item_get_property (GObject         *object,
+                                  guint            prop_id,
+                                  GValue          *value,
+                                  GParamSpec      *pspec)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (object);
+
+  switch (prop_id)
+    {
+    case PROP_IMAGE:
+      g_value_set_object (value, egg_image_menu_item_get_image (image_menu_item));
+      break;
+    case PROP_ALWAYS_SHOW_IMAGE:
+      g_value_set_boolean (value, egg_image_menu_item_get_always_show_image (image_menu_item));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static gboolean
+show_image (EggImageMenuItem *image_menu_item)
+{
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+  GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (image_menu_item));
+  gboolean show;
+
+  if (priv->always_show_image)
+    show = TRUE;
+  else
+    g_object_get (settings, "gtk-menu-images", &show, NULL);
+
+  return show;
+}
+
+static void
+egg_image_menu_item_map (GtkWidget *widget)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (widget);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+
+  GTK_WIDGET_CLASS (egg_image_menu_item_parent_class)->map (widget);
+
+  if (priv->image)
+    g_object_set (priv->image,
+                  "visible", show_image (image_menu_item),
+                  NULL);
+}
+
+static void
+egg_image_menu_item_destroy (GtkWidget *widget)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (widget);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+
+  if (priv->image)
+    gtk_container_remove (GTK_CONTAINER (image_menu_item),
+                          priv->image);
+
+  GTK_WIDGET_CLASS (egg_image_menu_item_parent_class)->destroy (widget);
+}
+
+static void
+egg_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
+                                         gint        *requisition)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (menu_item);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+  GtkPackDirection pack_dir;
+  GtkWidget *parent;
+  GtkWidget *widget = GTK_WIDGET (menu_item);
+
+  parent = gtk_widget_get_parent (widget);
+
+  if (GTK_IS_MENU_BAR (parent))
+    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
+  else
+    pack_dir = GTK_PACK_DIRECTION_LTR;
+
+  *requisition = 0;
+
+  if (priv->image && gtk_widget_get_visible (priv->image))
+    {
+      GtkRequisition image_requisition;
+      guint toggle_spacing;
+
+      gtk_widget_get_preferred_size (priv->image, &image_requisition, NULL);
+
+      gtk_widget_style_get (GTK_WIDGET (menu_item),
+                            "toggle-spacing", &toggle_spacing,
+                            NULL);
+
+      if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
+        {
+          if (image_requisition.width > 0)
+            *requisition = image_requisition.width + toggle_spacing;
+        }
+      else
+        {
+          if (image_requisition.height > 0)
+            *requisition = image_requisition.height + toggle_spacing;
+        }
+    }
+}
+
+static void
+egg_image_menu_item_toggle_size_allocate (GtkMenuItem      *menu_item,
+                                          gint              allocation)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (menu_item);
+  image_menu_item->priv->toggle_size = allocation;
+  GTK_MENU_ITEM_CLASS (egg_image_menu_item_parent_class)->toggle_size_allocate (menu_item, allocation);
+}
+
+static void
+egg_image_menu_item_recalculate (EggImageMenuItem *image_menu_item)
+{
+  EggImageMenuItemPrivate    *priv = image_menu_item->priv;
+
+  GTK_MENU_ITEM_CLASS
+    (egg_image_menu_item_parent_class)->set_label (GTK_MENU_ITEM (image_menu_item), priv->label);
+}
+
+static void
+egg_image_menu_item_set_label (GtkMenuItem      *menu_item,
+                               const gchar      *label)
+{
+  EggImageMenuItemPrivate *priv = EGG_IMAGE_MENU_ITEM (menu_item)->priv;
+
+  if (priv->label != label)
+    {
+      g_free (priv->label);
+      priv->label = g_strdup (label);
+
+      egg_image_menu_item_recalculate (EGG_IMAGE_MENU_ITEM (menu_item));
+
+      g_object_notify (G_OBJECT (menu_item), "label");
+
+    }
+}
+
+static const gchar *
+egg_image_menu_item_get_label (GtkMenuItem *menu_item)
+{
+  EggImageMenuItemPrivate *priv = EGG_IMAGE_MENU_ITEM (menu_item)->priv;
+
+  return priv->label;
+}
+
+static void
+egg_image_menu_item_get_preferred_width (GtkWidget        *widget,
+                                         gint             *minimum,
+                                         gint             *natural)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (widget);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+  GtkPackDirection pack_dir;
+  GtkWidget *parent;
+
+  parent = gtk_widget_get_parent (widget);
+
+  if (GTK_IS_MENU_BAR (parent))
+    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
+  else
+    pack_dir = GTK_PACK_DIRECTION_LTR;
+
+  GTK_WIDGET_CLASS (egg_image_menu_item_parent_class)->get_preferred_width (widget, minimum, natural);
+
+  if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
+      priv->image &&
+      gtk_widget_get_visible (priv->image))
+    {
+      gint child_minimum, child_natural;
+
+      gtk_widget_get_preferred_width (priv->image, &child_minimum, &child_natural);
+
+      *minimum = MAX (*minimum, child_minimum);
+      *natural = MAX (*natural, child_natural);
+    }
+}
+
+static void
+egg_image_menu_item_get_preferred_height (GtkWidget        *widget,
+                                          gint             *minimum,
+                                          gint             *natural)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (widget);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+  gint child_height = 0;
+  GtkPackDirection pack_dir;
+  GtkWidget *parent;
+
+  parent = gtk_widget_get_parent (widget);
+
+  if (GTK_IS_MENU_BAR (parent))
+    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
+  else
+    pack_dir = GTK_PACK_DIRECTION_LTR;
+
+  if (priv->image && gtk_widget_get_visible (priv->image))
+    {
+      GtkRequisition child_requisition;
+
+      gtk_widget_get_preferred_size (priv->image, &child_requisition, NULL);
+
+      child_height = child_requisition.height;
+    }
+
+  GTK_WIDGET_CLASS (egg_image_menu_item_parent_class)->get_preferred_height (widget, minimum, natural);
+
+  if (pack_dir == GTK_PACK_DIRECTION_RTL || pack_dir == GTK_PACK_DIRECTION_LTR)
+    {
+      *minimum = MAX (*minimum, child_height);
+      *natural = MAX (*natural, child_height);
+    }
+}
+
+static void
+egg_image_menu_item_get_preferred_height_for_width (GtkWidget        *widget,
+                                                    gint              width,
+                                                    gint             *minimum,
+                                                    gint             *natural)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (widget);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+  gint child_height = 0;
+  GtkPackDirection pack_dir;
+  GtkWidget *parent;
+
+  parent = gtk_widget_get_parent (widget);
+
+  if (GTK_IS_MENU_BAR (parent))
+    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
+  else
+    pack_dir = GTK_PACK_DIRECTION_LTR;
+
+  if (priv->image && gtk_widget_get_visible (priv->image))
+    {
+      GtkRequisition child_requisition;
+
+      gtk_widget_get_preferred_size (priv->image, &child_requisition, NULL);
+
+      child_height = child_requisition.height;
+    }
+
+  GTK_WIDGET_CLASS
+    (egg_image_menu_item_parent_class)->get_preferred_height_for_width (widget, width, minimum, natural);
+
+  if (pack_dir == GTK_PACK_DIRECTION_RTL || pack_dir == GTK_PACK_DIRECTION_LTR)
+    {
+      *minimum = MAX (*minimum, child_height);
+      *natural = MAX (*natural, child_height);
+    }
+}
+
+
+static void
+egg_image_menu_item_size_allocate (GtkWidget     *widget,
+                                   GtkAllocation *allocation)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (widget);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+  GtkAllocation widget_allocation;
+  GtkPackDirection pack_dir;
+  GtkWidget *parent;
+
+  parent = gtk_widget_get_parent (widget);
+
+  if (GTK_IS_MENU_BAR (parent))
+    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
+  else
+    pack_dir = GTK_PACK_DIRECTION_LTR;
+
+  GTK_WIDGET_CLASS (egg_image_menu_item_parent_class)->size_allocate (widget, allocation);
+
+  if (priv->image && gtk_widget_get_visible (priv->image))
+    {
+      gint x, y, offset;
+      GtkStyleContext *context;
+      GtkStateFlags state;
+      GtkBorder padding;
+      GtkRequisition child_requisition;
+      GtkAllocation child_allocation;
+      guint horizontal_padding, toggle_spacing;
+      gint toggle_size;
+
+      toggle_size = priv->toggle_size;
+      gtk_widget_style_get (widget,
+                            "horizontal-padding", &horizontal_padding,
+                            "toggle-spacing", &toggle_spacing,
+                            NULL);
+
+      /* Man this is lame hardcoding action, but I can't
+       * come up with a solution that's really better.
+       */
+
+      gtk_widget_get_preferred_size (priv->image, &child_requisition, NULL);
+
+      gtk_widget_get_allocation (widget, &widget_allocation);
+
+      context = gtk_widget_get_style_context (widget);
+      state = gtk_widget_get_state_flags (widget);
+      gtk_style_context_get_padding (context, state, &padding);
+      offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item));
+
+      if (pack_dir == GTK_PACK_DIRECTION_LTR ||
+          pack_dir == GTK_PACK_DIRECTION_RTL)
+        {
+          if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
+              (pack_dir == GTK_PACK_DIRECTION_LTR))
+            x = offset + horizontal_padding + padding.left +
+               (toggle_size - toggle_spacing - child_requisition.width) / 2;
+          else
+            x = widget_allocation.width - offset - horizontal_padding - padding.right -
+              toggle_size + toggle_spacing +
+              (toggle_size - toggle_spacing - child_requisition.width) / 2;
+
+          y = (widget_allocation.height - child_requisition.height) / 2;
+        }
+      else
+        {
+          if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
+              (pack_dir == GTK_PACK_DIRECTION_TTB))
+            y = offset + horizontal_padding + padding.top +
+              (toggle_size - toggle_spacing - child_requisition.height) / 2;
+          else
+            y = widget_allocation.height - offset - horizontal_padding - padding.bottom -
+              toggle_size + toggle_spacing +
+              (toggle_size - toggle_spacing - child_requisition.height) / 2;
+
+          x = (widget_allocation.width - child_requisition.width) / 2;
+        }
+
+      child_allocation.width = child_requisition.width;
+      child_allocation.height = child_requisition.height;
+      child_allocation.x = widget_allocation.x + MAX (x, 0);
+      child_allocation.y = widget_allocation.y + MAX (y, 0);
+
+      gtk_widget_size_allocate (priv->image, &child_allocation);
+    }
+}
+
+static void
+egg_image_menu_item_forall (GtkContainer   *container,
+                            gboolean        include_internals,
+                            GtkCallback     callback,
+                            gpointer        callback_data)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (container);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+
+  GTK_CONTAINER_CLASS (egg_image_menu_item_parent_class)->forall (container,
+                                                                  include_internals,
+                                                                  callback,
+                                                                  callback_data);
+
+  if (include_internals && priv->image)
+    (* callback) (priv->image, callback_data);
+}
+
+GtkWidget*
+egg_image_menu_item_new (void)
+{
+  return g_object_new (EGG_TYPE_IMAGE_MENU_ITEM, NULL);
+}
+
+GtkWidget*
+egg_image_menu_item_new_with_label (const gchar *label)
+{
+  return g_object_new (EGG_TYPE_IMAGE_MENU_ITEM,
+                       "label", label,
+                       NULL);
+}
+
+GtkWidget*
+egg_image_menu_item_new_with_mnemonic (const gchar *label)
+{
+  return g_object_new (EGG_TYPE_IMAGE_MENU_ITEM,
+                       "use-underline", TRUE,
+                       "label", label,
+                       NULL);
+}
+
+void
+egg_image_menu_item_set_always_show_image (EggImageMenuItem *image_menu_item,
+                                           gboolean          always_show)
+{
+  EggImageMenuItemPrivate *priv;
+
+  g_return_if_fail (EGG_IS_IMAGE_MENU_ITEM (image_menu_item));
+
+  priv = image_menu_item->priv;
+
+  if (priv->always_show_image != always_show)
+    {
+      priv->always_show_image = always_show;
+
+      if (priv->image)
+        {
+          if (show_image (image_menu_item))
+            gtk_widget_show (priv->image);
+          else
+            gtk_widget_hide (priv->image);
+        }
+
+      g_object_notify (G_OBJECT (image_menu_item), "always-show-image");
+    }
+}
+
+gboolean
+egg_image_menu_item_get_always_show_image (EggImageMenuItem *image_menu_item)
+{
+  g_return_val_if_fail (EGG_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);
+
+  return image_menu_item->priv->always_show_image;
+}
+
+void
+egg_image_menu_item_set_image (EggImageMenuItem *image_menu_item,
+                               GtkWidget        *image)
+{
+  EggImageMenuItemPrivate *priv;
+
+  g_return_if_fail (EGG_IS_IMAGE_MENU_ITEM (image_menu_item));
+
+  priv = image_menu_item->priv;
+
+  if (image == priv->image)
+    return;
+
+  if (priv->image)
+    gtk_container_remove (GTK_CONTAINER (image_menu_item),
+                          priv->image);
+
+  priv->image = image;
+
+  if (image == NULL)
+    return;
+
+  gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
+  g_object_set (image,
+                "visible", show_image (image_menu_item),
+                "no-show-all", TRUE,
+                NULL);
+
+  g_object_notify (G_OBJECT (image_menu_item), "image");
+}
+
+GtkWidget*
+egg_image_menu_item_get_image (EggImageMenuItem *image_menu_item)
+{
+  g_return_val_if_fail (EGG_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
+
+  return image_menu_item->priv->image;
+}
+
+static void
+egg_image_menu_item_remove (GtkContainer *container,
+                            GtkWidget    *child)
+{
+  EggImageMenuItem *image_menu_item = EGG_IMAGE_MENU_ITEM (container);
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+
+  if (child == priv->image)
+    {
+      gboolean widget_was_visible;
+
+      widget_was_visible = gtk_widget_get_visible (child);
+
+      gtk_widget_unparent (child);
+      priv->image = NULL;
+
+      if (widget_was_visible &&
+          gtk_widget_get_visible (GTK_WIDGET (container)))
+        gtk_widget_queue_resize (GTK_WIDGET (container));
+
+      g_object_notify (G_OBJECT (image_menu_item), "image");
+    }
+  else
+    {
+      GTK_CONTAINER_CLASS (egg_image_menu_item_parent_class)->remove (container, child);
+    }
+}
+
+static void
+show_image_change_notify (EggImageMenuItem *image_menu_item)
+{
+  EggImageMenuItemPrivate *priv = image_menu_item->priv;
+
+  if (priv->image)
+    {
+      if (show_image (image_menu_item))
+        gtk_widget_show (priv->image);
+      else
+        gtk_widget_hide (priv->image);
+    }
+}
+
+static void
+traverse_container (GtkWidget *widget,
+                    gpointer   data)
+{
+  if (EGG_IS_IMAGE_MENU_ITEM (widget))
+    show_image_change_notify (EGG_IMAGE_MENU_ITEM (widget));
+  else if (GTK_IS_CONTAINER (widget))
+    gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
+}
+
+static void
+egg_image_menu_item_setting_changed (GtkSettings *settings)
+{
+  GList *list, *l;
+
+  list = gtk_window_list_toplevels ();
+
+  for (l = list; l; l = l->next)
+    gtk_container_forall (GTK_CONTAINER (l->data),
+                          traverse_container, NULL);
+
+  g_list_free (list);
+}
+
+static void
+egg_image_menu_item_screen_changed (GtkWidget *widget,
+                                    GdkScreen *previous_screen)
+{
+  GtkSettings *settings;
+  gulong show_image_connection;
+
+  if (!gtk_widget_has_screen (widget))
+    return;
+
+  settings = gtk_widget_get_settings (widget);
+
+  show_image_connection =
+    g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
+                           NULL, egg_image_menu_item_setting_changed, NULL);
+
+  if (show_image_connection)
+    return;
+
+  g_signal_connect (settings, "notify::gtk-menu-images",
+                    G_CALLBACK (egg_image_menu_item_setting_changed), NULL);
+
+  show_image_change_notify (EGG_IMAGE_MENU_ITEM (widget));
+}
diff --git a/ui/eggimagemenuitem.h b/ui/eggimagemenuitem.h
new file mode 100644
index 0000000..bc1cfae
--- /dev/null
+++ b/ui/eggimagemenuitem.h
@@ -0,0 +1,77 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * EggImageMenuItem was was deprecated from GTK+ without a bug containing
+ * reasoning or such. Gcr wants to use image menu items in button drop downs
+ * using the device icons (for the import button). So just copy it here :(
+ * ... with the stock and activatable sutff removed
+ */
+
+/*
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#ifndef __EGG_IMAGE_MENUITEM_H__
+#define __EGG_IMAGE_MENUITEM_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define EGG_TYPE_IMAGE_MENU_ITEM            (egg_image_menu_item_get_type ())
+#define EGG_IMAGE_MENU_ITEM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_IMAGE_MENU_ITEM, 
EggImageMenuItem))
+#define EGG_IMAGE_MENU_ITEM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_IMAGE_MENU_ITEM, 
EggImageMenuItemClass))
+#define EGG_IS_IMAGE_MENU_ITEM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_IMAGE_MENU_ITEM))
+#define EGG_IS_IMAGE_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EGG_TYPE_IMAGE_MENU_ITEM))
+#define EGG_IMAGE_MENU_ITEM_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), EGG_TYPE_IMAGE_MENU_ITEM, 
EggImageMenuItemClass))
+
+
+typedef struct _EggImageMenuItem              EggImageMenuItem;
+typedef struct _EggImageMenuItemPrivate       EggImageMenuItemPrivate;
+typedef struct _EggImageMenuItemClass         EggImageMenuItemClass;
+
+struct _EggImageMenuItem
+{
+  GtkMenuItem menu_item;
+
+  /*< private >*/
+  EggImageMenuItemPrivate *priv;
+};
+
+struct _EggImageMenuItemClass
+{
+  GtkMenuItemClass parent_class;
+};
+
+GType     egg_image_menu_item_get_type          (void) G_GNUC_CONST;
+GtkWidget* egg_image_menu_item_new               (void);
+GtkWidget* egg_image_menu_item_new_with_label    (const gchar      *label);
+GtkWidget* egg_image_menu_item_new_with_mnemonic (const gchar      *label);
+void       egg_image_menu_item_set_always_show_image (EggImageMenuItem *image_menu_item,
+                                                      gboolean          always_show);
+gboolean   egg_image_menu_item_get_always_show_image (EggImageMenuItem *image_menu_item);
+void       egg_image_menu_item_set_image         (EggImageMenuItem *image_menu_item,
+                                                  GtkWidget        *image);
+GtkWidget* egg_image_menu_item_get_image         (EggImageMenuItem *image_menu_item);
+
+G_END_DECLS
+
+#endif /* __EGG_IMAGE_MENUITEM_H__ */
diff --git a/ui/gcr-import-button.c b/ui/gcr-import-button.c
index cc0a161..3741340 100644
--- a/ui/gcr-import-button.c
+++ b/ui/gcr-import-button.c
@@ -28,6 +28,7 @@
 #include "gcr/gcr-marshal.h"
 #include "gcr/gcr-parser.h"
 
+#include "eggimagemenuitem.h"
 #include "gcr-import-button.h"
 #include "gcr-pkcs11-import-interaction.h"
 
@@ -372,12 +373,12 @@ update_importer_menu (GcrImportButton *self)
 
        for (l = self->pv->importers; l != NULL; l = g_list_next (l)) {
                g_object_get (l->data, "label", &label, "icon", &icon, NULL);
-               menu_item = gtk_image_menu_item_new_with_label (label);
+               menu_item = egg_image_menu_item_new_with_label (label);
                g_signal_connect (menu_item, "activate", G_CALLBACK (on_importer_menu_activated), self);
                g_object_set_qdata (G_OBJECT (menu_item), QUARK_IMPORTER, l->data);
                image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
-               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
-               gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menu_item), TRUE);
+               egg_image_menu_item_set_image (EGG_IMAGE_MENU_ITEM (menu_item), image);
+               egg_image_menu_item_set_always_show_image (EGG_IMAGE_MENU_ITEM (menu_item), TRUE);
                gtk_widget_show (image);
                gtk_widget_show (menu_item);
                gtk_container_add (GTK_CONTAINER (self->pv->menu), menu_item);
diff --git a/ui/tests/Makefile.am b/ui/tests/Makefile.am
index 8b6e34b..287b9dc 100644
--- a/ui/tests/Makefile.am
+++ b/ui/tests/Makefile.am
@@ -28,6 +28,7 @@ noinst_PROGRAMS = \
        frob-certificate \
        frob-combo-selector \
        frob-gnupg-selector \
+       frob-import-button \
        frob-key \
        frob-tree-selector \
        frob-prompt \
diff --git a/ui/tests/frob-import-button.c b/ui/tests/frob-import-button.c
new file mode 100644
index 0000000..75102c8
--- /dev/null
+++ b/ui/tests/frob-import-button.c
@@ -0,0 +1,253 @@
+/*
+ * Copyright (C) 2014 Stef Walter
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Author: Stef Walter <stefw redhat com>
+ */
+
+#include "config.h"
+
+#include "ui/gcr-ui.h"
+
+#include "gcr/gcr-importer.h"
+
+#include <gtk/gtk.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+typedef GObject MockImporter;
+typedef GObjectClass MockImporterClass;
+
+enum {
+       PROP_0,
+       PROP_LABEL,
+       PROP_ICON,
+       PROP_URI,
+       PROP_INTERACTION
+};
+
+GType mock_importer_get_type (void) G_GNUC_CONST;
+
+static GList *
+mock_importer_create_for_parsed (GcrParsed *parsed)
+{
+       GcrImporter *self;
+       GIcon *icon;
+
+       icon = g_themed_icon_new ("dialog-warning");
+       self = g_object_new (mock_importer_get_type (),
+                            "label", gcr_parsed_get_label (parsed),
+                            "icon", icon,
+                            NULL);
+       g_object_unref (icon);
+       return g_list_append (NULL, self);
+}
+
+static gboolean
+mock_importer_queue_for_parsed (GcrImporter *importer,
+                                GcrParsed *parsed)
+{
+       return TRUE;
+}
+
+static void
+mock_importer_import_async (GcrImporter *importer,
+                            GCancellable *cancellable,
+                            GAsyncReadyCallback callback,
+                            gpointer user_data)
+{
+       GSimpleAsyncResult *res;
+
+       res = g_simple_async_result_new (G_OBJECT (importer), callback, user_data,
+                                        mock_importer_import_async);
+
+       g_printerr ("Import %p\n", importer);
+       g_simple_async_result_complete_in_idle (res);
+       g_object_unref (res);
+}
+
+
+static gboolean
+mock_importer_import_finish (GcrImporter *importer,
+                             GAsyncResult *result,
+                             GError **error)
+{
+       return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error);
+}
+
+
+static void
+mock_importer_iface (GcrImporterIface *iface)
+{
+       iface->create_for_parsed = mock_importer_create_for_parsed;
+       iface->queue_for_parsed = mock_importer_queue_for_parsed;
+       iface->import_async = mock_importer_import_async;
+       iface->import_finish = mock_importer_import_finish;
+}
+
+G_DEFINE_TYPE_WITH_CODE (MockImporter, mock_importer, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (GCR_TYPE_IMPORTER, mock_importer_iface);
+);
+
+static void
+value_free (gpointer value)
+{
+       g_boxed_free (G_TYPE_VALUE, value);
+}
+
+static void
+mock_importer_init (MockImporter *self)
+{
+
+}
+
+static void
+mock_importer_set_property (GObject *obj,
+                            guint prop_id,
+                            const GValue *value,
+                            GParamSpec *pspec)
+{
+       g_object_set_data_full (obj, pspec->name, g_boxed_copy (G_TYPE_VALUE, value), value_free);
+}
+
+static void
+mock_importer_get_property (GObject *obj,
+                            guint prop_id,
+                            GValue *value,
+                            GParamSpec *pspec)
+{
+       GValue *val = g_object_get_data (obj, pspec->name);
+       g_return_if_fail (val != NULL);
+       g_value_copy (val, value);
+}
+
+static void
+mock_importer_class_init (MockImporterClass *klass)
+{
+       GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+       gobject_class->set_property = mock_importer_set_property;
+       gobject_class->get_property = mock_importer_get_property;
+
+       g_object_class_install_property (gobject_class, PROP_LABEL,
+                  g_param_spec_string ("label", "", "",
+                                       NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+       g_object_class_install_property (gobject_class, PROP_ICON,
+                  g_param_spec_object ("icon", "", "",
+                                       G_TYPE_ICON, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+       g_object_class_override_property (gobject_class, PROP_URI, "uri");
+       g_object_class_override_property (gobject_class, PROP_INTERACTION, "interaction");
+}
+
+typedef MockImporter MockImporterTwo;
+typedef MockImporterClass MockImporterTwoClass;
+
+GType mock_importer_two_get_type (void) G_GNUC_CONST;
+
+G_DEFINE_TYPE (MockImporterTwo, mock_importer_two, mock_importer_get_type ());
+
+static void
+mock_importer_two_init (MockImporterTwo *self)
+{
+
+}
+
+static void
+mock_importer_two_class_init (MockImporterTwoClass *klass)
+{
+
+}
+
+
+static void
+on_parser_parsed (GcrParser *parser,
+                  gpointer user_data)
+{
+       gcr_import_button_add_parsed (user_data,
+                                     gcr_parser_get_parsed (parser));
+}
+
+static void
+parse_file (GcrParser *parser,
+            const gchar *path)
+{
+       GError *err = NULL;
+       guchar *data;
+       gsize n_data;
+       GBytes *bytes;
+
+       if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
+               g_error ("couldn't read file: %s", path);
+
+       bytes = g_bytes_new_take (data, n_data);
+       if (!gcr_parser_parse_bytes (parser, bytes, &err))
+               g_error ("couldn't parse data: %s", err->message);
+
+       g_bytes_unref (bytes);
+}
+
+int
+main (int argc, char *argv[])
+{
+       GtkDialog *dialog;
+       GcrParser *parser;
+       GtkWidget *button;
+       GtkWidget *align;
+       int i;
+
+       gtk_init (&argc, &argv);
+
+       gcr_importer_register (mock_importer_get_type (), gck_attributes_new (0));
+       gcr_importer_register (mock_importer_two_get_type (), gck_attributes_new (0));
+
+       dialog = GTK_DIALOG (gtk_dialog_new ());
+       g_object_ref_sink (dialog);
+
+       button = GTK_WIDGET (gcr_import_button_new ("Import Button"));
+       gtk_widget_show (button);
+
+       align = gtk_alignment_new (0.5, 0.5, 0, 0);
+       gtk_container_add (GTK_CONTAINER (align), button);
+       gtk_widget_show (align);
+
+       gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (dialog)), align);
+
+       gtk_window_set_default_size (GTK_WINDOW (dialog), 200, 300);
+       gtk_container_set_border_width (GTK_CONTAINER (dialog), 20);
+
+       parser = gcr_parser_new ();
+       g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), button);
+
+       if (argc == 1) {
+               parse_file (parser, SRCDIR "/files/ca-certificates.crt");
+       } else {
+               for (i = 1; i < argc; ++i)
+                       parse_file (parser, argv[i]);
+       }
+
+       g_object_unref (parser);
+
+       gtk_dialog_run (dialog);
+
+       gtk_widget_destroy (GTK_WIDGET (dialog));
+       g_object_unref (dialog);
+
+       return 0;
+}



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