michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkScrollBarView.h" michael@0: #include "SkAnimator.h" michael@0: #include "SkWidgetViews.h" michael@0: #include "SkSystemEventTypes.h" michael@0: #include "SkTime.h" michael@0: michael@0: //see SkProgressBarView.cpp michael@0: //#include "SkWidgetViews.cpp" michael@0: michael@0: SkScrollBarView::SkScrollBarView() michael@0: { michael@0: fAnim.setHostEventSink(this); michael@0: init_skin_anim(kScroll_SkinEnum, &fAnim); michael@0: michael@0: fTotalLength = 0; michael@0: fStartPoint = 0; michael@0: fShownLength = 0; michael@0: michael@0: this->adjust(); michael@0: } michael@0: michael@0: void SkScrollBarView::setStart(unsigned start) michael@0: { michael@0: if ((int)start < 0) michael@0: start = 0; michael@0: michael@0: if (fStartPoint != start) michael@0: { michael@0: fStartPoint = start; michael@0: this->adjust(); michael@0: } michael@0: } michael@0: michael@0: void SkScrollBarView::setShown(unsigned shown) michael@0: { michael@0: if ((int)shown < 0) michael@0: shown = 0; michael@0: michael@0: if (fShownLength != shown) michael@0: { michael@0: fShownLength = shown; michael@0: this->adjust(); michael@0: } michael@0: } michael@0: michael@0: void SkScrollBarView::setTotal(unsigned total) michael@0: { michael@0: if ((int)total < 0) michael@0: total = 0; michael@0: michael@0: if (fTotalLength != total) michael@0: { michael@0: fTotalLength = total; michael@0: this->adjust(); michael@0: } michael@0: } michael@0: michael@0: /* virtual */ void SkScrollBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node) michael@0: { michael@0: this->INHERITED::onInflate(dom, node); michael@0: michael@0: int32_t value; michael@0: if (dom.findS32(node, "total", &value)) michael@0: this->setTotal(value); michael@0: if (dom.findS32(node, "shown", &value)) michael@0: this->setShown(value); michael@0: } michael@0: michael@0: /*virtual*/ void SkScrollBarView::onSizeChange() michael@0: { michael@0: this->INHERITED::onSizeChange(); michael@0: SkEvent evt("user"); michael@0: evt.setString("id", "setDim"); michael@0: evt.setScalar("dimX", this->width()); michael@0: evt.setScalar("dimY", this->height()); michael@0: fAnim.doUserEvent(evt); michael@0: } michael@0: michael@0: /*virtual*/ void SkScrollBarView::onDraw(SkCanvas* canvas) michael@0: { michael@0: SkPaint paint; michael@0: SkAnimator::DifferenceType diff = fAnim.draw(canvas, &paint, SkTime::GetMSecs()); michael@0: michael@0: if (diff == SkAnimator::kDifferent) michael@0: this->inval(NULL); michael@0: else if (diff == SkAnimator::kPartiallyDifferent) michael@0: { michael@0: SkRect bounds; michael@0: fAnim.getInvalBounds(&bounds); michael@0: this->inval(&bounds); michael@0: } michael@0: } michael@0: michael@0: /*virtual*/ bool SkScrollBarView::onEvent(const SkEvent& evt) michael@0: { michael@0: if (evt.isType(SK_EventType_Inval)) michael@0: { michael@0: this->inval(NULL); michael@0: return true; michael@0: } michael@0: if (evt.isType("recommendDim")) michael@0: { michael@0: SkScalar width; michael@0: michael@0: if (evt.findScalar("x", &width)) michael@0: this->setWidth(width); michael@0: return true; michael@0: } michael@0: michael@0: return this->INHERITED::onEvent(evt); michael@0: } michael@0: michael@0: void SkScrollBarView::adjust() michael@0: { michael@0: int total = fTotalLength; michael@0: int start = fStartPoint; michael@0: int shown = fShownLength; michael@0: // int hideBar = 0; michael@0: michael@0: if (total <= 0 || shown <= 0 || shown >= total) // no bar to show michael@0: { michael@0: total = 1; // avoid divide-by-zero. should be done by skin/script michael@0: // hideBar = 1; // signal we don't want a thumb michael@0: } michael@0: else michael@0: { michael@0: if (start + shown > total) michael@0: start = total - shown; michael@0: } michael@0: michael@0: SkEvent e("user"); michael@0: e.setString("id", "adjustScrollBar"); michael@0: e.setScalar("_totalLength", SkIntToScalar(total)); michael@0: e.setScalar("_startPoint", SkIntToScalar(start)); michael@0: e.setScalar("_shownLength", SkIntToScalar(shown)); michael@0: // e.setS32("hideBar", hideBar); michael@0: fAnim.doUserEvent(e); michael@0: }