From 56fa2433ef69a260f68b8c3147ad3769ee5c8fdb Mon Sep 17 00:00:00 2001 From: Alex Valavanis Date: Mon, 8 Aug 2016 11:01:15 +0100 Subject: Move GIMP widgets to their own folder (bzr r15043.1.2) --- src/ui/CMakeLists.txt | 4 - src/ui/dialog-events.h | 1 + src/ui/widget/color-icc-selector.cpp | 1 - src/ui/widget/color-notebook.h | 1 - src/ui/widget/color-scales.cpp | 1 - src/ui/widget/color-wheel-selector.cpp | 2 +- src/ui/widget/gimpcolorwheel.c | 1467 -------------------------------- src/ui/widget/gimpcolorwheel.h | 95 --- src/ui/widget/gimpspinscale.c | 969 --------------------- src/ui/widget/gimpspinscale.h | 82 -- src/ui/widget/spin-scale.cpp | 2 +- src/widgets/CMakeLists.txt | 75 +- src/widgets/desktop-widget.cpp | 2 +- src/widgets/ege-adjustment-action.cpp | 2 +- src/widgets/gimp/CMakeLists.txt | 13 + src/widgets/gimp/gimpcolorwheel.c | 1467 ++++++++++++++++++++++++++++++++ src/widgets/gimp/gimpcolorwheel.h | 95 +++ src/widgets/gimp/gimpspinscale.c | 969 +++++++++++++++++++++ src/widgets/gimp/gimpspinscale.h | 82 ++ src/widgets/gimp/ruler.cpp | 1425 +++++++++++++++++++++++++++++++ src/widgets/gimp/ruler.h | 86 ++ src/widgets/ruler.cpp | 1425 ------------------------------- src/widgets/ruler.h | 86 -- 23 files changed, 4179 insertions(+), 4173 deletions(-) delete mode 100644 src/ui/widget/gimpcolorwheel.c delete mode 100644 src/ui/widget/gimpcolorwheel.h delete mode 100644 src/ui/widget/gimpspinscale.c delete mode 100644 src/ui/widget/gimpspinscale.h create mode 100644 src/widgets/gimp/CMakeLists.txt create mode 100644 src/widgets/gimp/gimpcolorwheel.c create mode 100644 src/widgets/gimp/gimpcolorwheel.h create mode 100644 src/widgets/gimp/gimpspinscale.c create mode 100644 src/widgets/gimp/gimpspinscale.h create mode 100644 src/widgets/gimp/ruler.cpp create mode 100644 src/widgets/gimp/ruler.h delete mode 100644 src/widgets/ruler.cpp delete mode 100644 src/widgets/ruler.h (limited to 'src') diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 587974b90..edcc41cd2 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -130,8 +130,6 @@ set(ui_SRC widget/filter-effect-chooser.cpp widget/font-variants.cpp widget/frame.cpp - widget/gimpcolorwheel.c - widget/gimpspinscale.c widget/highlight-picker.cpp widget/imageicon.cpp widget/imagetoggler.cpp @@ -314,8 +312,6 @@ set(ui_SRC widget/filter-effect-chooser.h widget/font-variants.h widget/frame.h - widget/gimpspinscale.h - widget/gimpcolorwheel.h widget/highlight-picker.h widget/insertordericon.h widget/imageicon.h diff --git a/src/ui/dialog-events.h b/src/ui/dialog-events.h index b4a5d7c35..547cfb95d 100644 --- a/src/ui/dialog-events.h +++ b/src/ui/dialog-events.h @@ -12,6 +12,7 @@ #ifndef SEEN_DIALOG_EVENTS_H #define SEEN_DIALOG_EVENTS_H +#include /* * event callback can only accept one argument, but we need two, diff --git a/src/ui/widget/color-icc-selector.cpp b/src/ui/widget/color-icc-selector.cpp index fea6543ea..616e9afa8 100644 --- a/src/ui/widget/color-icc-selector.cpp +++ b/src/ui/widget/color-icc-selector.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include "ui/dialog-events.h" diff --git a/src/ui/widget/color-notebook.h b/src/ui/widget/color-notebook.h index 35e46ef14..0948e3d46 100644 --- a/src/ui/widget/color-notebook.h +++ b/src/ui/widget/color-notebook.h @@ -20,7 +20,6 @@ #include #include -#include #include #include "color.h" diff --git a/src/ui/widget/color-scales.cpp b/src/ui/widget/color-scales.cpp index 7fcca215f..89913b01b 100644 --- a/src/ui/widget/color-scales.cpp +++ b/src/ui/widget/color-scales.cpp @@ -8,7 +8,6 @@ #include #include -#include #include "ui/dialog-events.h" #include "ui/widget/color-scales.h" diff --git a/src/ui/widget/color-wheel-selector.cpp b/src/ui/widget/color-wheel-selector.cpp index 18f932f10..4f011c6c3 100644 --- a/src/ui/widget/color-wheel-selector.cpp +++ b/src/ui/widget/color-wheel-selector.cpp @@ -11,7 +11,7 @@ #include "ui/dialog-events.h" #include "ui/widget/color-scales.h" #include "ui/widget/color-slider.h" -#include "ui/widget/gimpcolorwheel.h" +#include "widgets/gimp/gimpcolorwheel.h" namespace Inkscape { namespace UI { diff --git a/src/ui/widget/gimpcolorwheel.c b/src/ui/widget/gimpcolorwheel.c deleted file mode 100644 index 212391497..000000000 --- a/src/ui/widget/gimpcolorwheel.c +++ /dev/null @@ -1,1467 +0,0 @@ -/* HSV color selector for GTK+ - * - * Copyright (C) 1999 The Free Software Foundation - * - * Authors: Simon Budig (original code) - * Federico Mena-Quintero (cleanup for GTK+) - * Jonathan Blandford (cleanup for GTK+) - * Michael Natterer (ported back to GIMP) - * - * 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 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, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -/* - * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS - * file for a list of people on the GTK+ Team. See the ChangeLog - * files for a list of changes. These files are distributed with - * GTK+ at ftp://ftp.gtk.org/pub/gtk/. - */ - -/* - * This widget was adopted by Inkscape by Alex Valavanis - * on 2013-01-08. Last merges with GIMP code were applied using the following - * commits from the GIMP git repository at - * http://git.gnome.org/browse/gimp/tree/modules/gimpcolorwheel.c - * - * Gtk+ 2 code merge: commit 632c5 (2013-01-06) - * Gtk+ 3 code merge: commit bcfc6, gtk3-port branch (2013-01-06) - */ - -#include "config.h" - -#include -#include - -#include "gimpcolorwheel.h" -#include - - -/* Default ring fraction */ -#define DEFAULT_FRACTION 0.1 - -/* Default width/height */ -#define DEFAULT_SIZE 100 - -/* Default ring width */ -#define DEFAULT_RING_WIDTH 10 - - -/* Dragging modes */ -typedef enum -{ - DRAG_NONE, - DRAG_H, - DRAG_SV -} DragMode; - -/* Private part of the GimpColorWheel structure */ -typedef struct -{ - /* Color value */ - gdouble h; - gdouble s; - gdouble v; - - /* ring_width is this fraction of size */ - gdouble ring_fraction; - - /* Size and ring width */ - gint size; - gint ring_width; - - /* Window for capturing events */ - GdkWindow *window; - - /* Dragging mode */ - DragMode mode; - - guint focus_on_ring : 1; -} GimpColorWheelPrivate; - -enum -{ - CHANGED, - MOVE, - LAST_SIGNAL -}; - -static void gimp_color_wheel_map (GtkWidget *widget); -static void gimp_color_wheel_unmap (GtkWidget *widget); -static void gimp_color_wheel_realize (GtkWidget *widget); -static void gimp_color_wheel_unrealize (GtkWidget *widget); -static void gimp_color_wheel_size_allocate (GtkWidget *widget, - GtkAllocation *allocation); -static gboolean gimp_color_wheel_button_press (GtkWidget *widget, - GdkEventButton *event); -static gboolean gimp_color_wheel_button_release (GtkWidget *widget, - GdkEventButton *event); -static gboolean gimp_color_wheel_motion (GtkWidget *widget, - GdkEventMotion *event); -static gboolean gimp_color_wheel_draw (GtkWidget *widget, - cairo_t *cr); -static void gimp_color_wheel_get_preferred_width (GtkWidget *widget, - gint *minimum_width, - gint *natural_width); -static void gimp_color_wheel_get_preferred_height (GtkWidget *widget, - gint *minimum_height, - gint *natural_height); -static gboolean gimp_color_wheel_grab_broken (GtkWidget *widget, - GdkEventGrabBroken *event); -static gboolean gimp_color_wheel_focus (GtkWidget *widget, - GtkDirectionType direction); -static void gimp_color_wheel_move (GimpColorWheel *wheel, - GtkDirectionType dir); - - -static guint wheel_signals[LAST_SIGNAL]; - -G_DEFINE_TYPE (GimpColorWheel, gimp_color_wheel, GTK_TYPE_WIDGET) - -#define parent_class gimp_color_wheel_parent_class - - -static void -gimp_color_wheel_class_init (GimpColorWheelClass *class) -{ - GObjectClass *object_class = G_OBJECT_CLASS (class); - GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); - GimpColorWheelClass *wheel_class = GIMP_COLOR_WHEEL_CLASS (class); - GtkBindingSet *binding_set; - - widget_class->map = gimp_color_wheel_map; - widget_class->unmap = gimp_color_wheel_unmap; - widget_class->realize = gimp_color_wheel_realize; - widget_class->unrealize = gimp_color_wheel_unrealize; - widget_class->size_allocate = gimp_color_wheel_size_allocate; - widget_class->button_press_event = gimp_color_wheel_button_press; - widget_class->button_release_event = gimp_color_wheel_button_release; - widget_class->motion_notify_event = gimp_color_wheel_motion; - widget_class->get_preferred_width = gimp_color_wheel_get_preferred_width; - widget_class->get_preferred_height = gimp_color_wheel_get_preferred_height; - widget_class->draw = gimp_color_wheel_draw; - widget_class->focus = gimp_color_wheel_focus; - widget_class->grab_broken_event = gimp_color_wheel_grab_broken; - - wheel_class->move = gimp_color_wheel_move; - - wheel_signals[CHANGED] = - g_signal_new ("changed", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (GimpColorWheelClass, changed), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); - - wheel_signals[MOVE] = - g_signal_new ("move", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, - G_STRUCT_OFFSET (GimpColorWheelClass, move), - NULL, NULL, - g_cclosure_marshal_VOID__ENUM, - G_TYPE_NONE, 1, - GTK_TYPE_DIRECTION_TYPE); - - binding_set = gtk_binding_set_by_class (class); - - gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_UP); - gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_UP); - - gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_DOWN); - gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_DOWN); - - - gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_RIGHT); - gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_RIGHT); - - gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_LEFT); - gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0, - "move", 1, - G_TYPE_ENUM, GTK_DIR_LEFT); - - g_type_class_add_private (object_class, sizeof (GimpColorWheelPrivate)); -} - -static void -gimp_color_wheel_init (GimpColorWheel *wheel) -{ - GimpColorWheelPrivate *priv; - - priv = G_TYPE_INSTANCE_GET_PRIVATE (wheel, GIMP_TYPE_COLOR_WHEEL, - GimpColorWheelPrivate); - - wheel->priv = priv; - - gtk_widget_set_has_window (GTK_WIDGET (wheel), FALSE); - gtk_widget_set_can_focus (GTK_WIDGET (wheel), TRUE); - - priv->ring_fraction = DEFAULT_FRACTION; - priv->size = DEFAULT_SIZE; - priv->ring_width = DEFAULT_RING_WIDTH; -} - -static void -gimp_color_wheel_map (GtkWidget *widget) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - - GTK_WIDGET_CLASS (parent_class)->map (widget); - - gdk_window_show (priv->window); -} - -static void -gimp_color_wheel_unmap (GtkWidget *widget) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - - gdk_window_hide (priv->window); - - GTK_WIDGET_CLASS (parent_class)->unmap (widget); -} - -static void -gimp_color_wheel_realize (GtkWidget *widget) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - GtkAllocation allocation; - GdkWindowAttr attr; - gint attr_mask; - GdkWindow *parent_window; - - gtk_widget_get_allocation (widget, &allocation); - - gtk_widget_set_realized (widget, TRUE); - - attr.window_type = GDK_WINDOW_CHILD; - attr.x = allocation.x; - attr.y = allocation.y; - attr.width = allocation.width; - attr.height = allocation.height; - attr.wclass = GDK_INPUT_ONLY; - attr.event_mask = (gtk_widget_get_events (widget) | - GDK_KEY_PRESS_MASK | - GDK_BUTTON_PRESS_MASK | - GDK_BUTTON_RELEASE_MASK | - GDK_POINTER_MOTION_MASK | - GDK_ENTER_NOTIFY_MASK | - GDK_LEAVE_NOTIFY_MASK); - - attr_mask = GDK_WA_X | GDK_WA_Y; - - parent_window = gtk_widget_get_parent_window (widget); - - gtk_widget_set_window (widget, parent_window); - g_object_ref (parent_window); - - priv->window = gdk_window_new (parent_window, &attr, attr_mask); - gdk_window_set_user_data (priv->window, wheel); -} - -static void -gimp_color_wheel_unrealize (GtkWidget *widget) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - - gdk_window_set_user_data (priv->window, NULL); - gdk_window_destroy (priv->window); - priv->window = NULL; - - GTK_WIDGET_CLASS (parent_class)->unrealize (widget); -} - -static void -gimp_color_wheel_get_preferred_width (GtkWidget *widget, - gint *minimum_width, - gint *natural_width) -{ - gint focus_width; - gint focus_pad; - - gtk_widget_style_get (widget, - "focus-line-width", &focus_width, - "focus-padding", &focus_pad, - NULL); - - *minimum_width = *natural_width = DEFAULT_SIZE + 2 * (focus_width + focus_pad); -} - -static void -gimp_color_wheel_get_preferred_height (GtkWidget *widget, - gint *minimum_height, - gint *natural_height) -{ - gint focus_width; - gint focus_pad; - - gtk_widget_style_get (widget, - "focus-line-width", &focus_width, - "focus-padding", &focus_pad, - NULL); - - *minimum_height = *natural_height = DEFAULT_SIZE + 2 * (focus_width + focus_pad); -} - -static void -gimp_color_wheel_size_allocate (GtkWidget *widget, - GtkAllocation *allocation) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - gint focus_width; - gint focus_pad; - - gtk_widget_set_allocation (widget, allocation); - - gtk_widget_style_get (widget, - "focus-line-width", &focus_width, - "focus-padding", &focus_pad, - NULL); - - priv->size = MIN (allocation->width - 2 * (focus_width + focus_pad), - allocation->height - 2 * (focus_width + focus_pad)); - - priv->ring_width = priv->size * priv->ring_fraction; - - if (gtk_widget_get_realized (widget)) - gdk_window_move_resize (priv->window, - allocation->x, - allocation->y, - allocation->width, - allocation->height); -} - - -/* Utility functions */ - -#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11) - -/* Converts from HSV to RGB */ -static void -hsv_to_rgb (gdouble *h, - gdouble *s, - gdouble *v) -{ - gdouble hue, saturation, value; - gdouble f, p, q, t; - - if (*s == 0.0) - { - *h = *v; - *s = *v; - *v = *v; /* heh */ - } - else - { - hue = *h * 6.0; - saturation = *s; - value = *v; - - if (hue == 6.0) - hue = 0.0; - - f = hue - (int) hue; - p = value * (1.0 - saturation); - q = value * (1.0 - saturation * f); - t = value * (1.0 - saturation * (1.0 - f)); - - switch ((int) hue) - { - case 0: - *h = value; - *s = t; - *v = p; - break; - - case 1: - *h = q; - *s = value; - *v = p; - break; - - case 2: - *h = p; - *s = value; - *v = t; - break; - - case 3: - *h = p; - *s = q; - *v = value; - break; - - case 4: - *h = t; - *s = p; - *v = value; - break; - - case 5: - *h = value; - *s = p; - *v = q; - break; - - default: - g_assert_not_reached (); - } - } -} - -/* Computes the vertices of the saturation/value triangle */ -static void -compute_triangle (GimpColorWheel *wheel, - gint *hx, - gint *hy, - gint *sx, - gint *sy, - gint *vx, - gint *vy) -{ - GimpColorWheelPrivate *priv = wheel->priv; - GtkAllocation allocation; - gdouble center_x; - gdouble center_y; - gdouble inner, outer; - gdouble angle; - - gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); - - center_x = allocation.width / 2.0; - center_y = allocation.height / 2.0; - - outer = priv->size / 2.0; - inner = outer - priv->ring_width; - angle = priv->h * 2.0 * G_PI; - - *hx = floor (center_x + cos (angle) * inner + 0.5); - *hy = floor (center_y - sin (angle) * inner + 0.5); - *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5); - *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5); - *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5); - *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5); -} - -/* Computes whether a point is inside the hue ring */ -static gboolean -is_in_ring (GimpColorWheel *wheel, - gdouble x, - gdouble y) -{ - GimpColorWheelPrivate *priv = wheel->priv; - GtkAllocation allocation; - gdouble dx, dy, dist; - gdouble center_x; - gdouble center_y; - gdouble inner, outer; - - gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); - - center_x = allocation.width / 2.0; - center_y = allocation.height / 2.0; - - outer = priv->size / 2.0; - inner = outer - priv->ring_width; - - dx = x - center_x; - dy = center_y - y; - dist = dx * dx + dy * dy; - - return (dist >= inner * inner && dist <= outer * outer); -} - -/* Computes a saturation/value pair based on the mouse coordinates */ -static void -compute_sv (GimpColorWheel *wheel, - gdouble x, - gdouble y, - gdouble *s, - gdouble *v) -{ - GtkAllocation allocation; - gint ihx, ihy, isx, isy, ivx, ivy; - gdouble hx, hy, sx, sy, vx, vy; - gdouble center_x; - gdouble center_y; - - gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); - - compute_triangle (wheel, &ihx, &ihy, &isx, &isy, &ivx, &ivy); - - center_x = allocation.width / 2.0; - center_y = allocation.height / 2.0; - - hx = ihx - center_x; - hy = center_y - ihy; - sx = isx - center_x; - sy = center_y - isy; - vx = ivx - center_x; - vy = center_y - ivy; - x -= center_x; - y = center_y - y; - - if (vx * (x - sx) + vy * (y - sy) < 0.0) - { - *s = 1.0; - *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy)) - / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy))); - - if (*v < 0.0) - *v = 0.0; - else if (*v > 1.0) - *v = 1.0; - } - else if (hx * (x - sx) + hy * (y - sy) < 0.0) - { - *s = 0.0; - *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy)) - / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy))); - - if (*v < 0.0) - *v = 0.0; - else if (*v > 1.0) - *v = 1.0; - } - else if (sx * (x - hx) + sy * (y - hy) < 0.0) - { - *v = 1.0; - *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) / - ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy))); - - if (*s < 0.0) - *s = 0.0; - else if (*s > 1.0) - *s = 1.0; - } - else - { - *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx)) - / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx))); - - if (*v<= 0.0) - { - *v = 0.0; - *s = 0.0; - } - else - { - if (*v > 1.0) - *v = 1.0; - - if (fabs (hy - vy) < fabs (hx - vx)) - *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx)); - else - *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy)); - - if (*s < 0.0) - *s = 0.0; - else if (*s > 1.0) - *s = 1.0; - } - } -} - -/* Computes whether a point is inside the saturation/value triangle */ -static gboolean -is_in_triangle (GimpColorWheel *wheel, - gdouble x, - gdouble y) -{ - gint hx, hy, sx, sy, vx, vy; - gdouble det, s, v; - - compute_triangle (wheel, &hx, &hy, &sx, &sy, &vx, &vy); - - det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx); - - s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det; - v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det; - - return (s >= 0.0 && v >= 0.0 && s + v <= 1.0); -} - -/* Computes a value based on the mouse coordinates */ -static double -compute_v (GimpColorWheel *wheel, - gdouble x, - gdouble y) -{ - GtkAllocation allocation; - gdouble center_x; - gdouble center_y; - gdouble dx, dy; - gdouble angle; - - gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); - - center_x = allocation.width / 2.0; - center_y = allocation.height / 2.0; - - dx = x - center_x; - dy = center_y - y; - - angle = atan2 (dy, dx); - if (angle < 0.0) - angle += 2.0 * G_PI; - - return angle / (2.0 * G_PI); -} - -static void -set_cross_grab (GimpColorWheel *wheel, - guint32 time) -{ - GimpColorWheelPrivate *priv = wheel->priv; - GdkCursor *cursor; - - cursor = - gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (wheel)), - GDK_CROSSHAIR); - - gdk_device_grab (gtk_get_current_event_device(), - priv->window, - GDK_OWNERSHIP_NONE, - FALSE, - GDK_POINTER_MOTION_MASK | - GDK_POINTER_MOTION_HINT_MASK | - GDK_BUTTON_RELEASE_MASK, - cursor, time); - g_object_unref (cursor); -} - -static gboolean gimp_color_wheel_grab_broken(GtkWidget *widget, GdkEventGrabBroken *event) -{ - (void)event; - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - - priv->mode = DRAG_NONE; - - return TRUE; -} - -static gboolean -gimp_color_wheel_button_press (GtkWidget *widget, - GdkEventButton *event) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - gdouble x, y; - - if (priv->mode != DRAG_NONE || event->button != 1) - return FALSE; - - x = event->x; - y = event->y; - - if (is_in_ring (wheel, x, y)) - { - priv->mode = DRAG_H; - set_cross_grab (wheel, event->time); - - gimp_color_wheel_set_color (wheel, - compute_v (wheel, x, y), - priv->s, - priv->v); - - gtk_widget_grab_focus (widget); - priv->focus_on_ring = TRUE; - - return TRUE; - } - - if (is_in_triangle (wheel, x, y)) - { - gdouble s, v; - - priv->mode = DRAG_SV; - set_cross_grab (wheel, event->time); - - compute_sv (wheel, x, y, &s, &v); - gimp_color_wheel_set_color (wheel, priv->h, s, v); - - gtk_widget_grab_focus (widget); - priv->focus_on_ring = FALSE; - - return TRUE; - } - - return FALSE; -} - -static gboolean -gimp_color_wheel_button_release (GtkWidget *widget, - GdkEventButton *event) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - DragMode mode; - gdouble x, y; - - if (priv->mode == DRAG_NONE || event->button != 1) - return FALSE; - - /* Set the drag mode to DRAG_NONE so that signal handlers for "catched" - * can see that this is the final color state. - */ - - mode = priv->mode; - priv->mode = DRAG_NONE; - - x = event->x; - y = event->y; - - if (mode == DRAG_H) - { - gimp_color_wheel_set_color (wheel, - compute_v (wheel, x, y), priv->s, priv->v); - } - else if (mode == DRAG_SV) - { - gdouble s, v; - - compute_sv (wheel, x, y, &s, &v); - gimp_color_wheel_set_color (wheel, priv->h, s, v); - } - else - g_assert_not_reached (); - - gdk_device_ungrab (gtk_get_current_event_device(), - event->time); - - return TRUE; -} - -static gboolean -gimp_color_wheel_motion (GtkWidget *widget, - GdkEventMotion *event) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - gdouble x, y; - - if (priv->mode == DRAG_NONE) - return FALSE; - - gdk_event_request_motions (event); - x = event->x; - y = event->y; - - if (priv->mode == DRAG_H) - { - gimp_color_wheel_set_color (wheel, - compute_v (wheel, x, y), priv->s, priv->v); - return TRUE; - } - else if (priv->mode == DRAG_SV) - { - gdouble s, v; - - compute_sv (wheel, x, y, &s, &v); - gimp_color_wheel_set_color (wheel, priv->h, s, v); - return TRUE; - } - - g_assert_not_reached (); - - return FALSE; -} - - -/* Redrawing */ - -/* Paints the hue ring */ -static void -paint_ring (GimpColorWheel *wheel, - cairo_t *cr) -{ - GtkWidget *widget = GTK_WIDGET (wheel); - GimpColorWheelPrivate *priv = wheel->priv; - gint width, height; - gint xx, yy; - gdouble dx, dy, dist; - gdouble center_x; - gdouble center_y; - gdouble inner, outer; - guint32 *buf, *p; - gdouble angle; - gdouble hue; - gdouble r, g, b; - cairo_surface_t *source; - cairo_t *source_cr; - gint stride; - - width = gtk_widget_get_allocated_width (widget); - height = gtk_widget_get_allocated_height (widget); - - center_x = width / 2.0; - center_y = height / 2.0; - - outer = priv->size / 2.0; - inner = outer - priv->ring_width; - - /* Create an image initialized with the ring colors */ - - stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width); - buf = g_new (guint32, height * stride / 4); - - for (yy = 0; yy < height; yy++) - { - p = buf + yy * width; - dy = -(yy - center_y); - - for (xx = 0; xx < width; xx++) - { - dx = xx - center_x; - - dist = dx * dx + dy * dy; - if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1))) - { - *p++ = 0; - continue; - } - - angle = atan2 (dy, dx); - if (angle < 0.0) - angle += 2.0 * G_PI; - - hue = angle / (2.0 * G_PI); - - r = hue; - g = 1.0; - b = 1.0; - hsv_to_rgb (&r, &g, &b); - - *p++ = (((int)floor (r * 255 + 0.5) << 16) | - ((int)floor (g * 255 + 0.5) << 8) | - (int)floor (b * 255 + 0.5)); - } - } - - source = cairo_image_surface_create_for_data ((unsigned char *)buf, - CAIRO_FORMAT_RGB24, - width, height, stride); - - /* Now draw the value marker onto the source image, so that it - * will get properly clipped at the edges of the ring - */ - source_cr = cairo_create (source); - - r = priv->h; - g = 1.0; - b = 1.0; - hsv_to_rgb (&r, &g, &b); - - if (INTENSITY (r, g, b) > 0.5) - cairo_set_source_rgb (source_cr, 0.0, 0.0, 0.0); - else - cairo_set_source_rgb (source_cr, 1.0, 1.0, 1.0); - - cairo_move_to (source_cr, center_x, center_y); - cairo_line_to (source_cr, - center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2, - center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2); - cairo_stroke (source_cr); - cairo_destroy (source_cr); - - /* Draw the ring using the source image */ - - cairo_save (cr); - - cairo_set_source_surface (cr, source, 0, 0); - cairo_surface_destroy (source); - - cairo_set_line_width (cr, priv->ring_width); - cairo_new_path (cr); - cairo_arc (cr, - center_x, center_y, - priv->size / 2.0 - priv->ring_width / 2.0, - 0, 2 * G_PI); - cairo_stroke (cr); - - cairo_restore (cr); - - g_free (buf); -} - -/* Converts an HSV triplet to an integer RGB triplet */ -static void -get_color (gdouble h, - gdouble s, - gdouble v, - gint *r, - gint *g, - gint *b) -{ - hsv_to_rgb (&h, &s, &v); - - *r = floor (h * 255 + 0.5); - *g = floor (s * 255 + 0.5); - *b = floor (v * 255 + 0.5); -} - -#define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t)) - -#define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \ - ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \ - : (a)) - -/* Number of pixels we extend out from the edges when creating - * color source to avoid artifacts - */ -#define PAD 3 - -/* Paints the HSV triangle */ -static void -paint_triangle (GimpColorWheel *wheel, - cairo_t *cr, - gboolean draw_focus) -{ - GtkWidget *widget = GTK_WIDGET (wheel); - GimpColorWheelPrivate *priv = wheel->priv; - gint hx, hy, sx, sy, vx, vy; /* HSV vertices */ - gint x1, y1, r1, g1, b1; /* First vertex in scanline order */ - gint x2, y2, r2, g2, b2; /* Second vertex */ - gint x3, y3, r3, g3, b3; /* Third vertex */ - gint t; - guint32 *buf, *p, c; - gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */ - gint xx, yy; - gint x_interp, y_interp; - gint x_start, x_end; - cairo_surface_t *source; - gdouble r, g, b; - gint stride; - gint width, height; - GtkStyleContext *context; - - width = gtk_widget_get_allocated_width (widget); - height = gtk_widget_get_allocated_height (widget); - - /* Compute triangle's vertices */ - - compute_triangle (wheel, &hx, &hy, &sx, &sy, &vx, &vy); - - x1 = hx; - y1 = hy; - get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1); - - x2 = sx; - y2 = sy; - get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2); - - x3 = vx; - y3 = vy; - get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3); - - if (y2 > y3) - { - SWAP (x2, x3, t); - SWAP (y2, y3, t); - SWAP (r2, r3, t); - SWAP (g2, g3, t); - SWAP (b2, b3, t); - } - - if (y1 > y3) - { - SWAP (x1, x3, t); - SWAP (y1, y3, t); - SWAP (r1, r3, t); - SWAP (g1, g3, t); - SWAP (b1, b3, t); - } - - if (y1 > y2) - { - SWAP (x1, x2, t); - SWAP (y1, y2, t); - SWAP (r1, r2, t); - SWAP (g1, g2, t); - SWAP (b1, b2, t); - } - - /* Shade the triangle */ - - stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width); - buf = g_new (guint32, height * stride / 4); - - for (yy = 0; yy < height; yy++) - { - p = buf + yy * width; - - if (yy >= y1 - PAD && yy < y3 + PAD) - { - y_interp = CLAMP (yy, y1, y3); - - if (y_interp < y2) - { - xl = LERP (x1, x2, y1, y2, y_interp); - - rl = LERP (r1, r2, y1, y2, y_interp); - gl = LERP (g1, g2, y1, y2, y_interp); - bl = LERP (b1, b2, y1, y2, y_interp); - } - else - { - xl = LERP (x2, x3, y2, y3, y_interp); - - rl = LERP (r2, r3, y2, y3, y_interp); - gl = LERP (g2, g3, y2, y3, y_interp); - bl = LERP (b2, b3, y2, y3, y_interp); - } - - xr = LERP (x1, x3, y1, y3, y_interp); - - rr = LERP (r1, r3, y1, y3, y_interp); - gr = LERP (g1, g3, y1, y3, y_interp); - br = LERP (b1, b3, y1, y3, y_interp); - - if (xl > xr) - { - SWAP (xl, xr, t); - SWAP (rl, rr, t); - SWAP (gl, gr, t); - SWAP (bl, br, t); - } - - x_start = MAX (xl - PAD, 0); - x_end = MIN (xr + PAD, width); - x_start = MIN (x_start, x_end); - - c = (rl << 16) | (gl << 8) | bl; - - for (xx = 0; xx < x_start; xx++) - *p++ = c; - - for (; xx < x_end; xx++) - { - x_interp = CLAMP (xx, xl, xr); - - *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) | - (LERP (gl, gr, xl, xr, x_interp) << 8) | - LERP (bl, br, xl, xr, x_interp)); - } - - c = (rr << 16) | (gr << 8) | br; - - for (; xx < width; xx++) - *p++ = c; - } - } - - source = cairo_image_surface_create_for_data ((unsigned char *)buf, - CAIRO_FORMAT_RGB24, - width, height, stride); - - /* Draw a triangle with the image as a source */ - - cairo_set_source_surface (cr, source, 0, 0); - cairo_surface_destroy (source); - - cairo_move_to (cr, x1, y1); - cairo_line_to (cr, x2, y2); - cairo_line_to (cr, x3, y3); - cairo_close_path (cr); - cairo_fill (cr); - - g_free (buf); - - /* Draw value marker */ - - xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5); - yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5); - - r = priv->h; - g = priv->s; - b = priv->v; - hsv_to_rgb (&r, &g, &b); - - context = gtk_widget_get_style_context (widget); - - gtk_style_context_save (context); - - if (INTENSITY (r, g, b) > 0.5) - { - gtk_style_context_add_class (context, "light-area-focus"); - cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); - } - else - { - gtk_style_context_add_class (context, "dark-area-focus"); - cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); - } - -#define RADIUS 4 -#define FOCUS_RADIUS 6 - - cairo_new_path (cr); - cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI); - cairo_stroke (cr); - - /* Draw focus outline */ - - if (draw_focus && ! priv->focus_on_ring) - { - gint focus_width; - gint focus_pad; - - gtk_widget_style_get (widget, - "focus-line-width", &focus_width, - "focus-padding", &focus_pad, - NULL); - - gtk_render_focus (context, cr, - xx - FOCUS_RADIUS - focus_width - focus_pad, - yy - FOCUS_RADIUS - focus_width - focus_pad, - 2 * (FOCUS_RADIUS + focus_width + focus_pad), - 2 * (FOCUS_RADIUS + focus_width + focus_pad)); - } - - gtk_style_context_restore (context); -} - -static gboolean -gimp_color_wheel_draw (GtkWidget *widget, - cairo_t *cr) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - gboolean draw_focus; - - draw_focus = gtk_widget_has_visible_focus (widget); - - paint_ring (wheel, cr); - paint_triangle (wheel, cr, draw_focus); - - if (draw_focus && priv->focus_on_ring) - { - GtkStyleContext *context = gtk_widget_get_style_context (widget); - - gtk_render_focus (context, cr, 0, 0, - gtk_widget_get_allocated_width (widget), - gtk_widget_get_allocated_height (widget)); - } - - return FALSE; -} - -static gboolean -gimp_color_wheel_focus (GtkWidget *widget, - GtkDirectionType dir) -{ - GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); - GimpColorWheelPrivate *priv = wheel->priv; - - if (!gtk_widget_has_focus (widget)) - { - if (dir == GTK_DIR_TAB_BACKWARD) - priv->focus_on_ring = FALSE; - else - priv->focus_on_ring = TRUE; - - gtk_widget_grab_focus (widget); - return TRUE; - } - - switch (dir) - { - case GTK_DIR_UP: - if (priv->focus_on_ring) - return FALSE; - else - priv->focus_on_ring = TRUE; - break; - - case GTK_DIR_DOWN: - if (priv->focus_on_ring) - priv->focus_on_ring = FALSE; - else - return FALSE; - break; - - case GTK_DIR_LEFT: - case GTK_DIR_TAB_BACKWARD: - if (priv->focus_on_ring) - return FALSE; - else - priv->focus_on_ring = TRUE; - break; - - case GTK_DIR_RIGHT: - case GTK_DIR_TAB_FORWARD: - if (priv->focus_on_ring) - priv->focus_on_ring = FALSE; - else - return FALSE; - break; - } - - gtk_widget_queue_draw (widget); - - return TRUE; -} - -/** - * gimp_color_wheel_new: - * - * Creates a new HSV color selector. - * - * Return value: A newly-created HSV color selector. - * - * Since: 2.14 - */ -GtkWidget* -gimp_color_wheel_new (void) -{ - return g_object_new (GIMP_TYPE_COLOR_WHEEL, NULL); -} - -/** - * gimp_color_wheel_set_color: - * @hsv: An HSV color selector - * @h: Hue - * @s: Saturation - * @v: Value - * - * Sets the current color in an HSV color selector. - * Color component values must be in the [0.0, 1.0] range. - * - * Since: 2.14 - */ -void -gimp_color_wheel_set_color (GimpColorWheel *wheel, - gdouble h, - gdouble s, - gdouble v) -{ - GimpColorWheelPrivate *priv; - - g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel)); - g_return_if_fail (h >= 0.0 && h <= 1.0); - g_return_if_fail (s >= 0.0 && s <= 1.0); - g_return_if_fail (v >= 0.0 && v <= 1.0); - - priv = wheel->priv; - - if(h == 0.0 && s == 0.0) { - h = priv->h; - } - - priv->h = h; - priv->s = s; - priv->v = v; - - g_signal_emit (wheel, wheel_signals[CHANGED], 0); - - gtk_widget_queue_draw (GTK_WIDGET (wheel)); -} - -/** - * gimp_color_wheel_get_color: - * @hsv: An HSV color selector - * @h: (out): Return value for the hue - * @s: (out): Return value for the saturation - * @v: (out): Return value for the value - * - * Queries the current color in an HSV color selector. - * Returned values will be in the [0.0, 1.0] range. - * - * Since: 2.14 - */ -void -gimp_color_wheel_get_color (GimpColorWheel *wheel, - gdouble *h, - gdouble *s, - gdouble *v) -{ - GimpColorWheelPrivate *priv; - - g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel)); - - priv = wheel->priv; - - if (h) *h = priv->h; - if (s) *s = priv->s; - if (v) *v = priv->v; -} - -/** - * gimp_color_wheel_set_ring_fraction: - * @ring: A wheel color selector - * @fraction: Ring fraction - * - * Sets the ring fraction of a wheel color selector. - * - * Since: GIMP 2.10 - */ -void -gimp_color_wheel_set_ring_fraction (GimpColorWheel *hsv, - gdouble fraction) -{ - GimpColorWheelPrivate *priv; - - g_return_if_fail (GIMP_IS_COLOR_WHEEL (hsv)); - - priv = hsv->priv; - - priv->ring_fraction = CLAMP (fraction, 0.01, 0.99); - - gtk_widget_queue_draw (GTK_WIDGET (hsv)); -} - -/** - * gimp_color_wheel_get_ring_fraction: - * @ring: A wheel color selector - * - * Returns value: The ring fraction of the wheel color selector. - * - * Since: GIMP 2.10 - */ -gdouble -gimp_color_wheel_get_ring_fraction (GimpColorWheel *wheel) -{ - GimpColorWheelPrivate *priv; - - g_return_val_if_fail (GIMP_IS_COLOR_WHEEL (wheel), DEFAULT_FRACTION); - - priv = wheel->priv; - - return priv->ring_fraction; -} - -/** - * gimp_color_wheel_is_adjusting: - * @hsv: A #GimpColorWheel - * - * An HSV color selector can be said to be adjusting if multiple rapid - * changes are being made to its value, for example, when the user is - * adjusting the value with the mouse. This function queries whether - * the HSV color selector is being adjusted or not. - * - * Return value: %TRUE if clients can ignore changes to the color value, - * since they may be transitory, or %FALSE if they should consider - * the color value status to be final. - * - * Since: 2.14 - */ -gboolean -gimp_color_wheel_is_adjusting (GimpColorWheel *wheel) -{ - GimpColorWheelPrivate *priv; - - g_return_val_if_fail (GIMP_IS_COLOR_WHEEL (wheel), FALSE); - - priv = wheel->priv; - - return priv->mode != DRAG_NONE; -} - -static void -gimp_color_wheel_move (GimpColorWheel *wheel, - GtkDirectionType dir) -{ - GimpColorWheelPrivate *priv = wheel->priv; - gdouble hue, sat, val; - gint hx, hy, sx, sy, vx, vy; /* HSV vertices */ - gint x, y; /* position in triangle */ - - hue = priv->h; - sat = priv->s; - val = priv->v; - - compute_triangle (wheel, &hx, &hy, &sx, &sy, &vx, &vy); - - x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5); - y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5); - -#define HUE_DELTA 0.002 - switch (dir) - { - case GTK_DIR_UP: - if (priv->focus_on_ring) - hue += HUE_DELTA; - else - { - y -= 1; - compute_sv (wheel, x, y, &sat, &val); - } - break; - - case GTK_DIR_DOWN: - if (priv->focus_on_ring) - hue -= HUE_DELTA; - else - { - y += 1; - compute_sv (wheel, x, y, &sat, &val); - } - break; - - case GTK_DIR_LEFT: - if (priv->focus_on_ring) - hue += HUE_DELTA; - else - { - x -= 1; - compute_sv (wheel, x, y, &sat, &val); - } - break; - - case GTK_DIR_RIGHT: - if (priv->focus_on_ring) - hue -= HUE_DELTA - ; - else - { - x += 1; - compute_sv (wheel, x, y, &sat, &val); - } - break; - - default: - /* we don't care about the tab directions */ - break; - } - - /* Wrap */ - if (hue < 0.0) - hue = 1.0; - else if (hue > 1.0) - hue = 0.0; - - gimp_color_wheel_set_color (wheel, hue, sat, val); -} diff --git a/src/ui/widget/gimpcolorwheel.h b/src/ui/widget/gimpcolorwheel.h deleted file mode 100644 index 016fb593f..000000000 --- a/src/ui/widget/gimpcolorwheel.h +++ /dev/null @@ -1,95 +0,0 @@ -/* HSV color selector for GTK+ - * - * Copyright (C) 1999 The Free Software Foundation - * - * Authors: Simon Budig (original code) - * Federico Mena-Quintero (cleanup for GTK+) - * Jonathan Blandford (cleanup for GTK+) - * - * 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 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, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -/* - * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS - * file for a list of people on the GTK+ Team. See the ChangeLog - * files for a list of changes. These files are distributed with - * GTK+ at ftp://ftp.gtk.org/pub/gtk/. - */ - -#ifndef __GIMP_COLOR_WHEEL_H__ -#define __GIMP_COLOR_WHEEL_H__ - -G_BEGIN_DECLS - -#define GIMP_TYPE_COLOR_WHEEL (gimp_color_wheel_get_type ()) -#define GIMP_COLOR_WHEEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_COLOR_WHEEL, GimpColorWheel)) -#define GIMP_COLOR_WHEEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_COLOR_WHEEL, GimpColorWheelClass)) -#define GIMP_IS_COLOR_WHEEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_COLOR_WHEEL)) -#define GIMP_IS_COLOR_WHEEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_COLOR_WHEEL)) -#define GIMP_COLOR_WHEEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_COLOR_WHEEL, GimpColorWheelClass)) - - -typedef struct _GimpColorWheel GimpColorWheel; -typedef struct _GimpColorWheelClass GimpColorWheelClass; - -struct _GimpColorWheel -{ - GtkWidget parent_instance; - - /* Private data */ - gpointer priv; -}; - -struct _GimpColorWheelClass -{ - GtkWidgetClass parent_class; - - /* Notification signals */ - void (* changed) (GimpColorWheel *wheel); - - /* Keybindings */ - void (* move) (GimpColorWheel *wheel, - GtkDirectionType type); - - /* Padding for future expansion */ - void (*_gimp_reserved1) (void); - void (*_gimp_reserved2) (void); - void (*_gimp_reserved3) (void); - void (*_gimp_reserved4) (void); -}; - - -GType gimp_color_wheel_get_type (void) G_GNUC_CONST; -GtkWidget * gimp_color_wheel_new (void); - -void gimp_color_wheel_set_color (GimpColorWheel *wheel, - double h, - double s, - double v); -void gimp_color_wheel_get_color (GimpColorWheel *wheel, - gdouble *h, - gdouble *s, - gdouble *v); - -void gimp_color_wheel_set_ring_fraction (GimpColorWheel *wheel, - gdouble fraction); -gdouble gimp_color_wheel_get_ring_fraction (GimpColorWheel *wheel); - -gboolean gimp_color_wheel_is_adjusting (GimpColorWheel *wheel); - -G_END_DECLS - -#endif /* __GIMP_COLOR_WHEEL_H__ */ diff --git a/src/ui/widget/gimpspinscale.c b/src/ui/widget/gimpspinscale.c deleted file mode 100644 index 8d8c6c935..000000000 --- a/src/ui/widget/gimpspinscale.c +++ /dev/null @@ -1,969 +0,0 @@ -/* GIMP - The GNU Image Manipulation Program - * Copyright (C) 1995 Spencer Kimball and Peter Mattis - * - * gimpspinscale.c - * Copyright (C) 2010 Michael Natterer - * 2012 Øyvind Kolås - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include -#include -#include - -#include "gimpspinscale.h" - - -enum -{ - PROP_0, - PROP_LABEL, - PROP_FOCUS_WIDGET -}; - -typedef enum -{ - TARGET_NUMBER, - TARGET_UPPER, - TARGET_LOWER, - TARGET_NONE -} SpinScaleTarget; - -typedef enum -{ - APPEARANCE_FULL = 1, /* Full size suitable for tablets */ - APPEARANCE_COMPACT, /* Compact, suitable for desktops with mouse control */ -} SpinScaleAppearance; - -typedef struct _GimpSpinScalePrivate GimpSpinScalePrivate; - -struct _GimpSpinScalePrivate -{ - gchar *label; - - gboolean scale_limits_set; - gdouble scale_lower; - gdouble scale_upper; - gdouble gamma; - - PangoLayout *layout; - gboolean changing_value; - gboolean relative_change; - gdouble start_x; - gdouble start_value; - - GtkWidget* focusWidget; - gboolean transferFocus; - SpinScaleAppearance appearanceMode; -}; - -#define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \ - GIMP_TYPE_SPIN_SCALE, \ - GimpSpinScalePrivate)) - - -static void gimp_spin_scale_dispose (GObject *object); -static void gimp_spin_scale_finalize (GObject *object); -static void gimp_spin_scale_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec); -static void gimp_spin_scale_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec); - -static void gimp_spin_scale_style_set (GtkWidget *widget, - GtkStyle *prev_style); - -static void gimp_spin_scale_get_preferred_width (GtkWidget *widget, - gint *minimum_width, - gint *natural_width); -static void gimp_spin_scale_get_preferred_height (GtkWidget *widget, - gint *minimum_width, - gint *natural_width); -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, - GdkEventButton *event); -static gboolean gimp_spin_scale_motion_notify (GtkWidget *widget, - GdkEventMotion *event); -static gboolean gimp_spin_scale_leave_notify (GtkWidget *widget, - GdkEventCrossing *event); -static gboolean gimp_spin_scale_keypress( GtkWidget *widget, - GdkEventKey *event); - -static void gimp_spin_scale_defocus( GtkSpinButton *spin_button ); - -static void gimp_spin_scale_value_changed (GtkSpinButton *spin_button); - - -G_DEFINE_TYPE (GimpSpinScale, gimp_spin_scale, GTK_TYPE_SPIN_BUTTON); - -#define parent_class gimp_spin_scale_parent_class - - -static void -gimp_spin_scale_class_init (GimpSpinScaleClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); - GtkSpinButtonClass *spin_button_class = GTK_SPIN_BUTTON_CLASS (klass); - - object_class->dispose = gimp_spin_scale_dispose; - object_class->finalize = gimp_spin_scale_finalize; - object_class->set_property = gimp_spin_scale_set_property; - object_class->get_property = gimp_spin_scale_get_property; - - widget_class->style_set = gimp_spin_scale_style_set; - widget_class->get_preferred_width = gimp_spin_scale_get_preferred_width; - widget_class->get_preferred_height = gimp_spin_scale_get_preferred_height; - 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_motion_notify; - widget_class->leave_notify_event = gimp_spin_scale_leave_notify; - widget_class->key_press_event = gimp_spin_scale_keypress; - - spin_button_class->value_changed = gimp_spin_scale_value_changed; - - g_object_class_install_property (object_class, PROP_LABEL, - g_param_spec_string ("label", NULL, NULL, - NULL, - G_PARAM_READWRITE)); - - g_type_class_add_private (klass, sizeof (GimpSpinScalePrivate)); -} - -static void -gimp_spin_scale_init (GimpSpinScale *scale) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (scale); - - gtk_widget_add_events (GTK_WIDGET (scale), - GDK_BUTTON_PRESS_MASK | - GDK_BUTTON_RELEASE_MASK | - GDK_POINTER_MOTION_MASK | - GDK_BUTTON1_MOTION_MASK | - GDK_LEAVE_NOTIFY_MASK); - - gtk_entry_set_alignment (GTK_ENTRY (scale), 1.0); - gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (scale), TRUE); - - private->gamma = 1.0; - private->focusWidget = NULL; - private->transferFocus = FALSE; - private->appearanceMode = APPEARANCE_COMPACT; -} - -static void -gimp_spin_scale_dispose (GObject *object) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (object); - - if (private->layout) - { - g_object_unref (private->layout); - private->layout = NULL; - } - - G_OBJECT_CLASS (parent_class)->dispose (object); -} - -static void -gimp_spin_scale_finalize (GObject *object) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (object); - - if (private->label) - { - g_free (private->label); - private->label = NULL; - } - - G_OBJECT_CLASS (parent_class)->finalize (object); -} - -static void -gimp_spin_scale_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (object); - GimpSpinScale *scale = GIMP_SPIN_SCALE (object); - - switch (property_id) - { - case PROP_LABEL: - gimp_spin_scale_set_label (scale, g_value_get_string (value)); - break; - - case PROP_FOCUS_WIDGET: - { - /* TODO unhook prior */ - private->focusWidget = GTK_WIDGET (g_value_get_pointer (value)); - } - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } -} - -static void -gimp_spin_scale_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (object); - GimpSpinScale *scale = GIMP_SPIN_SCALE (object); - - switch (property_id) - { - case PROP_LABEL: - g_value_set_string (value, gimp_spin_scale_get_label (scale)); - break; - - case PROP_FOCUS_WIDGET: - g_value_set_pointer (value, private->focusWidget); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } -} - - -void -gimp_spin_scale_set_focuswidget( GtkWidget *scale, GtkWidget* widget ) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (scale); - - /* TODO unhook prior */ - - private->focusWidget = widget; -} - -void -gimp_spin_scale_set_appearance( GtkWidget *widget, const gchar *appearance) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - - if ( strcmp("full", appearance) == 0 ) { - private->appearanceMode = APPEARANCE_FULL; - } else if ( strcmp("compact", appearance) == 0 ) { - private->appearanceMode = APPEARANCE_COMPACT; - } -} - -static void -gimp_spin_scale_get_preferred_width (GtkWidget *widget, - gint *minimum_width, - gint *natural_width) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - PangoContext *context = gtk_widget_get_pango_context (widget); - PangoFontMetrics *metrics; - - GTK_WIDGET_CLASS (parent_class)->get_preferred_width (widget, - minimum_width, - natural_width); - - metrics = pango_context_get_metrics (context, - pango_context_get_font_description (context), - pango_context_get_language (context)); - - if (private->label) - { - gint char_width; - gint digit_width; - gint char_pixels; - - char_width = pango_font_metrics_get_approximate_char_width (metrics); - digit_width = pango_font_metrics_get_approximate_digit_width (metrics); - char_pixels = PANGO_PIXELS (MAX (char_width, digit_width)); - - /* ~3 chars for the ellipse */ - *minimum_width += char_pixels * 3; - *natural_width += char_pixels * 3; - } - - pango_font_metrics_unref (metrics); -} - -static void -gimp_spin_scale_get_preferred_height (GtkWidget *widget, - gint *minimum_height, - gint *natural_height) -{ - PangoContext *context = gtk_widget_get_pango_context (widget); - PangoFontMetrics *metrics; - //gint height; - - GTK_WIDGET_CLASS (parent_class)->get_preferred_height (widget, - minimum_height, - natural_height); - - metrics = pango_context_get_metrics (context, - pango_context_get_font_description (context), - pango_context_get_language (context)); - - //height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + - // pango_font_metrics_get_descent (metrics)); - - *minimum_height += 1; - *natural_height += 1; - - pango_font_metrics_unref (metrics); -} - -static void -gimp_spin_scale_style_set (GtkWidget *widget, - GtkStyle *prev_style) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - - GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style); - - if (private->layout) - { - g_object_unref (private->layout); - private->layout = NULL; - } -} - - -static gboolean - gimp_spin_scale_draw (GtkWidget *widget, - cairo_t *cr) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - GtkStyleContext *style = gtk_widget_get_style_context(widget); - GtkAllocation allocation; - GdkRGBA color; - - cairo_save (cr); - GTK_WIDGET_CLASS (parent_class)->draw (widget, cr); - cairo_restore (cr); - - gtk_widget_get_allocation (widget, &allocation); - - cairo_set_line_width (cr, 1.0); - - if (private->label) - { - GdkRectangle text_area; - gint minimum_width; - gint natural_width; - PangoRectangle logical; - gint layout_offset_x; - gint layout_offset_y; - GtkStateFlags state; - GdkRGBA text_color; - GdkRGBA bar_text_color; - gdouble progress_fraction; - gint progress_x; - gint progress_y; - gint progress_width; - gint progress_height; - - gtk_entry_get_text_area (GTK_ENTRY (widget), &text_area); - - GTK_WIDGET_CLASS (parent_class)->get_preferred_width (widget, - &minimum_width, - &natural_width); - - if (! private->layout) - { - private->layout = gtk_widget_create_pango_layout (widget, - private->label); - pango_layout_set_ellipsize (private->layout, PANGO_ELLIPSIZE_END); - } - - pango_layout_set_width (private->layout, - PANGO_SCALE * - (allocation.width - minimum_width)); - pango_layout_get_pixel_extents (private->layout, NULL, &logical); - - gtk_entry_get_layout_offsets (GTK_ENTRY (widget), NULL, &layout_offset_y); - - if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) - layout_offset_x = text_area.x + text_area.width - logical.width - 4; - else - layout_offset_x = 4; - - layout_offset_x -= logical.x; - - state = gtk_widget_get_state_flags (widget); - - gtk_style_context_get_color (style, state, &text_color); - - gtk_style_context_save (style); - gtk_style_context_add_class (style, GTK_STYLE_CLASS_PROGRESSBAR); - gtk_style_context_get_color (style, state, &bar_text_color); - gtk_style_context_restore (style); - - progress_fraction = gtk_entry_get_progress_fraction (GTK_ENTRY (widget)); - - if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) - { - progress_fraction = 1.0 - progress_fraction; - - progress_x = text_area.width * progress_fraction; - progress_y = 0; - progress_width = text_area.width - progress_x; - progress_height = text_area.height; - } - else - { - progress_x = 0; - progress_y = 0; - progress_width = text_area.width * progress_fraction; - progress_height = text_area.height; - } - - cairo_save (cr); - - cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD); - cairo_rectangle (cr, 0, 0, text_area.width, text_area.height); - cairo_rectangle (cr, progress_x, progress_y, - progress_width, progress_height); - cairo_clip (cr); - cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING); - - cairo_move_to (cr, layout_offset_x, text_area.y + layout_offset_y-3); - gdk_cairo_set_source_rgba (cr, &text_color); - pango_cairo_show_layout (cr, private->layout); - cairo_restore (cr); - - cairo_rectangle (cr, progress_x, progress_y, - progress_width, progress_height); - cairo_clip (cr); - - cairo_move_to (cr, layout_offset_x, text_area.y + layout_offset_y-3); - gdk_cairo_set_source_rgba (cr, &bar_text_color); - pango_cairo_show_layout (cr, private->layout); - } - - 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 SpinScaleTarget -gimp_spin_scale_get_target (GtkWidget *widget, - gdouble x, - gdouble y) -{ - GtkAllocation allocation; - PangoRectangle logical; - gint layout_x; - gint layout_y; - - gtk_widget_get_allocation (widget, &allocation); - gtk_entry_get_layout_offsets (GTK_ENTRY (widget), &layout_x, &layout_y); - pango_layout_get_pixel_extents (gtk_entry_get_layout (GTK_ENTRY (widget)), - NULL, &logical); - - GdkRectangle text_area; - gtk_entry_get_text_area (GTK_ENTRY (widget), &text_area); - - if (x >= text_area.x && x < text_area.width && - y >= text_area.y && y < text_area.height) - { - x -= text_area.x; - y -= text_area.y; - - if (x > layout_x && x < layout_x + logical.width && - y > layout_y && y < layout_y + logical.height) - { - return TARGET_NUMBER; - } - else if (y > text_area.height / 2) - { - return TARGET_LOWER; - } - - return TARGET_UPPER; - } - - return TARGET_NONE; -} - -static void -gimp_spin_scale_get_limits (GimpSpinScale *scale, - gdouble *lower, - gdouble *upper) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (scale); - - if (private->scale_limits_set) - { - *lower = private->scale_lower; - *upper = private->scale_upper; - } - else - { - GtkSpinButton *spin_button = GTK_SPIN_BUTTON (scale); - GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); - - *lower = gtk_adjustment_get_lower (adjustment); - *upper = gtk_adjustment_get_upper (adjustment); - } -} - -static void -gimp_spin_scale_change_value (GtkWidget *widget, - gdouble x) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget); - GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); - gdouble lower; - gdouble upper; - gdouble value; - GdkRectangle text_area; - gtk_entry_get_text_area (GTK_ENTRY (widget), &text_area); - gimp_spin_scale_get_limits (GIMP_SPIN_SCALE (widget), &lower, &upper); - - if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) - x = text_area.width - x; - - if (private->relative_change) - { - gdouble diff; - gdouble step; - - step = (upper - lower) / text_area.width / 10.0; - if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) - diff = x - (text_area.width - private->start_x); - else - diff = x - private->start_x; - - value = (private->start_value + diff * step); - } - else - { - gdouble fraction; - - fraction = x / (gdouble) text_area.width; - if (fraction > 0.0) - fraction = pow (fraction, private->gamma); - - value = fraction * (upper - lower) + lower; - } - - gtk_adjustment_set_value (adjustment, value); -} - -static gboolean -gimp_spin_scale_button_press (GtkWidget *widget, - GdkEventButton *event) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - - private->changing_value = FALSE; - private->relative_change = FALSE; - - gint x, y; - gimp_spin_scale_event_to_widget_coords (widget, event->window, - event->x, event->y, - &x, &y); - switch (gimp_spin_scale_get_target (widget, x, y)) - { - case TARGET_UPPER: - private->changing_value = TRUE; - - gtk_widget_grab_focus (widget); - - gimp_spin_scale_change_value (widget, x); - - return TRUE; - - case TARGET_LOWER: - private->changing_value = TRUE; - - gtk_widget_grab_focus (widget); - - private->relative_change = TRUE; - private->start_x = x; - private->start_value = gtk_adjustment_get_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget))); - - return TRUE; - - default: - break; - } - - return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event); -} - -static gboolean -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_change_value (widget, x); - return TRUE; - } - - return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event); -} - -static gboolean -gimp_spin_scale_motion_notify (GtkWidget *widget, - GdkEventMotion *event) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - - gdk_event_request_motions (event); - - 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_change_value (widget, x); - - return TRUE; - } - - GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event); - - if (! (event->state & - (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) - ) - { - GdkDisplay *display = gtk_widget_get_display (widget); - GdkCursor *cursor = NULL; - - switch (gimp_spin_scale_get_target (widget, x, y)) - { - case TARGET_NUMBER: - cursor = gdk_cursor_new_for_display (display, GDK_XTERM); - break; - - case TARGET_UPPER: - cursor = gdk_cursor_new_for_display (display, GDK_SB_UP_ARROW); - break; - - case TARGET_LOWER: - cursor = gdk_cursor_new_for_display (display, GDK_SB_H_DOUBLE_ARROW); - break; - - default: - break; - } - - if (cursor) - { - gdk_window_set_cursor (event->window, cursor); - g_object_unref (cursor); - } - } - - return FALSE; -} - -static gboolean -gimp_spin_scale_leave_notify (GtkWidget *widget, - GdkEventCrossing *event) -{ - gdk_window_set_cursor (event->window, NULL); - - return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); -} - -gboolean gimp_spin_scale_keypress( GtkWidget *widget, GdkEventKey *event) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (widget); - guint key = 0; - gdk_keymap_translate_keyboard_state( gdk_keymap_get_for_display( gdk_display_get_default() ), - event->hardware_keycode, (GdkModifierType)event->state, - 0, &key, 0, 0, 0 ); - - switch ( key ) { - - case GDK_KEY_Escape: - case GDK_KEY_Return: - case GDK_KEY_KP_Enter: - { - private->transferFocus = TRUE; - gimp_spin_scale_defocus( GTK_SPIN_BUTTON(widget) ); - } - break; - - } - - return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event); -} - -static void -gimp_spin_scale_defocus( GtkSpinButton *spin_button ) -{ - GimpSpinScalePrivate *private = GET_PRIVATE (spin_button); - - if ( private->transferFocus ) { - if ( private->focusWidget ) { - gtk_widget_grab_focus( private->focusWidget ); - } - } -} - -static void -gimp_spin_scale_value_changed (GtkSpinButton *spin_button) -{ - GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); - GimpSpinScalePrivate *private = GET_PRIVATE (spin_button); - gdouble lower; - gdouble upper; - gdouble value; - - gimp_spin_scale_get_limits (GIMP_SPIN_SCALE (spin_button), &lower, &upper); - - value = CLAMP (gtk_adjustment_get_value (adjustment), lower, upper); - - gtk_entry_set_progress_fraction (GTK_ENTRY (spin_button), - pow ((value - lower) / (upper - lower), - 1.0 / private->gamma)); - - // TODO - Allow scrollwheel to change value then return focus, - // but clicks/keypress should keep focus in the control - //if ( gtk_widget_has_focus( GTK_WIDGET(spin_button) ) ) { - // gimp_spin_scale_defocus( spin_button ); - //} -} - - -/* public functions */ - -GtkWidget * -gimp_spin_scale_new (GtkAdjustment *adjustment, - const gchar *label, - gint digits) -{ - g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL); - - return g_object_new (GIMP_TYPE_SPIN_SCALE, - "adjustment", adjustment, - "label", label, - "digits", digits, - NULL); -} - -void -gimp_spin_scale_set_label (GimpSpinScale *scale, - const gchar *label) -{ - GimpSpinScalePrivate *private; - - g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); - - private = GET_PRIVATE (scale); - - if (label == private->label) - return; - - g_free (private->label); - private->label = g_strdup (label); - - if (private->layout) - { - g_object_unref (private->layout); - private->layout = NULL; - } - - gtk_widget_queue_resize (GTK_WIDGET (scale)); - - g_object_notify (G_OBJECT (scale), "label"); -} - -const gchar * -gimp_spin_scale_get_label (GimpSpinScale *scale) -{ - g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), NULL); - - return GET_PRIVATE (scale)->label; -} - -void -gimp_spin_scale_set_scale_limits (GimpSpinScale *scale, - gdouble lower, - gdouble upper) -{ - GimpSpinScalePrivate *private; - GtkSpinButton *spin_button; - GtkAdjustment *adjustment; - - g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); - - private = GET_PRIVATE (scale); - spin_button = GTK_SPIN_BUTTON (scale); - adjustment = gtk_spin_button_get_adjustment (spin_button); - - g_return_if_fail (lower >= gtk_adjustment_get_lower (adjustment)); - g_return_if_fail (upper <= gtk_adjustment_get_upper (adjustment)); - - private->scale_limits_set = TRUE; - private->scale_lower = lower; - private->scale_upper = upper; - private->gamma = 1.0; - - gimp_spin_scale_value_changed (spin_button); -} - -void -gimp_spin_scale_unset_scale_limits (GimpSpinScale *scale) -{ - GimpSpinScalePrivate *private; - - g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); - - private = GET_PRIVATE (scale); - - private->scale_limits_set = FALSE; - private->scale_lower = 0.0; - private->scale_upper = 0.0; - - gimp_spin_scale_value_changed (GTK_SPIN_BUTTON (scale)); -} - -gboolean -gimp_spin_scale_get_scale_limits (GimpSpinScale *scale, - gdouble *lower, - gdouble *upper) -{ - GimpSpinScalePrivate *private; - - g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), FALSE); - - private = GET_PRIVATE (scale); - - if (lower) - *lower = private->scale_lower; - - if (upper) - *upper = private->scale_upper; - - return private->scale_limits_set; -} - -void -gimp_spin_scale_set_gamma (GimpSpinScale *scale, - gdouble gamma) -{ - GimpSpinScalePrivate *private; - - g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); - - private = GET_PRIVATE (scale); - - private->gamma = gamma; - - gimp_spin_scale_value_changed (GTK_SPIN_BUTTON (scale)); -} - -gdouble -gimp_spin_scale_get_gamma (GimpSpinScale *scale) -{ - g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), 1.0); - - return GET_PRIVATE(scale)->gamma; -} diff --git a/src/ui/widget/gimpspinscale.h b/src/ui/widget/gimpspinscale.h deleted file mode 100644 index b42a0faf8..000000000 --- a/src/ui/widget/gimpspinscale.h +++ /dev/null @@ -1,82 +0,0 @@ -/* GIMP - The GNU Image Manipulation Program - * Copyright (C) 1995 Spencer Kimball and Peter Mattis - * - * gimpspinscale.h - * Copyright (C) 2010 Michael Natterer - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef __GIMP_SPIN_SCALE_H__ -#define __GIMP_SPIN_SCALE_H__ - -#ifndef WITH_GIMP -#include -#endif - -G_BEGIN_DECLS - -#define GIMP_TYPE_SPIN_SCALE (gimp_spin_scale_get_type ()) -#define GIMP_SPIN_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_SPIN_SCALE, GimpSpinScale)) -#define GIMP_SPIN_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_SPIN_SCALE, GimpSpinScaleClass)) -#define GIMP_IS_SPIN_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_SPIN_SCALE)) -#define GIMP_IS_SPIN_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_SPIN_SCALE)) -#define GIMP_SPIN_SCALE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_SPIN_SCALE, GimpSpinScaleClass)) - - -typedef struct _GimpSpinScale GimpSpinScale; -typedef struct _GimpSpinScaleClass GimpSpinScaleClass; - -struct _GimpSpinScale -{ - GtkSpinButton parent_instance; -}; - -struct _GimpSpinScaleClass -{ - GtkSpinButtonClass parent_class; -}; - - -GType gimp_spin_scale_get_type (void) G_GNUC_CONST; - -GtkWidget * gimp_spin_scale_new (GtkAdjustment *adjustment, - const gchar *label, - gint digits); - -void gimp_spin_scale_set_label (GimpSpinScale *scale, - const gchar *label); -const gchar * gimp_spin_scale_get_label (GimpSpinScale *scale); - -void gimp_spin_scale_set_scale_limits (GimpSpinScale *scale, - gdouble lower, - gdouble upper); -void gimp_spin_scale_unset_scale_limits (GimpSpinScale *scale); -gboolean gimp_spin_scale_get_scale_limits (GimpSpinScale *scale, - gdouble *lower, - gdouble *upper); - -void gimp_spin_scale_set_gamma (GimpSpinScale *scale, - gdouble gamma); -gdouble gimp_spin_scale_get_gamma (GimpSpinScale *scale); - -void gimp_spin_scale_set_focuswidget (GtkWidget *scale, - GtkWidget *widget); - -void gimp_spin_scale_set_appearance (GtkWidget *scale, - const gchar *appearance); - -G_END_DECLS - -#endif /* __GIMP_SPIN_SCALE_H__ */ diff --git a/src/ui/widget/spin-scale.cpp b/src/ui/widget/spin-scale.cpp index eb91aaf39..f74626d9b 100644 --- a/src/ui/widget/spin-scale.cpp +++ b/src/ui/widget/spin-scale.cpp @@ -11,7 +11,7 @@ #include #include -#include "ui/widget/gimpspinscale.h" +#include "widgets/gimp/gimpspinscale.h" namespace Inkscape { namespace UI { diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt index 47ffe82ac..b2071af4e 100644 --- a/src/widgets/CMakeLists.txt +++ b/src/widgets/CMakeLists.txt @@ -1,24 +1,25 @@ +add_subdirectory(gimp) set(widgets_SRC button.cpp - arc-toolbar.cpp - box3d-toolbar.cpp - calligraphy-toolbar.cpp - connector-toolbar.cpp - dropper-toolbar.cpp - eraser-toolbar.cpp - lpe-toolbar.cpp - measure-toolbar.cpp - mesh-toolbar.cpp - node-toolbar.cpp - pencil-toolbar.cpp - rect-toolbar.cpp - spiral-toolbar.cpp - spray-toolbar.cpp - star-toolbar.cpp - text-toolbar.cpp - tweak-toolbar.cpp - zoom-toolbar.cpp + arc-toolbar.cpp + box3d-toolbar.cpp + calligraphy-toolbar.cpp + connector-toolbar.cpp + dropper-toolbar.cpp + eraser-toolbar.cpp + lpe-toolbar.cpp + measure-toolbar.cpp + mesh-toolbar.cpp + node-toolbar.cpp + pencil-toolbar.cpp + rect-toolbar.cpp + spiral-toolbar.cpp + spray-toolbar.cpp + star-toolbar.cpp + text-toolbar.cpp + tweak-toolbar.cpp + zoom-toolbar.cpp dash-selector.cpp desktop-widget.cpp eek-preview.cpp @@ -40,7 +41,6 @@ set(widgets_SRC ink-toggle-action.cpp ink-tool-menu-action.cpp paint-selector.cpp - ruler.cpp select-toolbar.cpp sp-attribute-widget.cpp sp-color-selector.cpp @@ -59,24 +59,24 @@ set(widgets_SRC # ------- # Headers button.h - arc-toolbar.h - box3d-toolbar.h - calligraphy-toolbar.h - connector-toolbar.h - dropper-toolbar.h - eraser-toolbar.h - lpe-toolbar.h - measure-toolbar.h - mesh-toolbar.h - node-toolbar.h - pencil-toolbar.h - rect-toolbar.h - spiral-toolbar.h - spray-toolbar.h - star-toolbar.h - text-toolbar.h - tweak-toolbar.h - zoom-toolbar.h + arc-toolbar.h + box3d-toolbar.h + calligraphy-toolbar.h + connector-toolbar.h + dropper-toolbar.h + eraser-toolbar.h + lpe-toolbar.h + measure-toolbar.h + mesh-toolbar.h + node-toolbar.h + pencil-toolbar.h + rect-toolbar.h + spiral-toolbar.h + spray-toolbar.h + star-toolbar.h + text-toolbar.h + tweak-toolbar.h + zoom-toolbar.h dash-selector.h desktop-widget.h eek-preview.h @@ -99,7 +99,6 @@ set(widgets_SRC ink-toggle-action.h ink-tool-menu-action.h paint-selector.h - ruler.h select-toolbar.h sp-attribute-widget.h sp-color-selector.h diff --git a/src/widgets/desktop-widget.cpp b/src/widgets/desktop-widget.cpp index aa485498e..bd72560c6 100644 --- a/src/widgets/desktop-widget.cpp +++ b/src/widgets/desktop-widget.cpp @@ -57,7 +57,7 @@ // We're in the "widgets" directory, so no need to explicitly prefix these: #include "button.h" -#include "ruler.h" +#include "gimp/ruler.h" #include "spinbutton-events.h" #include "spw-utilities.h" #include "toolbox.h" diff --git a/src/widgets/ege-adjustment-action.cpp b/src/widgets/ege-adjustment-action.cpp index 8fef21741..6678d1e5a 100644 --- a/src/widgets/ege-adjustment-action.cpp +++ b/src/widgets/ege-adjustment-action.cpp @@ -47,7 +47,7 @@ #include "icon-size.h" #include "widgets/ege-adjustment-action.h" -#include "ui/widget/gimpspinscale.h" +#include "gimp/gimpspinscale.h" #include "ui/icon-names.h" diff --git a/src/widgets/gimp/CMakeLists.txt b/src/widgets/gimp/CMakeLists.txt new file mode 100644 index 000000000..7b3e834e3 --- /dev/null +++ b/src/widgets/gimp/CMakeLists.txt @@ -0,0 +1,13 @@ +set(gimpwidgets_SRC + gimpcolorwheel.c + gimpspinscale.c + ruler.cpp + + # ------- + # Headers + gimpcolorwheel.h + gimpspinscale.h + ruler.h +) + +add_inkscape_source("${gimpwidgets_SRC}") diff --git a/src/widgets/gimp/gimpcolorwheel.c b/src/widgets/gimp/gimpcolorwheel.c new file mode 100644 index 000000000..212391497 --- /dev/null +++ b/src/widgets/gimp/gimpcolorwheel.c @@ -0,0 +1,1467 @@ +/* HSV color selector for GTK+ + * + * Copyright (C) 1999 The Free Software Foundation + * + * Authors: Simon Budig (original code) + * Federico Mena-Quintero (cleanup for GTK+) + * Jonathan Blandford (cleanup for GTK+) + * Michael Natterer (ported back to GIMP) + * + * 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 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, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GTK+ Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GTK+ at ftp://ftp.gtk.org/pub/gtk/. + */ + +/* + * This widget was adopted by Inkscape by Alex Valavanis + * on 2013-01-08. Last merges with GIMP code were applied using the following + * commits from the GIMP git repository at + * http://git.gnome.org/browse/gimp/tree/modules/gimpcolorwheel.c + * + * Gtk+ 2 code merge: commit 632c5 (2013-01-06) + * Gtk+ 3 code merge: commit bcfc6, gtk3-port branch (2013-01-06) + */ + +#include "config.h" + +#include +#include + +#include "gimpcolorwheel.h" +#include + + +/* Default ring fraction */ +#define DEFAULT_FRACTION 0.1 + +/* Default width/height */ +#define DEFAULT_SIZE 100 + +/* Default ring width */ +#define DEFAULT_RING_WIDTH 10 + + +/* Dragging modes */ +typedef enum +{ + DRAG_NONE, + DRAG_H, + DRAG_SV +} DragMode; + +/* Private part of the GimpColorWheel structure */ +typedef struct +{ + /* Color value */ + gdouble h; + gdouble s; + gdouble v; + + /* ring_width is this fraction of size */ + gdouble ring_fraction; + + /* Size and ring width */ + gint size; + gint ring_width; + + /* Window for capturing events */ + GdkWindow *window; + + /* Dragging mode */ + DragMode mode; + + guint focus_on_ring : 1; +} GimpColorWheelPrivate; + +enum +{ + CHANGED, + MOVE, + LAST_SIGNAL +}; + +static void gimp_color_wheel_map (GtkWidget *widget); +static void gimp_color_wheel_unmap (GtkWidget *widget); +static void gimp_color_wheel_realize (GtkWidget *widget); +static void gimp_color_wheel_unrealize (GtkWidget *widget); +static void gimp_color_wheel_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); +static gboolean gimp_color_wheel_button_press (GtkWidget *widget, + GdkEventButton *event); +static gboolean gimp_color_wheel_button_release (GtkWidget *widget, + GdkEventButton *event); +static gboolean gimp_color_wheel_motion (GtkWidget *widget, + GdkEventMotion *event); +static gboolean gimp_color_wheel_draw (GtkWidget *widget, + cairo_t *cr); +static void gimp_color_wheel_get_preferred_width (GtkWidget *widget, + gint *minimum_width, + gint *natural_width); +static void gimp_color_wheel_get_preferred_height (GtkWidget *widget, + gint *minimum_height, + gint *natural_height); +static gboolean gimp_color_wheel_grab_broken (GtkWidget *widget, + GdkEventGrabBroken *event); +static gboolean gimp_color_wheel_focus (GtkWidget *widget, + GtkDirectionType direction); +static void gimp_color_wheel_move (GimpColorWheel *wheel, + GtkDirectionType dir); + + +static guint wheel_signals[LAST_SIGNAL]; + +G_DEFINE_TYPE (GimpColorWheel, gimp_color_wheel, GTK_TYPE_WIDGET) + +#define parent_class gimp_color_wheel_parent_class + + +static void +gimp_color_wheel_class_init (GimpColorWheelClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); + GimpColorWheelClass *wheel_class = GIMP_COLOR_WHEEL_CLASS (class); + GtkBindingSet *binding_set; + + widget_class->map = gimp_color_wheel_map; + widget_class->unmap = gimp_color_wheel_unmap; + widget_class->realize = gimp_color_wheel_realize; + widget_class->unrealize = gimp_color_wheel_unrealize; + widget_class->size_allocate = gimp_color_wheel_size_allocate; + widget_class->button_press_event = gimp_color_wheel_button_press; + widget_class->button_release_event = gimp_color_wheel_button_release; + widget_class->motion_notify_event = gimp_color_wheel_motion; + widget_class->get_preferred_width = gimp_color_wheel_get_preferred_width; + widget_class->get_preferred_height = gimp_color_wheel_get_preferred_height; + widget_class->draw = gimp_color_wheel_draw; + widget_class->focus = gimp_color_wheel_focus; + widget_class->grab_broken_event = gimp_color_wheel_grab_broken; + + wheel_class->move = gimp_color_wheel_move; + + wheel_signals[CHANGED] = + g_signal_new ("changed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (GimpColorWheelClass, changed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + wheel_signals[MOVE] = + g_signal_new ("move", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (GimpColorWheelClass, move), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_DIRECTION_TYPE); + + binding_set = gtk_binding_set_by_class (class); + + gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_UP); + gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_UP); + + gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_DOWN); + gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_DOWN); + + + gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_RIGHT); + gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_RIGHT); + + gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_LEFT); + gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0, + "move", 1, + G_TYPE_ENUM, GTK_DIR_LEFT); + + g_type_class_add_private (object_class, sizeof (GimpColorWheelPrivate)); +} + +static void +gimp_color_wheel_init (GimpColorWheel *wheel) +{ + GimpColorWheelPrivate *priv; + + priv = G_TYPE_INSTANCE_GET_PRIVATE (wheel, GIMP_TYPE_COLOR_WHEEL, + GimpColorWheelPrivate); + + wheel->priv = priv; + + gtk_widget_set_has_window (GTK_WIDGET (wheel), FALSE); + gtk_widget_set_can_focus (GTK_WIDGET (wheel), TRUE); + + priv->ring_fraction = DEFAULT_FRACTION; + priv->size = DEFAULT_SIZE; + priv->ring_width = DEFAULT_RING_WIDTH; +} + +static void +gimp_color_wheel_map (GtkWidget *widget) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + + GTK_WIDGET_CLASS (parent_class)->map (widget); + + gdk_window_show (priv->window); +} + +static void +gimp_color_wheel_unmap (GtkWidget *widget) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + + gdk_window_hide (priv->window); + + GTK_WIDGET_CLASS (parent_class)->unmap (widget); +} + +static void +gimp_color_wheel_realize (GtkWidget *widget) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + GtkAllocation allocation; + GdkWindowAttr attr; + gint attr_mask; + GdkWindow *parent_window; + + gtk_widget_get_allocation (widget, &allocation); + + gtk_widget_set_realized (widget, TRUE); + + attr.window_type = GDK_WINDOW_CHILD; + attr.x = allocation.x; + attr.y = allocation.y; + attr.width = allocation.width; + attr.height = allocation.height; + attr.wclass = GDK_INPUT_ONLY; + attr.event_mask = (gtk_widget_get_events (widget) | + GDK_KEY_PRESS_MASK | + GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK | + GDK_POINTER_MOTION_MASK | + GDK_ENTER_NOTIFY_MASK | + GDK_LEAVE_NOTIFY_MASK); + + attr_mask = GDK_WA_X | GDK_WA_Y; + + parent_window = gtk_widget_get_parent_window (widget); + + gtk_widget_set_window (widget, parent_window); + g_object_ref (parent_window); + + priv->window = gdk_window_new (parent_window, &attr, attr_mask); + gdk_window_set_user_data (priv->window, wheel); +} + +static void +gimp_color_wheel_unrealize (GtkWidget *widget) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + + gdk_window_set_user_data (priv->window, NULL); + gdk_window_destroy (priv->window); + priv->window = NULL; + + GTK_WIDGET_CLASS (parent_class)->unrealize (widget); +} + +static void +gimp_color_wheel_get_preferred_width (GtkWidget *widget, + gint *minimum_width, + gint *natural_width) +{ + gint focus_width; + gint focus_pad; + + gtk_widget_style_get (widget, + "focus-line-width", &focus_width, + "focus-padding", &focus_pad, + NULL); + + *minimum_width = *natural_width = DEFAULT_SIZE + 2 * (focus_width + focus_pad); +} + +static void +gimp_color_wheel_get_preferred_height (GtkWidget *widget, + gint *minimum_height, + gint *natural_height) +{ + gint focus_width; + gint focus_pad; + + gtk_widget_style_get (widget, + "focus-line-width", &focus_width, + "focus-padding", &focus_pad, + NULL); + + *minimum_height = *natural_height = DEFAULT_SIZE + 2 * (focus_width + focus_pad); +} + +static void +gimp_color_wheel_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + gint focus_width; + gint focus_pad; + + gtk_widget_set_allocation (widget, allocation); + + gtk_widget_style_get (widget, + "focus-line-width", &focus_width, + "focus-padding", &focus_pad, + NULL); + + priv->size = MIN (allocation->width - 2 * (focus_width + focus_pad), + allocation->height - 2 * (focus_width + focus_pad)); + + priv->ring_width = priv->size * priv->ring_fraction; + + if (gtk_widget_get_realized (widget)) + gdk_window_move_resize (priv->window, + allocation->x, + allocation->y, + allocation->width, + allocation->height); +} + + +/* Utility functions */ + +#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11) + +/* Converts from HSV to RGB */ +static void +hsv_to_rgb (gdouble *h, + gdouble *s, + gdouble *v) +{ + gdouble hue, saturation, value; + gdouble f, p, q, t; + + if (*s == 0.0) + { + *h = *v; + *s = *v; + *v = *v; /* heh */ + } + else + { + hue = *h * 6.0; + saturation = *s; + value = *v; + + if (hue == 6.0) + hue = 0.0; + + f = hue - (int) hue; + p = value * (1.0 - saturation); + q = value * (1.0 - saturation * f); + t = value * (1.0 - saturation * (1.0 - f)); + + switch ((int) hue) + { + case 0: + *h = value; + *s = t; + *v = p; + break; + + case 1: + *h = q; + *s = value; + *v = p; + break; + + case 2: + *h = p; + *s = value; + *v = t; + break; + + case 3: + *h = p; + *s = q; + *v = value; + break; + + case 4: + *h = t; + *s = p; + *v = value; + break; + + case 5: + *h = value; + *s = p; + *v = q; + break; + + default: + g_assert_not_reached (); + } + } +} + +/* Computes the vertices of the saturation/value triangle */ +static void +compute_triangle (GimpColorWheel *wheel, + gint *hx, + gint *hy, + gint *sx, + gint *sy, + gint *vx, + gint *vy) +{ + GimpColorWheelPrivate *priv = wheel->priv; + GtkAllocation allocation; + gdouble center_x; + gdouble center_y; + gdouble inner, outer; + gdouble angle; + + gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); + + center_x = allocation.width / 2.0; + center_y = allocation.height / 2.0; + + outer = priv->size / 2.0; + inner = outer - priv->ring_width; + angle = priv->h * 2.0 * G_PI; + + *hx = floor (center_x + cos (angle) * inner + 0.5); + *hy = floor (center_y - sin (angle) * inner + 0.5); + *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5); + *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5); + *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5); + *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5); +} + +/* Computes whether a point is inside the hue ring */ +static gboolean +is_in_ring (GimpColorWheel *wheel, + gdouble x, + gdouble y) +{ + GimpColorWheelPrivate *priv = wheel->priv; + GtkAllocation allocation; + gdouble dx, dy, dist; + gdouble center_x; + gdouble center_y; + gdouble inner, outer; + + gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); + + center_x = allocation.width / 2.0; + center_y = allocation.height / 2.0; + + outer = priv->size / 2.0; + inner = outer - priv->ring_width; + + dx = x - center_x; + dy = center_y - y; + dist = dx * dx + dy * dy; + + return (dist >= inner * inner && dist <= outer * outer); +} + +/* Computes a saturation/value pair based on the mouse coordinates */ +static void +compute_sv (GimpColorWheel *wheel, + gdouble x, + gdouble y, + gdouble *s, + gdouble *v) +{ + GtkAllocation allocation; + gint ihx, ihy, isx, isy, ivx, ivy; + gdouble hx, hy, sx, sy, vx, vy; + gdouble center_x; + gdouble center_y; + + gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); + + compute_triangle (wheel, &ihx, &ihy, &isx, &isy, &ivx, &ivy); + + center_x = allocation.width / 2.0; + center_y = allocation.height / 2.0; + + hx = ihx - center_x; + hy = center_y - ihy; + sx = isx - center_x; + sy = center_y - isy; + vx = ivx - center_x; + vy = center_y - ivy; + x -= center_x; + y = center_y - y; + + if (vx * (x - sx) + vy * (y - sy) < 0.0) + { + *s = 1.0; + *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy)) + / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy))); + + if (*v < 0.0) + *v = 0.0; + else if (*v > 1.0) + *v = 1.0; + } + else if (hx * (x - sx) + hy * (y - sy) < 0.0) + { + *s = 0.0; + *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy)) + / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy))); + + if (*v < 0.0) + *v = 0.0; + else if (*v > 1.0) + *v = 1.0; + } + else if (sx * (x - hx) + sy * (y - hy) < 0.0) + { + *v = 1.0; + *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) / + ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy))); + + if (*s < 0.0) + *s = 0.0; + else if (*s > 1.0) + *s = 1.0; + } + else + { + *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx)) + / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx))); + + if (*v<= 0.0) + { + *v = 0.0; + *s = 0.0; + } + else + { + if (*v > 1.0) + *v = 1.0; + + if (fabs (hy - vy) < fabs (hx - vx)) + *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx)); + else + *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy)); + + if (*s < 0.0) + *s = 0.0; + else if (*s > 1.0) + *s = 1.0; + } + } +} + +/* Computes whether a point is inside the saturation/value triangle */ +static gboolean +is_in_triangle (GimpColorWheel *wheel, + gdouble x, + gdouble y) +{ + gint hx, hy, sx, sy, vx, vy; + gdouble det, s, v; + + compute_triangle (wheel, &hx, &hy, &sx, &sy, &vx, &vy); + + det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx); + + s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det; + v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det; + + return (s >= 0.0 && v >= 0.0 && s + v <= 1.0); +} + +/* Computes a value based on the mouse coordinates */ +static double +compute_v (GimpColorWheel *wheel, + gdouble x, + gdouble y) +{ + GtkAllocation allocation; + gdouble center_x; + gdouble center_y; + gdouble dx, dy; + gdouble angle; + + gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation); + + center_x = allocation.width / 2.0; + center_y = allocation.height / 2.0; + + dx = x - center_x; + dy = center_y - y; + + angle = atan2 (dy, dx); + if (angle < 0.0) + angle += 2.0 * G_PI; + + return angle / (2.0 * G_PI); +} + +static void +set_cross_grab (GimpColorWheel *wheel, + guint32 time) +{ + GimpColorWheelPrivate *priv = wheel->priv; + GdkCursor *cursor; + + cursor = + gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (wheel)), + GDK_CROSSHAIR); + + gdk_device_grab (gtk_get_current_event_device(), + priv->window, + GDK_OWNERSHIP_NONE, + FALSE, + GDK_POINTER_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK | + GDK_BUTTON_RELEASE_MASK, + cursor, time); + g_object_unref (cursor); +} + +static gboolean gimp_color_wheel_grab_broken(GtkWidget *widget, GdkEventGrabBroken *event) +{ + (void)event; + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + + priv->mode = DRAG_NONE; + + return TRUE; +} + +static gboolean +gimp_color_wheel_button_press (GtkWidget *widget, + GdkEventButton *event) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + gdouble x, y; + + if (priv->mode != DRAG_NONE || event->button != 1) + return FALSE; + + x = event->x; + y = event->y; + + if (is_in_ring (wheel, x, y)) + { + priv->mode = DRAG_H; + set_cross_grab (wheel, event->time); + + gimp_color_wheel_set_color (wheel, + compute_v (wheel, x, y), + priv->s, + priv->v); + + gtk_widget_grab_focus (widget); + priv->focus_on_ring = TRUE; + + return TRUE; + } + + if (is_in_triangle (wheel, x, y)) + { + gdouble s, v; + + priv->mode = DRAG_SV; + set_cross_grab (wheel, event->time); + + compute_sv (wheel, x, y, &s, &v); + gimp_color_wheel_set_color (wheel, priv->h, s, v); + + gtk_widget_grab_focus (widget); + priv->focus_on_ring = FALSE; + + return TRUE; + } + + return FALSE; +} + +static gboolean +gimp_color_wheel_button_release (GtkWidget *widget, + GdkEventButton *event) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + DragMode mode; + gdouble x, y; + + if (priv->mode == DRAG_NONE || event->button != 1) + return FALSE; + + /* Set the drag mode to DRAG_NONE so that signal handlers for "catched" + * can see that this is the final color state. + */ + + mode = priv->mode; + priv->mode = DRAG_NONE; + + x = event->x; + y = event->y; + + if (mode == DRAG_H) + { + gimp_color_wheel_set_color (wheel, + compute_v (wheel, x, y), priv->s, priv->v); + } + else if (mode == DRAG_SV) + { + gdouble s, v; + + compute_sv (wheel, x, y, &s, &v); + gimp_color_wheel_set_color (wheel, priv->h, s, v); + } + else + g_assert_not_reached (); + + gdk_device_ungrab (gtk_get_current_event_device(), + event->time); + + return TRUE; +} + +static gboolean +gimp_color_wheel_motion (GtkWidget *widget, + GdkEventMotion *event) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + gdouble x, y; + + if (priv->mode == DRAG_NONE) + return FALSE; + + gdk_event_request_motions (event); + x = event->x; + y = event->y; + + if (priv->mode == DRAG_H) + { + gimp_color_wheel_set_color (wheel, + compute_v (wheel, x, y), priv->s, priv->v); + return TRUE; + } + else if (priv->mode == DRAG_SV) + { + gdouble s, v; + + compute_sv (wheel, x, y, &s, &v); + gimp_color_wheel_set_color (wheel, priv->h, s, v); + return TRUE; + } + + g_assert_not_reached (); + + return FALSE; +} + + +/* Redrawing */ + +/* Paints the hue ring */ +static void +paint_ring (GimpColorWheel *wheel, + cairo_t *cr) +{ + GtkWidget *widget = GTK_WIDGET (wheel); + GimpColorWheelPrivate *priv = wheel->priv; + gint width, height; + gint xx, yy; + gdouble dx, dy, dist; + gdouble center_x; + gdouble center_y; + gdouble inner, outer; + guint32 *buf, *p; + gdouble angle; + gdouble hue; + gdouble r, g, b; + cairo_surface_t *source; + cairo_t *source_cr; + gint stride; + + width = gtk_widget_get_allocated_width (widget); + height = gtk_widget_get_allocated_height (widget); + + center_x = width / 2.0; + center_y = height / 2.0; + + outer = priv->size / 2.0; + inner = outer - priv->ring_width; + + /* Create an image initialized with the ring colors */ + + stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width); + buf = g_new (guint32, height * stride / 4); + + for (yy = 0; yy < height; yy++) + { + p = buf + yy * width; + dy = -(yy - center_y); + + for (xx = 0; xx < width; xx++) + { + dx = xx - center_x; + + dist = dx * dx + dy * dy; + if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1))) + { + *p++ = 0; + continue; + } + + angle = atan2 (dy, dx); + if (angle < 0.0) + angle += 2.0 * G_PI; + + hue = angle / (2.0 * G_PI); + + r = hue; + g = 1.0; + b = 1.0; + hsv_to_rgb (&r, &g, &b); + + *p++ = (((int)floor (r * 255 + 0.5) << 16) | + ((int)floor (g * 255 + 0.5) << 8) | + (int)floor (b * 255 + 0.5)); + } + } + + source = cairo_image_surface_create_for_data ((unsigned char *)buf, + CAIRO_FORMAT_RGB24, + width, height, stride); + + /* Now draw the value marker onto the source image, so that it + * will get properly clipped at the edges of the ring + */ + source_cr = cairo_create (source); + + r = priv->h; + g = 1.0; + b = 1.0; + hsv_to_rgb (&r, &g, &b); + + if (INTENSITY (r, g, b) > 0.5) + cairo_set_source_rgb (source_cr, 0.0, 0.0, 0.0); + else + cairo_set_source_rgb (source_cr, 1.0, 1.0, 1.0); + + cairo_move_to (source_cr, center_x, center_y); + cairo_line_to (source_cr, + center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2, + center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2); + cairo_stroke (source_cr); + cairo_destroy (source_cr); + + /* Draw the ring using the source image */ + + cairo_save (cr); + + cairo_set_source_surface (cr, source, 0, 0); + cairo_surface_destroy (source); + + cairo_set_line_width (cr, priv->ring_width); + cairo_new_path (cr); + cairo_arc (cr, + center_x, center_y, + priv->size / 2.0 - priv->ring_width / 2.0, + 0, 2 * G_PI); + cairo_stroke (cr); + + cairo_restore (cr); + + g_free (buf); +} + +/* Converts an HSV triplet to an integer RGB triplet */ +static void +get_color (gdouble h, + gdouble s, + gdouble v, + gint *r, + gint *g, + gint *b) +{ + hsv_to_rgb (&h, &s, &v); + + *r = floor (h * 255 + 0.5); + *g = floor (s * 255 + 0.5); + *b = floor (v * 255 + 0.5); +} + +#define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t)) + +#define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \ + ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \ + : (a)) + +/* Number of pixels we extend out from the edges when creating + * color source to avoid artifacts + */ +#define PAD 3 + +/* Paints the HSV triangle */ +static void +paint_triangle (GimpColorWheel *wheel, + cairo_t *cr, + gboolean draw_focus) +{ + GtkWidget *widget = GTK_WIDGET (wheel); + GimpColorWheelPrivate *priv = wheel->priv; + gint hx, hy, sx, sy, vx, vy; /* HSV vertices */ + gint x1, y1, r1, g1, b1; /* First vertex in scanline order */ + gint x2, y2, r2, g2, b2; /* Second vertex */ + gint x3, y3, r3, g3, b3; /* Third vertex */ + gint t; + guint32 *buf, *p, c; + gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */ + gint xx, yy; + gint x_interp, y_interp; + gint x_start, x_end; + cairo_surface_t *source; + gdouble r, g, b; + gint stride; + gint width, height; + GtkStyleContext *context; + + width = gtk_widget_get_allocated_width (widget); + height = gtk_widget_get_allocated_height (widget); + + /* Compute triangle's vertices */ + + compute_triangle (wheel, &hx, &hy, &sx, &sy, &vx, &vy); + + x1 = hx; + y1 = hy; + get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1); + + x2 = sx; + y2 = sy; + get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2); + + x3 = vx; + y3 = vy; + get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3); + + if (y2 > y3) + { + SWAP (x2, x3, t); + SWAP (y2, y3, t); + SWAP (r2, r3, t); + SWAP (g2, g3, t); + SWAP (b2, b3, t); + } + + if (y1 > y3) + { + SWAP (x1, x3, t); + SWAP (y1, y3, t); + SWAP (r1, r3, t); + SWAP (g1, g3, t); + SWAP (b1, b3, t); + } + + if (y1 > y2) + { + SWAP (x1, x2, t); + SWAP (y1, y2, t); + SWAP (r1, r2, t); + SWAP (g1, g2, t); + SWAP (b1, b2, t); + } + + /* Shade the triangle */ + + stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width); + buf = g_new (guint32, height * stride / 4); + + for (yy = 0; yy < height; yy++) + { + p = buf + yy * width; + + if (yy >= y1 - PAD && yy < y3 + PAD) + { + y_interp = CLAMP (yy, y1, y3); + + if (y_interp < y2) + { + xl = LERP (x1, x2, y1, y2, y_interp); + + rl = LERP (r1, r2, y1, y2, y_interp); + gl = LERP (g1, g2, y1, y2, y_interp); + bl = LERP (b1, b2, y1, y2, y_interp); + } + else + { + xl = LERP (x2, x3, y2, y3, y_interp); + + rl = LERP (r2, r3, y2, y3, y_interp); + gl = LERP (g2, g3, y2, y3, y_interp); + bl = LERP (b2, b3, y2, y3, y_interp); + } + + xr = LERP (x1, x3, y1, y3, y_interp); + + rr = LERP (r1, r3, y1, y3, y_interp); + gr = LERP (g1, g3, y1, y3, y_interp); + br = LERP (b1, b3, y1, y3, y_interp); + + if (xl > xr) + { + SWAP (xl, xr, t); + SWAP (rl, rr, t); + SWAP (gl, gr, t); + SWAP (bl, br, t); + } + + x_start = MAX (xl - PAD, 0); + x_end = MIN (xr + PAD, width); + x_start = MIN (x_start, x_end); + + c = (rl << 16) | (gl << 8) | bl; + + for (xx = 0; xx < x_start; xx++) + *p++ = c; + + for (; xx < x_end; xx++) + { + x_interp = CLAMP (xx, xl, xr); + + *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) | + (LERP (gl, gr, xl, xr, x_interp) << 8) | + LERP (bl, br, xl, xr, x_interp)); + } + + c = (rr << 16) | (gr << 8) | br; + + for (; xx < width; xx++) + *p++ = c; + } + } + + source = cairo_image_surface_create_for_data ((unsigned char *)buf, + CAIRO_FORMAT_RGB24, + width, height, stride); + + /* Draw a triangle with the image as a source */ + + cairo_set_source_surface (cr, source, 0, 0); + cairo_surface_destroy (source); + + cairo_move_to (cr, x1, y1); + cairo_line_to (cr, x2, y2); + cairo_line_to (cr, x3, y3); + cairo_close_path (cr); + cairo_fill (cr); + + g_free (buf); + + /* Draw value marker */ + + xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5); + yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5); + + r = priv->h; + g = priv->s; + b = priv->v; + hsv_to_rgb (&r, &g, &b); + + context = gtk_widget_get_style_context (widget); + + gtk_style_context_save (context); + + if (INTENSITY (r, g, b) > 0.5) + { + gtk_style_context_add_class (context, "light-area-focus"); + cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); + } + else + { + gtk_style_context_add_class (context, "dark-area-focus"); + cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); + } + +#define RADIUS 4 +#define FOCUS_RADIUS 6 + + cairo_new_path (cr); + cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI); + cairo_stroke (cr); + + /* Draw focus outline */ + + if (draw_focus && ! priv->focus_on_ring) + { + gint focus_width; + gint focus_pad; + + gtk_widget_style_get (widget, + "focus-line-width", &focus_width, + "focus-padding", &focus_pad, + NULL); + + gtk_render_focus (context, cr, + xx - FOCUS_RADIUS - focus_width - focus_pad, + yy - FOCUS_RADIUS - focus_width - focus_pad, + 2 * (FOCUS_RADIUS + focus_width + focus_pad), + 2 * (FOCUS_RADIUS + focus_width + focus_pad)); + } + + gtk_style_context_restore (context); +} + +static gboolean +gimp_color_wheel_draw (GtkWidget *widget, + cairo_t *cr) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + gboolean draw_focus; + + draw_focus = gtk_widget_has_visible_focus (widget); + + paint_ring (wheel, cr); + paint_triangle (wheel, cr, draw_focus); + + if (draw_focus && priv->focus_on_ring) + { + GtkStyleContext *context = gtk_widget_get_style_context (widget); + + gtk_render_focus (context, cr, 0, 0, + gtk_widget_get_allocated_width (widget), + gtk_widget_get_allocated_height (widget)); + } + + return FALSE; +} + +static gboolean +gimp_color_wheel_focus (GtkWidget *widget, + GtkDirectionType dir) +{ + GimpColorWheel *wheel = GIMP_COLOR_WHEEL (widget); + GimpColorWheelPrivate *priv = wheel->priv; + + if (!gtk_widget_has_focus (widget)) + { + if (dir == GTK_DIR_TAB_BACKWARD) + priv->focus_on_ring = FALSE; + else + priv->focus_on_ring = TRUE; + + gtk_widget_grab_focus (widget); + return TRUE; + } + + switch (dir) + { + case GTK_DIR_UP: + if (priv->focus_on_ring) + return FALSE; + else + priv->focus_on_ring = TRUE; + break; + + case GTK_DIR_DOWN: + if (priv->focus_on_ring) + priv->focus_on_ring = FALSE; + else + return FALSE; + break; + + case GTK_DIR_LEFT: + case GTK_DIR_TAB_BACKWARD: + if (priv->focus_on_ring) + return FALSE; + else + priv->focus_on_ring = TRUE; + break; + + case GTK_DIR_RIGHT: + case GTK_DIR_TAB_FORWARD: + if (priv->focus_on_ring) + priv->focus_on_ring = FALSE; + else + return FALSE; + break; + } + + gtk_widget_queue_draw (widget); + + return TRUE; +} + +/** + * gimp_color_wheel_new: + * + * Creates a new HSV color selector. + * + * Return value: A newly-created HSV color selector. + * + * Since: 2.14 + */ +GtkWidget* +gimp_color_wheel_new (void) +{ + return g_object_new (GIMP_TYPE_COLOR_WHEEL, NULL); +} + +/** + * gimp_color_wheel_set_color: + * @hsv: An HSV color selector + * @h: Hue + * @s: Saturation + * @v: Value + * + * Sets the current color in an HSV color selector. + * Color component values must be in the [0.0, 1.0] range. + * + * Since: 2.14 + */ +void +gimp_color_wheel_set_color (GimpColorWheel *wheel, + gdouble h, + gdouble s, + gdouble v) +{ + GimpColorWheelPrivate *priv; + + g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel)); + g_return_if_fail (h >= 0.0 && h <= 1.0); + g_return_if_fail (s >= 0.0 && s <= 1.0); + g_return_if_fail (v >= 0.0 && v <= 1.0); + + priv = wheel->priv; + + if(h == 0.0 && s == 0.0) { + h = priv->h; + } + + priv->h = h; + priv->s = s; + priv->v = v; + + g_signal_emit (wheel, wheel_signals[CHANGED], 0); + + gtk_widget_queue_draw (GTK_WIDGET (wheel)); +} + +/** + * gimp_color_wheel_get_color: + * @hsv: An HSV color selector + * @h: (out): Return value for the hue + * @s: (out): Return value for the saturation + * @v: (out): Return value for the value + * + * Queries the current color in an HSV color selector. + * Returned values will be in the [0.0, 1.0] range. + * + * Since: 2.14 + */ +void +gimp_color_wheel_get_color (GimpColorWheel *wheel, + gdouble *h, + gdouble *s, + gdouble *v) +{ + GimpColorWheelPrivate *priv; + + g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel)); + + priv = wheel->priv; + + if (h) *h = priv->h; + if (s) *s = priv->s; + if (v) *v = priv->v; +} + +/** + * gimp_color_wheel_set_ring_fraction: + * @ring: A wheel color selector + * @fraction: Ring fraction + * + * Sets the ring fraction of a wheel color selector. + * + * Since: GIMP 2.10 + */ +void +gimp_color_wheel_set_ring_fraction (GimpColorWheel *hsv, + gdouble fraction) +{ + GimpColorWheelPrivate *priv; + + g_return_if_fail (GIMP_IS_COLOR_WHEEL (hsv)); + + priv = hsv->priv; + + priv->ring_fraction = CLAMP (fraction, 0.01, 0.99); + + gtk_widget_queue_draw (GTK_WIDGET (hsv)); +} + +/** + * gimp_color_wheel_get_ring_fraction: + * @ring: A wheel color selector + * + * Returns value: The ring fraction of the wheel color selector. + * + * Since: GIMP 2.10 + */ +gdouble +gimp_color_wheel_get_ring_fraction (GimpColorWheel *wheel) +{ + GimpColorWheelPrivate *priv; + + g_return_val_if_fail (GIMP_IS_COLOR_WHEEL (wheel), DEFAULT_FRACTION); + + priv = wheel->priv; + + return priv->ring_fraction; +} + +/** + * gimp_color_wheel_is_adjusting: + * @hsv: A #GimpColorWheel + * + * An HSV color selector can be said to be adjusting if multiple rapid + * changes are being made to its value, for example, when the user is + * adjusting the value with the mouse. This function queries whether + * the HSV color selector is being adjusted or not. + * + * Return value: %TRUE if clients can ignore changes to the color value, + * since they may be transitory, or %FALSE if they should consider + * the color value status to be final. + * + * Since: 2.14 + */ +gboolean +gimp_color_wheel_is_adjusting (GimpColorWheel *wheel) +{ + GimpColorWheelPrivate *priv; + + g_return_val_if_fail (GIMP_IS_COLOR_WHEEL (wheel), FALSE); + + priv = wheel->priv; + + return priv->mode != DRAG_NONE; +} + +static void +gimp_color_wheel_move (GimpColorWheel *wheel, + GtkDirectionType dir) +{ + GimpColorWheelPrivate *priv = wheel->priv; + gdouble hue, sat, val; + gint hx, hy, sx, sy, vx, vy; /* HSV vertices */ + gint x, y; /* position in triangle */ + + hue = priv->h; + sat = priv->s; + val = priv->v; + + compute_triangle (wheel, &hx, &hy, &sx, &sy, &vx, &vy); + + x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5); + y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5); + +#define HUE_DELTA 0.002 + switch (dir) + { + case GTK_DIR_UP: + if (priv->focus_on_ring) + hue += HUE_DELTA; + else + { + y -= 1; + compute_sv (wheel, x, y, &sat, &val); + } + break; + + case GTK_DIR_DOWN: + if (priv->focus_on_ring) + hue -= HUE_DELTA; + else + { + y += 1; + compute_sv (wheel, x, y, &sat, &val); + } + break; + + case GTK_DIR_LEFT: + if (priv->focus_on_ring) + hue += HUE_DELTA; + else + { + x -= 1; + compute_sv (wheel, x, y, &sat, &val); + } + break; + + case GTK_DIR_RIGHT: + if (priv->focus_on_ring) + hue -= HUE_DELTA + ; + else + { + x += 1; + compute_sv (wheel, x, y, &sat, &val); + } + break; + + default: + /* we don't care about the tab directions */ + break; + } + + /* Wrap */ + if (hue < 0.0) + hue = 1.0; + else if (hue > 1.0) + hue = 0.0; + + gimp_color_wheel_set_color (wheel, hue, sat, val); +} diff --git a/src/widgets/gimp/gimpcolorwheel.h b/src/widgets/gimp/gimpcolorwheel.h new file mode 100644 index 000000000..016fb593f --- /dev/null +++ b/src/widgets/gimp/gimpcolorwheel.h @@ -0,0 +1,95 @@ +/* HSV color selector for GTK+ + * + * Copyright (C) 1999 The Free Software Foundation + * + * Authors: Simon Budig (original code) + * Federico Mena-Quintero (cleanup for GTK+) + * Jonathan Blandford (cleanup for GTK+) + * + * 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 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, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GTK+ Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GTK+ at ftp://ftp.gtk.org/pub/gtk/. + */ + +#ifndef __GIMP_COLOR_WHEEL_H__ +#define __GIMP_COLOR_WHEEL_H__ + +G_BEGIN_DECLS + +#define GIMP_TYPE_COLOR_WHEEL (gimp_color_wheel_get_type ()) +#define GIMP_COLOR_WHEEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_COLOR_WHEEL, GimpColorWheel)) +#define GIMP_COLOR_WHEEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_COLOR_WHEEL, GimpColorWheelClass)) +#define GIMP_IS_COLOR_WHEEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_COLOR_WHEEL)) +#define GIMP_IS_COLOR_WHEEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_COLOR_WHEEL)) +#define GIMP_COLOR_WHEEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_COLOR_WHEEL, GimpColorWheelClass)) + + +typedef struct _GimpColorWheel GimpColorWheel; +typedef struct _GimpColorWheelClass GimpColorWheelClass; + +struct _GimpColorWheel +{ + GtkWidget parent_instance; + + /* Private data */ + gpointer priv; +}; + +struct _GimpColorWheelClass +{ + GtkWidgetClass parent_class; + + /* Notification signals */ + void (* changed) (GimpColorWheel *wheel); + + /* Keybindings */ + void (* move) (GimpColorWheel *wheel, + GtkDirectionType type); + + /* Padding for future expansion */ + void (*_gimp_reserved1) (void); + void (*_gimp_reserved2) (void); + void (*_gimp_reserved3) (void); + void (*_gimp_reserved4) (void); +}; + + +GType gimp_color_wheel_get_type (void) G_GNUC_CONST; +GtkWidget * gimp_color_wheel_new (void); + +void gimp_color_wheel_set_color (GimpColorWheel *wheel, + double h, + double s, + double v); +void gimp_color_wheel_get_color (GimpColorWheel *wheel, + gdouble *h, + gdouble *s, + gdouble *v); + +void gimp_color_wheel_set_ring_fraction (GimpColorWheel *wheel, + gdouble fraction); +gdouble gimp_color_wheel_get_ring_fraction (GimpColorWheel *wheel); + +gboolean gimp_color_wheel_is_adjusting (GimpColorWheel *wheel); + +G_END_DECLS + +#endif /* __GIMP_COLOR_WHEEL_H__ */ diff --git a/src/widgets/gimp/gimpspinscale.c b/src/widgets/gimp/gimpspinscale.c new file mode 100644 index 000000000..8d8c6c935 --- /dev/null +++ b/src/widgets/gimp/gimpspinscale.c @@ -0,0 +1,969 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpspinscale.c + * Copyright (C) 2010 Michael Natterer + * 2012 Øyvind Kolås + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include + +#include "gimpspinscale.h" + + +enum +{ + PROP_0, + PROP_LABEL, + PROP_FOCUS_WIDGET +}; + +typedef enum +{ + TARGET_NUMBER, + TARGET_UPPER, + TARGET_LOWER, + TARGET_NONE +} SpinScaleTarget; + +typedef enum +{ + APPEARANCE_FULL = 1, /* Full size suitable for tablets */ + APPEARANCE_COMPACT, /* Compact, suitable for desktops with mouse control */ +} SpinScaleAppearance; + +typedef struct _GimpSpinScalePrivate GimpSpinScalePrivate; + +struct _GimpSpinScalePrivate +{ + gchar *label; + + gboolean scale_limits_set; + gdouble scale_lower; + gdouble scale_upper; + gdouble gamma; + + PangoLayout *layout; + gboolean changing_value; + gboolean relative_change; + gdouble start_x; + gdouble start_value; + + GtkWidget* focusWidget; + gboolean transferFocus; + SpinScaleAppearance appearanceMode; +}; + +#define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \ + GIMP_TYPE_SPIN_SCALE, \ + GimpSpinScalePrivate)) + + +static void gimp_spin_scale_dispose (GObject *object); +static void gimp_spin_scale_finalize (GObject *object); +static void gimp_spin_scale_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void gimp_spin_scale_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void gimp_spin_scale_style_set (GtkWidget *widget, + GtkStyle *prev_style); + +static void gimp_spin_scale_get_preferred_width (GtkWidget *widget, + gint *minimum_width, + gint *natural_width); +static void gimp_spin_scale_get_preferred_height (GtkWidget *widget, + gint *minimum_width, + gint *natural_width); +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, + GdkEventButton *event); +static gboolean gimp_spin_scale_motion_notify (GtkWidget *widget, + GdkEventMotion *event); +static gboolean gimp_spin_scale_leave_notify (GtkWidget *widget, + GdkEventCrossing *event); +static gboolean gimp_spin_scale_keypress( GtkWidget *widget, + GdkEventKey *event); + +static void gimp_spin_scale_defocus( GtkSpinButton *spin_button ); + +static void gimp_spin_scale_value_changed (GtkSpinButton *spin_button); + + +G_DEFINE_TYPE (GimpSpinScale, gimp_spin_scale, GTK_TYPE_SPIN_BUTTON); + +#define parent_class gimp_spin_scale_parent_class + + +static void +gimp_spin_scale_class_init (GimpSpinScaleClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + GtkSpinButtonClass *spin_button_class = GTK_SPIN_BUTTON_CLASS (klass); + + object_class->dispose = gimp_spin_scale_dispose; + object_class->finalize = gimp_spin_scale_finalize; + object_class->set_property = gimp_spin_scale_set_property; + object_class->get_property = gimp_spin_scale_get_property; + + widget_class->style_set = gimp_spin_scale_style_set; + widget_class->get_preferred_width = gimp_spin_scale_get_preferred_width; + widget_class->get_preferred_height = gimp_spin_scale_get_preferred_height; + 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_motion_notify; + widget_class->leave_notify_event = gimp_spin_scale_leave_notify; + widget_class->key_press_event = gimp_spin_scale_keypress; + + spin_button_class->value_changed = gimp_spin_scale_value_changed; + + g_object_class_install_property (object_class, PROP_LABEL, + g_param_spec_string ("label", NULL, NULL, + NULL, + G_PARAM_READWRITE)); + + g_type_class_add_private (klass, sizeof (GimpSpinScalePrivate)); +} + +static void +gimp_spin_scale_init (GimpSpinScale *scale) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (scale); + + gtk_widget_add_events (GTK_WIDGET (scale), + GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK | + GDK_POINTER_MOTION_MASK | + GDK_BUTTON1_MOTION_MASK | + GDK_LEAVE_NOTIFY_MASK); + + gtk_entry_set_alignment (GTK_ENTRY (scale), 1.0); + gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (scale), TRUE); + + private->gamma = 1.0; + private->focusWidget = NULL; + private->transferFocus = FALSE; + private->appearanceMode = APPEARANCE_COMPACT; +} + +static void +gimp_spin_scale_dispose (GObject *object) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (object); + + if (private->layout) + { + g_object_unref (private->layout); + private->layout = NULL; + } + + G_OBJECT_CLASS (parent_class)->dispose (object); +} + +static void +gimp_spin_scale_finalize (GObject *object) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (object); + + if (private->label) + { + g_free (private->label); + private->label = NULL; + } + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +gimp_spin_scale_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (object); + GimpSpinScale *scale = GIMP_SPIN_SCALE (object); + + switch (property_id) + { + case PROP_LABEL: + gimp_spin_scale_set_label (scale, g_value_get_string (value)); + break; + + case PROP_FOCUS_WIDGET: + { + /* TODO unhook prior */ + private->focusWidget = GTK_WIDGET (g_value_get_pointer (value)); + } + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +gimp_spin_scale_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (object); + GimpSpinScale *scale = GIMP_SPIN_SCALE (object); + + switch (property_id) + { + case PROP_LABEL: + g_value_set_string (value, gimp_spin_scale_get_label (scale)); + break; + + case PROP_FOCUS_WIDGET: + g_value_set_pointer (value, private->focusWidget); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + + +void +gimp_spin_scale_set_focuswidget( GtkWidget *scale, GtkWidget* widget ) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (scale); + + /* TODO unhook prior */ + + private->focusWidget = widget; +} + +void +gimp_spin_scale_set_appearance( GtkWidget *widget, const gchar *appearance) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + + if ( strcmp("full", appearance) == 0 ) { + private->appearanceMode = APPEARANCE_FULL; + } else if ( strcmp("compact", appearance) == 0 ) { + private->appearanceMode = APPEARANCE_COMPACT; + } +} + +static void +gimp_spin_scale_get_preferred_width (GtkWidget *widget, + gint *minimum_width, + gint *natural_width) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + PangoContext *context = gtk_widget_get_pango_context (widget); + PangoFontMetrics *metrics; + + GTK_WIDGET_CLASS (parent_class)->get_preferred_width (widget, + minimum_width, + natural_width); + + metrics = pango_context_get_metrics (context, + pango_context_get_font_description (context), + pango_context_get_language (context)); + + if (private->label) + { + gint char_width; + gint digit_width; + gint char_pixels; + + char_width = pango_font_metrics_get_approximate_char_width (metrics); + digit_width = pango_font_metrics_get_approximate_digit_width (metrics); + char_pixels = PANGO_PIXELS (MAX (char_width, digit_width)); + + /* ~3 chars for the ellipse */ + *minimum_width += char_pixels * 3; + *natural_width += char_pixels * 3; + } + + pango_font_metrics_unref (metrics); +} + +static void +gimp_spin_scale_get_preferred_height (GtkWidget *widget, + gint *minimum_height, + gint *natural_height) +{ + PangoContext *context = gtk_widget_get_pango_context (widget); + PangoFontMetrics *metrics; + //gint height; + + GTK_WIDGET_CLASS (parent_class)->get_preferred_height (widget, + minimum_height, + natural_height); + + metrics = pango_context_get_metrics (context, + pango_context_get_font_description (context), + pango_context_get_language (context)); + + //height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + + // pango_font_metrics_get_descent (metrics)); + + *minimum_height += 1; + *natural_height += 1; + + pango_font_metrics_unref (metrics); +} + +static void +gimp_spin_scale_style_set (GtkWidget *widget, + GtkStyle *prev_style) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + + GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style); + + if (private->layout) + { + g_object_unref (private->layout); + private->layout = NULL; + } +} + + +static gboolean + gimp_spin_scale_draw (GtkWidget *widget, + cairo_t *cr) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + GtkStyleContext *style = gtk_widget_get_style_context(widget); + GtkAllocation allocation; + GdkRGBA color; + + cairo_save (cr); + GTK_WIDGET_CLASS (parent_class)->draw (widget, cr); + cairo_restore (cr); + + gtk_widget_get_allocation (widget, &allocation); + + cairo_set_line_width (cr, 1.0); + + if (private->label) + { + GdkRectangle text_area; + gint minimum_width; + gint natural_width; + PangoRectangle logical; + gint layout_offset_x; + gint layout_offset_y; + GtkStateFlags state; + GdkRGBA text_color; + GdkRGBA bar_text_color; + gdouble progress_fraction; + gint progress_x; + gint progress_y; + gint progress_width; + gint progress_height; + + gtk_entry_get_text_area (GTK_ENTRY (widget), &text_area); + + GTK_WIDGET_CLASS (parent_class)->get_preferred_width (widget, + &minimum_width, + &natural_width); + + if (! private->layout) + { + private->layout = gtk_widget_create_pango_layout (widget, + private->label); + pango_layout_set_ellipsize (private->layout, PANGO_ELLIPSIZE_END); + } + + pango_layout_set_width (private->layout, + PANGO_SCALE * + (allocation.width - minimum_width)); + pango_layout_get_pixel_extents (private->layout, NULL, &logical); + + gtk_entry_get_layout_offsets (GTK_ENTRY (widget), NULL, &layout_offset_y); + + if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) + layout_offset_x = text_area.x + text_area.width - logical.width - 4; + else + layout_offset_x = 4; + + layout_offset_x -= logical.x; + + state = gtk_widget_get_state_flags (widget); + + gtk_style_context_get_color (style, state, &text_color); + + gtk_style_context_save (style); + gtk_style_context_add_class (style, GTK_STYLE_CLASS_PROGRESSBAR); + gtk_style_context_get_color (style, state, &bar_text_color); + gtk_style_context_restore (style); + + progress_fraction = gtk_entry_get_progress_fraction (GTK_ENTRY (widget)); + + if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) + { + progress_fraction = 1.0 - progress_fraction; + + progress_x = text_area.width * progress_fraction; + progress_y = 0; + progress_width = text_area.width - progress_x; + progress_height = text_area.height; + } + else + { + progress_x = 0; + progress_y = 0; + progress_width = text_area.width * progress_fraction; + progress_height = text_area.height; + } + + cairo_save (cr); + + cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD); + cairo_rectangle (cr, 0, 0, text_area.width, text_area.height); + cairo_rectangle (cr, progress_x, progress_y, + progress_width, progress_height); + cairo_clip (cr); + cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING); + + cairo_move_to (cr, layout_offset_x, text_area.y + layout_offset_y-3); + gdk_cairo_set_source_rgba (cr, &text_color); + pango_cairo_show_layout (cr, private->layout); + cairo_restore (cr); + + cairo_rectangle (cr, progress_x, progress_y, + progress_width, progress_height); + cairo_clip (cr); + + cairo_move_to (cr, layout_offset_x, text_area.y + layout_offset_y-3); + gdk_cairo_set_source_rgba (cr, &bar_text_color); + pango_cairo_show_layout (cr, private->layout); + } + + 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 SpinScaleTarget +gimp_spin_scale_get_target (GtkWidget *widget, + gdouble x, + gdouble y) +{ + GtkAllocation allocation; + PangoRectangle logical; + gint layout_x; + gint layout_y; + + gtk_widget_get_allocation (widget, &allocation); + gtk_entry_get_layout_offsets (GTK_ENTRY (widget), &layout_x, &layout_y); + pango_layout_get_pixel_extents (gtk_entry_get_layout (GTK_ENTRY (widget)), + NULL, &logical); + + GdkRectangle text_area; + gtk_entry_get_text_area (GTK_ENTRY (widget), &text_area); + + if (x >= text_area.x && x < text_area.width && + y >= text_area.y && y < text_area.height) + { + x -= text_area.x; + y -= text_area.y; + + if (x > layout_x && x < layout_x + logical.width && + y > layout_y && y < layout_y + logical.height) + { + return TARGET_NUMBER; + } + else if (y > text_area.height / 2) + { + return TARGET_LOWER; + } + + return TARGET_UPPER; + } + + return TARGET_NONE; +} + +static void +gimp_spin_scale_get_limits (GimpSpinScale *scale, + gdouble *lower, + gdouble *upper) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (scale); + + if (private->scale_limits_set) + { + *lower = private->scale_lower; + *upper = private->scale_upper; + } + else + { + GtkSpinButton *spin_button = GTK_SPIN_BUTTON (scale); + GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); + + *lower = gtk_adjustment_get_lower (adjustment); + *upper = gtk_adjustment_get_upper (adjustment); + } +} + +static void +gimp_spin_scale_change_value (GtkWidget *widget, + gdouble x) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget); + GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); + gdouble lower; + gdouble upper; + gdouble value; + GdkRectangle text_area; + gtk_entry_get_text_area (GTK_ENTRY (widget), &text_area); + gimp_spin_scale_get_limits (GIMP_SPIN_SCALE (widget), &lower, &upper); + + if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) + x = text_area.width - x; + + if (private->relative_change) + { + gdouble diff; + gdouble step; + + step = (upper - lower) / text_area.width / 10.0; + if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) + diff = x - (text_area.width - private->start_x); + else + diff = x - private->start_x; + + value = (private->start_value + diff * step); + } + else + { + gdouble fraction; + + fraction = x / (gdouble) text_area.width; + if (fraction > 0.0) + fraction = pow (fraction, private->gamma); + + value = fraction * (upper - lower) + lower; + } + + gtk_adjustment_set_value (adjustment, value); +} + +static gboolean +gimp_spin_scale_button_press (GtkWidget *widget, + GdkEventButton *event) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + + private->changing_value = FALSE; + private->relative_change = FALSE; + + gint x, y; + gimp_spin_scale_event_to_widget_coords (widget, event->window, + event->x, event->y, + &x, &y); + switch (gimp_spin_scale_get_target (widget, x, y)) + { + case TARGET_UPPER: + private->changing_value = TRUE; + + gtk_widget_grab_focus (widget); + + gimp_spin_scale_change_value (widget, x); + + return TRUE; + + case TARGET_LOWER: + private->changing_value = TRUE; + + gtk_widget_grab_focus (widget); + + private->relative_change = TRUE; + private->start_x = x; + private->start_value = gtk_adjustment_get_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget))); + + return TRUE; + + default: + break; + } + + return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event); +} + +static gboolean +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_change_value (widget, x); + return TRUE; + } + + return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event); +} + +static gboolean +gimp_spin_scale_motion_notify (GtkWidget *widget, + GdkEventMotion *event) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + + gdk_event_request_motions (event); + + 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_change_value (widget, x); + + return TRUE; + } + + GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event); + + if (! (event->state & + (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) + ) + { + GdkDisplay *display = gtk_widget_get_display (widget); + GdkCursor *cursor = NULL; + + switch (gimp_spin_scale_get_target (widget, x, y)) + { + case TARGET_NUMBER: + cursor = gdk_cursor_new_for_display (display, GDK_XTERM); + break; + + case TARGET_UPPER: + cursor = gdk_cursor_new_for_display (display, GDK_SB_UP_ARROW); + break; + + case TARGET_LOWER: + cursor = gdk_cursor_new_for_display (display, GDK_SB_H_DOUBLE_ARROW); + break; + + default: + break; + } + + if (cursor) + { + gdk_window_set_cursor (event->window, cursor); + g_object_unref (cursor); + } + } + + return FALSE; +} + +static gboolean +gimp_spin_scale_leave_notify (GtkWidget *widget, + GdkEventCrossing *event) +{ + gdk_window_set_cursor (event->window, NULL); + + return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); +} + +gboolean gimp_spin_scale_keypress( GtkWidget *widget, GdkEventKey *event) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (widget); + guint key = 0; + gdk_keymap_translate_keyboard_state( gdk_keymap_get_for_display( gdk_display_get_default() ), + event->hardware_keycode, (GdkModifierType)event->state, + 0, &key, 0, 0, 0 ); + + switch ( key ) { + + case GDK_KEY_Escape: + case GDK_KEY_Return: + case GDK_KEY_KP_Enter: + { + private->transferFocus = TRUE; + gimp_spin_scale_defocus( GTK_SPIN_BUTTON(widget) ); + } + break; + + } + + return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event); +} + +static void +gimp_spin_scale_defocus( GtkSpinButton *spin_button ) +{ + GimpSpinScalePrivate *private = GET_PRIVATE (spin_button); + + if ( private->transferFocus ) { + if ( private->focusWidget ) { + gtk_widget_grab_focus( private->focusWidget ); + } + } +} + +static void +gimp_spin_scale_value_changed (GtkSpinButton *spin_button) +{ + GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); + GimpSpinScalePrivate *private = GET_PRIVATE (spin_button); + gdouble lower; + gdouble upper; + gdouble value; + + gimp_spin_scale_get_limits (GIMP_SPIN_SCALE (spin_button), &lower, &upper); + + value = CLAMP (gtk_adjustment_get_value (adjustment), lower, upper); + + gtk_entry_set_progress_fraction (GTK_ENTRY (spin_button), + pow ((value - lower) / (upper - lower), + 1.0 / private->gamma)); + + // TODO - Allow scrollwheel to change value then return focus, + // but clicks/keypress should keep focus in the control + //if ( gtk_widget_has_focus( GTK_WIDGET(spin_button) ) ) { + // gimp_spin_scale_defocus( spin_button ); + //} +} + + +/* public functions */ + +GtkWidget * +gimp_spin_scale_new (GtkAdjustment *adjustment, + const gchar *label, + gint digits) +{ + g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL); + + return g_object_new (GIMP_TYPE_SPIN_SCALE, + "adjustment", adjustment, + "label", label, + "digits", digits, + NULL); +} + +void +gimp_spin_scale_set_label (GimpSpinScale *scale, + const gchar *label) +{ + GimpSpinScalePrivate *private; + + g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); + + private = GET_PRIVATE (scale); + + if (label == private->label) + return; + + g_free (private->label); + private->label = g_strdup (label); + + if (private->layout) + { + g_object_unref (private->layout); + private->layout = NULL; + } + + gtk_widget_queue_resize (GTK_WIDGET (scale)); + + g_object_notify (G_OBJECT (scale), "label"); +} + +const gchar * +gimp_spin_scale_get_label (GimpSpinScale *scale) +{ + g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), NULL); + + return GET_PRIVATE (scale)->label; +} + +void +gimp_spin_scale_set_scale_limits (GimpSpinScale *scale, + gdouble lower, + gdouble upper) +{ + GimpSpinScalePrivate *private; + GtkSpinButton *spin_button; + GtkAdjustment *adjustment; + + g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); + + private = GET_PRIVATE (scale); + spin_button = GTK_SPIN_BUTTON (scale); + adjustment = gtk_spin_button_get_adjustment (spin_button); + + g_return_if_fail (lower >= gtk_adjustment_get_lower (adjustment)); + g_return_if_fail (upper <= gtk_adjustment_get_upper (adjustment)); + + private->scale_limits_set = TRUE; + private->scale_lower = lower; + private->scale_upper = upper; + private->gamma = 1.0; + + gimp_spin_scale_value_changed (spin_button); +} + +void +gimp_spin_scale_unset_scale_limits (GimpSpinScale *scale) +{ + GimpSpinScalePrivate *private; + + g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); + + private = GET_PRIVATE (scale); + + private->scale_limits_set = FALSE; + private->scale_lower = 0.0; + private->scale_upper = 0.0; + + gimp_spin_scale_value_changed (GTK_SPIN_BUTTON (scale)); +} + +gboolean +gimp_spin_scale_get_scale_limits (GimpSpinScale *scale, + gdouble *lower, + gdouble *upper) +{ + GimpSpinScalePrivate *private; + + g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), FALSE); + + private = GET_PRIVATE (scale); + + if (lower) + *lower = private->scale_lower; + + if (upper) + *upper = private->scale_upper; + + return private->scale_limits_set; +} + +void +gimp_spin_scale_set_gamma (GimpSpinScale *scale, + gdouble gamma) +{ + GimpSpinScalePrivate *private; + + g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); + + private = GET_PRIVATE (scale); + + private->gamma = gamma; + + gimp_spin_scale_value_changed (GTK_SPIN_BUTTON (scale)); +} + +gdouble +gimp_spin_scale_get_gamma (GimpSpinScale *scale) +{ + g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), 1.0); + + return GET_PRIVATE(scale)->gamma; +} diff --git a/src/widgets/gimp/gimpspinscale.h b/src/widgets/gimp/gimpspinscale.h new file mode 100644 index 000000000..b42a0faf8 --- /dev/null +++ b/src/widgets/gimp/gimpspinscale.h @@ -0,0 +1,82 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpspinscale.h + * Copyright (C) 2010 Michael Natterer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __GIMP_SPIN_SCALE_H__ +#define __GIMP_SPIN_SCALE_H__ + +#ifndef WITH_GIMP +#include +#endif + +G_BEGIN_DECLS + +#define GIMP_TYPE_SPIN_SCALE (gimp_spin_scale_get_type ()) +#define GIMP_SPIN_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_SPIN_SCALE, GimpSpinScale)) +#define GIMP_SPIN_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_SPIN_SCALE, GimpSpinScaleClass)) +#define GIMP_IS_SPIN_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_SPIN_SCALE)) +#define GIMP_IS_SPIN_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_SPIN_SCALE)) +#define GIMP_SPIN_SCALE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_SPIN_SCALE, GimpSpinScaleClass)) + + +typedef struct _GimpSpinScale GimpSpinScale; +typedef struct _GimpSpinScaleClass GimpSpinScaleClass; + +struct _GimpSpinScale +{ + GtkSpinButton parent_instance; +}; + +struct _GimpSpinScaleClass +{ + GtkSpinButtonClass parent_class; +}; + + +GType gimp_spin_scale_get_type (void) G_GNUC_CONST; + +GtkWidget * gimp_spin_scale_new (GtkAdjustment *adjustment, + const gchar *label, + gint digits); + +void gimp_spin_scale_set_label (GimpSpinScale *scale, + const gchar *label); +const gchar * gimp_spin_scale_get_label (GimpSpinScale *scale); + +void gimp_spin_scale_set_scale_limits (GimpSpinScale *scale, + gdouble lower, + gdouble upper); +void gimp_spin_scale_unset_scale_limits (GimpSpinScale *scale); +gboolean gimp_spin_scale_get_scale_limits (GimpSpinScale *scale, + gdouble *lower, + gdouble *upper); + +void gimp_spin_scale_set_gamma (GimpSpinScale *scale, + gdouble gamma); +gdouble gimp_spin_scale_get_gamma (GimpSpinScale *scale); + +void gimp_spin_scale_set_focuswidget (GtkWidget *scale, + GtkWidget *widget); + +void gimp_spin_scale_set_appearance (GtkWidget *scale, + const gchar *appearance); + +G_END_DECLS + +#endif /* __GIMP_SPIN_SCALE_H__ */ diff --git a/src/widgets/gimp/ruler.cpp b/src/widgets/gimp/ruler.cpp new file mode 100644 index 000000000..bfb9c9071 --- /dev/null +++ b/src/widgets/gimp/ruler.cpp @@ -0,0 +1,1425 @@ +/* + * Customized ruler class for inkscape. Note that this is a fork of + * the GimpRuler widget from GIMP: libgimpwidgets/gimpruler.c. + * The GIMP code is released under the GPL 3. The GIMP code itself + * is a fork of the now-obsolete GtkRuler widget from GTK+ 2. + * + * Major differences between implementations in Inkscape and GIMP are + * as follows: + * - We use a 1,2,4,8... scale for inches and 1,2,5,10... for everything + * else. GIMP uses 1,2,5,10... for everything. + * + * - We use a default font size of PANGO_SCALE_X_SMALL for labels, + * GIMP uses PANGO_SCALE_SMALL (i.e., a bit larger than ours). + * + * - We abbreviate large numbers in tick-labels (e.g., 10000 -> 10k) + * + * Authors: + * Lauris Kaplinski + * Frank Felfe + * bulia byak + * Diederik van Lierop + * Jon A. Cruz + * Alex Valavanis + * + * Copyright (C) 1999-2011 authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include +#include +#include + +#include "ruler.h" +#include "round.h" +#include +#include "util/units.h" + +#define ROUND(x) ((int) ((x) + 0.5)) + +#define GTK_PARAM_READWRITE G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB + +#define DEFAULT_RULER_FONT_SCALE PANGO_SCALE_X_SMALL +#define MINIMUM_INCR 5 +#define IMMEDIATE_REDRAW_THRESHOLD 20 + +using Inkscape::Util::unit_table; + +enum { + PROP_0, + PROP_ORIENTATION, + PROP_UNIT, + PROP_LOWER, + PROP_UPPER, + PROP_POSITION, + PROP_MAX_SIZE +}; + + +/* All distances below are in 1/72nd's of an inch. (According to + * Adobe, that's a point, but points are really 1/72.27 in.) + */ +typedef struct +{ + GtkOrientation orientation; + Inkscape::Util::Unit const *unit; + gdouble lower; + gdouble upper; + gdouble position; + gdouble max_size; + + GdkWindow *input_window; + cairo_surface_t *backing_store; + gboolean backing_store_valid; + GdkRectangle last_pos_rect; + guint pos_redraw_idle_id; + PangoLayout *layout; + gdouble font_scale; + + GList *track_widgets; +} SPRulerPrivate; + +#define SP_RULER_GET_PRIVATE(ruler) \ + G_TYPE_INSTANCE_GET_PRIVATE (ruler, SP_TYPE_RULER, SPRulerPrivate) + + +struct SPRulerMetric +{ + gdouble ruler_scale[16]; + gint subdivide[5]; +}; + +// Ruler metric for general use. +static SPRulerMetric const ruler_metric_general = { + { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000 }, + { 1, 5, 10, 50, 100 } +}; + +// Ruler metric for inch scales. +static SPRulerMetric const ruler_metric_inches = { + { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 }, + { 1, 2, 4, 8, 16 } +}; + +static void sp_ruler_dispose (GObject *object); +static void sp_ruler_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void sp_ruler_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + +static void sp_ruler_realize (GtkWidget *widget); +static void sp_ruler_unrealize (GtkWidget *widget); +static void sp_ruler_map (GtkWidget *widget); +static void sp_ruler_unmap (GtkWidget *widget); +static void sp_ruler_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); + +static void sp_ruler_get_preferred_width (GtkWidget *widget, + gint *minimum_width, + gint *natural_width); + +static void sp_ruler_get_preferred_height (GtkWidget *widget, + gint *minimum_height, + gint *natural_height); +static void sp_ruler_style_updated (GtkWidget *widget); + +static gboolean sp_ruler_motion_notify (GtkWidget *widget, + GdkEventMotion *event); +static gboolean sp_ruler_draw (GtkWidget *widget, + cairo_t *cr); +static void sp_ruler_draw_ticks (SPRuler *ruler); +static GdkRectangle sp_ruler_get_pos_rect (SPRuler *ruler, + gdouble position); +static gboolean sp_ruler_idle_queue_pos_redraw(gpointer data); +static void sp_ruler_queue_pos_redraw (SPRuler *ruler); +static void sp_ruler_draw_pos (SPRuler *ruler, + cairo_t *cr); +static void sp_ruler_make_pixmap (SPRuler *ruler); + +static PangoLayout * sp_ruler_get_layout (GtkWidget *widget, + const gchar *text); + + +G_DEFINE_TYPE (SPRuler, sp_ruler, GTK_TYPE_WIDGET) + +#define parent_class sp_ruler_parent_class + + +static void +sp_ruler_class_init (SPRulerClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + +#if GTK_CHECK_VERSION(3,20,0) + gtk_widget_class_set_css_name (widget_class, "ruler-widget"); +#endif + + object_class->dispose = sp_ruler_dispose; + object_class->set_property = sp_ruler_set_property; + object_class->get_property = sp_ruler_get_property; + + widget_class->realize = sp_ruler_realize; + widget_class->unrealize = sp_ruler_unrealize; + widget_class->map = sp_ruler_map; + widget_class->unmap = sp_ruler_unmap; + widget_class->size_allocate = sp_ruler_size_allocate; + widget_class->get_preferred_width = sp_ruler_get_preferred_width; + widget_class->get_preferred_height = sp_ruler_get_preferred_height; + widget_class->style_updated = sp_ruler_style_updated; + widget_class->draw = sp_ruler_draw; + widget_class->motion_notify_event = sp_ruler_motion_notify; + + g_type_class_add_private (object_class, sizeof (SPRulerPrivate)); + + g_object_class_install_property (object_class, + PROP_ORIENTATION, + g_param_spec_enum ("orientation", + _("Orientation"), + _("The orientation of the ruler"), + GTK_TYPE_ORIENTATION, + GTK_ORIENTATION_HORIZONTAL, + static_cast(GTK_PARAM_READWRITE))); + + /* FIXME: Should probably use g_param_spec_enum */ + g_object_class_install_property (object_class, + PROP_UNIT, + g_param_spec_string ("unit", + _("Unit"), + _("Unit of the ruler"), + "px", + static_cast(GTK_PARAM_READWRITE))); + + g_object_class_install_property (object_class, + PROP_LOWER, + g_param_spec_double ("lower", + _("Lower"), + _("Lower limit of ruler"), + -G_MAXDOUBLE, + G_MAXDOUBLE, + 0.0, + static_cast(GTK_PARAM_READWRITE))); + + g_object_class_install_property (object_class, + PROP_UPPER, + g_param_spec_double ("upper", + _("Upper"), + _("Upper limit of ruler"), + -G_MAXDOUBLE, + G_MAXDOUBLE, + 0.0, + static_cast(GTK_PARAM_READWRITE))); + + g_object_class_install_property (object_class, + PROP_POSITION, + g_param_spec_double ("position", + _("Position"), + _("Position of mark on the ruler"), + -G_MAXDOUBLE, + G_MAXDOUBLE, + 0.0, + static_cast(GTK_PARAM_READWRITE))); + + g_object_class_install_property (object_class, + PROP_MAX_SIZE, + g_param_spec_double ("max-size", + _("Max Size"), + _("Maximum size of the ruler"), + -G_MAXDOUBLE, + G_MAXDOUBLE, + 0.0, + static_cast(GTK_PARAM_READWRITE))); + + gtk_widget_class_install_style_property (widget_class, + g_param_spec_double ("font-scale", + NULL, NULL, + 0.0, + G_MAXDOUBLE, + DEFAULT_RULER_FONT_SCALE, + G_PARAM_READABLE)); +} + +static void +sp_ruler_init (SPRuler *ruler) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + + gtk_widget_set_has_window (GTK_WIDGET (ruler), FALSE); + + priv->orientation = GTK_ORIENTATION_HORIZONTAL; + priv->unit = unit_table.getUnit("px"); + priv->lower = 0; + priv->upper = 0; + priv->position = 0; + priv->max_size = 0; + + priv->backing_store = NULL; + priv->backing_store_valid = FALSE; + + priv->last_pos_rect.x = 0; + priv->last_pos_rect.y = 0; + priv->last_pos_rect.width = 0; + priv->last_pos_rect.height = 0; + priv->pos_redraw_idle_id = 0; + + priv->font_scale = DEFAULT_RULER_FONT_SCALE; +} + +static void +sp_ruler_dispose (GObject *object) +{ + SPRuler *ruler = SP_RULER (object); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + + while (priv->track_widgets) + sp_ruler_remove_track_widget (ruler, GTK_WIDGET(priv->track_widgets->data)); + + if (priv->pos_redraw_idle_id) + { + g_source_remove (priv->pos_redraw_idle_id); + priv->pos_redraw_idle_id = 0; + } + + G_OBJECT_CLASS (parent_class)->dispose (object); +} + + +/** + * sp_ruler_set_range: + * @ruler: the SPRuler + * @lower: the lower limit of the ruler + * @upper: the upper limit of the ruler + * @max_size: the maximum size of the ruler used when calculating the space to + * leave for the text + * + * This sets the range of the ruler. + */ +void +sp_ruler_set_range (SPRuler *ruler, + gdouble lower, + gdouble upper, + gdouble max_size) +{ + SPRulerPrivate *priv; + + g_return_if_fail (SP_IS_RULER (ruler)); + + priv = SP_RULER_GET_PRIVATE (ruler); + + g_object_freeze_notify (G_OBJECT (ruler)); + if (priv->lower != lower) + { + priv->lower = lower; + g_object_notify (G_OBJECT (ruler), "lower"); + } + if (priv->upper != upper) + { + priv->upper = upper; + g_object_notify (G_OBJECT (ruler), "upper"); + } + if (priv->max_size != max_size) + { + priv->max_size = max_size; + g_object_notify (G_OBJECT (ruler), "max-size"); + } + g_object_thaw_notify (G_OBJECT (ruler)); + + priv->backing_store_valid = FALSE; + gtk_widget_queue_draw (GTK_WIDGET (ruler)); +} + +/** + * sp_ruler_get_range: + * @ruler: a #SPRuler + * @lower: (allow-none): location to store lower limit of the ruler, or %NULL + * @upper: (allow-none): location to store upper limit of the ruler, or %NULL + * @max_size: location to store the maximum size of the ruler used when calculating + * the space to leave for the text, or %NULL. + * + * Retrieves values indicating the range and current position of a #SPRuler. + * See sp_ruler_set_range(). + **/ +void +sp_ruler_get_range (SPRuler *ruler, + gdouble *lower, + gdouble *upper, + gdouble *max_size) +{ + SPRulerPrivate *priv; + + g_return_if_fail (SP_IS_RULER (ruler)); + + priv = SP_RULER_GET_PRIVATE (ruler); + + if (lower) + *lower = priv->lower; + if (upper) + *upper = priv->upper; + if (max_size) + *max_size = priv->max_size; +} + +static void +sp_ruler_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + SPRuler *ruler = SP_RULER (object); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + + switch (prop_id) + { + case PROP_ORIENTATION: + priv->orientation = static_cast(g_value_get_enum (value)); + gtk_widget_queue_resize (GTK_WIDGET (ruler)); + break; + + case PROP_UNIT: + sp_ruler_set_unit (ruler, unit_table.getUnit(g_value_get_string (value))); + break; + + case PROP_LOWER: + sp_ruler_set_range (ruler, + g_value_get_double (value), + priv->upper, + priv->max_size); + break; + case PROP_UPPER: + sp_ruler_set_range (ruler, + priv->lower, + g_value_get_double (value), + priv->max_size); + break; + + case PROP_POSITION: + sp_ruler_set_position (ruler, g_value_get_double (value)); + break; + + case PROP_MAX_SIZE: + sp_ruler_set_range (ruler, + priv->lower, + priv->upper, + g_value_get_double (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +sp_ruler_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SPRuler *ruler = SP_RULER (object); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + + switch (prop_id) + { + case PROP_ORIENTATION: + g_value_set_enum (value, priv->orientation); + break; + + case PROP_UNIT: + g_value_set_string (value, priv->unit->abbr.c_str()); + break; + case PROP_LOWER: + g_value_set_double (value, priv->lower); + break; + case PROP_UPPER: + g_value_set_double (value, priv->upper); + break; + case PROP_POSITION: + g_value_set_double (value, priv->position); + break; + case PROP_MAX_SIZE: + g_value_set_double (value, priv->max_size); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +sp_ruler_realize (GtkWidget *widget) +{ + SPRuler *ruler = SP_RULER (widget); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + GtkAllocation allocation; + GdkWindowAttr attributes; + gint attributes_mask; + + GTK_WIDGET_CLASS (sp_ruler_parent_class)->realize (widget); + + gtk_widget_get_allocation (widget, &allocation); + + attributes.window_type = GDK_WINDOW_CHILD; + attributes.x = allocation.x; + attributes.y = allocation.y; + attributes.width = allocation.width; + attributes.height = allocation.height; + attributes.wclass = GDK_INPUT_ONLY; + 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; + + priv->input_window = gdk_window_new (gtk_widget_get_parent_window (widget), + &attributes, attributes_mask); + gdk_window_set_user_data (priv->input_window, ruler); + + sp_ruler_make_pixmap (ruler); +} + +static void +sp_ruler_unrealize(GtkWidget *widget) +{ + SPRuler *ruler = SP_RULER (widget); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + + if (priv->backing_store) + { + cairo_surface_destroy (priv->backing_store); + priv->backing_store = NULL; + } + + priv->backing_store_valid = FALSE; + + if (priv->layout) + { + g_object_unref (priv->layout); + priv->layout = NULL; + } + + if (priv->input_window) + { + gdk_window_destroy (priv->input_window); + priv->input_window = NULL; + } + + GTK_WIDGET_CLASS (sp_ruler_parent_class)->unrealize (widget); +} + +static void +sp_ruler_map (GtkWidget *widget) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); + + GTK_WIDGET_CLASS (sp_ruler_parent_class)->map (widget); + + if (priv->input_window) + gdk_window_show (priv->input_window); +} + +static void +sp_ruler_unmap (GtkWidget *widget) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); + + if (priv->input_window) + gdk_window_hide (priv->input_window); + + GTK_WIDGET_CLASS (sp_ruler_parent_class)->unmap (widget); +} + +static void +sp_ruler_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + SPRuler *ruler = SP_RULER(widget); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + 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); + + if (gtk_widget_get_realized (widget)) + { + gdk_window_move_resize (priv->input_window, + allocation->x, allocation->y, + allocation->width, allocation->height); + + if (resized) + sp_ruler_make_pixmap (ruler); + } +} + +static void +sp_ruler_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); + PangoLayout *layout; + PangoRectangle ink_rect; + gint size; + + layout = sp_ruler_get_layout (widget, "0123456789"); + pango_layout_get_pixel_extents (layout, &ink_rect, NULL); + + size = 2 + ink_rect.height * 1.7; + + GtkStyleContext *context = gtk_widget_get_style_context (widget); + GtkBorder border; + + gtk_style_context_get_border (context, static_cast(0), &border); + + requisition->width = border.left + border.right; + requisition->height = border.top + border.bottom; + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + requisition->width += 1; + requisition->height += size; + } + else + { + requisition->width += size; + requisition->height += 1; + } +} + +static void +sp_ruler_style_updated (GtkWidget *widget) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); + + GTK_WIDGET_CLASS (sp_ruler_parent_class)->style_updated (widget); + + gtk_widget_style_get (widget, + "font-scale", &priv->font_scale, + NULL); + + if (priv->layout) + { + g_object_unref (priv->layout); + priv->layout = NULL; + } +} + +static void +sp_ruler_get_preferred_width (GtkWidget *widget, + gint *minimum_width, + gint *natural_width) +{ + GtkRequisition requisition; + + sp_ruler_size_request (widget, &requisition); + + *minimum_width = *natural_width = requisition.width; +} + +static void +sp_ruler_get_preferred_height (GtkWidget *widget, + gint *minimum_height, + gint *natural_height) +{ + GtkRequisition requisition; + + sp_ruler_size_request(widget, &requisition); + + *minimum_height = *natural_height = requisition.height; +} + +static gboolean +sp_ruler_draw (GtkWidget *widget, + cairo_t *cr) +{ + SPRuler *ruler = SP_RULER (widget); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + GtkStyleContext *context = gtk_widget_get_style_context (widget); + GtkAllocation allocation; + + gtk_widget_get_allocation (widget, &allocation); + gtk_render_background (context, cr, 0, 0, allocation.width, allocation.height); + gtk_render_frame (context, cr, 0, 0, allocation.width, allocation.height); + + sp_ruler_draw_ticks (ruler); + + cairo_set_source_surface(cr, priv->backing_store, 0, 0); + cairo_paint(cr); + + sp_ruler_draw_pos (ruler, cr); + + return FALSE; +} + +static void +sp_ruler_make_pixmap (SPRuler *ruler) +{ + GtkWidget *widget = GTK_WIDGET (ruler); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + GtkAllocation allocation; + + gtk_widget_get_allocation(widget, &allocation); + + if (priv->backing_store) + cairo_surface_destroy (priv->backing_store); + + priv->backing_store = + gdk_window_create_similar_surface (gtk_widget_get_window (widget), + CAIRO_CONTENT_COLOR_ALPHA, + allocation.width, + allocation.height); + + priv->backing_store_valid = FALSE; +} + +static void +sp_ruler_draw_pos (SPRuler *ruler, + cairo_t *cr) +{ + GtkWidget *widget = GTK_WIDGET (ruler); + + GtkStyleContext *context = gtk_widget_get_style_context (widget); + GdkRGBA color; + + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + GdkRectangle pos_rect; + + if (! gtk_widget_is_drawable (widget)) + return; + + pos_rect = sp_ruler_get_pos_rect (ruler, sp_ruler_get_position (ruler)); + + if ((pos_rect.width > 0) && (pos_rect.height > 0)) + { + gtk_style_context_get_color (context, gtk_widget_get_state_flags (widget), + &color); + gdk_cairo_set_source_rgba (cr, &color); + + cairo_move_to (cr, pos_rect.x, pos_rect.y); + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + cairo_line_to (cr, pos_rect.x + pos_rect.width / 2.0, + pos_rect.y + pos_rect.height); + cairo_line_to (cr, pos_rect.x + pos_rect.width, + pos_rect.y); + } + else + { + cairo_line_to (cr, pos_rect.x + pos_rect.width, + + pos_rect.y + pos_rect.height / 2.0); + cairo_line_to (cr, pos_rect.x, + pos_rect.y + pos_rect.height); + } + + cairo_fill (cr); + } + + priv->last_pos_rect = pos_rect; +} + +/** + * sp_ruler_new: + * @orientation: the ruler's orientation + * + * Creates a new ruler. + * + * Return value: a new #SPRuler widget. + */ +GtkWidget * +sp_ruler_new (GtkOrientation orientation) +{ + return GTK_WIDGET (g_object_new (SP_TYPE_RULER, + "orientation", orientation, + NULL)); +} + +static void +sp_ruler_update_position (SPRuler *ruler, + gdouble x, + gdouble y) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + GtkAllocation allocation; + gdouble lower; + gdouble upper; + + gtk_widget_get_allocation (GTK_WIDGET (ruler), &allocation); + sp_ruler_get_range (ruler, &lower, &upper, NULL); + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + sp_ruler_set_position (ruler, + lower + + (upper - lower) * x / allocation.width); + } + else + { + sp_ruler_set_position (ruler, + lower + + (upper - lower) * y / allocation.height); + } +} + +/* 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_effective_parent (w)) + { + gdouble px, py; + + gdk_window_coords_to_parent (w, *x, *y, &px, &py); + + *x += px; + *y += px; + } + + if (w == NULL) + { + *x = 0; + *y = 0; + return FALSE; + } + + return TRUE; +} + +static void +sp_ruler_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 +sp_ruler_track_widget_motion_notify (GtkWidget *widget, + GdkEventMotion *mevent, + SPRuler *ruler) +{ + gint widget_x; + gint widget_y; + gint ruler_x; + gint ruler_y; + + widget = gtk_get_event_widget (reinterpret_cast(mevent)); + + sp_ruler_event_to_widget_coords (widget, mevent->window, + mevent->x, mevent->y, + &widget_x, &widget_y); + + if (gtk_widget_translate_coordinates (widget, GTK_WIDGET (ruler), + widget_x, widget_y, + &ruler_x, &ruler_y)) + { + sp_ruler_update_position (ruler, ruler_x, ruler_y); + } + + return FALSE; +} + +void +sp_ruler_add_track_widget (SPRuler *ruler, + GtkWidget *widget) +{ + SPRulerPrivate *priv; + + g_return_if_fail (SP_IS_RULER (ruler)); + g_return_if_fail (GTK_IS_WIDGET (ruler)); + + priv = SP_RULER_GET_PRIVATE (ruler); + + g_return_if_fail (g_list_find (priv->track_widgets, widget) == NULL); + + priv->track_widgets = g_list_prepend (priv->track_widgets, widget); + + g_signal_connect (widget, "motion-notify-event", + G_CALLBACK (sp_ruler_track_widget_motion_notify), + ruler); + g_signal_connect (widget, "destroy", + G_CALLBACK (sp_ruler_remove_track_widget), + ruler); +} + +/** + * sp_ruler_remove_track_widget: + * @ruler: an #SPRuler + * @widget: the track widget to remove + * + * Removes a previously added track widget from the ruler. See + * sp_ruler_add_track_widget(). + */ +void +sp_ruler_remove_track_widget (SPRuler *ruler, + GtkWidget *widget) +{ + SPRulerPrivate *priv; + + g_return_if_fail (SP_IS_RULER (ruler)); + g_return_if_fail (GTK_IS_WIDGET (ruler)); + + priv = SP_RULER_GET_PRIVATE (ruler); + + g_return_if_fail (g_list_find (priv->track_widgets, widget) != NULL); + + priv->track_widgets = g_list_remove (priv->track_widgets, widget); + + g_signal_handlers_disconnect_by_func (widget, + (gpointer) G_CALLBACK (sp_ruler_track_widget_motion_notify), + ruler); + g_signal_handlers_disconnect_by_func (widget, + (gpointer) G_CALLBACK (sp_ruler_remove_track_widget), + ruler); +} + +/** + * sp_ruler_set_unit: + * @ruler: a #SPRuler + * @unit: the #SPMetric to set the ruler to + * + * This sets the unit of the ruler. + */ +void +sp_ruler_set_unit (SPRuler *ruler, + Inkscape::Util::Unit const *unit) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + + g_return_if_fail (SP_IS_RULER (ruler)); + + if (*priv->unit != *unit) + { + priv->unit = unit; + g_object_notify(G_OBJECT(ruler), "unit"); + + priv->backing_store_valid = FALSE; + gtk_widget_queue_draw (GTK_WIDGET (ruler)); + } +} + +/** + * sp_ruler_get_unit: + * @ruler: a #SPRuler + * + * Return value: the unit currently used in the @ruler widget. + **/ +Inkscape::Util::Unit const* +sp_ruler_get_unit (SPRuler *ruler) +{ + return SP_RULER_GET_PRIVATE (ruler)->unit; +} + +/** + * sp_ruler_set_position: + * @ruler: a #SPRuler + * @position: the position to set the ruler to + * + * This sets the position of the ruler. + */ +void +sp_ruler_set_position (SPRuler *ruler, + gdouble position) +{ + SPRulerPrivate *priv; + + g_return_if_fail (SP_IS_RULER (ruler)); + + priv = SP_RULER_GET_PRIVATE (ruler); + + if (priv->position != position) + { + GdkRectangle rect; + gint xdiff, ydiff; + + priv->position = position; + g_object_notify (G_OBJECT (ruler), "position"); + + rect = sp_ruler_get_pos_rect (ruler, priv->position); + + xdiff = rect.x - priv->last_pos_rect.x; + ydiff = rect.y - priv->last_pos_rect.y; + + /* + * If the position has changed far enough, queue a redraw immediately. + * Otherwise, we only queue a redraw in a low priority idle handler, to + * allow for other things (like updating the canvas) to run. + * + * TODO: This might not be necessary any more in GTK3 with the frame + * clock. Investigate this more after the port to GTK3. + */ + if (priv->last_pos_rect.width != 0 && + priv->last_pos_rect.height != 0 && + (ABS (xdiff) > IMMEDIATE_REDRAW_THRESHOLD || + ABS (ydiff) > IMMEDIATE_REDRAW_THRESHOLD)) + { + sp_ruler_queue_pos_redraw (ruler); + } + else if (! priv->pos_redraw_idle_id) + { + priv->pos_redraw_idle_id = + g_idle_add_full (G_PRIORITY_LOW, + sp_ruler_idle_queue_pos_redraw, + ruler, NULL); + } + } +} + +/** + * sp_ruler_get_position: + * @ruler: a #SPRuler + * + * Return value: the current position of the @ruler widget. + */ +gdouble +sp_ruler_get_position (SPRuler *ruler) +{ + g_return_val_if_fail (SP_IS_RULER (ruler), 0.0); + + return SP_RULER_GET_PRIVATE (ruler)->position; +} + +static gboolean +sp_ruler_motion_notify (GtkWidget *widget, + GdkEventMotion *event) +{ + SPRuler *ruler = SP_RULER(widget); + + sp_ruler_update_position (ruler, event->x, event->y); + + return FALSE; +} + +static void +sp_ruler_draw_ticks (SPRuler *ruler) +{ + GtkWidget *widget = GTK_WIDGET (ruler); + + GtkStyleContext *context = gtk_widget_get_style_context (widget); + GtkBorder border; + GdkRGBA color; + + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + GtkAllocation allocation; + cairo_t *cr; + gint i; + gint width, height; + gint length, ideal_length; + gdouble lower, upper; /* Upper and lower limits, in ruler units */ + gdouble increment; /* Number of pixels per unit */ + guint scale; /* Number of units per major unit */ + gdouble start, end, cur; + gchar unit_str[32]; + gint digit_height; + gint digit_offset; + gchar digit_str[2] = { '\0', '\0' }; + gint text_size; + gint pos; + gdouble max_size; + Inkscape::Util::Unit const *unit = NULL; + SPRulerMetric ruler_metric = ruler_metric_general; /* The metric to use for this unit system */ + PangoLayout *layout; + PangoRectangle logical_rect, ink_rect; + + if (! gtk_widget_is_drawable (widget)) + return; + + gtk_widget_get_allocation (widget, &allocation); + + gtk_style_context_get_border (context, static_cast(0), &border); + + layout = sp_ruler_get_layout (widget, "0123456789"); + pango_layout_get_extents (layout, &ink_rect, &logical_rect); + + digit_height = PANGO_PIXELS (ink_rect.height) + 2; + digit_offset = ink_rect.y; + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + width = allocation.width; + height = allocation.height - (border.top + border.bottom); + } + else + { + width = allocation.height; + height = allocation.width - (border.top + border.bottom); + } + + cr = cairo_create (priv->backing_store); + + cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); + cairo_paint (cr); + cairo_set_operator (cr, CAIRO_OPERATOR_OVER); + + gtk_style_context_get_color (context, gtk_widget_get_state_flags (widget), + &color); + gdk_cairo_set_source_rgba (cr, &color); + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + cairo_rectangle (cr, + border.left, + height + border.top, + allocation.width - (border.left + border.right), + 1); + } + else + { + cairo_rectangle (cr, + height + border.left, + border.top, + 1, + allocation.height - (border.top + border.bottom)); + } + + sp_ruler_get_range (ruler, &lower, &upper, &max_size); + + if ((upper - lower) == 0) + goto out; + + increment = (gdouble) width / (upper - lower); + + /* determine the scale + * Use the maximum extents of the ruler to determine the largest + * possible number to be displayed. Calculate the height in pixels + * of this displayed text. Use this height to find a scale which + * leaves sufficient room for drawing the ruler. + * + * We calculate the text size as for the vruler instead of + * actually measuring the text width, so that the result for the + * scale looks consistent with an accompanying vruler + */ + scale = ceil (priv->max_size); + sprintf (unit_str, "%d", scale); + text_size = strlen (unit_str) * digit_height + 1; + + /* Inkscape change to ruler: Use a 1,2,4,8... scale for inches + * or a 1,2,5,10... scale for everything else */ + if (*sp_ruler_get_unit (ruler) == *unit_table.getUnit("in")) + ruler_metric = ruler_metric_inches; + + for (scale = 0; scale < G_N_ELEMENTS (ruler_metric.ruler_scale); scale++) + if (ruler_metric.ruler_scale[scale] * fabs (increment) > 2 * text_size) + break; + + if (scale == G_N_ELEMENTS (ruler_metric.ruler_scale)) + scale = G_N_ELEMENTS (ruler_metric.ruler_scale) - 1; + + unit = sp_ruler_get_unit (ruler); + + /* drawing starts here */ + length = 0; + for (i = G_N_ELEMENTS (ruler_metric.subdivide) - 1; i >= 0; i--) + { + gdouble subd_incr; + + /* hack to get proper subdivisions at full pixels */ + if (*unit == *unit_table.getUnit("px") && scale == 1 && i == 1) + subd_incr = 1.0; + else + subd_incr = ((gdouble) ruler_metric.ruler_scale[scale] / + (gdouble) ruler_metric.subdivide[i]); + + if (subd_incr * fabs (increment) <= MINIMUM_INCR) + continue; + + /* Calculate the length of the tickmarks. Make sure that + * this length increases for each set of ticks + */ + ideal_length = height / (i + 1) - 1; + if (ideal_length > ++length) + length = ideal_length; + + if (lower < upper) + { + start = floor (lower / subd_incr) * subd_incr; + end = ceil (upper / subd_incr) * subd_incr; + } + else + { + start = floor (upper / subd_incr) * subd_incr; + end = ceil (lower / subd_incr) * subd_incr; + } + + gint tick_index = 0; + + for (cur = start; cur <= end; cur += subd_incr) + { + // due to the typical values for cur, lower and increment, pos will often end up to + // be e.g. 641.50000000000; rounding behaviour is not defined in such a case (see round.h) + // and jitter will be apparent (upon redrawing some of the lines on the ruler might jump a + // by a pixel, and jump back on the next redraw). This is suppressed by adding 1e-9 (that's only one nanopixel ;-)) + pos = gint(Inkscape::round((cur - lower) * increment + 1e-12)); + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + cairo_rectangle (cr, + pos, height + border.top - length, + 1, length); + } + else + { + cairo_rectangle (cr, + height + border.left - length, pos, + length, 1); + } + + /* draw label */ + double label_spacing_px = fabs(increment*(double)ruler_metric.ruler_scale[scale]/ruler_metric.subdivide[i]); + if (i == 0 && + (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) && + (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0)) + { + if (std::abs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur)) + sprintf (unit_str, "%dk", ((int) cur)/1000); + else + sprintf (unit_str, "%d", (int) cur); + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + pango_layout_set_text (layout, unit_str, -1); + pango_layout_get_extents (layout, &logical_rect, NULL); + + cairo_move_to (cr, + pos + 2, + border.top + PANGO_PIXELS (logical_rect.y - digit_offset)); + + pango_cairo_show_layout(cr, layout); + } + else + { + gint j; + + for (j = 0; j < (int) strlen (unit_str); j++) + { + digit_str[0] = unit_str[j]; + pango_layout_set_text (layout, digit_str, 1); + pango_layout_get_extents (layout, NULL, &logical_rect); + + cairo_move_to (cr, + border.left + 1, + pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset)); + pango_cairo_show_layout (cr, layout); + } + } + } + + ++tick_index; + } + } + + cairo_fill (cr); + + priv->backing_store_valid = TRUE; + +out: + cairo_destroy (cr); +} + +static GdkRectangle +sp_ruler_get_pos_rect (SPRuler *ruler, + gdouble position) +{ + GtkWidget *widget = GTK_WIDGET (ruler); + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + GtkAllocation allocation; + gint width, height; + gint xthickness; + gint ythickness; + gdouble upper, lower; + gdouble increment; + GdkRectangle rect = { 0, 0, 0, 0 }; + + if (! gtk_widget_is_drawable (widget)) + return rect; + + gtk_widget_get_allocation (widget, &allocation); + + GtkStyleContext *context = gtk_widget_get_style_context (widget); + GtkBorder padding; + + gtk_style_context_get_border(context, static_cast(0), &padding); + + xthickness = padding.left + padding.right; + ythickness = padding.top + padding.bottom; + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + width = allocation.width; + height = allocation.height - ythickness * 2; + + rect.width = height / 2 + 2; + rect.width |= 1; /* make sure it's odd */ + rect.height = rect.width / 2 + 1; + } + else + { + width = allocation.width - xthickness * 2; + height = allocation.height; + + rect.height = width / 2 + 2; + rect.height |= 1; /* make sure it's odd */ + rect.width = rect.height / 2 + 1; + } + + sp_ruler_get_range (ruler, &lower, &upper, NULL); + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + increment = (gdouble) width / (upper - lower); + + rect.x = ROUND ((position - lower) * increment) + (xthickness - rect.width) / 2 - 1; + rect.y = (height + rect.height) / 2 + ythickness; + } + else + { + increment = (gdouble) height / (upper - lower); + + rect.x = (width + rect.width) / 2 + xthickness; + rect.y = ROUND ((position - lower) * increment) + (ythickness - rect.height) / 2 - 1; + } + + return rect; +} + +static gboolean +sp_ruler_idle_queue_pos_redraw (gpointer data) +{ + SPRuler *ruler = (SPRuler *)data; + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + + sp_ruler_queue_pos_redraw (ruler); + + gboolean ret = g_source_remove(priv->pos_redraw_idle_id); + priv->pos_redraw_idle_id = 0; + + return ret; +} + +static void +sp_ruler_queue_pos_redraw (SPRuler *ruler) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); + const GdkRectangle rect = sp_ruler_get_pos_rect (ruler, priv->position); + GtkAllocation allocation; + + gtk_widget_get_allocation (GTK_WIDGET(ruler), &allocation); + + gtk_widget_queue_draw_area (GTK_WIDGET(ruler), + rect.x + allocation.x, + rect.y + allocation.y, + rect.width, + rect.height); + + if (priv->last_pos_rect.width != 0 || priv->last_pos_rect.height != 0) + { + gtk_widget_queue_draw_area (GTK_WIDGET(ruler), + priv->last_pos_rect.x + allocation.x, + priv->last_pos_rect.y + allocation.y, + priv->last_pos_rect.width, + priv->last_pos_rect.height); + + priv->last_pos_rect.x = 0; + priv->last_pos_rect.y = 0; + priv->last_pos_rect.width = 0; + priv->last_pos_rect.height = 0; + } +} + +static PangoLayout* +sp_ruler_create_layout (GtkWidget *widget, + const gchar *text) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); + PangoLayout *layout; + PangoAttrList *attrs; + PangoAttribute *attr; + + layout = gtk_widget_create_pango_layout (widget, text); + + attrs = pango_attr_list_new (); + + attr = pango_attr_scale_new (priv->font_scale); + attr->start_index = 0; + attr->end_index = -1; + pango_attr_list_insert (attrs, attr); + + pango_layout_set_attributes (layout, attrs); + pango_attr_list_unref (attrs); + + return layout; +} + +static PangoLayout * +sp_ruler_get_layout (GtkWidget *widget, + const gchar *text) +{ + SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); + + if (priv->layout) + { + pango_layout_set_text (priv->layout, text, -1); + return priv->layout; + } + + priv->layout = sp_ruler_create_layout (widget, text); + + return priv->layout; +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/widgets/gimp/ruler.h b/src/widgets/gimp/ruler.h new file mode 100644 index 000000000..ed529d082 --- /dev/null +++ b/src/widgets/gimp/ruler.h @@ -0,0 +1,86 @@ +#ifndef __SP_RULER_H__ +#define __SP_RULER_H__ + +/* + * Customized ruler class for inkscape + * + * Authors: + * Lauris Kaplinski + * Frank Felfe + * + * Copyright (C) 1999-2002 authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include +#include +#include + +namespace Inkscape { + namespace Util { + class Unit; + } +} + +G_BEGIN_DECLS + +#define SP_TYPE_RULER (sp_ruler_get_type ()) +#define SP_RULER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SP_TYPE_RULER, SPRuler)) +#define SP_RULER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SP_TYPE_RULER, SPRulerClass)) +#define SP_IS_RULER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SP_TYPE_RULER)) +#define SP_IS_RULER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SP_TYPE_RULER)) +#define SP_RULER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SP_TYPE_RULER, SPRulerClass)) + +typedef struct _SPRuler SPRuler; +typedef struct _SPRulerClass SPRulerClass; + +struct _SPRuler +{ + GtkWidget parent_instance; +}; + +struct _SPRulerClass +{ + GtkWidgetClass parent_class; +}; + + +GType sp_ruler_get_type (void) G_GNUC_CONST; + +GtkWidget* sp_ruler_new (GtkOrientation orientation); + +void sp_ruler_add_track_widget (SPRuler *ruler, + GtkWidget *widget); +void sp_ruler_remove_track_widget (SPRuler *ruler, + GtkWidget *widget); + +void sp_ruler_set_unit (SPRuler *ruler, + const Inkscape::Util::Unit *unit); +Inkscape::Util::Unit const * sp_ruler_get_unit (SPRuler *ruler); +void sp_ruler_set_position (SPRuler *ruler, + gdouble set_position); +gdouble sp_ruler_get_position (SPRuler *ruler); +void sp_ruler_set_range (SPRuler *ruler, + gdouble lower, + gdouble upper, + gdouble max_size); +void sp_ruler_get_range (SPRuler *ruler, + gdouble *lower, + gdouble *upper, + gdouble *max_size); + +G_END_DECLS + +#endif /* __SP_RULER_H__ */ + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/widgets/ruler.cpp b/src/widgets/ruler.cpp deleted file mode 100644 index bfb9c9071..000000000 --- a/src/widgets/ruler.cpp +++ /dev/null @@ -1,1425 +0,0 @@ -/* - * Customized ruler class for inkscape. Note that this is a fork of - * the GimpRuler widget from GIMP: libgimpwidgets/gimpruler.c. - * The GIMP code is released under the GPL 3. The GIMP code itself - * is a fork of the now-obsolete GtkRuler widget from GTK+ 2. - * - * Major differences between implementations in Inkscape and GIMP are - * as follows: - * - We use a 1,2,4,8... scale for inches and 1,2,5,10... for everything - * else. GIMP uses 1,2,5,10... for everything. - * - * - We use a default font size of PANGO_SCALE_X_SMALL for labels, - * GIMP uses PANGO_SCALE_SMALL (i.e., a bit larger than ours). - * - * - We abbreviate large numbers in tick-labels (e.g., 10000 -> 10k) - * - * Authors: - * Lauris Kaplinski - * Frank Felfe - * bulia byak - * Diederik van Lierop - * Jon A. Cruz - * Alex Valavanis - * - * Copyright (C) 1999-2011 authors - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - -#include -#include -#include - -#include "ruler.h" -#include "round.h" -#include -#include "util/units.h" - -#define ROUND(x) ((int) ((x) + 0.5)) - -#define GTK_PARAM_READWRITE G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB - -#define DEFAULT_RULER_FONT_SCALE PANGO_SCALE_X_SMALL -#define MINIMUM_INCR 5 -#define IMMEDIATE_REDRAW_THRESHOLD 20 - -using Inkscape::Util::unit_table; - -enum { - PROP_0, - PROP_ORIENTATION, - PROP_UNIT, - PROP_LOWER, - PROP_UPPER, - PROP_POSITION, - PROP_MAX_SIZE -}; - - -/* All distances below are in 1/72nd's of an inch. (According to - * Adobe, that's a point, but points are really 1/72.27 in.) - */ -typedef struct -{ - GtkOrientation orientation; - Inkscape::Util::Unit const *unit; - gdouble lower; - gdouble upper; - gdouble position; - gdouble max_size; - - GdkWindow *input_window; - cairo_surface_t *backing_store; - gboolean backing_store_valid; - GdkRectangle last_pos_rect; - guint pos_redraw_idle_id; - PangoLayout *layout; - gdouble font_scale; - - GList *track_widgets; -} SPRulerPrivate; - -#define SP_RULER_GET_PRIVATE(ruler) \ - G_TYPE_INSTANCE_GET_PRIVATE (ruler, SP_TYPE_RULER, SPRulerPrivate) - - -struct SPRulerMetric -{ - gdouble ruler_scale[16]; - gint subdivide[5]; -}; - -// Ruler metric for general use. -static SPRulerMetric const ruler_metric_general = { - { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000 }, - { 1, 5, 10, 50, 100 } -}; - -// Ruler metric for inch scales. -static SPRulerMetric const ruler_metric_inches = { - { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 }, - { 1, 2, 4, 8, 16 } -}; - -static void sp_ruler_dispose (GObject *object); -static void sp_ruler_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); -static void sp_ruler_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec); - -static void sp_ruler_realize (GtkWidget *widget); -static void sp_ruler_unrealize (GtkWidget *widget); -static void sp_ruler_map (GtkWidget *widget); -static void sp_ruler_unmap (GtkWidget *widget); -static void sp_ruler_size_allocate (GtkWidget *widget, - GtkAllocation *allocation); - -static void sp_ruler_get_preferred_width (GtkWidget *widget, - gint *minimum_width, - gint *natural_width); - -static void sp_ruler_get_preferred_height (GtkWidget *widget, - gint *minimum_height, - gint *natural_height); -static void sp_ruler_style_updated (GtkWidget *widget); - -static gboolean sp_ruler_motion_notify (GtkWidget *widget, - GdkEventMotion *event); -static gboolean sp_ruler_draw (GtkWidget *widget, - cairo_t *cr); -static void sp_ruler_draw_ticks (SPRuler *ruler); -static GdkRectangle sp_ruler_get_pos_rect (SPRuler *ruler, - gdouble position); -static gboolean sp_ruler_idle_queue_pos_redraw(gpointer data); -static void sp_ruler_queue_pos_redraw (SPRuler *ruler); -static void sp_ruler_draw_pos (SPRuler *ruler, - cairo_t *cr); -static void sp_ruler_make_pixmap (SPRuler *ruler); - -static PangoLayout * sp_ruler_get_layout (GtkWidget *widget, - const gchar *text); - - -G_DEFINE_TYPE (SPRuler, sp_ruler, GTK_TYPE_WIDGET) - -#define parent_class sp_ruler_parent_class - - -static void -sp_ruler_class_init (SPRulerClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); - -#if GTK_CHECK_VERSION(3,20,0) - gtk_widget_class_set_css_name (widget_class, "ruler-widget"); -#endif - - object_class->dispose = sp_ruler_dispose; - object_class->set_property = sp_ruler_set_property; - object_class->get_property = sp_ruler_get_property; - - widget_class->realize = sp_ruler_realize; - widget_class->unrealize = sp_ruler_unrealize; - widget_class->map = sp_ruler_map; - widget_class->unmap = sp_ruler_unmap; - widget_class->size_allocate = sp_ruler_size_allocate; - widget_class->get_preferred_width = sp_ruler_get_preferred_width; - widget_class->get_preferred_height = sp_ruler_get_preferred_height; - widget_class->style_updated = sp_ruler_style_updated; - widget_class->draw = sp_ruler_draw; - widget_class->motion_notify_event = sp_ruler_motion_notify; - - g_type_class_add_private (object_class, sizeof (SPRulerPrivate)); - - g_object_class_install_property (object_class, - PROP_ORIENTATION, - g_param_spec_enum ("orientation", - _("Orientation"), - _("The orientation of the ruler"), - GTK_TYPE_ORIENTATION, - GTK_ORIENTATION_HORIZONTAL, - static_cast(GTK_PARAM_READWRITE))); - - /* FIXME: Should probably use g_param_spec_enum */ - g_object_class_install_property (object_class, - PROP_UNIT, - g_param_spec_string ("unit", - _("Unit"), - _("Unit of the ruler"), - "px", - static_cast(GTK_PARAM_READWRITE))); - - g_object_class_install_property (object_class, - PROP_LOWER, - g_param_spec_double ("lower", - _("Lower"), - _("Lower limit of ruler"), - -G_MAXDOUBLE, - G_MAXDOUBLE, - 0.0, - static_cast(GTK_PARAM_READWRITE))); - - g_object_class_install_property (object_class, - PROP_UPPER, - g_param_spec_double ("upper", - _("Upper"), - _("Upper limit of ruler"), - -G_MAXDOUBLE, - G_MAXDOUBLE, - 0.0, - static_cast(GTK_PARAM_READWRITE))); - - g_object_class_install_property (object_class, - PROP_POSITION, - g_param_spec_double ("position", - _("Position"), - _("Position of mark on the ruler"), - -G_MAXDOUBLE, - G_MAXDOUBLE, - 0.0, - static_cast(GTK_PARAM_READWRITE))); - - g_object_class_install_property (object_class, - PROP_MAX_SIZE, - g_param_spec_double ("max-size", - _("Max Size"), - _("Maximum size of the ruler"), - -G_MAXDOUBLE, - G_MAXDOUBLE, - 0.0, - static_cast(GTK_PARAM_READWRITE))); - - gtk_widget_class_install_style_property (widget_class, - g_param_spec_double ("font-scale", - NULL, NULL, - 0.0, - G_MAXDOUBLE, - DEFAULT_RULER_FONT_SCALE, - G_PARAM_READABLE)); -} - -static void -sp_ruler_init (SPRuler *ruler) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - - gtk_widget_set_has_window (GTK_WIDGET (ruler), FALSE); - - priv->orientation = GTK_ORIENTATION_HORIZONTAL; - priv->unit = unit_table.getUnit("px"); - priv->lower = 0; - priv->upper = 0; - priv->position = 0; - priv->max_size = 0; - - priv->backing_store = NULL; - priv->backing_store_valid = FALSE; - - priv->last_pos_rect.x = 0; - priv->last_pos_rect.y = 0; - priv->last_pos_rect.width = 0; - priv->last_pos_rect.height = 0; - priv->pos_redraw_idle_id = 0; - - priv->font_scale = DEFAULT_RULER_FONT_SCALE; -} - -static void -sp_ruler_dispose (GObject *object) -{ - SPRuler *ruler = SP_RULER (object); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - - while (priv->track_widgets) - sp_ruler_remove_track_widget (ruler, GTK_WIDGET(priv->track_widgets->data)); - - if (priv->pos_redraw_idle_id) - { - g_source_remove (priv->pos_redraw_idle_id); - priv->pos_redraw_idle_id = 0; - } - - G_OBJECT_CLASS (parent_class)->dispose (object); -} - - -/** - * sp_ruler_set_range: - * @ruler: the SPRuler - * @lower: the lower limit of the ruler - * @upper: the upper limit of the ruler - * @max_size: the maximum size of the ruler used when calculating the space to - * leave for the text - * - * This sets the range of the ruler. - */ -void -sp_ruler_set_range (SPRuler *ruler, - gdouble lower, - gdouble upper, - gdouble max_size) -{ - SPRulerPrivate *priv; - - g_return_if_fail (SP_IS_RULER (ruler)); - - priv = SP_RULER_GET_PRIVATE (ruler); - - g_object_freeze_notify (G_OBJECT (ruler)); - if (priv->lower != lower) - { - priv->lower = lower; - g_object_notify (G_OBJECT (ruler), "lower"); - } - if (priv->upper != upper) - { - priv->upper = upper; - g_object_notify (G_OBJECT (ruler), "upper"); - } - if (priv->max_size != max_size) - { - priv->max_size = max_size; - g_object_notify (G_OBJECT (ruler), "max-size"); - } - g_object_thaw_notify (G_OBJECT (ruler)); - - priv->backing_store_valid = FALSE; - gtk_widget_queue_draw (GTK_WIDGET (ruler)); -} - -/** - * sp_ruler_get_range: - * @ruler: a #SPRuler - * @lower: (allow-none): location to store lower limit of the ruler, or %NULL - * @upper: (allow-none): location to store upper limit of the ruler, or %NULL - * @max_size: location to store the maximum size of the ruler used when calculating - * the space to leave for the text, or %NULL. - * - * Retrieves values indicating the range and current position of a #SPRuler. - * See sp_ruler_set_range(). - **/ -void -sp_ruler_get_range (SPRuler *ruler, - gdouble *lower, - gdouble *upper, - gdouble *max_size) -{ - SPRulerPrivate *priv; - - g_return_if_fail (SP_IS_RULER (ruler)); - - priv = SP_RULER_GET_PRIVATE (ruler); - - if (lower) - *lower = priv->lower; - if (upper) - *upper = priv->upper; - if (max_size) - *max_size = priv->max_size; -} - -static void -sp_ruler_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - SPRuler *ruler = SP_RULER (object); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - - switch (prop_id) - { - case PROP_ORIENTATION: - priv->orientation = static_cast(g_value_get_enum (value)); - gtk_widget_queue_resize (GTK_WIDGET (ruler)); - break; - - case PROP_UNIT: - sp_ruler_set_unit (ruler, unit_table.getUnit(g_value_get_string (value))); - break; - - case PROP_LOWER: - sp_ruler_set_range (ruler, - g_value_get_double (value), - priv->upper, - priv->max_size); - break; - case PROP_UPPER: - sp_ruler_set_range (ruler, - priv->lower, - g_value_get_double (value), - priv->max_size); - break; - - case PROP_POSITION: - sp_ruler_set_position (ruler, g_value_get_double (value)); - break; - - case PROP_MAX_SIZE: - sp_ruler_set_range (ruler, - priv->lower, - priv->upper, - g_value_get_double (value)); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -sp_ruler_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - SPRuler *ruler = SP_RULER (object); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - - switch (prop_id) - { - case PROP_ORIENTATION: - g_value_set_enum (value, priv->orientation); - break; - - case PROP_UNIT: - g_value_set_string (value, priv->unit->abbr.c_str()); - break; - case PROP_LOWER: - g_value_set_double (value, priv->lower); - break; - case PROP_UPPER: - g_value_set_double (value, priv->upper); - break; - case PROP_POSITION: - g_value_set_double (value, priv->position); - break; - case PROP_MAX_SIZE: - g_value_set_double (value, priv->max_size); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -sp_ruler_realize (GtkWidget *widget) -{ - SPRuler *ruler = SP_RULER (widget); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - GtkAllocation allocation; - GdkWindowAttr attributes; - gint attributes_mask; - - GTK_WIDGET_CLASS (sp_ruler_parent_class)->realize (widget); - - gtk_widget_get_allocation (widget, &allocation); - - attributes.window_type = GDK_WINDOW_CHILD; - attributes.x = allocation.x; - attributes.y = allocation.y; - attributes.width = allocation.width; - attributes.height = allocation.height; - attributes.wclass = GDK_INPUT_ONLY; - 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; - - priv->input_window = gdk_window_new (gtk_widget_get_parent_window (widget), - &attributes, attributes_mask); - gdk_window_set_user_data (priv->input_window, ruler); - - sp_ruler_make_pixmap (ruler); -} - -static void -sp_ruler_unrealize(GtkWidget *widget) -{ - SPRuler *ruler = SP_RULER (widget); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - - if (priv->backing_store) - { - cairo_surface_destroy (priv->backing_store); - priv->backing_store = NULL; - } - - priv->backing_store_valid = FALSE; - - if (priv->layout) - { - g_object_unref (priv->layout); - priv->layout = NULL; - } - - if (priv->input_window) - { - gdk_window_destroy (priv->input_window); - priv->input_window = NULL; - } - - GTK_WIDGET_CLASS (sp_ruler_parent_class)->unrealize (widget); -} - -static void -sp_ruler_map (GtkWidget *widget) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); - - GTK_WIDGET_CLASS (sp_ruler_parent_class)->map (widget); - - if (priv->input_window) - gdk_window_show (priv->input_window); -} - -static void -sp_ruler_unmap (GtkWidget *widget) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); - - if (priv->input_window) - gdk_window_hide (priv->input_window); - - GTK_WIDGET_CLASS (sp_ruler_parent_class)->unmap (widget); -} - -static void -sp_ruler_size_allocate (GtkWidget *widget, - GtkAllocation *allocation) -{ - SPRuler *ruler = SP_RULER(widget); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - 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); - - if (gtk_widget_get_realized (widget)) - { - gdk_window_move_resize (priv->input_window, - allocation->x, allocation->y, - allocation->width, allocation->height); - - if (resized) - sp_ruler_make_pixmap (ruler); - } -} - -static void -sp_ruler_size_request (GtkWidget *widget, - GtkRequisition *requisition) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); - PangoLayout *layout; - PangoRectangle ink_rect; - gint size; - - layout = sp_ruler_get_layout (widget, "0123456789"); - pango_layout_get_pixel_extents (layout, &ink_rect, NULL); - - size = 2 + ink_rect.height * 1.7; - - GtkStyleContext *context = gtk_widget_get_style_context (widget); - GtkBorder border; - - gtk_style_context_get_border (context, static_cast(0), &border); - - requisition->width = border.left + border.right; - requisition->height = border.top + border.bottom; - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - requisition->width += 1; - requisition->height += size; - } - else - { - requisition->width += size; - requisition->height += 1; - } -} - -static void -sp_ruler_style_updated (GtkWidget *widget) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); - - GTK_WIDGET_CLASS (sp_ruler_parent_class)->style_updated (widget); - - gtk_widget_style_get (widget, - "font-scale", &priv->font_scale, - NULL); - - if (priv->layout) - { - g_object_unref (priv->layout); - priv->layout = NULL; - } -} - -static void -sp_ruler_get_preferred_width (GtkWidget *widget, - gint *minimum_width, - gint *natural_width) -{ - GtkRequisition requisition; - - sp_ruler_size_request (widget, &requisition); - - *minimum_width = *natural_width = requisition.width; -} - -static void -sp_ruler_get_preferred_height (GtkWidget *widget, - gint *minimum_height, - gint *natural_height) -{ - GtkRequisition requisition; - - sp_ruler_size_request(widget, &requisition); - - *minimum_height = *natural_height = requisition.height; -} - -static gboolean -sp_ruler_draw (GtkWidget *widget, - cairo_t *cr) -{ - SPRuler *ruler = SP_RULER (widget); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - GtkStyleContext *context = gtk_widget_get_style_context (widget); - GtkAllocation allocation; - - gtk_widget_get_allocation (widget, &allocation); - gtk_render_background (context, cr, 0, 0, allocation.width, allocation.height); - gtk_render_frame (context, cr, 0, 0, allocation.width, allocation.height); - - sp_ruler_draw_ticks (ruler); - - cairo_set_source_surface(cr, priv->backing_store, 0, 0); - cairo_paint(cr); - - sp_ruler_draw_pos (ruler, cr); - - return FALSE; -} - -static void -sp_ruler_make_pixmap (SPRuler *ruler) -{ - GtkWidget *widget = GTK_WIDGET (ruler); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - GtkAllocation allocation; - - gtk_widget_get_allocation(widget, &allocation); - - if (priv->backing_store) - cairo_surface_destroy (priv->backing_store); - - priv->backing_store = - gdk_window_create_similar_surface (gtk_widget_get_window (widget), - CAIRO_CONTENT_COLOR_ALPHA, - allocation.width, - allocation.height); - - priv->backing_store_valid = FALSE; -} - -static void -sp_ruler_draw_pos (SPRuler *ruler, - cairo_t *cr) -{ - GtkWidget *widget = GTK_WIDGET (ruler); - - GtkStyleContext *context = gtk_widget_get_style_context (widget); - GdkRGBA color; - - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - GdkRectangle pos_rect; - - if (! gtk_widget_is_drawable (widget)) - return; - - pos_rect = sp_ruler_get_pos_rect (ruler, sp_ruler_get_position (ruler)); - - if ((pos_rect.width > 0) && (pos_rect.height > 0)) - { - gtk_style_context_get_color (context, gtk_widget_get_state_flags (widget), - &color); - gdk_cairo_set_source_rgba (cr, &color); - - cairo_move_to (cr, pos_rect.x, pos_rect.y); - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - cairo_line_to (cr, pos_rect.x + pos_rect.width / 2.0, - pos_rect.y + pos_rect.height); - cairo_line_to (cr, pos_rect.x + pos_rect.width, - pos_rect.y); - } - else - { - cairo_line_to (cr, pos_rect.x + pos_rect.width, - - pos_rect.y + pos_rect.height / 2.0); - cairo_line_to (cr, pos_rect.x, - pos_rect.y + pos_rect.height); - } - - cairo_fill (cr); - } - - priv->last_pos_rect = pos_rect; -} - -/** - * sp_ruler_new: - * @orientation: the ruler's orientation - * - * Creates a new ruler. - * - * Return value: a new #SPRuler widget. - */ -GtkWidget * -sp_ruler_new (GtkOrientation orientation) -{ - return GTK_WIDGET (g_object_new (SP_TYPE_RULER, - "orientation", orientation, - NULL)); -} - -static void -sp_ruler_update_position (SPRuler *ruler, - gdouble x, - gdouble y) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - GtkAllocation allocation; - gdouble lower; - gdouble upper; - - gtk_widget_get_allocation (GTK_WIDGET (ruler), &allocation); - sp_ruler_get_range (ruler, &lower, &upper, NULL); - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - sp_ruler_set_position (ruler, - lower + - (upper - lower) * x / allocation.width); - } - else - { - sp_ruler_set_position (ruler, - lower + - (upper - lower) * y / allocation.height); - } -} - -/* 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_effective_parent (w)) - { - gdouble px, py; - - gdk_window_coords_to_parent (w, *x, *y, &px, &py); - - *x += px; - *y += px; - } - - if (w == NULL) - { - *x = 0; - *y = 0; - return FALSE; - } - - return TRUE; -} - -static void -sp_ruler_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 -sp_ruler_track_widget_motion_notify (GtkWidget *widget, - GdkEventMotion *mevent, - SPRuler *ruler) -{ - gint widget_x; - gint widget_y; - gint ruler_x; - gint ruler_y; - - widget = gtk_get_event_widget (reinterpret_cast(mevent)); - - sp_ruler_event_to_widget_coords (widget, mevent->window, - mevent->x, mevent->y, - &widget_x, &widget_y); - - if (gtk_widget_translate_coordinates (widget, GTK_WIDGET (ruler), - widget_x, widget_y, - &ruler_x, &ruler_y)) - { - sp_ruler_update_position (ruler, ruler_x, ruler_y); - } - - return FALSE; -} - -void -sp_ruler_add_track_widget (SPRuler *ruler, - GtkWidget *widget) -{ - SPRulerPrivate *priv; - - g_return_if_fail (SP_IS_RULER (ruler)); - g_return_if_fail (GTK_IS_WIDGET (ruler)); - - priv = SP_RULER_GET_PRIVATE (ruler); - - g_return_if_fail (g_list_find (priv->track_widgets, widget) == NULL); - - priv->track_widgets = g_list_prepend (priv->track_widgets, widget); - - g_signal_connect (widget, "motion-notify-event", - G_CALLBACK (sp_ruler_track_widget_motion_notify), - ruler); - g_signal_connect (widget, "destroy", - G_CALLBACK (sp_ruler_remove_track_widget), - ruler); -} - -/** - * sp_ruler_remove_track_widget: - * @ruler: an #SPRuler - * @widget: the track widget to remove - * - * Removes a previously added track widget from the ruler. See - * sp_ruler_add_track_widget(). - */ -void -sp_ruler_remove_track_widget (SPRuler *ruler, - GtkWidget *widget) -{ - SPRulerPrivate *priv; - - g_return_if_fail (SP_IS_RULER (ruler)); - g_return_if_fail (GTK_IS_WIDGET (ruler)); - - priv = SP_RULER_GET_PRIVATE (ruler); - - g_return_if_fail (g_list_find (priv->track_widgets, widget) != NULL); - - priv->track_widgets = g_list_remove (priv->track_widgets, widget); - - g_signal_handlers_disconnect_by_func (widget, - (gpointer) G_CALLBACK (sp_ruler_track_widget_motion_notify), - ruler); - g_signal_handlers_disconnect_by_func (widget, - (gpointer) G_CALLBACK (sp_ruler_remove_track_widget), - ruler); -} - -/** - * sp_ruler_set_unit: - * @ruler: a #SPRuler - * @unit: the #SPMetric to set the ruler to - * - * This sets the unit of the ruler. - */ -void -sp_ruler_set_unit (SPRuler *ruler, - Inkscape::Util::Unit const *unit) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - - g_return_if_fail (SP_IS_RULER (ruler)); - - if (*priv->unit != *unit) - { - priv->unit = unit; - g_object_notify(G_OBJECT(ruler), "unit"); - - priv->backing_store_valid = FALSE; - gtk_widget_queue_draw (GTK_WIDGET (ruler)); - } -} - -/** - * sp_ruler_get_unit: - * @ruler: a #SPRuler - * - * Return value: the unit currently used in the @ruler widget. - **/ -Inkscape::Util::Unit const* -sp_ruler_get_unit (SPRuler *ruler) -{ - return SP_RULER_GET_PRIVATE (ruler)->unit; -} - -/** - * sp_ruler_set_position: - * @ruler: a #SPRuler - * @position: the position to set the ruler to - * - * This sets the position of the ruler. - */ -void -sp_ruler_set_position (SPRuler *ruler, - gdouble position) -{ - SPRulerPrivate *priv; - - g_return_if_fail (SP_IS_RULER (ruler)); - - priv = SP_RULER_GET_PRIVATE (ruler); - - if (priv->position != position) - { - GdkRectangle rect; - gint xdiff, ydiff; - - priv->position = position; - g_object_notify (G_OBJECT (ruler), "position"); - - rect = sp_ruler_get_pos_rect (ruler, priv->position); - - xdiff = rect.x - priv->last_pos_rect.x; - ydiff = rect.y - priv->last_pos_rect.y; - - /* - * If the position has changed far enough, queue a redraw immediately. - * Otherwise, we only queue a redraw in a low priority idle handler, to - * allow for other things (like updating the canvas) to run. - * - * TODO: This might not be necessary any more in GTK3 with the frame - * clock. Investigate this more after the port to GTK3. - */ - if (priv->last_pos_rect.width != 0 && - priv->last_pos_rect.height != 0 && - (ABS (xdiff) > IMMEDIATE_REDRAW_THRESHOLD || - ABS (ydiff) > IMMEDIATE_REDRAW_THRESHOLD)) - { - sp_ruler_queue_pos_redraw (ruler); - } - else if (! priv->pos_redraw_idle_id) - { - priv->pos_redraw_idle_id = - g_idle_add_full (G_PRIORITY_LOW, - sp_ruler_idle_queue_pos_redraw, - ruler, NULL); - } - } -} - -/** - * sp_ruler_get_position: - * @ruler: a #SPRuler - * - * Return value: the current position of the @ruler widget. - */ -gdouble -sp_ruler_get_position (SPRuler *ruler) -{ - g_return_val_if_fail (SP_IS_RULER (ruler), 0.0); - - return SP_RULER_GET_PRIVATE (ruler)->position; -} - -static gboolean -sp_ruler_motion_notify (GtkWidget *widget, - GdkEventMotion *event) -{ - SPRuler *ruler = SP_RULER(widget); - - sp_ruler_update_position (ruler, event->x, event->y); - - return FALSE; -} - -static void -sp_ruler_draw_ticks (SPRuler *ruler) -{ - GtkWidget *widget = GTK_WIDGET (ruler); - - GtkStyleContext *context = gtk_widget_get_style_context (widget); - GtkBorder border; - GdkRGBA color; - - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - GtkAllocation allocation; - cairo_t *cr; - gint i; - gint width, height; - gint length, ideal_length; - gdouble lower, upper; /* Upper and lower limits, in ruler units */ - gdouble increment; /* Number of pixels per unit */ - guint scale; /* Number of units per major unit */ - gdouble start, end, cur; - gchar unit_str[32]; - gint digit_height; - gint digit_offset; - gchar digit_str[2] = { '\0', '\0' }; - gint text_size; - gint pos; - gdouble max_size; - Inkscape::Util::Unit const *unit = NULL; - SPRulerMetric ruler_metric = ruler_metric_general; /* The metric to use for this unit system */ - PangoLayout *layout; - PangoRectangle logical_rect, ink_rect; - - if (! gtk_widget_is_drawable (widget)) - return; - - gtk_widget_get_allocation (widget, &allocation); - - gtk_style_context_get_border (context, static_cast(0), &border); - - layout = sp_ruler_get_layout (widget, "0123456789"); - pango_layout_get_extents (layout, &ink_rect, &logical_rect); - - digit_height = PANGO_PIXELS (ink_rect.height) + 2; - digit_offset = ink_rect.y; - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - width = allocation.width; - height = allocation.height - (border.top + border.bottom); - } - else - { - width = allocation.height; - height = allocation.width - (border.top + border.bottom); - } - - cr = cairo_create (priv->backing_store); - - cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); - cairo_paint (cr); - cairo_set_operator (cr, CAIRO_OPERATOR_OVER); - - gtk_style_context_get_color (context, gtk_widget_get_state_flags (widget), - &color); - gdk_cairo_set_source_rgba (cr, &color); - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - cairo_rectangle (cr, - border.left, - height + border.top, - allocation.width - (border.left + border.right), - 1); - } - else - { - cairo_rectangle (cr, - height + border.left, - border.top, - 1, - allocation.height - (border.top + border.bottom)); - } - - sp_ruler_get_range (ruler, &lower, &upper, &max_size); - - if ((upper - lower) == 0) - goto out; - - increment = (gdouble) width / (upper - lower); - - /* determine the scale - * Use the maximum extents of the ruler to determine the largest - * possible number to be displayed. Calculate the height in pixels - * of this displayed text. Use this height to find a scale which - * leaves sufficient room for drawing the ruler. - * - * We calculate the text size as for the vruler instead of - * actually measuring the text width, so that the result for the - * scale looks consistent with an accompanying vruler - */ - scale = ceil (priv->max_size); - sprintf (unit_str, "%d", scale); - text_size = strlen (unit_str) * digit_height + 1; - - /* Inkscape change to ruler: Use a 1,2,4,8... scale for inches - * or a 1,2,5,10... scale for everything else */ - if (*sp_ruler_get_unit (ruler) == *unit_table.getUnit("in")) - ruler_metric = ruler_metric_inches; - - for (scale = 0; scale < G_N_ELEMENTS (ruler_metric.ruler_scale); scale++) - if (ruler_metric.ruler_scale[scale] * fabs (increment) > 2 * text_size) - break; - - if (scale == G_N_ELEMENTS (ruler_metric.ruler_scale)) - scale = G_N_ELEMENTS (ruler_metric.ruler_scale) - 1; - - unit = sp_ruler_get_unit (ruler); - - /* drawing starts here */ - length = 0; - for (i = G_N_ELEMENTS (ruler_metric.subdivide) - 1; i >= 0; i--) - { - gdouble subd_incr; - - /* hack to get proper subdivisions at full pixels */ - if (*unit == *unit_table.getUnit("px") && scale == 1 && i == 1) - subd_incr = 1.0; - else - subd_incr = ((gdouble) ruler_metric.ruler_scale[scale] / - (gdouble) ruler_metric.subdivide[i]); - - if (subd_incr * fabs (increment) <= MINIMUM_INCR) - continue; - - /* Calculate the length of the tickmarks. Make sure that - * this length increases for each set of ticks - */ - ideal_length = height / (i + 1) - 1; - if (ideal_length > ++length) - length = ideal_length; - - if (lower < upper) - { - start = floor (lower / subd_incr) * subd_incr; - end = ceil (upper / subd_incr) * subd_incr; - } - else - { - start = floor (upper / subd_incr) * subd_incr; - end = ceil (lower / subd_incr) * subd_incr; - } - - gint tick_index = 0; - - for (cur = start; cur <= end; cur += subd_incr) - { - // due to the typical values for cur, lower and increment, pos will often end up to - // be e.g. 641.50000000000; rounding behaviour is not defined in such a case (see round.h) - // and jitter will be apparent (upon redrawing some of the lines on the ruler might jump a - // by a pixel, and jump back on the next redraw). This is suppressed by adding 1e-9 (that's only one nanopixel ;-)) - pos = gint(Inkscape::round((cur - lower) * increment + 1e-12)); - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - cairo_rectangle (cr, - pos, height + border.top - length, - 1, length); - } - else - { - cairo_rectangle (cr, - height + border.left - length, pos, - length, 1); - } - - /* draw label */ - double label_spacing_px = fabs(increment*(double)ruler_metric.ruler_scale[scale]/ruler_metric.subdivide[i]); - if (i == 0 && - (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) && - (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0)) - { - if (std::abs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur)) - sprintf (unit_str, "%dk", ((int) cur)/1000); - else - sprintf (unit_str, "%d", (int) cur); - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - pango_layout_set_text (layout, unit_str, -1); - pango_layout_get_extents (layout, &logical_rect, NULL); - - cairo_move_to (cr, - pos + 2, - border.top + PANGO_PIXELS (logical_rect.y - digit_offset)); - - pango_cairo_show_layout(cr, layout); - } - else - { - gint j; - - for (j = 0; j < (int) strlen (unit_str); j++) - { - digit_str[0] = unit_str[j]; - pango_layout_set_text (layout, digit_str, 1); - pango_layout_get_extents (layout, NULL, &logical_rect); - - cairo_move_to (cr, - border.left + 1, - pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset)); - pango_cairo_show_layout (cr, layout); - } - } - } - - ++tick_index; - } - } - - cairo_fill (cr); - - priv->backing_store_valid = TRUE; - -out: - cairo_destroy (cr); -} - -static GdkRectangle -sp_ruler_get_pos_rect (SPRuler *ruler, - gdouble position) -{ - GtkWidget *widget = GTK_WIDGET (ruler); - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - GtkAllocation allocation; - gint width, height; - gint xthickness; - gint ythickness; - gdouble upper, lower; - gdouble increment; - GdkRectangle rect = { 0, 0, 0, 0 }; - - if (! gtk_widget_is_drawable (widget)) - return rect; - - gtk_widget_get_allocation (widget, &allocation); - - GtkStyleContext *context = gtk_widget_get_style_context (widget); - GtkBorder padding; - - gtk_style_context_get_border(context, static_cast(0), &padding); - - xthickness = padding.left + padding.right; - ythickness = padding.top + padding.bottom; - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - width = allocation.width; - height = allocation.height - ythickness * 2; - - rect.width = height / 2 + 2; - rect.width |= 1; /* make sure it's odd */ - rect.height = rect.width / 2 + 1; - } - else - { - width = allocation.width - xthickness * 2; - height = allocation.height; - - rect.height = width / 2 + 2; - rect.height |= 1; /* make sure it's odd */ - rect.width = rect.height / 2 + 1; - } - - sp_ruler_get_range (ruler, &lower, &upper, NULL); - - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) - { - increment = (gdouble) width / (upper - lower); - - rect.x = ROUND ((position - lower) * increment) + (xthickness - rect.width) / 2 - 1; - rect.y = (height + rect.height) / 2 + ythickness; - } - else - { - increment = (gdouble) height / (upper - lower); - - rect.x = (width + rect.width) / 2 + xthickness; - rect.y = ROUND ((position - lower) * increment) + (ythickness - rect.height) / 2 - 1; - } - - return rect; -} - -static gboolean -sp_ruler_idle_queue_pos_redraw (gpointer data) -{ - SPRuler *ruler = (SPRuler *)data; - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - - sp_ruler_queue_pos_redraw (ruler); - - gboolean ret = g_source_remove(priv->pos_redraw_idle_id); - priv->pos_redraw_idle_id = 0; - - return ret; -} - -static void -sp_ruler_queue_pos_redraw (SPRuler *ruler) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (ruler); - const GdkRectangle rect = sp_ruler_get_pos_rect (ruler, priv->position); - GtkAllocation allocation; - - gtk_widget_get_allocation (GTK_WIDGET(ruler), &allocation); - - gtk_widget_queue_draw_area (GTK_WIDGET(ruler), - rect.x + allocation.x, - rect.y + allocation.y, - rect.width, - rect.height); - - if (priv->last_pos_rect.width != 0 || priv->last_pos_rect.height != 0) - { - gtk_widget_queue_draw_area (GTK_WIDGET(ruler), - priv->last_pos_rect.x + allocation.x, - priv->last_pos_rect.y + allocation.y, - priv->last_pos_rect.width, - priv->last_pos_rect.height); - - priv->last_pos_rect.x = 0; - priv->last_pos_rect.y = 0; - priv->last_pos_rect.width = 0; - priv->last_pos_rect.height = 0; - } -} - -static PangoLayout* -sp_ruler_create_layout (GtkWidget *widget, - const gchar *text) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); - PangoLayout *layout; - PangoAttrList *attrs; - PangoAttribute *attr; - - layout = gtk_widget_create_pango_layout (widget, text); - - attrs = pango_attr_list_new (); - - attr = pango_attr_scale_new (priv->font_scale); - attr->start_index = 0; - attr->end_index = -1; - pango_attr_list_insert (attrs, attr); - - pango_layout_set_attributes (layout, attrs); - pango_attr_list_unref (attrs); - - return layout; -} - -static PangoLayout * -sp_ruler_get_layout (GtkWidget *widget, - const gchar *text) -{ - SPRulerPrivate *priv = SP_RULER_GET_PRIVATE (widget); - - if (priv->layout) - { - pango_layout_set_text (priv->layout, text, -1); - return priv->layout; - } - - priv->layout = sp_ruler_create_layout (widget, text); - - return priv->layout; -} - -/* - Local Variables: - mode:c++ - c-file-style:"stroustrup" - c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) - indent-tabs-mode:nil - fill-column:99 - End: -*/ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/widgets/ruler.h b/src/widgets/ruler.h deleted file mode 100644 index ed529d082..000000000 --- a/src/widgets/ruler.h +++ /dev/null @@ -1,86 +0,0 @@ -#ifndef __SP_RULER_H__ -#define __SP_RULER_H__ - -/* - * Customized ruler class for inkscape - * - * Authors: - * Lauris Kaplinski - * Frank Felfe - * - * Copyright (C) 1999-2002 authors - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - -#include -#include -#include - -namespace Inkscape { - namespace Util { - class Unit; - } -} - -G_BEGIN_DECLS - -#define SP_TYPE_RULER (sp_ruler_get_type ()) -#define SP_RULER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SP_TYPE_RULER, SPRuler)) -#define SP_RULER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SP_TYPE_RULER, SPRulerClass)) -#define SP_IS_RULER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SP_TYPE_RULER)) -#define SP_IS_RULER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SP_TYPE_RULER)) -#define SP_RULER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SP_TYPE_RULER, SPRulerClass)) - -typedef struct _SPRuler SPRuler; -typedef struct _SPRulerClass SPRulerClass; - -struct _SPRuler -{ - GtkWidget parent_instance; -}; - -struct _SPRulerClass -{ - GtkWidgetClass parent_class; -}; - - -GType sp_ruler_get_type (void) G_GNUC_CONST; - -GtkWidget* sp_ruler_new (GtkOrientation orientation); - -void sp_ruler_add_track_widget (SPRuler *ruler, - GtkWidget *widget); -void sp_ruler_remove_track_widget (SPRuler *ruler, - GtkWidget *widget); - -void sp_ruler_set_unit (SPRuler *ruler, - const Inkscape::Util::Unit *unit); -Inkscape::Util::Unit const * sp_ruler_get_unit (SPRuler *ruler); -void sp_ruler_set_position (SPRuler *ruler, - gdouble set_position); -gdouble sp_ruler_get_position (SPRuler *ruler); -void sp_ruler_set_range (SPRuler *ruler, - gdouble lower, - gdouble upper, - gdouble max_size); -void sp_ruler_get_range (SPRuler *ruler, - gdouble *lower, - gdouble *upper, - gdouble *max_size); - -G_END_DECLS - -#endif /* __SP_RULER_H__ */ - -/* - Local Variables: - mode:c++ - c-file-style:"stroustrup" - c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) - indent-tabs-mode:nil - fill-column:99 - End: -*/ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : -- cgit v1.2.3