1 /* 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 3 * Copyright (C) 2008, 2009 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "core/platform/chromium/ScrollbarThemeChromium.h" 29 30 #include "core/platform/PlatformMouseEvent.h" 31 #include "core/platform/ScrollableArea.h" 32 #include "core/platform/Scrollbar.h" 33 #include "core/platform/ScrollbarTheme.h" 34 #include "core/platform/graphics/GraphicsContextStateSaver.h" 35 36 // ----------------------------------------------------------------------------- 37 // This file contains scrollbar theme code that is cross platform. Additional 38 // members of ScrollbarThemeChromium can be found in the platform specific files 39 // ----------------------------------------------------------------------------- 40 41 namespace WebCore { 42 43 bool ScrollbarThemeChromium::hasThumb(ScrollbarThemeClient* scrollbar) 44 { 45 // This method is just called as a paint-time optimization to see if 46 // painting the thumb can be skipped. We don't have to be exact here. 47 return thumbLength(scrollbar) > 0; 48 } 49 50 IntRect ScrollbarThemeChromium::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool) 51 { 52 // Windows and Linux just have single arrows. 53 if (part == BackButtonEndPart) 54 return IntRect(); 55 56 IntSize size = buttonSize(scrollbar); 57 return IntRect(scrollbar->x(), scrollbar->y(), size.width(), size.height()); 58 } 59 60 IntRect ScrollbarThemeChromium::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool) 61 { 62 // Windows and Linux just have single arrows. 63 if (part == ForwardButtonStartPart) 64 return IntRect(); 65 66 IntSize size = buttonSize(scrollbar); 67 int x, y; 68 if (scrollbar->orientation() == HorizontalScrollbar) { 69 x = scrollbar->x() + scrollbar->width() - size.width(); 70 y = scrollbar->y(); 71 } else { 72 x = scrollbar->x(); 73 y = scrollbar->y() + scrollbar->height() - size.height(); 74 } 75 return IntRect(x, y, size.width(), size.height()); 76 } 77 78 IntRect ScrollbarThemeChromium::trackRect(ScrollbarThemeClient* scrollbar, bool) 79 { 80 IntSize bs = buttonSize(scrollbar); 81 int thickness = scrollbarThickness(scrollbar->controlSize()); 82 if (scrollbar->orientation() == HorizontalScrollbar) { 83 // Once the scrollbar becomes smaller than the size of the 84 // two buttons with a 1 pixel gap, the track disappears. 85 if (scrollbar->width() <= 2 * bs.width() + 1) 86 return IntRect(); 87 return IntRect(scrollbar->x() + bs.width(), scrollbar->y(), scrollbar->width() - 2 * bs.width(), thickness); 88 } 89 if (scrollbar->height() <= 2 * bs.height() + 1) 90 return IntRect(); 91 return IntRect(scrollbar->x(), scrollbar->y() + bs.height(), thickness, scrollbar->height() - 2 * bs.height()); 92 } 93 94 void ScrollbarThemeChromium::paintTrackBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect) 95 { 96 // Just assume a forward track part. We only paint the track as a single piece when there is no thumb. 97 if (!hasThumb(scrollbar)) 98 paintTrackPiece(context, scrollbar, rect, ForwardTrackPart); 99 } 100 101 void ScrollbarThemeChromium::paintTickmarks(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect) 102 { 103 if (scrollbar->orientation() != VerticalScrollbar) 104 return; 105 106 if (rect.height() <= 0 || rect.width() <= 0) 107 return; // nothing to draw on. 108 109 // Get the tickmarks for the frameview. 110 Vector<IntRect> tickmarks; 111 scrollbar->getTickmarks(tickmarks); 112 if (!tickmarks.size()) 113 return; 114 115 GraphicsContextStateSaver stateSaver(*context); 116 context->setShouldAntialias(false); 117 118 for (Vector<IntRect>::const_iterator i = tickmarks.begin(); i != tickmarks.end(); ++i) { 119 // Calculate how far down (in %) the tick-mark should appear. 120 const float percent = static_cast<float>(i->y()) / scrollbar->totalSize(); 121 122 // Calculate how far down (in pixels) the tick-mark should appear. 123 const int yPos = rect.y() + (rect.height() * percent); 124 125 context->setFillColor(Color(0xCC, 0xAA, 0x00, 0xFF)); 126 FloatRect tickRect(rect.x(), yPos, rect.width(), 3); 127 context->fillRect(tickRect); 128 129 context->setFillColor(Color(0xFF, 0xDD, 0x00, 0xFF)); 130 FloatRect tickStroke(rect.x(), yPos + 1, rect.width(), 1); 131 context->fillRect(tickStroke); 132 } 133 } 134 135 } // namespace WebCore 136