1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2007 Staikos Computing Services Inc. <info@staikos.net>
4  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "config.h"
29 #include "ScrollbarThemeQt.h"
30 
31 #include "GraphicsContext.h"
32 #include "PlatformMouseEvent.h"
33 #include "RenderThemeQt.h"
34 #include "ScrollView.h"
35 #include "Scrollbar.h"
36 
37 #include <QApplication>
38 #ifdef Q_WS_MAC
39 #include <QMacStyle>
40 #endif
41 #include <QMenu>
42 #include <QPainter>
43 #include <QStyle>
44 #include <QStyleOptionSlider>
45 
46 namespace WebCore {
47 
nativeTheme()48 ScrollbarTheme* ScrollbarTheme::nativeTheme()
49 {
50     static ScrollbarThemeQt theme;
51     return &theme;
52 }
53 
~ScrollbarThemeQt()54 ScrollbarThemeQt::~ScrollbarThemeQt()
55 {
56 }
57 
scPart(const ScrollbarPart & part)58 static QStyle::SubControl scPart(const ScrollbarPart& part)
59 {
60     switch (part) {
61     case NoPart:
62         return QStyle::SC_None;
63     case BackButtonStartPart:
64     case BackButtonEndPart:
65         return QStyle::SC_ScrollBarSubLine;
66     case BackTrackPart:
67         return QStyle::SC_ScrollBarSubPage;
68     case ThumbPart:
69         return QStyle::SC_ScrollBarSlider;
70     case ForwardTrackPart:
71         return QStyle::SC_ScrollBarAddPage;
72     case ForwardButtonStartPart:
73     case ForwardButtonEndPart:
74         return QStyle::SC_ScrollBarAddLine;
75     }
76 
77     return QStyle::SC_None;
78 }
79 
scrollbarPart(const QStyle::SubControl & sc)80 static ScrollbarPart scrollbarPart(const QStyle::SubControl& sc)
81 {
82     switch (sc) {
83     case QStyle::SC_None:
84         return NoPart;
85     case QStyle::SC_ScrollBarSubLine:
86         return BackButtonStartPart;
87     case QStyle::SC_ScrollBarSubPage:
88         return BackTrackPart;
89     case QStyle::SC_ScrollBarSlider:
90         return ThumbPart;
91     case QStyle::SC_ScrollBarAddPage:
92         return ForwardTrackPart;
93     case QStyle::SC_ScrollBarAddLine:
94         return ForwardButtonStartPart;
95     }
96     return NoPart;
97 }
98 
styleOptionSlider(Scrollbar * scrollbar,QWidget * widget=0)99 static QStyleOptionSlider* styleOptionSlider(Scrollbar* scrollbar, QWidget* widget = 0)
100 {
101     static QStyleOptionSlider opt;
102     if (widget)
103         opt.initFrom(widget);
104     else
105         opt.state |= QStyle::State_Active;
106 
107     opt.state &= ~QStyle::State_HasFocus;
108 
109     opt.rect = scrollbar->frameRect();
110     if (scrollbar->enabled())
111         opt.state |= QStyle::State_Enabled;
112     if (scrollbar->controlSize() != RegularScrollbar)
113         opt.state |= QStyle::State_Mini;
114     opt.orientation = (scrollbar->orientation() == VerticalScrollbar) ? Qt::Vertical : Qt::Horizontal;
115 
116     if (scrollbar->orientation() == HorizontalScrollbar)
117         opt.state |= QStyle::State_Horizontal;
118     else
119         opt.state &= ~QStyle::State_Horizontal;
120 
121     opt.sliderValue = scrollbar->value();
122     opt.sliderPosition = opt.sliderValue;
123     opt.pageStep = scrollbar->pageStep();
124     opt.singleStep = scrollbar->lineStep();
125     opt.minimum = 0;
126     opt.maximum = qMax(0, scrollbar->maximum());
127     ScrollbarPart pressedPart = scrollbar->pressedPart();
128     ScrollbarPart hoveredPart = scrollbar->hoveredPart();
129     if (pressedPart != NoPart) {
130         opt.activeSubControls = scPart(scrollbar->pressedPart());
131         if (pressedPart == BackButtonStartPart || pressedPart == ForwardButtonStartPart
132             || pressedPart == BackButtonEndPart || pressedPart == ForwardButtonEndPart
133             || pressedPart == ThumbPart)
134             opt.state |= QStyle::State_Sunken;
135     } else
136         opt.activeSubControls = scPart(hoveredPart);
137     if (hoveredPart != NoPart)
138         opt.state |= QStyle::State_MouseOver;
139     return &opt;
140 }
141 
paint(Scrollbar * scrollbar,GraphicsContext * graphicsContext,const IntRect & damageRect)142 bool ScrollbarThemeQt::paint(Scrollbar* scrollbar, GraphicsContext* graphicsContext, const IntRect& damageRect)
143 {
144     if (graphicsContext->updatingControlTints()) {
145        scrollbar->invalidateRect(damageRect);
146        return false;
147     }
148 
149     StylePainter p(this, graphicsContext);
150     if (!p.isValid())
151       return true;
152 
153     p.painter->save();
154     QStyleOptionSlider* opt = styleOptionSlider(scrollbar, p.widget);
155 
156     p.painter->setClipRect(opt->rect.intersected(damageRect), Qt::IntersectClip);
157 
158 #ifdef Q_WS_MAC
159     // FIXME: We also need to check the widget style but today ScrollbarTheme is not aware of the page so we
160     // can't get the widget.
161     if (qobject_cast<QMacStyle*>(style()))
162         p.drawComplexControl(QStyle::CC_ScrollBar, *opt);
163     else
164 #endif
165     {
166         // The QStyle expects the background to be already filled.
167         p.painter->fillRect(opt->rect, opt->palette.background());
168 
169         const QPoint topLeft = opt->rect.topLeft();
170         p.painter->translate(topLeft);
171         opt->rect.moveTo(QPoint(0, 0));
172         p.drawComplexControl(QStyle::CC_ScrollBar, *opt);
173         opt->rect.moveTo(topLeft);
174     }
175     p.painter->restore();
176 
177     return true;
178 }
179 
hitTest(Scrollbar * scrollbar,const PlatformMouseEvent & evt)180 ScrollbarPart ScrollbarThemeQt::hitTest(Scrollbar* scrollbar, const PlatformMouseEvent& evt)
181 {
182     QStyleOptionSlider* opt = styleOptionSlider(scrollbar);
183     const QPoint pos = scrollbar->convertFromContainingWindow(evt.pos());
184     opt->rect.moveTo(QPoint(0, 0));
185     QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_ScrollBar, opt, pos, 0);
186     return scrollbarPart(sc);
187 }
188 
shouldCenterOnThumb(Scrollbar *,const PlatformMouseEvent & evt)189 bool ScrollbarThemeQt::shouldCenterOnThumb(Scrollbar*, const PlatformMouseEvent& evt)
190 {
191     // Middle click centers slider thumb (if supported).
192     return style()->styleHint(QStyle::SH_ScrollBar_MiddleClickAbsolutePosition) && evt.button() == MiddleButton;
193 }
194 
invalidatePart(Scrollbar * scrollbar,ScrollbarPart)195 void ScrollbarThemeQt::invalidatePart(Scrollbar* scrollbar, ScrollbarPart)
196 {
197     // FIXME: Do more precise invalidation.
198     scrollbar->invalidate();
199 }
200 
scrollbarThickness(ScrollbarControlSize controlSize)201 int ScrollbarThemeQt::scrollbarThickness(ScrollbarControlSize controlSize)
202 {
203 #if USE(QT_MOBILE_THEME)
204     return 0;
205 #endif
206     QStyleOptionSlider o;
207     o.orientation = Qt::Vertical;
208     o.state &= ~QStyle::State_Horizontal;
209     if (controlSize != RegularScrollbar)
210         o.state |= QStyle::State_Mini;
211     return style()->pixelMetric(QStyle::PM_ScrollBarExtent, &o, 0);
212 }
213 
thumbPosition(Scrollbar * scrollbar)214 int ScrollbarThemeQt::thumbPosition(Scrollbar* scrollbar)
215 {
216     if (scrollbar->enabled())
217         return (int)((float)scrollbar->currentPos() * (trackLength(scrollbar) - thumbLength(scrollbar)) / scrollbar->maximum());
218     return 0;
219 }
220 
thumbLength(Scrollbar * scrollbar)221 int ScrollbarThemeQt::thumbLength(Scrollbar* scrollbar)
222 {
223     QStyleOptionSlider* opt = styleOptionSlider(scrollbar);
224     IntRect thumb = style()->subControlRect(QStyle::CC_ScrollBar, opt, QStyle::SC_ScrollBarSlider, 0);
225     return scrollbar->orientation() == HorizontalScrollbar ? thumb.width() : thumb.height();
226 }
227 
trackPosition(Scrollbar * scrollbar)228 int ScrollbarThemeQt::trackPosition(Scrollbar* scrollbar)
229 {
230     QStyleOptionSlider* opt = styleOptionSlider(scrollbar);
231     IntRect track = style()->subControlRect(QStyle::CC_ScrollBar, opt, QStyle::SC_ScrollBarGroove, 0);
232     return scrollbar->orientation() == HorizontalScrollbar ? track.x() - scrollbar->x() : track.y() - scrollbar->y();
233 }
234 
trackLength(Scrollbar * scrollbar)235 int ScrollbarThemeQt::trackLength(Scrollbar* scrollbar)
236 {
237     QStyleOptionSlider* opt = styleOptionSlider(scrollbar);
238     IntRect track = style()->subControlRect(QStyle::CC_ScrollBar, opt, QStyle::SC_ScrollBarGroove, 0);
239     return scrollbar->orientation() == HorizontalScrollbar ? track.width() : track.height();
240 }
241 
paintScrollCorner(ScrollView * scrollView,GraphicsContext * context,const IntRect & rect)242 void ScrollbarThemeQt::paintScrollCorner(ScrollView* scrollView, GraphicsContext* context, const IntRect& rect)
243 {
244     if (context->updatingControlTints()) {
245        scrollView->invalidateRect(rect);
246        return;
247     }
248 
249     StylePainter p(this, context);
250     if (!p.isValid())
251         return;
252 
253     QStyleOption option;
254     option.rect = rect;
255     p.drawPrimitive(QStyle::PE_PanelScrollAreaCorner, option);
256 }
257 
style() const258 QStyle* ScrollbarThemeQt::style() const
259 {
260     return QApplication::style();
261 }
262 
263 }
264 
265