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 "ScrollbarThemeChromium.h" 29 30 #include "PlatformBridge.h" 31 #include "PlatformMouseEvent.h" 32 #include "ScrollableArea.h" 33 #include "Scrollbar.h" 34 #include "ScrollbarThemeComposite.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(Scrollbar* 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(Scrollbar* 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(Scrollbar* 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(Scrollbar* scrollbar, bool) 79 { 80 IntSize bs = buttonSize(scrollbar); 81 // The buttons at the top and bottom of the scrollbar are square, so the 82 // thickness of the scrollbar is also their height. 83 int thickness = scrollbarThickness(scrollbar->controlSize()); 84 if (scrollbar->orientation() == HorizontalScrollbar) { 85 // Once the scrollbar becomes smaller than the natural size of the 86 // two buttons, the track disappears. 87 if (scrollbar->width() < 2 * thickness) 88 return IntRect(); 89 return IntRect(scrollbar->x() + bs.width(), scrollbar->y(), scrollbar->width() - 2 * bs.width(), thickness); 90 } 91 if (scrollbar->height() < 2 * thickness) 92 return IntRect(); 93 return IntRect(scrollbar->x(), scrollbar->y() + bs.height(), thickness, scrollbar->height() - 2 * bs.height()); 94 } 95 96 void ScrollbarThemeChromium::paintTrackBackground(GraphicsContext* context, Scrollbar* scrollbar, const IntRect& rect) 97 { 98 // Just assume a forward track part. We only paint the track as a single piece when there is no thumb. 99 if (!hasThumb(scrollbar)) 100 paintTrackPiece(context, scrollbar, rect, ForwardTrackPart); 101 } 102 103 void ScrollbarThemeChromium::paintTickmarks(GraphicsContext* context, Scrollbar* scrollbar, const IntRect& rect) 104 { 105 if (scrollbar->orientation() != VerticalScrollbar) 106 return; 107 108 if (rect.height() <= 0 || rect.width() <= 0) 109 return; // nothing to draw on. 110 111 // Get the tickmarks for the frameview. 112 Vector<IntRect> tickmarks; 113 scrollbar->scrollableArea()->getTickmarks(tickmarks); 114 if (!tickmarks.size()) 115 return; 116 117 // Get the image for the tickmarks. 118 static RefPtr<Image> dash = Image::loadPlatformResource("tickmarkDash"); 119 if (dash->isNull()) { 120 ASSERT_NOT_REACHED(); 121 return; 122 } 123 124 context->save(); 125 126 for (Vector<IntRect>::const_iterator i = tickmarks.begin(); i != tickmarks.end(); ++i) { 127 // Calculate how far down (in %) the tick-mark should appear. 128 const float percent = static_cast<float>(i->y()) / scrollbar->totalSize(); 129 130 // Calculate how far down (in pixels) the tick-mark should appear. 131 const int yPos = rect.y() + (rect.height() * percent); 132 133 IntPoint tick(scrollbar->x(), yPos); 134 context->drawImage(dash.get(), ColorSpaceDeviceRGB, tick); 135 } 136 137 context->restore(); 138 } 139 140 } // namespace WebCore 141