2 * Copyright (C) 2013 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/scroll/ScrollbarThemeMacNonOverlayAPI.h"
34 #include "platform/graphics/GraphicsContext.h"
35 #include "platform/graphics/ImageBuffer.h"
36 #include "platform/scroll/ScrollbarThemeClient.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebRect.h"
39 #include "public/platform/WebThemeEngine.h"
40 #include "skia/ext/skia_utils_mac.h"
41 #include <Carbon/Carbon.h>
45 // FIXME: Get these numbers from CoreUI.
46 static int cRealButtonLength[] = { 28, 21 };
47 static int cButtonHitInset[] = { 3, 2 };
48 // cRealButtonLength - cButtonInset
49 static int cButtonLength[] = { 14, 10 };
50 static int cScrollbarThickness[] = { 15, 11 };
51 static int cButtonInset[] = { 14, 11 };
52 static int cThumbMinLength[] = { 26, 20 };
54 static int cOuterButtonLength[] = { 16, 14 }; // The outer button in a double button pair is a bit bigger.
55 static int cOuterButtonOverlap = 2;
57 static ScrollbarButtonsPlacement gButtonPlacement = ScrollbarButtonsDoubleEnd;
59 void ScrollbarThemeMacNonOverlayAPI::updateButtonPlacement()
61 NSString *buttonPlacement = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleScrollBarVariant"];
62 if ([buttonPlacement isEqualToString:@"Single"])
63 gButtonPlacement = ScrollbarButtonsSingle;
64 else if ([buttonPlacement isEqualToString:@"DoubleMin"])
65 gButtonPlacement = ScrollbarButtonsDoubleStart;
66 else if ([buttonPlacement isEqualToString:@"DoubleBoth"])
67 gButtonPlacement = ScrollbarButtonsDoubleBoth;
69 gButtonPlacement = ScrollbarButtonsDoubleEnd;
73 static blink::WebThemeEngine::State scrollbarStateToThemeState(ScrollbarThemeClient* scrollbar)
75 if (!scrollbar->enabled())
76 return blink::WebThemeEngine::StateDisabled;
77 if (!scrollbar->isScrollableAreaActive())
78 return blink::WebThemeEngine::StateInactive;
79 if (scrollbar->pressedPart() == ThumbPart)
80 return blink::WebThemeEngine::StatePressed;
82 return blink::WebThemeEngine::StateActive;
85 // Override ScrollbarThemeMacCommon::paint() to add support for the following:
86 // - drawing using WebThemeEngine functions
87 // - drawing tickmarks
88 // - Skia specific changes
89 bool ScrollbarThemeMacNonOverlayAPI::paint(ScrollbarThemeClient* scrollbar, GraphicsContext* context, const IntRect& damageRect)
91 // Get the tickmarks for the frameview.
92 Vector<IntRect> tickmarks;
93 scrollbar->getTickmarks(tickmarks);
95 HIThemeTrackDrawInfo trackInfo;
96 trackInfo.version = 0;
97 trackInfo.kind = scrollbar->controlSize() == RegularScrollbar ? kThemeMediumScrollBar : kThemeSmallScrollBar;
98 trackInfo.bounds = scrollbar->frameRect();
100 trackInfo.max = scrollbar->maximum();
101 trackInfo.value = scrollbar->currentPos();
102 trackInfo.trackInfo.scrollbar.viewsize = scrollbar->visibleSize();
103 trackInfo.attributes = 0;
104 if (scrollbar->orientation() == HorizontalScrollbar)
105 trackInfo.attributes |= kThemeTrackHorizontal;
107 if (!scrollbar->enabled())
108 trackInfo.enableState = kThemeTrackDisabled;
110 trackInfo.enableState = scrollbar->isScrollableAreaActive() ? kThemeTrackActive : kThemeTrackInactive;
112 if (!hasButtons(scrollbar))
113 trackInfo.enableState = kThemeTrackNothingToScroll;
114 trackInfo.trackInfo.scrollbar.pressState = scrollbarPartToHIPressedState(scrollbar->pressedPart());
116 SkCanvas* canvas = context->canvas();
117 CGAffineTransform currentCTM = gfx::SkMatrixToCGAffineTransform(canvas->getTotalMatrix());
119 // The Aqua scrollbar is buggy when rotated and scaled. We will just draw into a bitmap if we detect a scale or rotation.
120 bool canDrawDirectly = currentCTM.a == 1.0f && currentCTM.b == 0.0f && currentCTM.c == 0.0f && (currentCTM.d == 1.0f || currentCTM.d == -1.0f);
121 GraphicsContext* drawingContext = context;
122 OwnPtr<ImageBuffer> imageBuffer;
123 if (!canDrawDirectly) {
124 trackInfo.bounds = IntRect(IntPoint(), scrollbar->frameRect().size());
126 IntRect bufferRect(scrollbar->frameRect());
127 bufferRect.intersect(damageRect);
128 bufferRect.move(-scrollbar->frameRect().x(), -scrollbar->frameRect().y());
130 imageBuffer = ImageBuffer::create(bufferRect.size());
134 drawingContext = imageBuffer->context();
138 gfx::SkiaBitLocker bitLocker(drawingContext->canvas());
139 CGContextRef cgContext = bitLocker.cgContext();
140 HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
142 IntRect tickmarkTrackRect = trackRect(scrollbar, false);
143 if (!canDrawDirectly) {
144 tickmarkTrackRect.setX(0);
145 tickmarkTrackRect.setY(0);
147 // The ends are rounded and the thumb doesn't go there.
148 tickmarkTrackRect.inflateY(-tickmarkTrackRect.width());
150 tickmarkTrackRect.setX(tickmarkTrackRect.x() + 2);
151 tickmarkTrackRect.setWidth(tickmarkTrackRect.width() - 5);
152 paintGivenTickmarks(drawingContext, scrollbar, tickmarkTrackRect, tickmarks);
154 if (hasThumb(scrollbar)) {
155 blink::WebThemeEngine::ScrollbarInfo scrollbarInfo;
156 scrollbarInfo.orientation = scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::ScrollbarOrientationHorizontal : blink::WebThemeEngine::ScrollbarOrientationVertical;
157 scrollbarInfo.parent = scrollbar->isScrollViewScrollbar() ? blink::WebThemeEngine::ScrollbarParentScrollView : blink::WebThemeEngine::ScrollbarParentRenderLayer;
158 scrollbarInfo.maxValue = scrollbar->maximum();
159 scrollbarInfo.currentValue = scrollbar->currentPos();
160 scrollbarInfo.visibleSize = scrollbar->visibleSize();
161 scrollbarInfo.totalSize = scrollbar->totalSize();
163 blink::WebCanvas* webCanvas = drawingContext->canvas();
164 blink::Platform::current()->themeEngine()->paintScrollbarThumb(
166 scrollbarStateToThemeState(scrollbar),
167 scrollbar->controlSize() == RegularScrollbar ? blink::WebThemeEngine::SizeRegular : blink::WebThemeEngine::SizeSmall,
168 blink::WebRect(scrollbar->frameRect()),
172 if (!canDrawDirectly) {
174 context->drawImageBuffer(imageBuffer.get(),
175 FloatRect(scrollbar->frameRect().location(), imageBuffer->size()));
181 int ScrollbarThemeMacNonOverlayAPI::scrollbarThickness(ScrollbarControlSize controlSize)
183 return cScrollbarThickness[controlSize];
186 ScrollbarButtonsPlacement ScrollbarThemeMacNonOverlayAPI::buttonsPlacement() const
188 return gButtonPlacement;
191 bool ScrollbarThemeMacNonOverlayAPI::hasButtons(ScrollbarThemeClient* scrollbar)
193 return scrollbar->enabled() && buttonsPlacement() != ScrollbarButtonsNone
194 && (scrollbar->orientation() == HorizontalScrollbar
196 : scrollbar->height()) >= 2 * (cRealButtonLength[scrollbar->controlSize()] - cButtonHitInset[scrollbar->controlSize()]);
199 bool ScrollbarThemeMacNonOverlayAPI::hasThumb(ScrollbarThemeClient* scrollbar)
201 int minLengthForThumb = 2 * cButtonInset[scrollbar->controlSize()] + cThumbMinLength[scrollbar->controlSize()] + 1;
202 return scrollbar->enabled() && (scrollbar->orientation() == HorizontalScrollbar ?
204 scrollbar->height()) >= minLengthForThumb;
207 static IntRect buttonRepaintRect(const IntRect& buttonRect, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, bool start)
209 ASSERT(gButtonPlacement != ScrollbarButtonsNone);
211 IntRect paintRect(buttonRect);
212 if (orientation == HorizontalScrollbar) {
213 paintRect.setWidth(cRealButtonLength[controlSize]);
215 paintRect.setX(buttonRect.x() - (cRealButtonLength[controlSize] - buttonRect.width()));
217 paintRect.setHeight(cRealButtonLength[controlSize]);
219 paintRect.setY(buttonRect.y() - (cRealButtonLength[controlSize] - buttonRect.height()));
225 IntRect ScrollbarThemeMacNonOverlayAPI::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
229 if (part == BackButtonStartPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleEnd))
232 if (part == BackButtonEndPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleStart || buttonsPlacement() == ScrollbarButtonsSingle))
235 int thickness = scrollbarThickness(scrollbar->controlSize());
236 bool outerButton = part == BackButtonStartPart && (buttonsPlacement() == ScrollbarButtonsDoubleStart || buttonsPlacement() == ScrollbarButtonsDoubleBoth);
238 if (scrollbar->orientation() == HorizontalScrollbar)
239 result = IntRect(scrollbar->x(), scrollbar->y(), cOuterButtonLength[scrollbar->controlSize()] + (painting ? cOuterButtonOverlap : 0), thickness);
241 result = IntRect(scrollbar->x(), scrollbar->y(), thickness, cOuterButtonLength[scrollbar->controlSize()] + (painting ? cOuterButtonOverlap : 0));
245 // Our repaint rect is slightly larger, since we are a button that is adjacent to the track.
246 if (scrollbar->orientation() == HorizontalScrollbar) {
247 int start = part == BackButtonStartPart ? scrollbar->x() : scrollbar->x() + scrollbar->width() - cOuterButtonLength[scrollbar->controlSize()] - cButtonLength[scrollbar->controlSize()];
248 result = IntRect(start, scrollbar->y(), cButtonLength[scrollbar->controlSize()], thickness);
250 int start = part == BackButtonStartPart ? scrollbar->y() : scrollbar->y() + scrollbar->height() - cOuterButtonLength[scrollbar->controlSize()] - cButtonLength[scrollbar->controlSize()];
251 result = IntRect(scrollbar->x(), start, thickness, cButtonLength[scrollbar->controlSize()]);
255 return buttonRepaintRect(result, scrollbar->orientation(), scrollbar->controlSize(), part == BackButtonStartPart);
259 IntRect ScrollbarThemeMacNonOverlayAPI::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
263 if (part == ForwardButtonEndPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleStart))
266 if (part == ForwardButtonStartPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleEnd || buttonsPlacement() == ScrollbarButtonsSingle))
269 int thickness = scrollbarThickness(scrollbar->controlSize());
270 int outerButtonLength = cOuterButtonLength[scrollbar->controlSize()];
271 int buttonLength = cButtonLength[scrollbar->controlSize()];
273 bool outerButton = part == ForwardButtonEndPart && (buttonsPlacement() == ScrollbarButtonsDoubleEnd || buttonsPlacement() == ScrollbarButtonsDoubleBoth);
275 if (scrollbar->orientation() == HorizontalScrollbar) {
276 result = IntRect(scrollbar->x() + scrollbar->width() - outerButtonLength, scrollbar->y(), outerButtonLength, thickness);
278 result.inflateX(cOuterButtonOverlap);
280 result = IntRect(scrollbar->x(), scrollbar->y() + scrollbar->height() - outerButtonLength, thickness, outerButtonLength);
282 result.inflateY(cOuterButtonOverlap);
287 if (scrollbar->orientation() == HorizontalScrollbar) {
288 int start = part == ForwardButtonEndPart ? scrollbar->x() + scrollbar->width() - buttonLength : scrollbar->x() + outerButtonLength;
289 result = IntRect(start, scrollbar->y(), buttonLength, thickness);
291 int start = part == ForwardButtonEndPart ? scrollbar->y() + scrollbar->height() - buttonLength : scrollbar->y() + outerButtonLength;
292 result = IntRect(scrollbar->x(), start, thickness, buttonLength);
295 return buttonRepaintRect(result, scrollbar->orientation(), scrollbar->controlSize(), part == ForwardButtonStartPart);
299 IntRect ScrollbarThemeMacNonOverlayAPI::trackRect(ScrollbarThemeClient* scrollbar, bool painting)
301 if (painting || !hasButtons(scrollbar))
302 return scrollbar->frameRect();
305 int thickness = scrollbarThickness(scrollbar->controlSize());
308 int outerButtonLength = cOuterButtonLength[scrollbar->controlSize()];
309 int buttonLength = cButtonLength[scrollbar->controlSize()];
310 int doubleButtonLength = outerButtonLength + buttonLength;
311 switch (buttonsPlacement()) {
312 case ScrollbarButtonsSingle:
313 startWidth = buttonLength;
314 endWidth = buttonLength;
316 case ScrollbarButtonsDoubleStart:
317 startWidth = doubleButtonLength;
319 case ScrollbarButtonsDoubleEnd:
320 endWidth = doubleButtonLength;
322 case ScrollbarButtonsDoubleBoth:
323 startWidth = doubleButtonLength;
324 endWidth = doubleButtonLength;
330 int totalWidth = startWidth + endWidth;
331 if (scrollbar->orientation() == HorizontalScrollbar)
332 return IntRect(scrollbar->x() + startWidth, scrollbar->y(), scrollbar->width() - totalWidth, thickness);
333 return IntRect(scrollbar->x(), scrollbar->y() + startWidth, thickness, scrollbar->height() - totalWidth);
336 int ScrollbarThemeMacNonOverlayAPI::minimumThumbLength(ScrollbarThemeClient* scrollbar)
338 return cThumbMinLength[scrollbar->controlSize()];