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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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"
33 #define RULER_WIDTH 14
34 #define MINIMUM_INCR 5
35 #define MAXIMUM_SUBDIVIDE 5
36 #define MAXIMUM_SCALES 10
38 #define ROUND(x) ((int) ((x) + 0.5))
41 static void gtk_vruler_class_init (GtkVRulerClass *klass);
42 static void gtk_vruler_init (GtkVRuler *vruler);
43 static gint gtk_vruler_motion_notify (GtkWidget *widget,
44 GdkEventMotion *event);
45 static void gtk_vruler_draw_ticks (GtkRuler *ruler);
46 static void gtk_vruler_draw_pos (GtkRuler *ruler);
50 gtk_vruler_get_type (void)
52 static GtkType vruler_type = 0;
56 static const GtkTypeInfo vruler_info =
60 sizeof (GtkVRulerClass),
61 (GtkClassInitFunc) gtk_vruler_class_init,
62 (GtkObjectInitFunc) gtk_vruler_init,
63 /* reserved_1 */ NULL,
64 /* reserved_2 */ NULL,
65 (GtkClassInitFunc) NULL,
68 vruler_type = gtk_type_unique (GTK_TYPE_RULER, &vruler_info);
75 gtk_vruler_class_init (GtkVRulerClass *klass)
77 GtkWidgetClass *widget_class;
78 GtkRulerClass *ruler_class;
80 widget_class = (GtkWidgetClass*) klass;
81 ruler_class = (GtkRulerClass*) klass;
83 widget_class->motion_notify_event = gtk_vruler_motion_notify;
85 ruler_class->draw_ticks = gtk_vruler_draw_ticks;
86 ruler_class->draw_pos = gtk_vruler_draw_pos;
90 gtk_vruler_init (GtkVRuler *vruler)
94 widget = GTK_WIDGET (vruler);
95 widget->requisition.width = widget->style->klass->xthickness * 2 + RULER_WIDTH;
96 widget->requisition.height = widget->style->klass->ythickness * 2 + 1;
100 gtk_vruler_new (void)
102 return GTK_WIDGET (gtk_type_new (GTK_TYPE_VRULER));
107 gtk_vruler_motion_notify (GtkWidget *widget,
108 GdkEventMotion *event)
113 g_return_val_if_fail (widget != NULL, FALSE);
114 g_return_val_if_fail (GTK_IS_VRULER (widget), FALSE);
115 g_return_val_if_fail (event != NULL, FALSE);
117 ruler = GTK_RULER (widget);
120 gdk_window_get_pointer (widget->window, NULL, &y, NULL);
124 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y) / widget->allocation.height;
126 /* Make sure the ruler has been allocated already */
127 if (ruler->backing_store != NULL)
128 gtk_ruler_draw_pos (ruler);
134 gtk_vruler_draw_ticks (GtkRuler *ruler)
142 gint length, ideal_length;
143 gfloat lower, upper; /* Upper and lower limits, in ruler units */
144 gfloat increment; /* Number of pixels per unit */
145 gint scale; /* Number of units per major unit */
147 gfloat start, end, cur;
154 PangoRectangle logical_rect, ink_rect;
156 g_return_if_fail (ruler != NULL);
157 g_return_if_fail (GTK_IS_VRULER (ruler));
159 if (!GTK_WIDGET_DRAWABLE (ruler))
162 widget = GTK_WIDGET (ruler);
164 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
165 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
166 xthickness = widget->style->klass->xthickness;
167 ythickness = widget->style->klass->ythickness;
169 layout = gtk_widget_create_pango_layout (widget);
170 pango_layout_set_text (layout, "012456789", -1);
171 pango_layout_get_extents (layout, &ink_rect, &logical_rect);
173 digit_height = ink_rect.height / PANGO_SCALE + 2;
174 digit_offset = ink_rect.y;
176 width = widget->allocation.height;
177 height = widget->allocation.width - ythickness * 2;
179 gtk_paint_box (widget->style, ruler->backing_store,
180 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
181 NULL, widget, "vruler",
183 widget->allocation.width, widget->allocation.height);
185 gdk_draw_line (ruler->backing_store, gc,
189 widget->allocation.height - ythickness);
191 upper = ruler->upper / ruler->metric->pixels_per_unit;
192 lower = ruler->lower / ruler->metric->pixels_per_unit;
194 if ((upper - lower) == 0)
196 increment = (gfloat) width / (upper - lower);
198 /* determine the scale
199 * use the maximum extents of the ruler to determine the largest
200 * possible number to be displayed. Calculate the height in pixels
201 * of this displayed text. Use this height to find a scale which
202 * leaves sufficient room for drawing the ruler.
204 scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
205 sprintf (unit_str, "%d", scale);
206 text_height = strlen (unit_str) * digit_height + 1;
208 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
209 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_height)
212 if (scale == MAXIMUM_SCALES)
213 scale = MAXIMUM_SCALES - 1;
215 /* drawing starts here */
217 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
219 subd_incr = (gfloat) ruler->metric->ruler_scale[scale] /
220 (gfloat) ruler->metric->subdivide[i];
221 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
224 /* Calculate the length of the tickmarks. Make sure that
225 * this length increases for each set of ticks
227 ideal_length = height / (i + 1) - 1;
228 if (ideal_length > ++length)
229 length = ideal_length;
233 start = floor (lower / subd_incr) * subd_incr;
234 end = ceil (upper / subd_incr) * subd_incr;
238 start = floor (upper / subd_incr) * subd_incr;
239 end = ceil (lower / subd_incr) * subd_incr;
242 for (cur = start; cur <= end; cur += subd_incr)
244 pos = ROUND ((cur - lower) * increment);
246 gdk_draw_line (ruler->backing_store, gc,
247 height + xthickness - length, pos,
248 height + xthickness, pos);
253 sprintf (unit_str, "%d", (int) cur);
255 for (j = 0; j < (int) strlen (unit_str); j++)
257 pango_layout_set_text (layout, unit_str + j, 1);
258 pango_layout_get_extents (layout, NULL, &logical_rect);
260 gdk_draw_layout (ruler->backing_store, gc,
262 pos + digit_height * j + 2 + (logical_rect.y - digit_offset) / PANGO_SCALE,
269 pango_layout_unref (layout);
274 gtk_vruler_draw_pos (GtkRuler *ruler)
281 gint bs_width, bs_height;
286 g_return_if_fail (ruler != NULL);
287 g_return_if_fail (GTK_IS_VRULER (ruler));
289 if (GTK_WIDGET_DRAWABLE (ruler))
291 widget = GTK_WIDGET (ruler);
293 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
294 xthickness = widget->style->klass->xthickness;
295 ythickness = widget->style->klass->ythickness;
296 width = widget->allocation.width - xthickness * 2;
297 height = widget->allocation.height;
299 bs_height = width / 2;
300 bs_height |= 1; /* make sure it's odd */
301 bs_width = bs_height / 2 + 1;
303 if ((bs_width > 0) && (bs_height > 0))
305 /* If a backing store exists, restore the ruler */
306 if (ruler->backing_store && ruler->non_gr_exp_gc)
307 gdk_draw_pixmap (ruler->widget.window,
308 ruler->non_gr_exp_gc,
309 ruler->backing_store,
310 ruler->xsrc, ruler->ysrc,
311 ruler->xsrc, ruler->ysrc,
312 bs_width, bs_height);
314 increment = (gfloat) height / (ruler->upper - ruler->lower);
316 x = (width + bs_width) / 2 + xthickness;
317 y = ROUND ((ruler->position - ruler->lower) * increment) + (ythickness - bs_height) / 2 - 1;
319 for (i = 0; i < bs_width; i++)
320 gdk_draw_line (widget->window, gc,
322 x + i, y + bs_height - 1 - i);