1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/views/animated/SkScrollBarView.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 +#include "SkScrollBarView.h" 1.12 +#include "SkAnimator.h" 1.13 +#include "SkWidgetViews.h" 1.14 +#include "SkSystemEventTypes.h" 1.15 +#include "SkTime.h" 1.16 + 1.17 +//see SkProgressBarView.cpp 1.18 +//#include "SkWidgetViews.cpp" 1.19 + 1.20 +SkScrollBarView::SkScrollBarView() 1.21 +{ 1.22 + fAnim.setHostEventSink(this); 1.23 + init_skin_anim(kScroll_SkinEnum, &fAnim); 1.24 + 1.25 + fTotalLength = 0; 1.26 + fStartPoint = 0; 1.27 + fShownLength = 0; 1.28 + 1.29 + this->adjust(); 1.30 +} 1.31 + 1.32 +void SkScrollBarView::setStart(unsigned start) 1.33 +{ 1.34 + if ((int)start < 0) 1.35 + start = 0; 1.36 + 1.37 + if (fStartPoint != start) 1.38 + { 1.39 + fStartPoint = start; 1.40 + this->adjust(); 1.41 + } 1.42 +} 1.43 + 1.44 +void SkScrollBarView::setShown(unsigned shown) 1.45 +{ 1.46 + if ((int)shown < 0) 1.47 + shown = 0; 1.48 + 1.49 + if (fShownLength != shown) 1.50 + { 1.51 + fShownLength = shown; 1.52 + this->adjust(); 1.53 + } 1.54 +} 1.55 + 1.56 +void SkScrollBarView::setTotal(unsigned total) 1.57 +{ 1.58 + if ((int)total < 0) 1.59 + total = 0; 1.60 + 1.61 + if (fTotalLength != total) 1.62 + { 1.63 + fTotalLength = total; 1.64 + this->adjust(); 1.65 + } 1.66 +} 1.67 + 1.68 +/* virtual */ void SkScrollBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node) 1.69 +{ 1.70 + this->INHERITED::onInflate(dom, node); 1.71 + 1.72 + int32_t value; 1.73 + if (dom.findS32(node, "total", &value)) 1.74 + this->setTotal(value); 1.75 + if (dom.findS32(node, "shown", &value)) 1.76 + this->setShown(value); 1.77 +} 1.78 + 1.79 +/*virtual*/ void SkScrollBarView::onSizeChange() 1.80 +{ 1.81 + this->INHERITED::onSizeChange(); 1.82 + SkEvent evt("user"); 1.83 + evt.setString("id", "setDim"); 1.84 + evt.setScalar("dimX", this->width()); 1.85 + evt.setScalar("dimY", this->height()); 1.86 + fAnim.doUserEvent(evt); 1.87 +} 1.88 + 1.89 +/*virtual*/ void SkScrollBarView::onDraw(SkCanvas* canvas) 1.90 +{ 1.91 + SkPaint paint; 1.92 + SkAnimator::DifferenceType diff = fAnim.draw(canvas, &paint, SkTime::GetMSecs()); 1.93 + 1.94 + if (diff == SkAnimator::kDifferent) 1.95 + this->inval(NULL); 1.96 + else if (diff == SkAnimator::kPartiallyDifferent) 1.97 + { 1.98 + SkRect bounds; 1.99 + fAnim.getInvalBounds(&bounds); 1.100 + this->inval(&bounds); 1.101 + } 1.102 +} 1.103 + 1.104 +/*virtual*/ bool SkScrollBarView::onEvent(const SkEvent& evt) 1.105 +{ 1.106 + if (evt.isType(SK_EventType_Inval)) 1.107 + { 1.108 + this->inval(NULL); 1.109 + return true; 1.110 + } 1.111 + if (evt.isType("recommendDim")) 1.112 + { 1.113 + SkScalar width; 1.114 + 1.115 + if (evt.findScalar("x", &width)) 1.116 + this->setWidth(width); 1.117 + return true; 1.118 + } 1.119 + 1.120 + return this->INHERITED::onEvent(evt); 1.121 +} 1.122 + 1.123 +void SkScrollBarView::adjust() 1.124 +{ 1.125 + int total = fTotalLength; 1.126 + int start = fStartPoint; 1.127 + int shown = fShownLength; 1.128 +// int hideBar = 0; 1.129 + 1.130 + if (total <= 0 || shown <= 0 || shown >= total) // no bar to show 1.131 + { 1.132 + total = 1; // avoid divide-by-zero. should be done by skin/script 1.133 +// hideBar = 1; // signal we don't want a thumb 1.134 + } 1.135 + else 1.136 + { 1.137 + if (start + shown > total) 1.138 + start = total - shown; 1.139 + } 1.140 + 1.141 + SkEvent e("user"); 1.142 + e.setString("id", "adjustScrollBar"); 1.143 + e.setScalar("_totalLength", SkIntToScalar(total)); 1.144 + e.setScalar("_startPoint", SkIntToScalar(start)); 1.145 + e.setScalar("_shownLength", SkIntToScalar(shown)); 1.146 +// e.setS32("hideBar", hideBar); 1.147 + fAnim.doUserEvent(e); 1.148 +}