| 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 | #pragma once |
| 27 | |
| 28 | #include "HTMLFormControlElementWithState.h" |
| 29 | #include "TypeAhead.h" |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | class HTMLOptionsCollection; |
| 34 | |
| 35 | class HTMLSelectElement : public HTMLFormControlElementWithState, private TypeAheadDataSource { |
| 36 | WTF_MAKE_ISO_ALLOCATED(HTMLSelectElement); |
| 37 | public: |
| 38 | static Ref<HTMLSelectElement> create(const QualifiedName&, Document&, HTMLFormElement*); |
| 39 | |
| 40 | WEBCORE_EXPORT int selectedIndex() const; |
| 41 | WEBCORE_EXPORT void setSelectedIndex(int); |
| 42 | |
| 43 | WEBCORE_EXPORT void optionSelectedByUser(int index, bool dispatchChangeEvent, bool allowMultipleSelection = false); |
| 44 | |
| 45 | String validationMessage() const final; |
| 46 | bool valueMissing() const final; |
| 47 | |
| 48 | WEBCORE_EXPORT unsigned length() const; |
| 49 | |
| 50 | unsigned size() const { return m_size; } |
| 51 | bool multiple() const { return m_multiple; } |
| 52 | |
| 53 | bool () const; |
| 54 | |
| 55 | using OptionOrOptGroupElement = Variant<RefPtr<HTMLOptionElement>, RefPtr<HTMLOptGroupElement>>; |
| 56 | using HTMLElementOrInt = Variant<RefPtr<HTMLElement>, int>; |
| 57 | WEBCORE_EXPORT ExceptionOr<void> add(const OptionOrOptGroupElement&, const Optional<HTMLElementOrInt>& before); |
| 58 | |
| 59 | using Node::remove; |
| 60 | WEBCORE_EXPORT void remove(int); |
| 61 | |
| 62 | WEBCORE_EXPORT String value() const; |
| 63 | WEBCORE_EXPORT void setValue(const String&); |
| 64 | |
| 65 | WEBCORE_EXPORT Ref<HTMLOptionsCollection> options(); |
| 66 | Ref<HTMLCollection> selectedOptions(); |
| 67 | |
| 68 | void optionElementChildrenChanged(); |
| 69 | |
| 70 | void setRecalcListItems(); |
| 71 | void invalidateSelectedItems(); |
| 72 | void updateListItemSelectedStates(); |
| 73 | |
| 74 | WEBCORE_EXPORT const Vector<HTMLElement*>& listItems() const; |
| 75 | |
| 76 | void accessKeyAction(bool sendMouseEvents) final; |
| 77 | void accessKeySetSelectedIndex(int); |
| 78 | |
| 79 | WEBCORE_EXPORT void setMultiple(bool); |
| 80 | |
| 81 | WEBCORE_EXPORT void setSize(unsigned); |
| 82 | |
| 83 | // Called by the bindings for the unnamed index-setter. |
| 84 | ExceptionOr<void> setItem(unsigned index, HTMLOptionElement*); |
| 85 | ExceptionOr<void> setLength(unsigned); |
| 86 | |
| 87 | WEBCORE_EXPORT HTMLOptionElement* namedItem(const AtomicString& name); |
| 88 | WEBCORE_EXPORT HTMLOptionElement* item(unsigned index); |
| 89 | |
| 90 | void scrollToSelection(); |
| 91 | |
| 92 | void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true); |
| 93 | |
| 94 | bool canSelectAll() const; |
| 95 | void selectAll(); |
| 96 | int listToOptionIndex(int listIndex) const; |
| 97 | void listBoxOnChange(); |
| 98 | int optionToListIndex(int optionIndex) const; |
| 99 | int activeSelectionStartListIndex() const; |
| 100 | int activeSelectionEndListIndex() const; |
| 101 | void setActiveSelectionAnchorIndex(int); |
| 102 | void setActiveSelectionEndIndex(int); |
| 103 | void updateListBoxSelection(bool deselectOtherOptions); |
| 104 | |
| 105 | // For use in the implementation of HTMLOptionElement. |
| 106 | void optionSelectionStateChanged(HTMLOptionElement&, bool optionIsSelected); |
| 107 | bool allowsNonContiguousSelection() const { return m_allowsNonContiguousSelection; }; |
| 108 | |
| 109 | protected: |
| 110 | HTMLSelectElement(const QualifiedName&, Document&, HTMLFormElement*); |
| 111 | |
| 112 | private: |
| 113 | const AtomicString& formControlType() const final; |
| 114 | |
| 115 | bool isKeyboardFocusable(KeyboardEvent*) const final; |
| 116 | bool isMouseFocusable() const final; |
| 117 | |
| 118 | void dispatchFocusEvent(RefPtr<Element>&& oldFocusedElement, FocusDirection) final; |
| 119 | void dispatchBlurEvent(RefPtr<Element>&& newFocusedElement) final; |
| 120 | |
| 121 | bool canStartSelection() const final { return false; } |
| 122 | |
| 123 | bool isEnumeratable() const final { return true; } |
| 124 | bool supportLabels() const final { return true; } |
| 125 | |
| 126 | FormControlState saveFormControlState() const final; |
| 127 | void restoreFormControlState(const FormControlState&) final; |
| 128 | |
| 129 | void parseAttribute(const QualifiedName&, const AtomicString&) final; |
| 130 | bool isPresentationAttribute(const QualifiedName&) const final; |
| 131 | |
| 132 | bool childShouldCreateRenderer(const Node&) const final; |
| 133 | RenderPtr<RenderElement> createElementRenderer(RenderStyle&&, const RenderTreePosition&) final; |
| 134 | bool appendFormData(DOMFormData&, bool) final; |
| 135 | |
| 136 | void reset() final; |
| 137 | |
| 138 | void defaultEventHandler(Event&) final; |
| 139 | bool willRespondToMouseClickEvents() final; |
| 140 | |
| 141 | void (); |
| 142 | |
| 143 | void didRecalcStyle(Style::Change) final; |
| 144 | |
| 145 | void recalcListItems(bool updateSelectedStates = true) const; |
| 146 | |
| 147 | void deselectItems(HTMLOptionElement* excludeElement = nullptr); |
| 148 | void typeAheadFind(KeyboardEvent&); |
| 149 | void saveLastSelection(); |
| 150 | |
| 151 | InsertedIntoAncestorResult insertedIntoAncestor(InsertionType, ContainerNode&) final; |
| 152 | |
| 153 | bool isOptionalFormControl() const final { return !isRequiredFormControl(); } |
| 154 | bool isRequiredFormControl() const final; |
| 155 | |
| 156 | bool hasPlaceholderLabelOption() const; |
| 157 | |
| 158 | enum SelectOptionFlag { |
| 159 | DeselectOtherOptions = 1 << 0, |
| 160 | DispatchChangeEvent = 1 << 1, |
| 161 | UserDriven = 1 << 2, |
| 162 | }; |
| 163 | typedef unsigned SelectOptionFlags; |
| 164 | void selectOption(int optionIndex, SelectOptionFlags = 0); |
| 165 | void deselectItemsWithoutValidation(HTMLElement* elementToExclude = nullptr); |
| 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; |
| 174 | |
| 175 | enum SkipDirection { SkipBackwards = -1, SkipForwards = 1 }; |
| 176 | int nextValidIndex(int listIndex, SkipDirection, int skip) const; |
| 177 | int nextSelectableListIndex(int startIndex) const; |
| 178 | int previousSelectableListIndex(int startIndex) const; |
| 179 | int firstSelectableListIndex() const; |
| 180 | int lastSelectableListIndex() const; |
| 181 | int nextSelectableListIndexPageAway(int startIndex, SkipDirection) const; |
| 182 | |
| 183 | void childrenChanged(const ChildChange&) final; |
| 184 | |
| 185 | // TypeAheadDataSource functions. |
| 186 | int indexOfSelectedOption() const final; |
| 187 | int optionCount() const final; |
| 188 | String optionAtIndex(int index) const final; |
| 189 | |
| 190 | |
| 191 | // m_listItems contains HTMLOptionElement, HTMLOptGroupElement, and HTMLHRElement objects. |
| 192 | mutable Vector<HTMLElement*> m_listItems; |
| 193 | Vector<bool> m_lastOnChangeSelection; |
| 194 | Vector<bool> m_cachedStateForActiveSelection; |
| 195 | TypeAhead m_typeAhead; |
| 196 | unsigned m_size; |
| 197 | int m_lastOnChangeIndex; |
| 198 | int m_activeSelectionAnchorIndex; |
| 199 | int m_activeSelectionEndIndex; |
| 200 | bool m_isProcessingUserDrivenChange; |
| 201 | bool m_multiple; |
| 202 | bool m_activeSelectionState; |
| 203 | bool m_allowsNonContiguousSelection; |
| 204 | mutable bool m_shouldRecalcListItems; |
| 205 | }; |
| 206 | |
| 207 | } // namespace |
| 208 | |