Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLSelectElement.h
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, 2011 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 "core/events/Event.h"
30 #include "core/html/HTMLFormControlElementWithState.h"
31 #include "core/html/HTMLOptionsCollection.h"
32 #include "core/html/forms/TypeAhead.h"
33 #include "wtf/Vector.h"
34
35 namespace WebCore {
36
37 class ExceptionState;
38 class HTMLOptionElement;
39
40 class HTMLSelectElement FINAL : public HTMLFormControlElementWithState, public TypeAheadDataSource {
41 public:
42     static PassRefPtrWillBeRawPtr<HTMLSelectElement> create(Document&);
43     static PassRefPtrWillBeRawPtr<HTMLSelectElement> create(Document&, HTMLFormElement*);
44
45     int selectedIndex() const;
46     void setSelectedIndex(int);
47     int suggestedIndex() const;
48     void setSuggestedIndex(int);
49
50     void optionSelectedByUser(int index, bool dispatchChangeEvent, bool allowMultipleSelection = false);
51
52     // For ValidityState
53     virtual String validationMessage() const OVERRIDE;
54     virtual bool valueMissing() const OVERRIDE;
55
56     virtual void resetImpl() OVERRIDE;
57
58     unsigned length() const;
59
60     int size() const { return m_size; }
61     bool multiple() const { return m_multiple; }
62
63     bool usesMenuList() const;
64
65     void add(HTMLElement*, HTMLElement* beforeElement, ExceptionState&);
66     void addBeforeOptionAtIndex(HTMLElement*, int beforeIndex, ExceptionState&);
67
68     using Node::remove;
69     void remove(int index);
70
71     String value() const;
72     void setValue(const String&, bool sendEvents = false);
73     String suggestedValue() const;
74     void setSuggestedValue(const String&);
75
76     PassRefPtr<HTMLOptionsCollection> options();
77     PassRefPtr<HTMLCollection> selectedOptions();
78
79     void optionElementChildrenChanged();
80
81     void setRecalcListItems();
82     void invalidateSelectedItems();
83     void updateListItemSelectedStates();
84
85     const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems() const;
86
87     virtual void accessKeyAction(bool sendMouseEvents) OVERRIDE;
88     void accessKeySetSelectedIndex(int);
89
90     void setMultiple(bool);
91
92     void setSize(int);
93
94     void setOption(unsigned index, HTMLOptionElement*, ExceptionState&);
95     void setLength(unsigned, ExceptionState&);
96
97     Element* namedItem(const AtomicString& name);
98     Element* item(unsigned index);
99
100     void scrollToSelection();
101
102     void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true);
103
104     bool canSelectAll() const;
105     void selectAll();
106     int listToOptionIndex(int listIndex) const;
107     void listBoxOnChange();
108     int optionToListIndex(int optionIndex) const;
109     int activeSelectionStartListIndex() const;
110     int activeSelectionEndListIndex() const;
111     void setActiveSelectionAnchorIndex(int);
112     void setActiveSelectionEndIndex(int);
113     void updateListBoxSelection(bool deselectOtherOptions);
114
115     // For use in the implementation of HTMLOptionElement.
116     void optionSelectionStateChanged(HTMLOptionElement*, bool optionIsSelected);
117     bool anonymousIndexedSetter(unsigned, PassRefPtrWillBeRawPtr<HTMLOptionElement>, ExceptionState&);
118
119     void updateListOnRenderer();
120
121     virtual void trace(Visitor*) OVERRIDE;
122
123 protected:
124     HTMLSelectElement(Document&, HTMLFormElement*);
125
126 private:
127     virtual const AtomicString& formControlType() const OVERRIDE;
128
129     virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE;
130
131     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE;
132     virtual void dispatchBlurEvent(Element* newFocusedElemnet) OVERRIDE;
133
134     virtual bool canStartSelection() const OVERRIDE { return false; }
135
136     virtual bool isEnumeratable() const OVERRIDE { return true; }
137     virtual bool isInteractiveContent() const OVERRIDE;
138     virtual bool supportsAutofocus() const OVERRIDE;
139     virtual bool supportLabels() const OVERRIDE { return true; }
140
141     virtual FormControlState saveFormControlState() const OVERRIDE;
142     virtual void restoreFormControlState(const FormControlState&) OVERRIDE;
143
144     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
145     virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
146
147     virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE;
148     virtual bool appendFormData(FormDataList&, bool) OVERRIDE;
149
150     virtual void defaultEventHandler(Event*) OVERRIDE;
151
152     void dispatchInputAndChangeEventForMenuList(bool requiresUserGesture = true);
153
154     void recalcListItems(bool updateSelectedStates = true) const;
155
156     void typeAheadFind(KeyboardEvent*);
157     void saveLastSelection();
158
159     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
160
161     virtual bool isOptionalFormControl() const OVERRIDE { return !isRequiredFormControl(); }
162     virtual bool isRequiredFormControl() const OVERRIDE;
163
164     bool hasPlaceholderLabelOption() const;
165
166     enum SelectOptionFlag {
167         DeselectOtherOptions = 1 << 0,
168         DispatchInputAndChangeEvent = 1 << 1,
169         UserDriven = 1 << 2,
170     };
171     typedef unsigned SelectOptionFlags;
172     void selectOption(int optionIndex, SelectOptionFlags = 0);
173     void deselectItemsWithoutValidation(HTMLElement* elementToExclude = 0);
174     void parseMultipleAttribute(const AtomicString&);
175     int lastSelectedListIndex() const;
176     void updateSelectedState(int listIndex, bool multi, bool shift);
177     void menuListDefaultEventHandler(Event*);
178     bool platformHandleKeydownEvent(KeyboardEvent*);
179     void listBoxDefaultEventHandler(Event*);
180     void setOptionsChangedOnRenderer();
181     size_t searchOptionsForValue(const String&, size_t listIndexStart, size_t listIndexEnd) const;
182
183     enum SkipDirection {
184         SkipBackwards = -1,
185         SkipForwards = 1
186     };
187     int nextValidIndex(int listIndex, SkipDirection, int skip) const;
188     int nextSelectableListIndex(int startIndex) const;
189     int previousSelectableListIndex(int startIndex) const;
190     int firstSelectableListIndex() const;
191     int lastSelectableListIndex() const;
192     int nextSelectableListIndexPageAway(int startIndex, SkipDirection) const;
193
194     virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0) OVERRIDE;
195     virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }
196     virtual void finishParsingChildren() OVERRIDE;
197
198     // TypeAheadDataSource functions.
199     virtual int indexOfSelectedOption() const OVERRIDE;
200     virtual int optionCount() const OVERRIDE;
201     virtual String optionAtIndex(int index) const OVERRIDE;
202
203     // m_listItems contains HTMLOptionElement, HTMLOptGroupElement, and HTMLHRElement objects.
204     mutable WillBeHeapVector<RawPtrWillBeMember<HTMLElement> > m_listItems;
205     Vector<bool> m_lastOnChangeSelection;
206     Vector<bool> m_cachedStateForActiveSelection;
207     TypeAhead m_typeAhead;
208     int m_size;
209     int m_lastOnChangeIndex;
210     int m_activeSelectionAnchorIndex;
211     int m_activeSelectionEndIndex;
212     bool m_isProcessingUserDrivenChange;
213     bool m_multiple;
214     bool m_activeSelectionState;
215     mutable bool m_shouldRecalcListItems;
216     int m_suggestedIndex;
217 };
218
219 } // namespace
220
221 #endif