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