[gimp/gtk3-port: 75/140] libgimpwidgets: port the ruler to GTK+ 3.0



commit 71c87150682f64894fe60e168ecc7d71972c1fc0
Author: Michael Natterer <mitch gimp org>
Date:   Tue Oct 19 18:59:50 2010 +0200

    libgimpwidgets: port the ruler to GTK+ 3.0

 libgimpwidgets/gimpruler.c |  142 +++++++++++++++++++++-----------------------
 1 files changed, 68 insertions(+), 74 deletions(-)
---
diff --git a/libgimpwidgets/gimpruler.c b/libgimpwidgets/gimpruler.c
index 6df4865..108f82f 100644
--- a/libgimpwidgets/gimpruler.c
+++ b/libgimpwidgets/gimpruler.c
@@ -60,20 +60,19 @@ enum
  */
 typedef struct
 {
-  GtkOrientation  orientation;
-  GimpUnit        unit;
-  gdouble         lower;
-  gdouble         upper;
-  gdouble         position;
-  gdouble         max_size;
-
-  GdkPixmap      *backing_store;
-  GdkGC          *non_gr_exp_gc;
-  PangoLayout    *layout;
-  gdouble         font_scale;
-
-  gint            xsrc;
-  gint            ysrc;
+  GtkOrientation   orientation;
+  GimpUnit         unit;
+  gdouble          lower;
+  gdouble          upper;
+  gdouble          position;
+  gdouble          max_size;
+
+  cairo_surface_t *backing_store;
+  PangoLayout     *layout;
+  gdouble          font_scale;
+
+  gint             xsrc;
+  gint             ysrc;
 } GimpRulerPrivate;
 
 
@@ -107,8 +106,8 @@ static void          gimp_ruler_style_set     (GtkWidget      *widget,
                                                GtkStyle       *prev_style);
 static gboolean      gimp_ruler_motion_notify (GtkWidget      *widget,
                                                GdkEventMotion *event);
-static gboolean      gimp_ruler_expose        (GtkWidget      *widget,
-                                               GdkEventExpose *event);
+static gboolean      gimp_ruler_draw          (GtkWidget      *widget,
+                                               cairo_t        *cr);
 
 static void          gimp_ruler_draw_ticks    (GimpRuler      *ruler);
 static void          gimp_ruler_draw_pos      (GimpRuler      *ruler);
@@ -139,7 +138,7 @@ gimp_ruler_class_init (GimpRulerClass *klass)
   widget_class->size_request        = gimp_ruler_size_request;
   widget_class->style_set           = gimp_ruler_style_set;
   widget_class->motion_notify_event = gimp_ruler_motion_notify;
-  widget_class->expose_event        = gimp_ruler_expose;
+  widget_class->draw                = gimp_ruler_draw;
 
   g_type_class_add_private (object_class, sizeof (GimpRulerPrivate));
 
@@ -222,7 +221,6 @@ gimp_ruler_init (GimpRuler *ruler)
   priv->position      = 0;
   priv->max_size      = 0;
   priv->backing_store = NULL;
-  priv->non_gr_exp_gc = NULL;
   priv->font_scale    = DEFAULT_RULER_FONT_SCALE;
 }
 
@@ -520,13 +518,12 @@ gimp_ruler_realize (GtkWidget *widget)
   attributes.height      = allocation.height;
   attributes.wclass      = GDK_INPUT_OUTPUT;
   attributes.visual      = gtk_widget_get_visual (widget);
-  attributes.colormap    = gtk_widget_get_colormap (widget);
   attributes.event_mask  = (gtk_widget_get_events (widget) |
                             GDK_EXPOSURE_MASK              |
                             GDK_POINTER_MOTION_MASK        |
                             GDK_POINTER_MOTION_HINT_MASK);
 
