1/*
2 * Copyright (C) 2004, 2006, 2014-2015 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#pragma once
27
28#include "ScrollTypes.h"
29#include "Timer.h"
30#include "Widget.h"
31
32namespace WebCore {
33
34class GraphicsContext;
35class IntRect;
36class PlatformMouseEvent;
37class ScrollableArea;
38class ScrollbarTheme;
39
40class Scrollbar : public Widget {
41public:
42 // 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.
43 WEBCORE_EXPORT static Ref<Scrollbar> createNativeScrollbar(ScrollableArea&, ScrollbarOrientation, ScrollbarControlSize);
44
45 virtual ~Scrollbar();
46
47 // Called by the ScrollableArea when the scroll offset changes.
48 void offsetDidChange();
49
50 static int pixelsPerLineStep() { return 40; }
51 static float minFractionToStepWhenPaging() { return 0.8; }
52 WEBCORE_EXPORT static int maxOverlapBetweenPages();
53 static int pageStep(int viewWidthOrHeight, int contentWidthOrHeight) { return std::max(std::max<int>(lroundf(viewWidthOrHeight * Scrollbar::minFractionToStepWhenPaging()), lroundf(contentWidthOrHeight - Scrollbar::maxOverlapBetweenPages())), 1); }
54 static int pageStep(int viewWidthOrHeight) { return pageStep(viewWidthOrHeight, viewWidthOrHeight); }
55 static float pageStepDelta(int widthOrHeight) { return std::max(std::max(static_cast<float>(widthOrHeight) * Scrollbar::minFractionToStepWhenPaging(), static_cast<float>(widthOrHeight) - Scrollbar::maxOverlapBetweenPages()), 1.0f); }
56
57 ScrollableArea& scrollableArea() const { return m_scrollableArea; }
58
59 virtual bool isCustomScrollbar() const { return false; }
60 ScrollbarOrientation orientation() const { return m_orientation; }
61
62 int value() const { return lroundf(m_currentPos); }
63 float currentPos() const { return m_currentPos; }
64 int pressedPos() const { return m_pressedPos; }
65 int visibleSize() const { return m_visibleSize; }
66 int totalSize() const { return m_totalSize; }
67 int maximum() const { return m_totalSize - m_visibleSize; }
68 ScrollbarControlSize controlSize() const { return m_controlSize; }
69
70 int occupiedWidth() const;
71 int occupiedHeight() const;
72
73 int lineStep() const { return m_lineStep; }
74 int pageStep() const { return m_pageStep; }
75 float pixelStep() const { return m_pixelStep; }
76
77 ScrollbarPart pressedPart() const { return m_pressedPart; }
78 ScrollbarPart hoveredPart() const { return m_hoveredPart; }
79 virtual void setHoveredPart(ScrollbarPart);
80 virtual void setPressedPart(ScrollbarPart);
81
82 WEBCORE_EXPORT void setSteps(int lineStep, int pageStep, int pixelsPerStep = 1);
83 WEBCORE_EXPORT void setProportion(int visibleSize, int totalSize);
84 void setPressedPos(int p) { m_pressedPos = p; }
85
86 void paint(GraphicsContext&, const IntRect& damageRect, Widget::SecurityOriginPaintPolicy = SecurityOriginPaintPolicy::AnyOrigin) override;
87
88 bool enabled() const { return m_enabled; }
89 virtual void setEnabled(bool);
90
91 virtual bool isOverlayScrollbar() const;
92 bool shouldParticipateInHitTesting();
93
94 bool isWindowActive() const;
95
96 // These methods are used for platform scrollbars to give :hover feedback. They will not get called
97 // when the mouse went down in a scrollbar, since it is assumed the scrollbar will start
98 // grabbing all events in that case anyway.
99#if !PLATFORM(IOS_FAMILY)
100 WEBCORE_EXPORT bool mouseMoved(const PlatformMouseEvent&);
101#endif
102 WEBCORE_EXPORT void mouseEntered();
103 WEBCORE_EXPORT bool mouseExited();
104
105 // Used by some platform scrollbars to know when they've been released from capture.
106 WEBCORE_EXPORT bool mouseUp(const PlatformMouseEvent&);
107
108 WEBCORE_EXPORT bool mouseDown(const PlatformMouseEvent&);
109
110 ScrollbarTheme& theme() const { return m_theme; }
111
112 void invalidateRect(const IntRect&) override;
113
114 bool suppressInvalidation() const { return m_suppressInvalidation; }
115 void setSuppressInvalidation(bool s) { m_suppressInvalidation = s; }
116
117 virtual void styleChanged() { }
118
119 IntRect convertToContainingView(const IntRect&) const override;
120 IntRect convertFromContainingView(const IntRect&) const override;
121
122 IntPoint convertToContainingView(const IntPoint&) const override;
123 IntPoint convertFromContainingView(const IntPoint&) const override;
124
125 void moveThumb(int pos, bool draggingDocument = false);
126
127#if !PLATFORM(COCOA)
128 float opacity() const { return m_opacity; }
129 void setOpacity(float opacity) { m_opacity = opacity; }
130#endif
131
132 bool supportsUpdateOnSecondaryThread() const;
133
134protected:
135 Scrollbar(ScrollableArea&, ScrollbarOrientation, ScrollbarControlSize, ScrollbarTheme* = nullptr);
136
137 void updateThumb();
138 virtual void updateThumbPosition();
139 virtual void updateThumbProportion();
140
141 void autoscrollTimerFired();
142 void startTimerIfNeeded(Seconds delay);
143 void stopTimerIfNeeded();
144 void autoscrollPressedPart(Seconds delay);
145 ScrollDirection pressedPartScrollDirection();
146 ScrollGranularity pressedPartScrollGranularity();
147
148 ScrollableArea& m_scrollableArea;
149 ScrollbarOrientation m_orientation;
150 ScrollbarControlSize m_controlSize;
151 ScrollbarTheme& m_theme;
152
153 int m_visibleSize { 0 };
154 int m_totalSize { 0 };
155 float m_currentPos { 0 };
156 float m_dragOrigin { 0 };
157 int m_lineStep { 0 };
158 int m_pageStep { 0 };
159 float m_pixelStep { 1 };
160
161 ScrollbarPart m_hoveredPart { NoPart };
162 ScrollbarPart m_pressedPart { NoPart };
163 int m_pressedPos { 0 };
164 bool m_draggingDocument { false };
165 int m_documentDragPos { 0 };
166
167 bool m_enabled { true };
168
169 Timer m_scrollTimer;
170
171 bool m_suppressInvalidation { false };
172
173#if !PLATFORM(COCOA)
174 float m_opacity { 1 };
175#endif
176
177private:
178 bool isScrollbar() const override { return true; }
179};
180
181} // namespace WebCore
182
183SPECIALIZE_TYPE_TRAITS_WIDGET(Scrollbar, isScrollbar())
184
185