3 * Copyright (C) 2000-2011 Alfons Hoogervorst & The Claws Mail Team
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 3 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 /* I derived this class from hruler. S in HRuler could be read as
20 * Sylpheed (sylpheed.good-day.net), but also [S]ettable HRuler.
21 * I basically ripped apart the draw_ticks member of HRuler; it
22 * now draws the ticks at ruler->max_size. so gtk_ruler_set_range's
23 * last parameter has the distance between two ticks (which is
24 * the width of the fixed font character!
33 #include "gtkshruler.h"
37 #define RULER_HEIGHT 14
38 #define MINIMUM_INCR 5
39 #define MAXIMUM_SUBDIVIDE 5
40 #define MAXIMUM_SCALES 10
42 #define ROUND(x) ((int) ((x) + 0.5))
44 static void gtk_shruler_class_init (GtkSHRulerClass *klass);
45 static void gtk_shruler_init (GtkSHRuler *hruler);
46 static void gtk_shruler_draw_ticks (GtkRuler *ruler);
49 gtk_shruler_get_type(void)
51 static GType shruler_type = 0;
53 if ( !shruler_type ) {
54 static const GTypeInfo shruler_info = {
55 sizeof (GtkSHRulerClass),
58 (GBaseFinalizeFunc) NULL,
60 (GClassInitFunc) gtk_shruler_class_init,
61 (GClassFinalizeFunc) NULL,
62 NULL, /* class_data */
66 (GInstanceInitFunc) gtk_shruler_init,
68 /* inherit from GtkHRuler */
69 shruler_type = g_type_register_static (GTK_TYPE_HRULER, "GtkSHRuler", &shruler_info, (GTypeFlags)0);
75 gtk_shruler_class_init(GtkSHRulerClass * klass)
77 GtkWidgetClass * widget_class;
78 GtkRulerClass * hruler_class;
80 widget_class = GTK_WIDGET_CLASS(klass);
81 hruler_class = GTK_RULER_CLASS(klass);
83 /* just neglect motion notify events */
84 widget_class->motion_notify_event = NULL /* gtk_shruler_motion_notify */;
86 /* we want the old ruler draw ticks... */
87 /* ruler_class->draw_ticks = gtk_hruler_draw_ticks; */
88 hruler_class->draw_ticks = gtk_shruler_draw_ticks;
90 /* unimplemented draw pos */
91 hruler_class->draw_pos = NULL;
93 hruler_class->draw_pos = gtk_shruler_draw_pos;
98 gtk_shruler_init (GtkSHRuler * shruler)
102 widget = GTK_WIDGET (shruler);
103 widget->requisition.width = widget->style->xthickness * 2 + 1;
104 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
109 gtk_shruler_new(void)
111 return GTK_WIDGET( g_object_new( gtk_shruler_get_type(), NULL ) );
115 gtk_shruler_draw_ticks(GtkRuler *ruler)
125 cm_return_if_fail (ruler != NULL);
126 cm_return_if_fail (GTK_IS_HRULER (ruler));
128 if (!gtkut_widget_is_drawable (GTK_WIDGET(ruler)))
131 widget = GTK_WIDGET (ruler);
133 cr = gdk_cairo_create(ruler->backing_store);
134 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
135 cairo_set_line_width(cr, 1.);
136 gdk_cairo_set_source_color(cr, >k_widget_get_style(widget)->text[GTK_STATE_NORMAL]);
138 xthickness = widget->style->xthickness;
139 ythickness = widget->style->ythickness;
141 width = widget->allocation.width;
142 height = widget->allocation.height - ythickness * 2;
144 gtk_paint_box (widget->style, ruler->backing_store,
145 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
146 NULL, widget, "hruler",
148 widget->allocation.width, widget->allocation.height);
150 /* assume ruler->max_size has the char width */
151 /* i is increment of char_width, pos is label number
152 * y position is based on height of widget itself */
153 for ( i = 0, pos = 0; i < widget->allocation.width - xthickness; i += ruler->max_size, pos++ ) {
154 gint length = height / 8;
156 if ( pos % 10 == 0 ) length = ( 2 * height / 3 );
157 else if ( pos % 5 == 0 ) length = ( height / 3 );
159 cairo_move_to(cr, i, height + ythickness);
160 cairo_line_to(cr, i, height - length);
163 if ( pos % 10 == 0 ) {
168 g_snprintf(buf, sizeof buf, "%d", pos);
170 layout = gtk_widget_create_pango_layout
171 (GTK_WIDGET(ruler), buf);
173 cairo_move_to(cr, i+2, 0);
174 pango_cairo_show_layout(cr, layout);
176 g_object_unref(layout);