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