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