]> Pileus Git - ~andy/gtk/blob - gtk/gtkhruler.c
31d6775a5b7a20ba35118d8e5556614f6ea105c2
[~andy/gtk] / gtk / gtkhruler.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <math.h>
28 #include <glib/gprintf.h>
29 #include <string.h>
30 #include "gtkhruler.h"
31
32
33 #define RULER_HEIGHT          14
34 #define MINIMUM_INCR          5
35 #define MAXIMUM_SUBDIVIDE     5
36 #define MAXIMUM_SCALES        10
37
38 #define ROUND(x) ((int) ((x) + 0.5))
39
40
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);
47
48
49 GType
50 gtk_hruler_get_type (void)
51 {
52   static GType hruler_type = 0;
53
54   if (!hruler_type)
55     {
56       static const GTypeInfo hruler_info =
57       {
58         sizeof (GtkHRulerClass),
59         NULL,           /* base_init */
60         NULL,           /* base_finalize */
61         (GClassInitFunc) gtk_hruler_class_init,
62         NULL,           /* class_finalize */
63         NULL,           /* class_data */
64         sizeof (GtkHRuler),
65         0,              /* n_preallocs */
66         (GInstanceInitFunc) gtk_hruler_init,
67       };
68
69       hruler_type = g_type_register_static (GTK_TYPE_RULER, "GtkHRuler",
70                                             &hruler_info, 0);
71     }
72
73   return hruler_type;
74 }
75
76 static void
77 gtk_hruler_class_init (GtkHRulerClass *klass)
78 {
79   GtkWidgetClass *widget_class;
80   GtkRulerClass *ruler_class;
81
82   widget_class = (GtkWidgetClass*) klass;
83   ruler_class = (GtkRulerClass*) klass;
84
85   widget_class->motion_notify_event = gtk_hruler_motion_notify;
86
87   ruler_class->draw_ticks = gtk_hruler_draw_ticks;
88   ruler_class->draw_pos = gtk_hruler_draw_pos;
89 }
90
91 static void
92 gtk_hruler_init (GtkHRuler *hruler)
93 {
94   GtkWidget *widget;
95
96   widget = GTK_WIDGET (hruler);
97   widget->requisition.width = widget->style->xthickness * 2 + 1;
98   widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
99 }
100
101
102 GtkWidget*
103 gtk_hruler_new (void)
104 {
105   return g_object_new (GTK_TYPE_HRULER, NULL);
106 }
107
108 static gint
109 gtk_hruler_motion_notify (GtkWidget      *widget,
110                           GdkEventMotion *event)
111 {
112   GtkRuler *ruler;
113   gint x;
114
115   ruler = GTK_RULER (widget);
116
117   if (event->is_hint)
118     gdk_window_get_pointer (widget->window, &x, NULL, NULL);
119   else
120     x = event->x;
121
122   ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
123   g_object_notify (G_OBJECT (ruler), "position");
124
125   /*  Make sure the ruler has been allocated already  */
126   if (ruler->backing_store != NULL)
127     gtk_ruler_draw_pos (ruler);
128
129   return FALSE;
130 }
131
132 static void
133 gtk_hruler_draw_ticks (GtkRuler *ruler)
134 {
135   GtkWidget *widget;
136   GdkGC *gc, *bg_gc;
137   gint i;
138   gint width, height;
139   gint xthickness;
140   gint ythickness;
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 */
145   gdouble subd_incr;
146   gdouble start, end, cur;
147   gchar unit_str[32];
148   gint digit_height;
149   gint digit_offset;
150   gint text_width;
151   gint pos;
152   PangoLayout *layout;
153   PangoRectangle logical_rect, ink_rect;
154
155   if (!GTK_WIDGET_DRAWABLE (ruler)) 
156     return;
157
158   widget = GTK_WIDGET (ruler);
159
160   gc = widget->style->fg_gc[GTK_STATE_NORMAL];
161   bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
162
163   xthickness = widget->style->xthickness;
164   ythickness = widget->style->ythickness;
165
166   digit_height = PANGO_PIXELS (ink_rect.height) + 2;
167   digit_offset = ink_rect.y;
168
169   layout = gtk_widget_create_pango_layout (widget, "012456789");
170   pango_layout_get_extents (layout, &ink_rect, &logical_rect);
171   
172   digit_height = PANGO_PIXELS (ink_rect.height) + 1;
173   digit_offset = ink_rect.y;
174
175   width = widget->allocation.width;
176   height = widget->allocation.height - ythickness * 2;
177    
178   gtk_paint_box (widget->style, ruler->backing_store,
179                  GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
180                  NULL, widget, "hruler",
181                  0, 0, 
182                  widget->allocation.width, widget->allocation.height);
183   
184   
185   gdk_draw_line (ruler->backing_store, gc,
186                  xthickness,
187                  height + ythickness,
188                  widget->allocation.width - xthickness,
189                  height + ythickness);
190
191   upper = ruler->upper / ruler->metric->pixels_per_unit;
192   lower = ruler->lower / ruler->metric->pixels_per_unit;
193
194   if ((upper - lower) == 0) 
195     return;
196   increment = (gdouble) width / (upper - lower);
197
198   /* determine the scale
199    *  We calculate the text size as for the vruler instead of using
200    *  text_width = gdk_string_width(font, unit_str), so that the result
201    *  for the scale looks consistent with an accompanying vruler
202    */
203   scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
204   g_snprintf (unit_str, sizeof (unit_str), "%d", scale);
205   text_width = strlen (unit_str) * digit_height + 1;
206
207   for (scale = 0; scale < MAXIMUM_SCALES; scale++)
208     if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
209       break;
210
211   if (scale == MAXIMUM_SCALES)
212     scale = MAXIMUM_SCALES - 1;
213
214   /* drawing starts here */
215   length = 0;
216   for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
217     {
218       subd_incr = (gdouble) ruler->metric->ruler_scale[scale] / 
219                   (gdouble) ruler->metric->subdivide[i];
220       if (subd_incr * fabs(increment) <= MINIMUM_INCR) 
221         continue;
222
223       /* Calculate the length of the tickmarks. Make sure that
224        * this length increases for each set of ticks
225        */
226       ideal_length = height / (i + 1) - 1;
227       if (ideal_length > ++length)
228         length = ideal_length;
229
230       if (lower < upper)
231         {
232           start = floor (lower / subd_incr) * subd_incr;
233           end   = ceil  (upper / subd_incr) * subd_incr;
234         }
235       else
236         {
237           start = floor (upper / subd_incr) * subd_incr;
238           end   = ceil  (lower / subd_incr) * subd_incr;
239         }
240
241   
242       for (cur = start; cur <= end; cur += subd_incr)
243         {
244           pos = ROUND ((cur - lower) * increment);
245
246           gdk_draw_line (ruler->backing_store, gc,
247                          pos, height + ythickness, 
248                          pos, height - length + ythickness);
249
250           /* draw label */
251           if (i == 0)
252             {
253               g_snprintf (unit_str, sizeof (unit_str), "%d", (int) cur);
254               
255               pango_layout_set_text (layout, unit_str, -1);
256               pango_layout_get_extents (layout, &logical_rect, NULL);
257
258               gtk_paint_layout (widget->style,
259                                 ruler->backing_store,
260                                 GTK_WIDGET_STATE (widget),
261                                 FALSE,
262                                 NULL,
263                                 widget,
264                                 "hruler",
265                                 pos + 2, ythickness + PANGO_PIXELS (logical_rect.y - digit_offset),
266                                 layout);
267             }
268         }
269     }
270
271   g_object_unref (layout);
272 }
273
274 static void
275 gtk_hruler_draw_pos (GtkRuler *ruler)
276 {
277   GtkWidget *widget;
278   GdkGC *gc;
279   int i;
280   gint x, y;
281   gint width, height;
282   gint bs_width, bs_height;
283   gint xthickness;
284   gint ythickness;
285   gdouble increment;
286
287   if (GTK_WIDGET_DRAWABLE (ruler))
288     {
289       widget = GTK_WIDGET (ruler);
290
291       gc = widget->style->fg_gc[GTK_STATE_NORMAL];
292       xthickness = widget->style->xthickness;
293       ythickness = widget->style->ythickness;
294       width = widget->allocation.width;
295       height = widget->allocation.height - ythickness * 2;
296
297       bs_width = height / 2;
298       bs_width |= 1;  /* make sure it's odd */
299       bs_height = bs_width / 2 + 1;
300
301       if ((bs_width > 0) && (bs_height > 0))
302         {
303           /*  If a backing store exists, restore the ruler  */
304           if (ruler->backing_store && ruler->non_gr_exp_gc)
305             gdk_draw_drawable (ruler->widget.window,
306                                ruler->non_gr_exp_gc,
307                                ruler->backing_store,
308                                ruler->xsrc, ruler->ysrc,
309                                ruler->xsrc, ruler->ysrc,
310                                bs_width, bs_height);
311
312           increment = (gdouble) width / (ruler->upper - ruler->lower);
313
314           x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
315           y = (height + bs_height) / 2 + ythickness;
316
317           for (i = 0; i < bs_height; i++)
318             gdk_draw_line (widget->window, gc,
319                            x + i, y + i,
320                            x + bs_width - 1 - i, y + i);
321
322
323           ruler->xsrc = x;
324           ruler->ysrc = y;
325         }
326     }
327 }