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/.
29 #include <glib/gprintf.h>
31 #include "gtkhruler.h"
34 #define RULER_HEIGHT 14
35 #define MINIMUM_INCR 5
36 #define MAXIMUM_SUBDIVIDE 5
37 #define MAXIMUM_SCALES 10
39 #define ROUND(x) ((int) ((x) + 0.5))
42 static void gtk_hruler_class_init (GtkHRulerClass *klass);
43 static void gtk_hruler_init (GtkHRuler *hruler);
44 static gint gtk_hruler_motion_notify (GtkWidget *widget,
45 GdkEventMotion *event);
46 static void gtk_hruler_draw_ticks (GtkRuler *ruler);
47 static void gtk_hruler_draw_pos (GtkRuler *ruler);
51 gtk_hruler_get_type (void)
53 static GType hruler_type = 0;
57 static const GTypeInfo hruler_info =
59 sizeof (GtkHRulerClass),
61 NULL, /* base_finalize */
62 (GClassInitFunc) gtk_hruler_class_init,
63 NULL, /* class_finalize */
64 NULL, /* class_data */
67 (GInstanceInitFunc) gtk_hruler_init,
70 hruler_type = g_type_register_static (GTK_TYPE_RULER, "GtkHRuler",
78 gtk_hruler_class_init (GtkHRulerClass *klass)
80 GtkWidgetClass *widget_class;
81 GtkRulerClass *ruler_class;
83 widget_class = (GtkWidgetClass*) klass;
84 ruler_class = (GtkRulerClass*) klass;
86 widget_class->motion_notify_event = gtk_hruler_motion_notify;
88 ruler_class->draw_ticks = gtk_hruler_draw_ticks;
89 ruler_class->draw_pos = gtk_hruler_draw_pos;
93 gtk_hruler_init (GtkHRuler *hruler)
97 widget = GTK_WIDGET (hruler);
98 widget->requisition.width = widget->style->xthickness * 2 + 1;
99 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
104 gtk_hruler_new (void)
106 return g_object_new (GTK_TYPE_HRULER, NULL);
110 gtk_hruler_motion_notify (GtkWidget *widget,
111 GdkEventMotion *event)
116 ruler = GTK_RULER (widget);
119 gdk_window_get_pointer (widget->window, &x, NULL, NULL);
123 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
124 g_object_notify (G_OBJECT (ruler), "position");
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)
142 gint length, ideal_length;
143 gdouble lower, upper; /* Upper and lower limits, in ruler units */
144 gdouble increment; /* Number of pixels per unit */
145 gint scale; /* Number of units per major unit */
147 gdouble start, end, cur;
154 PangoRectangle logical_rect, ink_rect;
156 if (!GTK_WIDGET_DRAWABLE (ruler))
159 widget = GTK_WIDGET (ruler);
161 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
162 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
164 xthickness = widget->style->xthickness;
165 ythickness = widget->style->ythickness;
167 digit_height = PANGO_PIXELS (ink_rect.height) + 2;
168 digit_offset = ink_rect.y;
170 layout = gtk_widget_create_pango_layout (widget, "012456789");
171 pango_layout_get_extents (layout, &ink_rect, &logical_rect);
173 digit_height = PANGO_PIXELS (ink_rect.height) + 1;
174 digit_offset = ink_rect.y;
176 width = widget->allocation.width;
177 height = widget->allocation.height - ythickness * 2;
179 gtk_paint_box (widget->style, ruler->backing_store,
180 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
181 NULL, widget, "hruler",
183 widget->allocation.width, widget->allocation.height);
186 gdk_draw_line (ruler->backing_store, gc,
189 widget->allocation.width - xthickness,
190 height + ythickness);
192 upper = ruler->upper / ruler->metric->pixels_per_unit;
193 lower = ruler->lower / ruler->metric->pixels_per_unit;
195 if ((upper - lower) == 0)
197 increment = (gdouble) width / (upper - lower);
199 /* determine the scale
200 * We calculate the text size as for the vruler instead of using
201 * text_width = gdk_string_width(font, unit_str), so that the result
202 * for the scale looks consistent with an accompanying vruler
204 scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
205 g_snprintf (unit_str, sizeof (unit_str), "%d", scale);
206 text_width = 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_width)
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 = (gdouble) ruler->metric->ruler_scale[scale] /
220 (gdouble) 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;
243 for (cur = start; cur <= end; cur += subd_incr)
245 pos = ROUND ((cur - lower) * increment);
247 gdk_draw_line (ruler->backing_store, gc,
248 pos, height + ythickness,
249 pos, height - length + ythickness);
254 g_snprintf (unit_str, sizeof (unit_str), "%d", (int) cur);
256 pango_layout_set_text (layout, unit_str, -1);
257 pango_layout_get_extents (layout, &logical_rect, NULL);
259 gtk_paint_layout (widget->style,
260 ruler->backing_store,
261 GTK_WIDGET_STATE (widget),
266 pos + 2, ythickness + PANGO_PIXELS (logical_rect.y - digit_offset),
272 g_object_unref (layout);
276 gtk_hruler_draw_pos (GtkRuler *ruler)
283 gint bs_width, bs_height;
288 if (GTK_WIDGET_DRAWABLE (ruler))
290 widget = GTK_WIDGET (ruler);
292 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
293 xthickness = widget->style->xthickness;
294 ythickness = widget->style->ythickness;
295 width = widget->allocation.width;
296 height = widget->allocation.height - ythickness * 2;
298 bs_width = height / 2;
299 bs_width |= 1; /* make sure it's odd */
300 bs_height = bs_width / 2 + 1;
302 if ((bs_width > 0) && (bs_height > 0))
304 /* If a backing store exists, restore the ruler */
305 if (ruler->backing_store && ruler->non_gr_exp_gc)
306 gdk_draw_drawable (ruler->widget.window,
307 ruler->non_gr_exp_gc,
308 ruler->backing_store,
309 ruler->xsrc, ruler->ysrc,
310 ruler->xsrc, ruler->ysrc,
311 bs_width, bs_height);
313 increment = (gdouble) width / (ruler->upper - ruler->lower);
315 x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
316 y = (height + bs_height) / 2 + ythickness;
318 for (i = 0; i < bs_height; i++)
319 gdk_draw_line (widget->window, gc,
321 x + bs_width - 1 - i, y + i);