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 2 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 * Simple composite widget to provide vertical scrolling, based
29 * on GtkRange widget code.
30 * Modified by the Sylpheed Team and others 2003
37 #include <gtk/gtksignal.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkmisc.h>
41 #include <gtk/gtkbox.h>
42 #include <gtk/gtkmain.h>
44 #include "gtkvscrollbutton.h"
46 #define SCROLL_TIMER_LENGTH 20
47 #define SCROLL_INITIAL_DELAY 100 /* must hold button this long before ... */
48 #define SCROLL_LATER_DELAY 20 /* ... it starts repeating at this rate */
49 #define SCROLL_DELAY_LENGTH 300
57 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass * klass);
58 static void gtk_vscrollbutton_init(GtkVScrollbutton * vscrollbutton);
60 GtkType gtk_vscrollbutton_get_type (void);
61 GtkWidget *gtk_vscrollbutton_new (GtkAdjustment *adjustment);
63 static gint gtk_vscrollbutton_button_release (GtkWidget *widget,
64 GdkEventButton *event,
65 GtkVScrollbutton *scrollbutton);
67 static void gtk_vscrollbutton_set_adjustment (GtkVScrollbutton *scrollbutton,
68 GtkAdjustment *adjustment);
70 static gint gtk_vscrollbutton_button_press (GtkWidget *widget,
71 GdkEventButton *event,
72 GtkVScrollbutton *scrollbutton);
74 static gint gtk_vscrollbutton_button_release (GtkWidget *widget,
75 GdkEventButton *event,
76 GtkVScrollbutton *scrollbutton);
78 gint gtk_vscrollbutton_scroll (GtkVScrollbutton *scrollbutton);
80 static gboolean gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton);
82 static void gtk_vscrollbutton_add_timer (GtkVScrollbutton *scrollbutton);
84 static void gtk_vscrollbutton_remove_timer (GtkVScrollbutton *scrollbutton);
86 static gint gtk_real_vscrollbutton_timer (GtkVScrollbutton *scrollbutton);
88 static void gtk_vscrollbutton_set_sensitivity (GtkAdjustment *adjustment,
89 GtkVScrollbutton *scrollbutton);
91 GtkType gtk_vscrollbutton_get_type(void)
93 static GtkType vscrollbutton_type = 0;
95 if (!vscrollbutton_type) {
96 static const GtkTypeInfo vscrollbutton_info = {
98 sizeof(GtkVScrollbutton),
99 sizeof(GtkVScrollbuttonClass),
100 (GtkClassInitFunc) gtk_vscrollbutton_class_init,
101 (GtkObjectInitFunc) gtk_vscrollbutton_init,
102 /* reserved_1 */ NULL,
103 /* reserved_2 */ NULL,
104 (GtkClassInitFunc) NULL,
108 gtk_type_unique(GTK_TYPE_VBOX, &vscrollbutton_info);
111 return vscrollbutton_type;
114 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass *class)
118 static void gtk_vscrollbutton_init(GtkVScrollbutton *scrollbutton)
121 scrollbutton->upbutton = gtk_button_new();
122 scrollbutton->downbutton = gtk_button_new();
123 arrow = gtk_arrow_new(GTK_ARROW_UP, GTK_SHADOW_NONE);
124 gtk_widget_show(arrow);
125 gtk_container_add(GTK_CONTAINER(scrollbutton->upbutton), arrow);
126 gtk_widget_set_size_request(scrollbutton->upbutton, -1, 16);
127 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
128 gtk_widget_show(arrow);
129 gtk_container_add(GTK_CONTAINER(scrollbutton->downbutton), arrow);
130 gtk_widget_set_size_request(scrollbutton->downbutton, -1, 16);
131 GTK_WIDGET_UNSET_FLAGS(scrollbutton->upbutton, GTK_CAN_FOCUS);
132 GTK_WIDGET_UNSET_FLAGS(scrollbutton->downbutton, GTK_CAN_FOCUS);
133 gtk_widget_show(scrollbutton->downbutton);
134 gtk_widget_show(scrollbutton->upbutton);
136 g_signal_connect(G_OBJECT(scrollbutton->upbutton),
137 "button_press_event",
138 G_CALLBACK(gtk_vscrollbutton_button_press),
140 g_signal_connect(G_OBJECT(scrollbutton->downbutton),
141 "button_press_event",
142 G_CALLBACK(gtk_vscrollbutton_button_press),
144 g_signal_connect(G_OBJECT(scrollbutton->upbutton),
145 "button_release_event",
147 (gtk_vscrollbutton_button_release), scrollbutton);
148 g_signal_connect(G_OBJECT(scrollbutton->downbutton),
149 "button_release_event",
151 (gtk_vscrollbutton_button_release), scrollbutton);
152 gtk_box_pack_start(GTK_BOX(&scrollbutton->vbox),
153 scrollbutton->upbutton, TRUE, TRUE, 0);
154 gtk_box_pack_end(GTK_BOX(&scrollbutton->vbox),
155 scrollbutton->downbutton, TRUE, TRUE, 0);
156 scrollbutton->timer = 0;
159 GtkWidget *gtk_vscrollbutton_new(GtkAdjustment *adjustment)
161 GtkWidget *vscrollbutton;
162 vscrollbutton = GTK_WIDGET(gtk_type_new(gtk_vscrollbutton_get_type()));
163 gtk_vscrollbutton_set_adjustment(GTK_VSCROLLBUTTON(vscrollbutton),
165 g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
168 (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
169 g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
172 (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
173 return vscrollbutton;
177 void gtk_vscrollbutton_set_adjustment(GtkVScrollbutton *scrollbutton,
178 GtkAdjustment *adjustment)
180 g_return_if_fail(scrollbutton != NULL);
181 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
185 GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
187 g_return_if_fail(GTK_IS_ADJUSTMENT(adjustment));
189 if (scrollbutton->adjustment != adjustment) {
190 if (scrollbutton->adjustment) {
191 g_signal_handlers_disconnect_matched(scrollbutton->adjustment,
194 (gpointer) scrollbutton);
195 g_object_unref(G_OBJECT(scrollbutton->adjustment));
198 scrollbutton->adjustment = adjustment;
199 g_object_ref(G_OBJECT(adjustment));
200 gtk_object_sink(GTK_OBJECT(adjustment));
204 static gint gtk_vscrollbutton_button_press(GtkWidget *widget,
205 GdkEventButton *event,
206 GtkVScrollbutton *scrollbutton)
208 if (!GTK_WIDGET_HAS_FOCUS(widget))
209 gtk_widget_grab_focus(widget);
211 if (scrollbutton->button == 0) {
212 gtk_grab_add(widget);
213 scrollbutton->button = event->button;
215 if (widget == scrollbutton->downbutton)
216 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
218 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
219 gtk_vscrollbutton_scroll(scrollbutton);
220 gtk_vscrollbutton_add_timer(scrollbutton);
226 static gint gtk_vscrollbutton_button_release(GtkWidget *widget,
227 GdkEventButton *event,
228 GtkVScrollbutton *scrollbutton)
230 if (!GTK_WIDGET_HAS_FOCUS(widget))
231 gtk_widget_grab_focus(widget);
233 if (scrollbutton->button == event->button) {
234 gtk_grab_remove(widget);
236 scrollbutton->button = 0;
237 gtk_vscrollbutton_remove_timer(scrollbutton);
238 gtk_vscrollbutton_set_sensitivity(scrollbutton->adjustment, scrollbutton);
243 gint gtk_vscrollbutton_scroll(GtkVScrollbutton *scrollbutton)
248 g_return_val_if_fail(scrollbutton != NULL, FALSE);
249 g_return_val_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton), FALSE);
251 new_value = scrollbutton->adjustment->value;
254 switch (scrollbutton->scroll_type) {
256 case GTK_SCROLL_STEP_BACKWARD:
257 new_value -= scrollbutton->adjustment->step_increment;
258 if (new_value <= scrollbutton->adjustment->lower) {
259 new_value = scrollbutton->adjustment->lower;
261 scrollbutton->timer = 0;
265 case GTK_SCROLL_STEP_FORWARD:
266 new_value += scrollbutton->adjustment->step_increment;
268 (scrollbutton->adjustment->upper -
269 scrollbutton->adjustment->page_size)) {
271 scrollbutton->adjustment->upper -
272 scrollbutton->adjustment->page_size;
274 scrollbutton->timer = 0;
283 if (new_value != scrollbutton->adjustment->value) {
284 scrollbutton->adjustment->value = new_value;
285 g_signal_emit_by_name(G_OBJECT
286 (scrollbutton->adjustment),
288 gtk_widget_queue_resize(GTK_WIDGET(scrollbutton)); /* ensure resize */
295 gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton)
298 * If the real timeout function succeeds and the timeout is still set,
299 * replace it with a quicker one so successive scrolling goes faster.
301 g_object_ref(G_OBJECT(scrollbutton));
302 if (scrollbutton->timer) {
303 /* We explicitely remove ourselves here in the paranoia
304 * that due to things happening above in the callback
305 * above, we might have been removed, and another added.
307 g_source_remove(scrollbutton->timer);
308 scrollbutton->timer = gtk_timeout_add(SCROLL_LATER_DELAY,
310 gtk_real_vscrollbutton_timer,
313 g_object_unref(G_OBJECT(scrollbutton));
314 return FALSE; /* don't keep calling this function */
318 static void gtk_vscrollbutton_add_timer(GtkVScrollbutton *scrollbutton)
320 g_return_if_fail(scrollbutton != NULL);
321 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
323 if (!scrollbutton->timer) {
324 scrollbutton->need_timer = TRUE;
325 scrollbutton->timer = gtk_timeout_add(SCROLL_INITIAL_DELAY,
327 gtk_vscrollbutton_timer_1st_time,
332 static void gtk_vscrollbutton_remove_timer(GtkVScrollbutton *scrollbutton)
334 g_return_if_fail(scrollbutton != NULL);
335 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
337 if (scrollbutton->timer) {
338 gtk_timeout_remove(scrollbutton->timer);
339 scrollbutton->timer = 0;
341 scrollbutton->need_timer = FALSE;
344 static gint gtk_real_vscrollbutton_timer(GtkVScrollbutton *scrollbutton)
351 if (!scrollbutton->timer) {
353 if (scrollbutton->need_timer)
354 scrollbutton->timer =
355 gtk_timeout_add(SCROLL_TIMER_LENGTH,
356 (GtkFunction) gtk_real_vscrollbutton_timer,
357 (gpointer) scrollbutton);
362 scrollbutton->need_timer = FALSE;
365 return_val = gtk_vscrollbutton_scroll(scrollbutton);
369 static void gtk_vscrollbutton_set_sensitivity (GtkAdjustment *adjustment,
370 GtkVScrollbutton *scrollbutton)
372 if (!GTK_WIDGET_REALIZED(GTK_WIDGET(scrollbutton))) return;
373 if (scrollbutton->button != 0) return; /* not while something is pressed */
375 gtk_widget_set_sensitive(scrollbutton->upbutton,
376 (adjustment->value > adjustment->lower));
377 gtk_widget_set_sensitive(scrollbutton->downbutton,
378 (adjustment->value < (adjustment->upper - adjustment->page_size)));