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"
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 GType vruler_type = 0;
56 static const GTypeInfo vruler_info =
58 sizeof (GtkVRulerClass),
60 NULL, /* base_finalize */
61 (GClassInitFunc) gtk_vruler_class_init,
62 NULL, /* class_finalize */
63 NULL, /* class_data */
66 (GInstanceInitFunc) gtk_vruler_init,
69 vruler_type = g_type_register_static (GTK_TYPE_RULER, "GtkVRuler",
77 gtk_vruler_class_init (GtkVRulerClass *klass)
79 GtkWidgetClass *widget_class;
80 GtkRulerClass *ruler_class;
82 widget_class = (GtkWidgetClass*) klass;
83 ruler_class = (GtkRulerClass*) klass;
85 widget_class->motion_notify_event = gtk_vruler_motion_notify;
87 ruler_class->draw_ticks = gtk_vruler_draw_ticks;
88 ruler_class->draw_pos = gtk_vruler_draw_pos;
92 gtk_vruler_init (GtkVRuler *vruler)
96 widget = GTK_WIDGET (vruler);
97 widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
98 widget->requisition.height = widget->style->ythickness * 2 + 1;
102 gtk_vruler_new (void)
104 return g_object_new (GTK_TYPE_VRULER, NULL);
109 gtk_vruler_motion_notify (GtkWidget *widget,
110 GdkEventMotion *event)
115 ruler = GTK_RULER (widget);
118 gdk_window_get_pointer (widget->window, NULL, &y, NULL);
122 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y) / widget->allocation.height;
123 g_object_notify (G_OBJECT (ruler), "position");
125 /* Make sure the ruler has been allocated already */
126 if (ruler->backing_store != NULL)
127 gtk_ruler_draw_pos (ruler);
133 gtk_vruler_draw_ticks (GtkRuler *ruler)
141 gint length, ideal_length;
142 gdouble lower, upper; /* Upper and lower limits, in ruler units */
143 gdouble increment; /* Number of pixels per unit */
144 gint scale; /* Number of units per major unit */
146 gdouble start, end, cur;
153 PangoRectangle logical_rect, ink_rect;
155 if (!GTK_WIDGET_DRAWABLE (ruler))
158 widget = GTK_WIDGET (ruler);
160 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
161 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
163 xthickness = widget->style->xthickness;
164 ythickness = widget->style->ythickness;
166 layout = gtk_widget_create_pango_layout (widget, "012456789");
167 pango_layout_get_extents (layout, &ink_rect, &logical_rect);
169 digit_height = PANGO_PIXELS (ink_rect.height) + 2;
170 digit_offset = ink_rect.y;
172 width = widget->allocation.height;
173 height = widget->allocation.width - ythickness * 2;
175 gtk_paint_box (widget->style, ruler->backing_store,
176 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
177 NULL, widget, "vruler",
179 widget->allocation.width, widget->allocation.height);
181 gdk_draw_line (ruler->backing_store, gc,
185 widget->allocation.height - ythickness);
187 upper = ruler->upper / ruler->metric->pixels_per_unit;
188 lower = ruler->lower / ruler->metric->pixels_per_unit;
190 if ((upper - lower) == 0)
192 increment = (gdouble) width / (upper - lower);
194 /* determine the scale
195 * use the maximum extents of the ruler to determine the largest
196 * possible number to be displayed. Calculate the height in pixels
197 * of this displayed text. Use this height to find a scale which
198 * leaves sufficient room for drawing the ruler.
200 scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
201 sprintf (unit_str, "%d", scale);
202 text_height = 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_height)
208 if (scale == MAXIMUM_SCALES)
209 scale = MAXIMUM_SCALES - 1;
211 /* drawing starts here */
213 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
215 subd_incr = (gdouble) ruler->metric->ruler_scale[scale] /
216 (gdouble) ruler->metric->subdivide[i];
217 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
220 /* Calculate the length of the tickmarks. Make sure that
221 * this length increases for each set of ticks
223 ideal_length = height / (i + 1) - 1;
224 if (ideal_length > ++length)
225 length = ideal_length;
229 start = floor (lower / subd_incr) * subd_incr;
230 end = ceil (upper / subd_incr) * subd_incr;
234 start = floor (upper / subd_incr) * subd_incr;
235 end = ceil (lower / subd_incr) * subd_incr;
238 for (cur = start; cur <= end; cur += subd_incr)
240 pos = ROUND ((cur - lower) * increment);
242 gdk_draw_line (ruler->backing_store, gc,
243 height + xthickness - length, pos,
244 height + xthickness, pos);
249 sprintf (unit_str, "%d", (int) cur);
251 for (j = 0; j < (int) strlen (unit_str); j++)
253 pango_layout_set_text (layout, unit_str + j, 1);
254 pango_layout_get_extents (layout, NULL, &logical_rect);
257 gtk_paint_layout (widget->style,
258 ruler->backing_store,
259 GTK_WIDGET_STATE (widget),
265 pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset),
272 g_object_unref (layout);
277 gtk_vruler_draw_pos (GtkRuler *ruler)
284 gint bs_width, bs_height;
289 if (GTK_WIDGET_DRAWABLE (ruler))
291 widget = GTK_WIDGET (ruler);
293 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
294 xthickness = widget->style->xthickness;
295 ythickness = widget->style->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_drawable (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 = (gdouble) 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);