1/**
2 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22
23#if ENABLE(WML)
24#include "WMLDoElement.h"
25
26#include "Event.h"
27#include "EventNames.h"
28#include "HTMLNames.h"
29#include "KeyboardEvent.h"
30#include "MappedAttribute.h"
31#include "Page.h"
32#include "RenderButton.h"
33#include "WMLCardElement.h"
34#include "WMLDocument.h"
35#include "WMLTaskElement.h"
36#include "WMLTimerElement.h"
37#include "WMLNames.h"
38#include "WMLPageState.h"
39#include "WMLVariables.h"
40
41namespace WebCore {
42
43using namespace WMLNames;
44
45WMLDoElement::WMLDoElement(const QualifiedName& tagName, Document* doc)
46    : WMLElement(tagName, doc)
47    , m_task(0)
48    , m_isActive(false)
49    , m_isNoop(false)
50    , m_isOptional(false)
51{
52}
53
54void WMLDoElement::defaultEventHandler(Event* event)
55{
56    if (m_isOptional)
57        return;
58
59    if (event->type() == eventNames().keypressEvent) {
60        WMLElement::defaultEventHandler(event);
61        return;
62    }
63
64    if (event->type() != eventNames().clickEvent && event->type() != eventNames().keydownEvent)
65        return;
66
67    if (event->isKeyboardEvent()
68        && static_cast<KeyboardEvent*>(event)->keyIdentifier() != "Enter")
69        return;
70
71    if (m_type == "accept" || m_type == "options") {
72        if (m_task)
73            m_task->executeTask();
74    } else if (m_type == "prev") {
75        ASSERT(document()->isWMLDocument());
76        WMLDocument* document = static_cast<WMLDocument*>(this->document());
77
78        WMLPageState* pageState = wmlPageStateForDocument(document);
79        if (!pageState)
80            return;
81
82        // Stop the timer of the current card if it is active
83        if (WMLCardElement* card = document->activeCard()) {
84            if (WMLTimerElement* eventTimer = card->eventTimer())
85                eventTimer->stop();
86        }
87
88        pageState->page()->goBack();
89    } else if (m_type == "reset") {
90        WMLPageState* pageState = wmlPageStateForDocument(document());
91        if (!pageState)
92            return;
93
94        pageState->reset();
95    }
96}
97
98void WMLDoElement::parseMappedAttribute(MappedAttribute* attr)
99{
100    if (attr->name() == HTMLNames::typeAttr)
101        m_type = parseValueForbiddingVariableReferences(attr->value());
102    else if (attr->name() == HTMLNames::nameAttr)
103        m_name = parseValueForbiddingVariableReferences(attr->value());
104    else if (attr->name() == optionalAttr)
105        m_isOptional = (attr->value() == "true");
106    else
107        WMLElement::parseMappedAttribute(attr);
108}
109
110void WMLDoElement::insertedIntoDocument()
111{
112    WMLElement::insertedIntoDocument();
113
114    // Spec: An unspecified 'name' defaults to the value of the 'type' attribute.
115    if (!hasAttribute(HTMLNames::nameAttr))
116        m_name = m_type;
117
118    Node* parent = parentNode();
119    if (!parent || !parent->isWMLElement())
120        return;
121
122    if (WMLEventHandlingElement* eventHandlingElement = toWMLEventHandlingElement(static_cast<WMLElement*>(parent)))
123        eventHandlingElement->registerDoElement(this, document());
124}
125
126void WMLDoElement::removedFromDocument()
127{
128    Node* parent = parentNode();
129
130    if (parent  && parent->isWMLElement()) {
131        if (WMLEventHandlingElement* eventHandlingElement = toWMLEventHandlingElement(static_cast<WMLElement*>(parent)))
132            eventHandlingElement->deregisterDoElement(this);
133    }
134
135    WMLElement::removedFromDocument();
136}
137
138void WMLDoElement::attach()
139{
140    WMLElement::attach();
141
142    // The call to updateFromElement() needs to go after the call through
143    // to the base class's attach() because that can sometimes do a close
144    // on the renderer.
145    if (renderer())
146        renderer()->updateFromElement();
147}
148
149RenderObject* WMLDoElement::createRenderer(RenderArena* arena, RenderStyle* style)
150{
151    if (!m_isActive || m_isOptional || m_isNoop)
152        return 0;
153
154    if (style) {
155        style->setUnique();
156        style->setBackgroundColor(Color::lightGray);
157    }
158
159    return new (arena) RenderButton(this);
160}
161
162void WMLDoElement::recalcStyle(StyleChange change)
163{
164    WMLElement::recalcStyle(change);
165
166    if (renderer())
167        renderer()->updateFromElement();
168}
169
170void WMLDoElement::registerTask(WMLTaskElement* task)
171{
172    ASSERT(!m_task);
173    m_task = task;
174}
175
176void WMLDoElement::deregisterTask(WMLTaskElement* task)
177{
178    ASSERT_UNUSED(task, m_task == task);
179    m_task = 0;
180}
181
182String WMLDoElement::label() const
183{
184    return parseValueSubstitutingVariableReferences(getAttribute(HTMLNames::labelAttr));
185}
186
187}
188
189#endif
190