2009-02-11 [colin] 3.7.0cvs58
[claws.git] / src / gtk / gtkshruler.c
1 /* GtkSHRuler
2  *
3  *  Copyright (C) 2000-2005 Alfons Hoogervorst & The Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18  
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!
25  * 
26  * -- Alfons
27  */
28
29 #include <math.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <gtk/gtk.h>
33 #include "gtkshruler.h"
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 static void gtk_shruler_class_init      (GtkSHRulerClass *klass);
43 static void gtk_shruler_init            (GtkSHRuler      *hruler);
44 static void gtk_shruler_draw_ticks      (GtkRuler        *ruler);
45
46 GType
47 gtk_shruler_get_type(void)
48 {
49         static GType shruler_type = 0;
50
51         if ( !shruler_type ) {
52                 static const GTypeInfo shruler_info = {
53                         sizeof (GtkSHRulerClass),
54
55                         (GBaseInitFunc) NULL,
56                         (GBaseFinalizeFunc) NULL,
57
58                         (GClassInitFunc) gtk_shruler_class_init,
59                         (GClassFinalizeFunc) NULL,
60                         NULL,   /* class_data */
61
62                         sizeof (GtkSHRuler),
63                         0,      /* n_preallocs */
64                         (GInstanceInitFunc) gtk_shruler_init,
65                 };
66                 /* inherit from GtkHRuler */
67                 shruler_type = g_type_register_static (GTK_TYPE_HRULER, "GtkSHRuler", &shruler_info, (GTypeFlags)0);
68         }
69         return shruler_type;
70 }
71
72 static void
73 gtk_shruler_class_init(GtkSHRulerClass * klass)
74 {
75         GtkWidgetClass * widget_class;
76         GtkRulerClass * hruler_class;
77
78         widget_class = GTK_WIDGET_CLASS(klass);
79         hruler_class = GTK_RULER_CLASS(klass);
80
81         /* just neglect motion notify events */
82         widget_class->motion_notify_event = NULL /* gtk_shruler_motion_notify */;
83
84         /* we want the old ruler draw ticks... */
85         /* ruler_class->draw_ticks = gtk_hruler_draw_ticks; */
86         hruler_class->draw_ticks = gtk_shruler_draw_ticks;
87         
88         /* unimplemented draw pos */
89         hruler_class->draw_pos = NULL;
90 /*
91         hruler_class->draw_pos = gtk_shruler_draw_pos;
92 */
93 }
94
95 static void
96 gtk_shruler_init (GtkSHRuler * shruler)
97 {
98         GtkWidget * widget;
99         
100         widget = GTK_WIDGET (shruler);
101         widget->requisition.width = widget->style->xthickness * 2 + 1;
102         widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
103 }
104
105
106 GtkWidget*
107 gtk_shruler_new(void)
108 {
109         return GTK_WIDGET( g_object_new( gtk_shruler_get_type(), NULL ) );
110 }
111
112 static void
113 gtk_shruler_draw_ticks(GtkRuler *ruler)
114 {
115         GtkWidget *widget;
116         GdkGC *gc, *bg_gc;
117         gint i;
118         gint width, height;
119         gint xthickness;
120         gint ythickness;
121         gint pos;
122
123         g_return_if_fail (ruler != NULL);
124         g_return_if_fail (GTK_IS_HRULER (ruler));
125
126         if (!GTK_WIDGET_DRAWABLE (ruler)) 
127                 return;
128
129         widget = GTK_WIDGET (ruler);
130         
131         gc = widget->style->fg_gc[GTK_STATE_NORMAL];
132         bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
133
134         xthickness = widget->style->xthickness;
135         ythickness = widget->style->ythickness;
136
137         width = widget->allocation.width;
138         height = widget->allocation.height - ythickness * 2;
139   
140         gtk_paint_box (widget->style, ruler->backing_store,
141                        GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
142                        NULL, widget, "hruler",
143                        0, 0, 
144                        widget->allocation.width, widget->allocation.height);
145
146         /* assume ruler->max_size has the char width */
147         /* i is increment of char_width,  pos is label number
148          * y position is based on height of widget itself */
149         for ( i = 0, pos = 0; i < widget->allocation.width - xthickness; i += ruler->max_size, pos++ ) {        
150                 gint length = height / 8;
151         
152                 if ( pos % 10 == 0 ) length = ( 2 * height / 3 );
153                 else if ( pos % 5 == 0 ) length = ( height / 3 );
154                 
155                 gdk_draw_line(ruler->backing_store, gc,
156                               i, height + ythickness,
157                               i, height - length);                      
158                 
159                 if ( pos % 10 == 0 ) {
160                         gchar buf[8];
161                         PangoLayout *layout;
162
163                         /* draw label */
164                         g_snprintf(buf, sizeof buf, "%d", pos);
165
166                         layout = gtk_widget_create_pango_layout
167                                 (GTK_WIDGET(ruler), buf);
168
169                         gdk_draw_layout(ruler->backing_store, gc, i + 2,
170                                         0, layout);
171
172                         g_object_unref(layout);
173                 }
174         }
175 }