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/html/HTMLContentElement.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"
37 class AutoscrollController;
39 class HTMLOptionElement;
42 class HTMLSelectElement FINAL : public HTMLFormControlElementWithState, public TypeAheadDataSource {
44 static PassRefPtrWillBeRawPtr<HTMLSelectElement> create(Document&);
45 static PassRefPtrWillBeRawPtr<HTMLSelectElement> create(Document&, HTMLFormElement*);
47 int selectedIndex() const;
48 void setSelectedIndex(int);
49 int suggestedIndex() const;
50 void setSuggestedIndex(int);
52 void optionSelectedByUser(int index, bool dispatchChangeEvent, bool allowMultipleSelection = false);
55 virtual String validationMessage() const OVERRIDE;
56 virtual bool valueMissing() const OVERRIDE;
58 virtual void resetImpl() OVERRIDE;
60 unsigned length() const;
62 int size() const { return m_size; }
63 bool multiple() const { return m_multiple; }
65 bool usesMenuList() const;
67 void add(HTMLElement*, HTMLElement* beforeElement, ExceptionState&);
68 void addBeforeOptionAtIndex(HTMLElement*, int beforeIndex, ExceptionState&);
71 void remove(int index);
74 void setValue(const String&, bool sendEvents = false);
75 String suggestedValue() const;
76 void setSuggestedValue(const String&);
78 PassRefPtrWillBeRawPtr<HTMLOptionsCollection> options();
79 PassRefPtrWillBeRawPtr<HTMLCollection> selectedOptions();
81 void optionElementChildrenChanged();
83 void setRecalcListItems();
84 void invalidateSelectedItems();
85 void updateListItemSelectedStates();
87 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems() const;
89 virtual void accessKeyAction(bool sendMouseEvents) OVERRIDE;
90 void accessKeySetSelectedIndex(int);
92 void setMultiple(bool);
96 void setOption(unsigned index, HTMLOptionElement*, ExceptionState&);
97 void setLength(unsigned, ExceptionState&);
99 Element* namedItem(const AtomicString& name);
100 Element* item(unsigned index);
102 void scrollToSelection();
103 void scrollTo(int listIndex);
105 void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true);
107 bool canSelectAll() const;
109 int listToOptionIndex(int listIndex) const;
110 void listBoxOnChange();
111 int optionToListIndex(int optionIndex) const;
112 int activeSelectionStartListIndex() const;
113 int activeSelectionEndListIndex() const;
114 void setActiveSelectionAnchorIndex(int);
115 void setActiveSelectionEndIndex(int);
116 void updateListBoxSelection(bool deselectOtherOptions);
118 // For use in the implementation of HTMLOptionElement.
119 void optionSelectionStateChanged(HTMLOptionElement*, bool optionIsSelected);
120 void optionRemoved(const HTMLOptionElement&);
121 bool anonymousIndexedSetter(unsigned, PassRefPtrWillBeRawPtr<HTMLOptionElement>, ExceptionState&);
123 void updateListOnRenderer();
125 HTMLOptionElement* spatialNavigationFocusedOption();
126 void handleMouseRelease();
128 virtual void trace(Visitor*) OVERRIDE;
131 HTMLSelectElement(Document&, HTMLFormElement*);
134 virtual const AtomicString& formControlType() const OVERRIDE;
136 virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE;
138 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE;
139 virtual void dispatchBlurEvent(Element* newFocusedElemnet) OVERRIDE;
141 virtual bool canStartSelection() const OVERRIDE { return false; }
143 virtual bool isEnumeratable() const OVERRIDE { return true; }
144 virtual bool isInteractiveContent() const OVERRIDE;
145 virtual bool supportsAutofocus() const OVERRIDE;
146 virtual bool supportLabels() const OVERRIDE { return true; }
148 virtual FormControlState saveFormControlState() const OVERRIDE;
149 virtual void restoreFormControlState(const FormControlState&) OVERRIDE;
151 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
152 virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
154 virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE;
155 virtual bool appendFormData(FormDataList&, bool) OVERRIDE;
156 virtual void didAddUserAgentShadowRoot(ShadowRoot&) OVERRIDE;
158 virtual void defaultEventHandler(Event*) OVERRIDE;
160 void dispatchInputAndChangeEventForMenuList(bool requiresUserGesture = true);
162 void recalcListItems(bool updateSelectedStates = true) const;
164 void typeAheadFind(KeyboardEvent*);
165 void saveLastSelection();
167 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
169 virtual bool isOptionalFormControl() const OVERRIDE { return !isRequiredFormControl(); }
170 virtual bool isRequiredFormControl() const OVERRIDE;
172 bool hasPlaceholderLabelOption() const;
174 enum SelectOptionFlag {
175 DeselectOtherOptions = 1 << 0,
176 DispatchInputAndChangeEvent = 1 << 1,
179 typedef unsigned SelectOptionFlags;
180 void selectOption(int optionIndex, SelectOptionFlags = 0);
181 void deselectItemsWithoutValidation(HTMLElement* elementToExclude = 0);
182 void parseMultipleAttribute(const AtomicString&);
183 int lastSelectedListIndex() const;
184 void updateSelectedState(int listIndex, bool multi, bool shift);
185 void menuListDefaultEventHandler(Event*);
186 void handlePopupOpenKeyboardEvent(Event*);
187 bool shouldOpenPopupForKeyDownEvent(KeyboardEvent*);
188 bool shouldOpenPopupForKeyPressEvent(KeyboardEvent*);
189 void listBoxDefaultEventHandler(Event*);
190 void setOptionsChangedOnRenderer();
191 size_t searchOptionsForValue(const String&, size_t listIndexStart, size_t listIndexEnd) const;
197 int nextValidIndex(int listIndex, SkipDirection, int skip) const;
198 int nextSelectableListIndex(int startIndex) const;
199 int previousSelectableListIndex(int startIndex) const;
200 int firstSelectableListIndex() const;
201 int lastSelectableListIndex() const;
202 int nextSelectableListIndexPageAway(int startIndex, SkipDirection) const;
203 int listIndexForEventTargetOption(const Event&);
204 int listIndexForOption(const HTMLOptionElement&);
205 AutoscrollController* autoscrollController() const;
207 virtual void childrenChanged(const ChildrenChange&) OVERRIDE;
208 virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }
209 virtual void finishParsingChildren() OVERRIDE;
211 // TypeAheadDataSource functions.
212 virtual int indexOfSelectedOption() const OVERRIDE;
213 virtual int optionCount() const OVERRIDE;
214 virtual String optionAtIndex(int index) const OVERRIDE;
216 // m_listItems contains HTMLOptionElement, HTMLOptGroupElement, and HTMLHRElement objects.
217 mutable WillBeHeapVector<RawPtrWillBeMember<HTMLElement> > m_listItems;
218 Vector<bool> m_lastOnChangeSelection;
219 Vector<bool> m_cachedStateForActiveSelection;
220 TypeAhead m_typeAhead;
222 int m_lastOnChangeIndex;
223 int m_activeSelectionAnchorIndex;
224 int m_activeSelectionEndIndex;
225 bool m_isProcessingUserDrivenChange;
227 bool m_activeSelectionState;
228 mutable bool m_shouldRecalcListItems;
229 int m_suggestedIndex;