The Android Open Source Project | 0910916 | 2009-03-03 19:30:35 -0800 | [diff] [blame^] | 1 | #include "SkScrollBarView.h" |
| 2 | #include "SkAnimator.h" |
| 3 | #include "SkWidgetViews.h" |
| 4 | #include "SkSystemEventTypes.h" |
| 5 | #include "SkTime.h" |
| 6 | |
| 7 | //see SkProgressBarView.cpp |
| 8 | //#include "SkWidgetViews.cpp" |
| 9 | |
| 10 | SkScrollBarView::SkScrollBarView() |
| 11 | { |
| 12 | fAnim.setHostEventSink(this); |
| 13 | init_skin_anim(kScroll_SkinEnum, &fAnim); |
| 14 | |
| 15 | fTotalLength = 0; |
| 16 | fStartPoint = 0; |
| 17 | fShownLength = 0; |
| 18 | |
| 19 | this->adjust(); |
| 20 | } |
| 21 | |
| 22 | void SkScrollBarView::setStart(unsigned start) |
| 23 | { |
| 24 | if ((int)start < 0) |
| 25 | start = 0; |
| 26 | |
| 27 | if (fStartPoint != start) |
| 28 | { |
| 29 | fStartPoint = start; |
| 30 | this->adjust(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void SkScrollBarView::setShown(unsigned shown) |
| 35 | { |
| 36 | if ((int)shown < 0) |
| 37 | shown = 0; |
| 38 | |
| 39 | if (fShownLength != shown) |
| 40 | { |
| 41 | fShownLength = shown; |
| 42 | this->adjust(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void SkScrollBarView::setTotal(unsigned total) |
| 47 | { |
| 48 | if ((int)total < 0) |
| 49 | total = 0; |
| 50 | |
| 51 | if (fTotalLength != total) |
| 52 | { |
| 53 | fTotalLength = total; |
| 54 | this->adjust(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* virtual */ void SkScrollBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 59 | { |
| 60 | this->INHERITED::onInflate(dom, node); |
| 61 | |
| 62 | int32_t value; |
| 63 | if (dom.findS32(node, "total", &value)) |
| 64 | this->setTotal(value); |
| 65 | if (dom.findS32(node, "shown", &value)) |
| 66 | this->setShown(value); |
| 67 | } |
| 68 | |
| 69 | /*virtual*/ void SkScrollBarView::onSizeChange() |
| 70 | { |
| 71 | this->INHERITED::onSizeChange(); |
| 72 | SkEvent evt("user"); |
| 73 | evt.setString("id", "setDim"); |
| 74 | evt.setScalar("dimX", this->width()); |
| 75 | evt.setScalar("dimY", this->height()); |
| 76 | fAnim.doUserEvent(evt); |
| 77 | } |
| 78 | |
| 79 | /*virtual*/ void SkScrollBarView::onDraw(SkCanvas* canvas) |
| 80 | { |
| 81 | SkPaint paint; |
| 82 | SkAnimator::DifferenceType diff = fAnim.draw(canvas, &paint, SkTime::GetMSecs()); |
| 83 | |
| 84 | if (diff == SkAnimator::kDifferent) |
| 85 | this->inval(NULL); |
| 86 | else if (diff == SkAnimator::kPartiallyDifferent) |
| 87 | { |
| 88 | SkRect bounds; |
| 89 | fAnim.getInvalBounds(&bounds); |
| 90 | this->inval(&bounds); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /*virtual*/ bool SkScrollBarView::onEvent(const SkEvent& evt) |
| 95 | { |
| 96 | if (evt.isType(SK_EventType_Inval)) |
| 97 | { |
| 98 | this->inval(NULL); |
| 99 | return true; |
| 100 | } |
| 101 | if (evt.isType("recommendDim")) |
| 102 | { |
| 103 | SkScalar width; |
| 104 | |
| 105 | if (evt.findScalar("x", &width)) |
| 106 | this->setWidth(width); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | return this->INHERITED::onEvent(evt); |
| 111 | } |
| 112 | |
| 113 | void SkScrollBarView::adjust() |
| 114 | { |
| 115 | int total = fTotalLength; |
| 116 | int start = fStartPoint; |
| 117 | int shown = fShownLength; |
| 118 | int hideBar = 0; |
| 119 | |
| 120 | if (total <= 0 || shown <= 0 || shown >= total) // no bar to show |
| 121 | { |
| 122 | total = 1; // avoid divide-by-zero. should be done by skin/script |
| 123 | hideBar = 1; // signal we don't want a thumb |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | if (start + shown > total) |
| 128 | start = total - shown; |
| 129 | } |
| 130 | |
| 131 | SkEvent e("user"); |
| 132 | e.setString("id", "adjustScrollBar"); |
| 133 | e.setScalar("_totalLength", SkIntToScalar(total)); |
| 134 | e.setScalar("_startPoint", SkIntToScalar(start)); |
| 135 | e.setScalar("_shownLength", SkIntToScalar(shown)); |
| 136 | // e.setS32("hideBar", hideBar); |
| 137 | fAnim.doUserEvent(e); |
| 138 | } |
| 139 | |