Statistics
| Revision:

root / src / gtkshruler.c @ 1

History | View | Annotate | Download (5.71 KB)

1
/* GtkSHRuler
2
 *
3
 *  Copyright (C) 2000-2004 Alfons Hoogervorst & The Sylpheed Claws 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
#if 0
47
static gint gtk_shruler_motion_notify        (GtkWidget       *widget,
48
                                         GdkEventMotion  *event);
49
#endif
50
static void gtk_shruler_draw_ticks         (GtkRuler        *ruler);
51
#if 0
52
static void gtk_shruler_draw_pos              (GtkRuler        *ruler);
53
#endif
54

    
55
GType
56
gtk_shruler_get_type(void)
57
{
58
        static GType shruler_type = 0;
59

    
60
          if ( !shruler_type ) {
61
                   static const GTypeInfo shruler_info = {
62
                        sizeof (GtkSHRulerClass),
63

    
64
                        (GBaseInitFunc) NULL,
65
                        (GBaseFinalizeFunc) NULL,
66

    
67
                        (GClassInitFunc) gtk_shruler_class_init,
68
                        (GClassFinalizeFunc) NULL,
69
                        NULL,        /* class_data */
70

    
71
                        sizeof (GtkSHRuler),
72
                        0,        /* n_preallocs */
73
                        (GInstanceInitFunc) gtk_shruler_init,
74
                };
75
                /* inherit from GtkHRuler */
76
                shruler_type = g_type_register_static ( GTK_TYPE_HRULER, "GtkSHRuler", &shruler_info, (GTypeFlags) 0 );
77
        }
78
        return shruler_type;
79
}
80

    
81
static void
82
gtk_shruler_class_init(GtkSHRulerClass * klass)
83
{
84
         GtkWidgetClass * widget_class;
85
          GtkRulerClass * hruler_class;
86

    
87
          widget_class = (GtkWidgetClass*) klass;
88
          hruler_class = (GtkRulerClass*) klass;
89

    
90
        /* just neglect motion notify events */
91
          widget_class->motion_notify_event = NULL /* gtk_shruler_motion_notify */;
92

    
93
          /* we want the old ruler draw ticks... */
94
          /* ruler_class->draw_ticks = gtk_hruler_draw_ticks; */
95
        hruler_class->draw_ticks = gtk_shruler_draw_ticks;
96
        
97
        /* unimplemented draw pos */
98
        hruler_class->draw_pos = NULL;
99
/*
100
          hruler_class->draw_pos = gtk_shruler_draw_pos;
101
*/
102
}
103

    
104
static void
105
gtk_shruler_init (GtkSHRuler * shruler)
106
{
107
        GtkWidget * widget;
108
        
109
        widget = GTK_WIDGET (shruler);
110
        widget->requisition.width = widget->style->xthickness * 2 + 1;
111
        widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
112
}
113

    
114

    
115
GtkWidget*
116
gtk_shruler_new(void)
117
{
118
        return GTK_WIDGET( g_object_new( gtk_shruler_get_type(), NULL ) );
119
}
120

    
121
#if 0
122
static gint
123
gtk_shruler_motion_notify(GtkWidget      *widget,
124
                          GdkEventMotion *event)
125
{
126
        /* I could have perhaps set this to NULL */
127
        return FALSE;
128
}
129
#endif
130

    
131
static void
132
gtk_shruler_draw_ticks(GtkRuler *ruler)
133
{
134
        GtkWidget *widget;
135
        GdkGC *gc, *bg_gc;
136
        GdkFont *font;
137
        gint i;
138
        gint width, height;
139
        gint xthickness;
140
        gint ythickness;
141
        gint pos;
142

    
143
        g_return_if_fail (ruler != NULL);
144
        g_return_if_fail (GTK_IS_HRULER (ruler));
145

    
146
        if (!GTK_WIDGET_DRAWABLE (ruler)) 
147
                return;
148

    
149
        widget = GTK_WIDGET (ruler);
150
        
151
        gc = widget->style->fg_gc[GTK_STATE_NORMAL];
152
        bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
153
        font = gtk_style_get_font(widget->style);
154

    
155
        xthickness = widget->style->xthickness;
156
        ythickness = widget->style->ythickness;
157

    
158
        width = widget->allocation.width;
159
        height = widget->allocation.height - ythickness * 2;
160
  
161
        gtk_paint_box (widget->style, ruler->backing_store,
162
                       GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
163
                       NULL, widget, "hruler",
164
                       0, 0, 
165
                       widget->allocation.width, widget->allocation.height);
166

    
167
#if 0
168
        gdk_draw_line (ruler->backing_store, gc,
169
                       xthickness,
170
                       height + ythickness,
171
                       widget->allocation.width - xthickness,
172
                       height + ythickness);
173
#endif
174

    
175
        /* assume ruler->max_size has the char width */    
176
        /* i is increment of char_width,  pos is label number */
177
        for ( i = 0, pos = 0; i < widget->allocation.width - xthickness; i += ruler->max_size, pos++ ) {        
178
                int length = ythickness / 2;
179
        
180
                if ( pos % 10 == 0 ) length = ( 4 * ythickness );
181
                else if (pos % 5 == 0 ) length = ( 2 * ythickness );
182
                
183
                gdk_draw_line(ruler->backing_store, gc,
184
                              i, height + ythickness, 
185
                              i, height - length);                        
186
                
187
                if ( pos % 10 == 0 ) {
188
                        char buf[8];
189
                        /* draw label */
190
                        sprintf(buf, "%d", (int) pos);
191
                        gdk_draw_string(ruler->backing_store, font, gc,
192
                                        i + 2, ythickness + font->ascent - 1,
193
                                        buf);
194
            }
195
        }
196
}
197

    
198
/* gtk_ruler_set_pos() - does not work yet, need to reimplement 
199
 * gtk_ruler_draw_pos(). */
200
void
201
gtk_shruler_set_pos(GtkSHRuler * ruler, gfloat pos)
202
{
203
        GtkRuler * ruler_;
204
        g_return_if_fail( ruler != NULL );
205
        
206
        ruler_ = GTK_RULER(ruler);
207
        
208
        if ( pos < ruler_->lower ) 
209
                pos = ruler_->lower;
210
        if ( pos > ruler_->upper )
211
                pos = ruler_->upper;
212
        
213
        ruler_->position = pos;        
214
        
215
        /*  Make sure the ruler has been allocated already  */
216
        if ( ruler_->backing_store != NULL )
217
                gtk_ruler_draw_pos(ruler_);
218
}