initial import
[vuplus_webkit] / Source / WebCore / platform / chromium / PopupListBox.h
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 #ifndef PopupListBox_h
31 #define PopupListBox_h
32
33 #include "Font.h"
34 #include "FontSelector.h"
35 #include "FramelessScrollView.h"
36 #include "IntRect.h"
37 #include "Node.h"
38
39 namespace WebCore {
40
41 typedef unsigned long long TimeStamp;
42
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;
50
51 class GraphicsContext;
52 class PlatformKeyboardEvent;
53 class PlatformMouseEvent;
54 #if ENABLE(GESTURE_EVENTS)
55 class PlatformGestureEvent;
56 #endif
57 #if ENABLE(TOUCH_EVENTS)
58 class PlatformTouchEvent;
59 #endif
60 class PlatformWheelEvent;
61 class PopupMenuClient;
62
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;
67
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.
72     bool acceptOnAbandon;
73
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
76     // selected.
77     bool loopSelectionNavigation;
78
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;
82 };
83
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.
86 struct PopupItem {
87     enum Type {
88         TypeOption,
89         TypeGroup,
90         TypeSeparator
91     };
92
93     PopupItem(const String& label, Type type)
94         : label(label)
95         , type(type)
96         , yOffset(0)
97     {
98     }
99     String label;
100     Type type;
101     int yOffset; // y offset of this item, relative to the top of the popup.
102     TextDirection textDirection;
103     bool hasTextDirectionOverride;
104     bool enabled;
105 };
106
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 {
110 public:
111     static PassRefPtr<PopupListBox> create(PopupMenuClient* client, const PopupContainerSettings& settings)
112     {
113         return adoptRef(new PopupListBox(client, settings));
114     }
115
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&);
125 #endif
126 #if ENABLE(GESTURE_RECOGNIZER)
127     virtual bool handleGestureEvent(const PlatformGestureEvent&);
128 #endif
129
130     // ScrollView
131     virtual HostWindow* hostWindow() const;
132
133     // PopupListBox methods
134
135     // Hides the popup.
136     void hidePopup();
137
138     // Updates our internal list to match the client.
139     void updateFromElement();
140
141     // Frees any allocated resources used in a particular popup session.
142     void clear();
143
144     // Sets the index of the option that is displayed in the <select> widget in the page
145     void setOriginalIndex(int);
146
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; }
151
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);
156
157     // Returns the number of items in the list.
158     int numItems() const { return static_cast<int>(m_items.size()); }
159
160     void setBaseWidth(int width) { m_baseWidth = std::min(m_maxWindowWidth, width); }
161
162     // Computes the size of widget and children.
163     void layout();
164
165     // Returns whether the popup wants to process events for the passed key.
166     bool isInterestedInEventForKey(int keyCode);
167
168     // Gets the height of a row.
169     int getRowHeight(int index);
170
171     void setMaxHeight(int maxHeight) { m_maxHeight = maxHeight; }
172
173     void setMaxWidth(int maxWidth) { m_maxWindowWidth = maxWidth; }
174
175     void setMaxWidthAndLayout(int);
176
177     void disconnectClient() { m_popupClient = 0; }
178
179     const Vector<PopupItem*>& items() const { return m_items; }
180
181 private:
182     friend class PopupContainer;
183     friend class RefCounted<PopupListBox>;
184
185     PopupListBox(PopupMenuClient*, const PopupContainerSettings&);
186
187     ~PopupListBox()
188     {
189         clear();
190     }
191
192     // Closes the popup
193     void abandon();
194
195     // Returns true if the selection can be changed to index.
196     // Disabled items, or labels cannot be selected.
197     bool isSelectableItem(int index);
198
199     // Select an index in the list, scrolling if necessary.
200     void selectIndex(int index);
201
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);
205
206     // Clears the selection (so no row appears selected).
207     void clearSelection();
208
209     // Scrolls to reveal the given index.
210     void scrollToRevealRow(int index);
211     void scrollToRevealSelection() { scrollToRevealRow(m_selectedIndex); }
212
213     // Invalidates the row at the given index.
214     void invalidateRow(int index);
215
216     // Get the bounds of a row.
217     IntRect getRowBounds(int index);
218
219     // Converts a point to an index of the row the point is over
220     int pointToRowIndex(const IntPoint&);
221
222     // Paint an individual row
223     void paintRow(GraphicsContext*, const IntRect&, int rowIndex);
224
225     // Test if the given point is within the bounds of the popup window.
226     bool isPointInBounds(const IntPoint&);
227
228     // Called when the user presses a text key. Does a prefix-search of the items.
229     void typeAheadFind(const PlatformKeyboardEvent&);
230
231     // Returns the font to use for the given row
232     Font getRowFont(int index);
233
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();
238
239     // The settings that specify the behavior for this Popup window.
240     PopupContainerSettings m_settings;
241
242     // This is the index of the item marked as "selected" - i.e. displayed in the widget on the
243     // page.
244     int m_originalIndex;
245
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.
249     int m_selectedIndex;
250
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;
256
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.
260     int m_visibleRows;
261
262     // Our suggested width, not including scrollbar.
263     int m_baseWidth;
264
265     // The maximum height we can be without being off-screen.
266     int m_maxHeight;
267
268     // A list of the options contained within the <select>
269     Vector<PopupItem*> m_items;
270
271     // The <select> PopupMenuClient that opened us.
272     PopupMenuClient* m_popupClient;
273
274     // The scrollbar which has mouse capture. Mouse events go straight to this
275     // if not null.
276     RefPtr<Scrollbar> m_capturingScrollbar;
277
278     // The last scrollbar that the mouse was over. Used for mouseover highlights.
279     RefPtr<Scrollbar> m_lastScrollbarUnderMouse;
280
281     // The string the user has typed so far into the popup. Used for typeAheadFind.
282     String m_typedString;
283
284     // The char the user has hit repeatedly. Used for typeAheadFind.
285     UChar m_repeatingChar;
286
287     // The last time the user hit a key. Used for typeAheadFind.
288     TimeStamp m_lastCharTime;
289
290     // If width exeeds screen width, we have to clip it.
291     int m_maxWindowWidth;
292
293     // To forward last mouse release event.
294     RefPtr<Node> m_focusedNode;
295
296 };
297
298 } // namespace WebCore
299
300 #endif