48283623cda230b05d4b8610985376892ad7812d
[claws.git] / src / gtk / gtkvscrollbutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 /*
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/. 
23  */
24
25  /*
26   * Simple composite widget to provide vertical scrolling, based
27   * on GtkRange widget code.
28   * Modified by the Sylpheed Team and others 2003
29   */
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #endif
34
35 #include <glib.h>
36 #include <gtk/gtksignal.h>
37 #include <gtk/gtkbutton.h>
38 #include <gtk/gtkmisc.h>
39 #include <gtk/gtk.h>
40 #include <gtk/gtkbox.h>
41 #include <gtk/gtkmain.h>
42
43 #include "gtkvscrollbutton.h"
44
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
49
50
51 enum {
52     ARG_0,
53     ARG_ADJUSTMENT
54 };
55
56 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass * klass);
57 static void gtk_vscrollbutton_init(GtkVScrollbutton * vscrollbutton);
58
59 GtkType gtk_vscrollbutton_get_type              (void);
60 GtkWidget *gtk_vscrollbutton_new                (GtkAdjustment    *adjustment);
61
62 static gint gtk_vscrollbutton_button_release    (GtkWidget        *widget,
63                                                  GdkEventButton   *event,
64                                                  GtkVScrollbutton *scrollbutton);
65
66 static void gtk_vscrollbutton_set_adjustment    (GtkVScrollbutton *scrollbutton,
67                                                  GtkAdjustment    *adjustment);
68
69 static gint gtk_vscrollbutton_button_press      (GtkWidget        *widget,
70                                                  GdkEventButton   *event,
71                                                  GtkVScrollbutton *scrollbutton);
72
73 static gint gtk_vscrollbutton_button_release    (GtkWidget        *widget,
74                                                  GdkEventButton   *event,
75                                                  GtkVScrollbutton *scrollbutton);
76
77 gint gtk_vscrollbutton_scroll           (GtkVScrollbutton *scrollbutton);
78
79 static gboolean gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton);
80
81 static void gtk_vscrollbutton_add_timer         (GtkVScrollbutton *scrollbutton);
82
83 static void gtk_vscrollbutton_remove_timer      (GtkVScrollbutton *scrollbutton);
84
85 static gint gtk_real_vscrollbutton_timer        (GtkVScrollbutton *scrollbutton);
86
87 static void gtk_vscrollbutton_set_sensitivity   (GtkAdjustment    *adjustment,
88                                                  GtkVScrollbutton *scrollbutton);
89
90 GtkType gtk_vscrollbutton_get_type(void)
91 {
92     static GtkType vscrollbutton_type = 0;
93
94     if (!vscrollbutton_type) {
95         static const GtkTypeInfo vscrollbutton_info = {
96             "GtkVScrollbutton",
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,
104         };
105
106         vscrollbutton_type =
107             gtk_type_unique(GTK_TYPE_VBOX, &vscrollbutton_info);
108     }
109
110     return vscrollbutton_type;
111 }
112
113 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass *class)
114 {
115 }
116
117 static void gtk_vscrollbutton_init(GtkVScrollbutton *scrollbutton)
118 {
119     GtkWidget *arrow;
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);
134
135     g_signal_connect(G_OBJECT(scrollbutton->upbutton),
136                        "button_press_event",
137                        G_CALLBACK(gtk_vscrollbutton_button_press),
138                        scrollbutton);
139     g_signal_connect(G_OBJECT(scrollbutton->downbutton),
140                        "button_press_event",
141                        G_CALLBACK(gtk_vscrollbutton_button_press),
142                        scrollbutton);
143     g_signal_connect(G_OBJECT(scrollbutton->upbutton),
144                      "button_release_event",
145                      G_CALLBACK
146                      (gtk_vscrollbutton_button_release), scrollbutton);
147     g_signal_connect(G_OBJECT(scrollbutton->downbutton),
148                      "button_release_event",
149                      G_CALLBACK
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;
156 }
157
158 GtkWidget *gtk_vscrollbutton_new(GtkAdjustment *adjustment)
159 {
160     GtkWidget *vscrollbutton;
161     vscrollbutton = GTK_WIDGET(gtk_type_new(gtk_vscrollbutton_get_type()));
162     gtk_vscrollbutton_set_adjustment(GTK_VSCROLLBUTTON(vscrollbutton),
163                                      adjustment);
164     g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
165                        "value_changed",
166                        G_CALLBACK
167                        (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
168     g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
169                        "changed",
170                        G_CALLBACK
171                        (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
172     return vscrollbutton;
173 }
174
175
176 void gtk_vscrollbutton_set_adjustment(GtkVScrollbutton *scrollbutton,
177                                       GtkAdjustment *adjustment)
178 {
179     g_return_if_fail(scrollbutton != NULL);
180     g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
181
182     if (!adjustment)
183             adjustment =
184             GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
185     else
186         g_return_if_fail(GTK_IS_ADJUSTMENT(adjustment));
187
188     if (scrollbutton->adjustment != adjustment) {
189         if (scrollbutton->adjustment) {
190             g_signal_handlers_disconnect_matched(scrollbutton->adjustment,
191                                                  G_SIGNAL_MATCH_DATA,
192                                                  0, 0, NULL, NULL, 
193                                                  (gpointer) scrollbutton);
194             g_object_unref(G_OBJECT(scrollbutton->adjustment));
195         }
196
197         scrollbutton->adjustment = adjustment;
198         g_object_ref(G_OBJECT(adjustment));
199         gtk_object_sink(GTK_OBJECT(adjustment));
200     }
201 }
202
203 static gint gtk_vscrollbutton_button_press(GtkWidget *widget,
204                                            GdkEventButton *event,
205                                            GtkVScrollbutton *scrollbutton)
206 {
207     if (!GTK_WIDGET_HAS_FOCUS(widget))
208         gtk_widget_grab_focus(widget);
209
210     if (scrollbutton->button == 0) {
211         gtk_grab_add(widget);
212         scrollbutton->button = event->button;
213
214         if (widget == scrollbutton->downbutton)
215             scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
216         else
217             scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
218         gtk_vscrollbutton_scroll(scrollbutton);
219         gtk_vscrollbutton_add_timer(scrollbutton);
220     }
221     return TRUE;
222 }
223
224
225 static gint gtk_vscrollbutton_button_release(GtkWidget *widget,
226                                              GdkEventButton *event,
227                                              GtkVScrollbutton *scrollbutton)
228 {
229     if (!GTK_WIDGET_HAS_FOCUS(widget))
230         gtk_widget_grab_focus(widget);
231
232     if (scrollbutton->button == event->button) {
233         gtk_grab_remove(widget);
234
235         scrollbutton->button = 0;
236         gtk_vscrollbutton_remove_timer(scrollbutton);
237         gtk_vscrollbutton_set_sensitivity(scrollbutton->adjustment, scrollbutton);
238     }
239     return TRUE;
240 }
241
242 gint gtk_vscrollbutton_scroll(GtkVScrollbutton *scrollbutton)
243 {
244     gfloat new_value;
245     gint return_val;
246
247     g_return_val_if_fail(scrollbutton != NULL, FALSE);
248     g_return_val_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton), FALSE);
249
250     new_value = scrollbutton->adjustment->value;
251     return_val = TRUE;
252
253     switch (scrollbutton->scroll_type) {
254
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;
259             return_val = FALSE;
260             scrollbutton->timer = 0;
261         }
262         break;
263
264     case GTK_SCROLL_STEP_FORWARD:
265         new_value += scrollbutton->adjustment->step_increment;
266         if (new_value >=
267             (scrollbutton->adjustment->upper -
268              scrollbutton->adjustment->page_size)) {
269             new_value =
270                 scrollbutton->adjustment->upper -
271                 scrollbutton->adjustment->page_size;
272             return_val = FALSE;
273             scrollbutton->timer = 0;
274         }
275         break;
276     
277     default:
278         break;
279     
280     }
281
282     if (new_value != scrollbutton->adjustment->value) {
283         scrollbutton->adjustment->value = new_value;
284         g_signal_emit_by_name(G_OBJECT
285                                 (scrollbutton->adjustment),
286                                 "value_changed");
287         gtk_widget_queue_resize(GTK_WIDGET(scrollbutton)); /* ensure resize */
288     }
289
290     return return_val;
291 }
292
293 static gboolean
294 gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton)
295 {
296     /*
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.
299      */
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.
305          */
306         g_source_remove(scrollbutton->timer);
307         scrollbutton->timer = g_timeout_add(SCROLL_LATER_DELAY,
308                                             (GtkFunction)
309                                             gtk_real_vscrollbutton_timer,
310                                             scrollbutton);
311     }
312     g_object_unref(G_OBJECT(scrollbutton));
313     return FALSE;               /* don't keep calling this function */
314 }
315
316
317 static void gtk_vscrollbutton_add_timer(GtkVScrollbutton *scrollbutton)
318 {
319     g_return_if_fail(scrollbutton != NULL);
320     g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
321
322     if (!scrollbutton->timer) {
323         scrollbutton->need_timer = TRUE;
324         scrollbutton->timer = g_timeout_add(SCROLL_INITIAL_DELAY,
325                                             (GtkFunction)
326                                             gtk_vscrollbutton_timer_1st_time,
327                                             scrollbutton);
328     }
329 }
330
331 static void gtk_vscrollbutton_remove_timer(GtkVScrollbutton *scrollbutton)
332 {
333     g_return_if_fail(scrollbutton != NULL);
334     g_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
335
336     if (scrollbutton->timer) {
337         g_source_remove(scrollbutton->timer);
338         scrollbutton->timer = 0;
339     }
340     scrollbutton->need_timer = FALSE;
341 }
342
343 static gint gtk_real_vscrollbutton_timer(GtkVScrollbutton *scrollbutton)
344 {
345     gint return_val;
346
347     GDK_THREADS_ENTER();
348
349     return_val = TRUE;
350     if (!scrollbutton->timer) {
351         return_val = FALSE;
352         if (scrollbutton->need_timer)
353             scrollbutton->timer =
354                 g_timeout_add(SCROLL_TIMER_LENGTH, 
355                               (GtkFunction) gtk_real_vscrollbutton_timer,
356                               (gpointer) scrollbutton);
357         else {
358             GDK_THREADS_LEAVE();
359             return FALSE;
360         }
361         scrollbutton->need_timer = FALSE;
362     }
363     GDK_THREADS_LEAVE();
364     return_val = gtk_vscrollbutton_scroll(scrollbutton);
365     return return_val;
366 }
367
368 static void gtk_vscrollbutton_set_sensitivity   (GtkAdjustment    *adjustment,
369                                                  GtkVScrollbutton *scrollbutton)
370 {
371         if (!GTK_WIDGET_REALIZED(GTK_WIDGET(scrollbutton))) return;
372         if (scrollbutton->button != 0) return; /* not while something is pressed */
373         
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)));
378 }