]> Pileus Git - ~andy/gtk/blob - gtk/gtkvruler.c
2f794821c1d3dffc7f8d52cc3634214bf1716a4f
[~andy/gtk] / gtk / gtkvruler.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 <stdio.h>
29 #include <string.h>
30 #include "gtkvruler.h"
31
32
33 #define RULER_WIDTH           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_vruler_class_init    (GtkVRulerClass *klass);
42 static void gtk_vruler_init          (GtkVRuler      *vruler);
43 static gint gtk_vruler_motion_notify (GtkWidget      *widget,
44                                       GdkEventMotion *event);
45 static void gtk_vruler_draw_ticks    (GtkRuler       *ruler);
46 static void gtk_vruler_draw_pos      (GtkRuler       *ruler);
47
48
49 GtkType
50 gtk_vruler_get_type (void)
51 {
52   static GtkType vruler_type = 0;
53
54   if (!vruler_type)
55     {
56       static const GtkTypeInfo vruler_info =
57       {
58         "GtkVRuler",
59         sizeof (GtkVRuler),
60         sizeof (GtkVRulerClass),
61         (GtkClassInitFunc) gtk_vruler_class_init,
62         (GtkObjectInitFunc) gtk_vruler_init,
63         /* reserved_1 */ NULL,
64         /* reserved_2 */ NULL,
65         (GtkClassInitFunc) NULL,
66       };
67
68       vruler_type = gtk_type_unique (GTK_TYPE_RULER, &vruler_info);
69     }
70
71   return vruler_type;
72 }
73
74 static void
75 gtk_vruler_class_init (GtkVRulerClass *klass)
76 {
77   GtkWidgetClass *widget_class;
78   GtkRulerClass *ruler_class;
79
80   widget_class = (GtkWidgetClass*) klass;
81   ruler_class = (GtkRulerClass*) klass;
82
83   widget_class->motion_notify_event = gtk_vruler_motion_notify;
84
85   ruler_class->draw_ticks = gtk_vruler_draw_ticks;
86   ruler_class->draw_pos = gtk_vruler_draw_pos;
87 }
88
89 static void
90 gtk_vruler_init (GtkVRuler *vruler)
91 {
92   GtkWidget *widget;
93
94   widget = GTK_WIDGET (vruler);
95   widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
96   widget->requisition.height = widget->style->ythickness * 2 + 1;
97 }
98
99 GtkWidget*
100 gtk_vruler_new (void)
101 {
102   return GTK_WIDGET (gtk_type_new (GTK_TYPE_VRULER));
103 }
104
105
106 static gint
107 gtk_vruler_motion_notify (GtkWidget      *widget,
108                           GdkEventMotion *event)
109 {
110   GtkRuler *ruler;
111   gint y;
112
113   g_return_val_if_fail (widget != NULL, FALSE);
114   g_return_val_if_fail (GTK_IS_VRULER (widget), FALSE);
115   g_return_val_if_fail (event != NULL, FALSE);
116
117   ruler = GTK_RULER (widget);
118
119   if (event->is_hint)
120     gdk_window_get_pointer (widget->window, NULL, &y, NULL);
121   else
122     y = event->y;
123
124   ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y) / widget->allocation.height;
125
126   /*  Make sure the ruler has been allocated already  */
127   if (ruler->backing_store != NULL)
128     gtk_ruler_draw_pos (ruler);
129
130   return FALSE;
131 }
132
133 static void
134 gtk_vruler_draw_ticks (GtkRuler *ruler)
135 {
136   GtkWidget *widget;
137   GdkGC *gc, *bg_gc;
138   gint i, j;
139   gint width, height;
140   gint xthickness;
141   gint ythickness;
142   gint length, ideal_length;
143   gfloat lower, upper;          /* Upper and lower limits, in ruler units */
144   gfloat increment;             /* Number of pixels per unit */
145   gint scale;                   /* Number of units per major unit */
146   gfloat subd_incr;
147   gfloat start, end, cur;
148   gchar unit_str[32];
149   gint digit_height;
150   gint digit_offset;
151   gint text_height;
152   gint pos;
153   PangoLayout *layout;
154   PangoRectangle logical_rect, ink_rect;
155
156   g_return_if_fail (ruler != NULL);
157   g_return_if_fail (GTK_IS_VRULER (ruler));
158
159   if (!GTK_WIDGET_DRAWABLE (ruler)) 
160     return;
161
162   widget = GTK_WIDGET (ruler);
163
164   gc = widget->style->fg_gc[GTK_STATE_NORMAL];
165   bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
166
167   xthickness = widget->style->xthickness;
168   ythickness = widget->style->ythickness;
169
170   layout = gtk_widget_create_pango_layout (widget, "012456789");
171   pango_layout_get_extents (layout, &ink_rect, &logical_rect);
172   
173   digit_height = PANGO_PIXELS (ink_rect.height) + 2;
174   digit_offset = ink_rect.y;
175
176   width = widget->allocation.height;
177   height = widget->allocation.width - ythickness * 2;
178
179   gtk_paint_box (widget->style, ruler->backing_store,
180                  GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
181                  NULL, widget, "vruler",
182                  0, 0, 
183                   widget->allocation.width, widget->allocation.height);
184   
185   gdk_draw_line (ruler->backing_store, gc,
186                  height + xthickness,
187                  ythickness,
188                  height + xthickness,
189                  widget->allocation.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 = (gfloat) width / (upper - lower);
197
198   /* determine the scale
199    *   use the maximum extents of the ruler to determine the largest
200    *   possible number to be displayed.  Calculate the height in pixels
201    *   of this displayed text. Use this height to find a scale which
202    *   leaves sufficient room for drawing the ruler.  
203    */
204   scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
205   sprintf (unit_str, "%d", scale);
206   text_height = strlen (unit_str) * digit_height + 1;
207
208   for (scale = 0; scale < MAXIMUM_SCALES; scale++)
209     if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_height)
210       break;
211
212   if (scale == MAXIMUM_SCALES)
213     scale = MAXIMUM_SCALES - 1;
214
215   /* drawing starts here */
216   length = 0;
217   for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
218     {
219       subd_incr = (gfloat) ruler->metric->ruler_scale[scale] / 
220                   (gfloat) ruler->metric->subdivide[i];
221       if (subd_incr * fabs(increment) <= MINIMUM_INCR) 
222         continue;
223
224       /* Calculate the length of the tickmarks. Make sure that
225        * this length increases for each set of ticks
226        */
227       ideal_length = height / (i + 1) - 1;
228       if (ideal_length > ++length)
229         length = ideal_length;
230
231       if (lower < upper)
232         {
233           start = floor (lower / subd_incr) * subd_incr;
234           end   = ceil  (upper / subd_incr) * subd_incr;
235         }
236       else
237         {
238           start = floor (upper / subd_incr) * subd_incr;
239           end   = ceil  (lower / subd_incr) * subd_incr;
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                          height + xthickness - length, pos,
248                          height + xthickness, pos);
249
250           /* draw label */
251           if (i == 0)
252             {
253               sprintf (unit_str, "%d", (int) cur);
254               
255               for (j = 0; j < (int) strlen (unit_str); j++)
256                 {
257                   pango_layout_set_text (layout, unit_str + j, 1);
258                   pango_layout_get_extents (layout, NULL, &logical_rect);
259                   
260                   gdk_draw_layout (ruler->backing_store, gc,
261                                    xthickness + 1,
262                                    pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset),
263                                    layout);
264                 }
265             }
266         }
267     }
268
269   g_object_unref (G_OBJECT (layout));
270 }
271
272
273 static void
274 gtk_vruler_draw_pos (GtkRuler *ruler)
275 {
276   GtkWidget *widget;
277   GdkGC *gc;
278   int i;
279   gint x, y;
280   gint width, height;
281   gint bs_width, bs_height;
282   gint xthickness;
283   gint ythickness;
284   gfloat increment;
285
286   g_return_if_fail (ruler != NULL);
287   g_return_if_fail (GTK_IS_VRULER (ruler));
288
289   if (GTK_WIDGET_DRAWABLE (ruler))
290     {
291       widget = GTK_WIDGET (ruler);
292
293       gc = widget->style->fg_gc[GTK_STATE_NORMAL];
294       xthickness = widget->style->xthickness;
295       ythickness = widget->style->ythickness;
296       width = widget->allocation.width - xthickness * 2;
297       height = widget->allocation.height;
298
299       bs_height = width / 2;
300       bs_height |= 1;  /* make sure it's odd */
301       bs_width = bs_height / 2 + 1;
302
303       if ((bs_width > 0) && (bs_height > 0))
304         {
305           /*  If a backing store exists, restore the ruler  */
306           if (ruler->backing_store && ruler->non_gr_exp_gc)
307             gdk_draw_pixmap (ruler->widget.window,
308                              ruler->non_gr_exp_gc,
309                              ruler->backing_store,
310                              ruler->xsrc, ruler->ysrc,
311                              ruler->xsrc, ruler->ysrc,
312                              bs_width, bs_height);
313
314           increment = (gfloat) height / (ruler->upper - ruler->lower);
315
316           x = (width + bs_width) / 2 + xthickness;
317           y = ROUND ((ruler->position - ruler->lower) * increment) + (ythickness - bs_height) / 2 - 1;
318
319           for (i = 0; i < bs_width; i++)
320             gdk_draw_line (widget->window, gc,
321                            x + i, y + i,
322                            x + i, y + bs_height - 1 - i);
323
324           ruler->xsrc = x;
325           ruler->ysrc = y;
326         }
327     }
328 }