OSDN Git Service

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