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