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.
22 #include "gtkhruler.h"
25 #define RULER_HEIGHT 14
26 #define MINIMUM_INCR 5
27 #define MAXIMUM_SUBDIVIDE 5
28 #define MAXIMUM_SCALES 10
30 #define ROUND(x) ((int) ((x) + 0.5))
33 static void gtk_hruler_class_init (GtkHRulerClass *klass);
34 static void gtk_hruler_init (GtkHRuler *hruler);
35 static gint gtk_hruler_motion_notify (GtkWidget *widget,
36 GdkEventMotion *event);
37 static void gtk_hruler_draw_ticks (GtkRuler *ruler);
38 static void gtk_hruler_draw_pos (GtkRuler *ruler);
42 gtk_hruler_get_type ()
44 static guint hruler_type = 0;
48 GtkTypeInfo hruler_info =
52 sizeof (GtkHRulerClass),
53 (GtkClassInitFunc) gtk_hruler_class_init,
54 (GtkObjectInitFunc) gtk_hruler_init,
59 hruler_type = gtk_type_unique (gtk_ruler_get_type (), &hruler_info);
66 gtk_hruler_class_init (GtkHRulerClass *klass)
68 GtkWidgetClass *widget_class;
69 GtkRulerClass *ruler_class;
71 widget_class = (GtkWidgetClass*) klass;
72 ruler_class = (GtkRulerClass*) klass;
74 widget_class->motion_notify_event = gtk_hruler_motion_notify;
76 ruler_class->draw_ticks = gtk_hruler_draw_ticks;
77 ruler_class->draw_pos = gtk_hruler_draw_pos;
81 gtk_hruler_init (GtkHRuler *hruler)
85 widget = GTK_WIDGET (hruler);
86 widget->requisition.width = widget->style->klass->xthickness * 2 + 1;
87 widget->requisition.height = widget->style->klass->ythickness * 2 + RULER_HEIGHT;
94 return GTK_WIDGET (gtk_type_new (gtk_hruler_get_type ()));
98 gtk_hruler_motion_notify (GtkWidget *widget,
99 GdkEventMotion *event)
104 g_return_val_if_fail (widget != NULL, FALSE);
105 g_return_val_if_fail (GTK_IS_HRULER (widget), FALSE);
106 g_return_val_if_fail (event != NULL, FALSE);
108 ruler = GTK_RULER (widget);
111 gdk_window_get_pointer (widget->window, &x, NULL, NULL);
115 ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
117 /* Make sure the ruler has been allocated already */
118 if (ruler->backing_store != NULL)
119 gtk_ruler_draw_pos (ruler);
125 gtk_hruler_draw_ticks (GtkRuler *ruler)
134 gint length, ideal_length;
138 gfloat start, end, cur;
145 g_return_if_fail (ruler != NULL);
146 g_return_if_fail (GTK_IS_HRULER (ruler));
148 if (GTK_WIDGET_DRAWABLE (ruler))
150 widget = GTK_WIDGET (ruler);
152 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
153 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
154 font = widget->style->font;
156 xthickness = widget->style->klass->xthickness;
157 ythickness = widget->style->klass->ythickness;
158 digit_height = font->ascent; /* assume descent == 0? */
160 width = widget->allocation.width;
161 height = widget->allocation.height - ythickness * 2;
162 gdk_draw_line (ruler->backing_store, gc,
165 widget->allocation.width - xthickness,
166 height + ythickness);
168 if ((ruler->upper - ruler->lower) == 0)
171 increment = (gfloat) width * ruler->metric->pixels_per_unit / (ruler->upper - ruler->lower);
173 /* determine the scale
174 * use the maximum extents of the ruler to determine the largest possible
175 * number to be displayed. calculate the height in pixels of this displayed
176 * text as for the vertical ruler case. use this height to find a scale
177 * which leaves sufficient room for drawing the ruler.
179 scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
180 sprintf (unit_str, "%d", scale);
181 text_height = strlen (unit_str) * digit_height + 1;
183 for (scale = 0; scale < MAXIMUM_SCALES; scale++)
184 if (ruler->metric->ruler_scale[scale] * increment > 2 * text_height)
187 if (scale == MAXIMUM_SCALES)
188 scale = MAXIMUM_SCALES - 1;
191 for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
193 subd_incr = (gfloat) ruler->metric->ruler_scale[scale] / (gfloat) ruler->metric->subdivide[i];
194 step_incr = subd_incr * increment;
195 if (step_incr <= MINIMUM_INCR)
198 start = floor ((ruler->lower / ruler->metric->pixels_per_unit) / subd_incr) * subd_incr;
199 end = ceil ((ruler->upper / ruler->metric->pixels_per_unit) / subd_incr) * subd_incr;
201 ideal_length = height / (i + 1) - 1;
202 if (ideal_length > ++length)
203 length = ideal_length;
208 pos = ROUND ((cur - (ruler->lower / ruler->metric->pixels_per_unit)) * increment);
210 gdk_draw_line (ruler->backing_store, gc,
211 pos, height + ythickness, pos,
212 height - length + ythickness);
215 sprintf (unit_str, "%d", (int) cur);
216 gdk_draw_rectangle (ruler->backing_store,
219 gdk_string_width(font, unit_str) + 1,
221 gdk_draw_string (ruler->backing_store, font, gc,
222 pos + 2, ythickness + font->ascent - 1,
233 gtk_hruler_draw_pos (GtkRuler *ruler)
240 gint bs_width, bs_height;
245 g_return_if_fail (ruler != NULL);
246 g_return_if_fail (GTK_IS_HRULER (ruler));
248 if (GTK_WIDGET_DRAWABLE (ruler))
250 widget = GTK_WIDGET (ruler);
252 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
253 xthickness = widget->style->klass->xthickness;
254 ythickness = widget->style->klass->ythickness;
255 width = widget->allocation.width;
256 height = widget->allocation.height - ythickness * 2;
258 bs_width = height / 2;
259 bs_width |= 1; /* make sure it's odd */
260 bs_height = bs_width / 2 + 1;
262 if ((bs_width > 0) && (bs_height > 0))
264 /* If a backing store exists, restore the ruler */
265 if (ruler->backing_store && ruler->non_gr_exp_gc)
266 gdk_draw_pixmap (ruler->widget.window,
267 ruler->non_gr_exp_gc,
268 ruler->backing_store,
269 ruler->xsrc, ruler->ysrc,
270 ruler->xsrc, ruler->ysrc,
271 bs_width, bs_height);
273 increment = (gfloat) width / (ruler->upper - ruler->lower);
275 x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
276 y = (height + bs_height) / 2 + ythickness;
278 for (i = 0; i < bs_height; i++)
279 gdk_draw_line (widget->window, gc,
281 x + bs_width - 1 - i, y + i);