Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / scroll / ScrollbarThemeMacNonOverlayAPI.mm
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "platform/scroll/ScrollbarThemeMacNonOverlayAPI.h"
33
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"
41
42 namespace WebCore {
43
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 };
52
53 static int cOuterButtonLength[] = { 16, 14 }; // The outer button in a double button pair is a bit bigger.
54 static int cOuterButtonOverlap = 2;
55
56 static ScrollbarButtonsPlacement gButtonPlacement = ScrollbarButtonsDoubleEnd;
57
58 void ScrollbarThemeMacNonOverlayAPI::updateButtonPlacement()
59 {
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;
67     else {
68         gButtonPlacement = ScrollbarButtonsDoubleEnd;
69     }
70 }
71
72 static blink::WebThemeEngine::State scrollbarStateToThemeState(ScrollbarThemeClient* scrollbar)
73 {
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;
80
81     return blink::WebThemeEngine::StateActive;
82 }
83
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)
89 {
90     // Get the tickmarks for the frameview.
91     Vector<IntRect> tickmarks;
92     scrollbar->getTickmarks(tickmarks);
93
94     HIThemeTrackDrawInfo trackInfo;
95     trackInfo.version = 0;
96     trackInfo.kind = scrollbar->controlSize() == RegularScrollbar ? kThemeMediumScrollBar : kThemeSmallScrollBar;
97     trackInfo.bounds = scrollbar->frameRect();
98     trackInfo.min = 0;
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;
105
106     if (!scrollbar->enabled())
107         trackInfo.enableState = kThemeTrackDisabled;
108     else
109         trackInfo.enableState = scrollbar->isScrollableAreaActive() ? kThemeTrackActive : kThemeTrackInactive;
110
111     if (!hasButtons(scrollbar))
112         trackInfo.enableState = kThemeTrackNothingToScroll;
113     trackInfo.trackInfo.scrollbar.pressState = scrollbarPartToHIPressedState(scrollbar->pressedPart());
114
115     SkCanvas* canvas = context->canvas();
116     CGAffineTransform currentCTM = gfx::SkMatrixToCGAffineTransform(canvas->getTotalMatrix());
117
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());
124
125         IntRect bufferRect(scrollbar->frameRect());
126         bufferRect.intersect(damageRect);
127         bufferRect.move(-scrollbar->frameRect().x(), -scrollbar->frameRect().y());
128
129         imageBuffer = ImageBuffer::create(bufferRect.size());
130         if (!imageBuffer)
131             return true;
132
133         drawingContext = imageBuffer->context();
134     }
135
136     // Draw thumbless.
137     gfx::SkiaBitLocker bitLocker(drawingContext->canvas());
138     CGContextRef cgContext = bitLocker.cgContext();
139     HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
140
141     IntRect tickmarkTrackRect = trackRect(scrollbar, false);
142     if (!canDrawDirectly) {
143         tickmarkTrackRect.setX(0);
144         tickmarkTrackRect.setY(0);
145     }
146     // The ends are rounded and the thumb doesn't go there.
147     tickmarkTrackRect.inflateY(-tickmarkTrackRect.width());
148     // Inset a bit.
149     tickmarkTrackRect.setX(tickmarkTrackRect.x() + 2);
150     tickmarkTrackRect.setWidth(tickmarkTrackRect.width() - 5);
151     paintGivenTickmarks(drawingContext, scrollbar, tickmarkTrackRect, tickmarks);
152
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();
161
162         blink::WebCanvas* webCanvas = drawingContext->canvas();
163         blink::Platform::current()->themeEngine()->paintScrollbarThumb(
164             webCanvas,
165             scrollbarStateToThemeState(scrollbar),
166             scrollbar->controlSize() == RegularScrollbar ? blink::WebThemeEngine::SizeRegular : blink::WebThemeEngine::SizeSmall,
167             blink::WebRect(scrollbar->frameRect()),
168             scrollbarInfo);
169     }
170
171     if (!canDrawDirectly)
172         context->drawImageBuffer(imageBuffer.get(), scrollbar->frameRect().location());
173
174     return true;
175 }
176
177 int ScrollbarThemeMacNonOverlayAPI::scrollbarThickness(ScrollbarControlSize controlSize)
178 {
179     return cScrollbarThickness[controlSize];
180 }
181
182 ScrollbarButtonsPlacement ScrollbarThemeMacNonOverlayAPI::buttonsPlacement() const
183 {
184     return gButtonPlacement;
185 }
186
187 bool ScrollbarThemeMacNonOverlayAPI::hasButtons(ScrollbarThemeClient* scrollbar)
188 {
189     return scrollbar->enabled() && buttonsPlacement() != ScrollbarButtonsNone
190              && (scrollbar->orientation() == HorizontalScrollbar
191              ? scrollbar->width()
192              : scrollbar->height()) >= 2 * (cRealButtonLength[scrollbar->controlSize()] - cButtonHitInset[scrollbar->controlSize()]);
193 }
194
195 bool ScrollbarThemeMacNonOverlayAPI::hasThumb(ScrollbarThemeClient* scrollbar)
196 {
197     int minLengthForThumb = 2 * cButtonInset[scrollbar->controlSize()] + cThumbMinLength[scrollbar->controlSize()] + 1;
198     return scrollbar->enabled() && (scrollbar->orientation() == HorizontalScrollbar ?
199              scrollbar->width() :
200              scrollbar->height()) >= minLengthForThumb;
201 }
202
203 static IntRect buttonRepaintRect(const IntRect& buttonRect, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, bool start)
204 {
205     ASSERT(gButtonPlacement != ScrollbarButtonsNone);
206
207     IntRect paintRect(buttonRect);
208     if (orientation == HorizontalScrollbar) {
209         paintRect.setWidth(cRealButtonLength[controlSize]);
210         if (!start)
211             paintRect.setX(buttonRect.x() - (cRealButtonLength[controlSize] - buttonRect.width()));
212     } else {
213         paintRect.setHeight(cRealButtonLength[controlSize]);
214         if (!start)
215             paintRect.setY(buttonRect.y() - (cRealButtonLength[controlSize] - buttonRect.height()));
216     }
217
218     return paintRect;
219 }
220
221 IntRect ScrollbarThemeMacNonOverlayAPI::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
222 {
223     IntRect result;
224
225     if (part == BackButtonStartPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleEnd))
226         return result;
227
228     if (part == BackButtonEndPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleStart || buttonsPlacement() == ScrollbarButtonsSingle))
229         return result;
230
231     int thickness = scrollbarThickness(scrollbar->controlSize());
232     bool outerButton = part == BackButtonStartPart && (buttonsPlacement() == ScrollbarButtonsDoubleStart || buttonsPlacement() == ScrollbarButtonsDoubleBoth);
233     if (outerButton) {
234         if (scrollbar->orientation() == HorizontalScrollbar)
235             result = IntRect(scrollbar->x(), scrollbar->y(), cOuterButtonLength[scrollbar->controlSize()] + (painting ? cOuterButtonOverlap : 0), thickness);
236         else
237             result = IntRect(scrollbar->x(), scrollbar->y(), thickness, cOuterButtonLength[scrollbar->controlSize()] + (painting ? cOuterButtonOverlap : 0));
238         return result;
239     }
240
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);
245     } else {
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()]);
248     }
249
250     if (painting)
251         return buttonRepaintRect(result, scrollbar->orientation(), scrollbar->controlSize(), part == BackButtonStartPart);
252     return result;
253 }
254
255 IntRect ScrollbarThemeMacNonOverlayAPI::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
256 {
257     IntRect result;
258
259     if (part == ForwardButtonEndPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleStart))
260         return result;
261
262     if (part == ForwardButtonStartPart && (buttonsPlacement() == ScrollbarButtonsNone || buttonsPlacement() == ScrollbarButtonsDoubleEnd || buttonsPlacement() == ScrollbarButtonsSingle))
263         return result;
264
265     int thickness = scrollbarThickness(scrollbar->controlSize());
266     int outerButtonLength = cOuterButtonLength[scrollbar->controlSize()];
267     int buttonLength = cButtonLength[scrollbar->controlSize()];
268
269     bool outerButton = part == ForwardButtonEndPart && (buttonsPlacement() == ScrollbarButtonsDoubleEnd || buttonsPlacement() == ScrollbarButtonsDoubleBoth);
270     if (outerButton) {
271         if (scrollbar->orientation() == HorizontalScrollbar) {
272             result = IntRect(scrollbar->x() + scrollbar->width() - outerButtonLength, scrollbar->y(), outerButtonLength, thickness);
273             if (painting)
274                 result.inflateX(cOuterButtonOverlap);
275         } else {
276             result = IntRect(scrollbar->x(), scrollbar->y() + scrollbar->height() - outerButtonLength, thickness, outerButtonLength);
277             if (painting)
278                 result.inflateY(cOuterButtonOverlap);
279         }
280         return result;
281     }
282
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);
286     } else {
287         int start = part == ForwardButtonEndPart ? scrollbar->y() + scrollbar->height() - buttonLength : scrollbar->y() + outerButtonLength;
288         result = IntRect(scrollbar->x(), start, thickness, buttonLength);
289     }
290     if (painting)
291         return buttonRepaintRect(result, scrollbar->orientation(), scrollbar->controlSize(), part == ForwardButtonStartPart);
292     return result;
293 }
294
295 IntRect ScrollbarThemeMacNonOverlayAPI::trackRect(ScrollbarThemeClient* scrollbar, bool painting)
296 {
297     if (painting || !hasButtons(scrollbar))
298         return scrollbar->frameRect();
299
300     IntRect result;
301     int thickness = scrollbarThickness(scrollbar->controlSize());
302     int startWidth = 0;
303     int endWidth = 0;
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;
311             break;
312         case ScrollbarButtonsDoubleStart:
313             startWidth = doubleButtonLength;
314             break;
315         case ScrollbarButtonsDoubleEnd:
316             endWidth = doubleButtonLength;
317             break;
318         case ScrollbarButtonsDoubleBoth:
319             startWidth = doubleButtonLength;
320             endWidth = doubleButtonLength;
321             break;
322         default:
323             break;
324     }
325
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);
330 }
331
332 int ScrollbarThemeMacNonOverlayAPI::minimumThumbLength(ScrollbarThemeClient* scrollbar)
333 {
334     return cThumbMinLength[scrollbar->controlSize()];
335 }
336
337 }