1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
* Copyright (C) 2008, 2009 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ScrollbarThemeChromiumWin.h"
#include <windows.h>
#include <vsstyle.h>
#include "ChromiumBridge.h"
#include "GraphicsContext.h"
#include "PlatformContextSkia.h"
#include "PlatformMouseEvent.h"
#include "Scrollbar.h"
#include "WindowsVersion.h"
namespace WebCore {
ScrollbarTheme* ScrollbarTheme::nativeTheme()
{
static ScrollbarThemeChromiumWin theme;
return &theme;
}
// The scrollbar size in DumpRenderTree on the Mac - so we can match their
// layout results. Entries are for regular, small, and mini scrollbars.
// Metrics obtained using [NSScroller scrollerWidthForControlSize:]
static const int kMacScrollbarSize[3] = { 15, 11, 15 };
// Constants used to figure the drag rect outside which we should snap the
// scrollbar thumb back to its origin. These calculations are based on
// observing the behavior of the MSVC8 main window scrollbar + some
// guessing/extrapolation.
static const int kOffEndMultiplier = 3;
static const int kOffSideMultiplier = 8;
IntRect ScrollbarThemeChromium::trackRect(Scrollbar* scrollbar, bool)
{
IntSize bs = buttonSize(scrollbar);
// The buttons at the top and bottom of the scrollbar are square, so the
// thickness of the scrollbar is also their height.
int thickness = scrollbarThickness(scrollbar->controlSize());
if (scrollbar->orientation() == HorizontalScrollbar) {
// Once the scrollbar becomes smaller than the natural size of the
// two buttons, the track disappears.
if (scrollbar->width() < 2 * thickness)
return IntRect();
return IntRect(scrollbar->x() + bs.width(), scrollbar->y(), scrollbar->width() - 2 * bs.width(), thickness);
}
if (scrollbar->height() < 2 * thickness)
return IntRect();
return IntRect(scrollbar->x(), scrollbar->y() + bs.height(), thickness, scrollbar->height() - 2 * bs.height());
}
int ScrollbarThemeChromiumWin::scrollbarThickness(ScrollbarControlSize controlSize)
{
static int thickness;
if (!thickness) {
if (ChromiumBridge::layoutTestMode())
return kMacScrollbarSize[controlSize];
thickness = GetSystemMetrics(SM_CXVSCROLL);
}
return thickness;
}
bool ScrollbarThemeChromiumWin::invalidateOnMouseEnterExit()
{
return isVistaOrNewer();
}
bool ScrollbarThemeChromiumWin::shouldSnapBackToDragOrigin(Scrollbar* scrollbar, const PlatformMouseEvent& evt)
{
// Find the rect within which we shouldn't snap, by expanding the track rect
// in both dimensions.
IntRect rect = trackRect(scrollbar);
const bool horz = scrollbar->orientation() == HorizontalScrollbar;
const int thickness = scrollbarThickness(scrollbar->controlSize());
rect.inflateX((horz ? kOffEndMultiplier : kOffSideMultiplier) * thickness);
rect.inflateY((horz ? kOffSideMultiplier : kOffEndMultiplier) * thickness);
// Convert the event to local coordinates.
IntPoint mousePosition = scrollbar->convertFromContainingWindow(evt.pos());
mousePosition.move(scrollbar->x(), scrollbar->y());
// We should snap iff the event is outside our calculated rect.
return !rect.contains(mousePosition);
}
void ScrollbarThemeChromiumWin::paintTrackPiece(GraphicsContext* gc, Scrollbar* scrollbar, const IntRect& rect, ScrollbarPart partType)
{
bool horz = scrollbar->orientation() == HorizontalScrollbar;
int partId;
if (partType == BackTrackPart)
partId = horz ? SBP_UPPERTRACKHORZ : SBP_UPPERTRACKVERT;
else
partId = horz ? SBP_LOWERTRACKHORZ : SBP_LOWERTRACKVERT;
IntRect alignRect = trackRect(scrollbar, false);
// Draw the track area before/after the thumb on the scroll bar.
ChromiumBridge::paintScrollbarTrack(
gc,
partId,
getThemeState(scrollbar, partType),
getClassicThemeState(scrollbar, partType),
rect,
alignRect);
}
void ScrollbarThemeChromiumWin::paintButton(GraphicsContext* gc, Scrollbar* scrollbar, const IntRect& rect, ScrollbarPart part)
{
bool horz = scrollbar->orientation() == HorizontalScrollbar;
int partId;
if (part == BackButtonStartPart || part == ForwardButtonStartPart)
partId = horz ? DFCS_SCROLLLEFT : DFCS_SCROLLUP;
else
partId = horz ? DFCS_SCROLLRIGHT : DFCS_SCROLLDOWN;
// Draw the thumb (the box you drag in the scroll bar to scroll).
ChromiumBridge::paintScrollbarArrow(
gc,
getThemeArrowState(scrollbar, part),
partId | getClassicThemeState(scrollbar, part),
rect);
}
void ScrollbarThemeChromiumWin::paintThumb(GraphicsContext* gc, Scrollbar* scrollbar, const IntRect& rect)
{
bool horz = scrollbar->orientation() == HorizontalScrollbar;
// Draw the thumb (the box you drag in the scroll bar to scroll).
ChromiumBridge::paintScrollbarThumb(
gc,
horz ? SBP_THUMBBTNHORZ : SBP_THUMBBTNVERT,
getThemeState(scrollbar, ThumbPart),
getClassicThemeState(scrollbar, ThumbPart),
rect);
// Draw the gripper (the three little lines on the thumb).
ChromiumBridge::paintScrollbarThumb(
gc,
horz ? SBP_GRIPPERHORZ : SBP_GRIPPERVERT,
getThemeState(scrollbar, ThumbPart),
getClassicThemeState(scrollbar, ThumbPart),
rect);
}
int ScrollbarThemeChromiumWin::getThemeState(Scrollbar* scrollbar, ScrollbarPart part) const
{
// When dragging the thumb, draw thumb pressed and other segments normal
// regardless of where the cursor actually is. See also four places in
// getThemeArrowState().
if (scrollbar->pressedPart() == ThumbPart) {
if (part == ThumbPart)
return SCRBS_PRESSED;
return isVistaOrNewer() ? SCRBS_HOVER : SCRBS_NORMAL;
}
if (!scrollbar->enabled())
return SCRBS_DISABLED;
if (scrollbar->hoveredPart() != part || part == BackTrackPart || part == ForwardTrackPart)
return (scrollbar->hoveredPart() == NoPart || !isVistaOrNewer()) ? SCRBS_NORMAL : SCRBS_HOVER;
if (scrollbar->pressedPart() == NoPart)
return SCRBS_HOT;
return (scrollbar->pressedPart() == part) ? SCRBS_PRESSED : SCRBS_NORMAL;
}
int ScrollbarThemeChromiumWin::getThemeArrowState(Scrollbar* scrollbar, ScrollbarPart part) const
{
// We could take advantage of knowing the values in the state enum to write
// some simpler code, but treating the state enum as a black box seems
// clearer and more future-proof.
if (part == BackButtonStartPart || part == ForwardButtonStartPart) {
if (scrollbar->orientation() == HorizontalScrollbar) {
if (scrollbar->pressedPart() == ThumbPart)
return !isVistaOrNewer() ? ABS_LEFTNORMAL : ABS_LEFTHOVER;
if (!scrollbar->enabled())
return ABS_LEFTDISABLED;
if (scrollbar->hoveredPart() != part)
return ((scrollbar->hoveredPart() == NoPart) || !isVistaOrNewer()) ? ABS_LEFTNORMAL : ABS_LEFTHOVER;
if (scrollbar->pressedPart() == NoPart)
return ABS_LEFTHOT;
return (scrollbar->pressedPart() == part) ?
ABS_LEFTPRESSED : ABS_LEFTNORMAL;
}
if (scrollbar->pressedPart() == ThumbPart)
return !isVistaOrNewer() ? ABS_UPNORMAL : ABS_UPHOVER;
if (!scrollbar->enabled())
return ABS_UPDISABLED;
if (scrollbar->hoveredPart() != part)
return ((scrollbar->hoveredPart() == NoPart) || !isVistaOrNewer()) ? ABS_UPNORMAL : ABS_UPHOVER;
if (scrollbar->pressedPart() == NoPart)
return ABS_UPHOT;
return (scrollbar->pressedPart() == part) ? ABS_UPPRESSED : ABS_UPNORMAL;
}
if (scrollbar->orientation() == HorizontalScrollbar) {
if (scrollbar->pressedPart() == ThumbPart)
return !isVistaOrNewer() ? ABS_RIGHTNORMAL : ABS_RIGHTHOVER;
if (!scrollbar->enabled())
return ABS_RIGHTDISABLED;
if (scrollbar->hoveredPart() != part)
return ((scrollbar->hoveredPart() == NoPart) || !isVistaOrNewer()) ? ABS_RIGHTNORMAL : ABS_RIGHTHOVER;
if (scrollbar->pressedPart() == NoPart)
return ABS_RIGHTHOT;
return (scrollbar->pressedPart() == part) ? ABS_RIGHTPRESSED : ABS_RIGHTNORMAL;
}
if (scrollbar->pressedPart() == ThumbPart)
return !isVistaOrNewer() ? ABS_DOWNNORMAL : ABS_DOWNHOVER;
if (!scrollbar->enabled())
return ABS_DOWNDISABLED;
if (scrollbar->hoveredPart() != part)
return ((scrollbar->hoveredPart() == NoPart) || !isVistaOrNewer()) ? ABS_DOWNNORMAL : ABS_DOWNHOVER;
if (scrollbar->pressedPart() == NoPart)
return ABS_DOWNHOT;
return (scrollbar->pressedPart() == part) ? ABS_DOWNPRESSED : ABS_DOWNNORMAL;
}
int ScrollbarThemeChromiumWin::getClassicThemeState(Scrollbar* scrollbar, ScrollbarPart part) const
{
// When dragging the thumb, draw the buttons normal even when hovered.
if (scrollbar->pressedPart() == ThumbPart)
return 0;
if (!scrollbar->enabled())
return DFCS_INACTIVE;
if (scrollbar->hoveredPart() != part || part == BackTrackPart || part == ForwardTrackPart)
return 0;
if (scrollbar->pressedPart() == NoPart)
return DFCS_HOT;
return (scrollbar->pressedPart() == part) ? (DFCS_PUSHED | DFCS_FLAT) : 0;
}
bool ScrollbarThemeChromiumWin::shouldCenterOnThumb(Scrollbar*, const PlatformMouseEvent& evt)
{
return evt.shiftKey() && evt.button() == LeftButton;
}
IntSize ScrollbarThemeChromiumWin::buttonSize(Scrollbar* scrollbar)
{
// Our desired rect is essentially thickness by thickness.
// Our actual rect will shrink to half the available space when we have < 2
// times thickness pixels left. This allows the scrollbar to scale down
// and function even at tiny sizes.
int thickness = scrollbarThickness(scrollbar->controlSize());
// In layout test mode, we force the button "girth" (i.e., the length of
// the button along the axis of the scrollbar) to be a fixed size.
// FIXME: This is retarded! scrollbarThickness is already fixed in layout
// test mode so that should be enough to result in repeatable results, but
// preserving this hack avoids having to rebaseline pixel tests.
const int kLayoutTestModeGirth = 17;
int girth = ChromiumBridge::layoutTestMode() ? kLayoutTestModeGirth : thickness;
if (scrollbar->orientation() == HorizontalScrollbar) {
int width = scrollbar->width() < 2 * girth ? scrollbar->width() / 2 : girth;
return IntSize(width, thickness);
}
int height = scrollbar->height() < 2 * girth ? scrollbar->height() / 2 : girth;
return IntSize(thickness, height);
}
} // namespace WebCore
|