]> Pileus Git - ~andy/gtk/blob - gtk/gtkhruler.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~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 <stdio.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 GtkType
50 gtk_hruler_get_type (void)
51 {
52   static GtkType hruler_type = 0;
53
54   if (!hruler_type)
55     {
56       static const GtkTypeInfo hruler_info =
57       {
58         "GtkHRuler",
59         sizeof (GtkHRuler),
60         sizeof (GtkHRulerClass),
61         (GtkClassInitFunc) gtk_hruler_class_init,
62         (GtkObjectInitFunc) gtk_hruler_init,
63         /* reserved_1 */ NULL,
64         /* reserved_2 */ NULL,
65         (GtkClassInitFunc) NULL,
66       };
67
68       hruler_type = gtk_type_unique (GTK_TYPE_RULER, &hruler_info);
69     }
70
71   return hruler_type;
72 }
73
74 static void
75 gtk_hruler_class_init (GtkHRulerClass *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_hruler_motion_notify;
84
85   ruler_class->draw_ticks = gtk_hruler_draw_ticks;
86   ruler_class->draw_pos = gtk_hruler_draw_pos;
87 }
88
89 static void
90 gtk_hruler_init (GtkHRuler *hruler)
91 {
92   GtkWidget *widget;
93
94   widget = GTK_WIDGET (hruler);
95   widget->requisition.width = widget->style->xthickness * 2 + 1;
96   widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
97 }
98
99
100 GtkWidget*
101 gtk_hruler_new (void)
102 {
103   return GTK_WIDGET (gtk_type_new (GTK_TYPE_HRULER));
104 }
105
106 static gint
107 gtk_hruler_motion_notify (GtkWidget      *widget,
108                           GdkEventMotion *event)
109 {
110   GtkRuler *ruler;
111   gint x;
112
113   g_return_val_if_fail (widget != NULL, FALSE);
114   g_return_val_if_fail (GTK_IS_HRULER (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, &x, NULL, NULL);
121   else
122     x = event->x;
123
124   ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
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_hruler_draw_ticks (GtkRuler *ruler)
135 {
136   GtkWidget *widget;
137   GdkGC *gc, *bg_gc;
138   GdkFont *font;
139   gint i;
140   gint width, height;
141   gint xthickness;
142   gint ythickness;
143   gint length, ideal_length;
144   gfloat lower, upper;          /* Upper and lower limits, in ruler units */
145   gfloat increment;             /* Number of pixels per unit */
146   gint scale;                   /* Number of units per major unit */
147   gfloat subd_incr;
148   gfloat start, end, cur;
149   gchar unit_str[32];
150   gint digit_height;
151   gint digit_offset;
152   gint text_width;
153   gint pos;
154   PangoLayout *layout;
155   PangoRectangle logical_rect, ink_rect;
156
157   g_return_if_fail (ruler != NULL);
158   g_return_if_fail (GTK_IS_HRULER (ruler));
159
160   if (!GTK_WIDGET_DRAWABLE (ruler)) 
161     return;
162
163   widget = GTK_WIDGET (ruler);
164
165   gc = widget->style->fg_gc[GTK_STATE_NORMAL];
166   bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
167   font = widget->style->font;
168
169   xthickness = widget->style->xthickness;
170   ythickness = widget->style->ythickness;
171
172   digit_height = PANGO_PIXELS (ink_rect.height) + 2;
173   digit_offset = ink_rect.y;
174
175   layout = gtk_widget_create_pango_layout (widget, "012456789");
176   pango_layout_get_extents (layout, &ink_rect, &logical_rect);
177   
178   digit_height = PANGO_PIXELS (ink_rect.height) + 1;
179   digit_offset = ink_rect.y;
180
181   width = widget->allocation.width;
182   height = widget->allocation.height - ythickness * 2;
183    
184   gtk_paint_box (widget->style, ruler->backing_store,
185                  GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
186                  NULL, widget, "hruler",
187                  0, 0, 
188                  widget->allocation.width, widget->allocation.height);
189   
190   
191   gdk_draw_line (ruler->backing_store, gc,
192                  xthickness,
193                  height + ythickness,
194                  widget->allocation.width - xthickness,
195                  height + ythickness);
196
197   upper = ruler->upper / ruler->metric->pixels_per_unit;
198   lower = ruler->lower / ruler->metric->pixels_per_unit;
199
200   if ((upper - lower) == 0) 
201     return;
202   increment = (gfloat) width / (upper - lower);
203
204   /* determine the scale
205    *  We calculate the text size as for the vruler instead of using
206    *  text_width = gdk_string_width(font, unit_str), so that the result
207    *  for the scale looks consistent with an accompanying vruler
208    */
209   scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
210   sprintf (unit_str, "%d", scale);
211   text_width = strlen (unit_str) * digit_height + 1;
212
213   for (scale = 0; scale < MAXIMUM_SCALES; scale++)
214     if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_width)
215       break;
216
217   if (scale == MAXIMUM_SCALES)
218     scale = MAXIMUM_SCALES - 1;
219
220   /* drawing starts here */
221   length = 0;
222   for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
223     {
224       subd_incr = (gfloat) ruler->metric->ruler_scale[scale] / 
225                   (gfloat) ruler->metric->subdivide[i];
226       if (subd_incr * fabs(increment) <= MINIMUM_INCR) 
227         continue;
228
229       /* Calculate the length of the tickmarks. Make sure that
230        * this length increases for each set of ticks
231        */
232       ideal_length = height / (i + 1) - 1;
233       if (ideal_length > ++length)
234         length = ideal_length;
235
236       if (lower < upper)
237         {
238           start = floor (lower / subd_incr) * subd_incr;
239           end   = ceil  (upper / subd_incr) * subd_incr;
240         }
241       else
242         {
243           start = floor (upper / subd_incr) * subd_incr;
244           end   = ceil  (lower / subd_incr) * subd_incr;
245         }
246
247   
248       for (cur = start; cur <= end; cur += subd_incr)
249         {
250           pos = ROUND ((cur - lower) * increment);
251
252           gdk_draw_line (ruler->backing_store, gc,
253                          pos, height + ythickness, 
254                          pos, height - length + ythickness);
255
256           /* draw label */
257           if (i == 0)
258             {
259               sprintf (unit_str, "%d", (int) cur);
260               
261               pango_layout_set_text (layout, unit_str, -1);
262               pango_layout_get_extents (layout, &logical_rect, NULL);
263
264               gdk_draw_layout (ruler->backing_store, gc,
265                                pos + 2, ythickness + PANGO_PIXELS (logical_rect.y - digit_offset),
266                                layout);
267             }
268         }
269     }
270
271   g_object_unref (G_OBJECT (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   gfloat increment;
286
287   g_return_if_fail (ruler != NULL);
288   g_return_if_fail (GTK_IS_HRULER (ruler));
289
290   if (GTK_WIDGET_DRAWABLE (ruler))
291     {
292       widget = GTK_WIDGET (ruler);
293
294       gc = widget->style->fg_gc[GTK_STATE_NORMAL];
295       xthickness = widget->style->xthickness;
296       ythickness = widget->style->ythickness;
297       width = widget->allocation.width;
298       height = widget->allocation.height - ythickness * 2;
299
300       bs_width = height / 2;
301       bs_width |= 1;  /* make sure it's odd */
302       bs_height = bs_width / 2 + 1;
303
304       if ((bs_width > 0) && (bs_height > 0))
305         {
306           /*  If a backing store exists, restore the ruler  */
307           if (ruler->backing_store && ruler->non_gr_exp_gc)
308             gdk_draw_pixmap (ruler->widget.window,
309                              ruler->non_gr_exp_gc,
310                              ruler->backing_store,
311                              ruler->xsrc, ruler->ysrc,
312                              ruler->xsrc, ruler->ysrc,
313                              bs_width, bs_height);
314
315           increment = (gfloat) width / (ruler->upper - ruler->lower);
316
317           x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
318           y = (height + bs_height) / 2 + ythickness;
319
320           for (i = 0; i < bs_height; i++)
321             gdk_draw_line (widget->window, gc,
322                            x + i, y + i,
323                            x + bs_width - 1 - i, y + i);
324
325
326           ruler->xsrc = x;
327           ruler->ysrc = y;
328         }
329     }
330 }