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.
30 #ifndef PopupListBox_h
31 #define PopupListBox_h
34 #include "FontSelector.h"
35 #include "FramelessScrollView.h"
41 typedef unsigned long long TimeStamp;
43 static const int kLabelToIconPadding = 5;
44 static const int kLinePaddingHeight = 3; // Padding height put at the top and bottom of each line.
45 static const int kMaxHeight = 500;
46 static const int kMaxVisibleRows = 20;
47 static const int kMinEndOfLinePadding = 2;
48 static const int kTextToLabelPadding = 10;
49 static const TimeStamp kTypeAheadTimeoutMs = 1000;
51 class GraphicsContext;
52 class PlatformKeyboardEvent;
53 class PlatformMouseEvent;
54 #if ENABLE(GESTURE_EVENTS)
55 class PlatformGestureEvent;
57 #if ENABLE(TOUCH_EVENTS)
58 class PlatformTouchEvent;
60 class PlatformWheelEvent;
61 class PopupMenuClient;
63 struct PopupContainerSettings {
64 // Whether the PopupMenuClient should be told to change its text when a
65 // new item is selected by using the arrow keys.
66 bool setTextOnIndexChange;
68 // Whether the selection should be accepted when the popup menu is
69 // closed (through ESC being pressed or the focus going away).
70 // Note that when TAB is pressed, the selection is always accepted
71 // regardless of this setting.
74 // Whether we should move the selection to the first/last item when
75 // the user presses down/up arrow keys and the last/first item is
77 bool loopSelectionNavigation;
79 // Whether we should restrict the width of the PopupListBox or not.
80 // Autocomplete popups are restricted, combo-boxes (select tags) aren't.
81 bool restrictWidthOfListBox;
84 // A container for the data for each menu item (e.g. represented by <option>
85 // or <optgroup> in a <select> widget) and is used by PopupListBox.
93 PopupItem(const String& label, Type type)
101 int yOffset; // y offset of this item, relative to the top of the popup.
102 TextDirection textDirection;
103 bool hasTextDirectionOverride;
107 // This class uses WebCore code to paint and handle events for a drop-down list
108 // box ("combobox" on Windows).
109 class PopupListBox : public FramelessScrollView {
111 static PassRefPtr<PopupListBox> create(PopupMenuClient* client, const PopupContainerSettings& settings)
113 return adoptRef(new PopupListBox(client, settings));
116 // FramelessScrollView
117 virtual void paint(GraphicsContext*, const IntRect&);
118 virtual bool handleMouseDownEvent(const PlatformMouseEvent&);
119 virtual bool handleMouseMoveEvent(const PlatformMouseEvent&);
120 virtual bool handleMouseReleaseEvent(const PlatformMouseEvent&);
121 virtual bool handleWheelEvent(const PlatformWheelEvent&);
122 virtual bool handleKeyEvent(const PlatformKeyboardEvent&);
123 #if ENABLE(TOUCH_EVENTS)
124 virtual bool handleTouchEvent(const PlatformTouchEvent&);
126 #if ENABLE(GESTURE_RECOGNIZER)
127 virtual bool handleGestureEvent(const PlatformGestureEvent&);
131 virtual HostWindow* hostWindow() const;
133 // PopupListBox methods
138 // Updates our internal list to match the client.
139 void updateFromElement();
141 // Frees any allocated resources used in a particular popup session.
144 // Sets the index of the option that is displayed in the <select> widget in the page
145 void setOriginalIndex(int);
147 // Gets the index of the item that the user is currently moused over or has selected with
148 // the keyboard. This is not the same as the original index, since the user has not yet
149 // accepted this input.
150 int selectedIndex() const { return m_selectedIndex; }
152 // Moves selection down/up the given number of items, scrolling if necessary.
153 // Positive is down. The resulting index will be clamped to the range
154 // [0, numItems), and non-option items will be skipped.
155 void adjustSelectedIndex(int delta);
157 // Returns the number of items in the list.
158 int numItems() const { return static_cast<int>(m_items.size()); }
160 void setBaseWidth(int width) { m_baseWidth = std::min(m_maxWindowWidth, width); }
162 // Computes the size of widget and children.
165 // Returns whether the popup wants to process events for the passed key.
166 bool isInterestedInEventForKey(int keyCode);
168 // Gets the height of a row.
169 int getRowHeight(int index);
171 void setMaxHeight(int maxHeight) { m_maxHeight = maxHeight; }
173 void setMaxWidth(int maxWidth) { m_maxWindowWidth = maxWidth; }
175 void setMaxWidthAndLayout(int);
177 void disconnectClient() { m_popupClient = 0; }
179 const Vector<PopupItem*>& items() const { return m_items; }
182 friend class PopupContainer;
183 friend class RefCounted<PopupListBox>;
185 PopupListBox(PopupMenuClient*, const PopupContainerSettings&);
195 // Returns true if the selection can be changed to index.
196 // Disabled items, or labels cannot be selected.
197 bool isSelectableItem(int index);
199 // Select an index in the list, scrolling if necessary.
200 void selectIndex(int index);
202 // Accepts the selected index as the value to be displayed in the <select> widget on
203 // the web page, and closes the popup. Returns true if index is accepted.
204 bool acceptIndex(int index);
206 // Clears the selection (so no row appears selected).
207 void clearSelection();
209 // Scrolls to reveal the given index.
210 void scrollToRevealRow(int index);
211 void scrollToRevealSelection() { scrollToRevealRow(m_selectedIndex); }
213 // Invalidates the row at the given index.
214 void invalidateRow(int index);
216 // Get the bounds of a row.
217 IntRect getRowBounds(int index);
219 // Converts a point to an index of the row the point is over
220 int pointToRowIndex(const IntPoint&);
222 // Paint an individual row
223 void paintRow(GraphicsContext*, const IntRect&, int rowIndex);
225 // Test if the given point is within the bounds of the popup window.
226 bool isPointInBounds(const IntPoint&);
228 // Called when the user presses a text key. Does a prefix-search of the items.
229 void typeAheadFind(const PlatformKeyboardEvent&);
231 // Returns the font to use for the given row
232 Font getRowFont(int index);
234 // Moves the selection down/up one item, taking care of looping back to the
235 // first/last element if m_loopSelectionNavigation is true.
236 void selectPreviousRow();
237 void selectNextRow();
239 // The settings that specify the behavior for this Popup window.
240 PopupContainerSettings m_settings;
242 // This is the index of the item marked as "selected" - i.e. displayed in the widget on the
246 // This is the index of the item that the user is hovered over or has selected using the
247 // keyboard in the list. They have not confirmed this selection by clicking or pressing
248 // enter yet however.
251 // If >= 0, this is the index we should accept if the popup is "abandoned".
252 // This is used for keyboard navigation, where we want the
253 // selection to change immediately, and is only used if the settings
254 // acceptOnAbandon field is true.
255 int m_acceptedIndexOnAbandon;
257 // This is the number of rows visible in the popup. The maximum number visible at a time is
258 // defined as being kMaxVisibleRows. For a scrolled popup, this can be thought of as the
259 // page size in data units.
262 // Our suggested width, not including scrollbar.
265 // The maximum height we can be without being off-screen.
268 // A list of the options contained within the <select>
269 Vector<PopupItem*> m_items;
271 // The <select> PopupMenuClient that opened us.
272 PopupMenuClient* m_popupClient;
274 // The scrollbar which has mouse capture. Mouse events go straight to this
276 RefPtr<Scrollbar> m_capturingScrollbar;
278 // The last scrollbar that the mouse was over. Used for mouseover highlights.
279 RefPtr<Scrollbar> m_lastScrollbarUnderMouse;
281 // The string the user has typed so far into the popup. Used for typeAheadFind.
282 String m_typedString;
284 // The char the user has hit repeatedly. Used for typeAheadFind.
285 UChar m_repeatingChar;
287 // The last time the user hit a key. Used for typeAheadFind.
288 TimeStamp m_lastCharTime;
290 // If width exeeds screen width, we have to clip it.
291 int m_maxWindowWidth;
293 // To forward last mouse release event.
294 RefPtr<Node> m_focusedNode;
298 } // namespace WebCore