1 /* 2 * Copyright (C) 2011 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 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "platform/scroll/ScrollbarThemeOverlay.h" 28 29 #include "platform/PlatformMouseEvent.h" 30 #include "platform/graphics/GraphicsContext.h" 31 #include "platform/scroll/ScrollbarThemeClient.h" 32 #include "platform/transforms/TransformationMatrix.h" 33 #include "public/platform/Platform.h" 34 #include "public/platform/WebRect.h" 35 #include "public/platform/WebThemeEngine.h" 36 37 #include <algorithm> 38 39 using namespace std; 40 41 namespace WebCore { 42 43 ScrollbarThemeOverlay::ScrollbarThemeOverlay(int thumbThickness, int scrollbarMargin, HitTestBehavior allowHitTest, Color color) 44 : ScrollbarTheme() 45 , m_thumbThickness(thumbThickness) 46 , m_scrollbarMargin(scrollbarMargin) 47 , m_allowHitTest(allowHitTest) 48 , m_color(color) 49 , m_useSolidColor(true) 50 { 51 } 52 53 ScrollbarThemeOverlay::ScrollbarThemeOverlay(int thumbThickness, int scrollbarMargin, HitTestBehavior allowHitTest) 54 : ScrollbarTheme() 55 , m_thumbThickness(thumbThickness) 56 , m_scrollbarMargin(scrollbarMargin) 57 , m_allowHitTest(allowHitTest) 58 , m_useSolidColor(false) 59 { 60 } 61 62 int ScrollbarThemeOverlay::scrollbarThickness(ScrollbarControlSize controlSize) 63 { 64 return m_thumbThickness + m_scrollbarMargin; 65 } 66 67 bool ScrollbarThemeOverlay::usesOverlayScrollbars() const 68 { 69 return true; 70 } 71 72 int ScrollbarThemeOverlay::thumbPosition(ScrollbarThemeClient* scrollbar) 73 { 74 if (!scrollbar->totalSize()) 75 return 0; 76 77 int trackLen = trackLength(scrollbar); 78 float proportion = static_cast<float>(scrollbar->currentPos()) / scrollbar->totalSize(); 79 return round(proportion * trackLen); 80 } 81 82 int ScrollbarThemeOverlay::thumbLength(ScrollbarThemeClient* scrollbar) 83 { 84 int trackLen = trackLength(scrollbar); 85 86 if (!scrollbar->totalSize()) 87 return trackLen; 88 89 float proportion = static_cast<float>(scrollbar->visibleSize()) / scrollbar->totalSize(); 90 int length = round(proportion * trackLen); 91 length = min(max(length, minimumThumbLength(scrollbar)), trackLen); 92 return length; 93 } 94 95 bool ScrollbarThemeOverlay::hasThumb(ScrollbarThemeClient* scrollbar) 96 { 97 return true; 98 } 99 100 IntRect ScrollbarThemeOverlay::backButtonRect(ScrollbarThemeClient*, ScrollbarPart, bool) 101 { 102 return IntRect(); 103 } 104 105 IntRect ScrollbarThemeOverlay::forwardButtonRect(ScrollbarThemeClient*, ScrollbarPart, bool) 106 { 107 return IntRect(); 108 } 109 110 IntRect ScrollbarThemeOverlay::trackRect(ScrollbarThemeClient* scrollbar, bool) 111 { 112 IntRect rect = scrollbar->frameRect(); 113 if (scrollbar->orientation() == HorizontalScrollbar) 114 rect.inflateX(-m_scrollbarMargin); 115 else 116 rect.inflateY(-m_scrollbarMargin); 117 return rect; 118 } 119 120 int ScrollbarThemeOverlay::thumbThickness(ScrollbarThemeClient*) 121 { 122 return m_thumbThickness; 123 } 124 125 void ScrollbarThemeOverlay::paintThumb(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect) 126 { 127 if (context->paintingDisabled()) 128 return; 129 IntRect thumbRect = rect; 130 if (scrollbar->orientation() == HorizontalScrollbar) { 131 thumbRect.setHeight(thumbRect.height() - m_scrollbarMargin); 132 } else { 133 thumbRect.setWidth(thumbRect.width() - m_scrollbarMargin); 134 if (scrollbar->isLeftSideVerticalScrollbar()) 135 thumbRect.setX(thumbRect.x() + m_scrollbarMargin); 136 } 137 138 if (m_useSolidColor) { 139 context->fillRect(thumbRect, m_color); 140 return; 141 } 142 143 blink::WebThemeEngine::State state = blink::WebThemeEngine::StateNormal; 144 if (scrollbar->pressedPart() == ThumbPart) 145 state = blink::WebThemeEngine::StatePressed; 146 else if (scrollbar->hoveredPart() == ThumbPart) 147 state = blink::WebThemeEngine::StateHover; 148 149 blink::WebCanvas* canvas = context->canvas(); 150 151 blink::WebThemeEngine::Part part = blink::WebThemeEngine::PartScrollbarHorizontalThumb; 152 if (scrollbar->orientation() == VerticalScrollbar) 153 part = blink::WebThemeEngine::PartScrollbarVerticalThumb; 154 155 blink::Platform::current()->themeEngine()->paint(canvas, part, state, blink::WebRect(rect), 0); 156 } 157 158 ScrollbarPart ScrollbarThemeOverlay::hitTest(ScrollbarThemeClient* scrollbar, const IntPoint& position) 159 { 160 if (m_allowHitTest == DisallowHitTest) 161 return NoPart; 162 163 return ScrollbarTheme::hitTest(scrollbar, position); 164 } 165 166 } // namespace WebCore 167