diff --git a/src/widgets/ruler.cpp b/src/widgets/ruler.cpp
index 5eb752377851cbc8dba1438a281774fdbbfc9c67..c90b55e734e79c917cb057df7da84994e26ce2ae 100644 (file)
--- a/src/widgets/ruler.cpp
+++ b/src/widgets/ruler.cpp
* Lauris Kaplinski <lauris@kaplinski.com>
* Frank Felfe <innerspace@iname.com>
* bulia byak <buliabyak@users.sf.net>
+ * Diederik van Lierop <mail@diedenrezi.nl>
*
- * Copyright (C) 1999-2005 authors
+ * Copyright (C) 1999-2008 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
+#include <cstring>
#include <cmath>
-#include <string.h>
+#include <cstdio>
+
#include "widget-sizes.h"
+#include "desktop-widget.h"
#include "ruler.h"
#include "unit-constants.h"
+#include "round.h"
#define MINIMUM_INCR 5
#define MAXIMUM_SUBDIVIDE 5
#define MAXIMUM_SCALES 10
-
-#define ROUND(x) int (std::floor ((x) + 0.5000000001))
-
+#define UNUSED_PIXELS 2 // There appear to be two pixels that are not being used at each end of the ruler
static void sp_hruler_class_init (SPHRulerClass *klass);
static void sp_hruler_init (SPHRuler *hruler);
-static gint sp_hruler_motion_notify (GtkWidget *widget,
- GdkEventMotion *event);
+static gint sp_hruler_motion_notify (GtkWidget *widget, GdkEventMotion *event);
static void sp_hruler_draw_ticks (GtkRuler *ruler);
static void sp_hruler_draw_pos (GtkRuler *ruler);
+static void sp_hruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
+static GtkWidgetClass *hruler_parent_class;
GtkType
sp_hruler_get_type (void)
{
+ //TODO: switch to GObject
+ // GtkType and such calls were deprecated a while back with the
+ // introduction of GObject as a separate layer, with GType instead. --JonCruz
+
static GtkType hruler_type = 0;
if (!hruler_type)
{
static const GtkTypeInfo hruler_info =
{
- "SPHRuler",
+ (gchar*) "SPHRuler",
sizeof (SPHRuler),
sizeof (SPHRulerClass),
(GtkClassInitFunc) sp_hruler_class_init,
GtkWidgetClass *widget_class;
GtkRulerClass *ruler_class;
+ hruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
+
widget_class = (GtkWidgetClass*) klass;
ruler_class = (GtkRulerClass*) klass;
widget_class->motion_notify_event = sp_hruler_motion_notify;
+ widget_class->size_allocate = sp_hruler_size_allocate;
ruler_class->draw_ticks = sp_hruler_draw_ticks;
ruler_class->draw_pos = sp_hruler_draw_pos;
GdkEventMotion *event)
{
GtkRuler *ruler;
- gint x;
-
+
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (SP_IS_HRULER (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
ruler = GTK_RULER (widget);
-
- x = (int)event->x;
-
- ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
-
+ double x = event->x; //Although event->x is double according to the docs, it only appears to return integers
+ ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (x + UNUSED_PIXELS) / (widget->allocation.width + 2*UNUSED_PIXELS);
+
/* Make sure the ruler has been allocated already */
if (ruler->backing_store != NULL)
gtk_ruler_draw_pos (ruler);
xthickness = widget->style->xthickness;
ythickness = widget->style->ythickness;
- width = widget->allocation.width;
+ width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
height = widget->allocation.height;// - ythickness * 2;
-
+
gtk_paint_box (widget->style, ruler->backing_store,
GTK_STATE_NORMAL, GTK_SHADOW_NONE,
NULL, widget, "hruler",
0, 0,
widget->allocation.width, widget->allocation.height);
- upper = ruler->upper / ruler->metric->pixels_per_unit;
+ upper = ruler->upper / ruler->metric->pixels_per_unit; // upper and lower are expressed in ruler units
lower = ruler->lower / ruler->metric->pixels_per_unit;
-
+ /* "pixels_per_unit" should be "points_per_unit". This is the size of the unit
+ * in 1/72nd's of an inch and has nothing to do with screen pixels */
+
if ((upper - lower) == 0)
return;
- increment = (double) width / (upper - lower);
-
- /* determine the scale
+ increment = (double) (width + 2*UNUSED_PIXELS) / (upper - lower); // screen pixels per ruler unit
+
+ /* determine the scale
* We calculate the text size as for the vruler instead of using
* text_width = gdk_string_width(font, unit_str), so that the result
* for the scale looks consistent with an accompanying vruler
subd_incr = ruler->metric->ruler_scale[scale] /
ruler->metric->subdivide[i];
if (subd_incr * fabs(increment) <= MINIMUM_INCR)
- continue;
+ continue;
/* Calculate the length of the tickmarks. Make sure that
* this length increases for each set of ticks
}
tick_index = 0;
- cur = start;
+ cur = start; // location (in ruler units) of the first invisible tick at the left side of the canvas
while (cur <= end)
{
- pos = ROUND ((cur - lower) * increment);
-
- gdk_draw_line (ruler->backing_store, gc,
+ // 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 = int(Inkscape::round((cur - lower) * increment + 1e-12)) - UNUSED_PIXELS;
+ gdk_draw_line (ruler->backing_store, gc,
pos, height + ythickness,
pos, height - length + ythickness);
* errors in subd_incr.
*/
++tick_index;
- cur = start + (((double)tick_index) * (double)ruler->metric->ruler_scale[scale])/ ruler->metric->subdivide[i];
+ cur = start + tick_index * subd_incr;
}
}
}
gc = widget->style->fg_gc[GTK_STATE_NORMAL];
xthickness = widget->style->xthickness;
ythickness = widget->style->ythickness;
- width = widget->allocation.width;
+ width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
height = widget->allocation.height - ythickness * 2;
bs_width = height / 2;
ruler->xsrc, ruler->ysrc,
bs_width, bs_height);
- increment = (gfloat) width / (ruler->upper - ruler->lower);
+ increment = (gfloat) (width + 2*UNUSED_PIXELS) / (ruler->upper - ruler->lower);
- x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
+ // Calculate the coordinates (x, y, in pixels) of the tip of the triangle
+ x = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(xthickness - bs_width) / 2.0) - UNUSED_PIXELS);
y = (height + bs_height) / 2 + ythickness;
for (i = 0; i < bs_height; i++)
}
}
+/**
+ * The hruler widget's size_allocate callback.
+ */
+static void
+sp_hruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
+{
+ g_assert (widget != NULL);
+ g_assert (SP_IS_HRULER (widget));
+
+ // First call the default gtk_widget_size_allocate() method (which is being overridden here)
+ if (GTK_WIDGET_CLASS (hruler_parent_class)->size_allocate)
+ (* GTK_WIDGET_CLASS (hruler_parent_class)->size_allocate) (widget, allocation);
+
+ // Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
+ // For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
+ GtkWidget *parent = gtk_widget_get_parent(widget);
+ do {
+ if (SP_IS_DESKTOP_WIDGET(parent)) {
+ // Now we've found the desktop widget we can have the ruler boundaries updated
+ sp_desktop_widget_update_hruler(SP_DESKTOP_WIDGET(parent));
+ // If the size of the ruler has increased, then a blank part is uncovered; therefore
+ // it must be redrawn
+ sp_hruler_draw_ticks(GTK_RULER(widget));
+ break;
+ }
+ parent = gtk_widget_get_parent(parent);
+ } while (parent != NULL);
+}
GdkEventMotion *event);
static void sp_vruler_draw_ticks (GtkRuler *ruler);
static void sp_vruler_draw_pos (GtkRuler *ruler);
+static void sp_vruler_size_request (GtkWidget *widget, GtkRequisition *requisition);
+static void sp_vruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
+static GtkWidgetClass *vruler_parent_class;
GtkType
sp_vruler_get_type (void)
{
+ //TODO: switch to GObject
+ // GtkType and such calls were deprecated a while back with the
+ // introduction of GObject as a separate layer, with GType instead. --JonCruz
+
static GtkType vruler_type = 0;
if (!vruler_type)
{
static const GtkTypeInfo vruler_info =
{
- "SPVRuler",
+ (gchar*) "SPVRuler",
sizeof (SPVRuler),
sizeof (SPVRulerClass),
(GtkClassInitFunc) sp_vruler_class_init,
GtkWidgetClass *widget_class;
GtkRulerClass *ruler_class;
+ vruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
+
widget_class = (GtkWidgetClass*) klass;
ruler_class = (GtkRulerClass*) klass;
widget_class->motion_notify_event = sp_vruler_motion_notify;
+ widget_class->size_allocate = sp_vruler_size_allocate;
+ widget_class->size_request = sp_vruler_size_request;
ruler_class->draw_ticks = sp_vruler_draw_ticks;
ruler_class->draw_pos = sp_vruler_draw_pos;
GdkEventMotion *event)
{
GtkRuler *ruler;
- gint y;
-
+
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (SP_IS_VRULER (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
ruler = GTK_RULER (widget);
-
- y = (int)event->y;
-
- ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y) / widget->allocation.height;
+ double y = event->y; //Although event->y is double according to the docs, it only appears to return integers
+ ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (y + UNUSED_PIXELS) / (widget->allocation.height + 2*UNUSED_PIXELS);
/* Make sure the ruler has been allocated already */
if (ruler->backing_store != NULL)
xthickness = widget->style->xthickness;
ythickness = widget->style->ythickness;
- width = widget->allocation.height;
+ width = widget->allocation.height; //in pixels; is apparently 2 pixels shorter than the canvas at each end
height = widget->allocation.width;// - ythickness * 2;
gtk_paint_box (widget->style, ruler->backing_store,
0, 0,
widget->allocation.width, widget->allocation.height);
- upper = ruler->upper / ruler->metric->pixels_per_unit;
+ upper = ruler->upper / ruler->metric->pixels_per_unit; // upper and lower are expressed in ruler units
lower = ruler->lower / ruler->metric->pixels_per_unit;
-
+ /* "pixels_per_unit" should be "points_per_unit". This is the size of the unit
+ * in 1/72nd's of an inch and has nothing to do with screen pixels */
+
if ((upper - lower) == 0)
return;
- increment = (double) width / (upper - lower);
+ increment = (double) (width + 2*UNUSED_PIXELS) / (upper - lower); // screen pixels per ruler unit
/* determine the scale
* use the maximum extents of the ruler to determine the largest
}
tick_index = 0;
- cur = start;
+ cur = start; // location (in ruler units) of the first invisible tick at the top side of the canvas
while (cur < end) {
- pos = ROUND ((cur - lower) * increment);
+ // 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 = int(Inkscape::round((cur - lower) * increment + 1e-12)) - UNUSED_PIXELS;
gdk_draw_line (ruler->backing_store, gc,
height + xthickness - length, pos,
* errors in subd_incr.
*/
++tick_index;
- cur = start + (((double)tick_index) * (double)ruler->metric->ruler_scale[scale])/ ruler->metric->subdivide[i];
+ cur = start + tick_index * subd_incr;
}
}
}
xthickness = widget->style->xthickness;
ythickness = widget->style->ythickness;
width = widget->allocation.width - xthickness * 2;
- height = widget->allocation.height;
+ height = widget->allocation.height; // in pixels; is apparently 2 pixels shorter than the canvas at each end
bs_height = width / 2;
bs_height |= 1; /* make sure it's odd */
ruler->xsrc, ruler->ysrc,
bs_width, bs_height);
- increment = (gfloat) height / (ruler->upper - ruler->lower);
+ increment = (gfloat) (height + 2*UNUSED_PIXELS) / (ruler->upper - ruler->lower);
- x = (width + bs_width) / 2 + xthickness;
- y = ROUND ((ruler->position - ruler->lower) * increment) + (ythickness - bs_height) / 2 - 1;
+ // Calculate the coordinates (x, y, in pixels) of the tip of the triangle
+ x = (width + bs_width) / 2 + xthickness;
+ y = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(ythickness - bs_height) / 2.0) - UNUSED_PIXELS);
for (i = 0; i < bs_width; i++)
gdk_draw_line (widget->window, gc,
}
}
+static void
+sp_vruler_size_request (GtkWidget *widget, GtkRequisition *requisition)
+{
+ requisition->width = widget->style->xthickness * 2 + RULER_WIDTH;
+}
+
+
+/**
+ * The vruler widget's size_allocate callback.
+ */
+static void
+sp_vruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
+{
+ g_assert (widget != NULL);
+ g_assert (SP_IS_VRULER (widget));
+
+ // First call the default gtk_widget_size_allocate() method (which is being overridden here)
+ if (GTK_WIDGET_CLASS (vruler_parent_class)->size_allocate)
+ (* GTK_WIDGET_CLASS (vruler_parent_class)->size_allocate) (widget, allocation);
+
+ // Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
+ // For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
+ GtkWidget *parent = gtk_widget_get_parent(widget);
+ do {
+ if (SP_IS_DESKTOP_WIDGET(parent)) {
+ // Now we've found the desktop widget we can have the ruler boundaries updated
+ sp_desktop_widget_update_vruler(SP_DESKTOP_WIDGET(parent));
+ // If the size of the ruler has increased, then a blank part is uncovered; therefore
+ // it must be redrawn
+ sp_vruler_draw_ticks(GTK_RULER(widget));
+ break;
+ }
+ parent = gtk_widget_get_parent(parent);
+ } while (parent != NULL);
+}
+
+//TODO: warning: deprecated conversion from string constant to ‘gchar*’
+//
+//Turn out to be warnings that we should probably leave in place. The
+// pointers/types used need to be read-only. So until we correct the using
+// code, those warnings are actually desired. They say "Hey! Fix this". We
+// definitely don't want to hide/ignore them. --JonCruz
+
/// Ruler metrics.
static GtkRulerMetric const sp_ruler_metrics[] = {
// NOTE: the order of records in this struct must correspond to the SPMetric enum.
- {"NONE", "", 1, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
- {"millimeters", "mm", PX_PER_MM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
- {"centimeters", "cm", PX_PER_CM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
- {"inches", "in", PX_PER_IN, { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }, { 1, 2, 4, 8, 16 }},
- {"points", "pt", PX_PER_PT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
- {"pixels", "px", PX_PER_PX, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
- {"meters", "m", PX_PER_M, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"NONE", "", 1, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"millimeters", "mm", PX_PER_MM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"centimeters", "cm", PX_PER_CM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"inches", "in", PX_PER_IN, { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }, { 1, 2, 4, 8, 16 }},
+ {"feet", "ft", PX_PER_FT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"points", "pt", PX_PER_PT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"picas", "pc", PX_PER_PC, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"pixels", "px", PX_PER_PX, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
+ {"meters", "m", PX_PER_M, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
};
void