2009-10-03 [colin] 3.7.2cvs42
[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/gtk.h>
37 #include "utils.h"
38 #include "gtkvscrollbutton.h"
39
40 #define SCROLL_TIMER_LENGTH  20
41 #define SCROLL_INITIAL_DELAY 100        /* must hold button this long before ... */
42 #define SCROLL_LATER_DELAY   20 /* ... it starts repeating at this rate  */
43 #define SCROLL_DELAY_LENGTH  300
44
45
46 enum {
47     ARG_0,
48     ARG_ADJUSTMENT
49 };
50
51 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass * klass);
52 static void gtk_vscrollbutton_init(GtkVScrollbutton * vscrollbutton);
53
54 GType gtk_vscrollbutton_get_type                (void);
55 GtkWidget *gtk_vscrollbutton_new                (GtkAdjustment    *adjustment);
56
57 static gint gtk_vscrollbutton_button_release    (GtkWidget        *widget,
58                                                  GdkEventButton   *event,
59                                                  GtkVScrollbutton *scrollbutton);
60
61 static void gtk_vscrollbutton_set_adjustment    (GtkVScrollbutton *scrollbutton,
62                                                  GtkAdjustment    *adjustment);
63
64 static gint gtk_vscrollbutton_button_press      (GtkWidget        *widget,
65                                                  GdkEventButton   *event,
66                                                  GtkVScrollbutton *scrollbutton);
67
68 static gint gtk_vscrollbutton_button_release    (GtkWidget        *widget,
69                                                  GdkEventButton   *event,
70                                                  GtkVScrollbutton *scrollbutton);
71
72 gint gtk_vscrollbutton_scroll           (GtkVScrollbutton *scrollbutton);
73
74 static gboolean gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton);
75
76 static void gtk_vscrollbutton_add_timer         (GtkVScrollbutton *scrollbutton);
77
78 static void gtk_vscrollbutton_remove_timer      (GtkVScrollbutton *scrollbutton);
79
80 static gint gtk_real_vscrollbutton_timer        (GtkVScrollbutton *scrollbutton);
81
82 static void gtk_vscrollbutton_set_sensitivity   (GtkAdjustment    *adjustment,
83                                                  GtkVScrollbutton *scrollbutton);
84
85 GType gtk_vscrollbutton_get_type(void)
86 {
87     static GType vscrollbutton_type = 0;
88
89     if (!vscrollbutton_type) {
90         static const GTypeInfo vscrollbutton_info = {
91                         sizeof (GtkVScrollbuttonClass),
92
93                         (GBaseInitFunc) NULL,
94                         (GBaseFinalizeFunc) NULL,
95
96                         (GClassInitFunc) gtk_vscrollbutton_class_init,
97                         (GClassFinalizeFunc) NULL,
98                         NULL,   /* class_data */
99
100                         sizeof (GtkVScrollbutton),
101                         0,      /* n_preallocs */
102                         (GInstanceInitFunc) gtk_vscrollbutton_init,
103         };
104
105         vscrollbutton_type = g_type_register_static (GTK_TYPE_VBOX, "GtkVScrollbutton", &vscrollbutton_info, (GTypeFlags)0);
106     }
107
108     return vscrollbutton_type;
109 }
110
111 static void gtk_vscrollbutton_class_init(GtkVScrollbuttonClass *class)
112 {
113 }
114
115 static GdkCursor *hand_cursor = NULL;
116
117 static gboolean vscroll_visi_notify(GtkWidget *widget,
118                                        GdkEventVisibility *event,
119                                        gpointer data)
120 {
121         gdk_window_set_cursor(widget->window, hand_cursor);
122         return FALSE;
123 }
124
125 static gboolean vscroll_leave_notify(GtkWidget *widget,
126                                       GdkEventCrossing *event,
127                                        gpointer data)
128 {
129         gdk_window_set_cursor(widget->window, NULL);
130         return FALSE;
131 }
132
133 static gboolean vscroll_enter_notify(GtkWidget *widget,
134                                       GdkEventCrossing *event,
135                                        gpointer data)
136 {
137         gdk_window_set_cursor(widget->window, hand_cursor);
138         return FALSE;
139 }
140
141
142 static void gtk_vscrollbutton_init(GtkVScrollbutton *scrollbutton)
143 {
144     GtkWidget *arrow;
145
146     if (!hand_cursor)
147             hand_cursor = gdk_cursor_new(GDK_HAND2);
148
149     scrollbutton->upbutton = gtk_event_box_new();
150     scrollbutton->downbutton = gtk_event_box_new();
151     arrow = gtk_arrow_new(GTK_ARROW_UP, GTK_SHADOW_NONE);
152     gtk_widget_show(arrow);
153     gtk_container_add(GTK_CONTAINER(scrollbutton->upbutton), arrow);
154     gtk_widget_set_size_request(scrollbutton->upbutton, -1, 16);
155     arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
156     gtk_widget_show(arrow);
157     gtk_container_add(GTK_CONTAINER(scrollbutton->downbutton), arrow);
158     gtk_widget_set_size_request(scrollbutton->downbutton, -1, 16);
159     GTK_WIDGET_UNSET_FLAGS(scrollbutton->upbutton, GTK_CAN_FOCUS);
160     GTK_WIDGET_UNSET_FLAGS(scrollbutton->downbutton, GTK_CAN_FOCUS);
161     gtk_widget_show(scrollbutton->downbutton);
162     gtk_widget_show(scrollbutton->upbutton);
163
164     g_signal_connect(G_OBJECT(scrollbutton->upbutton), "motion-notify-event",
165                      G_CALLBACK(vscroll_visi_notify), NULL);
166     g_signal_connect(G_OBJECT(scrollbutton->upbutton), "leave-notify-event",
167                      G_CALLBACK(vscroll_leave_notify), NULL);
168     g_signal_connect(G_OBJECT(scrollbutton->upbutton), "enter-notify-event",
169                      G_CALLBACK(vscroll_enter_notify), NULL);
170     g_signal_connect(G_OBJECT(scrollbutton->downbutton), "motion-notify-event",
171                      G_CALLBACK(vscroll_visi_notify), NULL);
172     g_signal_connect(G_OBJECT(scrollbutton->downbutton), "leave-notify-event",
173                      G_CALLBACK(vscroll_leave_notify), NULL);
174     g_signal_connect(G_OBJECT(scrollbutton->downbutton), "enter-notify-event",
175                      G_CALLBACK(vscroll_enter_notify), NULL);
176
177     g_signal_connect(G_OBJECT(scrollbutton->upbutton),
178                        "button_press_event",
179                        G_CALLBACK(gtk_vscrollbutton_button_press),
180                        scrollbutton);
181     g_signal_connect(G_OBJECT(scrollbutton->downbutton),
182                        "button_press_event",
183                        G_CALLBACK(gtk_vscrollbutton_button_press),
184                        scrollbutton);
185     g_signal_connect(G_OBJECT(scrollbutton->upbutton),
186                      "button_release_event",
187                      G_CALLBACK
188                      (gtk_vscrollbutton_button_release), scrollbutton);
189     g_signal_connect(G_OBJECT(scrollbutton->downbutton),
190                      "button_release_event",
191                      G_CALLBACK
192                      (gtk_vscrollbutton_button_release), scrollbutton);
193     gtk_box_pack_start(GTK_BOX(&scrollbutton->vbox),
194                        scrollbutton->upbutton, TRUE, TRUE, 0);
195     gtk_box_pack_end(GTK_BOX(&scrollbutton->vbox),
196                      scrollbutton->downbutton, TRUE, TRUE, 0);
197     scrollbutton->timer = 0;
198 }
199
200 GtkWidget *gtk_vscrollbutton_new(GtkAdjustment *adjustment)
201 {
202     GtkWidget *vscrollbutton;
203     vscrollbutton = g_object_new (gtk_vscrollbutton_get_type(),
204                         NULL);
205     gtk_vscrollbutton_set_adjustment(GTK_VSCROLLBUTTON(vscrollbutton),
206                                      adjustment);
207     g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
208                        "value_changed",
209                        G_CALLBACK
210                        (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
211     g_signal_connect(G_OBJECT(GTK_VSCROLLBUTTON(vscrollbutton)->adjustment),
212                        "changed",
213                        G_CALLBACK
214                        (gtk_vscrollbutton_set_sensitivity), vscrollbutton);
215     return vscrollbutton;
216 }
217
218
219 void gtk_vscrollbutton_set_adjustment(GtkVScrollbutton *scrollbutton,
220                                       GtkAdjustment *adjustment)
221 {
222     cm_return_if_fail(scrollbutton != NULL);
223     cm_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
224
225     if (!adjustment)
226             adjustment =
227             GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
228     else
229         cm_return_if_fail(GTK_IS_ADJUSTMENT(adjustment));
230
231     if (scrollbutton->adjustment != adjustment) {
232         if (scrollbutton->adjustment) {
233             g_signal_handlers_disconnect_matched(scrollbutton->adjustment,
234                                                  G_SIGNAL_MATCH_DATA,
235                                                  0, 0, NULL, NULL, 
236                                                  (gpointer) scrollbutton);
237             g_object_unref(G_OBJECT(scrollbutton->adjustment));
238         }
239
240         scrollbutton->adjustment = adjustment;
241         g_object_ref(G_OBJECT(adjustment));
242 #if GLIB_CHECK_VERSION(2,10,0)
243         g_object_ref_sink (G_OBJECT(adjustment));
244 #else
245         gtk_object_ref (G_OBJECT (adjustment));
246         gtk_object_sink (G_OBJECT (adjustment));
247 #endif
248     }
249 }
250
251 static gint gtk_vscrollbutton_button_press(GtkWidget *widget,
252                                            GdkEventButton *event,
253                                            GtkVScrollbutton *scrollbutton)
254 {
255     if (!GTK_WIDGET_HAS_FOCUS(widget))
256         gtk_widget_grab_focus(widget);
257
258     if (scrollbutton->button == 0) {
259         gtk_grab_add(widget);
260         scrollbutton->button = event->button;
261
262         if (widget == scrollbutton->downbutton)
263             scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
264         else
265             scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
266         gtk_vscrollbutton_scroll(scrollbutton);
267         gtk_vscrollbutton_add_timer(scrollbutton);
268     }
269     return TRUE;
270 }
271
272
273 static gint gtk_vscrollbutton_button_release(GtkWidget *widget,
274                                              GdkEventButton *event,
275                                              GtkVScrollbutton *scrollbutton)
276 {
277     if (!GTK_WIDGET_HAS_FOCUS(widget))
278         gtk_widget_grab_focus(widget);
279
280     if (scrollbutton->button == event->button) {
281         gtk_grab_remove(widget);
282
283         scrollbutton->button = 0;
284         gtk_vscrollbutton_remove_timer(scrollbutton);
285         gtk_vscrollbutton_set_sensitivity(scrollbutton->adjustment, scrollbutton);
286     }
287     return TRUE;
288 }
289
290 gint gtk_vscrollbutton_scroll(GtkVScrollbutton *scrollbutton)
291 {
292     gfloat new_value;
293     gint return_val;
294
295     cm_return_val_if_fail(scrollbutton != NULL, FALSE);
296     cm_return_val_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton), FALSE);
297
298     new_value = scrollbutton->adjustment->value;
299     return_val = TRUE;
300
301     switch (scrollbutton->scroll_type) {
302
303     case GTK_SCROLL_STEP_BACKWARD:
304         new_value -= scrollbutton->adjustment->step_increment;
305         if (new_value <= scrollbutton->adjustment->lower) {
306             new_value = scrollbutton->adjustment->lower;
307             return_val = FALSE;
308             scrollbutton->timer = 0;
309         }
310         break;
311
312     case GTK_SCROLL_STEP_FORWARD:
313         new_value += scrollbutton->adjustment->step_increment;
314         if (new_value >=
315             (scrollbutton->adjustment->upper -
316              scrollbutton->adjustment->page_size)) {
317             new_value =
318                 scrollbutton->adjustment->upper -
319                 scrollbutton->adjustment->page_size;
320             return_val = FALSE;
321             scrollbutton->timer = 0;
322         }
323         break;
324     
325     default:
326         break;
327     
328     }
329
330     if (new_value != scrollbutton->adjustment->value) {
331         scrollbutton->adjustment->value = new_value;
332         g_signal_emit_by_name(G_OBJECT
333                                 (scrollbutton->adjustment),
334                                 "value_changed");
335         gtk_widget_queue_resize(GTK_WIDGET(scrollbutton)); /* ensure resize */
336     }
337
338     return return_val;
339 }
340
341 static gboolean
342 gtk_vscrollbutton_timer_1st_time(GtkVScrollbutton *scrollbutton)
343 {
344     /*
345      * If the real timeout function succeeds and the timeout is still set,
346      * replace it with a quicker one so successive scrolling goes faster.
347      */
348     g_object_ref(G_OBJECT(scrollbutton));
349     if (scrollbutton->timer) {
350         /* We explicitely remove ourselves here in the paranoia
351          * that due to things happening above in the callback
352          * above, we might have been removed, and another added.
353          */
354         g_source_remove(scrollbutton->timer);
355         scrollbutton->timer = g_timeout_add(SCROLL_LATER_DELAY,
356                                             (GtkFunction)
357                                             gtk_real_vscrollbutton_timer,
358                                             scrollbutton);
359     }
360     g_object_unref(G_OBJECT(scrollbutton));
361     return FALSE;               /* don't keep calling this function */
362 }
363
364
365 static void gtk_vscrollbutton_add_timer(GtkVScrollbutton *scrollbutton)
366 {
367     cm_return_if_fail(scrollbutton != NULL);
368     cm_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
369
370     if (!scrollbutton->timer) {
371         scrollbutton->need_timer = TRUE;
372         scrollbutton->timer = g_timeout_add(SCROLL_INITIAL_DELAY,
373                                             (GtkFunction)
374                                             gtk_vscrollbutton_timer_1st_time,
375                                             scrollbutton);
376     }
377 }
378
379 static void gtk_vscrollbutton_remove_timer(GtkVScrollbutton *scrollbutton)
380 {
381     cm_return_if_fail(scrollbutton != NULL);
382     cm_return_if_fail(GTK_IS_VSCROLLBUTTON(scrollbutton));
383
384     if (scrollbutton->timer) {
385         g_source_remove(scrollbutton->timer);
386         scrollbutton->timer = 0;
387     }
388     scrollbutton->need_timer = FALSE;
389 }
390
391 static gint gtk_real_vscrollbutton_timer(GtkVScrollbutton *scrollbutton)
392 {
393     gint return_val;
394
395     GDK_THREADS_ENTER();
396
397     return_val = TRUE;
398     if (!scrollbutton->timer) {
399         return_val = FALSE;
400         if (scrollbutton->need_timer)
401             scrollbutton->timer =
402                 g_timeout_add(SCROLL_TIMER_LENGTH, 
403                               (GtkFunction) gtk_real_vscrollbutton_timer,
404                               (gpointer) scrollbutton);
405         else {
406             GDK_THREADS_LEAVE();
407             return FALSE;
408         }
409         scrollbutton->need_timer = FALSE;
410     }
411     GDK_THREADS_LEAVE();
412     return_val = gtk_vscrollbutton_scroll(scrollbutton);
413     return return_val;
414 }
415
416 static void gtk_vscrollbutton_set_sensitivity   (GtkAdjustment    *adjustment,
417                                                  GtkVScrollbutton *scrollbutton)
418 {
419         if (!GTK_WIDGET_REALIZED(GTK_WIDGET(scrollbutton))) return;
420         if (scrollbutton->button != 0) return; /* not while something is pressed */
421         
422         gtk_widget_set_sensitive(scrollbutton->upbutton, 
423                                  (adjustment->value > adjustment->lower));
424         gtk_widget_set_sensitive(scrollbutton->downbutton, 
425                                  (adjustment->value <  (adjustment->upper - adjustment->page_size)));
426 }