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 <cmath>
17 #include <cstdio>
18 #include <string.h>
19 #include "widget-sizes.h"
20 #include "ruler.h"
21 #include "unit-constants.h"
23 #define MINIMUM_INCR 5
24 #define MAXIMUM_SUBDIVIDE 5
25 #define MAXIMUM_SCALES 10
27 #define ROUND(x) int (std::floor ((x) + 0.5000000001))
30 static void sp_hruler_class_init (SPHRulerClass *klass);
31 static void sp_hruler_init (SPHRuler *hruler);
32 static gint sp_hruler_motion_notify (GtkWidget *widget,
33 GdkEventMotion *event);
34 static void sp_hruler_draw_ticks (GtkRuler *ruler);
35 static void sp_hruler_draw_pos (GtkRuler *ruler);
38 GtkType
39 sp_hruler_get_type (void)
40 {
41 static GtkType hruler_type = 0;
43 if (!hruler_type)
44 {
45 static const GtkTypeInfo hruler_info =
46 {
47 "SPHRuler",
48 sizeof (SPHRuler),
49 sizeof (SPHRulerClass),
50 (GtkClassInitFunc) sp_hruler_class_init,
51 (GtkObjectInitFunc) sp_hruler_init,
52 /* reserved_1 */ NULL,
53 /* reserved_2 */ NULL,
54 (GtkClassInitFunc) NULL,
55 };
57 hruler_type = gtk_type_unique (gtk_ruler_get_type (), &hruler_info);
58 }
60 return hruler_type;
61 }
63 static void
64 sp_hruler_class_init (SPHRulerClass *klass)
65 {
66 GtkWidgetClass *widget_class;
67 GtkRulerClass *ruler_class;
69 widget_class = (GtkWidgetClass*) klass;
70 ruler_class = (GtkRulerClass*) klass;
72 widget_class->motion_notify_event = sp_hruler_motion_notify;
74 ruler_class->draw_ticks = sp_hruler_draw_ticks;
75 ruler_class->draw_pos = sp_hruler_draw_pos;
76 }
78 static void
79 sp_hruler_init (SPHRuler *hruler)
80 {
81 GtkWidget *widget;
83 widget = GTK_WIDGET (hruler);
84 widget->requisition.width = widget->style->xthickness * 2 + 1;
85 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
86 }
89 GtkWidget*
90 sp_hruler_new (void)
91 {
92 return GTK_WIDGET (gtk_type_new (sp_hruler_get_type ()));
93 }
95 static gint
96 sp_hruler_motion_notify (GtkWidget *widget,
97 GdkEventMotion *event)
98 {
99 GtkRuler *ruler;
100 gint x;
102 g_return_val_if_fail (widget != NULL, FALSE);
103 g_return_val_if_fail (SP_IS_HRULER (widget), FALSE);
104 g_return_val_if_fail (event != NULL, FALSE);
106 ruler = GTK_RULER (widget);
108 x = (int)event->x;
110 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
112 /* Make sure the ruler has been allocated already */
113 if (ruler->backing_store != NULL)
114 gtk_ruler_draw_pos (ruler);
116 return FALSE;
117 }
119 static void
120 sp_hruler_draw_ticks (GtkRuler *ruler)
121 {
122 GtkWidget *widget;
123 GdkGC *gc, *bg_gc;
124 PangoFontDescription *pango_desc;
125 PangoContext *pango_context;
126 PangoLayout *pango_layout;
127 gint i, tick_index;
128 gint width, height;
129 gint xthickness;
130 gint ythickness;
131 gint length, ideal_length;
132 double lower, upper; /* Upper and lower limits, in ruler units */
133 double increment; /* Number of pixels per unit */
134 gint scale; /* Number of units per major unit */
135 double subd_incr;
136 double start, end, cur;
137 gchar unit_str[32];
138 gint digit_height;
139 gint text_width;
140 gint pos;
142 g_return_if_fail (ruler != NULL);
143 g_return_if_fail (SP_IS_HRULER (ruler));
145 if (!GTK_WIDGET_DRAWABLE (ruler))
146 return;
148 widget = GTK_WIDGET (ruler);
150 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
151 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
153 pango_desc = widget->style->font_desc;
155 // Create the pango layout
156 pango_context = gtk_widget_get_pango_context (widget);
158 pango_layout = pango_layout_new (pango_context);
160 PangoFontDescription *fs = pango_font_description_new ();
161 pango_font_description_set_size (fs, RULER_FONT_SIZE);
162 pango_layout_set_font_description (pango_layout, fs);
163 pango_font_description_free (fs);
165 digit_height = (int) floor (RULER_FONT_SIZE * RULER_FONT_VERTICAL_SPACING / PANGO_SCALE + 0.5);
167 xthickness = widget->style->xthickness;
168 ythickness = widget->style->ythickness;
170 width = widget->allocation.width;
171 height = widget->allocation.height;// - ythickness * 2;
173 gtk_paint_box (widget->style, ruler->backing_store,
174 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
175 NULL, widget, "hruler",
176 0, 0,
177 widget->allocation.width, widget->allocation.height);
179 upper = ruler->upper / ruler->metric->pixels_per_unit;
180 lower = ruler->lower / ruler->metric->pixels_per_unit;
182 if ((upper - lower) == 0)
183 return;
184 increment = (double) width / (upper - lower);
186 /* determine the scale
187 * We calculate the text size as for the vruler instead of using
188 * text_width = gdk_string_width(font, unit_str), so that the result
189 * for the scale looks consistent with an accompanying vruler
190 */
191 scale = (int)(ceil (ruler->max_size / ruler->metric->pixels_per_unit));
192 sprintf (unit_str, "%d", scale);
193 text_width = strlen (unit_str) * digit_height + 1;
195 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
196 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
197 break;
199 if (scale == MAXIMUM_SCALES)
200 scale = MAXIMUM_SCALES - 1;
202 /* drawing starts here */
203 length = 0;
204 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
205 {
206 subd_incr = ruler->metric->ruler_scale[scale] /
207 ruler->metric->subdivide[i];
208 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
209 continue;
211 /* Calculate the length of the tickmarks. Make sure that
212 * this length increases for each set of ticks
213 */
214 ideal_length = height / (i + 1) - 1;
215 if (ideal_length > ++length)
216 length = ideal_length;
218 if (lower < upper)
219 {
220 start = floor (lower / subd_incr) * subd_incr;
221 end = ceil (upper / subd_incr) * subd_incr;
222 }
223 else
224 {
225 start = floor (upper / subd_incr) * subd_incr;
226 end = ceil (lower / subd_incr) * subd_incr;
227 }
229 tick_index = 0;
230 cur = start;
232 while (cur <= end)
233 {
234 pos = ROUND ((cur - lower) * increment);
236 gdk_draw_line (ruler->backing_store, gc,
237 pos, height + ythickness,
238 pos, height - length + ythickness);
240 /* draw label */
241 double label_spacing_px = (increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i];
242 if (i == 0 &&
243 (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
244 (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
245 {
246 if (fabs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
247 sprintf (unit_str, "%dk", ((int) cur)/1000);
248 else
249 sprintf (unit_str, "%d", (int) cur);
251 pango_layout_set_text (pango_layout, unit_str, -1);
253 gdk_draw_layout (ruler->backing_store, gc,
254 pos + 2, 0, pango_layout);
255 }
257 /* Calculate cur from start rather than incrementing by subd_incr
258 * in each iteration. This is to avoid propagation of floating point
259 * errors in subd_incr.
260 */
261 ++tick_index;
262 cur = start + (((double)tick_index) * (double)ruler->metric->ruler_scale[scale])/ ruler->metric->subdivide[i];
263 }
264 }
265 }
267 static void
268 sp_hruler_draw_pos (GtkRuler *ruler)
269 {
270 GtkWidget *widget;
271 GdkGC *gc;
272 int i;
273 gint x, y;
274 gint width, height;
275 gint bs_width, bs_height;
276 gint xthickness;
277 gint ythickness;
278 gfloat increment;
280 g_return_if_fail (ruler != NULL);
281 g_return_if_fail (SP_IS_HRULER (ruler));
283 if (GTK_WIDGET_DRAWABLE (ruler))
284 {
285 widget = GTK_WIDGET (ruler);
287 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
288 xthickness = widget->style->xthickness;
289 ythickness = widget->style->ythickness;
290 width = widget->allocation.width;
291 height = widget->allocation.height - ythickness * 2;
293 bs_width = height / 2;
294 bs_width |= 1; /* make sure it's odd */
295 bs_height = bs_width / 2 + 1;
297 if ((bs_width > 0) && (bs_height > 0))
298 {
299 /* If a backing store exists, restore the ruler */
300 if (ruler->backing_store && ruler->non_gr_exp_gc)
301 gdk_draw_pixmap (ruler->widget.window,
302 ruler->non_gr_exp_gc,
303 ruler->backing_store,
304 ruler->xsrc, ruler->ysrc,
305 ruler->xsrc, ruler->ysrc,
306 bs_width, bs_height);
308 increment = (gfloat) width / (ruler->upper - ruler->lower);
310 x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
311 y = (height + bs_height) / 2 + ythickness;
313 for (i = 0; i < bs_height; i++)
314 gdk_draw_line (widget->window, gc,
315 x + i, y + i,
316 x + bs_width - 1 - i, y + i);
319 ruler->xsrc = x;
320 ruler->ysrc = y;
321 }
322 }
323 }
329 // vruler
331 static void sp_vruler_class_init (SPVRulerClass *klass);
332 static void sp_vruler_init (SPVRuler *vruler);
333 static gint sp_vruler_motion_notify (GtkWidget *widget,
334 GdkEventMotion *event);
335 static void sp_vruler_draw_ticks (GtkRuler *ruler);
336 static void sp_vruler_draw_pos (GtkRuler *ruler);
339 GtkType
340 sp_vruler_get_type (void)
341 {
342 static GtkType vruler_type = 0;
344 if (!vruler_type)
345 {
346 static const GtkTypeInfo vruler_info =
347 {
348 "SPVRuler",
349 sizeof (SPVRuler),
350 sizeof (SPVRulerClass),
351 (GtkClassInitFunc) sp_vruler_class_init,
352 (GtkObjectInitFunc) sp_vruler_init,
353 /* reserved_1 */ NULL,
354 /* reserved_2 */ NULL,
355 (GtkClassInitFunc) NULL,
356 };
358 vruler_type = gtk_type_unique (gtk_ruler_get_type (), &vruler_info);
359 }
361 return vruler_type;
362 }
364 static void
365 sp_vruler_class_init (SPVRulerClass *klass)
366 {
367 GtkWidgetClass *widget_class;
368 GtkRulerClass *ruler_class;
370 widget_class = (GtkWidgetClass*) klass;
371 ruler_class = (GtkRulerClass*) klass;
373 widget_class->motion_notify_event = sp_vruler_motion_notify;
375 ruler_class->draw_ticks = sp_vruler_draw_ticks;
376 ruler_class->draw_pos = sp_vruler_draw_pos;
377 }
379 static void
380 sp_vruler_init (SPVRuler *vruler)
381 {
382 GtkWidget *widget;
384 widget = GTK_WIDGET (vruler);
385 widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
386 widget->requisition.height = widget->style->ythickness * 2 + 1;
387 }
389 GtkWidget*
390 sp_vruler_new (void)
391 {
392 return GTK_WIDGET (gtk_type_new (sp_vruler_get_type ()));
393 }
396 static gint
397 sp_vruler_motion_notify (GtkWidget *widget,
398 GdkEventMotion *event)
399 {
400 GtkRuler *ruler;
401 gint y;
403 g_return_val_if_fail (widget != NULL, FALSE);
404 g_return_val_if_fail (SP_IS_VRULER (widget), FALSE);
405 g_return_val_if_fail (event != NULL, FALSE);
407 ruler = GTK_RULER (widget);
409 y = (int)event->y;
411 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y) / widget->allocation.height;
413 /* Make sure the ruler has been allocated already */
414 if (ruler->backing_store != NULL)
415 gtk_ruler_draw_pos (ruler);
417 return FALSE;
418 }
420 static void
421 sp_vruler_draw_ticks (GtkRuler *ruler)
422 {
423 GtkWidget *widget;
424 GdkGC *gc, *bg_gc;
425 PangoFontDescription *pango_desc;
426 PangoContext *pango_context;
427 PangoLayout *pango_layout;
428 gint i, j, tick_index;
429 gint width, height;
430 gint xthickness;
431 gint ythickness;
432 gint length, ideal_length;
433 double lower, upper; /* Upper and lower limits, in ruler units */
434 double increment; /* Number of pixels per unit */
435 gint scale; /* Number of units per major unit */
436 double subd_incr;
437 double start, end, cur;
438 gchar unit_str[32];
439 gchar digit_str[2] = { '\0', '\0' };
440 gint digit_height;
441 gint text_height;
442 gint pos;
444 g_return_if_fail (ruler != NULL);
445 g_return_if_fail (SP_IS_VRULER (ruler));
447 if (!GTK_WIDGET_DRAWABLE (ruler))
448 return;
450 widget = GTK_WIDGET (ruler);
452 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
453 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
455 pango_desc = widget->style->font_desc;
457 // Create the pango layout
458 pango_context = gtk_widget_get_pango_context (widget);
460 pango_layout = pango_layout_new (pango_context);
462 PangoFontDescription *fs = pango_font_description_new ();
463 pango_font_description_set_size (fs, RULER_FONT_SIZE);
464 pango_layout_set_font_description (pango_layout, fs);
465 pango_font_description_free (fs);
467 digit_height = (int) floor (RULER_FONT_SIZE * RULER_FONT_VERTICAL_SPACING / PANGO_SCALE + 0.5);
469 xthickness = widget->style->xthickness;
470 ythickness = widget->style->ythickness;
472 width = widget->allocation.height;
473 height = widget->allocation.width;// - ythickness * 2;
475 gtk_paint_box (widget->style, ruler->backing_store,
476 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
477 NULL, widget, "vruler",
478 0, 0,
479 widget->allocation.width, widget->allocation.height);
481 upper = ruler->upper / ruler->metric->pixels_per_unit;
482 lower = ruler->lower / ruler->metric->pixels_per_unit;
484 if ((upper - lower) == 0)
485 return;
486 increment = (double) width / (upper - lower);
488 /* determine the scale
489 * use the maximum extents of the ruler to determine the largest
490 * possible number to be displayed. Calculate the height in pixels
491 * of this displayed text. Use this height to find a scale which
492 * leaves sufficient room for drawing the ruler.
493 */
494 scale = (int)ceil (ruler->max_size / ruler->metric->pixels_per_unit);
495 sprintf (unit_str, "%d", scale);
496 text_height = strlen (unit_str) * digit_height + 1;
498 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
499 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_height)
500 break;
502 if (scale == MAXIMUM_SCALES)
503 scale = MAXIMUM_SCALES - 1;
505 /* drawing starts here */
506 length = 0;
507 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--) {
508 subd_incr = (double) ruler->metric->ruler_scale[scale] /
509 (double) ruler->metric->subdivide[i];
510 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
511 continue;
513 /* Calculate the length of the tickmarks. Make sure that
514 * this length increases for each set of ticks
515 */
516 ideal_length = height / (i + 1) - 1;
517 if (ideal_length > ++length)
518 length = ideal_length;
520 if (lower < upper)
521 {
522 start = floor (lower / subd_incr) * subd_incr;
523 end = ceil (upper / subd_incr) * subd_incr;
524 }
525 else
526 {
527 start = floor (upper / subd_incr) * subd_incr;
528 end = ceil (lower / subd_incr) * subd_incr;
529 }
531 tick_index = 0;
532 cur = start;
534 while (cur < end) {
535 pos = ROUND ((cur - lower) * increment);
537 gdk_draw_line (ruler->backing_store, gc,
538 height + xthickness - length, pos,
539 height + xthickness, pos);
541 /* draw label */
542 double label_spacing_px = fabs((increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i]);
543 if (i == 0 &&
544 (label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
545 (label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
546 {
547 if (fabs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
548 sprintf (unit_str, "%dk", ((int) cur)/1000);
549 else
550 sprintf (unit_str, "%d", (int) cur);
551 for (j = 0; j < (int) strlen (unit_str); j++)
552 {
553 digit_str[0] = unit_str[j];
555 pango_layout_set_text (pango_layout, digit_str, 1);
557 gdk_draw_layout (ruler->backing_store, gc,
558 xthickness + 1,
559 pos + digit_height * (j) + 1,
560 pango_layout);
561 }
562 }
564 /* Calculate cur from start rather than incrementing by subd_incr
565 * in each iteration. This is to avoid propagation of floating point
566 * errors in subd_incr.
567 */
568 ++tick_index;
569 cur = start + (((double)tick_index) * (double)ruler->metric->ruler_scale[scale])/ ruler->metric->subdivide[i];
570 }
571 }
572 }
574 static void
575 sp_vruler_draw_pos (GtkRuler *ruler)
576 {
577 GtkWidget *widget;
578 GdkGC *gc;
579 int i;
580 gint x, y;
581 gint width, height;
582 gint bs_width, bs_height;
583 gint xthickness;
584 gint ythickness;
585 gfloat increment;
587 g_return_if_fail (ruler != NULL);
588 g_return_if_fail (SP_IS_VRULER (ruler));
590 if (GTK_WIDGET_DRAWABLE (ruler))
591 {
592 widget = GTK_WIDGET (ruler);
594 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
595 xthickness = widget->style->xthickness;
596 ythickness = widget->style->ythickness;
597 width = widget->allocation.width - xthickness * 2;
598 height = widget->allocation.height;
600 bs_height = width / 2;
601 bs_height |= 1; /* make sure it's odd */
602 bs_width = bs_height / 2 + 1;
604 if ((bs_width > 0) && (bs_height > 0))
605 {
606 /* If a backing store exists, restore the ruler */
607 if (ruler->backing_store && ruler->non_gr_exp_gc)
608 gdk_draw_pixmap (ruler->widget.window,
609 ruler->non_gr_exp_gc,
610 ruler->backing_store,
611 ruler->xsrc, ruler->ysrc,
612 ruler->xsrc, ruler->ysrc,
613 bs_width, bs_height);
615 increment = (gfloat) height / (ruler->upper - ruler->lower);
617 x = (width + bs_width) / 2 + xthickness;
618 y = ROUND ((ruler->position - ruler->lower) * increment) + (ythickness - bs_height) / 2 - 1;
620 for (i = 0; i < bs_width; i++)
621 gdk_draw_line (widget->window, gc,
622 x + i, y + i,
623 x + i, y + bs_height - 1 - i);
625 ruler->xsrc = x;
626 ruler->ysrc = y;
627 }
628 }
629 }
631 /// Ruler metrics.
632 static GtkRulerMetric const sp_ruler_metrics[] = {
633 // NOTE: the order of records in this struct must correspond to the SPMetric enum.
634 {"NONE", "", 1, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
635 {"millimeters", "mm", PX_PER_MM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
636 {"centimeters", "cm", PX_PER_CM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
637 {"inches", "in", PX_PER_IN, { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }, { 1, 2, 4, 8, 16 }},
638 {"points", "pt", PX_PER_PT, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
639 {"pixels", "px", PX_PER_PX, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
640 {"meters", "m", PX_PER_M, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
641 };
643 void
644 sp_ruler_set_metric (GtkRuler *ruler,
645 SPMetric metric)
646 {
647 g_return_if_fail (ruler != NULL);
648 g_return_if_fail (GTK_IS_RULER (ruler));
649 g_return_if_fail((unsigned) metric < G_N_ELEMENTS(sp_ruler_metrics));
651 if (metric == 0)
652 return;
654 ruler->metric = const_cast<GtkRulerMetric *>(&sp_ruler_metrics[metric]);
656 if (GTK_WIDGET_DRAWABLE (ruler))
657 gtk_widget_queue_draw (GTK_WIDGET (ruler));
658 }