Added a check button for NNTP authentication to account preferences.
[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 gint gtk_shruler_motion_notify   (GtkWidget       *widget,
47                                          GdkEventMotion  *event);
48 static void gtk_shruler_draw_ticks      (GtkRuler        *ruler);
49 #if 0
50 static void gtk_shruler_draw_pos        (GtkRuler        *ruler);
51 #endif
52
53 guint
54 gtk_shruler_get_type(void)
55 {
56         static guint shruler_type = 0;
57
58         if ( !shruler_type ) {
59                 static const GtkTypeInfo shruler_info = {
60                         "GtkSHRuler",
61                         sizeof (GtkSHRuler),
62                         sizeof (GtkSHRulerClass),
63                         (GtkClassInitFunc) gtk_shruler_class_init,
64                         (GtkObjectInitFunc) gtk_shruler_init,
65                         /* reserved_1 */ NULL,
66                 /* reserved_2 */ NULL,
67                 (GtkClassInitFunc) NULL,
68          };
69                 /* inherit from GtkHRuler */
70         shruler_type = gtk_type_unique( gtk_hruler_get_type (), &shruler_info );
71         }
72         return shruler_type;
73 }
74
75 static void
76 gtk_shruler_class_init(GtkSHRulerClass * klass)
77 {
78         GtkWidgetClass * widget_class;
79         GtkRulerClass * hruler_class;
80
81         widget_class = (GtkWidgetClass*) klass;
82         hruler_class = (GtkRulerClass*) klass;
83
84         /* just neglect motion notify events */
85         widget_class->motion_notify_event = NULL /* gtk_shruler_motion_notify */;
86
87         /* we want the old ruler draw ticks... */
88         /* ruler_class->draw_ticks = gtk_hruler_draw_ticks; */
89         hruler_class->draw_ticks = gtk_shruler_draw_ticks;
90         
91         /* unimplemented draw pos */
92         hruler_class->draw_pos = NULL;
93 /*
94         hruler_class->draw_pos = gtk_shruler_draw_pos;
95 */
96 }
97
98 static void
99 gtk_shruler_init (GtkSHRuler * shruler)
100 {
101         GtkWidget * widget;
102         
103         widget = GTK_WIDGET (shruler);
104         widget->requisition.width = widget->style->klass->xthickness * 2 + 1;
105         widget->requisition.height = widget->style->klass->ythickness * 2 + RULER_HEIGHT;
106 }
107
108
109 GtkWidget*
110 gtk_shruler_new(void)
111 {
112         return GTK_WIDGET( gtk_type_new( gtk_shruler_get_type() ) );
113 }
114
115 static gint
116 gtk_shruler_motion_notify(GtkWidget      *widget,
117                           GdkEventMotion *event)
118 {
119         /* I could have perhaps set this to NULL */
120         return FALSE;
121 }
122
123 static void
124 gtk_shruler_draw_ticks(GtkRuler *ruler)
125 {
126         GtkWidget *widget;
127         GdkGC *gc, *bg_gc;
128         GdkFont *font;
129         gint i;
130         gint width, height;
131         gint xthickness;
132         gint ythickness;
133         gint pos;
134
135         g_return_if_fail (ruler != NULL);
136         g_return_if_fail (GTK_IS_HRULER (ruler));
137
138         if (!GTK_WIDGET_DRAWABLE (ruler)) 
139                 return;
140
141         widget = GTK_WIDGET (ruler);
142         
143         gc = widget->style->fg_gc[GTK_STATE_NORMAL];
144         bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
145         font = widget->style->font;
146
147         xthickness = widget->style->klass->xthickness;
148         ythickness = widget->style->klass->ythickness;
149
150         width = widget->allocation.width;
151         height = widget->allocation.height - ythickness * 2;
152   
153         gtk_paint_box (widget->style, ruler->backing_store,
154                        GTK_STATE_NORMAL, GTK_SHADOW_OUT, 
155                        NULL, widget, "hruler",
156                        0, 0, 
157                        widget->allocation.width, widget->allocation.height);
158
159 #if 0
160         gdk_draw_line (ruler->backing_store, gc,
161                        xthickness,
162                        height + ythickness,
163                        widget->allocation.width - xthickness,
164                        height + ythickness);
165 #endif
166
167         /* assume ruler->max_size has the char width */    
168         /* i is increment of char_width,  pos is label number */
169         for ( i = 0, pos = 0; i < widget->allocation.width - xthickness; i += ruler->max_size, pos++ ) {        
170                 int length = ythickness / 2;
171         
172                 if ( pos % 10 == 0 ) length = ( 4 * ythickness );
173                 else if (pos % 5 == 0 ) length = ( 2 * ythickness );
174                 
175                 gdk_draw_line(ruler->backing_store, gc,
176                               i, height + ythickness, 
177                               i, height - length);                      
178                 
179                 if ( pos % 10 == 0 ) {
180                         char buf[8];
181                         /* draw label */
182                         sprintf(buf, "%d", (int) pos);
183                         gdk_draw_string(ruler->backing_store, font, gc,
184                                         i + 2, ythickness + font->ascent - 1,
185                                         buf);
186             }
187         }
188 }
189
190 /* gtk_ruler_set_pos() - does not work yet, need to reimplement 
191  * gtk_ruler_draw_pos(). */
192 void
193 gtk_shruler_set_pos(GtkSHRuler * ruler, gfloat pos)
194 {
195         GtkRuler * ruler_;
196         g_return_if_fail( ruler != NULL );
197         
198         ruler_ = GTK_RULER(ruler);
199         
200         if ( pos < ruler_->lower ) 
201                 pos = ruler_->lower;
202         if ( pos > ruler_->upper )
203                 pos = ruler_->upper;
204         
205         ruler_->position = pos; 
206         
207         /*  Make sure the ruler has been allocated already  */
208         if ( ruler_->backing_store != NULL )
209                 gtk_ruler_draw_pos(ruler_);
210 }