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