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.
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.
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.
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.
26 #ifndef HTMLSelectElement_h
27 #define HTMLSelectElement_h
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"
38 class HTMLOptionElement;
40 class HTMLSelectElement FINAL : public HTMLFormControlElementWithState, public TypeAheadDataSource {
42 static PassRefPtr<HTMLSelectElement> create(Document&);
43 static PassRefPtr<HTMLSelectElement> create(Document&, HTMLFormElement*);
45 int selectedIndex() const;
46 void setSelectedIndex(int);
48 void optionSelectedByUser(int index, bool dispatchChangeEvent, bool allowMultipleSelection = false);
51 virtual String validationMessage() const OVERRIDE;
52 virtual bool valueMissing() const OVERRIDE;
54 virtual void resetImpl() OVERRIDE;
56 unsigned length() const;
58 int size() const { return m_size; }
59 bool multiple() const { return m_multiple; }
61 bool usesMenuList() const;
63 void add(HTMLElement*, HTMLElement* beforeElement, ExceptionState&);
66 void remove(int index);
67 void remove(HTMLOptionElement*);
70 void setValue(const String&);
72 PassRefPtr<HTMLOptionsCollection> options();
73 PassRefPtr<HTMLCollection> selectedOptions();
75 void optionElementChildrenChanged();
77 void setRecalcListItems();
78 void invalidateSelectedItems();
79 void updateListItemSelectedStates();
81 const Vector<HTMLElement*>& listItems() const;
83 virtual void accessKeyAction(bool sendMouseEvents) OVERRIDE;
84 void accessKeySetSelectedIndex(int);
86 void setMultiple(bool);
90 void setOption(unsigned index, HTMLOptionElement*, ExceptionState&);
91 void setLength(unsigned, ExceptionState&);
93 Element* namedItem(const AtomicString& name);
94 Element* item(unsigned index);
96 void scrollToSelection();
98 void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true);
100 bool canSelectAll() const;
102 int listToOptionIndex(int listIndex) const;
103 void listBoxOnChange();
104 int optionToListIndex(int optionIndex) const;
105 int activeSelectionStartListIndex() const;
106 int activeSelectionEndListIndex() const;
107 void setActiveSelectionAnchorIndex(int);
108 void setActiveSelectionEndIndex(int);
109 void updateListBoxSelection(bool deselectOtherOptions);
111 // For use in the implementation of HTMLOptionElement.
112 void optionSelectionStateChanged(HTMLOptionElement*, bool optionIsSelected);
113 bool anonymousIndexedSetter(unsigned, PassRefPtr<HTMLOptionElement>, ExceptionState&);
116 HTMLSelectElement(Document&, HTMLFormElement*);
119 virtual const AtomicString& formControlType() const OVERRIDE;
121 virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE;
123 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE;
124 virtual void dispatchBlurEvent(Element* newFocusedElemnet) OVERRIDE;
126 virtual bool canStartSelection() const OVERRIDE { return false; }
128 virtual bool isEnumeratable() const OVERRIDE { return true; }
129 virtual bool isInteractiveContent() const OVERRIDE;
130 virtual bool supportsAutofocus() const OVERRIDE;
131 virtual bool supportLabels() const OVERRIDE { return true; }
133 virtual FormControlState saveFormControlState() const OVERRIDE;
134 virtual void restoreFormControlState(const FormControlState&) OVERRIDE;
136 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
137 virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
139 virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE;
140 virtual bool appendFormData(FormDataList&, bool) OVERRIDE;
142 virtual void defaultEventHandler(Event*) OVERRIDE;
144 void dispatchChangeEventForMenuList();
146 void recalcListItems(bool updateSelectedStates = true) const;
148 void typeAheadFind(KeyboardEvent*);
149 void saveLastSelection();
151 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
153 virtual bool isOptionalFormControl() const OVERRIDE { return !isRequiredFormControl(); }
154 virtual bool isRequiredFormControl() const OVERRIDE;
156 bool hasPlaceholderLabelOption() const;
158 enum SelectOptionFlag {
159 DeselectOtherOptions = 1 << 0,
160 DispatchChangeEvent = 1 << 1,
163 typedef unsigned SelectOptionFlags;
164 void selectOption(int optionIndex, SelectOptionFlags = 0);
165 void deselectItemsWithoutValidation(HTMLElement* elementToExclude = 0);
166 void parseMultipleAttribute(const AtomicString&);
167 int lastSelectedListIndex() const;
168 void updateSelectedState(int listIndex, bool multi, bool shift);
169 void menuListDefaultEventHandler(Event*);
170 bool platformHandleKeydownEvent(KeyboardEvent*);
171 void listBoxDefaultEventHandler(Event*);
172 void setOptionsChangedOnRenderer();
173 size_t searchOptionsForValue(const String&, size_t listIndexStart, size_t listIndexEnd) const;
179 int nextValidIndex(int listIndex, SkipDirection, int skip) const;
180 int nextSelectableListIndex(int startIndex) const;
181 int previousSelectableListIndex(int startIndex) const;
182 int firstSelectableListIndex() const;
183 int lastSelectableListIndex() const;
184 int nextSelectableListIndexPageAway(int startIndex, SkipDirection) const;
186 virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0) OVERRIDE;
187 virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }
188 virtual void finishParsingChildren() OVERRIDE;
190 // TypeAheadDataSource functions.
191 virtual int indexOfSelectedOption() const OVERRIDE;
192 virtual int optionCount() const OVERRIDE;
193 virtual String optionAtIndex(int index) const OVERRIDE;
195 // m_listItems contains HTMLOptionElement, HTMLOptGroupElement, and HTMLHRElement objects.
196 mutable Vector<HTMLElement*> m_listItems;
197 Vector<bool> m_lastOnChangeSelection;
198 Vector<bool> m_cachedStateForActiveSelection;
199 TypeAhead m_typeAhead;
201 int m_lastOnChangeIndex;
202 int m_activeSelectionAnchorIndex;
203 int m_activeSelectionEndIndex;
204 bool m_isProcessingUserDrivenChange;
206 bool m_activeSelectionState;
207 mutable bool m_shouldRecalcListItems;
210 DEFINE_NODE_TYPE_CASTS(HTMLSelectElement, hasTagName(HTMLNames::selectTag));