[rhythmbox] widgets: Simplify RbCellRendererPixbuf code



commit dbff5af57e1c17c8543a830478ab6e064ff90b87
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Feb 24 06:46:04 2011 +0000

    widgets: Simplify RbCellRendererPixbuf code
    
    By subclassing a pixbuf cell renderer rather than a cell renderer.

 widgets/rb-cell-renderer-pixbuf.c |  182 +------------------------------------
 widgets/rb-cell-renderer-pixbuf.h |    6 +-
 2 files changed, 3 insertions(+), 185 deletions(-)
---
diff --git a/widgets/rb-cell-renderer-pixbuf.c b/widgets/rb-cell-renderer-pixbuf.c
index 3c95685..979d7d1 100644
--- a/widgets/rb-cell-renderer-pixbuf.c
+++ b/widgets/rb-cell-renderer-pixbuf.c
@@ -29,29 +29,8 @@
 #include "rb-cell-renderer-pixbuf.h"
 #include "rb-cut-and-paste-code.h"
 
-static void rb_cell_renderer_pixbuf_get_property  (GObject                    *object,
-						   guint                       param_id,
-						   GValue                     *value,
-						   GParamSpec                 *pspec);
-static void rb_cell_renderer_pixbuf_set_property  (GObject                    *object,
-						   guint                       param_id,
-						   const GValue               *value,
-						   GParamSpec                 *pspec);
 static void rb_cell_renderer_pixbuf_init       (RBCellRendererPixbuf      *celltext);
 static void rb_cell_renderer_pixbuf_class_init (RBCellRendererPixbufClass *class);
