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