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 "platform/Timer.h"
30#include "platform/Widget.h"
31#include "platform/scroll/ScrollTypes.h"
32#include "platform/scroll/ScrollbarThemeClient.h"
33#include "wtf/MathExtras.h"
34#include "wtf/PassRefPtr.h"
35
36namespace WebCore {
37
38class GraphicsContext;
39class IntRect;
40class PlatformGestureEvent;
41class PlatformMouseEvent;
42class ScrollableArea;
43class ScrollbarTheme;
44class ScrollView;
45
46class PLATFORM_EXPORT Scrollbar : public Widget,
47                  public ScrollbarThemeClient {
48
49public:
50    static PassRefPtr<Scrollbar> create(ScrollableArea*, ScrollbarOrientation, ScrollbarControlSize);
51
52    virtual ~Scrollbar();
53
54    // ScrollbarThemeClient implementation.
55    virtual int x() const OVERRIDE { return Widget::x(); }
56    virtual int y() const OVERRIDE { return Widget::y(); }
57    virtual int width() const OVERRIDE { return Widget::width(); }
58    virtual int height() const OVERRIDE { return Widget::height(); }
59    virtual IntSize size() const OVERRIDE { return Widget::size(); }
60    virtual IntPoint location() const OVERRIDE { return Widget::location(); }
61
62    virtual Widget* parent() const OVERRIDE { return Widget::parent(); }
63    virtual Widget* root() const OVERRIDE { return Widget::root(); }
64
65    void removeFromParent();
66    ScrollView* parentScrollView() const;
67    ScrollView* rootScrollView() const;
68
69    virtual void setFrameRect(const IntRect&) OVERRIDE;
70    virtual IntRect frameRect() const OVERRIDE { return Widget::frameRect(); }
71
72    virtual void invalidate() OVERRIDE { Widget::invalidate(); }
73    virtual void invalidateRect(const IntRect&) OVERRIDE;
74
75    virtual ScrollbarOverlayStyle scrollbarOverlayStyle() const OVERRIDE;
76    virtual void getTickmarks(Vector<IntRect>&) const OVERRIDE;
77    virtual bool isScrollableAreaActive() const OVERRIDE;
78    virtual bool isScrollViewScrollbar() const OVERRIDE;
79
80    virtual IntPoint convertFromContainingWindow(const IntPoint& windowPoint) OVERRIDE { return Widget::convertFromContainingWindow(windowPoint); }
81
82    virtual bool isCustomScrollbar() const OVERRIDE { return false; }
83    virtual ScrollbarOrientation orientation() const OVERRIDE { return m_orientation; }
84    virtual bool isLeftSideVerticalScrollbar() const OVERRIDE;
85
86    virtual int value() const OVERRIDE { return lroundf(m_currentPos); }
87    virtual float currentPos() const OVERRIDE { return m_currentPos; }
88    virtual int visibleSize() const OVERRIDE { return m_visibleSize; }
89    virtual int totalSize() const OVERRIDE { return m_totalSize; }
90    virtual int maximum() const OVERRIDE { return m_totalSize - m_visibleSize; }
91    virtual ScrollbarControlSize controlSize() const OVERRIDE { return m_controlSize; }
92
93    virtual ScrollbarPart pressedPart() const OVERRIDE { return m_pressedPart; }
94    virtual ScrollbarPart hoveredPart() const OVERRIDE { return m_hoveredPart; }
95
96    virtual void styleChanged() OVERRIDE { }
97
98    virtual bool enabled() const OVERRIDE { return m_enabled; }
99    virtual void setEnabled(bool) OVERRIDE;
100
101    // Called by the ScrollableArea when the scroll offset changes.
102    void offsetDidChange();
103
104    void disconnectFromScrollableArea() { m_scrollableArea = 0; }
105    ScrollableArea* scrollableArea() const { return m_scrollableArea; }
106
107    int pressedPos() const { return m_pressedPos; }
108
109    virtual void setHoveredPart(ScrollbarPart);
110    virtual void setPressedPart(ScrollbarPart);
111
112    void setProportion(int visibleSize, int totalSize);
113    void setPressedPos(int p) { m_pressedPos = p; }
114
115    virtual void paint(GraphicsContext*, const IntRect& damageRect) OVERRIDE;
116
117    virtual bool isOverlayScrollbar() const OVERRIDE;
118    bool shouldParticipateInHitTesting();
119
120    bool isWindowActive() const;
121
122    bool gestureEvent(const PlatformGestureEvent&);
123
124    // These methods are used for platform scrollbars to give :hover feedback.  They will not get called
125    // when the mouse went down in a scrollbar, since it is assumed the scrollbar will start
126    // grabbing all events in that case anyway.
127    void mouseMoved(const PlatformMouseEvent&);
128    void mouseEntered();
129    void mouseExited();
130
131    // Used by some platform scrollbars to know when they've been released from capture.
132    void mouseUp(const PlatformMouseEvent&);
133    void mouseDown(const PlatformMouseEvent&);
134
135    ScrollbarTheme* theme() const { return m_theme; }
136
137    virtual void setParent(Widget*) OVERRIDE;
138
139    bool suppressInvalidation() const { return m_suppressInvalidation; }
140    void setSuppressInvalidation(bool s) { m_suppressInvalidation = s; }
141
142    virtual IntRect convertToContainingView(const IntRect&) const OVERRIDE;
143    virtual IntRect convertFromContainingView(const IntRect&) const OVERRIDE;
144
145    virtual IntPoint convertToContainingView(const IntPoint&) const OVERRIDE;
146    virtual IntPoint convertFromContainingView(const IntPoint&) const OVERRIDE;
147
148    void moveThumb(int pos, bool draggingDocument = false);
149
150    virtual bool isAlphaLocked() const OVERRIDE { return m_isAlphaLocked; }
151    virtual void setIsAlphaLocked(bool flag) OVERRIDE { m_isAlphaLocked = flag; }
152
153protected:
154    Scrollbar(ScrollableArea*, ScrollbarOrientation, ScrollbarControlSize, ScrollbarTheme* = 0);
155
156    void updateThumb();
157    virtual void updateThumbPosition();
158    virtual void updateThumbProportion();
159
160    void autoscrollTimerFired(Timer<Scrollbar>*);
161    void startTimerIfNeeded(double delay);
162    void stopTimerIfNeeded();
163    void autoscrollPressedPart(double delay);
164    ScrollDirection pressedPartScrollDirection();
165    ScrollGranularity pressedPartScrollGranularity();
166
167    ScrollableArea* m_scrollableArea;
168    ScrollbarOrientation m_orientation;
169    ScrollbarControlSize m_controlSize;
170    ScrollbarTheme* m_theme;
171
172    int m_visibleSize;
173    int m_totalSize;
174    float m_currentPos;
175    float m_dragOrigin;
176
177    ScrollbarPart m_hoveredPart;
178    ScrollbarPart m_pressedPart;
179    int m_pressedPos;
180    float m_scrollPos;
181    bool m_draggingDocument;
182    int m_documentDragPos;
183
184    bool m_enabled;
185
186    Timer<Scrollbar> m_scrollTimer;
187    bool m_overlapsResizer;
188
189    bool m_suppressInvalidation;
190
191    bool m_isAlphaLocked;
192
193private:
194    virtual bool isScrollbar() const OVERRIDE { return true; }
195
196    float scrollableAreaCurrentPos() const;
197};
198
199DEFINE_TYPE_CASTS(Scrollbar, Widget, widget, widget->isScrollbar(), widget.isScrollbar());
200
201} // namespace WebCore
202
203#endif // Scrollbar_h
204