1/**
2 * Copyright (C) 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 "WMLOptionElement.h"
25
26#include "Attribute.h"
27#include "HTMLNames.h"
28#include "NodeRenderStyle.h"
29#include "RenderStyle.h"
30#include "WMLNames.h"
31#include "WMLSelectElement.h"
32
33namespace WebCore {
34
35using namespace WMLNames;
36
37WMLOptionElement::WMLOptionElement(const QualifiedName& tagName, Document* doc)
38    : WMLFormControlElement(tagName, doc)
39{
40}
41
42PassRefPtr<WMLOptionElement> WMLOptionElement::create(const QualifiedName& tagName, Document* document)
43{
44    return adoptRef(new WMLOptionElement(tagName, document));
45}
46
47WMLOptionElement::~WMLOptionElement()
48{
49}
50
51const AtomicString& WMLOptionElement::formControlType() const
52{
53    DEFINE_STATIC_LOCAL(const AtomicString, option, ("option"));
54    return option;
55}
56
57static inline WMLSelectElement* ownerSelectElement(Element* element)
58{
59    ContainerNode* select = element->parentNode();
60    while (select && !select->hasTagName(selectTag))
61        select = select->parentNode();
62
63    if (!select)
64        return 0;
65
66    return static_cast<WMLSelectElement*>(select);
67}
68
69void WMLOptionElement::accessKeyAction(bool)
70{
71    if (WMLSelectElement* select = ownerSelectElement(this))
72        select->accessKeySetSelectedIndex(OptionElement::optionIndex(select, this));
73}
74
75void WMLOptionElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
76{
77    if (WMLSelectElement* select = ownerSelectElement(this))
78        select->childrenChanged(changedByParser);
79
80    WMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
81}
82
83void WMLOptionElement::parseMappedAttribute(Attribute* attr)
84{
85    if (attr->name() == HTMLNames::valueAttr)
86        m_data.setValue(parseValueSubstitutingVariableReferences(attr->value()));
87    else if (attr->name() == HTMLNames::titleAttr)
88        m_data.setLabel(parseValueSubstitutingVariableReferences(attr->value()));
89    else if (attr->name() == onpickAttr) {
90        // Register intrinsic event in card
91        RefPtr<WMLIntrinsicEvent> event = WMLIntrinsicEvent::create(document(), attr->value());
92
93        createEventHandlerIfNeeded();
94        eventHandler()->registerIntrinsicEvent(WMLIntrinsicEventOnPick, event);
95    } else
96        WMLFormControlElement::parseMappedAttribute(attr);
97}
98
99void WMLOptionElement::attach()
100{
101    if (parentNode()->renderStyle())
102        setRenderStyle(styleForRenderer());
103    WMLFormControlElement::attach();
104}
105
106void WMLOptionElement::detach()
107{
108    m_style.clear();
109    WMLFormControlElement::detach();
110}
111
112void WMLOptionElement::setRenderStyle(PassRefPtr<RenderStyle> style)
113{
114    m_style = style;
115}
116
117void WMLOptionElement::insertedIntoDocument()
118{
119    WMLSelectElement* select;
120    if (selected() && (select = ownerSelectElement(this)))
121        select->scrollToSelection();
122
123    WMLFormControlElement::insertedIntoDocument();
124}
125
126bool WMLOptionElement::selected() const
127{
128    return m_data.selected();
129}
130
131void WMLOptionElement::setSelectedState(bool selected)
132{
133    if (this->selected() == selected)
134        return;
135
136    OptionElement::setSelectedState(m_data, this, selected);
137
138    if (WMLSelectElement* select = ownerSelectElement(this)) {
139        if (select->multiple() || selected)
140            handleIntrinsicEventIfNeeded();
141    }
142}
143
144String WMLOptionElement::value() const
145{
146    return OptionElement::collectOptionValue(m_data, this);
147}
148
149String WMLOptionElement::text() const
150{
151    return OptionElement::collectOptionLabelOrText(m_data, this);
152}
153
154String WMLOptionElement::textIndentedToRespectGroupLabel() const
155{
156    return OptionElement::collectOptionTextRespectingGroupLabel(m_data, this);
157}
158
159RenderStyle* WMLOptionElement::nonRendererRenderStyle() const
160{
161    return m_style.get();
162}
163
164void WMLOptionElement::handleIntrinsicEventIfNeeded()
165{
166    WMLSelectElement* select = ownerSelectElement(this);
167    if (!select || !select->initialized())
168        return;
169
170    WMLIntrinsicEventHandler* eventHandler = this->eventHandler();
171    if (!eventHandler)
172        return;
173
174    if (eventHandler->hasIntrinsicEvent(WMLIntrinsicEventOnPick))
175        eventHandler->triggerIntrinsicEvent(WMLIntrinsicEventOnPick);
176}
177
178bool WMLOptionElement::disabled() const
179{
180    /* Dummy implementation, as disabled() is pure virtual in OptionElement class */
181    return false;
182}
183
184}
185
186#endif
187