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 "SkWidget.h"
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkKey.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkParsePaint.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkSystemEventTypes.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTextBox.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
18d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    static void assert_no_attr(const SkDOM& dom, const SkDOM::Node* node, const char attr[])
19d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
20d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        const char* value = dom.findAttr(node, attr);
21d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        if (value)
22d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            SkDebugf("unknown attribute %s=\"%s\"\n", attr, value);
23d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
25d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    #define assert_no_attr(dom, node, attr)
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkAnimator.h"
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTime.h"
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum SkinType {
34d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    kPushButton_SkinType,
35d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    kStaticText_SkinType,
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
37d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    kSkinTypeCount
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct SkinSuite {
41d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkinSuite();
42d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    ~SkinSuite()
43d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
44d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        for (int i = 0; i < kSkinTypeCount; i++)
45d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            delete fAnimators[i];
46d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
48d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkAnimator*    get(SkinType);
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
51d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkAnimator*    fAnimators[kSkinTypeCount];
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkinSuite::SkinSuite()
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
56d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    static const char kSkinPath[] = "skins/";
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
58d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    static const char* gSkinNames[] = {
59d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        "pushbutton_skin.xml",
60d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        "statictext_skin.xml"
61d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    };
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
63d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    for (unsigned i = 0; i < SK_ARRAY_COUNT(gSkinNames); i++)
64d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
65d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        size_t        len = strlen(gSkinNames[i]);
66d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkString    path(sizeof(kSkinPath) - 1 + len);
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
68d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        memcpy(path.writable_str(), kSkinPath, sizeof(kSkinPath) - 1);
69d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        memcpy(path.writable_str() + sizeof(kSkinPath) - 1, gSkinNames[i], len);
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
71d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fAnimators[i] = new SkAnimator;
72d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        if (!fAnimators[i]->decodeURI(path.c_str()))
73d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        {
74d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            delete fAnimators[i];
75d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            fAnimators[i] = NULL;
76d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        }
77d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkAnimator* SkinSuite::get(SkinType st)
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
82d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkASSERT((unsigned)st < kSkinTypeCount);
83d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return fAnimators[st];
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkinSuite* gSkinSuite;
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkAnimator* get_skin_animator(SkinType st)
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
91d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (gSkinSuite == NULL)
92d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        gSkinSuite = new SkinSuite;
93d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return gSkinSuite->get(st);
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
95d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return NULL;
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkWidget::Init()
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkWidget::Term()
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
107d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    delete gSkinSuite;
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkWidget::onEnabledChange()
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
112d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->inval(NULL);
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkWidget::postWidgetEvent()
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
117d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (!fEvent.isType("") && this->hasListeners())
118d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
119d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->prepareWidgetEvent(&fEvent);
120d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->postToListeners(fEvent);
121d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkWidget::prepareWidgetEvent(SkEvent*)
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
126d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // override in subclass to add any additional fields before posting
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkWidget::onInflate(const SkDOM& dom, const SkDOM::Node* node)
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
131d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onInflate(dom, node);
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
133d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((node = dom.getFirstChild(node, "event")) != NULL)
134d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fEvent.inflate(dom, node);
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comsize_t SkHasLabelWidget::getLabel(SkString* str) const
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
141d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (str)
142d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        *str = fLabel;
143d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return fLabel.size();
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comsize_t SkHasLabelWidget::getLabel(char buffer[]) const
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
148d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (buffer)
149d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        memcpy(buffer, fLabel.c_str(), fLabel.size());
150d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return fLabel.size();
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkHasLabelWidget::setLabel(const SkString& str)
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
155d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->setLabel(str.c_str(), str.size());
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkHasLabelWidget::setLabel(const char label[])
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
160d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->setLabel(label, strlen(label));
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkHasLabelWidget::setLabel(const char label[], size_t len)
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
165d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (!fLabel.equals(label, len))
166d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
167d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fLabel.set(label, len);
168d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->onLabelChange();
169d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkHasLabelWidget::onLabelChange()
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
174d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // override in subclass
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkHasLabelWidget::onInflate(const SkDOM& dom, const SkDOM::Node* node)
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
179d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onInflate(dom, node);
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
181d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    const char* text = dom.findAttr(node, "label");
182d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (text)
183d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setLabel(text);
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////////////////////
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkButtonWidget::setButtonState(State state)
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
190d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fState != state)
191d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
192d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fState = state;
193d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->onButtonStateChange();
194d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkButtonWidget::onButtonStateChange()
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
199d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->inval(NULL);
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkButtonWidget::onInflate(const SkDOM& dom, const SkDOM::Node* node)
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
204d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onInflate(dom, node);
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
206d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    int    index;
207d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((index = dom.findList(node, "buttonState", "off,on,unknown")) >= 0)
208d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setButtonState((State)index);
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////////////////////
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkPushButtonWidget::onEvent(const SkEvent& evt)
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
215d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (evt.isType(SK_EventType_Key) && evt.getFast32() == kOK_SkKey)
216d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
217d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->postWidgetEvent();
218d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return true;
219d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
220d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return this->INHERITED::onEvent(evt);
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const char* computeAnimatorState(int enabled, int focused, SkButtonWidget::State state)
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
225d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (!enabled)
226d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return "disabled";
227d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (state == SkButtonWidget::kOn_State)
228d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
229d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkASSERT(focused);
230d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return "enabled-pressed";
231d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
232d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (focused)
233d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return "enabled-focused";
234d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return "enabled";
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2377c9d0f3104408a64d52b84643f116179022d73bdcommit-bot@chromium.org#include "SkBlurMask.h"
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlurMaskFilter.h"
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkEmbossMaskFilter.h"
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void create_emboss(SkPaint* paint, SkScalar radius, bool focus, bool pressed)
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
243d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkEmbossMaskFilter::Light    light;
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
245d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    light.fDirection[0] = SK_Scalar1/2;
246d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    light.fDirection[1] = SK_Scalar1/2;
247d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    light.fDirection[2] = SK_Scalar1/3;
248d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    light.fAmbient        = 0x48;
249d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    light.fSpecular        = 0x80;
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
251d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (pressed)
252d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
253d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        light.fDirection[0] = -light.fDirection[0];
254d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        light.fDirection[1] = -light.fDirection[1];
255d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
256d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (focus)
257d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        light.fDirection[2] += SK_Scalar1/4;
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2597c9d0f3104408a64d52b84643f116179022d73bdcommit-bot@chromium.org    SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
2607c9d0f3104408a64d52b84643f116179022d73bdcommit-bot@chromium.org    paint->setMaskFilter(new SkEmbossMaskFilter(sigma, light))->unref();
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkPushButtonWidget::onDraw(SkCanvas* canvas)
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
265d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onDraw(canvas);
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
267d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkString label;
268d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->getLabel(&label);
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
270d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkAnimator* anim = get_skin_animator(kPushButton_SkinType);
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
272d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (anim)
273d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
274d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkEvent    evt("user");
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
276d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        evt.setString("id", "prime");
277d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        evt.setScalar("prime-width", this->width());
278d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        evt.setScalar("prime-height", this->height());
279d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        evt.setString("prime-text", label);
280d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        evt.setString("prime-state", computeAnimatorState(this->isEnabled(), this->hasFocus(), this->getButtonState()));
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
282d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        (void)anim->doUserEvent(evt);
283d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkPaint paint;
284d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        anim->draw(canvas, &paint, SkTime::GetMSecs());
285d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
286d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    else
287d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
288d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkRect    r;
289d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkPaint    p;
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
291d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        r.set(0, 0, this->width(), this->height());
292d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        p.setAntiAliasOn(true);
293d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        p.setColor(SK_ColorBLUE);
294d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        create_emboss(&p, SkIntToScalar(12)/5, this->hasFocus(), this->getButtonState() == kOn_State);
295d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        canvas->drawRoundRect(r, SkScalarHalf(this->height()), SkScalarHalf(this->height()), p);
296d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        p.setMaskFilter(NULL);
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
298d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        p.setTextAlign(SkPaint::kCenter_Align);
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
300d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkTextBox    box;
301d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        box.setMode(SkTextBox::kOneLine_Mode);
302d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        box.setSpacingAlign(SkTextBox::kCenter_SpacingAlign);
303d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        box.setBox(0, 0, this->width(), this->height());
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
305d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com//        if (this->getButtonState() == kOn_State)
306d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com//            p.setColor(SK_ColorRED);
307d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com//        else
308d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            p.setColor(SK_ColorWHITE);
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
310d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        box.draw(canvas, label.c_str(), label.size(), p);
311d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3149c55f801a35b0d6c39f007fae432bd13094f3c52sugoi@google.comSkView::Click* SkPushButtonWidget::onFindClickHandler(SkScalar x, SkScalar y, unsigned modi)
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
316d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->acceptFocus();
317d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return new Click(this);
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkPushButtonWidget::onClick(Click* click)
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
322d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkRect    r;
323d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    State    state = kOff_State;
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
325d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->getLocalBounds(&r);
326d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (r.contains(click->fCurr))
327d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
328d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        if (click->fState == Click::kUp_State)
329d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            this->postWidgetEvent();
330d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        else
331d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            state = kOn_State;
332d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
333d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->setButtonState(state);
334d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return true;
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////////////////
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkStaticTextView::SkStaticTextView(U32 flags) : SkView(flags)
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
341d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fMargin.set(0, 0);
342d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fMode = kFixedSize_Mode;
343d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fSpacingAlign = SkTextBox::kStart_SpacingAlign;
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkStaticTextView::~SkStaticTextView()
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::computeSize()
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
352d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fMode == kAutoWidth_Mode)
353d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
354d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkScalar width = fPaint.measureText(fText.c_str(), fText.size(), NULL, NULL);
355d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setWidth(width + fMargin.fX * 2);
356d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
357d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    else if (fMode == kAutoHeight_Mode)
358d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
359d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkScalar width = this->width() - fMargin.fX * 2;
360d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        int lines = width > 0 ? SkTextLineBreaker::CountLines(fText.c_str(), fText.size(), fPaint, width) : 0;
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
362d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkScalar    before, after;
363d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        (void)fPaint.measureText(0, NULL, &before, &after);
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
365d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setHeight(lines * (after - before) + fMargin.fY * 2);
366d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::setMode(Mode mode)
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
371d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkASSERT((unsigned)mode < kModeCount);
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
373d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fMode != mode)
374d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
375d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fMode = SkToU8(mode);
376d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->computeSize();
377d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::setSpacingAlign(SkTextBox::SpacingAlign align)
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
382d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    fSpacingAlign = SkToU8(align);
383d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->inval(NULL);
3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::getMargin(SkPoint* margin) const
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
388d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (margin)
389d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        *margin = fMargin;
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::setMargin(SkScalar dx, SkScalar dy)
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
394d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fMargin.fX != dx || fMargin.fY != dy)
395d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
396d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fMargin.set(dx, dy);
397d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->computeSize();
398d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->inval(NULL);
399d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comsize_t SkStaticTextView::getText(SkString* text) const
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
404d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (text)
405d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        *text = fText;
406d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return fText.size();
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comsize_t SkStaticTextView::getText(char text[]) const
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
411d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (text)
412d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        memcpy(text, fText.c_str(), fText.size());
413d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return fText.size();
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::setText(const SkString& text)
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
418d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->setText(text.c_str(), text.size());
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::setText(const char text[])
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
423d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->setText(text, strlen(text));
4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::setText(const char text[], size_t len)
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
428d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (!fText.equals(text, len))
429d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
430d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fText.set(text, len);
431d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->computeSize();
432d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->inval(NULL);
433d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::getPaint(SkPaint* paint) const
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
438d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (paint)
439d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        *paint = fPaint;
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::setPaint(const SkPaint& paint)
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
444d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fPaint != paint)
445d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
446d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fPaint = paint;
447d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->computeSize();
448d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->inval(NULL);
449d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::onDraw(SkCanvas* canvas)
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
454d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onDraw(canvas);
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
456d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fText.isEmpty())
457d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return;
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
459d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkTextBox    box;
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
461d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    box.setMode(fMode == kAutoWidth_Mode ? SkTextBox::kOneLine_Mode : SkTextBox::kLineBreak_Mode);
462d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    box.setSpacingAlign(this->getSpacingAlign());
463d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    box.setBox(fMargin.fX, fMargin.fY, this->width() - fMargin.fX, this->height() - fMargin.fY);
464d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    box.draw(canvas, fText.c_str(), fText.size(), fPaint);
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkStaticTextView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
469d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onInflate(dom, node);
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
471d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    int    index;
472d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((index = dom.findList(node, "mode", "fixed,auto-width,auto-height")) >= 0)
473d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setMode((Mode)index);
474d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    else
475d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        assert_no_attr(dom, node, "mode");
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
477d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((index = dom.findList(node, "spacing-align", "start,center,end")) >= 0)
478d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setSpacingAlign((SkTextBox::SpacingAlign)index);
479d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    else
480d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        assert_no_attr(dom, node, "mode");
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
482d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkScalar s[2];
483d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (dom.findScalars(node, "margin", s, 2))
484d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setMargin(s[0], s[1]);
485d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    else
486d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        assert_no_attr(dom, node, "margin");
4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
488d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    const char* text = dom.findAttr(node, "text");
489d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (text)
490d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setText(text);
4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
492d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if ((node = dom.getFirstChild(node, "paint")) != NULL)
493d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkPaint_Inflate(&fPaint, dom, node);
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////////////////////////////////////
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkImageDecoder.h"
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkBitmapView::SkBitmapView(U32 flags) : SkView(flags)
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkBitmapView::~SkBitmapView()
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBitmapView::getBitmap(SkBitmap* bitmap) const
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
510d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (bitmap)
511d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        *bitmap = fBitmap;
512e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    return fBitmap.colorType() != kUnknown_SkColorType;
5138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkBitmapView::setBitmap(const SkBitmap* bitmap, bool viewOwnsPixels)
5168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
517d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (bitmap)
518d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
519d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fBitmap = *bitmap;
520d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fBitmap.setOwnsPixels(viewOwnsPixels);
521d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBitmapView::loadBitmapFromFile(const char path[])
5258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
526d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    SkBitmap    bitmap;
5278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
528d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (SkImageDecoder::DecodeFile(path, &bitmap))
529d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
530d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        this->setBitmap(&bitmap, true);
531d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        bitmap.setOwnsPixels(false);
532d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return true;
533d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
534d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    return false;
5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkBitmapView::onDraw(SkCanvas* canvas)
5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
539e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    if (fBitmap.colorType() != kUnknown_SkColorType &&
540d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        fBitmap.width() && fBitmap.height())
541d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    {
542d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkAutoCanvasRestore    restore(canvas, true);
543d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        SkPaint                p;
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
545d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        p.setFilterType(SkPaint::kBilinear_FilterType);
546d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        canvas->scale(    this->width() / fBitmap.width(),
547d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com                        this->height() / fBitmap.height(),
548d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com                        0, 0);
549d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        canvas->drawBitmap(fBitmap, 0, 0, p);
550d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkBitmapView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
555d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    this->INHERITED::onInflate(dom, node);
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
557d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    const char* src = dom.findAttr(node, "src");
558d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (src)
559d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        (void)this->loadBitmapFromFile(src);
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
563