]> Pileus Git - ~andy/gtk/blob - gtk/gtkvruler.c
The render vfunc takes a GdkDrawable* instead of a GdkWindow*, because
[~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 <string.h>
29 #include "gtkvruler.h"
30
31 #include <glib/gprintf.h>
32
33
34 #define RULER_WIDTH           14
35 #define MINIMUM_INCR          5
36 #define MAXIMUM_SUBDIVIDE     5
37 #define MAXIMUM_SCALES        10
38
39 #define ROUND(x) ((int) ((x) + 0.5))
40
41
42 static void gtk_vruler_class_init    (GtkVRulerClass *klass);
43 static void gtk_vruler_init          (GtkVRuler      *vruler);
44 static gint gtk_vruler_motion_notify (GtkWidget      *widget,
45                                       GdkEventMotion *event);
46 static void gtk_vruler_draw_ticks    (GtkRuler       *ruler);
47 static void gtk_vruler_draw_pos      (GtkRuler       *ruler);
48
49
50 GType
51 gtk_vruler_get_type (void)
52 {
53   static GType vruler_type = 0;
54
55   if (!vruler_type)
56     {
57       static const GTypeInfo vruler_info =
58       {
59         sizeof (GtkVRulerClass),
60         NULL,           /* base_init */
61         NULL,           /* base_finalize */
62         (GClassInitFunc) gtk_vruler_class_init,
63         NULL,           /* class_finalize */
64         NULL,           /* class_data */
65         sizeof (GtkVRuler),
66         0,              /* n_preallocs */
67         (GInstanceInitFunc) gtk_vruler_init,
68       };
69
70       vruler_type = g_type_register_static (GTK_TYPE_RULER, "GtkVRuler",
71                                             &vruler_info, 0);
72     }
73
74   return vruler_type;
75 }
76
77 static void
78 gtk_vruler_class_init (GtkVRulerClass *klass)
79 {
80   GtkWidgetClass *widget_class;
81   GtkRulerClass *ruler_class;
82
83   widget_class = (GtkWidgetClass*) klass;
84   ruler_class = (GtkRulerClass*) klass;
85
86   widget_class->motion_notify_event = gtk_vruler_motion_notify;
87
88   ruler_class->draw_ticks = gtk_vruler_draw_ticks;
89   ruler_class->draw_pos = gtk_vruler_draw_pos;
90 }
91
92 static void
93 gtk_vruler_init (GtkVRuler *vruler)
94 {
95   GtkWidget *widget;
96
97   widget = GTK_WIDGET (vruler);
98   widget->requisition.width = widget->style->xthickness * 2 + RULER_WIDTH;
99   widget->requisition.height = widget->style->ythickness * 2 + 1;
100 }
101
102 GtkWidget*
103 gtk_vruler_new (void)
104 {
105   return g_object_new (GTK_TYPE_VRULER, NULL);
106 }
107
108
109 static gint
110 gtk_vruler_motion_notify (GtkWidget      *widget,
111                           GdkEventMotion *event)
112 {
113   GtkRuler *ruler;
114   gint y;
115
116   ruler = GTK_RULER (widget);
117
118   if (event->is_hint)
119     gdk_window_get_pointer (widget->window, NULL, &y, NULL);
120   else
121     y = event->y;
122
123   ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y) / widget->allocation.height;
124   g_object_notify (G_OBJECT (ruler), "position");
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   gdouble lower, upper;         /* Upper and lower limits, in ruler units */
144   gdouble increment;            /* Number of pixels per unit */
145   gint scale;                   /* Number of units per major unit */
146   gdouble subd_incr;
147   gdouble 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   if (!GTK_WIDGET_DRAWABLE (ruler)) 
157     return;
158
159   widget = GTK_WIDGET (ruler);
160
161   gc = widget->style->fg_gc[GTK_STATE_NORMAL];
162   bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
163
164   xthickness = widget->style->xthickness;
165   ythickness = widget->style->ythickness;
166
167   layout = gtk_widget_create_pango_layout (widget, "012456789");
168   pango_layout_get_extents (layout, &ink_rect, &logical_rect);
169   
170   digit_height = PANGO_PIXELS (ink_rect.height) + 2;
171   digit_offset = ink_rect.y;
172
173   width = widget->allocation.height;
174   height = widget->allocation.width - ythickness * 2;
175
176   gtk_paint_box (widget->style, ruler->backing_store,
177                  GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
178                  NULL, widget, "vruler",
179                  0, 0, 
180                   widget->allocation.width, widget->allocation.height);
181   
182   gdk_draw_line (ruler->backing_store, gc,
183                  height + xthickness,
184                  ythickness,
185                  height + xthickness,
186                  widget->allocation.height - ythickness);
187   
188   upper = ruler->upper / ruler->metric->pixels_per_unit;
189   lower = ruler->lower / ruler->metric->pixels_per_unit;
190
191   if ((upper - lower) == 0)
192     return;
193   increment = (gdouble) width / (upper - lower);
194
195   /* determine the scale
196    *   use the maximum extents of the ruler to determine the largest
197    *   possible number to be displayed.  Calculate the height in pixels
198    *   of this displayed text. Use this height to find a scale which
199    *   leaves sufficient room for drawing the ruler.  
200    */
201   scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
202   g_snprintf (unit_str, sizeof (unit_str), "%d", scale);
203   text_height = strlen (unit_str) * digit_height + 1;
204
205   for (scale = 0; scale < MAXIMUM_SCALES; scale++)
206     if (ruler->metric->ruler_scale[scale] * fabs(increment) > 2 * text_height)
207       break;
208
209   if (scale == MAXIMUM_SCALES)
210     scale = MAXIMUM_SCALES - 1;
211
212   /* drawing starts here */
213   length = 0;
214   for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
215     {
216       subd_incr = (gdouble) ruler->metric->ruler_scale[scale] / 
217                   (gdouble) ruler->metric->subdivide[i];
218       if (subd_incr * fabs(increment) <= MINIMUM_INCR) 
219         continue;
220
221       /* Calculate the length of the tickmarks. Make sure that
222        * this length increases for each set of ticks
223        */
224       ideal_length = height / (i + 1) - 1;
225       if (ideal_length > ++length)
226         length = ideal_length;
227
228       if (lower < upper)
229         {
230           start = floor (lower / subd_incr) * subd_incr;
231           end   = ceil  (upper / subd_incr) * subd_incr;
232         }
233       else
234         {
235           start = floor (upper / subd_incr) * subd_incr;
236           end   = ceil  (lower / subd_incr) * subd_incr;
237         }
238
239       for (cur = start; cur <= end; cur += subd_incr)
240         {
241           pos = ROUND ((cur - lower) * increment);
242
243           gdk_draw_line (ruler->backing_store, gc,
244                          height + xthickness - length, pos,
245                          height + xthickness, pos);
246
247           /* draw label */
248           if (i == 0)
249             {
250               g_snprintf (unit_str, sizeof (unit_str), "%d", (int) cur);
251               
252               for (j = 0; j < (int) strlen (unit_str); j++)
253                 {
254                   pango_layout_set_text (layout, unit_str + j, 1);
255                   pango_layout_get_extents (layout, NULL, &logical_rect);
256
257       
258                   gtk_paint_layout (widget->style,
259                                     ruler->backing_store,
260                                     GTK_WIDGET_STATE (widget),
261                                     FALSE,
262                                     NULL,
263                                     widget,
264                                     "vruler",
265                                     xthickness + 1,
266                                     pos + digit_height * j + 2 + PANGO_PIXELS (logical_rect.y - digit_offset),
267                                     layout);
268                 }
269             }
270         }
271     }
272
273   g_object_unref (layout);
274 }
275
276
277 static void
278 gtk_vruler_draw_pos (GtkRuler *ruler)
279 {
280   GtkWidget *widget;
281   GdkGC *gc;
282   int i;
283   gint x, y;
284   gint width, height;
285   gint bs_width, bs_height;
286   gint xthickness;
287   gint ythickness;
288   gdouble increment;
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 - xthickness * 2;
298       height = widget->allocation.height;
299
300       bs_height = width / 2;
301       bs_height |= 1;  /* make sure it's odd */
302       bs_width = bs_height / 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_drawable (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 = (gdouble) height / (ruler->upper - ruler->lower);
316
317           x = (width + bs_width) / 2 + xthickness;
318           y = ROUND ((ruler->position - ruler->lower) * increment) + (ythickness - bs_height) / 2 - 1;
319
320           for (i = 0; i < bs_width; i++)
321             gdk_draw_line (widget->window, gc,
322                            x + i, y + i,
323                            x + i, y + bs_height - 1 - i);
324
325           ruler->xsrc = x;
326           ruler->ysrc = y;
327         }
328     }
329 }