[gtk+/native-layout-incubator-2] GtkCellRenderer implements GtkCellSizeRequest



commit 5d48c9520c48ed5e51dc6205415cd6164a8d6f5c
Author: Tristan Van Berkom <tristan van berkom gmail com>
Date:   Wed Jun 30 11:20:37 2010 -0400

    GtkCellRenderer implements GtkCellSizeRequest
    
    This commit deprecated gtk_cell_renderer_get_size() in favour of the
    height-for-width apis in GtkCellSizeRequest, a private
    _gtk_cell_renderer_calc_offset is added to calculate the x/y_offset
    in the 'cell_area' previously passed to gtk_cell_renderer_get_size().

 gtk/gtkcellrenderer.c |  229 ++++++++++++++++++++++++++++++++++++++++++++-----
 gtk/gtkcellrenderer.h |    8 ++
 2 files changed, 215 insertions(+), 22 deletions(-)
---
diff --git a/gtk/gtkcellrenderer.c b/gtk/gtkcellrenderer.c
index 6093719..793cd02 100644
--- a/gtk/gtkcellrenderer.c
+++ b/gtk/gtkcellrenderer.c
@@ -19,12 +19,15 @@
 
 #include "config.h"
 #include "gtkcellrenderer.h"
+#include "gtkcellsizerequest.h"
 #include "gtkintl.h"
 #include "gtkmarshalers.h"
 #include "gtkprivate.h"
 #include "gtktreeprivate.h"
 #include "gtkalias.h"
 
