1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScrollBarView.h"
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkAnimator.h"
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkWidgetViews.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkSystemEventTypes.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTime.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//see SkProgressBarView.cpp
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//#include "SkWidgetViews.cpp"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkScrollBarView::SkScrollBarView()
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
19d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fAnim.setHostEventSink(this);
20d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    init_skin_anim(kScroll_SkinEnum, &fAnim);
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fTotalLength = 0;
23d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fStartPoint = 0;
24d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fShownLength = 0;
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->adjust();
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkScrollBarView::setStart(unsigned start)
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
31d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((int)start < 0)
32d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        start = 0;
33d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
34d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fStartPoint != start)
35d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
36d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fStartPoint = start;
37d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->adjust();
38d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkScrollBarView::setShown(unsigned shown)
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
43d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((int)shown < 0)
44d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        shown = 0;
45d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
46d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fShownLength != shown)
47d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
48d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fShownLength = shown;
49d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->adjust();
50d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkScrollBarView::setTotal(unsigned total)
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
55d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((int)total < 0)
56d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        total = 0;
57d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
58d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fTotalLength != total)
59d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
60d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fTotalLength = total;
61d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->adjust();
62d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* virtual */ void SkScrollBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
67d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onInflate(dom, node);
68d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
69d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    int32_t value;
70d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (dom.findS32(node, "total", &value))
71d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setTotal(value);
72d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (dom.findS32(node, "shown", &value))
73d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setShown(value);
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*virtual*/ void SkScrollBarView::onSizeChange()
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
78d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onSizeChange();
79d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkEvent evt("user");
80d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    evt.setString("id", "setDim");
81d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    evt.setScalar("dimX", this->width());
82d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    evt.setScalar("dimY", this->height());
83d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fAnim.doUserEvent(evt);
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*virtual*/ void SkScrollBarView::onDraw(SkCanvas* canvas)
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
88d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkPaint                        paint;
89d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkAnimator::DifferenceType    diff = fAnim.draw(canvas, &paint, SkTime::GetMSecs());
90d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
91d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (diff == SkAnimator::kDifferent)
92d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->inval(NULL);
93d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    else if (diff == SkAnimator::kPartiallyDifferent)
94d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
95d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkRect    bounds;
96d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fAnim.getInvalBounds(&bounds);
97d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->inval(&bounds);
98d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*virtual*/ bool SkScrollBarView::onEvent(const SkEvent& evt)
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
103d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (evt.isType(SK_EventType_Inval))
104d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
105d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->inval(NULL);
106d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return true;
107d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
108d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (evt.isType("recommendDim"))
109d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
110d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkScalar    width;
111d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
112d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        if (evt.findScalar("x", &width))
113d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            this->setWidth(width);
114d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return true;
115d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
116d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
117d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return this->INHERITED::onEvent(evt);
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkScrollBarView::adjust()
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
122d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    int total = fTotalLength;
123d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    int start = fStartPoint;
124d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    int shown = fShownLength;
1259c55f801a35b0d6c39f007fae432bd13094f3c52sugoi@google.com//    int hideBar = 0;
126d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
127d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (total <= 0 || shown <= 0 || shown >= total)    // no bar to show
128d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
129d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        total = 1;        // avoid divide-by-zero. should be done by skin/script
1309c55f801a35b0d6c39f007fae432bd13094f3c52sugoi@google.com//        hideBar = 1;    // signal we don't want a thumb
131d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
132d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    else
133d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
134d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        if (start + shown > total)
135d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            start = total - shown;
136d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
137d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
138d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkEvent e("user");
139d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    e.setString("id", "adjustScrollBar");
140d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    e.setScalar("_totalLength", SkIntToScalar(total));
141d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    e.setScalar("_startPoint", SkIntToScalar(start));
142d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    e.setScalar("_shownLength", SkIntToScalar(shown));
143d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com//    e.setS32("hideBar", hideBar);
144d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fAnim.doUserEvent(e);
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
146