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 <Carbon/Carbon.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"
44 // FIXME: Get these numbers from CoreUI.
45 static int cRealButtonLength[] = { 28, 21 };
46 static int cButtonHitInset[] = { 3, 2 };
47 // cRealButtonLength - cButtonInset
48 static int cButtonLength[] = { 14, 10 };
49 static int cScrollbarThickness[] = { 15, 11 };
50 static int cButtonInset[] = { 14, 11 };
51 static int cThumbMinLength[] = { 26, 20 };
53 static int cOuterButtonLength[] = { 16, 14 }; // The outer button in a double button pair is a bit bigger.
54 static int cOuterButtonOverlap = 2;
56 static ScrollbarButtonsPlacement gButtonPlacement = ScrollbarButtonsDoubleEnd;
58 void ScrollbarThemeMacNonOverlayAPI::updateButtonPlacement()
60 NSString *buttonPlacement = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleScrollBarVariant"];
61 if ([buttonPlacement isEqualToString:@"Single"])
62 gButtonPlacement = ScrollbarButtonsSingle;
63 else if ([buttonPlacement isEqualToString:@"DoubleMin"])
64 gButtonPlacement = ScrollbarButtonsDoubleStart;
65 else if ([buttonPlacement isEqualToString:@"DoubleBoth"])
66 gButtonPlacement = ScrollbarButtonsDoubleBoth;
68 gButtonPlacement = ScrollbarButtonsDoubleEnd;
72 static blink::WebThemeEngine::State scrollbarStateToThemeState(ScrollbarThemeClient* scrollbar)
74 if (!scrollbar->enabled())
75 return blink::WebThemeEngine::StateDisabled;
76 if (!scrollbar->isScrollableAreaActive())
77 return blink::WebThemeEngine::StateInactive;
78 if (scrollbar->pressedPart() == ThumbPart)
79 return blink::WebThemeEngine::StatePressed;
81 return blink::WebThemeEngine::StateActive;
84 // Override ScrollbarThemeMacCommon::paint() to add support for the following:
85 // - drawing using WebThemeEngine functions
86 // - drawing tickmarks
87 // - Skia specific changes
88 bool ScrollbarThemeMacNonOverlayAPI::paint(ScrollbarThemeClient* scrollbar, GraphicsContext* context, const IntRect& damageRect)
90 // Get the tickmarks for the frameview.
91 Vector<IntRect> tickmarks;
92 scrollbar->getTickmarks(tickmarks);
94 HIThemeTrackDrawInfo trackInfo;
95 trackInfo.version = 0;
96 trackInfo.kind = scrollbar->controlSize() == RegularScrollbar ? kThemeMediumScrollBar : kThemeSmallScrollBar;
97 trackInfo.bounds = scrollbar->frameRect();
99 trackInfo.max = scrollbar->maximum();
100 trackInfo.value = scrollbar->currentPos();
101 trackInfo.trackInfo.scrollbar.viewsize = scrollbar->visibleSize();
102 trackInfo.attributes = 0;
103 if (scrollbar->orientation() == HorizontalScrollbar)
104 trackInfo.attributes |= kThemeTrackHorizontal;
106 if (!scrollbar->enabled())
107 trackInfo.enableState = kThemeTrackDisabled;
109 trackInfo.enableState = scrollbar->isScrollableAreaActive() ? kThemeTrackActive : kThemeTrackInactive;
111 if (!hasButtons(scrollbar))
112 trackInfo.enableState = kThemeTrackNothingToScroll;
113 trackInfo.trackInfo.scrollbar.pressState = scrollbarPartToHIPressedState(scrollbar->pressedPart());
115 SkCanvas* canvas = context->canvas();
116 CGAffineTransform currentCTM = gfx::SkMatrixToCGAffineTransform(canvas->getTotalMatrix());
118 // The Aqua scrollbar is buggy when rotated and scaled. We will just draw into a bitmap if we detect a scale or rotation.
119 bool canDrawDirectly = currentCTM.a == 1.0f && currentCTM.b == 0.0f && currentCTM.c == 0.0f && (currentCTM.d == 1.0f || currentCTM.d == -1.0f);
120 GraphicsContext* drawingContext = context;
121 OwnPtr<ImageBuffer> imageBuffer;
122 if (!canDrawDirectly) {
123 trackInfo.bounds = IntRect(IntPoint(), scrollbar->frameRect().size());
125 IntRect bufferRect(scrollbar->frameRect());
126 bufferRect.intersect(damageRect);
127 bufferRect.move(-scrollbar->frameRect().x(), -scrollbar->frameRect().y());
129 imageBuffer = ImageBuffer::create(bufferRect.size());
133 drawingContext = imageBuffer->context();
137 gfx::SkiaBitLocker bitLocker(drawingContext->canvas());
138 CGContextRef cgContext = bitLocker.cgContext();
139 HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
141 IntRect tickmarkTrackRect = trackRect(scrollbar, false);
142 if (!canDrawDirectly) {
143 tickmarkTrackRect.setX(0);
144 tickmarkTrackRect.setY(0);
146 // The ends are rounded and the thumb doesn't go there.
147 tickmarkTrackRect.inflateY(-tickmarkTrackRect.width());
149 tickmarkTrackRect.setX(tickmarkTrackRect.x() + 2);
150 tickmarkTrackRect.setWidth(tickmarkTrackRect.width() - 5);
151 paintGivenTickmarks(drawingContext, scrollbar, tickmarkTrackRect, tickmarks);
153 if (hasThumb(scrollbar)) {
154 blink::WebThemeEngine::ScrollbarInfo scrollbarInfo;
155 scrollbarInfo.orientation = scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::ScrollbarOrientationHorizontal : blink::WebThemeEngine::ScrollbarOrientationVertical;
156 scrollbarInfo.parent = scrollbar->isScrollViewScrollbar() ? blink::WebThemeEngine::ScrollbarParentScrollView : blink::WebThemeEngine::ScrollbarParentRenderLayer;
157 scrollbarInfo.maxValue = scrollbar->maximum();
158 scrollbarInfo.currentValue = scrollbar->currentPos();
159 scrollbarInfo.visibleSize = scrollbar->visibleSize();
160 scrollbarInfo.totalSize = scrollbar->totalSize();
162 blink::WebCanvas* webCanvas = drawingContext->canvas();
163 blink::Platform::current()->themeEngine()->paintScrollbarThumb(
165 scrollbarStateToThemeState(scrollbar),
166 scrollbar->controlSize() == RegularScrollbar ? blink::WebThemeEngine::SizeRegular : blink::WebThemeEngine::SizeSmall,
167 blink::WebRect(scrollbar->frameRect()),
171 if (!canDrawDirectly)
172 context->drawImageBuffer(imageBuffer.get(), scrollbar->frameRect().location());
177 int ScrollbarThemeMacNonOverlayAPI::scrollbarThickness(ScrollbarControlSize controlSize)
179 return cScrollbarThickness[controlSize];
182 ScrollbarButtonsPlacement ScrollbarThemeMacNonOverlayAPI::buttonsPlacement() const
184 return gButtonPlacement;
187 bool ScrollbarThemeMacNonOverlayAPI::hasButtons(ScrollbarThemeClient* scrollbar)
189 return scrollbar->enabled() && buttonsPlacement() != ScrollbarButtonsNone
190 && (scrollbar->orientation() == HorizontalScrollbar
192 : scrollbar->height()) >= 2 * (cRealButtonLength[scrollbar->controlSize()] - cButtonHitInset[scrollbar->controlSize()]);
195 bool ScrollbarThemeMacNonOverlayAPI::hasThumb(ScrollbarThemeClient* scrollbar)
197 int minLengthForThumb = 2 * cButtonInset[scrollbar->controlSize()] + cThumbMinLength[scrollbar->controlSize()] + 1;
198 return scrollbar->enabled() && (scrollbar->orientation() == HorizontalScrollbar ?
200 scrollbar->height()) >= minLengthForThumb;
203 static IntRect buttonRepaintRect(const IntRect& buttonRect, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, bool start)
205 ASSERT(gButtonPlacement != ScrollbarButtonsNone);
207 IntRect paintRect(buttonRect);
208 if (orientation == HorizontalScrollbar) {
209 paintRect.setWidth(cRealButtonLength[controlSize]);
211 paintRect.setX(buttonRect.x() - (cRealButtonLength[controlSize] - buttonRect.width()));
213 paintRect.setHeight(cRealButtonLength[controlSize]);
215 paintRect.setY(buttonRect.y() - (cRealButtonLength[controlSize] - buttonRect.height()));
221 IntRect ScrollbarThemeMacNonOverlayAPI::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
225 if (part == BackButtonStartPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleEnd))
228 if (part == BackButtonEndPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleStart || buttonsPlacement() == ScrollbarButtonsSingle))
231 int thickness = scrollbarThickness(scrollbar->controlSize());
232 bool outerButton = part == BackButtonStartPart && (buttonsPlacement() == ScrollbarButtonsDoubleStart || buttonsPlacement() == ScrollbarButtonsDoubleBoth);
234 if (scrollbar->orientation() == HorizontalScrollbar)
235 result = IntRect(scrollbar->x(), scrollbar->y(), cOuterButtonLength[scrollbar->controlSize()] + (painting ? cOuterButtonOverlap : 0), thickness);
237 result = IntRect(scrollbar->x(), scrollbar->y(), thickness, cOuterButtonLength[scrollbar->controlSize()] + (painting ? cOuterButtonOverlap : 0));
241 // Our repaint rect is slightly larger, since we are a button that is adjacent to the track.
242 if (scrollbar->orientation() == HorizontalScrollbar) {
243 int start = part == BackButtonStartPart ? scrollbar->x() : scrollbar->x() + scrollbar->width() - cOuterButtonLength[scrollbar->controlSize()] - cButtonLength[scrollbar->controlSize()];
244 result = IntRect(start, scrollbar->y(), cButtonLength[scrollbar->controlSize()], thickness);
246 int start = part == BackButtonStartPart ? scrollbar->y() : scrollbar->y() + scrollbar->height() - cOuterButtonLength[scrollbar->controlSize()] - cButtonLength[scrollbar->controlSize()];
247 result = IntRect(scrollbar->x(), start, thickness, cButtonLength[scrollbar->controlSize()]);
251 return buttonRepaintRect(result, scrollbar->orientation(), scrollbar->controlSize(), part == BackButtonStartPart);
255 IntRect ScrollbarThemeMacNonOverlayAPI::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
259 if (part == ForwardButtonEndPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleStart))
262 if (part == ForwardButtonStartPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleEnd || buttonsPlacement() == ScrollbarButtonsSingle))
265 int thickness = scrollbarThickness(scrollbar->controlSize());
266 int outerButtonLength = cOuterButtonLength[scrollbar->controlSize()];
267 int buttonLength = cButtonLength[scrollbar->controlSize()];
269 bool outerButton = part == ForwardButtonEndPart && (buttonsPlacement() == ScrollbarButtonsDoubleEnd || buttonsPlacement() == ScrollbarButtonsDoubleBoth);
271 if (scrollbar->orientation() == HorizontalScrollbar) {
272 result = IntRect(scrollbar->x() + scrollbar->width() - outerButtonLength, scrollbar->y(), outerButtonLength, thickness);
274 result.inflateX(cOuterButtonOverlap);
276 result = IntRect(scrollbar->x(), scrollbar->y() + scrollbar->height() - outerButtonLength, thickness, outerButtonLength);
278 result.inflateY(cOuterButtonOverlap);
283 if (scrollbar->orientation() == HorizontalScrollbar) {
284 int start = part == ForwardButtonEndPart ? scrollbar->x() + scrollbar->width() - buttonLength : scrollbar->x() + outerButtonLength;
285 result = IntRect(start, scrollbar->y(), buttonLength, thickness);
287 int start = part == ForwardButtonEndPart ? scrollbar->y() + scrollbar->height() - buttonLength : scrollbar->y() + outerButtonLength;
288 result = IntRect(scrollbar->x(), start, thickness, buttonLength);
291 return buttonRepaintRect(result, scrollbar->orientation(), scrollbar->controlSize(), part == ForwardButtonStartPart);
295 IntRect ScrollbarThemeMacNonOverlayAPI::trackRect(ScrollbarThemeClient* scrollbar, bool painting)
297 if (painting || !hasButtons(scrollbar))
298 return scrollbar->frameRect();
301 int thickness = scrollbarThickness(scrollbar->controlSize());
304 int outerButtonLength = cOuterButtonLength[scrollbar->controlSize()];
305 int buttonLength = cButtonLength[scrollbar->controlSize()];
306 int doubleButtonLength = outerButtonLength + buttonLength;
307 switch (buttonsPlacement()) {
308 case ScrollbarButtonsSingle:
309 startWidth = buttonLength;
310 endWidth = buttonLength;
312 case ScrollbarButtonsDoubleStart:
313 startWidth = doubleButtonLength;
315 case ScrollbarButtonsDoubleEnd:
316 endWidth = doubleButtonLength;
318 case ScrollbarButtonsDoubleBoth:
319 startWidth = doubleButtonLength;
320 endWidth = doubleButtonLength;
326 int totalWidth = startWidth + endWidth;
327 if (scrollbar->orientation() == HorizontalScrollbar)
328 return IntRect(scrollbar->x() + startWidth, scrollbar->y(), scrollbar->width() - totalWidth, thickness);
329 return IntRect(scrollbar->x(), scrollbar->y() + startWidth, thickness, scrollbar->height() - totalWidth);
332 int ScrollbarThemeMacNonOverlayAPI::minimumThumbLength(ScrollbarThemeClient* scrollbar)
334 return cThumbMinLength[scrollbar->controlSize()];