Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / scroll / ScrollbarThemeGtkOrAura.cpp
1 /*
2  * Copyright (c) 2008, 2009, 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/ScrollbarThemeGtkOrAura.h"
33
34 #include "RuntimeEnabledFeatures.h"
35 #include "platform/LayoutTestSupport.h"
36 #include "platform/PlatformMouseEvent.h"
37 #include "platform/graphics/GraphicsContext.h"
38 #include "platform/scroll/ScrollbarThemeClient.h"
39 #include "platform/scroll/ScrollbarThemeOverlay.h"
40 #include "public/platform/Platform.h"
41 #include "public/platform/WebRect.h"
42 #include "public/platform/WebThemeEngine.h"
43
44 namespace WebCore {
45
46 static bool useMockTheme()
47 {
48     return isRunningLayoutTest();
49 }
50
51 ScrollbarTheme* ScrollbarTheme::nativeTheme()
52 {
53     if (RuntimeEnabledFeatures::overlayScrollbarsEnabled()) {
54 #if defined(OS_TIZEN)
55         DEFINE_STATIC_LOCAL(ScrollbarThemeOverlay, theme, (3, 4, ScrollbarThemeOverlay::DisallowHitTest));
56 #else
57         DEFINE_STATIC_LOCAL(ScrollbarThemeOverlay, theme, (10, 0, ScrollbarThemeOverlay::AllowHitTest, Color(128, 128, 128, 192)));
58 #endif
59         return &theme;
60     }
61
62     DEFINE_STATIC_LOCAL(ScrollbarThemeGtkOrAura, theme, ());
63     return &theme;
64 }
65
66 int ScrollbarThemeGtkOrAura::scrollbarThickness(ScrollbarControlSize controlSize)
67 {
68     // Horiz and Vert scrollbars are the same thickness.
69     // In unit tests we don't have the mock theme engine (because of layering violations), so we hard code the size (see bug 327470).
70     if (useMockTheme())
71         return 15;
72     IntSize scrollbarSize = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarVerticalTrack);
73     return scrollbarSize.width();
74 }
75
76 void ScrollbarThemeGtkOrAura::paintTrackPiece(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart partType)
77 {
78     blink::WebThemeEngine::State state = scrollbar->hoveredPart() == partType ? blink::WebThemeEngine::StateHover : blink::WebThemeEngine::StateNormal;
79
80     if (useMockTheme() && !scrollbar->enabled())
81         state = blink::WebThemeEngine::StateDisabled;
82
83     IntRect alignRect = trackRect(scrollbar, false);
84     blink::WebThemeEngine::ExtraParams extraParams;
85     blink::WebCanvas* canvas = gc->canvas();
86     extraParams.scrollbarTrack.isBack = (partType == BackTrackPart);
87     extraParams.scrollbarTrack.trackX = alignRect.x();
88     extraParams.scrollbarTrack.trackY = alignRect.y();
89     extraParams.scrollbarTrack.trackWidth = alignRect.width();
90     extraParams.scrollbarTrack.trackHeight = alignRect.height();
91     blink::Platform::current()->themeEngine()->paint(canvas, scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::PartScrollbarHorizontalTrack : blink::WebThemeEngine::PartScrollbarVerticalTrack, state, blink::WebRect(rect), &extraParams);
92 }
93
94 void ScrollbarThemeGtkOrAura::paintButton(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part)
95 {
96     blink::WebThemeEngine::Part paintPart;
97     blink::WebThemeEngine::State state = blink::WebThemeEngine::StateNormal;
98     blink::WebCanvas* canvas = gc->canvas();
99     bool checkMin = false;
100     bool checkMax = false;
101
102     if (scrollbar->orientation() == HorizontalScrollbar) {
103         if (part == BackButtonStartPart) {
104             paintPart = blink::WebThemeEngine::PartScrollbarLeftArrow;
105             checkMin = true;
106         } else if (useMockTheme() && part != ForwardButtonEndPart) {
107             return;
108         } else {
109             paintPart = blink::WebThemeEngine::PartScrollbarRightArrow;
110             checkMax = true;
111         }
112     } else {
113         if (part == BackButtonStartPart) {
114             paintPart = blink::WebThemeEngine::PartScrollbarUpArrow;
115             checkMin = true;
116         } else if (useMockTheme() && part != ForwardButtonEndPart) {
117             return;
118         } else {
119             paintPart = blink::WebThemeEngine::PartScrollbarDownArrow;
120             checkMax = true;
121         }
122     }
123     if (useMockTheme() && !scrollbar->enabled()) {
124         state = blink::WebThemeEngine::StateDisabled;
125     } else if (!useMockTheme() && ((checkMin && (scrollbar->currentPos() <= 0))
126         || (checkMax && scrollbar->currentPos() >= scrollbar->maximum()))) {
127         state = blink::WebThemeEngine::StateDisabled;
128     } else {
129         if (part == scrollbar->pressedPart())
130             state = blink::WebThemeEngine::StatePressed;
131         else if (part == scrollbar->hoveredPart())
132             state = blink::WebThemeEngine::StateHover;
133     }
134     blink::Platform::current()->themeEngine()->paint(canvas, paintPart, state, blink::WebRect(rect), 0);
135 }
136
137 void ScrollbarThemeGtkOrAura::paintThumb(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect)
138 {
139     blink::WebThemeEngine::State state;
140     blink::WebCanvas* canvas = gc->canvas();
141     if (scrollbar->pressedPart() == ThumbPart)
142         state = blink::WebThemeEngine::StatePressed;
143     else if (scrollbar->hoveredPart() == ThumbPart)
144         state = blink::WebThemeEngine::StateHover;
145     else
146         state = blink::WebThemeEngine::StateNormal;
147     blink::Platform::current()->themeEngine()->paint(canvas, scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::PartScrollbarHorizontalThumb : blink::WebThemeEngine::PartScrollbarVerticalThumb, state, blink::WebRect(rect), 0);
148 }
149
150 bool ScrollbarThemeGtkOrAura::shouldCenterOnThumb(ScrollbarThemeClient*, const PlatformMouseEvent& evt)
151 {
152     return (evt.shiftKey() && evt.button() == LeftButton) || (evt.button() == MiddleButton);
153 }
154
155 IntSize ScrollbarThemeGtkOrAura::buttonSize(ScrollbarThemeClient* scrollbar)
156 {
157     if (scrollbar->orientation() == VerticalScrollbar) {
158         IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarUpArrow);
159         return IntSize(size.width(), scrollbar->height() < 2 * size.height() ? scrollbar->height() / 2 : size.height());
160     }
161
162     // HorizontalScrollbar
163     IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarLeftArrow);
164     return IntSize(scrollbar->width() < 2 * size.width() ? scrollbar->width() / 2 : size.width(), size.height());
165 }
166
167 int ScrollbarThemeGtkOrAura::minimumThumbLength(ScrollbarThemeClient* scrollbar)
168 {
169     if (scrollbar->orientation() == VerticalScrollbar) {
170         IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarVerticalThumb);
171         return size.height();
172     }
173
174     IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarHorizontalThumb);
175     return size.width();
176 }
177
178 } // namespace WebCore