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 "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 GtkType 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_TYPE_RULER, &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->xthickness * 2 + 1;
96 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
101 gtk_hruler_new (void)
103 return GTK_WIDGET (gtk_type_new (GTK_TYPE_HRULER));
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;
155 PangoRectangle logical_rect, ink_rect;
157 g_return_if_fail (ruler != NULL);
158 g_return_if_fail (GTK_IS_HRULER (ruler));
160 if (!GTK_WIDGET_DRAWABLE (ruler))
163 widget = GTK_WIDGET (ruler);
165 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
166 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
167 font = widget->style->font;
169 xthickness = widget->style->xthickness;
170 ythickness = widget->style->ythickness;
172 digit_height = PANGO_PIXELS (ink_rect.height) + 2;
173 digit_offset = ink_rect.y;
175 layout = gtk_widget_create_pango_layout (widget, "012456789");
176 pango_layout_get_extents (layout, &ink_rect, &logical_rect);
178 digit_height = PANGO_PIXELS (ink_rect.height) + 1;
179 digit_offset = ink_rect.y;
181 width = widget->allocation.width;
182 height = widget->allocation.height - ythickness * 2;
184 gtk_paint_box (widget->style, ruler->backing_store,
185 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
186 NULL, widget, "hruler",
188 widget->allocation.width, widget->allocation.height);
191 gdk_draw_line (ruler->backing_store, gc,
194 widget->allocation.width - xthickness,
195 height + ythickness);
197 upper = ruler->upper / ruler->metric->pixels_per_unit;
198 lower = ruler->lower / ruler->metric->pixels_per_unit;
200 if ((upper - lower) == 0)
202 increment = (gfloat) width / (upper - lower);
204 /* determine the scale
205 * We calculate the text size as for the vruler instead of using
206 * text_width = gdk_string_width(font, unit_str), so that the result
207 * for the scale looks consistent with an accompanying vruler
209 scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
210 sprintf (unit_str, "%d", scale);
211 text_width = strlen (unit_str) * digit_height + 1;
213 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
214 if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
217 if (scale == MAXIMUM_SCALES)
218 scale = MAXIMUM_SCALES - 1;
220 /* drawing starts here */
222 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
224 subd_incr = (gfloat) ruler->metric->ruler_scale[scale] /
225 (gfloat) ruler->metric->subdivide[i];
226 if (subd_incr * fabs(increment) <= MINIMUM_INCR)
229 /* Calculate the length of the tickmarks. Make sure that
230 * this length increases for each set of ticks
232 ideal_length = height / (i + 1) - 1;
233 if (ideal_length > ++length)
234 length = ideal_length;
238 start = floor (lower / subd_incr) * subd_incr;
239 end = ceil (upper / subd_incr) * subd_incr;
243 start = floor (upper / subd_incr) * subd_incr;
244 end = ceil (lower / subd_incr) * subd_incr;
248 for (cur = start; cur <= end; cur += subd_incr)
250 pos = ROUND ((cur - lower) * increment);
252 gdk_draw_line (ruler->backing_store, gc,
253 pos, height + ythickness,
254 pos, height - length + ythickness);
259 sprintf (unit_str, "%d", (int) cur);
261 pango_layout_set_text (layout, unit_str, -1);
262 pango_layout_get_extents (layout, &logical_rect, NULL);
264 gdk_draw_layout (ruler->backing_store, gc,
265 pos + 2, ythickness + PANGO_PIXELS (logical_rect.y - digit_offset),
271 g_object_unref (G_OBJECT (layout));
275 gtk_hruler_draw_pos (GtkRuler *ruler)
282 gint bs_width, bs_height;
287 g_return_if_fail (ruler != NULL);
288 g_return_if_fail (GTK_IS_HRULER (ruler));
290 if (GTK_WIDGET_DRAWABLE (ruler))
292 widget = GTK_WIDGET (ruler);
294 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
295 xthickness = widget->style->xthickness;
296 ythickness = widget->style->ythickness;
297 width = widget->allocation.width;
298 height = widget->allocation.height - ythickness * 2;
300 bs_width = height / 2;
301 bs_width |= 1; /* make sure it's odd */
302 bs_height = bs_width / 2 + 1;
304 if ((bs_width > 0) && (bs_height > 0))
306 /* If a backing store exists, restore the ruler */
307 if (ruler->backing_store && ruler->non_gr_exp_gc)
308 gdk_draw_pixmap (ruler->widget.window,
309 ruler->non_gr_exp_gc,
310 ruler->backing_store,
311 ruler->xsrc, ruler->ysrc,
312 ruler->xsrc, ruler->ysrc,
313 bs_width, bs_height);
315 increment = (gfloat) width / (ruler->upper - ruler->lower);
317 x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
318 y = (height + bs_height) / 2 + ythickness;
320 for (i = 0; i < bs_height; i++)
321 gdk_draw_line (widget->window, gc,
323 x + bs_width - 1 - i, y + i);