This source file includes following definitions.
- m_maxWindowWidth
- handleMouseDownEvent
- handleMouseMoveEvent
- handleMouseReleaseEvent
- handleWheelEvent
- isInterestedInEventForKey
- handleTouchEvent
- handleGestureEvent
- isCharacterTypeEvent
- handleKeyEvent
- hostWindow
- shouldPlaceVerticalScrollbarOnLeft
- stripLeadingWhiteSpace
- typeAheadFind
- paint
- paintRow
- getRowFont
- abandon
- pointToRowIndex
- acceptIndex
- selectIndex
- setOriginalIndex
- getRowHeight
- getRowBounds
- invalidateRow
- scrollToRevealRow
- isSelectableItem
- clearSelection
- selectNextRow
- selectPreviousRow
- adjustSelectedIndex
- hidePopup
- updateFromElement
- setMaxWidthAndLayout
- layout
- clear
- isPointInBounds
- popupContentHeight
#include "config.h"
#include "PopupListBox.h"
#include "CSSValueKeywords.h"
#include "PopupContainer.h"
#include "PopupMenuChromium.h"
#include "RuntimeEnabledFeatures.h"
#include "core/rendering/RenderTheme.h"
#include "platform/KeyboardCodes.h"
#include "platform/PlatformGestureEvent.h"
#include "platform/PlatformKeyboardEvent.h"
#include "platform/PlatformMouseEvent.h"
#include "platform/PlatformScreen.h"
#include "platform/PlatformTouchEvent.h"
#include "platform/PlatformWheelEvent.h"
#include "platform/PopupMenuClient.h"
#include "platform/fonts/Font.h"
#include "platform/fonts/FontCache.h"
#include "platform/fonts/FontSelector.h"
#include "platform/geometry/IntRect.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/scroll/FramelessScrollViewClient.h"
#include "platform/scroll/ScrollbarTheme.h"
#include "platform/text/StringTruncator.h"
#include "platform/text/TextRun.h"
#include "wtf/ASCIICType.h"
#include "wtf/CurrentTime.h"
#include <limits>
namespace blink {
using namespace WTF::Unicode;
using namespace WebCore;
const int PopupListBox::defaultMaxHeight = 500;
static const int maxVisibleRows = 20;
static const int minEndOfLinePadding = 2;
static const TimeStamp typeAheadTimeoutMs = 1000;
PopupListBox::PopupListBox(PopupMenuClient* client, bool deviceSupportsTouch)
: m_deviceSupportsTouch(deviceSupportsTouch)
, m_originalIndex(0)
, m_selectedIndex(0)
, m_acceptedIndexOnAbandon(-1)
, m_visibleRows(0)
, m_baseWidth(0)
, m_maxHeight(defaultMaxHeight)
, m_popupClient(client)
, m_repeatingChar(0)
, m_lastCharTime(0)
, m_maxWindowWidth(std::numeric_limits<int>::max())
{
setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff);
}
bool PopupListBox::handleMouseDownEvent(const PlatformMouseEvent& event)
{
Scrollbar* scrollbar = scrollbarAtPoint(event.position());
if (scrollbar) {
m_capturingScrollbar = scrollbar;
m_capturingScrollbar->mouseDown(event);
return true;
}
if (!isPointInBounds(event.position()))
abandon();
return true;
}
bool PopupListBox::handleMouseMoveEvent(const PlatformMouseEvent& event)
{
if (m_capturingScrollbar) {
m_capturingScrollbar->mouseMoved(event);
return true;
}
Scrollbar* scrollbar = scrollbarAtPoint(event.position());
if (m_lastScrollbarUnderMouse != scrollbar) {
if (m_lastScrollbarUnderMouse)
m_lastScrollbarUnderMouse->mouseExited();
m_lastScrollbarUnderMouse = scrollbar;
}
if (scrollbar) {
scrollbar->mouseMoved(event);
return true;
}
if (!isPointInBounds(event.position()))
return false;
selectIndex(pointToRowIndex(event.position()));
return true;
}
bool PopupListBox::handleMouseReleaseEvent(const PlatformMouseEvent& event)
{
if (m_capturingScrollbar) {
m_capturingScrollbar->mouseUp(event);
m_capturingScrollbar = nullptr;
return true;
}
if (!isPointInBounds(event.position()))
return true;
if (acceptIndex(pointToRowIndex(event.position())) && m_focusedElement) {
m_focusedElement->dispatchMouseEvent(event, EventTypeNames::mouseup);
m_focusedElement->dispatchMouseEvent(event, EventTypeNames::click);
m_focusedElement = nullptr;
}
return true;
}
bool PopupListBox::handleWheelEvent(const PlatformWheelEvent& event)
{
if (!isPointInBounds(event.position())) {
abandon();
return true;
}
ScrollableArea::handleWheelEvent(event);
return true;
}
bool PopupListBox::isInterestedInEventForKey(int keyCode)
{
switch (keyCode) {
case VKEY_ESCAPE:
case VKEY_RETURN:
case VKEY_UP:
case VKEY_DOWN:
case VKEY_PRIOR:
case VKEY_NEXT:
case VKEY_HOME:
case VKEY_END:
case VKEY_TAB:
return true;
default:
return false;
}
}
bool PopupListBox::handleTouchEvent(const PlatformTouchEvent&)
{
return false;
}
bool PopupListBox::handleGestureEvent(const PlatformGestureEvent&)
{
return false;
}
static bool isCharacterTypeEvent(const PlatformKeyboardEvent& event)
{
return event.type() == PlatformEvent::Char;
}
bool PopupListBox::handleKeyEvent(const PlatformKeyboardEvent& event)
{
if (event.type() == PlatformEvent::KeyUp)
return true;
if (!numItems() && event.windowsVirtualKeyCode() != VKEY_ESCAPE)
return true;
switch (event.windowsVirtualKeyCode()) {
case VKEY_ESCAPE:
abandon();
return true;
case VKEY_RETURN:
if (m_selectedIndex == -1) {
hidePopup();
return false;
}
acceptIndex(m_selectedIndex);
return true;
case VKEY_UP:
selectPreviousRow();
break;
case VKEY_DOWN:
selectNextRow();
break;
case VKEY_PRIOR:
adjustSelectedIndex(-m_visibleRows);
break;
case VKEY_NEXT:
adjustSelectedIndex(m_visibleRows);
break;
case VKEY_HOME:
adjustSelectedIndex(-m_selectedIndex);
break;
case VKEY_END:
adjustSelectedIndex(m_items.size());
break;
default:
if (!event.ctrlKey() && !event.altKey() && !event.metaKey()
&& isPrintableChar(event.windowsVirtualKeyCode())
&& isCharacterTypeEvent(event))
typeAheadFind(event);
break;
}
if (m_originalIndex != m_selectedIndex) {
m_acceptedIndexOnAbandon = m_selectedIndex;
setOriginalIndex(m_selectedIndex);
m_popupClient->setTextFromItem(m_selectedIndex);
}
if (event.windowsVirtualKeyCode() == VKEY_TAB) {
if (m_selectedIndex >= 0) {
acceptIndex(m_selectedIndex);
return false;
}
abandon();
return false;
}
return true;
}
HostWindow* PopupListBox::hostWindow() const
{
return parent() ? parent()->hostWindow() : 0;
}
bool PopupListBox::shouldPlaceVerticalScrollbarOnLeft() const
{
return m_popupClient->menuStyle().textDirection() == RTL;
}
static String stripLeadingWhiteSpace(const String& string)
{
int length = string.length();
int i;
for (i = 0; i < length; ++i)
if (string[i] != noBreakSpace
&& !isSpaceOrNewline(string[i]))
break;
return string.substring(i, length - i);
}
void PopupListBox::typeAheadFind(const PlatformKeyboardEvent& event)
{
TimeStamp now = static_cast<TimeStamp>(currentTime() * 1000.0f);
TimeStamp delta = now - m_lastCharTime;
m_lastCharTime = now;
UChar c = event.windowsVirtualKeyCode();
String prefix;
int searchStartOffset = 1;
if (delta > typeAheadTimeoutMs) {
m_typedString = prefix = String(&c, 1);
m_repeatingChar = c;
} else {
m_typedString.append(c);
if (c == m_repeatingChar) {
prefix = String(&c, 1);
} else {
m_repeatingChar = 0;
prefix = m_typedString;
searchStartOffset = 0;
}
}
String prefixWithCaseFolded(prefix.foldCase());
int itemCount = numItems();
int index = (max(0, m_selectedIndex) + searchStartOffset) % itemCount;
for (int i = 0; i < itemCount; i++, index = (index + 1) % itemCount) {
if (!isSelectableItem(index))
continue;
if (stripLeadingWhiteSpace(m_items[index]->label).foldCase().startsWith(prefixWithCaseFolded)) {
selectIndex(index);
return;
}
}
}
void PopupListBox::paint(GraphicsContext* gc, const IntRect& rect)
{
IntRect r = intersection(rect, frameRect());
int tx = x() - scrollX() + ((shouldPlaceVerticalScrollbarOnLeft() && verticalScrollbar()) ? verticalScrollbar()->width() : 0);
int ty = y() - scrollY();
r.move(-tx, -ty);
gc->save();
gc->translate(static_cast<float>(tx), static_cast<float>(ty));
gc->clip(r);
for (int i = 0; i < numItems(); ++i)
paintRow(gc, r, i);
if (!numItems())
gc->fillRect(r, Color::white);
gc->restore();
ScrollView::paint(gc, rect);
}
static const int separatorPadding = 4;
static const int separatorHeight = 1;
static const int minRowHeight = 0;
static const int optionRowHeightForTouch = 28;
void PopupListBox::paintRow(GraphicsContext* gc, const IntRect& rect, int rowIndex)
{
IntRect rowRect = getRowBounds(rowIndex);
if (!rowRect.intersects(rect))
return;
PopupMenuStyle style = m_popupClient->itemStyle(rowIndex);
Color backColor, textColor, labelColor;
if (rowIndex == m_selectedIndex) {
backColor = RenderTheme::theme().activeListBoxSelectionBackgroundColor();
textColor = RenderTheme::theme().activeListBoxSelectionForegroundColor();
labelColor = textColor;
} else {
backColor = style.backgroundColor();
textColor = style.foregroundColor();
#if OS(LINUX) || OS(ANDROID)
if (style.backgroundColorType() == PopupMenuStyle::DefaultBackgroundColor && RenderTheme::theme().systemColor(CSSValueButtonface) == backColor)
backColor = RenderTheme::theme().systemColor(CSSValueMenu);
#endif
labelColor = Color(115, 115, 115);
}
if (backColor.hasAlpha())
gc->fillRect(rowRect, Color::white);
gc->fillRect(rowRect, backColor);
if (m_popupClient->itemIsSeparator(rowIndex)) {
IntRect separatorRect(
rowRect.x() + separatorPadding,
rowRect.y() + (rowRect.height() - separatorHeight) / 2,
rowRect.width() - 2 * separatorPadding, separatorHeight);
gc->fillRect(separatorRect, textColor);
return;
}
if (!style.isVisible())
return;
gc->setFillColor(textColor);
FontCachePurgePreventer fontCachePurgePreventer;
Font itemFont = getRowFont(rowIndex);
bool rightAligned = m_popupClient->menuStyle().textDirection() == RTL;
int textX = 0;
int maxWidth = 0;
if (rightAligned) {
maxWidth = rowRect.width() - max<int>(0, m_popupClient->clientPaddingRight());
} else {
textX = max<int>(0, m_popupClient->clientPaddingLeft());
maxWidth = rowRect.width() - textX;
}
String itemText = m_popupClient->itemText(rowIndex);
TextRun textRun(itemText, 0, 0, TextRun::AllowTrailingExpansion, style.textDirection(), style.hasTextDirectionOverride());
if (rightAligned)
textX += maxWidth - itemFont.width(textRun);
int textY = rowRect.y() + itemFont.fontMetrics().ascent() + (rowRect.height() - itemFont.fontMetrics().height()) / 2;
TextRunPaintInfo textRunPaintInfo(textRun);
textRunPaintInfo.bounds = rowRect;
gc->drawBidiText(itemFont, textRunPaintInfo, IntPoint(textX, textY));
}
Font PopupListBox::getRowFont(int rowIndex)
{
Font itemFont = m_popupClient->itemStyle(rowIndex).font();
if (m_popupClient->itemIsLabel(rowIndex)) {
FontDescription d = itemFont.fontDescription();
d.setWeight(FontWeightBold);
Font font(d);
font.update(nullptr);
return font;
}
return itemFont;
}
void PopupListBox::abandon()
{
RefPtr<PopupListBox> keepAlive(this);
m_selectedIndex = m_originalIndex;
hidePopup();
if (m_acceptedIndexOnAbandon >= 0) {
if (m_popupClient)
m_popupClient->valueChanged(m_acceptedIndexOnAbandon);
m_acceptedIndexOnAbandon = -1;
}
}
int PopupListBox::pointToRowIndex(const IntPoint& point)
{
int y = scrollY() + point.y();
for (int i = 0; i < numItems(); ++i) {
if (y < m_items[i]->yOffset)
return i-1;
}
if (y < contentsHeight())
return m_items.size()-1;
return -1;
}
bool PopupListBox::acceptIndex(int index)
{
if (m_acceptedIndexOnAbandon >= 0)
m_acceptedIndexOnAbandon = -1;
if (index >= numItems())
return false;
if (index < 0) {
if (m_popupClient) {
hidePopup();
}
return false;
}
if (isSelectableItem(index)) {
RefPtr<PopupListBox> keepAlive(this);
hidePopup();
m_popupClient->valueChanged(index);
return true;
}
return false;
}
void PopupListBox::selectIndex(int index)
{
if (index < 0 || index >= numItems())
return;
bool isSelectable = isSelectableItem(index);
if (index != m_selectedIndex && isSelectable) {
invalidateRow(m_selectedIndex);
m_selectedIndex = index;
invalidateRow(m_selectedIndex);
scrollToRevealSelection();
m_popupClient->selectionChanged(m_selectedIndex);
} else if (!isSelectable)
clearSelection();
}
void PopupListBox::setOriginalIndex(int index)
{
m_originalIndex = m_selectedIndex = index;
}
int PopupListBox::getRowHeight(int index)
{
int minimumHeight = m_deviceSupportsTouch ? optionRowHeightForTouch : minRowHeight;
if (index < 0 || m_popupClient->itemStyle(index).isDisplayNone())
return minimumHeight;
if (m_popupClient->itemIsSeparator(index))
return max(separatorHeight, minimumHeight);
int fontHeight = getRowFont(index).fontMetrics().height();
return max(fontHeight, minimumHeight);
}
IntRect PopupListBox::getRowBounds(int index)
{
if (index < 0)
return IntRect(0, 0, visibleWidth(), getRowHeight(index));
return IntRect(0, m_items[index]->yOffset, visibleWidth(), getRowHeight(index));
}
void PopupListBox::invalidateRow(int index)
{
if (index < 0)
return;
IntRect clipRect = contentsToWindow(getRowBounds(index));
if (shouldPlaceVerticalScrollbarOnLeft() && verticalScrollbar())
clipRect.move(verticalScrollbar()->width(), 0);
invalidateRect(clipRect);
}
void PopupListBox::scrollToRevealRow(int index)
{
if (index < 0)
return;
IntRect rowRect = getRowBounds(index);
if (rowRect.y() < scrollY()) {
ScrollView::setScrollPosition(IntPoint(0, rowRect.y()));
} else if (rowRect.maxY() > scrollY() + visibleHeight()) {
ScrollView::setScrollPosition(IntPoint(0, rowRect.maxY() - visibleHeight()));
}
}
bool PopupListBox::isSelectableItem(int index)
{
ASSERT(index >= 0 && index < numItems());
return m_items[index]->type == PopupItem::TypeOption && m_popupClient->itemIsEnabled(index);
}
void PopupListBox::clearSelection()
{
if (m_selectedIndex != -1) {
invalidateRow(m_selectedIndex);
m_selectedIndex = -1;
m_popupClient->selectionCleared();
}
}
void PopupListBox::selectNextRow()
{
adjustSelectedIndex(1);
}
void PopupListBox::selectPreviousRow()
{
adjustSelectedIndex(-1);
}
void PopupListBox::adjustSelectedIndex(int delta)
{
int targetIndex = m_selectedIndex + delta;
targetIndex = std::min(std::max(targetIndex, 0), numItems() - 1);
if (!isSelectableItem(targetIndex)) {
int dir = delta > 0 ? 1 : -1;
int testIndex = m_selectedIndex;
int bestIndex = m_selectedIndex;
bool passedTarget = false;
while (testIndex >= 0 && testIndex < numItems()) {
if (isSelectableItem(testIndex))
bestIndex = testIndex;
if (testIndex == targetIndex)
passedTarget = true;
if (passedTarget && bestIndex != m_selectedIndex)
break;
testIndex += dir;
}
targetIndex = bestIndex;
}
selectIndex(targetIndex);
scrollToRevealSelection();
}
void PopupListBox::hidePopup()
{
if (parent()) {
PopupContainer* container = static_cast<PopupContainer*>(parent());
if (container->client())
container->client()->popupClosed(container);
container->notifyPopupHidden();
}
if (m_popupClient)
m_popupClient->popupDidHide();
}
void PopupListBox::updateFromElement()
{
clear();
int size = m_popupClient->listSize();
for (int i = 0; i < size; ++i) {
PopupItem::Type type;
if (m_popupClient->itemIsSeparator(i))
type = PopupItem::TypeSeparator;
else if (m_popupClient->itemIsLabel(i))
type = PopupItem::TypeGroup;
else
type = PopupItem::TypeOption;
m_items.append(new PopupItem(m_popupClient->itemText(i), type));
m_items[i]->enabled = isSelectableItem(i);
PopupMenuStyle style = m_popupClient->itemStyle(i);
m_items[i]->textDirection = style.textDirection();
m_items[i]->hasTextDirectionOverride = style.hasTextDirectionOverride();
}
m_selectedIndex = m_popupClient->selectedIndex();
setOriginalIndex(m_selectedIndex);
layout();
}
void PopupListBox::setMaxWidthAndLayout(int maxWidth)
{
m_maxWindowWidth = maxWidth;
layout();
}
void PopupListBox::layout()
{
bool isRightAligned = m_popupClient->menuStyle().textDirection() == RTL;
int baseWidth = 0;
int paddingWidth = 0;
int lineEndPaddingWidth = 0;
int y = 0;
for (int i = 0; i < numItems(); ++i) {
m_items[i]->yOffset = y;
if (m_popupClient->itemStyle(i).isDisplayNone())
continue;
y += getRowHeight(i);
Font itemFont = getRowFont(i);
String text = m_popupClient->itemText(i);
int width = 0;
if (!text.isEmpty())
width = itemFont.width(TextRun(text));
baseWidth = max(baseWidth, width);
paddingWidth = max<int>(paddingWidth,
m_popupClient->clientPaddingLeft() + m_popupClient->clientPaddingRight());
lineEndPaddingWidth = max<int>(lineEndPaddingWidth,
isRightAligned ? m_popupClient->clientPaddingLeft() : m_popupClient->clientPaddingRight());
}
int windowHeight = 0;
m_visibleRows = std::min(numItems(), maxVisibleRows);
for (int i = 0; i < m_visibleRows; ++i) {
int rowHeight = getRowHeight(i);
if (windowHeight + rowHeight > m_maxHeight) {
m_visibleRows = i;
break;
}
windowHeight += rowHeight;
}
int scrollbarWidth = 0;
if (m_visibleRows < numItems()) {
scrollbarWidth = ScrollbarTheme::theme()->scrollbarThickness();
paddingWidth = paddingWidth - lineEndPaddingWidth + minEndOfLinePadding;
}
int windowWidth = baseWidth + scrollbarWidth + paddingWidth;
if (windowWidth > m_maxWindowWidth) {
windowWidth = m_maxWindowWidth;
baseWidth = windowWidth - scrollbarWidth - paddingWidth;
m_baseWidth = baseWidth;
}
int contentWidth = windowWidth - scrollbarWidth;
if (windowWidth < m_baseWidth) {
windowWidth = m_baseWidth;
contentWidth = m_baseWidth - scrollbarWidth;
} else {
m_baseWidth = baseWidth;
}
resize(windowWidth, windowHeight);
setContentsSize(IntSize(contentWidth, getRowBounds(numItems() - 1).maxY()));
if (hostWindow())
scrollToRevealSelection();
invalidate();
}
void PopupListBox::clear()
{
for (Vector<PopupItem*>::iterator it = m_items.begin(); it != m_items.end(); ++it)
delete *it;
m_items.clear();
}
bool PopupListBox::isPointInBounds(const IntPoint& point)
{
return numItems() && IntRect(0, 0, width(), height()).contains(point);
}
int PopupListBox::popupContentHeight() const
{
return height();
}
}