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/ScrollbarThemeAura.h" 33 34 #include "platform/LayoutTestSupport.h" 35 #include "platform/PlatformMouseEvent.h" 36 #include "platform/RuntimeEnabledFeatures.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 DEFINE_STATIC_LOCAL(ScrollbarThemeOverlay, theme, (10, 0, ScrollbarThemeOverlay::AllowHitTest)); 55 return &theme; 56 } 57 58 DEFINE_STATIC_LOCAL(ScrollbarThemeAura, theme, ()); 59 return &theme; 60 } 61 62 int ScrollbarThemeAura::scrollbarThickness(ScrollbarControlSize controlSize) 63 { 64 // Horiz and Vert scrollbars are the same thickness. 65 // In unit tests we don't have the mock theme engine (because of layering violations), so we hard code the size (see bug 327470). 66 if (useMockTheme()) 67 return 15; 68 IntSize scrollbarSize = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarVerticalTrack); 69 return scrollbarSize.width(); 70 } 71 72 void ScrollbarThemeAura::paintTrackPiece(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart partType) 73 { 74 if (gc->paintingDisabled()) 75 return; 76 blink::WebThemeEngine::State state = scrollbar->hoveredPart() == partType ? blink::WebThemeEngine::StateHover : blink::WebThemeEngine::StateNormal; 77 78 if (useMockTheme() && !scrollbar->enabled()) 79 state = blink::WebThemeEngine::StateDisabled; 80 81 IntRect alignRect = trackRect(scrollbar, false); 82 blink::WebThemeEngine::ExtraParams extraParams; 83 blink::WebCanvas* canvas = gc->canvas(); 84 extraParams.scrollbarTrack.isBack = (partType == BackTrackPart); 85 extraParams.scrollbarTrack.trackX = alignRect.x(); 86 extraParams.scrollbarTrack.trackY = alignRect.y(); 87 extraParams.scrollbarTrack.trackWidth = alignRect.width(); 88 extraParams.scrollbarTrack.trackHeight = alignRect.height(); 89 blink::Platform::current()->themeEngine()->paint(canvas, scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::PartScrollbarHorizontalTrack : blink::WebThemeEngine::PartScrollbarVerticalTrack, state, blink::WebRect(rect), &extraParams); 90 } 91 92 void ScrollbarThemeAura::paintButton(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part) 93 { 94 if (gc->paintingDisabled()) 95 return; 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 ScrollbarThemeAura::paintThumb(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect) 138 { 139 if (gc->paintingDisabled()) 140 return; 141 blink::WebThemeEngine::State state; 142 blink::WebCanvas* canvas = gc->canvas(); 143 if (scrollbar->pressedPart() == ThumbPart) 144 state = blink::WebThemeEngine::StatePressed; 145 else if (scrollbar->hoveredPart() == ThumbPart) 146 state = blink::WebThemeEngine::StateHover; 147 else 148 state = blink::WebThemeEngine::StateNormal; 149 blink::Platform::current()->themeEngine()->paint(canvas, scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::PartScrollbarHorizontalThumb : blink::WebThemeEngine::PartScrollbarVerticalThumb, state, blink::WebRect(rect), 0); 150 } 151 152 IntSize ScrollbarThemeAura::buttonSize(ScrollbarThemeClient* scrollbar) 153 { 154 if (scrollbar->orientation() == VerticalScrollbar) { 155 IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarUpArrow); 156 return IntSize(size.width(), scrollbar->height() < 2 * size.height() ? scrollbar->height() / 2 : size.height()); 157 } 158 159 // HorizontalScrollbar 160 IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarLeftArrow); 161 return IntSize(scrollbar->width() < 2 * size.width() ? scrollbar->width() / 2 : size.width(), size.height()); 162 } 163 164 int ScrollbarThemeAura::minimumThumbLength(ScrollbarThemeClient* scrollbar) 165 { 166 if (scrollbar->orientation() == VerticalScrollbar) { 167 IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarVerticalThumb); 168 return size.height(); 169 } 170 171 IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarHorizontalThumb); 172 return size.width(); 173 } 174 175 } // namespace WebCore 176