1/*
2 * Copyright (C) 2004, 2006 Apple Computer, Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef Scrollbar_h
27#define Scrollbar_h
28
29#include "ScrollbarThemeClient.h"
30#include "ScrollTypes.h"
31#include "Timer.h"
32#include "Widget.h"
33#include <wtf/MathExtras.h>
34#include <wtf/PassRefPtr.h>
35
36namespace WebCore {
37
38class GraphicsContext;
39class IntRect;
40class PlatformMouseEvent;
41class ScrollableArea;
42class ScrollbarTheme;
43
44#if ENABLE(GESTURE_EVENTS)
45class PlatformGestureEvent;
46#endif
47
48class Scrollbar : public Widget,
49                  public ScrollbarThemeClient {
50
51public:
52    // Must be implemented by platforms that can't simply use the Scrollbar base class.  Right now the only platform that is not using the base class is GTK.
53    static PassRefPtr<Scrollbar> createNativeScrollbar(ScrollableArea*, ScrollbarOrientation orientation, ScrollbarControlSize size);
54
55    virtual ~Scrollbar();
56
57    // ScrollbarThemeClient implementation.
58    virtual int x() const { return Widget::x(); }
59    virtual int y() const { return Widget::y(); }
60    virtual int width() const { return Widget::width(); }
61    virtual int height() const { return Widget::height(); }
62    virtual IntSize size() const { return Widget::size(); }
63    virtual IntPoint location() const { return Widget::location(); }
64
65    virtual ScrollView* parent() const { return Widget::parent(); }
66    virtual ScrollView* root() const { return Widget::root(); }
67
68    virtual void setFrameRect(const IntRect&);
69    virtual IntRect frameRect() const { return Widget::frameRect(); }
70
71    virtual void invalidate() { Widget::invalidate(); }
72    virtual void invalidateRect(const IntRect&);
73
74    virtual ScrollbarOverlayStyle scrollbarOverlayStyle() const;
75    virtual void getTickmarks(Vector<IntRect>&) const;
76    virtual bool isScrollableAreaActive() const;
77    virtual bool isScrollViewScrollbar() const;
78
79    virtual IntPoint convertFromContainingWindow(const IntPoint& windowPoint) { return Widget::convertFromContainingWindow(windowPoint); }
80
81    virtual bool isCustomScrollbar() const { return false; }
82    virtual ScrollbarOrientation orientation() const { return m_orientation; }
83
84    virtual int value() const { return lroundf(m_currentPos); }
85    virtual float currentPos() const { return m_currentPos; }
86    virtual int visibleSize() const { return m_visibleSize; }
87    virtual int totalSize() const { return m_totalSize; }
88    virtual int maximum() const { return m_totalSize - m_visibleSize; }
89    virtual ScrollbarControlSize controlSize() const { return m_controlSize; }
90
91    virtual int lineStep() const { return m_lineStep; }
92    virtual int pageStep() const { return m_pageStep; }
93
94    virtual ScrollbarPart pressedPart() const { return m_pressedPart; }
95    virtual ScrollbarPart hoveredPart() const { return m_hoveredPart; }
96
97    virtual void styleChanged() { }
98
99    virtual bool enabled() const { return m_enabled; }
100    virtual void setEnabled(bool);
101
102    // Called by the ScrollableArea when the scroll offset changes.
103    void offsetDidChange();
104
105    static int pixelsPerLineStep() { return 40; }
106    static float minFractionToStepWhenPaging() { return 0.875f; }
107    static int maxOverlapBetweenPages();
108
109    void disconnectFromScrollableArea() { m_scrollableArea = 0; }
110    ScrollableArea* scrollableArea() const { return m_scrollableArea; }
111
112    int pressedPos() const { return m_pressedPos; }
113
114    float pixelStep() const { return m_pixelStep; }
115
116    virtual void setHoveredPart(ScrollbarPart);
117    virtual void setPressedPart(ScrollbarPart);
118
119    void setSteps(int lineStep, int pageStep, int pixelsPerStep = 1);
120    void setProportion(int visibleSize, int totalSize);
121    void setPressedPos(int p) { m_pressedPos = p; }
122
123    virtual void paint(GraphicsContext*, const IntRect& damageRect);
124
125    virtual bool isOverlayScrollbar() const;
126    bool shouldParticipateInHitTesting();
127
128    bool isWindowActive() const;
129
130#if ENABLE(GESTURE_EVENTS)
131    bool gestureEvent(const PlatformGestureEvent&);
132#endif
133
134    // These methods are used for platform scrollbars to give :hover feedback.  They will not get called
135    // when the mouse went down in a scrollbar, since it is assumed the scrollbar will start
136    // grabbing all events in that case anyway.
137    bool mouseMoved(const PlatformMouseEvent&);
138    void mouseEntered();
139    bool mouseExited();
140
141    // Used by some platform scrollbars to know when they've been released from capture.
142    bool mouseUp(const PlatformMouseEvent&);
143
144    bool mouseDown(const PlatformMouseEvent&);
145
146    ScrollbarTheme* theme() const { return m_theme; }
147
148    virtual void setParent(ScrollView*);
149
150    bool suppressInvalidation() const { return m_suppressInvalidation; }
151    void setSuppressInvalidation(bool s) { m_suppressInvalidation = s; }
152
153    virtual IntRect convertToContainingView(const IntRect&) const;
154    virtual IntRect convertFromContainingView(const IntRect&) const;
155
156    virtual IntPoint convertToContainingView(const IntPoint&) const;
157    virtual IntPoint convertFromContainingView(const IntPoint&) const;
158
159    void moveThumb(int pos, bool draggingDocument = false);
160
161    virtual bool isAlphaLocked() const { return m_isAlphaLocked; }
162    virtual void setIsAlphaLocked(bool flag) { m_isAlphaLocked = flag; }
163
164protected:
165    Scrollbar(ScrollableArea*, ScrollbarOrientation, ScrollbarControlSize, ScrollbarTheme* = 0);
166
167    void updateThumb();
168    virtual void updateThumbPosition();
169    virtual void updateThumbProportion();
170
171    void autoscrollTimerFired(Timer<Scrollbar>*);
172    void startTimerIfNeeded(double delay);
173    void stopTimerIfNeeded();
174    void autoscrollPressedPart(double delay);
175    ScrollDirection pressedPartScrollDirection();
176    ScrollGranularity pressedPartScrollGranularity();
177
178    ScrollableArea* m_scrollableArea;
179    ScrollbarOrientation m_orientation;
180    ScrollbarControlSize m_controlSize;
181    ScrollbarTheme* m_theme;
182
183    int m_visibleSize;
184    int m_totalSize;
185    float m_currentPos;
186    float m_dragOrigin;
187    int m_lineStep;
188    int m_pageStep;
189    float m_pixelStep;
190
191    ScrollbarPart m_hoveredPart;
192    ScrollbarPart m_pressedPart;
193    int m_pressedPos;
194    float m_scrollPos;
195    bool m_draggingDocument;
196    int m_documentDragPos;
197
198    bool m_enabled;
199
200    Timer<Scrollbar> m_scrollTimer;
201    bool m_overlapsResizer;
202
203    bool m_suppressInvalidation;
204
205    bool m_isAlphaLocked;
206
207private:
208    virtual bool isScrollbar() const { return true; }
209};
210
211} // namespace WebCore
212
213#endif // Scrollbar_h
214