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 */
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
SkScrollBarView()17 SkScrollBarView::SkScrollBarView()
18 {
19 fAnim.setHostEventSink(this);
20 init_skin_anim(kScroll_SkinEnum, &fAnim);
21
22 fTotalLength = 0;
23 fStartPoint = 0;
24 fShownLength = 0;
25
26 this->adjust();
27 }
28
setStart(unsigned start)29 void SkScrollBarView::setStart(unsigned start)
30 {
31 if ((int)start < 0)
32 start = 0;
33
34 if (fStartPoint != start)
35 {
36 fStartPoint = start;
37 this->adjust();
38 }
39 }
40
setShown(unsigned shown)41 void SkScrollBarView::setShown(unsigned shown)
42 {
43 if ((int)shown < 0)
44 shown = 0;
45
46 if (fShownLength != shown)
47 {
48 fShownLength = shown;
49 this->adjust();
50 }
51 }
52
setTotal(unsigned total)53 void SkScrollBarView::setTotal(unsigned total)
54 {
55 if ((int)total < 0)
56 total = 0;
57
58 if (fTotalLength != total)
59 {
60 fTotalLength = total;
61 this->adjust();
62 }
63 }
64
onInflate(const SkDOM & dom,const SkDOM::Node * node)65 /* virtual */ void SkScrollBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
66 {
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);
74 }
75
onSizeChange()76 /*virtual*/ void SkScrollBarView::onSizeChange()
77 {
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);
84 }
85
onDraw(SkCanvas * canvas)86 /*virtual*/ void SkScrollBarView::onDraw(SkCanvas* canvas)
87 {
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 }
99 }
100
onEvent(const SkEvent & evt)101 /*virtual*/ bool SkScrollBarView::onEvent(const SkEvent& evt)
102 {
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;
111
112 if (evt.findScalar("x", &width))
113 this->setWidth(width);
114 return true;
115 }
116
117 return this->INHERITED::onEvent(evt);
118 }
119
adjust()120 void SkScrollBarView::adjust()
121 {
122 int total = fTotalLength;
123 int start = fStartPoint;
124 int shown = fShownLength;
125 // int hideBar = 0;
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
130 // hideBar = 1; // signal we don't want a thumb
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);
145 }
146