* src/summaryview.c
[claws.git] / src / gtkshruler.c
1 /* GtkSHRuler
2  *
3  *  Copyright (C) 2000 Alfons Hoogervorst
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 (alfons@proteus.demon.nl)
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 #if 0
48 static void gtk_shruler_draw_pos        (GtkRuler        *ruler);
49 #endif
50
51 guint
52 gtk_shruler_get_type(void)
53 {
54         static guint shruler_type = 0;
55
56         if ( !shruler_type ) {
57                 static const GtkTypeInfo shruler_info = {
58                         "GtkSHRuler",
59                         sizeof (GtkSHRuler),
60                         sizeof (GtkSHRulerClass),
61                         (GtkClassInitFunc) gtk_shruler_class_init,
62                         (GtkObjectInitFunc) gtk_shruler_init,
63                         /* reserved_1 */ NULL,
64                 /* reserved_2 */ NULL,
65                 (GtkClassInitFunc) NULL,
66          };
67                 /* inherit from GtkHRuler */
68         shruler_type = gtk_type_unique( gtk_hruler_get_type (), &shruler_info );
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 = (GtkWidgetClass*) klass;
80         hruler_class = (GtkRulerClass*) 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->klass->xthickness * 2 + 1;
103         widget->requisition.height = widget->style->klass->ythickness * 2 + RULER_HEIGHT;
104 }
105
106
107 GtkWidget*
108 gtk_shruler_new(void)
109 {
110         return GTK_WIDGET( gtk_type_new( gtk_shruler_get_type() ) );
111 }
112
113 static void
114 gtk_shruler_draw_ticks(GtkRuler *ruler)
115 {
116         GtkWidget *widget;
117         GdkGC *gc, *bg_gc;
118         GdkFont *font;
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         font = widget->style->font;
136
137         xthickness = widget->style->klass->xthickness;
138         ythickness = widget->style->klass->ythickness;
139
140         width = widget->allocation.width;
141         height = widget->allocation.height - ythickness * 2;
142   
143         gtk_paint_box (widget->style, ruler->backing_store,
144                        GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
145                        NULL, widget, "hruler",
146                        0, 0, 
147                        widget->allocation.width, widget->allocation.height);
148
149 #if 0
150         gdk_draw_line (ruler->backing_store, gc,
151                        xthickness,
152                        height + ythickness,
153                        widget->allocation.width - xthickness,
154                        height + ythickness);
155 #endif
156
157         /* assume ruler->max_size has the char width */    
158         /* i is increment of char_width,  pos is label number */
159         for ( i = 0, pos = 0; i < widget->allocation.width - xthickness; i += ruler->max_size, pos++ ) {        
160                 int length = ythickness / 2;
161         
162                 if ( pos % 10 == 0 ) length = ( 4 * ythickness );
163                 else if (pos % 5 == 0 ) length = ( 2 * ythickness );
164                 
165                 gdk_draw_line(ruler->backing_store, gc,
166                               i, height + ythickness, 
167                               i, height - length);                      
168                 
169                 if ( pos % 10 == 0 ) {
170                         char buf[8];
171                         /* draw label */
172                         sprintf(buf, "%d", (int) pos);
173                         gdk_draw_string(ruler->backing_store, font, gc,
174                                         i + 2, ythickness + font->ascent - 1,
175                                         buf);
176             }
177         }
178 }
179
180 /* gtk_ruler_set_pos() - does not work yet, need to reimplement 
181  * gtk_ruler_draw_pos(). */
182 void
183 gtk_shruler_set_pos(GtkSHRuler * ruler, gfloat pos)
184 {
185         GtkRuler * ruler_;
186         g_return_if_fail( ruler != NULL );
187         
188         ruler_ = GTK_RULER(ruler);
189         
190         if ( pos < ruler_->lower ) 
191                 pos = ruler_->lower;
192         if ( pos > ruler_->upper )
193                 pos = ruler_->upper;
194         
195         ruler_->position = pos; 
196         
197         /*  Make sure the ruler has been allocated already  */
198         if ( ruler_->backing_store != NULL )
199                 gtk_ruler_draw_pos(ruler_);
200 }