OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011, Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #ifndef PopupListBox_h | |
32 #define PopupListBox_h | |
33 | |
34 #include "core/dom/Element.h" | |
35 #include "platform/Widget.h" | |
36 #include "platform/scroll/ScrollTypes.h" | |
37 #include "platform/scroll/ScrollableArea.h" | |
38 #include "platform/text/TextDirection.h" | |
39 #include "wtf/text/WTFString.h" | |
40 | |
41 namespace blink { | |
42 | |
43 class Font; | |
44 class GraphicsContext; | |
45 class IntRect; | |
46 class PlatformKeyboardEvent; | |
47 class PlatformMouseEvent; | |
48 class PlatformGestureEvent; | |
49 class PlatformTouchEvent; | |
50 class PlatformWheelEvent; | |
51 class PopupContainer; | |
52 class PopupMenuClient; | |
53 typedef unsigned long long TimeStamp; | |
54 | |
55 class PopupContent { | |
56 public: | |
57 virtual void layout() = 0; | |
58 virtual void setMaxHeight(int) = 0; | |
59 virtual void setMaxWidthAndLayout(int) = 0; | |
60 virtual int popupContentHeight() const = 0; | |
61 virtual ~PopupContent() { }; | |
62 }; | |
63 | |
64 // A container for the data for each menu item (e.g. represented by <option> | |
65 // or <optgroup> in a <select> widget) and is used by PopupListBox. | |
66 struct PopupItem { | |
67 enum Type { | |
68 TypeOption, | |
69 TypeGroup, | |
70 TypeSeparator | |
71 }; | |
72 | |
73 PopupItem(const String& label, Type type) | |
74 : label(label) | |
75 , type(type) | |
76 , yOffset(0) | |
77 { | |
78 } | |
79 String label; | |
80 Type type; | |
81 int yOffset; // y offset of this item, relative to the top of the popup. | |
82 TextDirection textDirection; | |
83 bool hasTextDirectionOverride; | |
84 bool enabled; | |
85 bool displayNone; | |
86 }; | |
87 | |
88 // This class manages the scrollable content inside a <select> popup. | |
89 class PopupListBox final : public Widget, public ScrollableArea, public PopupCon
tent { | |
90 public: | |
91 static PassRefPtrWillBeRawPtr<PopupListBox> create(PopupMenuClient* client,
bool deviceSupportsTouch, PopupContainer* container) | |
92 { | |
93 return adoptRefWillBeNoop(new PopupListBox(client, deviceSupportsTouch,
container)); | |
94 } | |
95 | |
96 // Widget | |
97 virtual void invalidateRect(const IntRect&) override; | |
98 virtual void paint(GraphicsContext*, const IntRect&) override; | |
99 virtual HostWindow* hostWindow() const override; | |
100 virtual void setFrameRect(const IntRect&) override; | |
101 virtual IntPoint convertChildToSelf(const Widget* child, const IntPoint&) co
nst override; | |
102 virtual IntPoint convertSelfToChild(const Widget* child, const IntPoint&) co
nst override; | |
103 | |
104 // ScrollableArea | |
105 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) override; | |
106 virtual bool isActive() const override; | |
107 virtual bool scrollbarsCanBeActive() const override; | |
108 virtual IntRect scrollableAreaBoundingBox() const override; | |
109 virtual bool shouldPlaceVerticalScrollbarOnLeft() const override; | |
110 virtual int scrollSize(ScrollbarOrientation) const override; | |
111 virtual void setScrollOffset(const IntPoint&) override; | |
112 virtual bool isScrollCornerVisible() const override { return false; } | |
113 virtual bool userInputScrollable(ScrollbarOrientation orientation) const ove
rride { return orientation == VerticalScrollbar; } | |
114 virtual Scrollbar* verticalScrollbar() const override { return m_verticalScr
ollbar.get(); } | |
115 virtual IntRect visibleContentRect(IncludeScrollbarsInRect = ExcludeScrollba
rs) const override; | |
116 virtual IntSize contentsSize() const override { return m_contentsSize; } | |
117 virtual IntPoint scrollPosition() const override { return visibleContentRect
().location(); } | |
118 virtual IntPoint maximumScrollPosition() const override; // The maximum posi
tion we can be scrolled to. | |
119 virtual IntPoint minimumScrollPosition() const override; // The minimum posi
tion we can be scrolled to. | |
120 virtual IntRect scrollCornerRect() const override { return IntRect(); } | |
121 | |
122 // PopupListBox methods | |
123 | |
124 bool handleMouseDownEvent(const PlatformMouseEvent&); | |
125 bool handleMouseMoveEvent(const PlatformMouseEvent&); | |
126 bool handleMouseReleaseEvent(const PlatformMouseEvent&); | |
127 bool handleWheelEvent(const PlatformWheelEvent&); | |
128 bool handleKeyEvent(const PlatformKeyboardEvent&); | |
129 bool handleTouchEvent(const PlatformTouchEvent&); | |
130 bool handleGestureEvent(const PlatformGestureEvent&); | |
131 | |
132 // Closes the popup | |
133 void abandon(); | |
134 | |
135 // Updates our internal list to match the client. | |
136 void updateFromElement(); | |
137 | |
138 // Frees any allocated resources used in a particular popup session. | |
139 void clear(); | |
140 | |
141 // Sets the index of the option that is displayed in the <select> widget in
the page | |
142 void setOriginalIndex(int); | |
143 | |
144 // Gets the index of the item that the user is currently moused over or has | |
145 // selected with the keyboard. This is not the same as the original index, | |
146 // since the user has not yet accepted this input. | |
147 int selectedIndex() const { return m_selectedIndex; } | |
148 | |
149 // Moves selection down/up the given number of items, scrolling if necessary
. | |
150 // Positive is down. The resulting index will be clamped to the range | |
151 // [0, numItems), and non-option items will be skipped. | |
152 void adjustSelectedIndex(int delta); | |
153 | |
154 // Returns the number of items in the list. | |
155 int numItems() const { return static_cast<int>(m_items.size()); } | |
156 | |
157 void setBaseWidth(int width) { m_baseWidth = std::min(m_maxWindowWidth, widt
h); } | |
158 | |
159 // Computes the size of widget and children. | |
160 virtual void layout() override; | |
161 | |
162 // Returns whether the popup wants to process events for the passed key. | |
163 bool isInterestedInEventForKey(int keyCode); | |
164 | |
165 // Gets the height of a row. | |
166 int getRowHeight(int index) const; | |
167 | |
168 int getRowBaseWidth(int index); | |
169 | |
170 virtual void setMaxHeight(int maxHeight) override { m_maxHeight = maxHeight;
} | |
171 | |
172 void setMaxWidth(int maxWidth) { m_maxWindowWidth = maxWidth; } | |
173 | |
174 virtual void setMaxWidthAndLayout(int) override; | |
175 | |
176 void disconnectClient() { m_popupClient = 0; } | |
177 | |
178 const Vector<PopupItem*>& items() const { return m_items; } | |
179 | |
180 virtual int popupContentHeight() const override; | |
181 | |
182 static const int defaultMaxHeight; | |
183 | |
184 void trace(Visitor*) override; | |
185 | |
186 protected: | |
187 virtual void invalidateScrollCornerRect(const IntRect&) override { } | |
188 | |
189 private: | |
190 friend class PopupContainer; | |
191 friend class RefCounted<PopupListBox>; | |
192 | |
193 PopupListBox(PopupMenuClient*, bool deviceSupportsTouch, PopupContainer*); | |
194 virtual ~PopupListBox(); | |
195 | |
196 // Hides the popup. Other classes should not call this. Use abandon instead. | |
197 void hidePopup(); | |
198 | |
199 // Returns true if the selection can be changed to index. | |
200 // Disabled items, or labels cannot be selected. | |
201 bool isSelectableItem(int index); | |
202 | |
203 // Select an index in the list, scrolling if necessary. | |
204 void selectIndex(int index); | |
205 | |
206 // Accepts the selected index as the value to be displayed in the <select> | |
207 // widget on the web page, and closes the popup. Returns true if index is | |
208 // accepted. | |
209 bool acceptIndex(int index); | |
210 | |
211 // Clears the selection (so no row appears selected). | |
212 void clearSelection(); | |
213 | |
214 // Scrolls to reveal the given index. | |
215 void scrollToRevealRow(int index); | |
216 void scrollToRevealSelection() { scrollToRevealRow(m_selectedIndex); } | |
217 | |
218 // Invalidates the row at the given index. | |
219 void invalidateRow(int index); | |
220 | |
221 // Get the bounds of a row. | |
222 IntRect getRowBounds(int index); | |
223 | |
224 // Converts a point to an index of the row the point is over | |
225 int pointToRowIndex(const IntPoint&); | |
226 | |
227 // Paint an individual row | |
228 void paintRow(GraphicsContext*, const IntRect&, int rowIndex); | |
229 | |
230 // Test if the given point is within the bounds of the popup window. | |
231 bool isPointInBounds(const IntPoint&); | |
232 | |
233 // Called when the user presses a text key. Does a prefix-search of the item
s. | |
234 void typeAheadFind(const PlatformKeyboardEvent&); | |
235 | |
236 // Returns the font to use for the given row | |
237 Font getRowFont(int index) const; | |
238 | |
239 // Moves the selection down/up one item, taking care of looping back to the | |
240 // first/last element if m_loopSelectionNavigation is true. | |
241 void selectPreviousRow(); | |
242 void selectNextRow(); | |
243 | |
244 int scrollX() const { return scrollPosition().x(); } | |
245 int scrollY() const { return scrollPosition().y(); } | |
246 void updateScrollbars(IntPoint desiredOffset); | |
247 void setHasVerticalScrollbar(bool); | |
248 Scrollbar* scrollbarAtWindowPoint(const IntPoint& windowPoint); | |
249 IntRect contentsToWindow(const IntRect&) const; | |
250 void setContentsSize(const IntSize&); | |
251 void adjustScrollbarExistence(); | |
252 void updateScrollbarGeometry(); | |
253 IntRect windowClipRect() const; | |
254 | |
255 // If the device is a touch screen we increase the height of menu items | |
256 // to make it easier to unambiguously touch them. | |
257 bool m_deviceSupportsTouch; | |
258 | |
259 // This is the index of the item marked as "selected" - i.e. displayed in | |
260 // the widget on the page. | |
261 int m_originalIndex; | |
262 | |
263 // This is the index of the item that the user is hovered over or has | |
264 // selected using the keyboard in the list. They have not confirmed this | |
265 // selection by clicking or pressing enter yet however. | |
266 int m_selectedIndex; | |
267 | |
268 // If >= 0, this is the index we should accept if the popup is "abandoned". | |
269 // This is used for keyboard navigation, where we want the | |
270 // selection to change immediately, and is only used if the settings | |
271 // acceptOnAbandon field is true. | |
272 int m_acceptedIndexOnAbandon; | |
273 | |
274 // This is the number of rows visible in the popup. The maximum number | |
275 // visible at a time is defined as being kMaxVisibleRows. For a scrolled | |
276 // popup, this can be thought of as the page size in data units. | |
277 int m_visibleRows; | |
278 | |
279 // Our suggested width, not including scrollbar. | |
280 int m_baseWidth; | |
281 | |
282 // The maximum height we can be without being off-screen. | |
283 int m_maxHeight; | |
284 | |
285 // A list of the options contained within the <select> | |
286 Vector<PopupItem*> m_items; | |
287 | |
288 // The <select> PopupMenuClient that opened us. | |
289 PopupMenuClient* m_popupClient; | |
290 | |
291 // The scrollbar which has mouse capture. Mouse events go straight to this | |
292 // if not null. | |
293 RefPtrWillBeMember<Scrollbar> m_capturingScrollbar; | |
294 | |
295 // The last scrollbar that the mouse was over. Used for mouseover highlights
. | |
296 RefPtrWillBeMember<Scrollbar> m_lastScrollbarUnderMouse; | |
297 | |
298 // The string the user has typed so far into the popup. Used for typeAheadFi
nd. | |
299 String m_typedString; | |
300 | |
301 // The char the user has hit repeatedly. Used for typeAheadFind. | |
302 UChar m_repeatingChar; | |
303 | |
304 // The last time the user hit a key. Used for typeAheadFind. | |
305 TimeStamp m_lastCharTime; | |
306 | |
307 // If width exeeds screen width, we have to clip it. | |
308 int m_maxWindowWidth; | |
309 | |
310 // To forward last mouse release event. | |
311 RefPtrWillBeMember<Element> m_focusedElement; | |
312 | |
313 // Oilpan: the container owns/wraps this listbox. A (strong) | |
314 // Member can be used for the back reference without extending the | |
315 // container's lifetime; the two objects live equally long. | |
316 RawPtrWillBeMember<PopupContainer> m_container; | |
317 | |
318 RefPtrWillBeMember<Scrollbar> m_verticalScrollbar; | |
319 IntSize m_contentsSize; | |
320 IntPoint m_scrollOffset; | |
321 }; | |
322 | |
323 } // namespace blink | |
324 | |
325 #endif | |
OLD | NEW |