Home | History | Annotate | Download | only in scroll
      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 blink {
     45 
     46 static bool useMockTheme()
     47 {
     48     return LayoutTestSupport::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     blink::WebThemeEngine::State state = scrollbar->hoveredPart() == partType ? blink::WebThemeEngine::StateHover : blink::WebThemeEngine::StateNormal;
     75 
     76     if (useMockTheme() && !scrollbar->enabled())
     77         state = blink::WebThemeEngine::StateDisabled;
     78 
     79     IntRect alignRect = trackRect(scrollbar, false);
     80     blink::WebThemeEngine::ExtraParams extraParams;
     81     blink::WebCanvas* canvas = gc->canvas();
     82     extraParams.scrollbarTrack.isBack = (partType == BackTrackPart);
     83     extraParams.scrollbarTrack.trackX = alignRect.x();
     84     extraParams.scrollbarTrack.trackY = alignRect.y();
     85     extraParams.scrollbarTrack.trackWidth = alignRect.width();
     86     extraParams.scrollbarTrack.trackHeight = alignRect.height();
     87     blink::Platform::current()->themeEngine()->paint(canvas, scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::PartScrollbarHorizontalTrack : blink::WebThemeEngine::PartScrollbarVerticalTrack, state, blink::WebRect(rect), &extraParams);
     88 }
     89 
     90 void ScrollbarThemeAura::paintButton(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part)
     91 {
     92     blink::WebThemeEngine::Part paintPart;
     93     blink::WebThemeEngine::State state = blink::WebThemeEngine::StateNormal;
     94     blink::WebCanvas* canvas = gc->canvas();
     95     bool checkMin = false;
     96     bool checkMax = false;
     97 
     98     if (scrollbar->orientation() == HorizontalScrollbar) {
     99         if (part == BackButtonStartPart) {
    100             paintPart = blink::WebThemeEngine::PartScrollbarLeftArrow;
    101             checkMin = true;
    102         } else if (useMockTheme() && part != ForwardButtonEndPart) {
    103             return;
    104         } else {
    105             paintPart = blink::WebThemeEngine::PartScrollbarRightArrow;
    106             checkMax = true;
    107         }
    108     } else {
    109         if (part == BackButtonStartPart) {
    110             paintPart = blink::WebThemeEngine::PartScrollbarUpArrow;
    111             checkMin = true;
    112         } else if (useMockTheme() && part != ForwardButtonEndPart) {
    113             return;
    114         } else {
    115             paintPart = blink::WebThemeEngine::PartScrollbarDownArrow;
    116             checkMax = true;
    117         }
    118     }
    119     if (useMockTheme() && !scrollbar->enabled()) {
    120         state = blink::WebThemeEngine::StateDisabled;
    121     } else if (!useMockTheme() && ((checkMin && (scrollbar->currentPos() <= 0))
    122         || (checkMax && scrollbar->currentPos() >= scrollbar->maximum()))) {
    123         state = blink::WebThemeEngine::StateDisabled;
    124     } else {
    125         if (part == scrollbar->pressedPart())
    126             state = blink::WebThemeEngine::StatePressed;
    127         else if (part == scrollbar->hoveredPart())
    128             state = blink::WebThemeEngine::StateHover;
    129     }
    130     blink::Platform::current()->themeEngine()->paint(canvas, paintPart, state, blink::WebRect(rect), 0);
    131 }
    132 
    133 void ScrollbarThemeAura::paintThumb(GraphicsContext* gc, ScrollbarThemeClient* scrollbar, const IntRect& rect)
    134 {
    135     blink::WebThemeEngine::State state;
    136     blink::WebCanvas* canvas = gc->canvas();
    137     if (scrollbar->pressedPart() == ThumbPart)
    138         state = blink::WebThemeEngine::StatePressed;
    139     else if (scrollbar->hoveredPart() == ThumbPart)
    140         state = blink::WebThemeEngine::StateHover;
    141     else
    142         state = blink::WebThemeEngine::StateNormal;
    143     blink::Platform::current()->themeEngine()->paint(canvas, scrollbar->orientation() == HorizontalScrollbar ? blink::WebThemeEngine::PartScrollbarHorizontalThumb : blink::WebThemeEngine::PartScrollbarVerticalThumb, state, blink::WebRect(rect), 0);
    144 }
    145 
    146 IntSize ScrollbarThemeAura::buttonSize(ScrollbarThemeClient* scrollbar)
    147 {
    148     if (scrollbar->orientation() == VerticalScrollbar) {
    149         IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarUpArrow);
    150         return IntSize(size.width(), scrollbar->height() < 2 * size.height() ? scrollbar->height() / 2 : size.height());
    151     }
    152 
    153     // HorizontalScrollbar
    154     IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarLeftArrow);
    155     return IntSize(scrollbar->width() < 2 * size.width() ? scrollbar->width() / 2 : size.width(), size.height());
    156 }
    157 
    158 int ScrollbarThemeAura::minimumThumbLength(ScrollbarThemeClient* scrollbar)
    159 {
    160     if (scrollbar->orientation() == VerticalScrollbar) {
    161         IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarVerticalThumb);
    162         return size.height();
    163     }
    164 
    165     IntSize size = blink::Platform::current()->themeEngine()->getSize(blink::WebThemeEngine::PartScrollbarHorizontalThumb);
    166     return size.width();
    167 }
    168 
    169 } // namespace blink
    170