-  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
+  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
 
   gtk_widget_set_window (widget,
                          gdk_window_new (gtk_widget_get_parent_window (widget),
@@ -549,16 +546,10 @@ gimp_ruler_unrealize (GtkWidget *widget)
 
   if (priv->backing_store)
     {
-      g_object_unref (priv->backing_store);
+      cairo_surface_destroy (priv->backing_store);
       priv->backing_store = NULL;
     }
 
-  if (priv->non_gr_exp_gc)
-    {
-      g_object_unref (priv->non_gr_exp_gc);
-      priv->non_gr_exp_gc = NULL;
-    }
-
   if (priv->layout)
     {
       g_object_unref (priv->layout);
@@ -572,7 +563,14 @@ static void
 gimp_ruler_size_allocate (GtkWidget     *widget,
                           GtkAllocation *allocation)
 {
-  GimpRuler *ruler = GIMP_RULER (widget);
+  GimpRuler     *ruler = GIMP_RULER (widget);
+  GtkAllocation  widget_allocation;
+  gboolean       resized;
+
+  gtk_widget_get_allocation (widget, &widget_allocation);
+
+  resized = (widget_allocation.width  != allocation->width ||
+             widget_allocation.height != allocation->height);
 
   gtk_widget_set_allocation (widget, allocation);
 
@@ -582,7 +580,8 @@ gimp_ruler_size_allocate (GtkWidget     *widget,
                               allocation->x, allocation->y,
                               allocation->width, allocation->height);
 
-      gimp_ruler_make_pixmap (ruler);
+      if (resized)
+        gimp_ruler_make_pixmap (ruler);
     }
 }
 
@@ -666,28 +665,18 @@ gimp_ruler_motion_notify (GtkWidget      *widget,
 }
 
 static gboolean
-gimp_ruler_expose (GtkWidget      *widget,
-                   GdkEventExpose *event)
+gimp_ruler_draw (GtkWidget *widget,
+                 cairo_t   *cr)
 {
-  if (gtk_widget_is_drawable (widget))
-    {
-      GimpRuler        *ruler = GIMP_RULER (widget);
-      GimpRulerPrivate *priv  = GIMP_RULER_GET_PRIVATE (ruler);
-      GtkAllocation     allocation;
+  GimpRuler        *ruler = GIMP_RULER (widget);
+  GimpRulerPrivate *priv  = GIMP_RULER_GET_PRIVATE (ruler);
 
-      gtk_widget_get_allocation (widget, &allocation);
+  gimp_ruler_draw_ticks (ruler);
 
-      gimp_ruler_draw_ticks (ruler);
+  cairo_set_source_surface (cr, priv->backing_store, 0, 0);
+  cairo_paint (cr);
 
-      gdk_draw_drawable (gtk_widget_get_window (widget),
-                         priv->non_gr_exp_gc,
-                         priv->backing_store,
-                         0, 0, 0, 0,
-                         allocation.width,
-                         allocation.height);
-
-      gimp_ruler_draw_pos (ruler);
-    }
+  gimp_ruler_draw_pos (ruler);
 
   return FALSE;
 }
@@ -745,6 +734,10 @@ gimp_ruler_draw_ticks (GimpRuler *ruler)
       height = allocation.width - ythickness * 2;
     }
 
+  cr = cairo_create (priv->backing_store);
+  gdk_cairo_set_source_color (cr, &style->bg[state]);
+
+#if 0
   gtk_paint_box (style, priv->backing_store,
                  GTK_STATE_NORMAL, GTK_SHADOW_OUT,
                  NULL, widget,
@@ -752,8 +745,10 @@ gimp_ruler_draw_ticks (GimpRuler *ruler)
                  "hruler" : "vruler",
                  0, 0,
                  allocation.width, allocation.height);
+#else
+  cairo_paint (cr);
+#endif
 
-  cr = gdk_cairo_create (priv->backing_store);
   gdk_cairo_set_source_color (cr, &style->fg[state]);
 
   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
@@ -868,6 +863,7 @@ gimp_ruler_draw_ticks (GimpRuler *ruler)
                   pango_layout_set_text (layout, unit_str, -1);
                   pango_layout_get_extents (layout, &logical_rect, NULL);
 
+#if 0
                   gtk_paint_layout (style,
                                     priv->backing_store,
                                     state,
@@ -878,6 +874,12 @@ gimp_ruler_draw_ticks (GimpRuler *ruler)
                                     pos + 2,
                                     ythickness + PANGO_PIXELS (logical_rect.y - digit_offset),
                                     layout);
+#else
+                  cairo_move_to (cr,
+                                 pos + 2,
+                                 ythickness + PANGO_PIXELS (logical_rect.y - digit_offset));
+                  pango_cairo_show_layout (cr, layout);
+#endif
                 }
               else
                 {
@@ -888,6 +890,7 @@ gimp_ruler_draw_ticks (GimpRuler *ruler)
                       pango_layout_set_text (layout, unit_str + j, 1);
                       pango_layout_get_extents (layout, NULL, &logical_rect);
 
+#if 0
                       gtk_paint_layout (style,
                                         priv->backing_store,
                                         state,
@@ -898,6 +901,12 @@ gimp_ruler_draw_ticks (GimpRuler *ruler)
                                         xthickness + 1,
                                         pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset),
                                         layout);
+#else
+                      cairo_move_to (cr,
+                                     xthickness + 1,
+                                     pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset));
+                      pango_cairo_show_layout (cr, layout);
+#endif
                     }
                 }
             }
@@ -960,12 +969,11 @@ gimp_ruler_draw_pos (GimpRuler *ruler)
 
       /*  If a backing store exists, restore the ruler  */
       if (priv->backing_store)
-        gdk_draw_drawable (gtk_widget_get_window (widget),
-                           style->black_gc,
-                           priv->backing_store,
-                           priv->xsrc, priv->ysrc,
-                           priv->xsrc, priv->ysrc,
-                           bs_width, bs_height);
+        {
+          cairo_set_source_surface (cr, priv->backing_store, 0, 0);
+          cairo_rectangle (cr, priv->xsrc, priv->ysrc, bs_width, bs_height);
+          cairo_fill (cr);
+        }
 
       position = gimp_ruler_get_position (ruler);
 
@@ -1016,31 +1024,17 @@ gimp_ruler_make_pixmap (GimpRuler *ruler)
   GtkWidget        *widget = GTK_WIDGET (ruler);
   GimpRulerPrivate *priv   = GIMP_RULER_GET_PRIVATE (ruler);
   GtkAllocation     allocation;
-  gint              width;
-  gint              height;
 
   gtk_widget_get_allocation (widget, &allocation);
 
   if (priv->backing_store)
-    {
-      gdk_drawable_get_size (priv->backing_store, &width, &height);
-      if ((width  == allocation.width) &&
-          (height == allocation.height))
-        return;
+    cairo_surface_destroy (priv->backing_store);
 
-      g_object_unref (priv->backing_store);
-    }
-
-  priv->backing_store = gdk_pixmap_new (gtk_widget_get_window (widget),
-                                        allocation.width,
-                                        allocation.height,
-                                        -1);
-
-  if (!priv->non_gr_exp_gc)
-    {
-      priv->non_gr_exp_gc = gdk_gc_new (gtk_widget_get_window (widget));
-      gdk_gc_set_exposures (priv->non_gr_exp_gc, FALSE);
-    }
+  priv->backing_store =
+    gdk_window_create_similar_surface (gtk_widget_get_window (widget),
+                                       CAIRO_CONTENT_COLOR,
+                                       allocation.width,
+                                       allocation.height);
 }
 
 



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