1 /* 2 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http//www.torchmobile.com/) 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 * 19 */ 20 21 #ifndef SelectElement_h 22 #define SelectElement_h 23 24 #include "Event.h" 25 #include <wtf/Vector.h> 26 27 namespace WebCore { 28 29 class Element; 30 class Event; 31 class FormDataList; 32 class HTMLFormElement; 33 class KeyboardEvent; 34 class MappedAttribute; 35 class SelectElementData; 36 class String; 37 38 class SelectElement { 39 public: 40 virtual bool multiple() const = 0; 41 42 virtual int size() const = 0; 43 virtual const Vector<Element*>& listItems() const = 0; 44 45 virtual void listBoxOnChange() = 0; 46 virtual void updateListBoxSelection(bool deselectOtherOptions) = 0; 47 48 virtual void menuListOnChange() = 0; 49 50 virtual int activeSelectionStartListIndex() const = 0; 51 virtual int activeSelectionEndListIndex() const = 0; 52 53 virtual void setActiveSelectionAnchorIndex(int index) = 0; 54 virtual void setActiveSelectionEndIndex(int index) = 0; 55 56 virtual int listToOptionIndex(int listIndex) const = 0; 57 virtual int optionToListIndex(int optionIndex) const = 0; 58 59 virtual int selectedIndex() const = 0; 60 virtual void setSelectedIndex(int index, bool deselect = true) = 0; 61 virtual void setSelectedIndexByUser(int index, bool deselect = true, bool fireOnChangeNow = false) = 0; 62 63 protected: ~SelectElement()64 virtual ~SelectElement() { } 65 66 friend class SelectElementData; 67 68 static void selectAll(SelectElementData&, Element*); 69 static void saveLastSelection(SelectElementData&, Element*); 70 static int nextSelectableListIndex(SelectElementData&, Element*, int startIndex); 71 static int previousSelectableListIndex(SelectElementData&, Element*, int startIndex); 72 static void setActiveSelectionAnchorIndex(SelectElementData&, Element*, int index); 73 static void setActiveSelectionEndIndex(SelectElementData&, int index); 74 static void updateListBoxSelection(SelectElementData&, Element*, bool deselectOtherOptions); 75 static void listBoxOnChange(SelectElementData&, Element*); 76 static void menuListOnChange(SelectElementData&, Element*); 77 static void scrollToSelection(SelectElementData&, Element*); 78 static void recalcStyle(SelectElementData&, Element*); 79 static void setRecalcListItems(SelectElementData&, Element*); 80 static void recalcListItems(SelectElementData&, const Element*, bool updateSelectedStates = true); 81 static int selectedIndex(const SelectElementData&, const Element*); 82 static void setSelectedIndex(SelectElementData&, Element*, int optionIndex, bool deselect = true, bool fireOnChangeNow = false, bool userDrivenChange = true); 83 static int optionToListIndex(const SelectElementData&, const Element*, int optionIndex); 84 static int listToOptionIndex(const SelectElementData&, const Element*, int listIndex); 85 static void dispatchFocusEvent(SelectElementData&, Element*); 86 static void dispatchBlurEvent(SelectElementData&, Element*); 87 static void deselectItems(SelectElementData&, Element*, Element* excludeElement = 0); 88 static bool saveFormControlState(const SelectElementData&, const Element*, String& state); 89 static void restoreFormControlState(SelectElementData&, Element*, const String& state); 90 static void parseMultipleAttribute(SelectElementData&, Element*, MappedAttribute*); 91 static bool appendFormData(SelectElementData&, Element*, FormDataList&); 92 static void reset(SelectElementData&, Element*); 93 static void defaultEventHandler(SelectElementData&, Element*, Event*, HTMLFormElement* = 0); 94 static int lastSelectedListIndex(const SelectElementData&, const Element*); 95 static void typeAheadFind(SelectElementData&, Element*, KeyboardEvent*); 96 static void insertedIntoTree(SelectElementData&, Element*); 97 static void accessKeySetSelectedIndex(SelectElementData&, Element*, int index); 98 static unsigned optionCount(const SelectElementData&, const Element*); 99 100 private: 101 static void menuListDefaultEventHandler(SelectElementData&, Element*, Event*, HTMLFormElement*); 102 static void listBoxDefaultEventHandler(SelectElementData&, Element*, Event*, HTMLFormElement*); 103 }; 104 105 // HTML/WMLSelectElement hold this struct as member variable 106 // and pass it to the static helper functions in SelectElement 107 class SelectElementData { 108 public: 109 SelectElementData(); 110 multiple()111 bool multiple() const { return m_multiple; } setMultiple(bool value)112 void setMultiple(bool value) { m_multiple = value; } 113 size()114 int size() const { return m_size; } setSize(int value)115 void setSize(int value) { m_size = value; } 116 usesMenuList()117 bool usesMenuList() const { return !m_multiple && m_size <= 1; } 118 lastOnChangeIndex()119 int lastOnChangeIndex() const { return m_lastOnChangeIndex; } setLastOnChangeIndex(int value)120 void setLastOnChangeIndex(int value) { m_lastOnChangeIndex = value; } 121 userDrivenChange()122 bool userDrivenChange() const { return m_userDrivenChange; } setUserDrivenChange(bool value)123 void setUserDrivenChange(bool value) { m_userDrivenChange = value; } 124 lastOnChangeSelection()125 Vector<bool>& lastOnChangeSelection() { return m_lastOnChangeSelection; } 126 activeSelectionState()127 bool activeSelectionState() const { return m_activeSelectionState; } setActiveSelectionState(bool value)128 void setActiveSelectionState(bool value) { m_activeSelectionState = value; } 129 activeSelectionAnchorIndex()130 int activeSelectionAnchorIndex() const { return m_activeSelectionAnchorIndex; } setActiveSelectionAnchorIndex(int value)131 void setActiveSelectionAnchorIndex(int value) { m_activeSelectionAnchorIndex = value; } 132 activeSelectionEndIndex()133 int activeSelectionEndIndex() const { return m_activeSelectionEndIndex; } setActiveSelectionEndIndex(int value)134 void setActiveSelectionEndIndex(int value) { m_activeSelectionEndIndex = value; } 135 cachedStateForActiveSelection()136 Vector<bool>& cachedStateForActiveSelection() { return m_cachedStateForActiveSelection; } 137 shouldRecalcListItems()138 bool shouldRecalcListItems() const { return m_recalcListItems; } setShouldRecalcListItems(bool value)139 void setShouldRecalcListItems(bool value) { m_recalcListItems = value; } 140 rawListItems()141 Vector<Element*>& rawListItems() { return m_listItems; } 142 Vector<Element*>& listItems(const Element*); 143 const Vector<Element*>& listItems(const Element*) const; 144 repeatingChar()145 UChar repeatingChar() const { return m_repeatingChar; } setRepeatingChar(const UChar & value)146 void setRepeatingChar(const UChar& value) { m_repeatingChar = value; } 147 lastCharTime()148 DOMTimeStamp lastCharTime() const { return m_lastCharTime; } setLastCharTime(const DOMTimeStamp & value)149 void setLastCharTime(const DOMTimeStamp& value) { m_lastCharTime = value; } 150 typedString()151 String& typedString() { return m_typedString; } setTypedString(const String & value)152 void setTypedString(const String& value) { m_typedString = value; } 153 154 private: 155 void checkListItems(const Element*) const; 156 157 bool m_multiple; 158 int m_size; 159 160 int m_lastOnChangeIndex; 161 Vector<bool> m_lastOnChangeSelection; 162 bool m_userDrivenChange; 163 164 bool m_activeSelectionState; 165 int m_activeSelectionAnchorIndex; 166 int m_activeSelectionEndIndex; 167 Vector<bool> m_cachedStateForActiveSelection; 168 169 bool m_recalcListItems; 170 Vector<Element*> m_listItems; 171 172 // Instance variables for type-ahead find 173 UChar m_repeatingChar; 174 DOMTimeStamp m_lastCharTime; 175 String m_typedString; 176 }; 177 178 SelectElement* toSelectElement(Element*); 179 180 } 181 182 #endif 183