+static void gtk_cell_renderer_init          (GtkCellRenderer      *cell);
+static void gtk_cell_renderer_class_init    (GtkCellRendererClass *class);
 static void gtk_cell_renderer_get_property  (GObject              *object,
 					     guint                 param_id,
 					     GValue               *value,
@@ -36,6 +39,28 @@ static void gtk_cell_renderer_set_property  (GObject              *object,
 static void set_cell_bg_color               (GtkCellRenderer      *cell,
 					     GdkColor             *color);
 
+/* Fallback GtkCellSizeRequest implementation to use remaining ->get_size() implementations */
+static void gtk_cell_renderer_cell_size_request_init   (GtkCellSizeRequestIface *iface);
+static void gtk_cell_renderer_get_width                (GtkCellSizeRequest      *cell,
+							GtkWidget               *widget,
+							gint                    *minimum_size,
+							gint                    *natural_size);
+static void gtk_cell_renderer_get_height               (GtkCellSizeRequest      *cell,
+							GtkWidget               *widget,
+							gint                    *minimum_size,
+							gint                    *natural_size);
+static void gtk_cell_renderer_get_height_for_width     (GtkCellSizeRequest      *cell,
+							GtkWidget               *widget,
+							gint                     width,
+							gint                    *minimum_height,
+							gint                    *natural_height);
+static void gtk_cell_renderer_get_width_for_height     (GtkCellSizeRequest      *cell,
+							GtkWidget               *widget,
+							gint                     height,
+							gint                    *minimum_width,
+							gint                    *natural_width);
+
+
 
 #define GTK_CELL_RENDERER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER, GtkCellRendererPrivate))
 
@@ -72,9 +97,50 @@ enum {
   LAST_SIGNAL
 };
 
-static guint cell_renderer_signals[LAST_SIGNAL] = { 0 };
+static guint  cell_renderer_signals[LAST_SIGNAL] = { 0 };
+
+
+/* Do a manual _get_type() here to avoid a deadlock implementing
+ * the interface which we are a prerequisite of.
+ */
+GType
+gtk_cell_renderer_get_type (void)
+{
+  static GType cell_renderer_type = 0;
+
+  if (G_UNLIKELY (cell_renderer_type == 0))
+    {
+      const GTypeInfo cell_renderer_info =
+      {
+	sizeof (GtkCellRendererClass),
+	NULL,		/* base_init */
+	NULL,           /* base_finalize */
+	(GClassInitFunc) gtk_cell_renderer_class_init,
+	NULL,		/* class_finalize */
+	NULL,		/* class_init */
+	sizeof (GtkCellRenderer),
+	0,		/* n_preallocs */
+	(GInstanceInitFunc) gtk_cell_renderer_init,
+	NULL,		/* value_table */
+      };
+
+      const GInterfaceInfo cell_size_request_info =
+      {
+	(GInterfaceInitFunc) gtk_cell_renderer_cell_size_request_init,
+	(GInterfaceFinalizeFunc) NULL,
+	NULL /* interface data */
+      };
+
+      cell_renderer_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkCellRenderer",
+						   &cell_renderer_info, G_TYPE_FLAG_ABSTRACT);
+
+      g_type_add_interface_static (cell_renderer_type, GTK_TYPE_CELL_SIZE_REQUEST,
+                                   &cell_size_request_info) ;
+    }
+
+  return cell_renderer_type;
+}
 
-G_DEFINE_ABSTRACT_TYPE (GtkCellRenderer, gtk_cell_renderer, GTK_TYPE_OBJECT)
 
 static void
 gtk_cell_renderer_init (GtkCellRenderer *cell)
@@ -492,6 +558,9 @@ set_cell_bg_color (GtkCellRenderer *cell,
  *
  * Please note that the values set in @width and @height, as well as those 
  * in @x_offset and @y_offset are inclusive of the xpad and ypad properties.
+ *
+ *
+ * Deprecated: 3.0: Use gtk_cell_size_request_get_size() instead.
  **/
 void
 gtk_cell_renderer_get_size (GtkCellRenderer    *cell,
@@ -502,30 +571,22 @@ gtk_cell_renderer_get_size (GtkCellRenderer    *cell,
 			    gint               *width,
 			    gint               *height)
 {
-  gint *real_width = width;
-  gint *real_height = height;
+  GtkRequisition request;
 
   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
-  g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size != NULL);
 
-  if (width && cell->width != -1)
-    {
-      real_width = NULL;
-      *width = cell->width;
-    }
-  if (height && cell->height != -1)
-    {
-      real_height = NULL;
-      *height = cell->height;
-    }
+  gtk_cell_size_request_get_size (GTK_CELL_SIZE_REQUEST (cell),
+				  widget, &request, NULL);
+
+  if (width)
+    *width = request.width;
+  
+  if (height)
+    *height = request.height;
 
-  GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (cell,
-						widget,
-						(GdkRectangle *) cell_area,
-						x_offset,
-						y_offset,
-						real_width,
-						real_height);
+  if (cell_area)
+    _gtk_cell_renderer_calc_offset (cell, cell_area, gtk_widget_get_direction (widget),
+				    request.width, request.height, x_offset, y_offset);
 }
 
 /**
@@ -968,5 +1029,129 @@ gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
     }
 }
 
+static void
+gtk_cell_renderer_cell_size_request_init (GtkCellSizeRequestIface *iface)
+{
+  iface->get_width     = gtk_cell_renderer_get_width;
+  iface->get_height    = gtk_cell_renderer_get_height;
+
+  iface->get_width_for_height  = gtk_cell_renderer_get_width_for_height;
+  iface->get_height_for_width  = gtk_cell_renderer_get_height_for_width;
+}
+
+static void
+gtk_cell_renderer_get_desired_size (GtkCellSizeRequest   *cell,
+				    GtkWidget         *widget,
+				    GtkOrientation     orientation,
+				    gint              *minimum_size,
+				    gint              *natural_size)
+{
+  GtkRequisition min_req;
+
+  /* Fallback on the old API to get the size. */
+  if (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size)
+    GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (GTK_CELL_RENDERER (cell), widget, NULL, NULL, NULL,
+						  &min_req.width, &min_req.height);
+  else
+    {
+      min_req.width = 0;
+      min_req.height = 0;
+    }
+
+  if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+      if (minimum_size)
+	*minimum_size = min_req.width;
+
+      if (natural_size)
+	*natural_size = min_req.width;
+    }
+  else
+    {
+      if (minimum_size)
+	*minimum_size = min_req.height;
+
+      if (natural_size)
+	*natural_size = min_req.height;
+    }
+}
+
+static void
+gtk_cell_renderer_get_width (GtkCellSizeRequest   *cell,
+			     GtkWidget         *widget,
+			     gint              *minimum_size,
+			     gint              *natural_size)
+{
+  gtk_cell_renderer_get_desired_size (cell, widget, GTK_ORIENTATION_HORIZONTAL, 
+				      minimum_size, natural_size);
+}
+
+static void
+gtk_cell_renderer_get_height (GtkCellSizeRequest   *cell,
+			      GtkWidget         *widget,
+			      gint              *minimum_size,
+			      gint              *natural_size)
+{
+  gtk_cell_renderer_get_desired_size (cell, widget, GTK_ORIENTATION_VERTICAL, 
+				      minimum_size, natural_size);
+}
+
+
+static void
+gtk_cell_renderer_get_height_for_width (GtkCellSizeRequest *cell,
+					GtkWidget       *widget,
+					gint             width,
+					gint            *minimum_height,
+					gint            *natural_height)
+{
+  /* Fall back on the height reported from ->get_size() */
+  gtk_cell_size_request_get_height (cell, widget, minimum_height, natural_height);
+}
+
+static void
+gtk_cell_renderer_get_width_for_height (GtkCellSizeRequest *cell,
+					GtkWidget       *widget,
+					gint             height,
+					gint            *minimum_width,
+					gint            *natural_width)
+{
+  /* Fall back on the width reported from ->get_size() */
+  gtk_cell_size_request_get_width (cell, widget, minimum_width, natural_width);
+}
+
+/* An internal convenience function for some containers to peek at the
+ * cell alignment in a target allocation (used to draw focus and align
+ * cells in the icon view).
+ *
+ * Note this is only a trivial 'align * (allocation - request)' operation.
+ */
+void
+_gtk_cell_renderer_calc_offset    (GtkCellRenderer      *cell,
+				   const GdkRectangle   *cell_area,
+				   GtkTextDirection      direction,
+				   gint                  width,
+				   gint                  height,
+				   gint                 *x_offset,
+				   gint                 *y_offset)
+{
+  g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
+  g_return_if_fail (cell_area != NULL);
+  g_return_if_fail (x_offset || y_offset);
+
+  if (x_offset)
+    {
+      *x_offset = (((direction == GTK_TEXT_DIR_RTL) ?
+		    (1.0 - cell->xalign) : cell->xalign) * 
+		   (cell_area->width - width));
+      *x_offset = MAX (*x_offset, 0);
+    }
+  if (y_offset)
+    {
+      *y_offset = (cell->yalign *
+		   (cell_area->height - height));
+      *y_offset = MAX (*y_offset, 0);
+    }
+}
+
 #define __GTK_CELL_RENDERER_C__
 #include "gtkaliasdef.c"
diff --git a/gtk/gtkcellrenderer.h b/gtk/gtkcellrenderer.h
index d16f3d4..5cf7e5d 100644
--- a/gtk/gtkcellrenderer.h
+++ b/gtk/gtkcellrenderer.h
@@ -187,6 +187,14 @@ void             gtk_cell_renderer_stop_editing   (GtkCellRenderer      *cell,
                                                    gboolean              canceled);
 
 
+void            _gtk_cell_renderer_calc_offset    (GtkCellRenderer      *cell,
+						   const GdkRectangle   *cell_area,
+						   GtkTextDirection      direction,
+						   gint                  width,
+						   gint                  height,
+						   gint                 *x_offset,
+						   gint                 *y_offset);
+
 G_END_DECLS
 
 #endif /* __GTK_CELL_RENDERER_H__ */



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