2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / platform / Scrollbar.h
blob9238a77666341860622297517ed2a318e11caefb
1 /*
2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
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.
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.
26 #ifndef Scrollbar_h
27 #define Scrollbar_h
29 #include <wtf/RefCounted.h>
30 #include "ScrollTypes.h"
31 #include "Timer.h"
32 #include "Widget.h"
33 #include <wtf/MathExtras.h>
34 #include <wtf/PassRefPtr.h>
36 namespace WebCore {
38 class GraphicsContext;
39 class IntRect;
40 class ScrollbarClient;
41 class ScrollbarTheme;
42 class PlatformMouseEvent;
44 // These match the numbers we use over in WebKit (WebFrameView.m).
45 const int cScrollbarPixelsPerLineStep = 40;
46 const float cMouseWheelPixelsPerLineStep = 40.0f / 3.0f;
47 const int cAmountToKeepWhenPaging = 40;
49 class Scrollbar : public Widget, public RefCounted<Scrollbar> {
50 protected:
51 Scrollbar(ScrollbarClient*, ScrollbarOrientation, ScrollbarControlSize, ScrollbarTheme* = 0);
53 public:
54 virtual ~Scrollbar();
56 // 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.
57 static PassRefPtr<Scrollbar> createNativeScrollbar(ScrollbarClient* client, ScrollbarOrientation orientation, ScrollbarControlSize size);
59 void setClient(ScrollbarClient* client) { m_client = client; }
60 ScrollbarClient* client() const { return m_client; }
62 ScrollbarOrientation orientation() const { return m_orientation; }
64 int value() const { return lroundf(m_currentPos); }
65 float currentPos() const { return m_currentPos; }
66 int pressedPos() const { return m_pressedPos; }
67 int visibleSize() const { return m_visibleSize; }
68 int totalSize() const { return m_totalSize; }
69 int maximum() const { return m_totalSize - m_visibleSize; }
70 ScrollbarControlSize controlSize() const { return m_controlSize; }
72 int lineStep() const { return m_lineStep; }
73 int pageStep() const { return m_pageStep; }
74 float pixelStep() const { return m_pixelStep; }
76 ScrollbarPart pressedPart() const { return m_pressedPart; }
77 ScrollbarPart hoveredPart() const { return m_hoveredPart; }
78 virtual void setHoveredPart(ScrollbarPart);
79 virtual void setPressedPart(ScrollbarPart);
81 void setSteps(int lineStep, int pageStep, int pixelsPerStep = 1);
82 bool setValue(int);
83 void setProportion(int visibleSize, int totalSize);
84 void setPressedPos(int p) { m_pressedPos = p; }
86 bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1.0f);
88 virtual void paint(GraphicsContext*, const IntRect& damageRect);
90 bool enabled() const { return m_enabled; }
91 virtual void setEnabled(bool e);
93 bool isWindowActive() const;
95 // These methods are used for platform scrollbars to give :hover feedback. They will not get called
96 // when the mouse went down in a scrollbar, since it is assumed the scrollbar will start
97 // grabbing all events in that case anyway.
98 bool mouseMoved(const PlatformMouseEvent&);
99 bool mouseExited();
101 // Used by some platform scrollbars to know when they've been released from capture.
102 bool mouseUp();
104 bool mouseDown(const PlatformMouseEvent&);
106 #if PLATFORM(QT)
107 // For platforms that wish to handle context menu events.
108 // FIXME: This is misplaced. Normal hit testing should be used to populate a correct
109 // context menu. There's no reason why the scrollbar should have to do it.
110 bool contextMenu(const PlatformMouseEvent& event);
111 #endif
113 // Takes an event and accounts for any transforms that might occur on the scrollbar. Returns
114 // a new event that has had all of the transforms applied.
115 PlatformMouseEvent transformEvent(const PlatformMouseEvent&);
117 ScrollbarTheme* theme() const { return m_theme; }
119 virtual void setParent(ScrollView*);
120 virtual void setFrameRect(const IntRect&);
122 virtual void invalidateRect(const IntRect&);
124 bool suppressInvalidation() const { return m_suppressInvalidation; }
125 void setSuppressInvalidation(bool s) { m_suppressInvalidation = s; }
127 virtual void styleChanged() { }
129 protected:
130 virtual void updateThumbPosition();
131 virtual void updateThumbProportion();
133 void autoscrollTimerFired(Timer<Scrollbar>*);
134 void startTimerIfNeeded(double delay);
135 void stopTimerIfNeeded();
136 void autoscrollPressedPart(double delay);
137 ScrollDirection pressedPartScrollDirection();
138 ScrollGranularity pressedPartScrollGranularity();
140 void moveThumb(int pos);
142 ScrollbarClient* m_client;
143 ScrollbarOrientation m_orientation;
144 ScrollbarControlSize m_controlSize;
145 ScrollbarTheme* m_theme;
147 int m_visibleSize;
148 int m_totalSize;
149 float m_currentPos;
150 int m_lineStep;
151 int m_pageStep;
152 float m_pixelStep;
154 ScrollbarPart m_hoveredPart;
155 ScrollbarPart m_pressedPart;
156 int m_pressedPos;
158 bool m_enabled;
160 Timer<Scrollbar> m_scrollTimer;
161 bool m_overlapsResizer;
163 bool m_suppressInvalidation;
168 #endif