[gtk+] gadget: Add builtin icon gadget



commit 9d56a076cc562913e20f422aad9bfbb19d393518
Author: Benjamin Otte <otte redhat com>
Date:   Wed Dec 16 04:08:08 2015 +0100

    gadget: Add builtin icon gadget
    
    This is to be used in all the places where we now call
    gtk_render_activity()/option()/check() etc that in turn call the icon
    render function.

 gtk/Makefile.am             |    2 +
 gtk/gtkbuiltinicon.c        |  203 +++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkbuiltiniconprivate.h |   65 ++++++++++++++
 gtk/gtkspinner.c            |   77 +---------------
 4 files changed, 274 insertions(+), 73 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 51566ac..f0a9099 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -366,6 +366,7 @@ gtk_private_h_sources =             \
        gtkbookmarksmanager.h   \
        gtkboxprivate.h         \
        gtkbuilderprivate.h     \
+       gtkbuiltiniconprivate.h \
        gtkbuttonprivate.h      \
        gtkcairoblurprivate.h   \
        gtkcellareaboxcontextprivate.h  \
@@ -600,6 +601,7 @@ gtk_base_c_sources =                \
        gtkbuilder.c            \
        gtkbuilderparser.c      \
        gtkbuilder-menus.c      \
+       gtkbuiltinicon.c        \
        gtkbutton.c             \
        gtkcairoblur.c          \
        gtkcalendar.c           \
