1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 * Simple composite widget to provide vertical scrolling, based
27 * on GtkRange widget code.
28 * Modified by the Sylpheed Team and others 2003
36 #include <gtk/gtksignal.h>
37 #include <gtk/gtkbutton.h>
38 #include <gtk/gtkmisc.h>
40 #include <gtk/gtkbox.h>
41 #include <gtk/gtkmain.h>
43 #include "gtkvscrollbutton.h"
45 #define SCROLL_TIMER_LENGTH 20
46 #define SCROLL_INITIAL_DELAY 100 /* must hold button this long before ... */
47 #define SCROLL_LATER_DELAY 20 /* ... it starts repeating at this rate */
48 #define SCROLL_DELAY_LENGTH 300
56 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass * klass);
57 static void gtk_vscrollbutton_init(GtkVScrollbutton * vscrollbutton);
59 GtkType gtk_vscrollbutton_get_type (void);
60 GtkWidget *gtk_vscrollbutton_new (GtkAdjustment *adjustment);
62 static gint gtk_vscrollbutton_button_release (GtkWidget *widget,
63 GdkEventButton *event,
64 GtkVScrollbutton *scrollbutton);
66 static void gtk_vscrollbutton_set_adjustment (GtkVScrollbutton *scrollbutton,
67 GtkAdjustment *adjustment);
69 static gint gtk_vscrollbutton_button_press (GtkWidget *widget,
70 GdkEventButton *event,
71 GtkVScrollbutton *scrollbutton);
73 static gint gtk_vscrollbutton_button_release (GtkWidget *widget,
74 GdkEventButton *event,
75 GtkVScrollbutton *scrollbutton);
77 gint gtk_vscrollbutton_scroll (GtkVScrollbutton *scrollbutton);
79 static gboolean gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton);
81 static void gtk_vscrollbutton_add_timer (GtkVScrollbutton *scrollbutton);
83 static void gtk_vscrollbutton_remove_timer (GtkVScrollbutton *scrollbutton);
85 static gint gtk_real_vscrollbutton_timer (GtkVScrollbutton *scrollbutton);
87 static void gtk_vscrollbutton_set_sensitivity (GtkAdjustment *adjustment,
88 GtkVScrollbutton *scrollbutton);
90 GtkType gtk_vscrollbutton_get_type(void)
92 static GtkType vscrollbutton_type = 0;
94 if (!vscrollbutton_type) {
95 static const GtkTypeInfo vscrollbutton_info = {
97 sizeof(GtkVScrollbutton),
98 sizeof(GtkVScrollbuttonClass),
99 (GtkClassInitFunc) gtk_vscrollbutton_class_init,
100 (GtkObjectInitFunc) gtk_vscrollbutton_init,
101 /* reserved_1 */ NULL,
102 /* reserved_2 */ NULL,
103 (GtkClassInitFunc) NULL,
107 gtk_type_unique(GTK_TYPE_VBOX, &vscrollbutton_info);
110 return vscrollbutton_type;
113 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass *class)
117 static void gtk_vscrollbutton_init(GtkVScrollbutton *scrollbutton)
120 scrollbutton->upbutton = gtk_button_new();
121 scrollbutton->downbutton = gtk_button_new();
122 arrow = gtk_arrow_new(GTK_ARROW_UP, GTK_SHADOW_NONE);
123 gtk_widget_show(arrow);
124 gtk_container_add(GTK_CONTAINER(scrollbutton->upbutton), arrow);
125 gtk_widget_set_size_request(scrollbutton->upbutton, -1, 16);
126 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
127 gtk_widget_show(arrow);
128 gtk_container_add(GTK_CONTAINER(scrollbutton->downbutton), arrow);
129 gtk_widget_set_size_request(scrollbutton->downbutton, -1, 16);
130 GTK_WIDGET_UNSET_FLAGS(scrollbutton->upbutton, GTK_CAN_FOCUS);
131 GTK_WIDGET_UNSET_FLAGS(scrollbutton->downbutton, GTK_CAN_FOCUS);
132 gtk_widget_show(scrollbutton->downbutton);
133 gtk_widget_show(scrollbutton->upbutton);
135 g_signal_connect(G_OBJECT(scrollbutton->upbutton),
136 "button_press_event",
137 G_CALLBACK(gtk_vscrollbutton_button_press),
139 g_signal_connect(G_OBJECT(scrollbutton->downbutton),
140 "button_press_event",
141 G_CALLBACK(gtk_vscrollbutton_button_press),
143 g_signal_connect(G_OBJECT(scrollbutton->upbutton),
144 "button_release_event",
146 (gtk_vscrollbutton_button_release), scrollbutton);
147 g_signal_connect(G_OBJECT(scrollbutton->downbutton),
148 "button_release_event",
150 (gtk_vscrollbutton_button_release), scrollbutton);
151 gtk_box_pack_start(GTK_BOX(&scrollbutton->vbox),
152 scrollbutton->upbutton, TRUE, TRUE, 0);
153 gtk_box_pack_end(GTK_BOX(&scrollbutton->vbox),
154 scrollbutton->downbutton, TRUE, TRUE, 0);
155 scrollbutton->timer = 0;
158 GtkWidget *gtk_vscrollbutton_new(GtkAdjustment *adjustment)
160 GtkWidget *vscrollbutton;
161 vscrollbutton = GTK_WIDGET(gtk_type_new(gtk_vscrollbutton_get_type()));
162 gtk_vscrollbutton_set_adjustment(GTK_VSCROLLBUTTON(vscrollbutton),
164 g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
167 (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
168 g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
171 (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
172 return vscrollbutton;
176 void gtk_vscrollbutton_set_adjustment(GtkVScrollbutton *scrollbutton,
177 GtkAdjustment *adjustment)
179 g_return_if_fail(scrollbutton != NULL);
180 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
184 GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
186 g_return_if_fail(GTK_IS_ADJUSTMENT(adjustment));
188 if (scrollbutton->adjustment != adjustment) {
189 if (scrollbutton->adjustment) {
190 g_signal_handlers_disconnect_matched(scrollbutton->adjustment,
193 (gpointer) scrollbutton);
194 g_object_unref(G_OBJECT(scrollbutton->adjustment));
197 scrollbutton->adjustment = adjustment;
198 g_object_ref(G_OBJECT(adjustment));
199 gtk_object_sink(GTK_OBJECT(adjustment));
203 static gint gtk_vscrollbutton_button_press(GtkWidget *widget,
204 GdkEventButton *event,
205 GtkVScrollbutton *scrollbutton)
207 if (!GTK_WIDGET_HAS_FOCUS(widget))
208 gtk_widget_grab_focus(widget);
210 if (scrollbutton->button == 0) {
211 gtk_grab_add(widget);
212 scrollbutton->button = event->button;
214 if (widget == scrollbutton->downbutton)
215 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
217 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
218 gtk_vscrollbutton_scroll(scrollbutton);
219 gtk_vscrollbutton_add_timer(scrollbutton);
225 static gint gtk_vscrollbutton_button_release(GtkWidget *widget,
226 GdkEventButton *event,
227 GtkVScrollbutton *scrollbutton)
229 if (!GTK_WIDGET_HAS_FOCUS(widget))
230 gtk_widget_grab_focus(widget);
232 if (scrollbutton->button == event->button) {
233 gtk_grab_remove(widget);
235 scrollbutton->button = 0;
236 gtk_vscrollbutton_remove_timer(scrollbutton);
237 gtk_vscrollbutton_set_sensitivity(scrollbutton->adjustment, scrollbutton);
242 gint gtk_vscrollbutton_scroll(GtkVScrollbutton *scrollbutton)
247 g_return_val_if_fail(scrollbutton != NULL, FALSE);
248 g_return_val_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton), FALSE);
250 new_value = scrollbutton->adjustment->value;
253 switch (scrollbutton->scroll_type) {
255 case GTK_SCROLL_STEP_BACKWARD:
256 new_value -= scrollbutton->adjustment->step_increment;
257 if (new_value <= scrollbutton->adjustment->lower) {
258 new_value = scrollbutton->adjustment->lower;
260 scrollbutton->timer = 0;
264 case GTK_SCROLL_STEP_FORWARD:
265 new_value += scrollbutton->adjustment->step_increment;
267 (scrollbutton->adjustment->upper -
268 scrollbutton->adjustment->page_size)) {
270 scrollbutton->adjustment->upper -
271 scrollbutton->adjustment->page_size;
273 scrollbutton->timer = 0;
282 if (new_value != scrollbutton->adjustment->value) {
283 scrollbutton->adjustment->value = new_value;
284 g_signal_emit_by_name(G_OBJECT
285 (scrollbutton->adjustment),
287 gtk_widget_queue_resize(GTK_WIDGET(scrollbutton)); /* ensure resize */
294 gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton)
297 * If the real timeout function succeeds and the timeout is still set,
298 * replace it with a quicker one so successive scrolling goes faster.
300 g_object_ref(G_OBJECT(scrollbutton));
301 if (scrollbutton->timer) {
302 /* We explicitely remove ourselves here in the paranoia
303 * that due to things happening above in the callback
304 * above, we might have been removed, and another added.
306 g_source_remove(scrollbutton->timer);
307 scrollbutton->timer = g_timeout_add(SCROLL_LATER_DELAY,
309 gtk_real_vscrollbutton_timer,
312 g_object_unref(G_OBJECT(scrollbutton));
313 return FALSE; /* don't keep calling this function */
317 static void gtk_vscrollbutton_add_timer(GtkVScrollbutton *scrollbutton)
319 g_return_if_fail(scrollbutton != NULL);
320 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
322 if (!scrollbutton->timer) {
323 scrollbutton->need_timer = TRUE;
324 scrollbutton->timer = g_timeout_add(SCROLL_INITIAL_DELAY,
326 gtk_vscrollbutton_timer_1st_time,
331 static void gtk_vscrollbutton_remove_timer(GtkVScrollbutton *scrollbutton)
333 g_return_if_fail(scrollbutton != NULL);
334 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
336 if (scrollbutton->timer) {
337 g_source_remove(scrollbutton->timer);
338 scrollbutton->timer = 0;
340 scrollbutton->need_timer = FALSE;
343 static gint gtk_real_vscrollbutton_timer(GtkVScrollbutton *scrollbutton)
350 if (!scrollbutton->timer) {
352 if (scrollbutton->need_timer)
353 scrollbutton->timer =
354 g_timeout_add(SCROLL_TIMER_LENGTH,
355 (GtkFunction) gtk_real_vscrollbutton_timer,
356 (gpointer) scrollbutton);
361 scrollbutton->need_timer = FALSE;
364 return_val = gtk_vscrollbutton_scroll(scrollbutton);
368 static void gtk_vscrollbutton_set_sensitivity (GtkAdjustment *adjustment,
369 GtkVScrollbutton *scrollbutton)
371 if (!GTK_WIDGET_REALIZED(GTK_WIDGET(scrollbutton))) return;
372 if (scrollbutton->button != 0) return; /* not while something is pressed */
374 gtk_widget_set_sensitive(scrollbutton->upbutton,
375 (adjustment->value > adjustment->lower));
376 gtk_widget_set_sensitive(scrollbutton->downbutton,
377 (adjustment->value < (adjustment->upper - adjustment->page_size)));