[gimp/gtk3-port: 132/224] app: port GimpSpinScale to GTK+ 3.0



commit f571996e091853e584f91ec196bdccd39af5b293
Author: Michael Natterer <mitch gimp org>
Date:   Fri Nov 19 15:23:21 2010 +0100

    app: port GimpSpinScale to GTK+ 3.0

 app/widgets/gimpspinscale.c |  190 +++++++++++++++++++++++++++++--------------
 1 files changed, 128 insertions(+), 62 deletions(-)
---
diff --git a/app/widgets/gimpspinscale.c b/app/widgets/gimpspinscale.c
index d1b1368..7e51b39 100644
--- a/app/widgets/gimpspinscale.c
+++ b/app/widgets/gimpspinscale.c
@@ -64,8 +64,8 @@ static void       gimp_spin_scale_get_property   (GObject        *object,
 
 static void       gimp_spin_scale_style_set      (GtkWidget      *widget,
                                                   GtkStyle       *prev_style);
-static gboolean   gimp_spin_scale_expose         (GtkWidget      *widget,
-                                                  GdkEventExpose *event);
+static gboolean   gimp_spin_scale_draw           (GtkWidget      *widget,
+                                                  cairo_t        *cr);
 static gboolean   gimp_spin_scale_button_press   (GtkWidget      *widget,
                                                   GdkEventButton *event);
 static gboolean   gimp_spin_scale_button_release (GtkWidget      *widget,
@@ -94,7 +94,7 @@ gimp_spin_scale_class_init (GimpSpinScaleClass *klass)
   object_class->get_property         = gimp_spin_scale_get_property;
 
   widget_class->style_set            = gimp_spin_scale_style_set;
-  widget_class->expose_event         = gimp_spin_scale_expose;
+  widget_class->draw                 = gimp_spin_scale_draw;
   widget_class->button_press_event   = gimp_spin_scale_button_press;
   widget_class->button_release_event = gimp_spin_scale_button_release;
   widget_class->motion_notify_event  = gimp_spin_scale_button_motion;
@@ -208,59 +208,32 @@ gimp_spin_scale_style_set (GtkWidget *widget,
 }
 
 static gboolean
-gimp_spin_scale_expose (GtkWidget      *widget,
-                        GdkEventExpose *event)
+gimp_spin_scale_draw (GtkWidget *widget,
+                      cairo_t   *cr)
 {
   GimpSpinScalePrivate *private = GET_PRIVATE (widget);
   GtkStyle             *style   = gtk_widget_get_style (widget);
-  cairo_t              *cr;
-  gint                  w, h;
+  GtkAllocation         allocation;
 
-  GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
+  cairo_save (cr);
+  GTK_WIDGET_CLASS (parent_class)->draw (widget, cr);
+  cairo_restore (cr);
 
-  cr = gdk_cairo_create (event->window);
-  gdk_cairo_region (cr, event->region);
-  cairo_clip (cr);
-
-  gdk_drawable_get_size (event->window, &w, &h);
+  gtk_widget_get_allocation (widget, &allocation);
 
   cairo_set_line_width (cr, 1.0);
 
-  if (event->window == gtk_entry_get_text_window (GTK_ENTRY (widget)))
-    {
-      /* let right line of rectangle disappear */
-      cairo_rectangle (cr, 0.5, 0.5, w, h - 1.0);
-      gdk_cairo_set_source_color (cr,
-                                  &style->text[gtk_widget_get_state (widget)]);
-      cairo_stroke (cr);
-    }
-  else
-    {
-      /* let left line of rectangle disappear */
-      cairo_rectangle (cr, -0.5, 0.5, w, h - 1.0);
-      gdk_cairo_set_source_color (cr,
-                                  &style->text[gtk_widget_get_state (widget)]);
-      cairo_stroke (cr);
-
-      cairo_rectangle (cr, 0.5, 1.5, w - 2.0, h - 3.0);
-      gdk_cairo_set_source_color (cr,
-                                  &style->base[gtk_widget_get_state (widget)]);
-      cairo_stroke (cr);
-    }
+  cairo_rectangle (cr, 0.5, 0.5,
+                   allocation.width - 1.0, allocation.height - 1.0);
+  gdk_cairo_set_source_color (cr,
+                              &style->text[gtk_widget_get_state (widget)]);
+  cairo_stroke (cr);
 
-  cairo_destroy (cr);
-
-  if (private->label &&
-      gtk_widget_is_drawable (widget) &&
-      event->window == gtk_entry_get_text_window (GTK_ENTRY (widget)))
+  if (private->label)
     {
       PangoLayout     *layout;
       const GtkBorder *border;
 
-      cr = gdk_cairo_create (event->window);
-      gdk_cairo_region (cr, event->region);
-      cairo_clip (cr);
-
       border = gtk_entry_get_inner_border (GTK_ENTRY (widget));
 
       if (border)
@@ -273,7 +246,6 @@ gimp_spin_scale_expose (GtkWidget      *widget,
 
       layout = gtk_widget_create_pango_layout (widget, private->label);
       pango_cairo_show_layout (cr, layout);
-      cairo_destroy (cr);
 
       g_object_unref (layout);
     }
@@ -281,10 +253,92 @@ gimp_spin_scale_expose (GtkWidget      *widget,
   return FALSE;
 }
 
+/* Returns TRUE if a translation should be done */
+static gboolean
+gtk_widget_get_translation_to_window (GtkWidget *widget,
+                                      GdkWindow *window,
+                                      int       *x,
+                                      int       *y)
+{
+  GdkWindow *w, *widget_window;
+
+  if (!gtk_widget_get_has_window (widget))
+    {
+      GtkAllocation allocation;
+
+      gtk_widget_get_allocation (widget, &allocation);
+
+      *x = -allocation.x;
+      *y = -allocation.y;
+    }
+  else
+    {
+      *x = 0;
+      *y = 0;
+    }
+
+  widget_window = gtk_widget_get_window (widget);
+
+  for (w = window; w && w != widget_window; w = gdk_window_get_parent (w))
+    {
+      int wx, wy;
+      gdk_window_get_position (w, &wx, &wy);
+      *x += wx;
+      *y += wy;
+    }
+
+  if (w == NULL)
+    {
+      *x = 0;
+      *y = 0;
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+static void
+gimp_spin_scale_event_to_widget_coords (GtkWidget *widget,
+                                        GdkWindow *window,
+                                        gdouble    event_x,
+                                        gdouble    event_y,
+                                        gint      *widget_x,
+                                        gint      *widget_y)
+{
+  gint tx, ty;
+
+  if (gtk_widget_get_translation_to_window (widget, window, &tx, &ty))
+    {
+      event_x += tx;
+      event_y += ty;
+    }
+
+  *widget_x = event_x;
+  *widget_y = event_y;
+}
+
+static gboolean
+gimp_spin_scale_in_text_area (GtkWidget *widget,
+                              gint       x,
+                              gint       y)
+{
+  GdkRectangle area;
+
+  gtk_entry_get_text_area (GTK_ENTRY (widget), &area);
+
+  if (x >= area.x && x < area.width &&
+      y >= area.y && y < area.height)
+    {
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
 static gboolean
 gimp_spin_scale_on_number (GtkWidget *widget,
-                           gdouble    x,
-                           gdouble    y)
+                           gint       x,
+                           gint       y)
 {
   gint layout_x;
   gint layout_y;
@@ -301,18 +355,17 @@ gimp_spin_scale_on_number (GtkWidget *widget,
 
 static void
 gimp_spin_scale_set_value (GtkWidget *widget,
-                           gdouble    x)
+                           gint       x)
 {
   GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
   GtkAdjustment *adjustment  = gtk_spin_button_get_adjustment (spin_button);
-  GdkWindow     *text_window = gtk_entry_get_text_window (GTK_ENTRY (widget));
-  gint           width;
+  GdkRectangle   area;
   gdouble        fraction;
   gdouble        value;
 
-  gdk_drawable_get_size (text_window, &width, NULL);
+  gtk_entry_get_text_area (GTK_ENTRY (widget), &area);
 
-  fraction = x / (gdouble) width;
+  fraction = ((gdouble) x - (gdouble) area.x) / (gdouble) area.width;
 
   value = (fraction * (gtk_adjustment_get_upper (adjustment) -
                        gtk_adjustment_get_lower (adjustment)) +
@@ -326,21 +379,24 @@ gimp_spin_scale_button_press (GtkWidget      *widget,
                               GdkEventButton *event)
 {
   GimpSpinScalePrivate *private = GET_PRIVATE (widget);
+  gint                  x, y;
 
   private->changing_value = FALSE;
 
-  if (event->window == gtk_entry_get_text_window (GTK_ENTRY (widget)))
+  gimp_spin_scale_event_to_widget_coords (widget, event->window,
+                                          event->x, event->y,
+                                          &x, &y);
+
+  if (gimp_spin_scale_in_text_area (widget, x, y) &&
+      ! gimp_spin_scale_on_number (widget, x, y))
     {
-      if (! gimp_spin_scale_on_number (widget, event->x, event->y))
-        {
-          private->changing_value = TRUE;
+      private->changing_value = TRUE;
 
-          gtk_widget_grab_focus (widget);
+      gtk_widget_grab_focus (widget);
 
-          gimp_spin_scale_set_value (widget, event->x);
+      gimp_spin_scale_set_value (widget, x);
 
-          return TRUE;
-        }
+      return TRUE;
     }
 
   return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
@@ -351,12 +407,17 @@ gimp_spin_scale_button_release (GtkWidget      *widget,
                                 GdkEventButton *event)
 {
   GimpSpinScalePrivate *private = GET_PRIVATE (widget);
+  gint                  x, y;
+
+  gimp_spin_scale_event_to_widget_coords (widget, event->window,
+                                          event->x, event->y,
+                                          &x, &y);
 
   if (private->changing_value)
     {
       private->changing_value = FALSE;
 
-      gimp_spin_scale_set_value (widget, event->x);
+      gimp_spin_scale_set_value (widget, x);
 
       return TRUE;
     }
@@ -369,10 +430,15 @@ gimp_spin_scale_button_motion (GtkWidget      *widget,
                                GdkEventMotion *event)
 {
   GimpSpinScalePrivate *private = GET_PRIVATE (widget);
+  gint                  x, y;
+
+  gimp_spin_scale_event_to_widget_coords (widget, event->window,
+                                          event->x, event->y,
+                                          &x, &y);
 
   if (private->changing_value)
     {
-      gimp_spin_scale_set_value (widget, event->x);
+      gimp_spin_scale_set_value (widget, x);
 
       return TRUE;
     }
@@ -381,7 +447,7 @@ gimp_spin_scale_button_motion (GtkWidget      *widget,
 
   if (! (event->state &
          (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) &&
-      event->window == gtk_entry_get_text_window (GTK_ENTRY (widget)))
+      gimp_spin_scale_in_text_area (widget, x, y))
     {
       GdkDisplay *display = gtk_widget_get_display (widget);
       GdkCursor  *cursor;



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