diff --git a/gtk/gtkbuiltinicon.c b/gtk/gtkbuiltinicon.c
new file mode 100644
index 0000000..c2e2c07
--- /dev/null
+++ b/gtk/gtkbuiltinicon.c
@@ -0,0 +1,203 @@
+/*
+ * Copyright © 2015 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.1 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/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "gtkbuiltiniconprivate.h"
+
+#include "gtkcssnodeprivate.h"
+#include "gtkrendericonprivate.h"
+
+typedef struct _GtkBuiltinIconPrivate GtkBuiltinIconPrivate;
+struct _GtkBuiltinIconPrivate {
+  GtkCssImageBuiltinType        image_type;
+  int                           default_size;
+};
+
+G_DEFINE_TYPE_WITH_CODE (GtkBuiltinIcon, gtk_builtin_icon, GTK_TYPE_CSS_GADGET,
+                         G_ADD_PRIVATE (GtkBuiltinIcon))
+
+static void
+gtk_builtin_icon_get_preferred_size (GtkCssGadget   *gadget,
+                                     GtkOrientation  orientation,
+                                     gint            for_size,
+                                     gint           *minimum,
+                                     gint           *natural,
+                                     gint           *minimum_baseline,
+                                     gint           *natural_baseline)
+{
+  GtkBuiltinIconPrivate *priv = gtk_builtin_icon_get_instance_private (GTK_BUILTIN_ICON (gadget));
+
+  *minimum = priv->default_size;
+  *natural = priv->default_size;
+}
+
+static void
+gtk_builtin_icon_allocate (GtkCssGadget        *gadget,
+                           const GtkAllocation *allocation,
+                           int                  baseline,
+                           GtkAllocation       *out_clip)
+{
+  GdkRectangle icon_clip;
+
+  GTK_CSS_GADGET_CLASS (gtk_builtin_icon_parent_class)->allocate (gadget, allocation, baseline, out_clip);
+
+  gtk_css_style_render_icon_get_extents (gtk_css_gadget_get_style (gadget),
+                                         &icon_clip,
+                                         allocation->x, allocation->y,
+                                         allocation->width, allocation->height);
+  gdk_rectangle_union (out_clip, &icon_clip, out_clip);
+}
+
+static gboolean
+gtk_builtin_icon_draw (GtkCssGadget *gadget,
+                            cairo_t      *cr,
+                            int           x,
+                            int           y,
+                            int           width,
+                            int           height)
+{
+  GtkBuiltinIconPrivate *priv = gtk_builtin_icon_get_instance_private (GTK_BUILTIN_ICON (gadget));
+
+  gtk_css_style_render_icon (gtk_css_gadget_get_style (gadget),
+                             cr,
+                             x, y,
+                             width, height,
+                             priv->image_type);
+
+  return FALSE;
+}
+
+static void
+gtk_builtin_icon_finalize (GObject *object)
+{
+  //GtkBuiltinIconPrivate *priv = gtk_builtin_icon_get_instance_private (GTK_BUILTIN_ICON (object));
+
+  G_OBJECT_CLASS (gtk_builtin_icon_parent_class)->finalize (object);
+}
+
+static void
+gtk_builtin_icon_class_init (GtkBuiltinIconClass *klass)
+{
+  GtkCssGadgetClass *gadget_class = GTK_CSS_GADGET_CLASS (klass);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gtk_builtin_icon_finalize;
+
+  gadget_class->get_preferred_size = gtk_builtin_icon_get_preferred_size;
+  gadget_class->allocate = gtk_builtin_icon_allocate;
+  gadget_class->draw = gtk_builtin_icon_draw;
+}
+
+static void
+gtk_builtin_icon_init (GtkBuiltinIcon *custom_gadget)
+{
+}
+
+GtkCssGadget *
+gtk_builtin_icon_new_for_node (GtkCssNode *node,
+                               GtkWidget  *owner)
+{
+  return g_object_new (GTK_TYPE_BUILTIN_ICON,
+                       "node", node,
+                       "owner", owner,
+                       NULL);
+}
+
+GtkCssGadget *
+gtk_builtin_icon_new (const char   *name,
+                      GtkWidget    *owner,
+                      GtkCssGadget *parent,
+                      GtkCssGadget *next_sibling)
+{
+  GtkCssNode *node;
+  GtkCssGadget *result;
+
+  node = gtk_css_node_new ();
+  gtk_css_node_set_name (node, g_intern_string (name));
+  if (parent)
+    gtk_css_node_insert_before (gtk_css_gadget_get_node (parent),
+                                node,
+                                next_sibling ? gtk_css_gadget_get_node (next_sibling) : NULL);
+
+  result = gtk_builtin_icon_new_for_node (node, owner);
+
+  g_object_unref (node);
+
+  return result;
+}
+
+void
+gtk_builtin_icon_set_image (GtkBuiltinIcon         *icon,
+                            GtkCssImageBuiltinType  image)
+{
+  GtkBuiltinIconPrivate *priv;
+  
+  g_return_if_fail (GTK_IS_BUILTIN_ICON (icon));
+
+  priv = gtk_builtin_icon_get_instance_private (icon);
+
+  if (priv->image_type != image)
+    {
+      priv->image_type = image;
+      gtk_widget_queue_draw (gtk_css_gadget_get_owner (GTK_CSS_GADGET (icon)));
+    }
+}
+
+GtkCssImageBuiltinType
+gtk_builtin_icon_get_image (GtkBuiltinIcon *icon)
+{
+  GtkBuiltinIconPrivate *priv;
+
+  g_return_val_if_fail (GTK_IS_BUILTIN_ICON (icon), GTK_CSS_IMAGE_BUILTIN_NONE);
+
+  priv = gtk_builtin_icon_get_instance_private (icon);
+
+  return priv->image_type;
+}
+
+void
+gtk_builtin_icon_set_default_size (GtkBuiltinIcon *icon,
+                                   int             default_size)
+{
+  GtkBuiltinIconPrivate *priv;
+  
+  g_return_if_fail (GTK_IS_BUILTIN_ICON (icon));
+
+  priv = gtk_builtin_icon_get_instance_private (icon);
+
+  if (priv->default_size != default_size)
+    {
+      priv->default_size = default_size;
+      gtk_widget_queue_resize (gtk_css_gadget_get_owner (GTK_CSS_GADGET (icon)));
+    }
+}
+
+int
+gtk_builtin_icon_get_default_size (GtkBuiltinIcon *icon)
+{
+  GtkBuiltinIconPrivate *priv;
+
+  g_return_val_if_fail (GTK_IS_BUILTIN_ICON (icon), GTK_CSS_IMAGE_BUILTIN_NONE);
+
+  priv = gtk_builtin_icon_get_instance_private (icon);
+
+  return priv->default_size;
+}
+
diff --git a/gtk/gtkbuiltiniconprivate.h b/gtk/gtkbuiltiniconprivate.h
new file mode 100644
index 0000000..5da73ef
--- /dev/null
+++ b/gtk/gtkbuiltiniconprivate.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright © 2015 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.1 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/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_BUILTIN_ICON_PRIVATE_H__
+#define __GTK_BUILTIN_ICON_PRIVATE_H__
+
+#include "gtk/gtkcssgadgetprivate.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_BUILTIN_ICON           (gtk_builtin_icon_get_type ())
+#define GTK_BUILTIN_ICON(obj)           (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_BUILTIN_ICON, 
GtkBuiltinIcon))
+#define GTK_BUILTIN_ICON_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_BUILTIN_ICON, 
GtkBuiltinIconClass))
+#define GTK_IS_BUILTIN_ICON(obj)        (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_BUILTIN_ICON))
+#define GTK_IS_BUILTIN_ICON_CLASS(obj)  (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_BUILTIN_ICON))
+#define GTK_BUILTIN_ICON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_BUILTIN_ICON, 
GtkBuiltinIconClass))
+
+typedef struct _GtkBuiltinIcon           GtkBuiltinIcon;
+typedef struct _GtkBuiltinIconClass      GtkBuiltinIconClass;
+
+struct _GtkBuiltinIcon
+{
+  GtkCssGadget parent;
+};
+
+struct _GtkBuiltinIconClass
+{
+  GtkCssGadgetClass  parent_class;
+};
+
+GType                   gtk_builtin_icon_get_type               (void) G_GNUC_CONST;
+
+GtkCssGadget *          gtk_builtin_icon_new                    (const char             *name,
+                                                                 GtkWidget              *owner,
+                                                                 GtkCssGadget           *parent,
+                                                                 GtkCssGadget           *next_sibling);
+GtkCssGadget *          gtk_builtin_icon_new_for_node           (GtkCssNode             *node,
+                                                                 GtkWidget              *owner);
+
+void                    gtk_builtin_icon_set_image              (GtkBuiltinIcon         *icon,
+                                                                 GtkCssImageBuiltinType  image);
+GtkCssImageBuiltinType  gtk_builtin_icon_get_image              (GtkBuiltinIcon         *icon);
+void                    gtk_builtin_icon_set_default_size       (GtkBuiltinIcon         *icon,
+                                                                 int                     default_size);
+int                     gtk_builtin_icon_get_default_size       (GtkBuiltinIcon         *icon);
+
+G_END_DECLS
+
+#endif /* __GTK_BUILTIN_ICON_PRIVATE_H__ */
diff --git a/gtk/gtkspinner.c b/gtk/gtkspinner.c
index ed2c14d..0f410e0 100644
--- a/gtk/gtkspinner.c
+++ b/gtk/gtkspinner.c
@@ -38,7 +38,7 @@
 #include "gtkstylecontextprivate.h"
 #include "gtkwidgetprivate.h"
 #include "a11y/gtkspinneraccessible.h"
