1 #define __SP_RULER_C__
3 /*
4 * Customized ruler class for inkscape
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * bulia byak <buliabyak@users.sf.net>
10 * Diederik van Lierop <mail@diedenrezi.nl>
11 *
12 * Copyright (C) 1999-2008 authors
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #include <cstring>
18 #include <cmath>
19 #include <cstdio>
21 #include "widget-sizes.h"
22 #include "desktop-widget.h"
23 #include "ruler.h"
24 #include "unit-constants.h"
25 #include "round.h"
27 #define MINIMUM_INCR 5
28 #define MAXIMUM_SUBDIVIDE 5
29 #define MAXIMUM_SCALES 10
30 #define UNUSED_PIXELS 2 // There appear to be two pixels that are not being used at each end of the ruler
32 static void sp_ruler_common_draw_ticks (GtkRuler *ruler);
34 static void sp_hruler_class_init (SPHRulerClass *klass);
35 static void sp_hruler_init (SPHRuler *hruler);
36 static gint sp_hruler_motion_notify (GtkWidget *widget, GdkEventMotion *event);
38 static GtkWidgetClass *hruler_parent_class;
40 GtkType
41 sp_hruler_get_type (void)
42 {
43 //TODO: switch to GObject
44 // GtkType and such calls were deprecated a while back with the
45 // introduction of GObject as a separate layer, with GType instead. --JonCruz
47 static GtkType hruler_type = 0;
49 if (!hruler_type)
50 {
51 static const GtkTypeInfo hruler_info =
52 {
53 (gchar*) "SPHRuler",
54 sizeof (SPHRuler),
55 sizeof (SPHRulerClass),
56 (GtkClassInitFunc) sp_hruler_class_init,
57 (GtkObjectInitFunc) sp_hruler_init,
58 /* reserved_1 */ NULL,
59 /* reserved_2 */ NULL,
60 (GtkClassInitFunc) NULL,
61 };
63 hruler_type = gtk_type_unique (gtk_ruler_get_type (), &hruler_info);
64 }
66 return hruler_type;
67 }
69 static void
70 sp_hruler_class_init (SPHRulerClass *klass)
71 {
72 GtkWidgetClass *widget_class;
73 GtkRulerClass *ruler_class;
75 hruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
77 widget_class = (GtkWidgetClass*) klass;
78 ruler_class = (GtkRulerClass*) klass;
80 widget_class->motion_notify_event = sp_hruler_motion_notify;
82 ruler_class->draw_ticks = sp_ruler_common_draw_ticks;
83 }
85 static void
86 sp_hruler_init (SPHRuler *hruler)
87 {
88 GtkWidget *widget;
90 widget = GTK_WIDGET (hruler);
91 widget->requisition.width = widget->style->xthickness * 2 + 1;
92 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
93 }
96 GtkWidget*
97 sp_hruler_new (void)
98 {
99 return GTK_WIDGET (gtk_type_new (sp_hruler_get_type ()));
100 }
102 static gint
103 sp_hruler_motion_notify (GtkWidget *widget,
104 GdkEventMotion *event)
105 {
106 GtkRuler *ruler;
108 g_return_val_if_fail (widget != NULL, FALSE);
109 g_return_val_if_fail (SP_IS_HRULER (widget), FALSE);
110 g_return_val_if_fail (event != NULL, FALSE);
112 ruler = GTK_RULER (widget);
113 double x = event->x; //Although event->x is double according to the docs, it only appears to return integers
114 double pos = ruler->lower + (ruler->upper - ruler->lower) * (x + UNUSED_PIXELS) / (widget->allocation.width + 2*UNUSED_PIXELS);
116 gtk_ruler_set_range(ruler, ruler->lower, ruler->upper, pos, ruler->max_size);
118 return FALSE;
119 }
121 // vruler
123 static void sp_vruler_class_init (SPVRulerClass *klass);
124 static void sp_vruler_init (SPVRuler *vruler);
125 static gint sp_vruler_motion_notify (GtkWidget *widget,
126 GdkEventMotion *event);
127 static void sp_vruler_size_request (GtkWidget *widget, GtkRequisition *requisition);
129 static GtkWidgetClass *vruler_parent_class;
131 GtkType
132 sp_vruler_get_type (void)
133 {
134 //TODO: switch to GObject
135 // GtkType and such calls were deprecated a while back with the
136 // introduction of GObject as a separate layer, with GType instead. --JonCruz
138 static GtkType vruler_type = 0;
140 if (!vruler_type)
141 {
142 static const GtkTypeInfo vruler_info =
143 {
144 (gchar*) "SPVRuler",
145 sizeof (SPVRuler),
146 sizeof (SPVRulerClass),
147 (GtkClassInitFunc) sp_vruler_class_init,
148 (GtkObjectInitFunc) sp_vruler_init,
149 /* reserved_1 */ NULL,
150 /* reserved_2 */ NULL,
151 (GtkClassInitFunc) NULL,
152 };
154 vruler_type = gtk_type_unique (gtk_ruler_get_type (), &vruler_info);
155 }
157 return vruler_type;
158 }
160 static void
161 sp_vruler_class_init (SPVRulerClass *klass)
162 {
163 GtkWidgetClass *widget_class;
164 GtkRulerClass *ruler_class;
166 vruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
168 widget_class = (GtkWidgetClass*) klass;
169 ruler_class = (GtkRulerClass*) klass;
171 widget_class->motion_notify_event = sp_vruler_motion_notify;
172 widget_class->size_request = sp_vruler_size_request;
174 ruler_class->draw_ticks = sp_ruler_common_draw_ticks;
175 }
177 static void
178 sp_vruler_init (SPVRuler *vruler)
179 {
180 GtkWidget *widget;
182 widget = GTK_WIDGET (vruler);
183 widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
184 widget->requisition.height = widget->style->ythickness * 2 + 1;
186 g_object_set(G_OBJECT(vruler), "orientation", GTK_ORIENTATION_VERTICAL, NULL);
187 }
189 GtkWidget*
190 sp_vruler_new (void)
191 {
192 return GTK_WIDGET (gtk_type_new (sp_vruler_get_type ()));
193 }
196 static gint
197 sp_vruler_motion_notify (GtkWidget *widget,
198 GdkEventMotion *event)
199 {
200 GtkRuler *ruler;
202 g_return_val_if_fail (widget != NULL, FALSE);
203 g_return_val_if_fail (SP_IS_VRULER (widget), FALSE);
204 g_return_val_if_fail (event != NULL, FALSE);
206 ruler = GTK_RULER (widget);
207 double y = event->y; //Although event->y is double according to the docs, it only appears to return integers
208 double pos = ruler->lower + (ruler->upper - ruler->lower) * (y + UNUSED_PIXELS) / (widget->allocation.height + 2*UNUSED_PIXELS);
210 gtk_ruler_set_range(ruler, ruler->lower, ruler->upper, pos, ruler->max_size);
212 return FALSE;
213 }
215 static void
216 sp_vruler_size_request (GtkWidget *widget, GtkRequisition *requisition)
217 {
218 requisition->width = widget->style->xthickness * 2 + RULER_WIDTH;
219 }
221 static void
222 sp_ruler_common_draw_ticks (GtkRuler *ruler)
223 {
224 GtkWidget *widget;
225 GdkGC *gc, *bg_gc;
226 PangoFontDescription *pango_desc;
227 PangoContext *pango_context;
228 PangoLayout *pango_layout;
229 gint i, j, tick_index;
230 gint width, height;
231 gint xthickness;
232 gint ythickness;
233 gint length, ideal_length;
234 double lower, upper; /* Upper and lower limits, in ruler units */
235 double increment; /* Number of pixels per unit */
236 gint scale; /* Number of units per major unit */
237 double subd_incr;
238 double start, end, cur;
239 gchar unit_str[32];
240 gchar digit_str[2] = { '\0', '\0' };
241 gint digit_height;
242 //gint text_width, text_height;
243 gint text_dimension;
244 gint pos;
245 GtkOrientation orientation;
247 g_return_if_fail (ruler != NULL);
249 if (!GTK_WIDGET_DRAWABLE (ruler))
250 return;
252 g_object_get(G_OBJECT(ruler), "orientation", &orientation, NULL);
253 widget = GTK_WIDGET (ruler);
254 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
255 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
257 pango_desc = widget->style->font_desc;
258 pango_context = gtk_widget_get_pango_context (widget);
259 pango_layout = pango_layout_new (pango_context);
260 PangoFontDescription *fs = pango_font_description_new ();
261 pango_font_description_set_size (fs, RULER_FONT_SIZE);
262 pango_layout_set_font_description (pango_layout, fs);
263 pango_font_description_free (fs);
265 digit_height = (int) floor (RULER_FONT_SIZE * RULER_FONT_VERTICAL_SPACING / PANGO_SCALE + 0.5);
266 xthickness = widget->style->xthickness;
267 ythickness = widget->style->ythickness;
269 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
270 width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
271 height = widget->allocation.height;
272 } else {
273 width = widget->allocation.height;
274 height = widget->allocation.width;
275 }
277 gtk_paint_box (widget->style, ruler->backing_store,
278 GTK_STATE_NORMAL, GTK_SHADOW_NONE, NULL, widget,
279 orientation == GTK_ORIENTATION_HORIZONTAL ? "hruler" : "vruler",
280 0, 0,
281 widget->allocation.width, widget->allocation.height);
283 upper = ruler->upper / ruler->metric->pixels_per_unit; // upper and lower are expressed in ruler units
284 lower = ruler->lower / ruler->metric->pixels_per_unit;
285 /* "pixels_per_unit" should be "points_per_unit". This is the size of the unit
286 * in 1/72nd's of an inch and has nothing to do with screen pixels */
288 if ((upper - lower) == 0)
289 return;
291 increment = (double) (width + 2*UNUSED_PIXELS) / (upper - lower); // screen pixels per ruler unit
293 /* determine the scale
294 * For vruler, use the maximum extents of the ruler to determine the largest
295 * possible number to be displayed. Calculate the height in pixels
296 * of this displayed text. Use this height to find a scale which
297 * leaves sufficient room for drawing the ruler.
298 * For hruler, we calculate the text size as for the vruler instead of using
299 * text_width = gdk_string_width(font, unit_str), so that the result
300 * for the scale looks consistent with an accompanying vruler
301 */
302 scale = (int)(ceil (ruler->max_size / ruler->metric->pixels_per_unit));
303 sprintf (unit_str, "%d", scale);
304 text_dimension = strlen (unit_str) * digit_height + 1;
306 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
307 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_dimension)
308 break;
310 if (scale == MAXIMUM_SCALES)
311 scale = MAXIMUM_SCALES - 1;
313 /* drawing starts here */
314 length = 0;
315 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--) {
316 subd_incr = ruler->metric->ruler_scale[scale] /
317 ruler->metric->subdivide[i];
318 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
319 continue;
321 /* Calculate the length of the tickmarks. Make sure that
322 * this length increases for each set of ticks
323 */
324 ideal_length = height / (i + 1) - 1;
325 if (ideal_length > ++length)
326 length = ideal_length;
328 if (lower < upper) {
329 start = floor (lower / subd_incr) * subd_incr;
330 end = ceil (upper / subd_incr) * subd_incr;
331 } else {
332 start = floor (upper / subd_incr) * subd_incr;
333 end = ceil (lower / subd_incr) * subd_incr;
334 }
336 tick_index = 0;
337 cur = start; // location (in ruler units) of the first invisible tick at the left side of the canvas
339 while (cur <= end) {
340 // due to the typical values for cur, lower and increment, pos will often end up to
341 // be e.g. 641.50000000000; rounding behaviour is not defined in such a case (see round.h)
342 // and jitter will be apparent (upon redrawing some of the lines on the ruler might jump a
343 // by a pixel, and jump back on the next redraw). This is suppressed by adding 1e-9 (that's only one nanopixel ;-))
344 pos = int(Inkscape::round((cur - lower) * increment + 1e-12)) - UNUSED_PIXELS;
346 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
347 gdk_draw_line (ruler->backing_store, gc,
348 pos, height + ythickness,
349 pos, height - length + ythickness);
350 } else {
351 gdk_draw_line (ruler->backing_store, gc,
352 height + xthickness - length, pos,
353 height + xthickness, pos);
354 }
356 /* draw label */
357 double label_spacing_px = fabs((increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i]);
358 if (i == 0 &&
359 (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
360 (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
361 {
362 if (fabs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
363 sprintf (unit_str, "%dk", ((int) cur)/1000);
364 else
365 sprintf (unit_str, "%d", (int) cur);
367 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
368 pango_layout_set_text (pango_layout, unit_str, -1);
369 gdk_draw_layout (ruler->backing_store, gc,
370 pos + 2, 0, pango_layout);
371 } else {
372 for (j = 0; j < (int) strlen (unit_str); j++) {
373 digit_str[0] = unit_str[j];
374 pango_layout_set_text (pango_layout, digit_str, 1);
376 gdk_draw_layout (ruler->backing_store, gc,
377 xthickness + 1,
378 pos + digit_height * (j) + 1,
379 pango_layout);
380 }
381 }
382 }
383 /* Calculate cur from start rather than incrementing by subd_incr
384 * in each iteration. This is to avoid propagation of floating point
385 * errors in subd_incr.
386 */
387 ++tick_index;
388 cur = start + tick_index * subd_incr;
389 }
390 }
391 }
393 //TODO: warning: deprecated conversion from string constant to ‘gchar*’
394 //
395 //Turn out to be warnings that we should probably leave in place. The
396 // pointers/types used need to be read-only. So until we correct the using
397 // code, those warnings are actually desired. They say "Hey! Fix this". We
398 // definitely don't want to hide/ignore them. --JonCruz
400 // TODO address const/non-const gchar* issue:
401 /// Ruler metrics.
402 static GtkRulerMetric const sp_ruler_metrics[] = {
403 // NOTE: the order of records in this struct must correspond to the SPMetric enum.
404 {"NONE", "", 1, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
405 {"millimeters", "mm", PX_PER_MM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
406 {"centimeters", "cm", PX_PER_CM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
407 {"inches", "in", PX_PER_IN, { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }, { 1, 2, 4, 8, 16 }},
408 {"feet", "ft", PX_PER_FT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
409 {"points", "pt", PX_PER_PT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
410 {"picas", "pc", PX_PER_PC, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
411 {"pixels", "px", PX_PER_PX, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
412 {"meters", "m", PX_PER_M, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
413 };
415 void
416 sp_ruler_set_metric (GtkRuler *ruler,
417 SPMetric metric)
418 {
419 g_return_if_fail (ruler != NULL);
420 g_return_if_fail (GTK_IS_RULER (ruler));
421 g_return_if_fail((unsigned) metric < G_N_ELEMENTS(sp_ruler_metrics));
423 if (metric == 0)
424 return;
426 ruler->metric = const_cast<GtkRulerMetric *>(&sp_ruler_metrics[metric]);
428 if (GTK_WIDGET_DRAWABLE (ruler))
429 gtk_widget_queue_draw (GTK_WIDGET (ruler));
430 }