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 "gtkhruler.h"
33 #define RULER_HEIGHT 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_hruler_class_init (GtkHRulerClass *klass);
42 static void gtk_hruler_init (GtkHRuler *hruler);
43 static gint gtk_hruler_motion_notify (GtkWidget *widget,
44 GdkEventMotion *event);
45 static void gtk_hruler_draw_ticks (GtkRuler *ruler);
46 static void gtk_hruler_draw_pos (GtkRuler *ruler);
50 gtk_hruler_get_type (void)
52 static guint hruler_type = 0;
56 static const GtkTypeInfo hruler_info =
60 sizeof (GtkHRulerClass),
61 (GtkClassInitFunc) gtk_hruler_class_init,
62 (GtkObjectInitFunc) gtk_hruler_init,
63 /* reserved_1 */ NULL,
64 /* reserved_2 */ NULL,
65 (GtkClassInitFunc) NULL,
68 hruler_type = gtk_type_unique (gtk_ruler_get_type (), &hruler_info);
75 gtk_hruler_class_init (GtkHRulerClass *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_hruler_motion_notify;
85 ruler_class->draw_ticks = gtk_hruler_draw_ticks;
86 ruler_class->draw_pos = gtk_hruler_draw_pos;
90 gtk_hruler_init (GtkHRuler *hruler)
94 widget = GTK_WIDGET (hruler);
95 widget->requisition.width = widget->style->klass->xthickness * 2 + 1;
96 widget->requisition.height = widget->style->klass->ythickness * 2 + RULER_HEIGHT;
101 gtk_hruler_new (void)
103 return GTK_WIDGET (gtk_type_new (gtk_hruler_get_type ()));
107 gtk_hruler_motion_notify (GtkWidget *widget,
108 GdkEventMotion *event)
113 g_return_val_if_fail (widget != NULL, FALSE);
114 g_return_val_if_fail (GTK_IS_HRULER (widget), FALSE);
115 g_return_val_if_fail (event != NULL, FALSE);
117 ruler = GTK_RULER (widget);
120 gdk_window_get_pointer (widget->window, &x, NULL, NULL);
124 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
126 /* Make sure the ruler has been allocated already */
127 if (ruler->backing_store != NULL)
128 gtk_ruler_draw_pos (ruler);
134 gtk_hruler_draw_ticks (GtkRuler *ruler)
143 gint length, ideal_length;
144 gfloat lower, upper; /* Upper and lower limits, in ruler units */
145 gfloat increment; /* Number of pixels per unit */
146 gint scale; /* Number of units per major unit */
148 gfloat start, end, cur;
154 g_return_if_fail (ruler != NULL);
155 g_return_if_fail (GTK_IS_HRULER (ruler));
157 if (!GTK_WIDGET_DRAWABLE (ruler))
160 widget = GTK_WIDGET (ruler);
162 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
163 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
164 font = widget->style->font;
166 xthickness = widget->style->klass->xthickness;
167 ythickness = widget->style->klass->ythickness;
168 digit_height = font->ascent; /* assume descent == 0 ? */
170 width = widget->allocation.width;
171 height = widget->allocation.height - ythickness * 2;
174 gtk_paint_box (widget->style, ruler->backing_store,
175 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
176 NULL, widget, "hruler",
178 widget->allocation.width, widget->allocation.height);
181 gdk_draw_line (ruler->backing_store, gc,
184 widget->allocation.width - xthickness,
185 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 = (gfloat) width / (upper - lower);
194 /* determine the scale
195 * We calculate the text size as for the vruler instead of using
196 * text_width = gdk_string_width(font, unit_str), so that the result
197 * for the scale looks consistent with an accompanying vruler
199 scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
200 sprintf (unit_str, "%d", scale);
201 text_width = strlen (unit_str) * digit_height + 1;
203 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
204 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
207 if (scale == MAXIMUM_SCALES)
208 scale = MAXIMUM_SCALES - 1;
210 /* drawing starts here */
212 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
214 subd_incr = (gfloat) ruler->metric->ruler_scale[scale] /
215 (gfloat) ruler->metric->subdivide[i];
216 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
219 /* Calculate the length of the tickmarks. Make sure that
220 * this length increases for each set of ticks
222 ideal_length = height / (i + 1) - 1;
223 if (ideal_length > ++length)
224 length = ideal_length;
228 start = floor (lower / subd_incr) * subd_incr;
229 end = ceil (upper / subd_incr) * subd_incr;
233 start = floor (upper / subd_incr) * subd_incr;
234 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 pos, height + ythickness,
244 pos, height - length + ythickness);
249 sprintf (unit_str, "%d", (int) cur);
250 gdk_draw_string (ruler->backing_store, font, gc,
251 pos + 2, ythickness + font->ascent - 1,
259 gtk_hruler_draw_pos (GtkRuler *ruler)
266 gint bs_width, bs_height;
271 g_return_if_fail (ruler != NULL);
272 g_return_if_fail (GTK_IS_HRULER (ruler));
274 if (GTK_WIDGET_DRAWABLE (ruler))
276 widget = GTK_WIDGET (ruler);
278 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
279 xthickness = widget->style->klass->xthickness;
280 ythickness = widget->style->klass->ythickness;
281 width = widget->allocation.width;
282 height = widget->allocation.height - ythickness * 2;
284 bs_width = height / 2;
285 bs_width |= 1; /* make sure it's odd */
286 bs_height = bs_width / 2 + 1;
288 if ((bs_width > 0) && (bs_height > 0))
290 /* If a backing store exists, restore the ruler */
291 if (ruler->backing_store && ruler->non_gr_exp_gc)
292 gdk_draw_pixmap (ruler->widget.window,
293 ruler->non_gr_exp_gc,
294 ruler->backing_store,
295 ruler->xsrc, ruler->ysrc,
296 ruler->xsrc, ruler->ysrc,
297 bs_width, bs_height);
299 increment = (gfloat) width / (ruler->upper - ruler->lower);
301 x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
302 y = (height + bs_height) / 2 + ythickness;
304 for (i = 0; i < bs_height; i++)
305 gdk_draw_line (widget->window, gc,
307 x + bs_width - 1 - i, y + i);