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 static 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_usize(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_usize(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 gtk_signal_connect(GTK_OBJECT(scrollbutton->upbutton),
137 "button_press_event",
138 GTK_SIGNAL_FUNC(gtk_vscrollbutton_button_press),
140 gtk_signal_connect(GTK_OBJECT(scrollbutton->downbutton),
141 "button_press_event",
142 GTK_SIGNAL_FUNC(gtk_vscrollbutton_button_press),
144 gtk_signal_connect(GTK_OBJECT(scrollbutton->upbutton),
145 "button_release_event",
147 (gtk_vscrollbutton_button_release), scrollbutton);
148 gtk_signal_connect(GTK_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 gtk_signal_connect(GTK_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
168 (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
169 gtk_signal_connect(GTK_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 gtk_signal_disconnect_by_data(GTK_OBJECT
193 adjustment), (gpointer)
195 gtk_object_unref(GTK_OBJECT(scrollbutton->adjustment));
198 scrollbutton->adjustment = adjustment;
199 gtk_object_ref(GTK_OBJECT(adjustment));
200 gtk_object_sink(GTK_OBJECT(adjustment));
204 void gtk_vscrollbutton_reset(GtkVScrollbutton *scrollbutton)
206 /* do what luke does when scrolling */
207 scrollbutton->adjustment->value = scrollbutton->adjustment->lower;
208 gtk_signal_emit_by_name(GTK_OBJECT (scrollbutton->adjustment),
212 static gint gtk_vscrollbutton_button_press(GtkWidget *widget,
213 GdkEventButton *event,
214 GtkVScrollbutton *scrollbutton)
216 if (!GTK_WIDGET_HAS_FOCUS(widget))
217 gtk_widget_grab_focus(widget);
219 if (scrollbutton->button == 0) {
220 gtk_grab_add(widget);
221 scrollbutton->button = event->button;
223 if (widget == scrollbutton->downbutton)
224 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
226 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
227 gtk_vscrollbutton_scroll(scrollbutton);
228 gtk_vscrollbutton_add_timer(scrollbutton);
234 static gint gtk_vscrollbutton_button_release(GtkWidget *widget,
235 GdkEventButton *event,
236 GtkVScrollbutton *scrollbutton)
238 if (!GTK_WIDGET_HAS_FOCUS(widget))
239 gtk_widget_grab_focus(widget);
241 if (scrollbutton->button == event->button) {
242 gtk_grab_remove(widget);
244 scrollbutton->button = 0;
245 gtk_vscrollbutton_remove_timer(scrollbutton);
246 gtk_vscrollbutton_set_sensitivity(scrollbutton->adjustment, scrollbutton);
251 static gint gtk_vscrollbutton_scroll(GtkVScrollbutton *scrollbutton)
256 g_return_val_if_fail(scrollbutton != NULL, FALSE);
257 g_return_val_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton), FALSE);
259 new_value = scrollbutton->adjustment->value;
262 switch (scrollbutton->scroll_type) {
264 case GTK_SCROLL_STEP_BACKWARD:
265 new_value -= scrollbutton->adjustment->step_increment;
266 if (new_value <= scrollbutton->adjustment->lower) {
267 new_value = scrollbutton->adjustment->lower;
269 scrollbutton->timer = 0;
273 case GTK_SCROLL_STEP_FORWARD:
274 new_value += scrollbutton->adjustment->step_increment;
276 (scrollbutton->adjustment->upper -
277 scrollbutton->adjustment->page_size)) {
279 scrollbutton->adjustment->upper -
280 scrollbutton->adjustment->page_size;
282 scrollbutton->timer = 0;
291 if (new_value != scrollbutton->adjustment->value) {
292 scrollbutton->adjustment->value = new_value;
293 gtk_signal_emit_by_name(GTK_OBJECT
294 (scrollbutton->adjustment),
302 gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton)
305 * If the real timeout function succeeds and the timeout is still set,
306 * replace it with a quicker one so successive scrolling goes faster.
308 gtk_object_ref(GTK_OBJECT(scrollbutton));
309 if (scrollbutton->timer) {
310 /* We explicitely remove ourselves here in the paranoia
311 * that due to things happening above in the callback
312 * above, we might have been removed, and another added.
314 g_source_remove(scrollbutton->timer);
315 scrollbutton->timer = gtk_timeout_add(SCROLL_LATER_DELAY,
317 gtk_real_vscrollbutton_timer,
320 gtk_object_unref(GTK_OBJECT(scrollbutton));
321 return FALSE; /* don't keep calling this function */
325 static void gtk_vscrollbutton_add_timer(GtkVScrollbutton *scrollbutton)
327 g_return_if_fail(scrollbutton != NULL);
328 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
330 if (!scrollbutton->timer) {
331 scrollbutton->need_timer = TRUE;
332 scrollbutton->timer = gtk_timeout_add(SCROLL_INITIAL_DELAY,
334 gtk_vscrollbutton_timer_1st_time,
339 static void gtk_vscrollbutton_remove_timer(GtkVScrollbutton *scrollbutton)
341 g_return_if_fail(scrollbutton != NULL);
342 g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
344 if (scrollbutton->timer) {
345 gtk_timeout_remove(scrollbutton->timer);
346 scrollbutton->timer = 0;
348 scrollbutton->need_timer = FALSE;
351 static gint gtk_real_vscrollbutton_timer(GtkVScrollbutton *scrollbutton)
358 if (!scrollbutton->timer) {
360 if (scrollbutton->need_timer)
361 scrollbutton->timer =
362 gtk_timeout_add(SCROLL_TIMER_LENGTH,
363 (GtkFunction) gtk_real_vscrollbutton_timer,
364 (gpointer) scrollbutton);
369 scrollbutton->need_timer = FALSE;
372 return_val = gtk_vscrollbutton_scroll(scrollbutton);
376 static void gtk_vscrollbutton_set_sensitivity (GtkAdjustment *adjustment,
377 GtkVScrollbutton *scrollbutton)
379 if (!GTK_WIDGET_REALIZED(GTK_WIDGET(scrollbutton))) return;
380 if (scrollbutton->button != 0) return; /* not while something is pressed */
382 gtk_widget_set_sensitive(scrollbutton->upbutton,
383 (adjustment->value > adjustment->lower));
384 gtk_widget_set_sensitive(scrollbutton->downbutton,
385 (adjustment->value < (adjustment->upper - adjustment->page_size)));