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