1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkvruler.h"
34 #include <glib/gprintf.h>
37 #define RULER_WIDTH 14
38 #define MINIMUM_INCR 5
39 #define MAXIMUM_SUBDIVIDE 5
40 #define MAXIMUM_SCALES 10
42 #define ROUND(x) ((int) ((x) + 0.5))
45 static gint gtk_vruler_motion_notify (GtkWidget *widget,
46 GdkEventMotion *event);
47 static void gtk_vruler_draw_ticks (GtkRuler *ruler);
48 static void gtk_vruler_draw_pos (GtkRuler *ruler);
50 G_DEFINE_TYPE (GtkVRuler, gtk_vruler, GTK_TYPE_RULER)
53 gtk_vruler_class_init (GtkVRulerClass *klass)
55 GtkWidgetClass *widget_class;
56 GtkRulerClass *ruler_class;
58 widget_class = (GtkWidgetClass*) klass;
59 ruler_class = (GtkRulerClass*) klass;
61 widget_class->motion_notify_event = gtk_vruler_motion_notify;
63 ruler_class->draw_ticks = gtk_vruler_draw_ticks;
64 ruler_class->draw_pos = gtk_vruler_draw_pos;
68 gtk_vruler_init (GtkVRuler *vruler)
72 widget = GTK_WIDGET (vruler);
73 widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
74 widget->requisition.height = widget->style->ythickness * 2 + 1;
80 return g_object_new (GTK_TYPE_VRULER, NULL);
85 gtk_vruler_motion_notify (GtkWidget *widget,
86 GdkEventMotion *event)
91 ruler = GTK_RULER (widget);
93 gdk_event_request_motions (event);
96 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y) / widget->allocation.height;
97 g_object_notify (G_OBJECT (ruler), "position");
99 /* Make sure the ruler has been allocated already */
100 if (ruler->backing_store != NULL)
101 gtk_ruler_draw_pos (ruler);
107 gtk_vruler_draw_ticks (GtkRuler *ruler)
115 gint length, ideal_length;
116 gdouble lower, upper; /* Upper and lower limits, in ruler units */
117 gdouble increment; /* Number of pixels per unit */
118 gint scale; /* Number of units per major unit */
120 gdouble start, end, cur;
127 PangoRectangle logical_rect, ink_rect;
129 if (!GTK_WIDGET_DRAWABLE (ruler))
132 widget = GTK_WIDGET (ruler);
134 xthickness = widget->style->xthickness;
135 ythickness = widget->style->ythickness;
137 layout = gtk_widget_create_pango_layout (widget, "012456789");
138 pango_layout_get_extents (layout, &ink_rect, &logical_rect);
140 digit_height = PANGO_PIXELS (ink_rect.height) + 2;
141 digit_offset = ink_rect.y;
143 width = widget->allocation.height;
144 height = widget->allocation.width - ythickness * 2;
146 gtk_paint_box (widget->style, ruler->backing_store,
147 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
148 NULL, widget, "vruler",
150 widget->allocation.width, widget->allocation.height);
152 cr = gdk_cairo_create (ruler->backing_store);
153 gdk_cairo_set_source_color (cr, &widget->style->fg[widget->state]);
159 widget->allocation.height - 2 * ythickness);
161 upper = ruler->upper / ruler->metric->pixels_per_unit;
162 lower = ruler->lower / ruler->metric->pixels_per_unit;
164 if ((upper - lower) == 0)
167 increment = (gdouble) width / (upper - lower);
169 /* determine the scale
170 * use the maximum extents of the ruler to determine the largest
171 * possible number to be displayed. Calculate the height in pixels
172 * of this displayed text. Use this height to find a scale which
173 * leaves sufficient room for drawing the ruler.
175 scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
176 g_snprintf (unit_str, sizeof (unit_str), "%d", scale);
177 text_height = strlen (unit_str) * digit_height + 1;
179 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
180 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_height)
183 if (scale == MAXIMUM_SCALES)
184 scale = MAXIMUM_SCALES - 1;
186 /* drawing starts here */
188 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
190 subd_incr = (gdouble) ruler->metric->ruler_scale[scale] /
191 (gdouble) ruler->metric->subdivide[i];
192 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
195 /* Calculate the length of the tickmarks. Make sure that
196 * this length increases for each set of ticks
198 ideal_length = height / (i + 1) - 1;
199 if (ideal_length > ++length)
200 length = ideal_length;
204 start = floor (lower / subd_incr) * subd_incr;
205 end = ceil (upper / subd_incr) * subd_incr;
209 start = floor (upper / subd_incr) * subd_incr;
210 end = ceil (lower / subd_incr) * subd_incr;
213 for (cur = start; cur <= end; cur += subd_incr)
215 pos = ROUND ((cur - lower) * increment);
218 height + xthickness - length, pos,
224 g_snprintf (unit_str, sizeof (unit_str), "%d", (int) cur);
226 for (j = 0; j < (int) strlen (unit_str); j++)
228 pango_layout_set_text (layout, unit_str + j, 1);
229 pango_layout_get_extents (layout, NULL, &logical_rect);
232 gtk_paint_layout (widget->style,
233 ruler->backing_store,
234 GTK_WIDGET_STATE (widget),
240 pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset),
251 g_object_unref (layout);
256 gtk_vruler_draw_pos (GtkRuler *ruler)
258 GtkWidget *widget = GTK_WIDGET (ruler);
261 gint bs_width, bs_height;
266 if (GTK_WIDGET_DRAWABLE (ruler))
268 xthickness = widget->style->xthickness;
269 ythickness = widget->style->ythickness;
270 width = widget->allocation.width - xthickness * 2;
271 height = widget->allocation.height;
273 bs_height = width / 2 + 2;
274 bs_height |= 1; /* make sure it's odd */
275 bs_width = bs_height / 2 + 1;
277 if ((bs_width > 0) && (bs_height > 0))
279 cairo_t *cr = gdk_cairo_create (widget->window);
281 /* If a backing store exists, restore the ruler */
282 if (ruler->backing_store)
283 gdk_draw_drawable (widget->window,
284 widget->style->black_gc,
285 ruler->backing_store,
286 ruler->xsrc, ruler->ysrc,
287 ruler->xsrc, ruler->ysrc,
288 bs_width, bs_height);
290 increment = (gdouble) height / (ruler->upper - ruler->lower);
292 x = (width + bs_width) / 2 + xthickness;
293 y = ROUND ((ruler->position - ruler->lower) * increment) + (ythickness - bs_height) / 2 - 1;
295 gdk_cairo_set_source_color (cr, &widget->style->fg[widget->state]);
297 cairo_move_to (cr, x, y);
298 cairo_line_to (cr, x + bs_width, y + bs_height / 2.);
299 cairo_line_to (cr, x, y + bs_height);
310 #define __GTK_VRULER_C__
311 #include "gtkaliasdef.c"