]> Pileus Git - ~andy/gtk/blob - gtk/gtkhruler.c
call the base class init fucntions from all parent types upon class
[~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 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 "gtkhruler.h"
23
24
25 #define RULER_HEIGHT          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_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);
39
40
41 guint
42 gtk_hruler_get_type (void)
43 {
44   static guint hruler_type = 0;
45
46   if (!hruler_type)
47     {
48       GtkTypeInfo hruler_info =
49       {
50         "GtkHRuler",
51         sizeof (GtkHRuler),
52         sizeof (GtkHRulerClass),
53         (GtkClassInitFunc) gtk_hruler_class_init,
54         (GtkObjectInitFunc) gtk_hruler_init,
55         /* reversed_1 */ NULL,
56         /* reversed_2 */ NULL,
57         (GtkClassInitFunc) NULL,
58       };
59
60       hruler_type = gtk_type_unique (gtk_ruler_get_type (), &hruler_info);
61     }
62
63   return hruler_type;
64 }
65
66 static void
67 gtk_hruler_class_init (GtkHRulerClass *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_hruler_motion_notify;
76
77   ruler_class->draw_ticks = gtk_hruler_draw_ticks;
78   ruler_class->draw_pos = gtk_hruler_draw_pos;
79 }
80
81 static void
82 gtk_hruler_init (GtkHRuler *hruler)
83 {
84   GtkWidget *widget;
85
86   widget = GTK_WIDGET (hruler);
87   widget->requisition.width = widget->style->klass->xthickness * 2 + 1;
88   widget->requisition.height = widget->style->klass->ythickness * 2 + RULER_HEIGHT;
89 }
90
91
92 GtkWidget*
93 gtk_hruler_new (void)
94 {
95   return GTK_WIDGET (gtk_type_new (gtk_hruler_get_type ()));
96 }
97
98 static gint
99 gtk_hruler_motion_notify (GtkWidget      *widget,
100                           GdkEventMotion *event)
101 {
102   GtkRuler *ruler;
103   gint x;
104
105   g_return_val_if_fail (widget != NULL, FALSE);
106   g_return_val_if_fail (GTK_IS_HRULER (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, &x, NULL, NULL);
113   else
114     x = event->x;
115
116   ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
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_hruler_draw_ticks (GtkRuler *ruler)
127 {
128   GtkWidget *widget;
129   GdkGC *gc, *bg_gc;
130   GdkFont *font;
131   gint i;
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   gint digit_height;
143   gint text_width;
144   gint pos;
145
146   g_return_if_fail (ruler != NULL);
147   g_return_if_fail (GTK_IS_HRULER (ruler));
148
149   if (!GTK_WIDGET_DRAWABLE (ruler)) 
150     return;
151
152   widget = GTK_WIDGET (ruler);
153
154   gc = widget->style->fg_gc[GTK_STATE_NORMAL];
155   bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
156   font = widget->style->font;
157
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.width;
163   height = widget->allocation.height - ythickness * 2;
164
165   gdk_draw_line (ruler->backing_store, gc,
166                  xthickness,
167                  height + ythickness,
168                  widget->allocation.width - xthickness,
169                  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    *  We calculate the text size as for the vruler instead of using
180    *  text_width = gdk_string_width(font, unit_str), so that the result
181    *  for the scale looks consistent with an accompanying vruler
182    */
183   scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
184   sprintf (unit_str, "%d", scale);
185   text_width = strlen (unit_str) * digit_height + 1;
186
187   for (scale = 0; scale < MAXIMUM_SCALES; scale++)
188     if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
189       break;
190
191   if (scale == MAXIMUM_SCALES)
192     scale = MAXIMUM_SCALES - 1;
193
194   /* drawing starts here */
195   length = 0;
196   for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
197     {
198       subd_incr = (gfloat) ruler->metric->ruler_scale[scale] / 
199                   (gfloat) ruler->metric->subdivide[i];
200       if (subd_incr * fabs(increment) <= MINIMUM_INCR) 
201         continue;
202
203       /* Calculate the length of the tickmarks. Make sure that
204        * this length increases for each set of ticks
205        */
206       ideal_length = height / (i + 1) - 1;
207       if (ideal_length > ++length)
208         length = ideal_length;
209
210       if (lower < upper)
211         {
212           start = floor (lower / subd_incr) * subd_incr;
213           end   = ceil  (upper / subd_incr) * subd_incr;
214         }
215       else
216         {
217           start = floor (upper / subd_incr) * subd_incr;
218           end   = ceil  (lower / subd_incr) * subd_incr;
219         }
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                          pos, height + ythickness, 
228                          pos, height - length + ythickness);
229
230           /* draw label */
231           if (i == 0)
232             {
233               sprintf (unit_str, "%d", (int) cur);
234               gdk_draw_rectangle (ruler->backing_store,
235                                   bg_gc, TRUE,
236                                   pos + 1, ythickness,
237                                   gdk_string_width(font, unit_str) + 1,
238                                   digit_height);
239               gdk_draw_string (ruler->backing_store, font, gc,
240                                pos + 2, ythickness + font->ascent - 1,
241                                unit_str);
242             }
243         }
244     }
245 }
246
247 static void
248 gtk_hruler_draw_pos (GtkRuler *ruler)
249 {
250   GtkWidget *widget;
251   GdkGC *gc;
252   int i;
253   gint x, y;
254   gint width, height;
255   gint bs_width, bs_height;
256   gint xthickness;
257   gint ythickness;
258   gfloat increment;
259
260   g_return_if_fail (ruler != NULL);
261   g_return_if_fail (GTK_IS_HRULER (ruler));
262
263   if (GTK_WIDGET_DRAWABLE (ruler))
264     {
265       widget = GTK_WIDGET (ruler);
266
267       gc = widget->style->fg_gc[GTK_STATE_NORMAL];
268       xthickness = widget->style->klass->xthickness;
269       ythickness = widget->style->klass->ythickness;
270       width = widget->allocation.width;
271       height = widget->allocation.height - ythickness * 2;
272
273       bs_width = height / 2;
274       bs_width |= 1;  /* make sure it's odd */
275       bs_height = bs_width / 2 + 1;
276
277       if ((bs_width > 0) && (bs_height > 0))
278         {
279           /*  If a backing store exists, restore the ruler  */
280           if (ruler->backing_store && ruler->non_gr_exp_gc)
281             gdk_draw_pixmap (ruler->widget.window,
282                              ruler->non_gr_exp_gc,
283                              ruler->backing_store,
284                              ruler->xsrc, ruler->ysrc,
285                              ruler->xsrc, ruler->ysrc,
286                              bs_width, bs_height);
287
288           increment = (gfloat) width / (ruler->upper - ruler->lower);
289
290           x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
291           y = (height + bs_height) / 2 + ythickness;
292
293           for (i = 0; i < bs_height; i++)
294             gdk_draw_line (widget->window, gc,
295                            x + i, y + i,
296                            x + bs_width - 1 - i, y + i);
297
298
299           ruler->xsrc = x;
300           ruler->ysrc = y;
301         }
302     }
303 }