-static void rb_cell_renderer_pixbuf_get_size   (GtkCellRenderer            *cell,
-						GtkWidget                  *widget,
-						const GdkRectangle         *rectangle,
-						gint                       *x_offset,
-						gint                       *y_offset,
-						gint                       *width,
-						gint                       *height);
-static void rb_cell_renderer_pixbuf_render     (GtkCellRenderer            *cell,
-						cairo_t			   *cr,
-						GtkWidget                  *widget,
-						const GdkRectangle         *background_area,
-						const GdkRectangle         *cell_area,
-						guint                       flags);
 static gboolean rb_cell_renderer_pixbuf_activate (GtkCellRenderer     *cell,
 						  GdkEvent            *event,
 						  GtkWidget           *widget,
@@ -60,18 +39,13 @@ static gboolean rb_cell_renderer_pixbuf_activate (GtkCellRenderer     *cell,
 						  const GdkRectangle  *cell_area,
 						  GtkCellRendererState flags);
 
-enum {
-	PROP_ZERO,
-	PROP_PIXBUF
-};
-
 enum
 {
 	PIXBUF_CLICKED,
 	LAST_SIGNAL
 };
 
-G_DEFINE_TYPE (RBCellRendererPixbuf, rb_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
+G_DEFINE_TYPE (RBCellRendererPixbuf, rb_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER_PIXBUF)
 
 /**
  * SECTION:rb-cell-renderer-pixbuf
@@ -98,28 +72,9 @@ rb_cell_renderer_pixbuf_class_init (RBCellRendererPixbufClass *class)
 	GObjectClass *object_class = G_OBJECT_CLASS (class);
 	GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
 
-	object_class->get_property = rb_cell_renderer_pixbuf_get_property;
-	object_class->set_property = rb_cell_renderer_pixbuf_set_property;
-
-	cell_class->get_size = rb_cell_renderer_pixbuf_get_size;
-	cell_class->render = rb_cell_renderer_pixbuf_render;
 	cell_class->activate = rb_cell_renderer_pixbuf_activate;
 
 	/**
-	 * RBCellRendererPixbuf:pixbuf:
-	 *
-	 * The pixbuf to render in the cell.
-	 */
-	g_object_class_install_property (object_class,
-					 PROP_PIXBUF,
-					 g_param_spec_object ("pixbuf",
-							      _("Pixbuf Object"),
-							      _("The pixbuf to render."),
-							      GDK_TYPE_PIXBUF,
-							      G_PARAM_READABLE |
-							      G_PARAM_WRITABLE));
-
-	/**
 	 * RBCellRendererPixbuf::pixbuf-clicked:
 	 * @renderer: the #RBCellRendererPixbuf
 	 * @path: the #GtkTreePath to the row that was clicked
@@ -138,51 +93,6 @@ rb_cell_renderer_pixbuf_class_init (RBCellRendererPixbufClass *class)
 			      G_TYPE_STRING);
 }
 
-static void
-rb_cell_renderer_pixbuf_get_property (GObject        *object,
-				      guint           param_id,
-				      GValue         *value,
-				      GParamSpec     *pspec)
-{
-  RBCellRendererPixbuf *cellpixbuf = RB_CELL_RENDERER_PIXBUF (object);
-
-  switch (param_id)
-    {
-    case PROP_PIXBUF:
-      g_value_set_object (value,
-                          cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
-      break;
-    }
-}
-
-static void
-rb_cell_renderer_pixbuf_set_property (GObject      *object,
-				      guint         param_id,
-				      const GValue *value,
-				      GParamSpec   *pspec)
-{
-  GdkPixbuf *pixbuf;
-  RBCellRendererPixbuf *cellpixbuf = RB_CELL_RENDERER_PIXBUF (object);
-
-  switch (param_id)
-    {
-    case PROP_PIXBUF:
-      pixbuf = (GdkPixbuf*) g_value_get_object (value);
-      if (pixbuf)
-        g_object_ref (G_OBJECT (pixbuf));
-      if (cellpixbuf->pixbuf)
-	g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
-      cellpixbuf->pixbuf = pixbuf;
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
-      break;
-    }
-}
-
 /**
  * rb_cell_renderer_pixbuf_new:
  *
@@ -196,96 +106,6 @@ rb_cell_renderer_pixbuf_new (void)
   return GTK_CELL_RENDERER (g_object_new (rb_cell_renderer_pixbuf_get_type (), NULL, NULL));
 }
 
-/* XXX implement get_preferred_width/height/height_for_width/width_for_height */
-static void
-rb_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
-				  GtkWidget       *widget,
-				  const GdkRectangle *cell_area,
-				  gint            *x_offset,
-				  gint            *y_offset,
-				  gint            *width,
-				  gint            *height)
-{
-  RBCellRendererPixbuf *cellpixbuf = (RBCellRendererPixbuf *) cell;
-  gint pixbuf_width = 0;
-  gint pixbuf_height = 0;
-  gint calc_width;
-  gint calc_height;
-  gint xpad, ypad;
-  gfloat xalign, yalign;
-
-  if (cellpixbuf->pixbuf)
-    {
-      pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
-      pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
-    }
-
-  gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (cellpixbuf), &xpad, &ypad);
-  calc_width = xpad * 2 + pixbuf_width;
-  calc_height = ypad * 2 + pixbuf_height;
-
-  if (x_offset) *x_offset = 0;
-  if (y_offset) *y_offset = 0;
-
-  if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
-    {
-      gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (cellpixbuf), &xalign, &yalign);
-
-      if (x_offset)
-	{
-	  *x_offset = xalign * (cell_area->width - calc_width - (2 * xpad));
-	  *x_offset = MAX (*x_offset, 0) + xpad;
-	}
-      if (y_offset)
-	{
-	  *y_offset = yalign * (cell_area->height - calc_height - (2 * ypad));
-	  *y_offset = MAX (*y_offset, 0) + ypad;
-	}
-    }
-
-  if (calc_width)
-    *width = calc_width;
-
-  if (height)
-    *height = calc_height;
-}
-
-static void
-rb_cell_renderer_pixbuf_render (GtkCellRenderer    *cell,
-				cairo_t            *cr,
-				GtkWidget          *widget,
-				const GdkRectangle *background_area,
-				const GdkRectangle *cell_area,
-				guint               flags)
-
-{
-  RBCellRendererPixbuf *cellpixbuf = (RBCellRendererPixbuf *) cell;
-  GdkRectangle pix_rect;
-  GdkRectangle draw_rect;
-  gint xpad, ypad;
-
-  if (!cellpixbuf->pixbuf)
-    return;
-
-  rb_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
-				     &pix_rect.x,
-				     &pix_rect.y,
-				     &pix_rect.width,
-				     &pix_rect.height);
-
-  pix_rect.x += cell_area->x;
-  pix_rect.y += cell_area->y;
-  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
-  pix_rect.width -= xpad * 2;
-  pix_rect.height -= ypad * 2;
-
-  if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect)) {
-    gdk_cairo_set_source_pixbuf (cr, cellpixbuf->pixbuf, pix_rect.x, pix_rect.y);
-    gdk_cairo_rectangle (cr, &draw_rect);
-    cairo_paint (cr);
-  }
-}
-
 static gboolean
 rb_cell_renderer_pixbuf_activate (GtkCellRenderer *cell,
 				  GdkEvent *event,
diff --git a/widgets/rb-cell-renderer-pixbuf.h b/widgets/rb-cell-renderer-pixbuf.h
index 3e3e0df..fcf295f 100644
--- a/widgets/rb-cell-renderer-pixbuf.h
+++ b/widgets/rb-cell-renderer-pixbuf.h
@@ -40,14 +40,12 @@ typedef struct _RBCellRendererPixbufClass RBCellRendererPixbufClass;
 
 struct _RBCellRendererPixbuf
 {
-  GtkCellRenderer parent;
-
-  GdkPixbuf *pixbuf;
+  GtkCellRendererPixbuf parent;
 };
 
 struct _RBCellRendererPixbufClass
 {
-  GtkCellRendererClass parent_class;
+  GtkCellRendererPixbufClass parent_class;
 
   void (*pixbuf_clicked) (RBCellRendererPixbuf *renderer, GtkTreePath *path);
 };



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