blob: d78cafafd792b4c054d4440526c25eff5d26660d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#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
17SkScrollBarView::SkScrollBarView()
18{
rmistry@google.comd6176b02012-08-23 18:14:13 +000019 fAnim.setHostEventSink(this);
20 init_skin_anim(kScroll_SkinEnum, &fAnim);
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
rmistry@google.comd6176b02012-08-23 18:14:13 +000022 fTotalLength = 0;
23 fStartPoint = 0;
24 fShownLength = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
rmistry@google.comd6176b02012-08-23 18:14:13 +000026 this->adjust();
reed@android.com8a1c16f2008-12-17 15:59:43 +000027}
28
29void SkScrollBarView::setStart(unsigned start)
30{
rmistry@google.comd6176b02012-08-23 18:14:13 +000031 if ((int)start < 0)
32 start = 0;
33
34 if (fStartPoint != start)
35 {
36 fStartPoint = start;
37 this->adjust();
38 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000039}
40
41void SkScrollBarView::setShown(unsigned shown)
42{
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 if ((int)shown < 0)
44 shown = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
rmistry@google.comd6176b02012-08-23 18:14:13 +000046 if (fShownLength != shown)
47 {
48 fShownLength = shown;
49 this->adjust();
50 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000051}
52
53void SkScrollBarView::setTotal(unsigned total)
54{
rmistry@google.comd6176b02012-08-23 18:14:13 +000055 if ((int)total < 0)
56 total = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000057
rmistry@google.comd6176b02012-08-23 18:14:13 +000058 if (fTotalLength != total)
59 {
60 fTotalLength = total;
61 this->adjust();
62 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000063}
64
65/* virtual */ void SkScrollBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
66{
rmistry@google.comd6176b02012-08-23 18:14:13 +000067 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.com8a1c16f2008-12-17 15:59:43 +000074}
75
76/*virtual*/ void SkScrollBarView::onSizeChange()
77{
rmistry@google.comd6176b02012-08-23 18:14:13 +000078 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.com8a1c16f2008-12-17 15:59:43 +000084}
85
86/*virtual*/ void SkScrollBarView::onDraw(SkCanvas* canvas)
87{
rmistry@google.comd6176b02012-08-23 18:14:13 +000088 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.com8a1c16f2008-12-17 15:59:43 +000099}
100
101/*virtual*/ bool SkScrollBarView::onEvent(const SkEvent& evt)
102{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103 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.com8a1c16f2008-12-17 15:59:43 +0000111
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112 if (evt.findScalar("x", &width))
113 this->setWidth(width);
114 return true;
115 }
116
117 return this->INHERITED::onEvent(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118}
119
120void SkScrollBarView::adjust()
121{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122 int total = fTotalLength;
123 int start = fStartPoint;
124 int shown = fShownLength;
sugoi@google.com9c55f802013-03-07 20:52:59 +0000125// int hideBar = 0;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126
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.com9c55f802013-03-07 20:52:59 +0000130// hideBar = 1; // signal we don't want a thumb
rmistry@google.comd6176b02012-08-23 18:14:13 +0000131 }
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.com8a1c16f2008-12-17 15:59:43 +0000145}