]> Pileus Git - ~andy/gtk/blob - gtk/gtkhruler.c
Switch set_cairo_target() virtual function to ref_cairo_surface()
[~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 <config.h>
28 #include <math.h>
29 #include <glib/gprintf.h>
30 #include <string.h>
31 #include "gtkalias.h"
32 #include "gtkhruler.h"
33
34
35 #define RULER_HEIGHT          14
36 #define MINIMUM_INCR          5
37 #define MAXIMUM_SUBDIVIDE     5
38 #define MAXIMUM_SCALES        10
39
40 #define ROUND(x) ((int) ((x) + 0.5))
41
42
43 static void gtk_hruler_class_init    (GtkHRulerClass *klass);
44 static void gtk_hruler_init          (GtkHRuler      *hruler);
45 static gint gtk_hruler_motion_notify (GtkWidget      *widget,
46                                       GdkEventMotion *event);
47 static void gtk_hruler_draw_ticks    (GtkRuler       *ruler);
48 static void gtk_hruler_draw_pos      (GtkRuler       *ruler);
49
50
51 GType
52 gtk_hruler_get_type (void)
53 {
54   static GType hruler_type = 0;
55
56   if (!hruler_type)
57     {
58       static const GTypeInfo hruler_info =
59       {
60         sizeof (GtkHRulerClass),
61         NULL,           /* base_init */
62         NULL,           /* base_finalize */
63         (GClassInitFunc) gtk_hruler_class_init,
64         NULL,           /* class_finalize */
65         NULL,           /* class_data */
66         sizeof (GtkHRuler),
67         0,              /* n_preallocs */
68         (GInstanceInitFunc) gtk_hruler_init,
69       };
70
71       hruler_type = g_type_register_static (GTK_TYPE_RULER, "GtkHRuler",
72                                             &hruler_info, 0);
73     }
74
75   return hruler_type;
76 }
77
78 static void
79 gtk_hruler_class_init (GtkHRulerClass *klass)
80 {
81   GtkWidgetClass *widget_class;
82   GtkRulerClass *ruler_class;
83
84   widget_class = (GtkWidgetClass*) klass;
85   ruler_class = (GtkRulerClass*) klass;
86
87   widget_class->motion_notify_event = gtk_hruler_motion_notify;
88
89   ruler_class->draw_ticks = gtk_hruler_draw_ticks;
90   ruler_class->draw_pos = gtk_hruler_draw_pos;
91 }
92
93 static void
94 gtk_hruler_init (GtkHRuler *hruler)
95 {
96   GtkWidget *widget;
97
98   widget = GTK_WIDGET (hruler);
99   widget->requisition.width = widget->style->xthickness * 2 + 1;
100   widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
101 }
102
103
104 GtkWidget*
105 gtk_hruler_new (void)
106 {
107   return g_object_new (GTK_TYPE_HRULER, NULL);
108 }
109
110 static gint
111 gtk_hruler_motion_notify (GtkWidget      *widget,
112                           GdkEventMotion *event)
113 {
114   GtkRuler *ruler;
115   gint x;
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   g_object_notify (G_OBJECT (ruler), "position");
126
127   /*  Make sure the ruler has been allocated already  */
128   if (ruler->backing_store != NULL)
129     gtk_ruler_draw_pos (ruler);
130
131   return FALSE;
132 }
133
134 static void
135 gtk_hruler_draw_ticks (GtkRuler *ruler)
136 {
137   GtkWidget *widget;
138   cairo_t *cr;
139   gint i;
140   gint width, height;
141   gint xthickness;
142   gint ythickness;
143   gint length, ideal_length;
144   gdouble lower, upper;         /* Upper and lower limits, in ruler units */
145   gdouble increment;            /* Number of pixels per unit */
146   gint scale;                   /* Number of units per major unit */
147   gdouble subd_incr;
148   gdouble 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   if (!GTK_WIDGET_DRAWABLE (ruler)) 
158     return;
159
160   widget = GTK_WIDGET (ruler);
161
162   xthickness = widget->style->xthickness;
163   ythickness = widget->style->ythickness;
164
165   layout = gtk_widget_create_pango_layout (widget, "012456789");
166   pango_layout_get_extents (layout, &ink_rect, &logical_rect);
167   
168   digit_height = PANGO_PIXELS (ink_rect.height) + 2;
169   digit_offset = ink_rect.y;
170
171   width = widget->allocation.width;
172   height = widget->allocation.height - ythickness * 2;
173    
174   gtk_paint_box (widget->style, ruler->backing_store,
175                  GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
176                  NULL, widget, "hruler",
177                  0, 0, 
178                  widget->allocation.width, widget->allocation.height);
179
180   cr = gdk_drawable_create_cairo_context (ruler->backing_store);
181   gdk_cairo_set_source_color (cr, &widget->style->fg[widget->state]);
182  
183   cairo_rectangle (cr, 
184                    xthickness,
185                    height + ythickness,
186                    widget->allocation.width - 2 * xthickness,
187                    1);
188
189   upper = ruler->upper / ruler->metric->pixels_per_unit;
190   lower = ruler->lower / ruler->metric->pixels_per_unit;
191
192   if ((upper - lower) == 0) 
193     return;
194   increment = (gdouble) width / (upper - lower);
195
196   /* determine the scale
197    *  We calculate the text size as for the vruler instead of using
198    *  text_width = gdk_string_width(font, unit_str), so that the result
199    *  for the scale looks consistent with an accompanying vruler
200    */
201   scale = ceil (ruler->max_size / ruler->metric->pixels_per_unit);
202   g_snprintf (unit_str, sizeof (unit_str), "%d", scale);
203   text_width = 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_width)
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   
240       for (cur = start; cur <= end; cur += subd_incr)
241         {
242           pos = ROUND ((cur - lower) * increment);
243
244           cairo_rectangle (cr, 
245                            pos, height + ythickness - length, 
246                            1,   length);
247
248           /* draw label */
249           if (i == 0)
250             {
251               g_snprintf (unit_str, sizeof (unit_str), "%d", (int) cur);
252               
253               pango_layout_set_text (layout, unit_str, -1);
254               pango_layout_get_extents (layout, &logical_rect, NULL);
255
256               gtk_paint_layout (widget->style,
257                                 ruler->backing_store,
258                                 GTK_WIDGET_STATE (widget),
259                                 FALSE,
260                                 NULL,
261                                 widget,
262                                 "hruler",
263                                 pos + 2, ythickness + PANGO_PIXELS (logical_rect.y - digit_offset),
264                                 layout);
265             }
266         }
267     }
268
269   cairo_fill (cr);
270   cairo_destroy (cr);
271
272   g_object_unref (layout);
273 }
274
275 static void
276 gtk_hruler_draw_pos (GtkRuler *ruler)
277 {
278   GtkWidget *widget = GTK_WIDGET (ruler);
279   gint x, y;
280   gint width, height;
281   gint bs_width, bs_height;
282   gint xthickness;
283   gint ythickness;
284   gdouble increment;
285
286   if (GTK_WIDGET_DRAWABLE (ruler))
287     {
288       xthickness = widget->style->xthickness;
289       ythickness = widget->style->ythickness;
290       width = widget->allocation.width;
291       height = widget->allocation.height - ythickness * 2;
292
293       bs_width = height / 2 + 2;
294       bs_width |= 1;  /* make sure it's odd */
295       bs_height = bs_width / 2 + 1;
296
297       if ((bs_width > 0) && (bs_height > 0))
298         {
299           cairo_t *cr = gdk_drawable_create_cairo_context (widget->window);
300       
301           /*  If a backing store exists, restore the ruler  */
302           if (ruler->backing_store)
303             gdk_draw_drawable (widget->window,
304                                widget->style->black_gc,
305                                ruler->backing_store,
306                                ruler->xsrc, ruler->ysrc,
307                                ruler->xsrc, ruler->ysrc,
308                                bs_width, bs_height);
309
310           increment = (gdouble) width / (ruler->upper - ruler->lower);
311
312           x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
313           y = (height + bs_height) / 2 + ythickness;
314
315           gdk_cairo_set_source_color (cr, &widget->style->fg[widget->state]);
316
317           cairo_move_to (cr, x,                  y);
318           cairo_line_to (cr, x + bs_width / 2., y + bs_height);
319           cairo_line_to (cr, x + bs_width,      y);
320           cairo_fill (cr);
321
322           cairo_destroy (cr);
323
324           ruler->xsrc = x;
325           ruler->ysrc = y;
326         }
327     }
328 }