-#include "gtkcsscustomgadgetprivate.h"
+#include "gtkbuiltiniconprivate.h"
 
 
 /**
@@ -84,19 +84,6 @@ gtk_spinner_finalize (GObject *object)
 }
 
 static void
-gtk_spinner_measure (GtkCssGadget   *gadget,
-                     GtkOrientation  orientation,
-                     gint            for_size,
-                     gint           *minimum,
-                     gint           *natural,
-                     gint           *minimum_baseline,
-                     gint           *natural_baseline,
-                     gpointer        data)
-{
-  *minimum = *natural = 16;
-}
-
-static void
 gtk_spinner_get_preferred_width (GtkWidget *widget,
                                  gint      *minimum,
                                  gint      *natural)
@@ -121,32 +108,6 @@ gtk_spinner_get_preferred_height (GtkWidget *widget,
 }
 
 static void
-gtk_spinner_allocate (GtkCssGadget        *gadget,
-                      const GtkAllocation *allocation,
-                      gint                 baseline,
-                      GtkAllocation       *out_clip,
-                      gpointer             data)
-{
-  GtkWidget *widget;
-  GtkStyleContext *context;
-  gint size;
-
-  widget = gtk_css_gadget_get_owner (gadget);
-  context = gtk_widget_get_style_context (widget);
-  size = MIN (allocation->width, allocation->height);
-
-  gtk_widget_set_allocation (widget, allocation);
-
-  _gtk_style_context_get_icon_extents (context,
-                                       out_clip,
-                                       allocation->x + (allocation->width - size) / 2,
-                                       allocation->y + (allocation->height - size) / 2,
-                                       size, size);
-
-  gdk_rectangle_union (out_clip, allocation, out_clip);
-}
-
-static void
 gtk_spinner_size_allocate (GtkWidget     *widget,
                            GtkAllocation *allocation)
 {
@@ -171,32 +132,6 @@ gtk_spinner_draw (GtkWidget *widget,
   return FALSE;
 }
 
-static gboolean
-gtk_spinner_render (GtkCssGadget *gadget,
-                    cairo_t      *cr,
-                    gint          x,
-                    gint          y,
-                    gint          width,
-                    gint          height,
-                    gpointer      data)
-{
-  GtkWidget *widget;
-  GtkStyleContext *context;
-  gint size;
-
-  widget = gtk_css_gadget_get_owner (gadget);
-  context = gtk_widget_get_style_context (widget);
-
-  size = MIN (width, height);
-
-  gtk_render_activity (context, cr,
-                       (width - size) / 2,
-                       (height - size) / 2,
-                       size, size);
-
-  return FALSE;
-}
-
 static void
 gtk_spinner_set_active (GtkSpinner *spinner,
                         gboolean    active)
@@ -301,13 +236,9 @@ gtk_spinner_init (GtkSpinner *spinner)
   gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
 
   widget_node = gtk_widget_get_css_node (GTK_WIDGET (spinner));
-  spinner->priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
-                                                              GTK_WIDGET (spinner),
-                                                              gtk_spinner_measure,
-                                                              gtk_spinner_allocate,
-                                                              gtk_spinner_render,
-                                                              NULL,
-                                                              NULL);
+  spinner->priv->gadget = gtk_builtin_icon_new_for_node (widget_node, GTK_WIDGET (spinner));
+  gtk_builtin_icon_set_image (GTK_BUILTIN_ICON (spinner->priv->gadget), GTK_CSS_IMAGE_BUILTIN_SPINNER);
+  gtk_builtin_icon_set_default_size (GTK_BUILTIN_ICON (spinner->priv->gadget), 16);
 }
 
 /**


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