1/*
2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
3 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
4 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
5 *           (C) 2000 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010 Apple Inc. All rights reserved.
7 * Copyright (C) 2010 Google Inc. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB.  If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 */
25
26#ifndef HTMLSelectElement_h
27#define HTMLSelectElement_h
28
29#include "CollectionCache.h"
30#include "HTMLFormControlElement.h"
31#include "SelectElement.h"
32
33namespace WebCore {
34
35class HTMLOptionElement;
36class HTMLOptionsCollection;
37
38class HTMLSelectElement : public HTMLFormControlElementWithState, public SelectElement {
39public:
40    static PassRefPtr<HTMLSelectElement> create(const QualifiedName&, Document*, HTMLFormElement*);
41
42    virtual int selectedIndex() const;
43    virtual void setSelectedIndex(int index, bool deselect = true);
44    virtual void setSelectedIndexByUser(int index, bool deselect = true, bool fireOnChangeNow = false, bool allowMultipleSelection = false);
45
46    // For ValidityState
47    bool valueMissing() const;
48
49    unsigned length() const;
50
51    virtual int size() const { return m_data.size(); }
52    virtual bool multiple() const { return m_data.multiple(); }
53
54    void add(HTMLElement*, HTMLElement* beforeElement, ExceptionCode&);
55    void remove(int index);
56    void remove(HTMLOptionElement*);
57
58    String value() const;
59    void setValue(const String&);
60
61    PassRefPtr<HTMLOptionsCollection> options();
62
63    virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
64
65    void setRecalcListItems();
66    void recalcListItemsIfNeeded();
67
68    virtual const Vector<Element*>& listItems() const { return m_data.listItems(this); }
69
70    virtual void accessKeyAction(bool sendToAnyElement);
71    void accessKeySetSelectedIndex(int);
72
73    void setMultiple(bool);
74
75    void setSize(int);
76
77    void setOption(unsigned index, HTMLOptionElement*, ExceptionCode&);
78    void setLength(unsigned, ExceptionCode&);
79
80    Node* namedItem(const AtomicString& name);
81    Node* item(unsigned index);
82
83    CollectionCache* collectionInfo() { m_collectionInfo.checkConsistency(); return &m_collectionInfo; }
84
85    void scrollToSelection();
86
87    void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true);
88
89    virtual void updateValidity() { setNeedsValidityCheck(); }
90
91protected:
92    HTMLSelectElement(const QualifiedName&, Document*, HTMLFormElement*);
93
94private:
95    virtual const AtomicString& formControlType() const;
96
97    virtual bool isKeyboardFocusable(KeyboardEvent*) const;
98    virtual bool isMouseFocusable() const;
99    virtual bool canSelectAll() const;
100    virtual void selectAll();
101
102    virtual void recalcStyle(StyleChange);
103
104    virtual void dispatchFocusEvent();
105    virtual void dispatchBlurEvent();
106
107    virtual bool canStartSelection() const { return false; }
108
109    virtual bool isEnumeratable() const { return true; }
110
111    virtual bool saveFormControlState(String& value) const;
112    virtual void restoreFormControlState(const String&);
113
114    virtual void parseMappedAttribute(Attribute*);
115
116    virtual RenderObject* createRenderer(RenderArena*, RenderStyle *);
117    virtual bool appendFormData(FormDataList&, bool);
118
119    virtual int optionToListIndex(int optionIndex) const;
120    virtual int listToOptionIndex(int listIndex) const;
121
122    virtual void reset();
123
124    virtual void defaultEventHandler(Event*);
125
126    virtual void setActiveSelectionAnchorIndex(int index);
127    virtual void setActiveSelectionEndIndex(int index);
128    virtual void updateListBoxSelection(bool deselectOtherOptions);
129    virtual void listBoxOnChange();
130    virtual void menuListOnChange();
131
132    virtual int activeSelectionStartListIndex() const;
133    virtual int activeSelectionEndListIndex() const;
134
135    void recalcListItems(bool updateSelectedStates = true) const;
136
137    void deselectItems(HTMLOptionElement* excludeElement = 0);
138    void typeAheadFind(KeyboardEvent*);
139    void saveLastSelection();
140
141    virtual void insertedIntoTree(bool);
142
143    virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
144    virtual bool isRequiredFormControl() const;
145
146    bool hasPlaceholderLabelOption() const;
147
148    SelectElementData m_data;
149    CollectionCache m_collectionInfo;
150};
151
152} // namespace
153
154#endif
155