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 *
11 * Copyright (C) 1999-2005 authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <cstring>
17 #include <cmath>
18 #include <cstdio>
20 #include "widget-sizes.h"
21 #include "desktop-widget.h"
22 #include "ruler.h"
23 #include "unit-constants.h"
24 #include "round.h"
26 #define MINIMUM_INCR 5
27 #define MAXIMUM_SUBDIVIDE 5
28 #define MAXIMUM_SCALES 10
29 #define UNUSED_PIXELS 2 // There appear to be two pixels that are not being used at each end of the ruler
31 static void sp_hruler_class_init (SPHRulerClass *klass);
32 static void sp_hruler_init (SPHRuler *hruler);
33 static gint sp_hruler_motion_notify (GtkWidget *widget, GdkEventMotion *event);
34 static void sp_hruler_draw_ticks (GtkRuler *ruler);
35 static void sp_hruler_draw_pos (GtkRuler *ruler);
36 static void sp_hruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
38 static GtkWidgetClass *hruler_parent_class;
40 GtkType
41 sp_hruler_get_type (void)
42 {
43 static GtkType hruler_type = 0;
45 if (!hruler_type)
46 {
47 static const GtkTypeInfo hruler_info =
48 {
49 "SPHRuler",
50 sizeof (SPHRuler),
51 sizeof (SPHRulerClass),
52 (GtkClassInitFunc) sp_hruler_class_init,
53 (GtkObjectInitFunc) sp_hruler_init,
54 /* reserved_1 */ NULL,
55 /* reserved_2 */ NULL,
56 (GtkClassInitFunc) NULL,
57 };
59 hruler_type = gtk_type_unique (gtk_ruler_get_type (), &hruler_info);
60 }
62 return hruler_type;
63 }
65 static void
66 sp_hruler_class_init (SPHRulerClass *klass)
67 {
68 GtkWidgetClass *widget_class;
69 GtkRulerClass *ruler_class;
71 hruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
73 widget_class = (GtkWidgetClass*) klass;
74 ruler_class = (GtkRulerClass*) klass;
76 widget_class->motion_notify_event = sp_hruler_motion_notify;
77 widget_class->size_allocate = sp_hruler_size_allocate;
79 ruler_class->draw_ticks = sp_hruler_draw_ticks;
80 ruler_class->draw_pos = sp_hruler_draw_pos;
81 }
83 static void
84 sp_hruler_init (SPHRuler *hruler)
85 {
86 GtkWidget *widget;
88 widget = GTK_WIDGET (hruler);
89 widget->requisition.width = widget->style->xthickness * 2 + 1;
90 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
91 }
94 GtkWidget*
95 sp_hruler_new (void)
96 {
97 return GTK_WIDGET (gtk_type_new (sp_hruler_get_type ()));
98 }
100 static gint
101 sp_hruler_motion_notify (GtkWidget *widget,
102 GdkEventMotion *event)
103 {
104 GtkRuler *ruler;
106 g_return_val_if_fail (widget != NULL, FALSE);
107 g_return_val_if_fail (SP_IS_HRULER (widget), FALSE);
108 g_return_val_if_fail (event != NULL, FALSE);
110 ruler = GTK_RULER (widget);
111 double x = event->x; //Although event->x is double according to the docs, it only appears to return integers
112 ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (x + UNUSED_PIXELS) / (widget->allocation.width + 2*UNUSED_PIXELS);
114 /* Make sure the ruler has been allocated already */
115 if (ruler->backing_store != NULL)
116 gtk_ruler_draw_pos (ruler);
118 return FALSE;
119 }
121 static void
122 sp_hruler_draw_ticks (GtkRuler *ruler)
123 {
124 GtkWidget *widget;
125 GdkGC *gc, *bg_gc;
126 PangoFontDescription *pango_desc;
127 PangoContext *pango_context;
128 PangoLayout *pango_layout;
129 gint i, tick_index;
130 gint width, height;
131 gint xthickness;
132 gint ythickness;
133 gint length, ideal_length;
134 double lower, upper; /* Upper and lower limits, in ruler units */
135 double increment; /* Number of pixels per unit */
136 gint scale; /* Number of units per major unit */
137 double subd_incr;
138 double start, end, cur;
139 gchar unit_str[32];
140 gint digit_height;
141 gint text_width;
142 gint pos;
144 g_return_if_fail (ruler != NULL);
145 g_return_if_fail (SP_IS_HRULER (ruler));
147 if (!GTK_WIDGET_DRAWABLE (ruler))
148 return;
150 widget = GTK_WIDGET (ruler);
152 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
153 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
155 pango_desc = widget->style->font_desc;
157 // Create the pango layout
158 pango_context = gtk_widget_get_pango_context (widget);
160 pango_layout = pango_layout_new (pango_context);
162 PangoFontDescription *fs = pango_font_description_new ();
163 pango_font_description_set_size (fs, RULER_FONT_SIZE);
164 pango_layout_set_font_description (pango_layout, fs);
165 pango_font_description_free (fs);
167 digit_height = (int) floor (RULER_FONT_SIZE * RULER_FONT_VERTICAL_SPACING / PANGO_SCALE + 0.5);
169 xthickness = widget->style->xthickness;
170 ythickness = widget->style->ythickness;
172 width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
173 height = widget->allocation.height;// - ythickness * 2;
175 gtk_paint_box (widget->style, ruler->backing_store,
176 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
177 NULL, widget, "hruler",
178 0, 0,
179 widget->allocation.width, widget->allocation.height);
181 upper = ruler->upper / ruler->metric->pixels_per_unit;
182 lower = ruler->lower / ruler->metric->pixels_per_unit;
184 if ((upper - lower) == 0)
185 return;
186 increment = (double) (width + 2*UNUSED_PIXELS) / (upper - lower);
188 /* determine the scale
189 * We calculate the text size as for the vruler instead of using
190 * text_width = gdk_string_width(font, unit_str), so that the result
191 * for the scale looks consistent with an accompanying vruler
192 */
193 scale = (int)(ceil (ruler->max_size / ruler->metric->pixels_per_unit));
194 sprintf (unit_str, "%d", scale);
195 text_width = strlen (unit_str) * digit_height + 1;
197 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
198 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
199 break;
201 if (scale == MAXIMUM_SCALES)
202 scale = MAXIMUM_SCALES - 1;
204 /* drawing starts here */
205 length = 0;
206 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
207 {
208 subd_incr = ruler->metric->ruler_scale[scale] /
209 ruler->metric->subdivide[i];
210 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
211 continue;
213 /* Calculate the length of the tickmarks. Make sure that
214 * this length increases for each set of ticks
215 */
216 ideal_length = height / (i + 1) - 1;
217 if (ideal_length > ++length)
218 length = ideal_length;
220 if (lower < upper)
221 {
222 start = floor (lower / subd_incr) * subd_incr;
223 end = ceil (upper / subd_incr) * subd_incr;
224 }
225 else
226 {
227 start = floor (upper / subd_incr) * subd_incr;
228 end = ceil (lower / subd_incr) * subd_incr;
229 }
231 tick_index = 0;
232 cur = start;
234 while (cur <= end)
235 {
236 pos = int(Inkscape::round ((cur - lower) * increment) - UNUSED_PIXELS);
238 gdk_draw_line (ruler->backing_store, gc,
239 pos, height + ythickness,
240 pos, height - length + ythickness);
242 /* draw label */
243 double label_spacing_px = (increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i];
244 if (i == 0 &&
245 (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
246 (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
247 {
248 if (fabs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
249 sprintf (unit_str, "%dk", ((int) cur)/1000);
250 else
251 sprintf (unit_str, "%d", (int) cur);
253 pango_layout_set_text (pango_layout, unit_str, -1);
255 gdk_draw_layout (ruler->backing_store, gc,
256 pos + 2, 0, pango_layout);
257 }
259 /* Calculate cur from start rather than incrementing by subd_incr
260 * in each iteration. This is to avoid propagation of floating point
261 * errors in subd_incr.
262 */
263 ++tick_index;
264 cur = start + (((double)tick_index) * (double)ruler->metric->ruler_scale[scale])/ ruler->metric->subdivide[i];
265 }
266 }
267 }
269 static void
270 sp_hruler_draw_pos (GtkRuler *ruler)
271 {
272 GtkWidget *widget;
273 GdkGC *gc;
274 int i;
275 gint x, y;
276 gint width, height;
277 gint bs_width, bs_height;
278 gint xthickness;
279 gint ythickness;
280 gfloat increment;
282 g_return_if_fail (ruler != NULL);
283 g_return_if_fail (SP_IS_HRULER (ruler));
285 if (GTK_WIDGET_DRAWABLE (ruler))
286 {
287 widget = GTK_WIDGET (ruler);
289 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
290 xthickness = widget->style->xthickness;
291 ythickness = widget->style->ythickness;
292 width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
293 height = widget->allocation.height - ythickness * 2;
295 bs_width = height / 2;
296 bs_width |= 1; /* make sure it's odd */
297 bs_height = bs_width / 2 + 1;
299 if ((bs_width > 0) && (bs_height > 0))
300 {
301 /* If a backing store exists, restore the ruler */
302 if (ruler->backing_store && ruler->non_gr_exp_gc)
303 gdk_draw_pixmap (ruler->widget.window,
304 ruler->non_gr_exp_gc,
305 ruler->backing_store,
306 ruler->xsrc, ruler->ysrc,
307 ruler->xsrc, ruler->ysrc,
308 bs_width, bs_height);
310 increment = (gfloat) (width + 2*UNUSED_PIXELS) / (ruler->upper - ruler->lower);
312 x = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(xthickness - bs_width) / 2.0) - 1);
313 y = (height + bs_height) / 2 + ythickness;
315 for (i = 0; i < bs_height; i++)
316 gdk_draw_line (widget->window, gc,
317 x + i, y + i,
318 x + bs_width - 1 - i, y + i);
321 ruler->xsrc = x;
322 ruler->ysrc = y;
323 }
324 }
325 }
327 /**
328 * The hruler widget's size_allocate callback.
329 */
330 static void
331 sp_hruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
332 {
333 g_assert (widget != NULL);
334 g_assert (SP_IS_HRULER (widget));
336 // First call the default gtk_widget_size_allocate() method (which is being overridden here)
337 if (GTK_WIDGET_CLASS (hruler_parent_class)->size_allocate)
338 (* GTK_WIDGET_CLASS (hruler_parent_class)->size_allocate) (widget, allocation);
340 // Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
341 // For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
342 GtkWidget *parent = gtk_widget_get_parent(widget);
343 do {
344 if (SP_IS_DESKTOP_WIDGET(parent)) {
345 // Now we've found the desktop widget we can have the ruler boundaries updated
346 sp_desktop_widget_update_hruler(SP_DESKTOP_WIDGET(parent));
347 // If the size of the ruler has increased, then a blank part is uncovered; therefore
348 // it must be redrawn
349 sp_hruler_draw_ticks(GTK_RULER(widget));
350 break;
351 }
352 parent = gtk_widget_get_parent(parent);
353 } while (parent != NULL);
354 }
359 // vruler
361 static void sp_vruler_class_init (SPVRulerClass *klass);
362 static void sp_vruler_init (SPVRuler *vruler);
363 static gint sp_vruler_motion_notify (GtkWidget *widget,
364 GdkEventMotion *event);
365 static void sp_vruler_draw_ticks (GtkRuler *ruler);
366 static void sp_vruler_draw_pos (GtkRuler *ruler);
367 static void sp_vruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
369 static GtkWidgetClass *vruler_parent_class;
371 GtkType
372 sp_vruler_get_type (void)
373 {
374 static GtkType vruler_type = 0;
376 if (!vruler_type)
377 {
378 static const GtkTypeInfo vruler_info =
379 {
380 "SPVRuler",
381 sizeof (SPVRuler),
382 sizeof (SPVRulerClass),
383 (GtkClassInitFunc) sp_vruler_class_init,
384 (GtkObjectInitFunc) sp_vruler_init,
385 /* reserved_1 */ NULL,
386 /* reserved_2 */ NULL,
387 (GtkClassInitFunc) NULL,
388 };
390 vruler_type = gtk_type_unique (gtk_ruler_get_type (), &vruler_info);
391 }
393 return vruler_type;
394 }
396 static void
397 sp_vruler_class_init (SPVRulerClass *klass)
398 {
399 GtkWidgetClass *widget_class;
400 GtkRulerClass *ruler_class;
402 vruler_parent_class = (GtkWidgetClass *) gtk_type_class (GTK_TYPE_RULER);
404 widget_class = (GtkWidgetClass*) klass;
405 ruler_class = (GtkRulerClass*) klass;
407 widget_class->motion_notify_event = sp_vruler_motion_notify;
408 widget_class->size_allocate = sp_vruler_size_allocate;
410 ruler_class->draw_ticks = sp_vruler_draw_ticks;
411 ruler_class->draw_pos = sp_vruler_draw_pos;
412 }
414 static void
415 sp_vruler_init (SPVRuler *vruler)
416 {
417 GtkWidget *widget;
419 widget = GTK_WIDGET (vruler);
420 widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
421 widget->requisition.height = widget->style->ythickness * 2 + 1;
422 }
424 GtkWidget*
425 sp_vruler_new (void)
426 {
427 return GTK_WIDGET (gtk_type_new (sp_vruler_get_type ()));
428 }
431 static gint
432 sp_vruler_motion_notify (GtkWidget *widget,
433 GdkEventMotion *event)
434 {
435 GtkRuler *ruler;
437 g_return_val_if_fail (widget != NULL, FALSE);
438 g_return_val_if_fail (SP_IS_VRULER (widget), FALSE);
439 g_return_val_if_fail (event != NULL, FALSE);
441 ruler = GTK_RULER (widget);
442 double y = event->y; //Although event->y is double according to the docs, it only appears to return integers
443 ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (y + UNUSED_PIXELS) / (widget->allocation.height + 2*UNUSED_PIXELS);
445 /* Make sure the ruler has been allocated already */
446 if (ruler->backing_store != NULL)
447 gtk_ruler_draw_pos (ruler);
449 return FALSE;
450 }
452 static void
453 sp_vruler_draw_ticks (GtkRuler *ruler)
454 {
455 GtkWidget *widget;
456 GdkGC *gc, *bg_gc;
457 PangoFontDescription *pango_desc;
458 PangoContext *pango_context;
459 PangoLayout *pango_layout;
460 gint i, j, tick_index;
461 gint width, height;
462 gint xthickness;
463 gint ythickness;
464 gint length, ideal_length;
465 double lower, upper; /* Upper and lower limits, in ruler units */
466 double increment; /* Number of pixels per unit */
467 gint scale; /* Number of units per major unit */
468 double subd_incr;
469 double start, end, cur;
470 gchar unit_str[32];
471 gchar digit_str[2] = { '\0', '\0' };
472 gint digit_height;
473 gint text_height;
474 gint pos;
476 g_return_if_fail (ruler != NULL);
477 g_return_if_fail (SP_IS_VRULER (ruler));
479 if (!GTK_WIDGET_DRAWABLE (ruler))
480 return;
482 widget = GTK_WIDGET (ruler);
484 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
485 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
487 pango_desc = widget->style->font_desc;
489 // Create the pango layout
490 pango_context = gtk_widget_get_pango_context (widget);
492 pango_layout = pango_layout_new (pango_context);
494 PangoFontDescription *fs = pango_font_description_new ();
495 pango_font_description_set_size (fs, RULER_FONT_SIZE);
496 pango_layout_set_font_description (pango_layout, fs);
497 pango_font_description_free (fs);
499 digit_height = (int) floor (RULER_FONT_SIZE * RULER_FONT_VERTICAL_SPACING / PANGO_SCALE + 0.5);
501 xthickness = widget->style->xthickness;
502 ythickness = widget->style->ythickness;
504 width = widget->allocation.height; //in pixels; is apparently 2 pixels shorter than the canvas at each end
505 height = widget->allocation.width;// - ythickness * 2;
507 gtk_paint_box (widget->style, ruler->backing_store,
508 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
509 NULL, widget, "vruler",
510 0, 0,
511 widget->allocation.width, widget->allocation.height);
513 upper = ruler->upper / ruler->metric->pixels_per_unit;
514 lower = ruler->lower / ruler->metric->pixels_per_unit;
516 if ((upper - lower) == 0)
517 return;
518 increment = (double) (width + 2*UNUSED_PIXELS) / (upper - lower);
520 /* determine the scale
521 * use the maximum extents of the ruler to determine the largest
522 * possible number to be displayed. Calculate the height in pixels
523 * of this displayed text. Use this height to find a scale which
524 * leaves sufficient room for drawing the ruler.
525 */
526 scale = (int)ceil (ruler->max_size / ruler->metric->pixels_per_unit);
527 sprintf (unit_str, "%d", scale);
528 text_height = strlen (unit_str) * digit_height + 1;
530 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
531 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_height)
532 break;
534 if (scale == MAXIMUM_SCALES)
535 scale = MAXIMUM_SCALES - 1;
537 /* drawing starts here */
538 length = 0;
539 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--) {
540 subd_incr = (double) ruler->metric->ruler_scale[scale] /
541 (double) ruler->metric->subdivide[i];
542 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
543 continue;
545 /* Calculate the length of the tickmarks. Make sure that
546 * this length increases for each set of ticks
547 */
548 ideal_length = height / (i + 1) - 1;
549 if (ideal_length > ++length)
550 length = ideal_length;
552 if (lower < upper)
553 {
554 start = floor (lower / subd_incr) * subd_incr;
555 end = ceil (upper / subd_incr) * subd_incr;
556 }
557 else
558 {
559 start = floor (upper / subd_incr) * subd_incr;
560 end = ceil (lower / subd_incr) * subd_incr;
561 }
563 tick_index = 0;
564 cur = start;
566 while (cur < end) {
567 pos = int(Inkscape::round ((cur - lower) * increment) - UNUSED_PIXELS);
569 gdk_draw_line (ruler->backing_store, gc,
570 height + xthickness - length, pos,
571 height + xthickness, pos);
573 /* draw label */
574 double label_spacing_px = fabs((increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i]);
575 if (i == 0 &&
576 (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
577 (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
578 {
579 if (fabs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
580 sprintf (unit_str, "%dk", ((int) cur)/1000);
581 else
582 sprintf (unit_str, "%d", (int) cur);
583 for (j = 0; j < (int) strlen (unit_str); j++)
584 {
585 digit_str[0] = unit_str[j];
587 pango_layout_set_text (pango_layout, digit_str, 1);
589 gdk_draw_layout (ruler->backing_store, gc,
590 xthickness + 1,
591 pos + digit_height * (j) + 1,
592 pango_layout);
593 }
594 }
596 /* Calculate cur from start rather than incrementing by subd_incr
597 * in each iteration. This is to avoid propagation of floating point
598 * errors in subd_incr.
599 */
600 ++tick_index;
601 cur = start + (((double)tick_index) * (double)ruler->metric->ruler_scale[scale])/ ruler->metric->subdivide[i];
602 }
603 }
604 }
606 static void
607 sp_vruler_draw_pos (GtkRuler *ruler)
608 {
609 GtkWidget *widget;
610 GdkGC *gc;
611 int i;
612 gint x, y;
613 gint width, height;
614 gint bs_width, bs_height;
615 gint xthickness;
616 gint ythickness;
617 gfloat increment;
619 g_return_if_fail (ruler != NULL);
620 g_return_if_fail (SP_IS_VRULER (ruler));
622 if (GTK_WIDGET_DRAWABLE (ruler))
623 {
624 widget = GTK_WIDGET (ruler);
626 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
627 xthickness = widget->style->xthickness;
628 ythickness = widget->style->ythickness;
629 width = widget->allocation.width - xthickness * 2;
630 height = widget->allocation.height; // in pixels; is apparently 2 pixels shorter than the canvas at each end
632 bs_height = width / 2;
633 bs_height |= 1; /* make sure it's odd */
634 bs_width = bs_height / 2 + 1;
636 if ((bs_width > 0) && (bs_height > 0))
637 {
638 /* If a backing store exists, restore the ruler */
639 if (ruler->backing_store && ruler->non_gr_exp_gc)
640 gdk_draw_pixmap (ruler->widget.window,
641 ruler->non_gr_exp_gc,
642 ruler->backing_store,
643 ruler->xsrc, ruler->ysrc,
644 ruler->xsrc, ruler->ysrc,
645 bs_width, bs_height);
647 increment = (gfloat) (height + 2*UNUSED_PIXELS) / (ruler->upper - ruler->lower);
649 x = (width + bs_width) / 2 + xthickness;
650 y = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(ythickness - bs_height) / 2.0) - 1);
652 for (i = 0; i < bs_width; i++)
653 gdk_draw_line (widget->window, gc,
654 x + i, y + i,
655 x + i, y + bs_height - 1 - i);
657 ruler->xsrc = x;
658 ruler->ysrc = y;
659 }
660 }
661 }
663 /**
664 * The vruler widget's size_allocate callback.
665 */
666 static void
667 sp_vruler_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
668 {
669 g_assert (widget != NULL);
670 g_assert (SP_IS_VRULER (widget));
672 // First call the default gtk_widget_size_allocate() method (which is being overridden here)
673 if (GTK_WIDGET_CLASS (vruler_parent_class)->size_allocate)
674 (* GTK_WIDGET_CLASS (vruler_parent_class)->size_allocate) (widget, allocation);
676 // Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
677 // For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
678 GtkWidget *parent = gtk_widget_get_parent(widget);
679 do {
680 if (SP_IS_DESKTOP_WIDGET(parent)) {
681 // Now we've found the desktop widget we can have the ruler boundaries updated
682 sp_desktop_widget_update_vruler(SP_DESKTOP_WIDGET(parent));
683 // If the size of the ruler has increased, then a blank part is uncovered; therefore
684 // it must be redrawn
685 sp_vruler_draw_ticks(GTK_RULER(widget));
686 break;
687 }
688 parent = gtk_widget_get_parent(parent);
689 } while (parent != NULL);
690 }
693 /// Ruler metrics.
694 static GtkRulerMetric const sp_ruler_metrics[] = {
695 // NOTE: the order of records in this struct must correspond to the SPMetric enum.
696 {"NONE", "", 1, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
697 {"millimeters", "mm", PX_PER_MM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
698 {"centimeters", "cm", PX_PER_CM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
699 {"inches", "in", PX_PER_IN, { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }, { 1, 2, 4, 8, 16 }},
700 {"points", "pt", PX_PER_PT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
701 {"pixels", "px", PX_PER_PX, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
702 {"meters", "m", PX_PER_M, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
703 };
705 void
706 sp_ruler_set_metric (GtkRuler *ruler,
707 SPMetric metric)
708 {
709 g_return_if_fail (ruler != NULL);
710 g_return_if_fail (GTK_IS_RULER (ruler));
711 g_return_if_fail((unsigned) metric < G_N_ELEMENTS(sp_ruler_metrics));
713 if (metric == 0)
714 return;
716 ruler->metric = const_cast<GtkRulerMetric *>(&sp_ruler_metrics[metric]);
718 if (GTK_WIDGET_DRAWABLE (ruler))
719 gtk_widget_queue_draw (GTK_WIDGET (ruler));
720 }