2 * Copyright (c) 2011, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef PopupListBox_h
32 #define PopupListBox_h
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"
44 class GraphicsContext;
46 class PlatformKeyboardEvent;
47 class PlatformMouseEvent;
48 class PlatformGestureEvent;
49 class PlatformTouchEvent;
50 class PlatformWheelEvent;
52 class PopupMenuClient;
53 typedef unsigned long long TimeStamp;
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() { };
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.
73 PopupItem(const String& label, Type type)
81 int yOffset; // y offset of this item, relative to the top of the popup.
82 TextDirection textDirection;
83 bool hasTextDirectionOverride;
88 // This class manages the scrollable content inside a <select> popup.
89 class PopupListBox final : public Widget, public ScrollableArea, public PopupContent {
91 static PassRefPtrWillBeRawPtr<PopupListBox> create(PopupMenuClient* client, bool deviceSupportsTouch, PopupContainer* container)
93 return adoptRefWillBeNoop(new PopupListBox(client, deviceSupportsTouch, container));
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&) const override;
102 virtual IntPoint convertSelfToChild(const Widget* child, const IntPoint&) const override;
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 override { return orientation == VerticalScrollbar; }
114 virtual Scrollbar* verticalScrollbar() const override { return m_verticalScrollbar.get(); }
115 virtual IntRect visibleContentRect(IncludeScrollbarsInRect = ExcludeScrollbars) 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 position we can be scrolled to.
119 virtual IntPoint minimumScrollPosition() const override; // The minimum position we can be scrolled to.
120 virtual IntRect scrollCornerRect() const override { return IntRect(); }
122 // PopupListBox methods
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&);
135 // Updates our internal list to match the client.
136 void updateFromElement();
138 // Frees any allocated resources used in a particular popup session.
141 // Sets the index of the option that is displayed in the <select> widget in the page
142 void setOriginalIndex(int);
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; }
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);
154 // Returns the number of items in the list.
155 int numItems() const { return static_cast<int>(m_items.size()); }
157 void setBaseWidth(int width) { m_baseWidth = std::min(m_maxWindowWidth, width); }
159 // Computes the size of widget and children.
160 virtual void layout() override;
162 // Returns whether the popup wants to process events for the passed key.
163 bool isInterestedInEventForKey(int keyCode);
165 // Gets the height of a row.
166 int getRowHeight(int index) const;
168 int getRowBaseWidth(int index);
170 virtual void setMaxHeight(int maxHeight) override { m_maxHeight = maxHeight; }
172 void setMaxWidth(int maxWidth) { m_maxWindowWidth = maxWidth; }
174 virtual void setMaxWidthAndLayout(int) override;
176 void disconnectClient() { m_popupClient = 0; }
178 const Vector<PopupItem*>& items() const { return m_items; }
180 virtual int popupContentHeight() const override;
182 static const int defaultMaxHeight;
184 void trace(Visitor*) override;
187 virtual void invalidateScrollCornerRect(const IntRect&) override { }
190 friend class PopupContainer;
191 friend class RefCounted<PopupListBox>;
193 PopupListBox(PopupMenuClient*, bool deviceSupportsTouch, PopupContainer*);
194 virtual ~PopupListBox();
196 // Hides the popup. Other classes should not call this. Use abandon instead.
199 // Returns true if the selection can be changed to index.
200 // Disabled items, or labels cannot be selected.
201 bool isSelectableItem(int index);
203 // Select an index in the list, scrolling if necessary.
204 void selectIndex(int index);
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
209 bool acceptIndex(int index);
211 // Clears the selection (so no row appears selected).
212 void clearSelection();
214 // Scrolls to reveal the given index.
215 void scrollToRevealRow(int index);
216 void scrollToRevealSelection() { scrollToRevealRow(m_selectedIndex); }
218 // Invalidates the row at the given index.
219 void invalidateRow(int index);
221 // Get the bounds of a row.
222 IntRect getRowBounds(int index);
224 // Converts a point to an index of the row the point is over
225 int pointToRowIndex(const IntPoint&);
227 // Paint an individual row
228 void paintRow(GraphicsContext*, const IntRect&, int rowIndex);
230 // Test if the given point is within the bounds of the popup window.
231 bool isPointInBounds(const IntPoint&);
233 // Called when the user presses a text key. Does a prefix-search of the items.
234 void typeAheadFind(const PlatformKeyboardEvent&);
236 // Returns the font to use for the given row
237 Font getRowFont(int index) const;
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();
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;
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;
259 // This is the index of the item marked as "selected" - i.e. displayed in
260 // the widget on the page.
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.
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;
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.
279 // Our suggested width, not including scrollbar.
282 // The maximum height we can be without being off-screen.
285 // A list of the options contained within the <select>
286 Vector<PopupItem*> m_items;
288 // The <select> PopupMenuClient that opened us.
289 PopupMenuClient* m_popupClient;
291 // The scrollbar which has mouse capture. Mouse events go straight to this
293 RefPtrWillBeMember<Scrollbar> m_capturingScrollbar;
295 // The last scrollbar that the mouse was over. Used for mouseover highlights.
296 RefPtrWillBeMember<Scrollbar> m_lastScrollbarUnderMouse;
298 // The string the user has typed so far into the popup. Used for typeAheadFind.
299 String m_typedString;
301 // The char the user has hit repeatedly. Used for typeAheadFind.
302 UChar m_repeatingChar;
304 // The last time the user hit a key. Used for typeAheadFind.
305 TimeStamp m_lastCharTime;
307 // If width exeeds screen width, we have to clip it.
308 int m_maxWindowWidth;
310 // To forward last mouse release event.
311 RefPtrWillBeMember<Element> m_focusedElement;
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;
318 RefPtrWillBeMember<Scrollbar> m_verticalScrollbar;
319 IntSize m_contentsSize;
320 IntPoint m_scrollOffset;