summaryrefslogtreecommitdiff
path: root/src/gtkshruler.c
diff options
context:
space:
mode:
authorSimeon Simeonov2018-02-26 11:23:00 +0100
committerSimeon Simeonov2018-02-26 11:23:00 +0100
commit0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch)
treeaf916a30553c78ce9d4f2d8658656a175c5b6921 /src/gtkshruler.c
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'src/gtkshruler.c')
-rw-r--r--src/gtkshruler.c228
1 files changed, 228 insertions, 0 deletions
diff --git a/src/gtkshruler.c b/src/gtkshruler.c
new file mode 100644
index 0000000..546affd
--- /dev/null
+++ b/src/gtkshruler.c
@@ -0,0 +1,228 @@
1/* GtkSHRuler
2 *
3 * Copyright (C) 2000-2005 Alfons Hoogervorst & The Sylpheed Claws Team
4 * Copyright (C) 2007 Hiroyuki Yamamoto
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22/* I derived this class from hruler. S in HRuler could be read as
23 * Sylpheed (sylpheed.sraoss.jp), but also [S]ettable HRuler.
24 * I basically ripped apart the draw_ticks member of HRuler; it
25 * now draws the ticks at ruler->max_size. so gtk_ruler_set_range's
26 * last parameter has the distance between two ticks (which is
27 * the width of the fixed font character!
28 *
29 * -- Alfons
30 */
31
32#include <math.h>
33#include <stdio.h>
34#include <string.h>
35#include <gtk/gtkhruler.h>
36#include "gtkshruler.h"
37
38#define RULER_HEIGHT 14
39#define MINIMUM_INCR 5
40#define MAXIMUM_SUBDIVIDE 5
41#define MAXIMUM_SCALES 10
42
43#define ROUND(x) ((int) ((x) + 0.5))
44
45static void gtk_shruler_class_init (GtkSHRulerClass *klass);
46static void gtk_shruler_init (GtkSHRuler *hruler);
47static void gtk_shruler_draw_ticks (GtkRuler *ruler);
48#if 0
49static void gtk_shruler_draw_pos (GtkRuler *ruler);
50#endif
51
52GType
53gtk_shruler_get_type(void)
54{
55 static GType shruler_type = 0;
56
57 if ( !shruler_type ) {
58 static const GTypeInfo shruler_info = {
59 sizeof (GtkSHRulerClass),
60
61 (GBaseInitFunc) NULL,
62 (GBaseFinalizeFunc) NULL,
63
64 (GClassInitFunc) gtk_shruler_class_init,
65 (GClassFinalizeFunc) NULL,
66 NULL, /* class_data */
67
68 sizeof (GtkSHRuler),
69 0, /* n_preallocs */
70 (GInstanceInitFunc) gtk_shruler_init,
71 };
72 /* inherit from GtkHRuler */
73 shruler_type = g_type_register_static (GTK_TYPE_HRULER, "GtkSHRuler", &shruler_info, (GTypeFlags)0);
74 }
75 return shruler_type;
76}
77
78static void
79gtk_shruler_class_init(GtkSHRulerClass * klass)
80{
81 GtkWidgetClass * widget_class;
82 GtkRulerClass * hruler_class;
83
84 widget_class = (GtkWidgetClass*) klass;
85 hruler_class = (GtkRulerClass*) klass;
86
87 /* just neglect motion notify events */
88 widget_class->motion_notify_event = NULL /* gtk_shruler_motion_notify */;
89
90 /* we want the old ruler draw ticks... */
91 /* ruler_class->draw_ticks = gtk_hruler_draw_ticks; */
92 hruler_class->draw_ticks = gtk_shruler_draw_ticks;
93
94 /* unimplemented draw pos */
95 hruler_class->draw_pos = NULL;
96/*
97 hruler_class->draw_pos = gtk_shruler_draw_pos;
98*/
99}
100
101static void
102gtk_shruler_init (GtkSHRuler * shruler)
103{
104 GtkWidget * widget;
105
106 widget = GTK_WIDGET (shruler);
107 widget->requisition.width = widget->style->xthickness * 2 + 1;
108 widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
109 shruler->start_pos = 0;
110}
111
112
113GtkWidget*
114gtk_shruler_new(void)
115{
116 return GTK_WIDGET( g_object_new( gtk_shruler_get_type(), NULL ) );
117}
118
119void
120gtk_shruler_set_start_pos(GtkSHRuler *ruler, gint pos)
121{
122 g_return_if_fail (GTK_IS_SHRULER (ruler));
123
124 ruler->start_pos = pos;
125}
126
127gint
128gtk_shruler_get_start_pos(GtkSHRuler *ruler)
129{
130 g_return_val_if_fail (GTK_IS_SHRULER (ruler), 0);
131
132 return ruler->start_pos;
133}
134
135static void
136gtk_shruler_draw_ticks(GtkRuler *ruler)
137{
138 GtkWidget *widget;
139 GdkGC *gc, *bg_gc;
140 gint i;
141 gint width, height;
142 gint xthickness;
143 gint ythickness;
144 gint pos;
145
146 g_return_if_fail (ruler != NULL);
147 g_return_if_fail (GTK_IS_HRULER (ruler));
148
149 if (!GTK_WIDGET_DRAWABLE (ruler))
150 return;
151
152 widget = GTK_WIDGET (ruler);
153
154 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
155 bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
156
157 xthickness = widget->style->xthickness;
158 ythickness = widget->style->ythickness;
159
160 width = widget->allocation.width;
161 height = widget->allocation.height - ythickness * 2;
162
163 gtk_paint_box (widget->style, ruler->backing_store,
164 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
165 NULL, widget, "hruler",
166 0, 0,
167 widget->allocation.width, widget->allocation.height);
168
169#if 0
170 gdk_draw_line (ruler->backing_store, gc,
171 xthickness,
172 height + ythickness,
173 widget->allocation.width - xthickness,
174 height + ythickness);
175#endif
176
177 /* assume ruler->max_size has the char width */
178 /* i is increment of char_width, pos is label number
179 * y position is based on height of widget itself */
180 for ( pos = GTK_SHRULER(ruler)->start_pos, i = 0; pos < widget->allocation.width - xthickness; pos += ruler->max_size, i++ ) {
181 gint length = height / 8;
182
183 if ( i % 10 == 0 ) length = ( 2 * height / 3 );
184 else if ( i % 5 == 0 ) length = ( height / 3 );
185
186 gdk_draw_line(ruler->backing_store, gc,
187 pos, height + ythickness,
188 pos, height - length);
189
190 if ( i % 10 == 0 ) {
191 gchar buf[8];
192 PangoLayout *layout;
193
194 /* draw label */
195 g_snprintf(buf, sizeof buf, "%d", i);
196
197 layout = gtk_widget_create_pango_layout
198 (GTK_WIDGET(ruler), buf);
199
200 gdk_draw_layout(ruler->backing_store, gc, pos + 2,
201 0, layout);
202
203 g_object_unref(layout);
204 }
205 }
206}
207
208/* gtk_ruler_set_pos() - does not work yet, need to reimplement
209 * gtk_ruler_draw_pos(). */
210void
211gtk_shruler_set_pos(GtkSHRuler * ruler, gfloat pos)
212{
213 GtkRuler * ruler_;
214 g_return_if_fail( ruler != NULL );
215
216 ruler_ = GTK_RULER(ruler);
217
218 if ( pos < ruler_->lower )
219 pos = ruler_->lower;
220 if ( pos > ruler_->upper )
221 pos = ruler_->upper;
222
223 ruler_->position = pos;
224
225 /* Make sure the ruler has been allocated already */
226 if ( ruler_->backing_store != NULL )
227 gtk_ruler_draw_pos(ruler_);
228}