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_hruler_class_init (SPHRulerClass *klass);
33 static void sp_hruler_init (SPHRuler *hruler);
34 static gint sp_hruler_motion_notify (GtkWidget *widget, GdkEventMotion *event);
35 static void sp_hruler_draw_ticks (GtkRuler *ruler);
36 static void sp_hruler_draw_pos (GtkRuler *ruler);
37 static void sp_hruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
39 static GtkWidgetClass *hruler_parent_class;
41 GtkType
42 sp_hruler_get_type (void)
43 {
44 //TODO: switch to GObject
45 // GtkType and such calls were deprecated a while back with the
46 // introduction of GObject as a separate layer, with GType instead. --JonCruz
48 static GtkType hruler_type = 0;
50 if (!hruler_type)
51 {
52 static const GtkTypeInfo hruler_info =
53 {
54 (gchar*) "SPHRuler",
55 sizeof (SPHRuler),
56 sizeof (SPHRulerClass),
57 (GtkClassInitFunc) sp_hruler_class_init,
58 (GtkObjectInitFunc) sp_hruler_init,
59 /* reserved_1 */ NULL,
60 /* reserved_2 */ NULL,
61 (GtkClassInitFunc) NULL,
62 };
64 hruler_type = gtk_type_unique (gtk_ruler_get_type (), &hruler_info);
65 }
67 return hruler_type;
68 }
70 static void
71 sp_hruler_class_init (SPHRulerClass *klass)
72 {
73 GtkWidgetClass *widget_class;
74 GtkRulerClass *ruler_class;
76 hruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
78 widget_class = (GtkWidgetClass*) klass;
79 ruler_class = (GtkRulerClass*) klass;
81 widget_class->motion_notify_event = sp_hruler_motion_notify;
82 widget_class->size_allocate = sp_hruler_size_allocate;
84 ruler_class->draw_ticks = sp_hruler_draw_ticks;
85 ruler_class->draw_pos = sp_hruler_draw_pos;
86 }
88 static void
89 sp_hruler_init (SPHRuler *hruler)
90 {
91 GtkWidget *widget;
93 widget = GTK_WIDGET (hruler);
94 widget->requisition.width = widget->style->xthickness * 2 + 1;
95 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
96 }
99 GtkWidget*
100 sp_hruler_new (void)
101 {
102 return GTK_WIDGET (gtk_type_new (sp_hruler_get_type ()));
103 }
105 static gint
106 sp_hruler_motion_notify (GtkWidget *widget,
107 GdkEventMotion *event)
108 {
109 GtkRuler *ruler;
111 g_return_val_if_fail (widget != NULL, FALSE);
112 g_return_val_if_fail (SP_IS_HRULER (widget), FALSE);
113 g_return_val_if_fail (event != NULL, FALSE);
115 ruler = GTK_RULER (widget);
116 double x = event->x; //Although event->x is double according to the docs, it only appears to return integers
117 ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (x + UNUSED_PIXELS) / (widget->allocation.width + 2*UNUSED_PIXELS);
119 /* Make sure the ruler has been allocated already */
120 if (ruler->backing_store != NULL)
121 gtk_ruler_draw_pos (ruler);
123 return FALSE;
124 }
126 static void
127 sp_hruler_draw_ticks (GtkRuler *ruler)
128 {
129 GtkWidget *widget;
130 GdkGC *gc, *bg_gc;
131 PangoFontDescription *pango_desc;
132 PangoContext *pango_context;
133 PangoLayout *pango_layout;
134 gint i, tick_index;
135 gint width, height;
136 gint xthickness;
137 gint ythickness;
138 gint length, ideal_length;
139 double lower, upper; /* Upper and lower limits, in ruler units */
140 double increment; /* Number of pixels per unit */
141 gint scale; /* Number of units per major unit */
142 double subd_incr;
143 double start, end, cur;
144 gchar unit_str[32];
145 gint digit_height;
146 gint text_width;
147 gint pos;
149 g_return_if_fail (ruler != NULL);
150 g_return_if_fail (SP_IS_HRULER (ruler));
152 if (!GTK_WIDGET_DRAWABLE (ruler))
153 return;
155 widget = GTK_WIDGET (ruler);
157 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
158 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
160 pango_desc = widget->style->font_desc;
162 // Create the pango layout
163 pango_context = gtk_widget_get_pango_context (widget);
165 pango_layout = pango_layout_new (pango_context);
167 PangoFontDescription *fs = pango_font_description_new ();
168 pango_font_description_set_size (fs, RULER_FONT_SIZE);
169 pango_layout_set_font_description (pango_layout, fs);
170 pango_font_description_free (fs);
172 digit_height = (int) floor (RULER_FONT_SIZE * RULER_FONT_VERTICAL_SPACING / PANGO_SCALE + 0.5);
174 xthickness = widget->style->xthickness;
175 ythickness = widget->style->ythickness;
177 width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
178 height = widget->allocation.height;// - ythickness * 2;
180 gtk_paint_box (widget->style, ruler->backing_store,
181 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
182 NULL, widget, "hruler",
183 0, 0,
184 widget->allocation.width, widget->allocation.height);
186 upper = ruler->upper / ruler->metric->pixels_per_unit; // upper and lower are expressed in ruler units
187 lower = ruler->lower / ruler->metric->pixels_per_unit;
188 /* "pixels_per_unit" should be "points_per_unit". This is the size of the unit
189 * in 1/72nd's of an inch and has nothing to do with screen pixels */
191 if ((upper - lower) == 0)
192 return;
193 increment = (double) (width + 2*UNUSED_PIXELS) / (upper - lower); // screen pixels per ruler unit
195 /* determine the scale
196 * We calculate the text size as for the vruler instead of using
197 * text_width = gdk_string_width(font, unit_str), so that the result
198 * for the scale looks consistent with an accompanying vruler
199 */
200 scale = (int)(ceil (ruler->max_size / ruler->metric->pixels_per_unit));
201 sprintf (unit_str, "%d", scale);
202 text_width = strlen (unit_str) * digit_height + 1;
204 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
205 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
206 break;
208 if (scale == MAXIMUM_SCALES)
209 scale = MAXIMUM_SCALES - 1;
211 /* drawing starts here */
212 length = 0;
213 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
214 {
215 subd_incr = ruler->metric->ruler_scale[scale] /
216 ruler->metric->subdivide[i];
217 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
218 continue;
220 /* Calculate the length of the tickmarks. Make sure that
221 * this length increases for each set of ticks
222 */
223 ideal_length = height / (i + 1) - 1;
224 if (ideal_length > ++length)
225 length = ideal_length;
227 if (lower < upper)
228 {
229 start = floor (lower / subd_incr) * subd_incr;
230 end = ceil (upper / subd_incr) * subd_incr;
231 }
232 else
233 {
234 start = floor (upper / subd_incr) * subd_incr;
235 end = ceil (lower / subd_incr) * subd_incr;
236 }
238 tick_index = 0;
239 cur = start; // location (in ruler units) of the first invisible tick at the left side of the canvas
241 while (cur <= end)
242 {
243 // due to the typical values for cur, lower and increment, pos will often end up to
244 // be e.g. 641.50000000000; rounding behaviour is not defined in such a case (see round.h)
245 // and jitter will be apparent (upon redrawing some of the lines on the ruler might jump a
246 // by a pixel, and jump back on the next redraw). This is suppressed by adding 1e-9 (that's only one nanopixel ;-))
247 pos = int(Inkscape::round((cur - lower) * increment + 1e-12)) - UNUSED_PIXELS;
248 gdk_draw_line (ruler->backing_store, gc,
249 pos, height + ythickness,
250 pos, height - length + ythickness);
252 /* draw label */
253 double label_spacing_px = (increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i];
254 if (i == 0 &&
255 (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
256 (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
257 {
258 if (fabs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
259 sprintf (unit_str, "%dk", ((int) cur)/1000);
260 else
261 sprintf (unit_str, "%d", (int) cur);
263 pango_layout_set_text (pango_layout, unit_str, -1);
265 gdk_draw_layout (ruler->backing_store, gc,
266 pos + 2, 0, pango_layout);
267 }
269 /* Calculate cur from start rather than incrementing by subd_incr
270 * in each iteration. This is to avoid propagation of floating point
271 * errors in subd_incr.
272 */
273 ++tick_index;
274 cur = start + tick_index * subd_incr;
275 }
276 }
277 }
279 static void
280 sp_hruler_draw_pos (GtkRuler *ruler)
281 {
282 GtkWidget *widget;
283 GdkGC *gc;
284 int i;
285 gint x, y;
286 gint width, height;
287 gint bs_width, bs_height;
288 gint xthickness;
289 gint ythickness;
290 gfloat increment;
292 g_return_if_fail (ruler != NULL);
293 g_return_if_fail (SP_IS_HRULER (ruler));
295 if (GTK_WIDGET_DRAWABLE (ruler))
296 {
297 widget = GTK_WIDGET (ruler);
299 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
300 xthickness = widget->style->xthickness;
301 ythickness = widget->style->ythickness;
302 width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
303 height = widget->allocation.height - ythickness * 2;
305 bs_width = height / 2;
306 bs_width |= 1; /* make sure it's odd */
307 bs_height = bs_width / 2 + 1;
309 if ((bs_width > 0) && (bs_height > 0))
310 {
311 /* If a backing store exists, restore the ruler */
312 if (ruler->backing_store && ruler->non_gr_exp_gc)
313 gdk_draw_pixmap (ruler->widget.window,
314 ruler->non_gr_exp_gc,
315 ruler->backing_store,
316 ruler->xsrc, ruler->ysrc,
317 ruler->xsrc, ruler->ysrc,
318 bs_width, bs_height);
320 increment = (gfloat) (width + 2*UNUSED_PIXELS) / (ruler->upper - ruler->lower);
322 // Calculate the coordinates (x, y, in pixels) of the tip of the triangle
323 x = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(xthickness - bs_width) / 2.0) - UNUSED_PIXELS);
324 y = (height + bs_height) / 2 + ythickness;
326 for (i = 0; i < bs_height; i++)
327 gdk_draw_line (widget->window, gc,
328 x + i, y + i,
329 x + bs_width - 1 - i, y + i);
332 ruler->xsrc = x;
333 ruler->ysrc = y;
334 }
335 }
336 }
338 /**
339 * The hruler widget's size_allocate callback.
340 */
341 static void
342 sp_hruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
343 {
344 g_assert (widget != NULL);
345 g_assert (SP_IS_HRULER (widget));
347 // First call the default gtk_widget_size_allocate() method (which is being overridden here)
348 if (GTK_WIDGET_CLASS (hruler_parent_class)->size_allocate)
349 (* GTK_WIDGET_CLASS (hruler_parent_class)->size_allocate) (widget, allocation);
351 // Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
352 // For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
353 GtkWidget *parent = gtk_widget_get_parent(widget);
354 do {
355 if (SP_IS_DESKTOP_WIDGET(parent)) {
356 // Now we've found the desktop widget we can have the ruler boundaries updated
357 sp_desktop_widget_update_hruler(SP_DESKTOP_WIDGET(parent));
358 // If the size of the ruler has increased, then a blank part is uncovered; therefore
359 // it must be redrawn
360 sp_hruler_draw_ticks(GTK_RULER(widget));
361 break;
362 }
363 parent = gtk_widget_get_parent(parent);
364 } while (parent != NULL);
365 }
370 // vruler
372 static void sp_vruler_class_init (SPVRulerClass *klass);
373 static void sp_vruler_init (SPVRuler *vruler);
374 static gint sp_vruler_motion_notify (GtkWidget *widget,
375 GdkEventMotion *event);
376 static void sp_vruler_draw_ticks (GtkRuler *ruler);
377 static void sp_vruler_draw_pos (GtkRuler *ruler);
378 static void sp_vruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
380 static GtkWidgetClass *vruler_parent_class;
382 GtkType
383 sp_vruler_get_type (void)
384 {
385 //TODO: switch to GObject
386 // GtkType and such calls were deprecated a while back with the
387 // introduction of GObject as a separate layer, with GType instead. --JonCruz
389 static GtkType vruler_type = 0;
391 if (!vruler_type)
392 {
393 static const GtkTypeInfo vruler_info =
394 {
395 (gchar*) "SPVRuler",
396 sizeof (SPVRuler),
397 sizeof (SPVRulerClass),
398 (GtkClassInitFunc) sp_vruler_class_init,
399 (GtkObjectInitFunc) sp_vruler_init,
400 /* reserved_1 */ NULL,
401 /* reserved_2 */ NULL,
402 (GtkClassInitFunc) NULL,
403 };
405 vruler_type = gtk_type_unique (gtk_ruler_get_type (), &vruler_info);
406 }
408 return vruler_type;
409 }
411 static void
412 sp_vruler_class_init (SPVRulerClass *klass)
413 {
414 GtkWidgetClass *widget_class;
415 GtkRulerClass *ruler_class;
417 vruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
419 widget_class = (GtkWidgetClass*) klass;
420 ruler_class = (GtkRulerClass*) klass;
422 widget_class->motion_notify_event = sp_vruler_motion_notify;
423 widget_class->size_allocate = sp_vruler_size_allocate;
425 ruler_class->draw_ticks = sp_vruler_draw_ticks;
426 ruler_class->draw_pos = sp_vruler_draw_pos;
427 }
429 static void
430 sp_vruler_init (SPVRuler *vruler)
431 {
432 GtkWidget *widget;
434 widget = GTK_WIDGET (vruler);
435 widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
436 widget->requisition.height = widget->style->ythickness * 2 + 1;
437 }
439 GtkWidget*
440 sp_vruler_new (void)
441 {
442 return GTK_WIDGET (gtk_type_new (sp_vruler_get_type ()));
443 }
446 static gint
447 sp_vruler_motion_notify (GtkWidget *widget,
448 GdkEventMotion *event)
449 {
450 GtkRuler *ruler;
452 g_return_val_if_fail (widget != NULL, FALSE);
453 g_return_val_if_fail (SP_IS_VRULER (widget), FALSE);
454 g_return_val_if_fail (event != NULL, FALSE);
456 ruler = GTK_RULER (widget);
457 double y = event->y; //Although event->y is double according to the docs, it only appears to return integers
458 ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (y + UNUSED_PIXELS) / (widget->allocation.height + 2*UNUSED_PIXELS);
460 /* Make sure the ruler has been allocated already */
461 if (ruler->backing_store != NULL)
462 gtk_ruler_draw_pos (ruler);
464 return FALSE;
465 }
467 static void
468 sp_vruler_draw_ticks (GtkRuler *ruler)
469 {
470 GtkWidget *widget;
471 GdkGC *gc, *bg_gc;
472 PangoFontDescription *pango_desc;
473 PangoContext *pango_context;
474 PangoLayout *pango_layout;
475 gint i, j, tick_index;
476 gint width, height;
477 gint xthickness;
478 gint ythickness;
479 gint length, ideal_length;
480 double lower, upper; /* Upper and lower limits, in ruler units */
481 double increment; /* Number of pixels per unit */
482 gint scale; /* Number of units per major unit */
483 double subd_incr;
484 double start, end, cur;
485 gchar unit_str[32];
486 gchar digit_str[2] = { '\0', '\0' };
487 gint digit_height;
488 gint text_height;
489 gint pos;
491 g_return_if_fail (ruler != NULL);
492 g_return_if_fail (SP_IS_VRULER (ruler));
494 if (!GTK_WIDGET_DRAWABLE (ruler))
495 return;
497 widget = GTK_WIDGET (ruler);
499 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
500 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
502 pango_desc = widget->style->font_desc;
504 // Create the pango layout
505 pango_context = gtk_widget_get_pango_context (widget);
507 pango_layout = pango_layout_new (pango_context);
509 PangoFontDescription *fs = pango_font_description_new ();
510 pango_font_description_set_size (fs, RULER_FONT_SIZE);
511 pango_layout_set_font_description (pango_layout, fs);
512 pango_font_description_free (fs);
514 digit_height = (int) floor (RULER_FONT_SIZE * RULER_FONT_VERTICAL_SPACING / PANGO_SCALE + 0.5);
516 xthickness = widget->style->xthickness;
517 ythickness = widget->style->ythickness;
519 width = widget->allocation.height; //in pixels; is apparently 2 pixels shorter than the canvas at each end
520 height = widget->allocation.width;// - ythickness * 2;
522 gtk_paint_box (widget->style, ruler->backing_store,
523 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
524 NULL, widget, "vruler",
525 0, 0,
526 widget->allocation.width, widget->allocation.height);
528 upper = ruler->upper / ruler->metric->pixels_per_unit; // upper and lower are expressed in ruler units
529 lower = ruler->lower / ruler->metric->pixels_per_unit;
530 /* "pixels_per_unit" should be "points_per_unit". This is the size of the unit
531 * in 1/72nd's of an inch and has nothing to do with screen pixels */
533 if ((upper - lower) == 0)
534 return;
535 increment = (double) (width + 2*UNUSED_PIXELS) / (upper - lower); // screen pixels per ruler unit
537 /* determine the scale
538 * use the maximum extents of the ruler to determine the largest
539 * possible number to be displayed. Calculate the height in pixels
540 * of this displayed text. Use this height to find a scale which
541 * leaves sufficient room for drawing the ruler.
542 */
543 scale = (int)ceil (ruler->max_size / ruler->metric->pixels_per_unit);
544 sprintf (unit_str, "%d", scale);
545 text_height = strlen (unit_str) * digit_height + 1;
547 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
548 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_height)
549 break;
551 if (scale == MAXIMUM_SCALES)
552 scale = MAXIMUM_SCALES - 1;
554 /* drawing starts here */
555 length = 0;
556 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--) {
557 subd_incr = (double) ruler->metric->ruler_scale[scale] /
558 (double) ruler->metric->subdivide[i];
559 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
560 continue;
562 /* Calculate the length of the tickmarks. Make sure that
563 * this length increases for each set of ticks
564 */
565 ideal_length = height / (i + 1) - 1;
566 if (ideal_length > ++length)
567 length = ideal_length;
569 if (lower < upper)
570 {
571 start = floor (lower / subd_incr) * subd_incr;
572 end = ceil (upper / subd_incr) * subd_incr;
573 }
574 else
575 {
576 start = floor (upper / subd_incr) * subd_incr;
577 end = ceil (lower / subd_incr) * subd_incr;
578 }
580 tick_index = 0;
581 cur = start; // location (in ruler units) of the first invisible tick at the top side of the canvas
583 while (cur < end) {
584 // due to the typical values for cur, lower and increment, pos will often end up to
585 // be e.g. 641.50000000000; rounding behaviour is not defined in such a case (see round.h)
586 // and jitter will be apparent (upon redrawing some of the lines on the ruler might jump a
587 // by a pixel, and jump back on the next redraw). This is suppressed by adding 1e-9 (that's only one nanopixel ;-))
588 pos = int(Inkscape::round((cur - lower) * increment + 1e-12)) - UNUSED_PIXELS;
590 gdk_draw_line (ruler->backing_store, gc,
591 height + xthickness - length, pos,
592 height + xthickness, pos);
594 /* draw label */
595 double label_spacing_px = fabs((increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i]);
596 if (i == 0 &&
597 (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
598 (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
599 {
600 if (fabs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
601 sprintf (unit_str, "%dk", ((int) cur)/1000);
602 else
603 sprintf (unit_str, "%d", (int) cur);
604 for (j = 0; j < (int) strlen (unit_str); j++)
605 {
606 digit_str[0] = unit_str[j];
608 pango_layout_set_text (pango_layout, digit_str, 1);
610 gdk_draw_layout (ruler->backing_store, gc,
611 xthickness + 1,
612 pos + digit_height * (j) + 1,
613 pango_layout);
614 }
615 }
617 /* Calculate cur from start rather than incrementing by subd_incr
618 * in each iteration. This is to avoid propagation of floating point
619 * errors in subd_incr.
620 */
621 ++tick_index;
622 cur = start + tick_index * subd_incr;
623 }
624 }
625 }
627 static void
628 sp_vruler_draw_pos (GtkRuler *ruler)
629 {
630 GtkWidget *widget;
631 GdkGC *gc;
632 int i;
633 gint x, y;
634 gint width, height;
635 gint bs_width, bs_height;
636 gint xthickness;
637 gint ythickness;
638 gfloat increment;
640 g_return_if_fail (ruler != NULL);
641 g_return_if_fail (SP_IS_VRULER (ruler));
643 if (GTK_WIDGET_DRAWABLE (ruler))
644 {
645 widget = GTK_WIDGET (ruler);
647 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
648 xthickness = widget->style->xthickness;
649 ythickness = widget->style->ythickness;
650 width = widget->allocation.width - xthickness * 2;
651 height = widget->allocation.height; // in pixels; is apparently 2 pixels shorter than the canvas at each end
653 bs_height = width / 2;
654 bs_height |= 1; /* make sure it's odd */
655 bs_width = bs_height / 2 + 1;
657 if ((bs_width > 0) && (bs_height > 0))
658 {
659 /* If a backing store exists, restore the ruler */
660 if (ruler->backing_store && ruler->non_gr_exp_gc)
661 gdk_draw_pixmap (ruler->widget.window,
662 ruler->non_gr_exp_gc,
663 ruler->backing_store,
664 ruler->xsrc, ruler->ysrc,
665 ruler->xsrc, ruler->ysrc,
666 bs_width, bs_height);
668 increment = (gfloat) (height + 2*UNUSED_PIXELS) / (ruler->upper - ruler->lower);
670 // Calculate the coordinates (x, y, in pixels) of the tip of the triangle
671 x = (width + bs_width) / 2 + xthickness;
672 y = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(ythickness - bs_height) / 2.0) - UNUSED_PIXELS);
674 for (i = 0; i < bs_width; i++)
675 gdk_draw_line (widget->window, gc,
676 x + i, y + i,
677 x + i, y + bs_height - 1 - i);
679 ruler->xsrc = x;
680 ruler->ysrc = y;
681 }
682 }
683 }
685 /**
686 * The vruler widget's size_allocate callback.
687 */
688 static void
689 sp_vruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
690 {
691 g_assert (widget != NULL);
692 g_assert (SP_IS_VRULER (widget));
694 // First call the default gtk_widget_size_allocate() method (which is being overridden here)
695 if (GTK_WIDGET_CLASS (vruler_parent_class)->size_allocate)
696 (* GTK_WIDGET_CLASS (vruler_parent_class)->size_allocate) (widget, allocation);
698 // Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
699 // For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
700 GtkWidget *parent = gtk_widget_get_parent(widget);
701 do {
702 if (SP_IS_DESKTOP_WIDGET(parent)) {
703 // Now we've found the desktop widget we can have the ruler boundaries updated
704 sp_desktop_widget_update_vruler(SP_DESKTOP_WIDGET(parent));
705 // If the size of the ruler has increased, then a blank part is uncovered; therefore
706 // it must be redrawn
707 sp_vruler_draw_ticks(GTK_RULER(widget));
708 break;
709 }
710 parent = gtk_widget_get_parent(parent);
711 } while (parent != NULL);
712 }
714 //TODO: warning: deprecated conversion from string constant to ‘gchar*’
715 //
716 //Turn out to be warnings that we should probably leave in place. The
717 // pointers/types used need to be read-only. So until we correct the using
718 // code, those warnings are actually desired. They say "Hey! Fix this". We
719 // definitely don't want to hide/ignore them. --JonCruz
721 /// Ruler metrics.
722 static GtkRulerMetric const sp_ruler_metrics[] = {
723 // NOTE: the order of records in this struct must correspond to the SPMetric enum.
724 {"NONE", "", 1, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
725 {"millimeters", "mm", PX_PER_MM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
726 {"centimeters", "cm", PX_PER_CM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
727 {"inches", "in", PX_PER_IN, { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }, { 1, 2, 4, 8, 16 }},
728 {"feet", "ft", PX_PER_FT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
729 {"points", "pt", PX_PER_PT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
730 {"picas", "pc", PX_PER_PC, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
731 {"pixels", "px", PX_PER_PX, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
732 {"meters", "m", PX_PER_M, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
733 };
735 void
736 sp_ruler_set_metric (GtkRuler *ruler,
737 SPMetric metric)
738 {
739 g_return_if_fail (ruler != NULL);
740 g_return_if_fail (GTK_IS_RULER (ruler));
741 g_return_if_fail((unsigned) metric < G_N_ELEMENTS(sp_ruler_metrics));
743 if (metric == 0)
744 return;
746 ruler->metric = const_cast<GtkRulerMetric *>(&sp_ruler_metrics[metric]);
748 if (GTK_WIDGET_DRAWABLE (ruler))
749 gtk_widget_queue_draw (GTK_WIDGET (ruler));
750 }