aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/views/animated/SkScrollBarView.cpp
blob: d78cafafd792b4c054d4440526c25eff5d26660d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkScrollBarView.h"
#include "SkAnimator.h"
#include "SkWidgetViews.h"
#include "SkSystemEventTypes.h"
#include "SkTime.h"

//see SkProgressBarView.cpp
//#include "SkWidgetViews.cpp"

SkScrollBarView::SkScrollBarView()
{
    fAnim.setHostEventSink(this);
    init_skin_anim(kScroll_SkinEnum, &fAnim);

    fTotalLength = 0;
    fStartPoint = 0;
    fShownLength = 0;

    this->adjust();
}

void SkScrollBarView::setStart(unsigned start)
{
    if ((int)start < 0)
        start = 0;

    if (fStartPoint != start)
    {
        fStartPoint = start;
        this->adjust();
    }
}

void SkScrollBarView::setShown(unsigned shown)
{
    if ((int)shown < 0)
        shown = 0;

    if (fShownLength != shown)
    {
        fShownLength = shown;
        this->adjust();
    }
}

void SkScrollBarView::setTotal(unsigned total)
{
    if ((int)total < 0)
        total = 0;

    if (fTotalLength != total)
    {
        fTotalLength = total;
        this->adjust();
    }
}

/* virtual */ void SkScrollBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
{
    this->INHERITED::onInflate(dom, node);

    int32_t value;
    if (dom.findS32(node, "total", &value))
        this->setTotal(value);
    if (dom.findS32(node, "shown", &value))
        this->setShown(value);
}

/*virtual*/ void SkScrollBarView::onSizeChange()
{
    this->INHERITED::onSizeChange();
    SkEvent evt("user");
    evt.setString("id", "setDim");
    evt.setScalar("dimX", this->width());
    evt.setScalar("dimY", this->height());
    fAnim.doUserEvent(evt);
}

/*virtual*/ void SkScrollBarView::onDraw(SkCanvas* canvas)
{
    SkPaint                        paint;
    SkAnimator::DifferenceType    diff = fAnim.draw(canvas, &paint, SkTime::GetMSecs());

    if (diff == SkAnimator::kDifferent)
        this->inval(NULL);
    else if (diff == SkAnimator::kPartiallyDifferent)
    {
        SkRect    bounds;
        fAnim.getInvalBounds(&bounds);
        this->inval(&bounds);
    }
}

/*virtual*/ bool SkScrollBarView::onEvent(const SkEvent& evt)
{
    if (evt.isType(SK_EventType_Inval))
    {
        this->inval(NULL);
        return true;
    }
    if (evt.isType("recommendDim"))
    {
        SkScalar    width;

        if (evt.findScalar("x", &width))
            this->setWidth(width);
        return true;
    }

    return this->INHERITED::onEvent(evt);
}

void SkScrollBarView::adjust()
{
    int total = fTotalLength;
    int start = fStartPoint;
    int shown = fShownLength;
//    int hideBar = 0;

    if (total <= 0 || shown <= 0 || shown >= total)    // no bar to show
    {
        total = 1;        // avoid divide-by-zero. should be done by skin/script
//        hideBar = 1;    // signal we don't want a thumb
    }
    else
    {
        if (start + shown > total)
            start = total - shown;
    }

    SkEvent e("user");
    e.setString("id", "adjustScrollBar");
    e.setScalar("_totalLength", SkIntToScalar(total));
    e.setScalar("_startPoint", SkIntToScalar(start));
    e.setScalar("_shownLength", SkIntToScalar(shown));
//    e.setS32("hideBar", hideBar);
    fAnim.doUserEvent(e);
}