1/*
2 * Copyright (C) 2004, 2006 Apple 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 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 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/PassRefPtr.h>
34
35namespace WebCore {
36
37class GraphicsContext;
38class IntRect;
39class PlatformMouseEvent;
40class ScrollableArea;
41class ScrollbarTheme;
42
43class Scrollbar : public Widget,
44                  public ScrollbarThemeClient {
45
46public:
47    // 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.
48    static PassRefPtr<Scrollbar> createNativeScrollbar(ScrollableArea*, ScrollbarOrientation orientation, ScrollbarControlSize size);
49
50    virtual ~Scrollbar();
51
52    // ScrollbarThemeClient implementation.
53    virtual int x() const override { return Widget::x(); }
54    virtual int y() const override { return Widget::y(); }
55    virtual int width() const override { return Widget::width(); }
56    virtual int height() const override { return Widget::height(); }
57    virtual IntSize size() const override { return Widget::size(); }
58    virtual IntPoint location() const override { return Widget::location(); }
59
60    virtual ScrollView* parent() const override { return Widget::parent(); }
61    virtual ScrollView* root() const override { return Widget::root(); }
62
63    virtual void setFrameRect(const IntRect&) override;
64    virtual IntRect frameRect() const override { return Widget::frameRect(); }
65
66    virtual void invalidate() override { Widget::invalidate(); }
67    virtual void invalidateRect(const IntRect&) override;
68
69    virtual ScrollbarOverlayStyle scrollbarOverlayStyle() const override;
70    virtual bool isScrollableAreaActive() const override;
71    virtual bool isScrollViewScrollbar() const override;
72
73    virtual IntPoint convertFromContainingWindow(const IntPoint& windowPoint) override { return Widget::convertFromContainingWindow(windowPoint); }
74
75    virtual bool isCustomScrollbar() const override final { return m_isCustomScrollbar; }
76    virtual ScrollbarOrientation orientation() const override { return m_orientation; }
77
78    virtual int value() const override { return lroundf(m_currentPos); }
79    virtual float currentPos() const override { return m_currentPos; }
80    virtual int visibleSize() const override { return m_visibleSize; }
81    virtual int totalSize() const override { return m_totalSize; }
82    virtual int maximum() const override { return m_totalSize - m_visibleSize; }
83    virtual ScrollbarControlSize controlSize() const override { return m_controlSize; }
84
85    virtual int lineStep() const override { return m_lineStep; }
86    virtual int pageStep() const override { return m_pageStep; }
87
88    virtual ScrollbarPart pressedPart() const override { return m_pressedPart; }
89    virtual ScrollbarPart hoveredPart() const override { return m_hoveredPart; }
90
91    virtual void styleChanged() override { }
92
93    virtual bool enabled() const override { return m_enabled; }
94    virtual void setEnabled(bool) override;
95
96    // Called by the ScrollableArea when the scroll offset changes.
97    void offsetDidChange();
98
99    static int pixelsPerLineStep() { return 40; }
100    static float minFractionToStepWhenPaging() { return 0.875f; }
101    static int maxOverlapBetweenPages();
102
103    void disconnectFromScrollableArea() { m_scrollableArea = 0; }
104    ScrollableArea* scrollableArea() const { return m_scrollableArea; }
105
106    int pressedPos() const { return m_pressedPos; }
107
108    float pixelStep() const { return m_pixelStep; }
109
110    virtual void setHoveredPart(ScrollbarPart);
111    virtual void setPressedPart(ScrollbarPart);
112
113    void setSteps(int lineStep, int pageStep, int pixelsPerStep = 1);
114    void setProportion(int visibleSize, int totalSize);
115    void setPressedPos(int p) { m_pressedPos = p; }
116
117    virtual void paint(GraphicsContext*, const IntRect& damageRect) override;
118
119    virtual bool isOverlayScrollbar() const override;
120    bool shouldParticipateInHitTesting();
121
122    bool isWindowActive() const;
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#if !PLATFORM(IOS)
128    bool mouseMoved(const PlatformMouseEvent&);
129#endif
130    void mouseEntered();
131    bool mouseExited();
132
133    // Used by some platform scrollbars to know when they've been released from capture.
134    bool mouseUp(const PlatformMouseEvent&);
135
136    bool mouseDown(const PlatformMouseEvent&);
137
138    ScrollbarTheme* theme() const { return m_theme; }
139
140    virtual void setParent(ScrollView*) override;
141
142    bool suppressInvalidation() const { return m_suppressInvalidation; }
143    void setSuppressInvalidation(bool s) { m_suppressInvalidation = s; }
144
145    virtual IntRect convertToContainingView(const IntRect&) const override;
146    virtual IntRect convertFromContainingView(const IntRect&) const override;
147
148    virtual IntPoint convertToContainingView(const IntPoint&) const override;
149    virtual IntPoint convertFromContainingView(const IntPoint&) const override;
150
151    void moveThumb(int pos, bool draggingDocument = false);
152
153    virtual bool isAlphaLocked() const override { return m_isAlphaLocked; }
154    virtual void setIsAlphaLocked(bool flag) override { m_isAlphaLocked = flag; }
155
156    virtual bool supportsUpdateOnSecondaryThread() const;
157
158protected:
159    Scrollbar(ScrollableArea*, ScrollbarOrientation, ScrollbarControlSize, ScrollbarTheme* = 0, bool isCustomScrollbar = false);
160
161    void updateThumb();
162    virtual void updateThumbPosition();
163    virtual void updateThumbProportion();
164
165    void autoscrollTimerFired(Timer<Scrollbar>&);
166    void startTimerIfNeeded(double delay);
167    void stopTimerIfNeeded();
168    void autoscrollPressedPart(double delay);
169    ScrollDirection pressedPartScrollDirection();
170    ScrollGranularity pressedPartScrollGranularity();
171
172    ScrollableArea* m_scrollableArea;
173    ScrollbarOrientation m_orientation;
174    ScrollbarControlSize m_controlSize;
175    ScrollbarTheme* m_theme;
176
177    int m_visibleSize;
178    int m_totalSize;
179    float m_currentPos;
180    float m_dragOrigin;
181    int m_lineStep;
182    int m_pageStep;
183    float m_pixelStep;
184
185    ScrollbarPart m_hoveredPart;
186    ScrollbarPart m_pressedPart;
187    int m_pressedPos;
188    float m_scrollPos;
189    bool m_draggingDocument;
190    int m_documentDragPos;
191
192    bool m_enabled;
193
194    Timer<Scrollbar> m_scrollTimer;
195    bool m_overlapsResizer;
196
197    bool m_suppressInvalidation;
198
199    bool m_isAlphaLocked;
200
201    bool m_isCustomScrollbar;
202
203private:
204    virtual bool isScrollbar() const override { return true; }
205};
206
207WIDGET_TYPE_CASTS(Scrollbar, isScrollbar());
208
209} // namespace WebCore
210
211#endif // Scrollbar_h
212