2006-11-18 [paul] 2.6.0cvs55
[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 2 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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20  
21 /* I derived this class from hruler. S in HRuler could be read as
22  * Sylpheed (sylpheed.good-day.net), but also [S]ettable HRuler.
23  * I basically ripped apart the draw_ticks member of HRuler; it
24  * now draws the ticks at ruler->max_size. so gtk_ruler_set_range's
25  * last parameter has the distance between two ticks (which is
26  * the width of the fixed font character!
27  * 
28  * -- Alfons
29  */
30
31 #include <math.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <gtk/gtkhruler.h>
35 #include "gtkshruler.h"
36
37 #define RULER_HEIGHT          14
38 #define MINIMUM_INCR          5
39 #define MAXIMUM_SUBDIVIDE     5
40 #define MAXIMUM_SCALES        10
41
42 #define ROUND(x) ((int) ((x) + 0.5))
43
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);
47
48 GType
49 gtk_shruler_get_type(void)
50 {
51         static GType shruler_type = 0;
52
53         if ( !shruler_type ) {
54                 static const GTypeInfo shruler_info = {
55                         sizeof (GtkSHRulerClass),
56
57                         (GBaseInitFunc) NULL,
58                         (GBaseFinalizeFunc) NULL,
59
60                         (GClassInitFunc) gtk_shruler_class_init,
61                         (GClassFinalizeFunc) NULL,
62                         NULL,   /* class_data */
63
64                         sizeof (GtkSHRuler),
65                         0,      /* n_preallocs */
66                         (GInstanceInitFunc) gtk_shruler_init,
67                 };
68                 /* inherit from GtkHRuler */
69                 shruler_type = g_type_register_static (GTK_TYPE_HRULER, "GtkSHRuler", &shruler_info, (GTypeFlags)0);
70         }
71         return shruler_type;
72 }
73
74 static void
75 gtk_shruler_class_init(GtkSHRulerClass * klass)
76 {
77         GtkWidgetClass * widget_class;
78         GtkRulerClass * hruler_class;
79
80         widget_class = GTK_WIDGET_CLASS(klass);
81         hruler_class = GTK_RULER_CLASS(klass);
82
83         /* just neglect motion notify events */
84         widget_class->motion_notify_event = NULL /* gtk_shruler_motion_notify */;
85
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;
89         
90         /* unimplemented draw pos */
91         hruler_class->draw_pos = NULL;
92 /*
93         hruler_class->draw_pos = gtk_shruler_draw_pos;
94 */
95 }
96
97 static void
98 gtk_shruler_init (GtkSHRuler * shruler)
99 {
100         GtkWidget * widget;
101         
102         widget = GTK_WIDGET (shruler);
103         widget->requisition.width = widget->style->xthickness * 2 + 1;
104         widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
105 }
106
107
108 GtkWidget*
109 gtk_shruler_new(void)
110 {
111         return GTK_WIDGET( g_object_new( gtk_shruler_get_type(), NULL ) );
112 }
113
114 static void
115 gtk_shruler_draw_ticks(GtkRuler *ruler)
116 {
117         GtkWidget *widget;
118         GdkGC *gc, *bg_gc;
119         gint i;
120         gint width, height;
121         gint xthickness;
122         gint ythickness;
123         gint pos;
124
125         g_return_if_fail (ruler != NULL);
126         g_return_if_fail (GTK_IS_HRULER (ruler));
127
128         if (!GTK_WIDGET_DRAWABLE (ruler)) 
129                 return;
130
131         widget = GTK_WIDGET (ruler);
132         
133         gc = widget->style->fg_gc[GTK_STATE_NORMAL];
134         bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
135
136         xthickness = widget->style->xthickness;
137         ythickness = widget->style->ythickness;
138
139         width = widget->allocation.width;
140         height = widget->allocation.height - ythickness * 2;
141   
142         gtk_paint_box (widget->style, ruler->backing_store,
143                        GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
144                        NULL, widget, "hruler",
145                        0, 0, 
146                        widget->allocation.width, widget->allocation.height);
147
148         /* assume ruler->max_size has the char width */
149         /* i is increment of char_width,  pos is label number
150          * y position is based on height of widget itself */
151         for ( i = 0, pos = 0; i < widget->allocation.width - xthickness; i += ruler->max_size, pos++ ) {        
152                 gint length = height / 8;
153         
154                 if ( pos % 10 == 0 ) length = ( 2 * height / 3 );
155                 else if ( pos % 5 == 0 ) length = ( height / 3 );
156                 
157                 gdk_draw_line(ruler->backing_store, gc,
158                               i, height + ythickness,
159                               i, height - length);                      
160                 
161                 if ( pos % 10 == 0 ) {
162                         gchar buf[8];
163                         PangoLayout *layout;
164
165                         /* draw label */
166                         g_snprintf(buf, sizeof buf, "%d", pos);
167
168                         layout = gtk_widget_create_pango_layout
169                                 (GTK_WIDGET(ruler), buf);
170
171                         gdk_draw_layout(ruler->backing_store, gc, i + 2,
172                                         0, layout);
173
174                         g_object_unref(layout);
175                 }
176         }
177 }
178
179 /* gtk_ruler_set_pos() - does not work yet, need to reimplement 
180  * gtk_ruler_draw_pos(). */
181 void
182 gtk_shruler_set_pos(GtkSHRuler * ruler, gfloat pos)
183 {
184         GtkRuler * ruler_;
185         g_return_if_fail( ruler != NULL );
186         
187         ruler_ = GTK_RULER(ruler);
188         
189         if ( pos < ruler_->lower ) 
190                 pos = ruler_->lower;
191         if ( pos > ruler_->upper )
192                 pos = ruler_->upper;
193         
194         ruler_->position = pos; 
195         
196         /*  Make sure the ruler has been allocated already  */
197         if ( ruler_->backing_store != NULL )
198                 gtk_ruler_draw_pos(ruler_);
199 }