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 PassRefPtrWillBeRawPtr<HTMLSelectElement> create(Document&);
43 static PassRefPtrWillBeRawPtr<HTMLSelectElement> create(Document&, HTMLFormElement*);
45 int selectedIndex() const;
46 void setSelectedIndex(int);
47 int suggestedIndex() const;
48 void setSuggestedIndex(int);
50 void optionSelectedByUser(int index, bool dispatchChangeEvent, bool allowMultipleSelection = false);
53 virtual String validationMessage() const OVERRIDE;
54 virtual bool valueMissing() const OVERRIDE;
56 virtual void resetImpl() OVERRIDE;
58 unsigned length() const;
60 int size() const { return m_size; }
61 bool multiple() const { return m_multiple; }
63 bool usesMenuList() const;
65 void add(HTMLElement*, HTMLElement* beforeElement, ExceptionState&);
66 void addBeforeOptionAtIndex(HTMLElement*, int beforeIndex, ExceptionState&);
69 void remove(int index);
72 void setValue(const String&, bool sendEvents = false);
73 String suggestedValue() const;
74 void setSuggestedValue(const String&);
76 PassRefPtr<HTMLOptionsCollection> options();
77 PassRefPtr<HTMLCollection> selectedOptions();
79 void optionElementChildrenChanged();
81 void setRecalcListItems();
82 void invalidateSelectedItems();
83 void updateListItemSelectedStates();
85 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems() const;
87 virtual void accessKeyAction(bool sendMouseEvents) OVERRIDE;
88 void accessKeySetSelectedIndex(int);
90 void setMultiple(bool);
94 void setOption(unsigned index, HTMLOptionElement*, ExceptionState&);
95 void setLength(unsigned, ExceptionState&);
97 Element* namedItem(const AtomicString& name);
98 Element* item(unsigned index);
100 void scrollToSelection();
102 void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true);
104 bool canSelectAll() const;
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);
115 // For use in the implementation of HTMLOptionElement.
116 void optionSelectionStateChanged(HTMLOptionElement*, bool optionIsSelected);
117 bool anonymousIndexedSetter(unsigned, PassRefPtrWillBeRawPtr<HTMLOptionElement>, ExceptionState&);
119 void updateListOnRenderer();
121 virtual void trace(Visitor*) OVERRIDE;
124 HTMLSelectElement(Document&, HTMLFormElement*);
127 virtual const AtomicString& formControlType() const OVERRIDE;
129 virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE;
131 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE;
132 virtual void dispatchBlurEvent(Element* newFocusedElemnet) OVERRIDE;
134 virtual bool canStartSelection() const OVERRIDE { return false; }
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; }
141 virtual FormControlState saveFormControlState() const OVERRIDE;
142 virtual void restoreFormControlState(const FormControlState&) OVERRIDE;
144 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
145 virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
147 virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE;
148 virtual bool appendFormData(FormDataList&, bool) OVERRIDE;
150 virtual void defaultEventHandler(Event*) OVERRIDE;
152 void dispatchInputAndChangeEventForMenuList(bool requiresUserGesture = true);
154 void recalcListItems(bool updateSelectedStates = true) const;
156 void typeAheadFind(KeyboardEvent*);
157 void saveLastSelection();
159 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
161 virtual bool isOptionalFormControl() const OVERRIDE { return !isRequiredFormControl(); }
162 virtual bool isRequiredFormControl() const OVERRIDE;
164 bool hasPlaceholderLabelOption() const;
166 enum SelectOptionFlag {
167 DeselectOtherOptions = 1 << 0,
168 DispatchInputAndChangeEvent = 1 << 1,
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;
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;
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;
198 // TypeAheadDataSource functions.
199 virtual int indexOfSelectedOption() const OVERRIDE;
200 virtual int optionCount() const OVERRIDE;
201 virtual String optionAtIndex(int index) const OVERRIDE;
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;
209 int m_lastOnChangeIndex;
210 int m_activeSelectionAnchorIndex;
211 int m_activeSelectionEndIndex;
212 bool m_isProcessingUserDrivenChange;
214 bool m_activeSelectionState;
215 mutable bool m_shouldRecalcListItems;
216 int m_suggestedIndex;