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 #include "config.h" | |
32 #include "core/platform/chromium/PopupListBox.h" | |
33 | |
34 #include "CSSValueKeywords.h" | |
35 #include "RuntimeEnabledFeatures.h" | |
36 #include "core/platform/PlatformGestureEvent.h" | |
37 #include "core/platform/PlatformKeyboardEvent.h" | |
38 #include "core/platform/PlatformMouseEvent.h" | |
39 #include "core/platform/PlatformScreen.h" | |
40 #include "core/platform/PlatformTouchEvent.h" | |
41 #include "core/platform/PlatformWheelEvent.h" | |
42 #include "core/platform/PopupMenuClient.h" | |
43 #include "core/platform/ScrollbarTheme.h" | |
44 #include "core/platform/chromium/FramelessScrollViewClient.h" | |
45 #include "core/platform/chromium/KeyboardCodes.h" | |
46 #include "core/platform/chromium/PopupContainer.h" | |
47 #include "core/platform/chromium/PopupMenuChromium.h" | |
48 #include "core/platform/graphics/Font.h" | |
49 #include "core/platform/graphics/FontSelector.h" | |
50 #include "core/platform/graphics/GraphicsContext.h" | |
51 #include "core/platform/graphics/IntRect.h" | |
52 #include "core/platform/graphics/StringTruncator.h" | |
53 #include "core/platform/graphics/TextRun.h" | |
54 #include "core/rendering/RenderTheme.h" | |
55 #include "wtf/ASCIICType.h" | |
56 #include "wtf/CurrentTime.h" | |
57 #include <limits> | |
58 | |
59 namespace WebCore { | |
60 | |
61 using namespace WTF::Unicode; | |
62 | |
63 static const int labelToIconPadding = 5; | |
64 // Padding height put at the top and bottom of each line. | |
65 static const int autofillLinePaddingHeight = 3; | |
66 const int PopupListBox::defaultMaxHeight = 500; | |
67 static const int maxVisibleRows = 20; | |
68 static const int minEndOfLinePadding = 2; | |
69 static const int textToLabelPadding = 10; | |
70 static const TimeStamp typeAheadTimeoutMs = 1000; | |
71 | |
72 PopupListBox::PopupListBox(PopupMenuClient* client, const PopupContainerSettings
& settings) | |
73 : m_settings(settings) | |
74 , m_originalIndex(0) | |
75 , m_selectedIndex(0) | |
76 , m_acceptedIndexOnAbandon(-1) | |
77 , m_visibleRows(0) | |
78 , m_baseWidth(0) | |
79 , m_maxHeight(defaultMaxHeight) | |
80 , m_popupClient(client) | |
81 , m_repeatingChar(0) | |
82 , m_lastCharTime(0) | |
83 , m_maxWindowWidth(std::numeric_limits<int>::max()) | |
84 { | |
85 setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff); | |
86 } | |
87 | |
88 bool PopupListBox::handleMouseDownEvent(const PlatformMouseEvent& event) | |
89 { | |
90 Scrollbar* scrollbar = scrollbarAtPoint(event.position()); | |
91 if (scrollbar) { | |
92 m_capturingScrollbar = scrollbar; | |
93 m_capturingScrollbar->mouseDown(event); | |
94 return true; | |
95 } | |
96 | |
97 if (!isPointInBounds(event.position())) | |
98 abandon(); | |
99 | |
100 return true; | |
101 } | |
102 | |
103 bool PopupListBox::handleMouseMoveEvent(const PlatformMouseEvent& event) | |
104 { | |
105 if (m_capturingScrollbar) { | |
106 m_capturingScrollbar->mouseMoved(event); | |
107 return true; | |
108 } | |
109 | |
110 Scrollbar* scrollbar = scrollbarAtPoint(event.position()); | |
111 if (m_lastScrollbarUnderMouse != scrollbar) { | |
112 // Send mouse exited to the old scrollbar. | |
113 if (m_lastScrollbarUnderMouse) | |
114 m_lastScrollbarUnderMouse->mouseExited(); | |
115 m_lastScrollbarUnderMouse = scrollbar; | |
116 } | |
117 | |
118 if (scrollbar) { | |
119 scrollbar->mouseMoved(event); | |
120 return true; | |
121 } | |
122 | |
123 if (!isPointInBounds(event.position())) | |
124 return false; | |
125 | |
126 selectIndex(pointToRowIndex(event.position())); | |
127 return true; | |
128 } | |
129 | |
130 bool PopupListBox::handleMouseReleaseEvent(const PlatformMouseEvent& event) | |
131 { | |
132 if (m_capturingScrollbar) { | |
133 m_capturingScrollbar->mouseUp(event); | |
134 m_capturingScrollbar = 0; | |
135 return true; | |
136 } | |
137 | |
138 if (!isPointInBounds(event.position())) | |
139 return true; | |
140 | |
141 // Need to check before calling acceptIndex(), because m_popupClient might | |
142 // be removed in acceptIndex() calling because of event handler. | |
143 bool isSelectPopup = m_popupClient->menuStyle().menuType() == PopupMenuStyle
::SelectPopup; | |
144 if (acceptIndex(pointToRowIndex(event.position())) && m_focusedNode && isSel
ectPopup) { | |
145 m_focusedNode->dispatchMouseEvent(event, eventNames().mouseupEvent); | |
146 m_focusedNode->dispatchMouseEvent(event, eventNames().clickEvent); | |
147 | |
148 // Clear m_focusedNode here, because we cannot clear in hidePopup() | |
149 // which is called before dispatchMouseEvent() is called. | |
150 m_focusedNode = 0; | |
151 } | |
152 | |
153 return true; | |
154 } | |
155 | |
156 bool PopupListBox::handleWheelEvent(const PlatformWheelEvent& event) | |
157 { | |
158 if (!isPointInBounds(event.position())) { | |
159 abandon(); | |
160 return true; | |
161 } | |
162 | |
163 ScrollableArea::handleWheelEvent(event); | |
164 return true; | |
165 } | |
166 | |
167 // Should be kept in sync with handleKeyEvent(). | |
168 bool PopupListBox::isInterestedInEventForKey(int keyCode) | |
169 { | |
170 switch (keyCode) { | |
171 case VKEY_ESCAPE: | |
172 case VKEY_RETURN: | |
173 case VKEY_UP: | |
174 case VKEY_DOWN: | |
175 case VKEY_PRIOR: | |
176 case VKEY_NEXT: | |
177 case VKEY_HOME: | |
178 case VKEY_END: | |
179 case VKEY_TAB: | |
180 return true; | |
181 default: | |
182 return false; | |
183 } | |
184 } | |
185 | |
186 bool PopupListBox::handleTouchEvent(const PlatformTouchEvent&) | |
187 { | |
188 return false; | |
189 } | |
190 | |
191 bool PopupListBox::handleGestureEvent(const PlatformGestureEvent&) | |
192 { | |
193 return false; | |
194 } | |
195 | |
196 static bool isCharacterTypeEvent(const PlatformKeyboardEvent& event) | |
197 { | |
198 // Check whether the event is a character-typed event or not. | |
199 // We use RawKeyDown/Char/KeyUp event scheme on all platforms, | |
200 // so PlatformKeyboardEvent::Char (not RawKeyDown) type event | |
201 // is considered as character type event. | |
202 return event.type() == PlatformEvent::Char; | |
203 } | |
204 | |
205 bool PopupListBox::handleKeyEvent(const PlatformKeyboardEvent& event) | |
206 { | |
207 if (event.type() == PlatformEvent::KeyUp) | |
208 return true; | |
209 | |
210 if (!numItems() && event.windowsVirtualKeyCode() != VKEY_ESCAPE) | |
211 return true; | |
212 | |
213 switch (event.windowsVirtualKeyCode()) { | |
214 case VKEY_ESCAPE: | |
215 abandon(); // may delete this | |
216 return true; | |
217 case VKEY_RETURN: | |
218 if (m_selectedIndex == -1) { | |
219 hidePopup(); | |
220 // Don't eat the enter if nothing is selected. | |
221 return false; | |
222 } | |
223 acceptIndex(m_selectedIndex); // may delete this | |
224 return true; | |
225 case VKEY_UP: | |
226 case VKEY_DOWN: | |
227 // We have to forward only shift + up combination to focused node when | |
228 // autofill popup. Because all characters from the cursor to the start | |
229 // of the text area should selected when you press shift + up arrow. | |
230 // shift + down should be the similar way to shift + up. | |
231 if (event.modifiers() && m_popupClient->menuStyle().menuType() == PopupM
enuStyle::AutofillPopup) | |
232 m_focusedNode->dispatchKeyEvent(event); | |
233 else if (event.windowsVirtualKeyCode() == VKEY_UP) | |
234 selectPreviousRow(); | |
235 else | |
236 selectNextRow(); | |
237 break; | |
238 case VKEY_PRIOR: | |
239 adjustSelectedIndex(-m_visibleRows); | |
240 break; | |
241 case VKEY_NEXT: | |
242 adjustSelectedIndex(m_visibleRows); | |
243 break; | |
244 case VKEY_HOME: | |
245 adjustSelectedIndex(-m_selectedIndex); | |
246 break; | |
247 case VKEY_END: | |
248 adjustSelectedIndex(m_items.size()); | |
249 break; | |
250 default: | |
251 if (!event.ctrlKey() && !event.altKey() && !event.metaKey() | |
252 && isPrintableChar(event.windowsVirtualKeyCode()) | |
253 && isCharacterTypeEvent(event)) | |
254 typeAheadFind(event); | |
255 break; | |
256 } | |
257 | |
258 if (m_originalIndex != m_selectedIndex) { | |
259 // Keyboard events should update the selection immediately (but we don't | |
260 // want to fire the onchange event until the popup is closed, to match | |
261 // IE). We change the original index so we revert to that when the | |
262 // popup is closed. | |
263 if (m_settings.acceptOnAbandon) | |
264 m_acceptedIndexOnAbandon = m_selectedIndex; | |
265 | |
266 setOriginalIndex(m_selectedIndex); | |
267 if (m_settings.setTextOnIndexChange) | |
268 m_popupClient->setTextFromItem(m_selectedIndex); | |
269 } | |
270 if (event.windowsVirtualKeyCode() == VKEY_TAB) { | |
271 // TAB is a special case as it should select the current item if any and | |
272 // advance focus. | |
273 if (m_selectedIndex >= 0) { | |
274 acceptIndex(m_selectedIndex); // May delete us. | |
275 // Return false so the TAB key event is propagated to the page. | |
276 return false; | |
277 } | |
278 // Call abandon() so we honor m_acceptedIndexOnAbandon if set. | |
279 abandon(); | |
280 // Return false so the TAB key event is propagated to the page. | |
281 return false; | |
282 } | |
283 | |
284 return true; | |
285 } | |
286 | |
287 HostWindow* PopupListBox::hostWindow() const | |
288 { | |
289 // Our parent is the root ScrollView, so it is the one that has a | |
290 // HostWindow. FrameView::hostWindow() works similarly. | |
291 return parent() ? parent()->hostWindow() : 0; | |
292 } | |
293 | |
294 // From HTMLSelectElement.cpp | |
295 static String stripLeadingWhiteSpace(const String& string) | |
296 { | |
297 int length = string.length(); | |
298 int i; | |
299 for (i = 0; i < length; ++i) | |
300 if (string[i] != noBreakSpace | |
301 && (string[i] <= 0x7F ? !isASCIISpace(string[i]) : (direction(string
[i]) != WhiteSpaceNeutral))) | |
302 break; | |
303 | |
304 return string.substring(i, length - i); | |
305 } | |
306 | |
307 // From HTMLSelectElement.cpp, with modifications | |
308 void PopupListBox::typeAheadFind(const PlatformKeyboardEvent& event) | |
309 { | |
310 TimeStamp now = static_cast<TimeStamp>(currentTime() * 1000.0f); | |
311 TimeStamp delta = now - m_lastCharTime; | |
312 | |
313 // Reset the time when user types in a character. The time gap between | |
314 // last character and the current character is used to indicate whether | |
315 // user typed in a string or just a character as the search prefix. | |
316 m_lastCharTime = now; | |
317 | |
318 UChar c = event.windowsVirtualKeyCode(); | |
319 | |
320 String prefix; | |
321 int searchStartOffset = 1; | |
322 if (delta > typeAheadTimeoutMs) { | |
323 m_typedString = prefix = String(&c, 1); | |
324 m_repeatingChar = c; | |
325 } else { | |
326 m_typedString.append(c); | |
327 | |
328 if (c == m_repeatingChar) { | |
329 // The user is likely trying to cycle through all the items starting | |
330 // with this character, so just search on the character. | |
331 prefix = String(&c, 1); | |
332 } else { | |
333 m_repeatingChar = 0; | |
334 prefix = m_typedString; | |
335 searchStartOffset = 0; | |
336 } | |
337 } | |
338 | |
339 // Compute a case-folded copy of the prefix string before beginning the | |
340 // search for a matching element. This code uses foldCase to work around the | |
341 // fact that String::startWith does not fold non-ASCII characters. This code | |
342 // can be changed to use startWith once that is fixed. | |
343 String prefixWithCaseFolded(prefix.foldCase()); | |
344 int itemCount = numItems(); | |
345 int index = (max(0, m_selectedIndex) + searchStartOffset) % itemCount; | |
346 for (int i = 0; i < itemCount; i++, index = (index + 1) % itemCount) { | |
347 if (!isSelectableItem(index)) | |
348 continue; | |
349 | |
350 if (stripLeadingWhiteSpace(m_items[index]->label).foldCase().startsWith(
prefixWithCaseFolded)) { | |
351 selectIndex(index); | |
352 return; | |
353 } | |
354 } | |
355 } | |
356 | |
357 void PopupListBox::paint(GraphicsContext* gc, const IntRect& rect) | |
358 { | |
359 // Adjust coords for scrolled frame. | |
360 IntRect r = intersection(rect, frameRect()); | |
361 int tx = x() - scrollX(); | |
362 int ty = y() - scrollY(); | |
363 | |
364 r.move(-tx, -ty); | |
365 | |
366 // Set clip rect to match revised damage rect. | |
367 gc->save(); | |
368 gc->translate(static_cast<float>(tx), static_cast<float>(ty)); | |
369 gc->clip(r); | |
370 | |
371 // FIXME: Can we optimize scrolling to not require repainting the entire | |
372 // window? Should we? | |
373 for (int i = 0; i < numItems(); ++i) | |
374 paintRow(gc, r, i); | |
375 | |
376 // Special case for an empty popup. | |
377 if (!numItems()) | |
378 gc->fillRect(r, Color::white, ColorSpaceDeviceRGB); | |
379 | |
380 gc->restore(); | |
381 | |
382 ScrollView::paint(gc, rect); | |
383 } | |
384 | |
385 static const int separatorPadding = 4; | |
386 static const int separatorHeight = 1; | |
387 | |
388 void PopupListBox::paintRow(GraphicsContext* gc, const IntRect& rect, int rowInd
ex) | |
389 { | |
390 // This code is based largely on RenderListBox::paint* methods. | |
391 | |
392 IntRect rowRect = getRowBounds(rowIndex); | |
393 if (!rowRect.intersects(rect)) | |
394 return; | |
395 | |
396 PopupMenuStyle style = m_popupClient->itemStyle(rowIndex); | |
397 | |
398 // Paint background | |
399 Color backColor, textColor, labelColor; | |
400 if (rowIndex == m_selectedIndex) { | |
401 backColor = RenderTheme::defaultTheme()->activeListBoxSelectionBackgroun
dColor(); | |
402 textColor = RenderTheme::defaultTheme()->activeListBoxSelectionForegroun
dColor(); | |
403 labelColor = textColor; | |
404 } else { | |
405 backColor = style.backgroundColor(); | |
406 textColor = style.foregroundColor(); | |
407 | |
408 #if OS(LINUX) | |
409 // On other platforms, the <option> background color is the same as the | |
410 // <select> background color. On Linux, that makes the <option> | |
411 // background color very dark, so by default, try to use a lighter | |
412 // background color for <option>s. | |
413 if (style.backgroundColorType() == PopupMenuStyle::DefaultBackgroundColo
r && RenderTheme::defaultTheme()->systemColor(CSSValueButtonface) == backColor) | |
414 backColor = RenderTheme::defaultTheme()->systemColor(CSSValueMenu); | |
415 #endif | |
416 | |
417 // FIXME: for now the label color is hard-coded. It should be added to | |
418 // the PopupMenuStyle. | |
419 labelColor = Color(115, 115, 115); | |
420 } | |
421 | |
422 // If we have a transparent background, make sure it has a color to blend | |
423 // against. | |
424 if (backColor.hasAlpha()) | |
425 gc->fillRect(rowRect, Color::white, ColorSpaceDeviceRGB); | |
426 | |
427 gc->fillRect(rowRect, backColor, ColorSpaceDeviceRGB); | |
428 | |
429 // It doesn't look good but Autofill requires special style for separator. | |
430 // Autofill doesn't have padding and #dcdcdc color. | |
431 if (m_popupClient->itemIsSeparator(rowIndex)) { | |
432 int padding = style.menuType() == PopupMenuStyle::AutofillPopup ? 0 : se
paratorPadding; | |
433 IntRect separatorRect( | |
434 rowRect.x() + padding, | |
435 rowRect.y() + (rowRect.height() - separatorHeight) / 2, | |
436 rowRect.width() - 2 * padding, separatorHeight); | |
437 gc->fillRect(separatorRect, style.menuType() == PopupMenuStyle::Autofill
Popup ? Color(0xdc, 0xdc, 0xdc) : textColor, ColorSpaceDeviceRGB); | |
438 return; | |
439 } | |
440 | |
441 if (!style.isVisible()) | |
442 return; | |
443 | |
444 gc->setFillColor(textColor, ColorSpaceDeviceRGB); | |
445 | |
446 Font itemFont = getRowFont(rowIndex); | |
447 // FIXME: http://crbug.com/19872 We should get the padding of individual opt
ion | |
448 // elements. This probably implies changes to PopupMenuClient. | |
449 bool rightAligned = m_popupClient->menuStyle().textDirection() == RTL; | |
450 int textX = 0; | |
451 int maxWidth = 0; | |
452 if (rightAligned) | |
453 maxWidth = rowRect.width() - max<int>(0, m_popupClient->clientPaddingRig
ht() - m_popupClient->clientInsetRight()); | |
454 else { | |
455 textX = max<int>(0, m_popupClient->clientPaddingLeft() - m_popupClient->
clientInsetLeft()); | |
456 maxWidth = rowRect.width() - textX; | |
457 } | |
458 // Prepare text to be drawn. | |
459 String itemText = m_popupClient->itemText(rowIndex); | |
460 String itemLabel = m_popupClient->itemLabel(rowIndex); | |
461 String itemIcon = m_popupClient->itemIcon(rowIndex); | |
462 if (m_settings.restrictWidthOfListBox) { // Truncate strings to fit in. | |
463 // FIXME: We should leftTruncate for the rtl case. | |
464 // StringTruncator::leftTruncate would have to be implemented. | |
465 String str = StringTruncator::rightTruncate(itemText, maxWidth, itemFont
); | |
466 if (str != itemText) { | |
467 itemText = str; | |
468 // Don't display the label or icon, we already don't have enough | |
469 // room for the item text. | |
470 itemLabel = ""; | |
471 itemIcon = ""; | |
472 } else if (!itemLabel.isEmpty()) { | |
473 int availableWidth = maxWidth - textToLabelPadding - StringTruncator
::width(itemText, itemFont); | |
474 itemLabel = StringTruncator::rightTruncate(itemLabel, availableWidth
, itemFont); | |
475 } | |
476 } | |
477 | |
478 // Prepare the directionality to draw text. | |
479 TextRun textRun(itemText, 0, 0, TextRun::AllowTrailingExpansion, style.textD
irection(), style.hasTextDirectionOverride()); | |
480 // If the text is right-to-left, make it right-aligned by adjusting its | |
481 // beginning position. | |
482 if (rightAligned) | |
483 textX += maxWidth - itemFont.width(textRun); | |
484 | |
485 // Draw the item text. | |
486 int textY = rowRect.y() + itemFont.fontMetrics().ascent() + (rowRect.height(
) - itemFont.fontMetrics().height()) / 2; | |
487 TextRunPaintInfo textRunPaintInfo(textRun); | |
488 textRunPaintInfo.bounds = rowRect; | |
489 gc->drawBidiText(itemFont, textRunPaintInfo, IntPoint(textX, textY)); | |
490 | |
491 // We are using the left padding as the right padding includes room for the
scroll-bar which | |
492 // does not show in this case. | |
493 int rightPadding = max<int>(0, m_popupClient->clientPaddingLeft() - m_popupC
lient->clientInsetLeft()); | |
494 int remainingWidth = rowRect.width() - rightPadding; | |
495 | |
496 // Draw the icon if applicable. | |
497 RefPtr<Image> image(Image::loadPlatformResource(itemIcon.utf8().data())); | |
498 if (image && !image->isNull()) { | |
499 IntRect imageRect = image->rect(); | |
500 remainingWidth -= (imageRect.width() + labelToIconPadding); | |
501 imageRect.setX(rowRect.width() - rightPadding - imageRect.width()); | |
502 imageRect.setY(rowRect.y() + (rowRect.height() - imageRect.height()) / 2
); | |
503 gc->drawImage(image.get(), ColorSpaceDeviceRGB, imageRect); | |
504 } | |
505 | |
506 // Draw the the label if applicable. | |
507 if (itemLabel.isEmpty()) | |
508 return; | |
509 | |
510 // Autofill label is 0.9 smaller than regular font size. | |
511 if (style.menuType() == PopupMenuStyle::AutofillPopup) { | |
512 itemFont = m_popupClient->itemStyle(rowIndex).font(); | |
513 FontDescription d = itemFont.fontDescription(); | |
514 d.setComputedSize(d.computedSize() * 0.9); | |
515 itemFont = Font(d, itemFont.letterSpacing(), itemFont.wordSpacing()); | |
516 itemFont.update(0); | |
517 } | |
518 | |
519 TextRun labelTextRun(itemLabel, 0, 0, TextRun::AllowTrailingExpansion, style
.textDirection(), style.hasTextDirectionOverride()); | |
520 if (rightAligned) | |
521 textX = max<int>(0, m_popupClient->clientPaddingLeft() - m_popupClient->
clientInsetLeft()); | |
522 else | |
523 textX = remainingWidth - itemFont.width(labelTextRun); | |
524 TextRunPaintInfo labelTextRunPaintInfo(labelTextRun); | |
525 labelTextRunPaintInfo.bounds = rowRect; | |
526 | |
527 gc->setFillColor(labelColor, ColorSpaceDeviceRGB); | |
528 gc->drawBidiText(itemFont, labelTextRunPaintInfo, IntPoint(textX, textY)); | |
529 } | |
530 | |
531 Font PopupListBox::getRowFont(int rowIndex) | |
532 { | |
533 Font itemFont = m_popupClient->itemStyle(rowIndex).font(); | |
534 if (m_popupClient->itemIsLabel(rowIndex)) { | |
535 // Bold-ify labels (ie, an <optgroup> heading). | |
536 FontDescription d = itemFont.fontDescription(); | |
537 d.setWeight(FontWeightBold); | |
538 Font font(d, itemFont.letterSpacing(), itemFont.wordSpacing()); | |
539 font.update(0); | |
540 return font; | |
541 } | |
542 | |
543 return itemFont; | |
544 } | |
545 | |
546 void PopupListBox::abandon() | |
547 { | |
548 RefPtr<PopupListBox> keepAlive(this); | |
549 | |
550 m_selectedIndex = m_originalIndex; | |
551 | |
552 hidePopup(); | |
553 | |
554 if (m_acceptedIndexOnAbandon >= 0) { | |
555 if (m_popupClient) | |
556 m_popupClient->valueChanged(m_acceptedIndexOnAbandon); | |
557 m_acceptedIndexOnAbandon = -1; | |
558 } | |
559 } | |
560 | |
561 int PopupListBox::pointToRowIndex(const IntPoint& point) | |
562 { | |
563 int y = scrollY() + point.y(); | |
564 | |
565 // FIXME: binary search if perf matters. | |
566 for (int i = 0; i < numItems(); ++i) { | |
567 if (y < m_items[i]->yOffset) | |
568 return i-1; | |
569 } | |
570 | |
571 // Last item? | |
572 if (y < contentsHeight()) | |
573 return m_items.size()-1; | |
574 | |
575 return -1; | |
576 } | |
577 | |
578 bool PopupListBox::acceptIndex(int index) | |
579 { | |
580 // Clear m_acceptedIndexOnAbandon once user accepts the selected index. | |
581 if (m_acceptedIndexOnAbandon >= 0) | |
582 m_acceptedIndexOnAbandon = -1; | |
583 | |
584 if (index >= numItems()) | |
585 return false; | |
586 | |
587 if (index < 0) { | |
588 if (m_popupClient) { | |
589 // Enter pressed with no selection, just close the popup. | |
590 hidePopup(); | |
591 } | |
592 return false; | |
593 } | |
594 | |
595 if (isSelectableItem(index)) { | |
596 RefPtr<PopupListBox> keepAlive(this); | |
597 | |
598 // Hide ourselves first since valueChanged may have numerous side-effect
s. | |
599 hidePopup(); | |
600 | |
601 // Tell the <select> PopupMenuClient what index was selected. | |
602 m_popupClient->valueChanged(index); | |
603 | |
604 return true; | |
605 } | |
606 | |
607 return false; | |
608 } | |
609 | |
610 void PopupListBox::selectIndex(int index) | |
611 { | |
612 if (index < 0 || index >= numItems()) | |
613 return; | |
614 | |
615 bool isSelectable = isSelectableItem(index); | |
616 if (index != m_selectedIndex && isSelectable) { | |
617 invalidateRow(m_selectedIndex); | |
618 m_selectedIndex = index; | |
619 invalidateRow(m_selectedIndex); | |
620 | |
621 scrollToRevealSelection(); | |
622 m_popupClient->selectionChanged(m_selectedIndex); | |
623 } else if (!isSelectable) | |
624 clearSelection(); | |
625 } | |
626 | |
627 void PopupListBox::setOriginalIndex(int index) | |
628 { | |
629 m_originalIndex = m_selectedIndex = index; | |
630 } | |
631 | |
632 int PopupListBox::getRowHeight(int index) | |
633 { | |
634 int minimumHeight = PopupMenuChromium::minimumRowHeight(); | |
635 if (m_settings.deviceSupportsTouch) | |
636 minimumHeight = max(minimumHeight, PopupMenuChromium::optionRowHeightFor
Touch()); | |
637 | |
638 if (index < 0 || m_popupClient->itemStyle(index).isDisplayNone()) | |
639 return minimumHeight; | |
640 | |
641 // Separator row height is the same size as itself. | |
642 if (m_popupClient->itemIsSeparator(index)) | |
643 return max(separatorHeight, minimumHeight); | |
644 | |
645 String icon = m_popupClient->itemIcon(index); | |
646 RefPtr<Image> image(Image::loadPlatformResource(icon.utf8().data())); | |
647 | |
648 int fontHeight = getRowFont(index).fontMetrics().height(); | |
649 int iconHeight = (image && !image->isNull()) ? image->rect().height() : 0; | |
650 | |
651 int linePaddingHeight = m_popupClient->menuStyle().menuType() == PopupMenuSt
yle::AutofillPopup ? autofillLinePaddingHeight : 0; | |
652 int calculatedRowHeight = max(fontHeight, iconHeight) + linePaddingHeight *
2; | |
653 return max(calculatedRowHeight, minimumHeight); | |
654 } | |
655 | |
656 IntRect PopupListBox::getRowBounds(int index) | |
657 { | |
658 if (index < 0) | |
659 return IntRect(0, 0, visibleWidth(), getRowHeight(index)); | |
660 | |
661 return IntRect(0, m_items[index]->yOffset, visibleWidth(), getRowHeight(inde
x)); | |
662 } | |
663 | |
664 void PopupListBox::invalidateRow(int index) | |
665 { | |
666 if (index < 0) | |
667 return; | |
668 | |
669 // Invalidate in the window contents, as FramelessScrollView::invalidateRect | |
670 // paints in the window coordinates. | |
671 invalidateRect(contentsToWindow(getRowBounds(index))); | |
672 } | |
673 | |
674 void PopupListBox::scrollToRevealRow(int index) | |
675 { | |
676 if (index < 0) | |
677 return; | |
678 | |
679 IntRect rowRect = getRowBounds(index); | |
680 | |
681 if (rowRect.y() < scrollY()) { | |
682 // Row is above current scroll position, scroll up. | |
683 ScrollView::setScrollPosition(IntPoint(0, rowRect.y())); | |
684 } else if (rowRect.maxY() > scrollY() + visibleHeight()) { | |
685 // Row is below current scroll position, scroll down. | |
686 ScrollView::setScrollPosition(IntPoint(0, rowRect.maxY() - visibleHeight
())); | |
687 } | |
688 } | |
689 | |
690 bool PopupListBox::isSelectableItem(int index) | |
691 { | |
692 ASSERT(index >= 0 && index < numItems()); | |
693 return m_items[index]->type == PopupItem::TypeOption && m_popupClient->itemI
sEnabled(index); | |
694 } | |
695 | |
696 void PopupListBox::clearSelection() | |
697 { | |
698 if (m_selectedIndex != -1) { | |
699 invalidateRow(m_selectedIndex); | |
700 m_selectedIndex = -1; | |
701 m_popupClient->selectionCleared(); | |
702 } | |
703 } | |
704 | |
705 void PopupListBox::selectNextRow() | |
706 { | |
707 if (!m_settings.loopSelectionNavigation || m_selectedIndex != numItems() - 1
) { | |
708 adjustSelectedIndex(1); | |
709 return; | |
710 } | |
711 | |
712 // We are moving past the last item, no row should be selected. | |
713 clearSelection(); | |
714 } | |
715 | |
716 void PopupListBox::selectPreviousRow() | |
717 { | |
718 if (!m_settings.loopSelectionNavigation || m_selectedIndex > 0) { | |
719 adjustSelectedIndex(-1); | |
720 return; | |
721 } | |
722 | |
723 if (!m_selectedIndex) { | |
724 // We are moving past the first item, clear the selection. | |
725 clearSelection(); | |
726 return; | |
727 } | |
728 | |
729 // No row is selected, jump to the last item. | |
730 selectIndex(numItems() - 1); | |
731 scrollToRevealSelection(); | |
732 } | |
733 | |
734 void PopupListBox::adjustSelectedIndex(int delta) | |
735 { | |
736 int targetIndex = m_selectedIndex + delta; | |
737 targetIndex = std::min(std::max(targetIndex, 0), numItems() - 1); | |
738 if (!isSelectableItem(targetIndex)) { | |
739 // We didn't land on an option. Try to find one. | |
740 // We try to select the closest index to target, prioritizing any in | |
741 // the range [current, target]. | |
742 | |
743 int dir = delta > 0 ? 1 : -1; | |
744 int testIndex = m_selectedIndex; | |
745 int bestIndex = m_selectedIndex; | |
746 bool passedTarget = false; | |
747 while (testIndex >= 0 && testIndex < numItems()) { | |
748 if (isSelectableItem(testIndex)) | |
749 bestIndex = testIndex; | |
750 if (testIndex == targetIndex) | |
751 passedTarget = true; | |
752 if (passedTarget && bestIndex != m_selectedIndex) | |
753 break; | |
754 | |
755 testIndex += dir; | |
756 } | |
757 | |
758 // Pick the best index, which may mean we don't change. | |
759 targetIndex = bestIndex; | |
760 } | |
761 | |
762 // Select the new index, and ensure its visible. We do this regardless of | |
763 // whether the selection changed to ensure keyboard events always bring the | |
764 // selection into view. | |
765 selectIndex(targetIndex); | |
766 scrollToRevealSelection(); | |
767 } | |
768 | |
769 void PopupListBox::hidePopup() | |
770 { | |
771 if (parent()) { | |
772 PopupContainer* container = static_cast<PopupContainer*>(parent()); | |
773 if (container->client()) | |
774 container->client()->popupClosed(container); | |
775 container->notifyPopupHidden(); | |
776 } | |
777 | |
778 if (m_popupClient) | |
779 m_popupClient->popupDidHide(); | |
780 } | |
781 | |
782 void PopupListBox::updateFromElement() | |
783 { | |
784 clear(); | |
785 | |
786 int size = m_popupClient->listSize(); | |
787 for (int i = 0; i < size; ++i) { | |
788 PopupItem::Type type; | |
789 if (m_popupClient->itemIsSeparator(i)) | |
790 type = PopupItem::TypeSeparator; | |
791 else if (m_popupClient->itemIsLabel(i)) | |
792 type = PopupItem::TypeGroup; | |
793 else | |
794 type = PopupItem::TypeOption; | |
795 m_items.append(new PopupItem(m_popupClient->itemText(i), type)); | |
796 m_items[i]->enabled = isSelectableItem(i); | |
797 PopupMenuStyle style = m_popupClient->itemStyle(i); | |
798 m_items[i]->textDirection = style.textDirection(); | |
799 m_items[i]->hasTextDirectionOverride = style.hasTextDirectionOverride(); | |
800 } | |
801 | |
802 m_selectedIndex = m_popupClient->selectedIndex(); | |
803 setOriginalIndex(m_selectedIndex); | |
804 | |
805 layout(); | |
806 } | |
807 | |
808 void PopupListBox::setMaxWidthAndLayout(int maxWidth) | |
809 { | |
810 m_maxWindowWidth = maxWidth; | |
811 layout(); | |
812 } | |
813 | |
814 void PopupListBox::layout() | |
815 { | |
816 bool isRightAligned = m_popupClient->menuStyle().textDirection() == RTL; | |
817 | |
818 // Size our child items. | |
819 int baseWidth = 0; | |
820 int paddingWidth = 0; | |
821 int lineEndPaddingWidth = 0; | |
822 int y = 0; | |
823 for (int i = 0; i < numItems(); ++i) { | |
824 // Place the item vertically. | |
825 m_items[i]->yOffset = y; | |
826 if (m_popupClient->itemStyle(i).isDisplayNone()) | |
827 continue; | |
828 y += getRowHeight(i); | |
829 | |
830 // Ensure the popup is wide enough to fit this item. | |
831 Font itemFont = getRowFont(i); | |
832 String text = m_popupClient->itemText(i); | |
833 String label = m_popupClient->itemLabel(i); | |
834 String icon = m_popupClient->itemIcon(i); | |
835 RefPtr<Image> iconImage(Image::loadPlatformResource(icon.utf8().data()))
; | |
836 int width = 0; | |
837 if (!text.isEmpty()) | |
838 width = itemFont.width(TextRun(text)); | |
839 if (!label.isEmpty()) { | |
840 if (width > 0) | |
841 width += textToLabelPadding; | |
842 width += itemFont.width(TextRun(label)); | |
843 } | |
844 if (iconImage && !iconImage->isNull()) { | |
845 if (width > 0) | |
846 width += labelToIconPadding; | |
847 width += iconImage->rect().width(); | |
848 } | |
849 | |
850 baseWidth = max(baseWidth, width); | |
851 // FIXME: http://b/1210481 We should get the padding of individual | |
852 // option elements. | |
853 paddingWidth = max<int>(paddingWidth, | |
854 m_popupClient->clientPaddingLeft() + m_popupClient->clientPaddingRig
ht()); | |
855 lineEndPaddingWidth = max<int>(lineEndPaddingWidth, | |
856 isRightAligned ? m_popupClient->clientPaddingLeft() : m_popupClient-
>clientPaddingRight()); | |
857 } | |
858 | |
859 // Calculate scroll bar width. | |
860 int windowHeight = 0; | |
861 m_visibleRows = std::min(numItems(), maxVisibleRows); | |
862 | |
863 for (int i = 0; i < m_visibleRows; ++i) { | |
864 int rowHeight = getRowHeight(i); | |
865 | |
866 // Only clip the window height for non-Mac platforms. | |
867 if (windowHeight + rowHeight > m_maxHeight) { | |
868 m_visibleRows = i; | |
869 break; | |
870 } | |
871 | |
872 windowHeight += rowHeight; | |
873 } | |
874 | |
875 // Set our widget and scrollable contents sizes. | |
876 int scrollbarWidth = 0; | |
877 if (m_visibleRows < numItems()) { | |
878 scrollbarWidth = ScrollbarTheme::theme()->scrollbarThickness(); | |
879 | |
880 // Use minEndOfLinePadding when there is a scrollbar so that we use | |
881 // as much as (lineEndPaddingWidth - minEndOfLinePadding) padding | |
882 // space for scrollbar and allow user to use CSS padding to make the | |
883 // popup listbox align with the select element. | |
884 paddingWidth = paddingWidth - lineEndPaddingWidth + minEndOfLinePadding; | |
885 } | |
886 | |
887 int windowWidth; | |
888 int contentWidth; | |
889 if (m_settings.restrictWidthOfListBox) { | |
890 windowWidth = m_baseWidth; | |
891 contentWidth = m_baseWidth - scrollbarWidth; | |
892 } else { | |
893 windowWidth = baseWidth + scrollbarWidth + paddingWidth; | |
894 if (windowWidth > m_maxWindowWidth) { | |
895 // windowWidth exceeds m_maxWindowWidth, so we have to clip. | |
896 windowWidth = m_maxWindowWidth; | |
897 baseWidth = windowWidth - scrollbarWidth - paddingWidth; | |
898 m_baseWidth = baseWidth; | |
899 } | |
900 contentWidth = windowWidth - scrollbarWidth; | |
901 | |
902 if (windowWidth < m_baseWidth) { | |
903 windowWidth = m_baseWidth; | |
904 contentWidth = m_baseWidth - scrollbarWidth; | |
905 } else | |
906 m_baseWidth = baseWidth; | |
907 } | |
908 | |
909 resize(windowWidth, windowHeight); | |
910 setContentsSize(IntSize(contentWidth, getRowBounds(numItems() - 1).maxY())); | |
911 | |
912 if (hostWindow()) | |
913 scrollToRevealSelection(); | |
914 | |
915 invalidate(); | |
916 } | |
917 | |
918 void PopupListBox::clear() | |
919 { | |
920 for (Vector<PopupItem*>::iterator it = m_items.begin(); it != m_items.end();
++it) | |
921 delete *it; | |
922 m_items.clear(); | |
923 } | |
924 | |
925 bool PopupListBox::isPointInBounds(const IntPoint& point) | |
926 { | |
927 return numItems() && IntRect(0, 0, width(), height()).contains(point); | |
928 } | |
929 | |
930 int PopupListBox::popupContentHeight() const | |
931 { | |
932 return height(); | |
933 } | |
934 | |
935 } // namespace WebCore | |
OLD | NEW |