WebKit merge 128969:128981
[chromium-blink-merge.git] / cc / CCScrollbarGeometryFixedThumb.cpp
blobdc55829fb8cb8282f6b94004d509500d19e6cd62
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "config.h"
7 #include "CCScrollbarGeometryFixedThumb.h"
9 #include <cmath>
10 #include <public/WebRect.h>
11 #include <public/WebScrollbar.h>
13 using WebKit::WebRect;
14 using WebKit::WebScrollbar;
15 using WebKit::WebScrollbarThemeGeometry;
17 namespace cc {
19 PassOwnPtr<CCScrollbarGeometryFixedThumb> CCScrollbarGeometryFixedThumb::create(PassOwnPtr<WebScrollbarThemeGeometry> geometry)
21 return adoptPtr(new CCScrollbarGeometryFixedThumb(geometry));
24 CCScrollbarGeometryFixedThumb::~CCScrollbarGeometryFixedThumb()
28 void CCScrollbarGeometryFixedThumb::update(WebScrollbar* scrollbar)
30 int length = CCScrollbarGeometryStub::thumbLength(scrollbar);
32 if (scrollbar->orientation() == WebScrollbar::Horizontal)
33 m_thumbSize = IntSize(length, scrollbar->size().height);
34 else
35 m_thumbSize = IntSize(scrollbar->size().width, length);
38 WebScrollbarThemeGeometry* CCScrollbarGeometryFixedThumb::clone() const
40 CCScrollbarGeometryFixedThumb* geometry = new CCScrollbarGeometryFixedThumb(adoptPtr(CCScrollbarGeometryStub::clone()));
41 geometry->m_thumbSize = m_thumbSize;
42 return geometry;
45 int CCScrollbarGeometryFixedThumb::thumbLength(WebScrollbar* scrollbar)
47 if (scrollbar->orientation() == WebScrollbar::Horizontal)
48 return m_thumbSize.width();
49 return m_thumbSize.height();
52 int CCScrollbarGeometryFixedThumb::thumbPosition(WebScrollbar* scrollbar)
54 if (scrollbar->enabled()) {
55 float size = scrollbar->maximum();
56 if (!size)
57 return 1;
58 int value = std::min(std::max(0, scrollbar->value()), scrollbar->maximum());
59 float pos = (trackLength(scrollbar) - thumbLength(scrollbar)) * value / size;
60 return static_cast<int>(floorf((pos < 1 && pos > 0) ? 1 : pos));
62 return 0;
64 void CCScrollbarGeometryFixedThumb::splitTrack(WebScrollbar* scrollbar, const WebRect& unconstrainedTrackRect, WebRect& beforeThumbRect, WebRect& thumbRect, WebRect& afterThumbRect)
66 // This is a reimplementation of ScrollbarThemeComposite::splitTrack.
67 // Because the WebScrollbarThemeGeometry functions call down to native
68 // ScrollbarThemeComposite code which uses ScrollbarThemeComposite virtual
69 // helpers, there's no way to override a helper like thumbLength from
70 // the WebScrollbarThemeGeometry level. So, these three functions
71 // (splitTrack, thumbPosition, thumbLength) are copied here so that the
72 // WebScrollbarThemeGeometry helper functions are used instead and
73 // a fixed size thumbLength can be used.
75 WebRect trackRect = constrainTrackRectToTrackPieces(scrollbar, unconstrainedTrackRect);
76 int thickness = scrollbar->orientation() == WebScrollbar::Horizontal ? scrollbar->size().height : scrollbar->size().width;
77 int thumbPos = thumbPosition(scrollbar);
78 if (scrollbar->orientation() == WebScrollbar::Horizontal) {
79 thumbRect = WebRect(trackRect.x + thumbPos, trackRect.y + (trackRect.height - thickness) / 2, thumbLength(scrollbar), thickness);
80 beforeThumbRect = WebRect(trackRect.x, trackRect.y, thumbPos + thumbRect.width / 2, trackRect.height);
81 afterThumbRect = WebRect(trackRect.x + beforeThumbRect.width, trackRect.y, trackRect.x + trackRect.width - beforeThumbRect.x - beforeThumbRect.width, trackRect.height);
82 } else {
83 thumbRect = WebRect(trackRect.x + (trackRect.width - thickness) / 2, trackRect.y + thumbPos, thickness, thumbLength(scrollbar));
84 beforeThumbRect = WebRect(trackRect.x, trackRect.y, trackRect.width, thumbPos + thumbRect.height / 2);
85 afterThumbRect = WebRect(trackRect.x, trackRect.y + beforeThumbRect.height, trackRect.width, trackRect.y + trackRect.height - beforeThumbRect.y - beforeThumbRect.height);
89 CCScrollbarGeometryFixedThumb::CCScrollbarGeometryFixedThumb(PassOwnPtr<WebScrollbarThemeGeometry> geometry)
90 : CCScrollbarGeometryStub(geometry)