WebCore:
[WebKit-https.git] / WebCore / platform / gtk / ScrollViewGtk.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Computer, Inc. All rights reserved.
3  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
4  * Copyright (C) 2007 Holger Hans Peter Freyther
5  * Copyright (C) 2008 Collabora Ltd.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "ScrollView.h"
33
34 #include "FloatRect.h"
35 #include "GraphicsContext.h"
36 #include "HostWindow.h"
37 #include "IntRect.h"
38 #include "NotImplemented.h"
39 #include "PlatformMouseEvent.h"
40 #include "PlatformWheelEvent.h"
41 #include "ScrollbarGtk.h"
42 #include "ScrollbarTheme.h"
43
44 #include <gtk/gtk.h>
45
46 using namespace std;
47
48 namespace WebCore {
49
50 static void adjustmentChanged(GtkAdjustment* adjustment, gpointer _that)
51 {
52     ScrollView* that = reinterpret_cast<ScrollView*>(_that);
53
54     // Figure out if we really moved.
55     IntSize newOffset = that->scrollOffset();
56     if (adjustment == that->m_horizontalAdjustment)
57         newOffset.setWidth(static_cast<int>(gtk_adjustment_get_value(adjustment)));
58     else if (adjustment == that->m_verticalAdjustment)
59         newOffset.setHeight(static_cast<int>(gtk_adjustment_get_value(adjustment)));
60
61     IntSize scrollDelta = newOffset - that->scrollOffset();
62     if (scrollDelta == IntSize())
63         return;
64     that->setScrollOffset(newOffset);
65
66     if (that->scrollbarsSuppressed())
67         return;
68
69     that->scrollContents(scrollDelta);
70 }
71
72 ScrollView::ScrollView()
73 {
74     init();
75     m_horizontalAdjustment = 0;
76     m_verticalAdjustment = 0;
77 }
78
79 ScrollView::~ScrollView()
80 {
81     destroy();
82     if (m_horizontalAdjustment) {
83         g_signal_handlers_disconnect_by_func(G_OBJECT(m_horizontalAdjustment), (gpointer)adjustmentChanged, this);
84         g_object_unref(m_horizontalAdjustment);
85     }
86
87     if (m_verticalAdjustment) {
88         g_signal_handlers_disconnect_by_func(G_OBJECT(m_verticalAdjustment), (gpointer)adjustmentChanged, this);
89         g_object_unref(m_verticalAdjustment);
90     }
91 }
92
93 /*
94  * The following is assumed:
95  *   (hadj && vadj) || (!hadj && !vadj)
96  */
97 void ScrollView::setGtkAdjustments(GtkAdjustment* hadj, GtkAdjustment* vadj)
98 {
99     ASSERT(!hadj == !vadj);
100
101     if (m_horizontalAdjustment) {
102         g_signal_handlers_disconnect_by_func(G_OBJECT(m_horizontalAdjustment), (gpointer)adjustmentChanged, this);
103         g_signal_handlers_disconnect_by_func(G_OBJECT(m_verticalAdjustment), (gpointer)adjustmentChanged, this);
104         g_object_unref(m_horizontalAdjustment);
105         g_object_unref(m_verticalAdjustment);
106     }
107
108     m_horizontalAdjustment = hadj;
109     m_verticalAdjustment = vadj;
110
111     if (m_horizontalAdjustment) {
112         g_signal_connect(m_horizontalAdjustment, "value-changed", G_CALLBACK(adjustmentChanged), this);
113         g_signal_connect(m_verticalAdjustment, "value-changed", G_CALLBACK(adjustmentChanged), this);
114
115         /*
116          * disable the scrollbars (if we have any) as the GtkAdjustment over
117          */
118         setHasVerticalScrollbar(false);
119         setHasHorizontalScrollbar(false);
120
121         g_object_ref(m_horizontalAdjustment);
122         g_object_ref(m_verticalAdjustment);
123     }
124
125     updateScrollbars(m_scrollOffset);
126 }
127
128 void ScrollView::platformAddChild(Widget* child)
129 {
130     if (!GTK_IS_SOCKET(child->platformWidget()))
131         gtk_container_add(GTK_CONTAINER(containingWindow()), child->platformWidget());
132 }
133
134 void ScrollView::platformRemoveChild(Widget* child)
135 {
136     if (GTK_WIDGET(containingWindow()) == GTK_WIDGET(child->platformWidget())->parent)
137         gtk_container_remove(GTK_CONTAINER(containingWindow()), child->platformWidget());
138 }
139
140 bool ScrollView::platformHandleHorizontalAdjustment(const IntSize& scroll)
141 {
142     if (m_horizontalAdjustment) {
143         m_horizontalAdjustment->page_size = visibleWidth();
144         m_horizontalAdjustment->step_increment = visibleWidth() / 10.0;
145         m_horizontalAdjustment->page_increment = visibleWidth() * 0.9;
146         m_horizontalAdjustment->lower = 0;
147         m_horizontalAdjustment->upper = contentsWidth();
148         gtk_adjustment_changed(m_horizontalAdjustment);
149
150         if (m_scrollOffset.width() != scroll.width()) {
151             m_horizontalAdjustment->value = scroll.width();
152             gtk_adjustment_value_changed(m_horizontalAdjustment);
153         }
154         return true;
155     }
156     return false;
157 }
158
159 bool ScrollView::platformHandleVerticalAdjustment(const IntSize& scroll)
160 {
161     if (m_verticalAdjustment) {
162         m_verticalAdjustment->page_size = visibleHeight();
163         m_verticalAdjustment->step_increment = visibleHeight() / 10.0;
164         m_verticalAdjustment->page_increment = visibleHeight() * 0.9;
165         m_verticalAdjustment->lower = 0;
166         m_verticalAdjustment->upper = contentsHeight();
167         gtk_adjustment_changed(m_verticalAdjustment);
168
169         if (m_scrollOffset.height() != scroll.height()) {
170             m_verticalAdjustment->value = scroll.height();
171             gtk_adjustment_value_changed(m_verticalAdjustment);
172         }
173         return true;
174     } 
175     return false;
176 }
177
178 bool ScrollView::platformHasHorizontalAdjustment() const
179 {
180     return m_horizontalAdjustment != 0;
181 }
182
183 bool ScrollView::platformHasVerticalAdjustment() const
184 {
185     return m_verticalAdjustment != 0;
186 }
187
188 }