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 "Attribute.h"
27#include "BackForwardController.h"
28#include "Event.h"
29#include "EventNames.h"
30#include "HTMLNames.h"
31#include "KeyboardEvent.h"
32#include "Page.h"
33#include "RenderButton.h"
34#include "WMLCardElement.h"
35#include "WMLDocument.h"
36#include "WMLNames.h"
37#include "WMLPageState.h"
38#include "WMLTaskElement.h"
39#include "WMLTimerElement.h"
40#include "WMLVariables.h"
41
42namespace WebCore {
43
44using namespace WMLNames;
45
46WMLDoElement::WMLDoElement(const QualifiedName& tagName, Document* doc)
47    : WMLElement(tagName, doc)
48    , m_task(0)
49    , m_isActive(false)
50    , m_isNoop(false)
51    , m_isOptional(false)
52{
53}
54
55PassRefPtr<WMLDoElement> WMLDoElement::create(const QualifiedName& tagName, Document* document)
56{
57    return adoptRef(new WMLDoElement(tagName, document));
58}
59
60void WMLDoElement::defaultEventHandler(Event* event)
61{
62    if (m_isOptional)
63        return;
64
65    if (event->type() == eventNames().keypressEvent) {
66        WMLElement::defaultEventHandler(event);
67        return;
68    }
69
70    if (event->type() != eventNames().clickEvent && event->type() != eventNames().keydownEvent)
71        return;
72
73    if (event->isKeyboardEvent()
74        && static_cast<KeyboardEvent*>(event)->keyIdentifier() != "Enter")
75        return;
76
77    if (m_type == "accept" || m_type == "options") {
78        if (m_task)
79            m_task->executeTask();
80    } else if (m_type == "prev") {
81        ASSERT(document()->isWMLDocument());
82        WMLDocument* document = static_cast<WMLDocument*>(this->document());
83
84        WMLPageState* pageState = wmlPageStateForDocument(document);
85        if (!pageState)
86            return;
87
88        // Stop the timer of the current card if it is active
89        if (WMLCardElement* card = document->activeCard()) {
90            if (WMLTimerElement* eventTimer = card->eventTimer())
91                eventTimer->stop();
92        }
93
94        pageState->page()->backForward()->goBack();
95    } else if (m_type == "reset") {
96        WMLPageState* pageState = wmlPageStateForDocument(document());
97        if (!pageState)
98            return;
99
100        pageState->reset();
101    }
102}
103
104void WMLDoElement::parseMappedAttribute(Attribute* attr)
105{
106    if (attr->name() == HTMLNames::typeAttr)
107        m_type = parseValueForbiddingVariableReferences(attr->value());
108    else if (attr->name() == HTMLNames::nameAttr)
109        m_name = parseValueForbiddingVariableReferences(attr->value());
110    else if (attr->name() == optionalAttr)
111        m_isOptional = (attr->value() == "true");
112    else
113        WMLElement::parseMappedAttribute(attr);
114}
115
116void WMLDoElement::insertedIntoDocument()
117{
118    WMLElement::insertedIntoDocument();
119
120    // Spec: An unspecified 'name' defaults to the value of the 'type' attribute.
121    if (!hasAttribute(HTMLNames::nameAttr))
122        m_name = m_type;
123
124    ContainerNode* parent = parentNode();
125    if (!parent || !parent->isWMLElement())
126        return;
127
128    if (WMLEventHandlingElement* eventHandlingElement = toWMLEventHandlingElement(static_cast<WMLElement*>(parent)))
129        eventHandlingElement->registerDoElement(this, document());
130}
131
132void WMLDoElement::removedFromDocument()
133{
134    ContainerNode* parent = parentNode();
135
136    if (parent  && parent->isWMLElement()) {
137        if (WMLEventHandlingElement* eventHandlingElement = toWMLEventHandlingElement(static_cast<WMLElement*>(parent)))
138            eventHandlingElement->deregisterDoElement(this);
139    }
140
141    WMLElement::removedFromDocument();
142}
143
144void WMLDoElement::attach()
145{
146    WMLElement::attach();
147
148    // The call to updateFromElement() needs to go after the call through
149    // to the base class's attach() because that can sometimes do a close
150    // on the renderer.
151    if (renderer())
152        renderer()->updateFromElement();
153}
154
155RenderObject* WMLDoElement::createRenderer(RenderArena* arena, RenderStyle* style)
156{
157    if (!m_isActive || m_isOptional || m_isNoop)
158        return 0;
159
160    if (style) {
161        style->setUnique();
162        style->setBackgroundColor(Color::lightGray);
163    }
164
165    return new (arena) RenderButton(this);
166}
167
168void WMLDoElement::recalcStyle(StyleChange change)
169{
170    WMLElement::recalcStyle(change);
171
172    if (renderer())
173        renderer()->updateFromElement();
174}
175
176void WMLDoElement::registerTask(WMLTaskElement* task)
177{
178    ASSERT(!m_task);
179    m_task = task;
180}
181
182void WMLDoElement::deregisterTask(WMLTaskElement* task)
183{
184    ASSERT_UNUSED(task, m_task == task);
185    m_task = 0;
186}
187
188String WMLDoElement::label() const
189{
190    return parseValueSubstitutingVariableReferences(getAttribute(HTMLNames::labelAttr));
191}
192
193}
194
195#endif
196