PopupContainer.cpp [plain text]
#include "config.h"
#include "PopupContainer.h"
#include "Chrome.h"
#include "ChromeClientChromium.h"
#include "Document.h"
#include "Frame.h"
#include "FrameView.h"
#include "FramelessScrollView.h"
#include "FramelessScrollViewClient.h"
#include "GraphicsContext.h"
#include "IntRect.h"
#include "Page.h"
#include "PlatformKeyboardEvent.h"
#include "PlatformMouseEvent.h"
#include "PlatformScreen.h"
#include "PlatformWheelEvent.h"
#include "PopupListBox.h"
#include "PopupMenuClient.h"
#include "UserGestureIndicator.h"
#include <limits>
#if ENABLE(GESTURE_EVENTS)
#include "PlatformGestureEvent.h"
#endif
#if ENABLE(TOUCH_EVENTS)
#include "PlatformTouchEvent.h"
#endif
using namespace std;
namespace WebCore {
static const int kBorderSize = 1;
static PlatformMouseEvent constructRelativeMouseEvent(const PlatformMouseEvent& e,
FramelessScrollView* parent,
FramelessScrollView* child)
{
IntPoint pos = parent->convertSelfToChild(child, e.position());
PlatformMouseEvent relativeEvent = e;
IntPoint& relativePos = const_cast<IntPoint&>(relativeEvent.position());
relativePos.setX(pos.x());
relativePos.setY(pos.y());
return relativeEvent;
}
static PlatformWheelEvent constructRelativeWheelEvent(const PlatformWheelEvent& e,
FramelessScrollView* parent,
FramelessScrollView* child)
{
IntPoint pos = parent->convertSelfToChild(child, e.position());
PlatformWheelEvent relativeEvent = e;
IntPoint& relativePos = const_cast<IntPoint&>(relativeEvent.position());
relativePos.setX(pos.x());
relativePos.setY(pos.y());
return relativeEvent;
}
PassRefPtr<PopupContainer> PopupContainer::create(PopupMenuClient* client,
PopupType popupType,
const PopupContainerSettings& settings)
{
return adoptRef(new PopupContainer(client, popupType, settings));
}
PopupContainer::PopupContainer(PopupMenuClient* client,
PopupType popupType,
const PopupContainerSettings& settings)
: m_listBox(PopupListBox::create(client, settings))
, m_settings(settings)
, m_popupType(popupType)
, m_popupOpen(false)
{
setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff);
}
PopupContainer::~PopupContainer()
{
if (m_listBox && m_listBox->parent())
removeChild(m_listBox.get());
}
IntRect PopupContainer::layoutAndCalculateWidgetRect(int targetControlHeight, const IntPoint& popupInitialCoordinate)
{
m_listBox->setMaxHeight(kMaxHeight);
m_listBox->setMaxWidth(std::numeric_limits<int>::max());
int rtlOffset = layoutAndGetRTLOffset();
bool isRTL = this->isRTL();
int rightOffset = isRTL ? rtlOffset : 0;
IntSize targetSize(m_listBox->width() + kBorderSize * 2,
m_listBox->height() + kBorderSize * 2);
IntRect widgetRectInScreen;
ChromeClientChromium* chromeClient = chromeClientChromium();
if (chromeClient) {
FloatRect screen = screenAvailableRect(m_frameView.get());
widgetRectInScreen = chromeClient->rootViewToScreen(IntRect(popupInitialCoordinate.x() + rightOffset, popupInitialCoordinate.y(), targetSize.width(), targetSize.height()));
FloatRect windowRect = chromeClient->windowRect();
if (windowRect.x() >= screen.x() && windowRect.maxX() <= screen.maxX() && (widgetRectInScreen.x() < screen.x() || widgetRectInScreen.maxX() > screen.maxX())) {
IntRect inverseWidgetRectInScreen = chromeClient->rootViewToScreen(IntRect(popupInitialCoordinate.x() + (isRTL ? 0 : rtlOffset), popupInitialCoordinate.y(), targetSize.width(), targetSize.height()));
IntRect enclosingScreen = enclosingIntRect(screen);
unsigned originalCutoff = max(enclosingScreen.x() - widgetRectInScreen.x(), 0) + max(widgetRectInScreen.maxX() - enclosingScreen.maxX(), 0);
unsigned inverseCutoff = max(enclosingScreen.x() - inverseWidgetRectInScreen.x(), 0) + max(inverseWidgetRectInScreen.maxX() - enclosingScreen.maxX(), 0);
if (inverseCutoff < originalCutoff)
widgetRectInScreen = inverseWidgetRectInScreen;
if (widgetRectInScreen.x() < screen.x()) {
unsigned widgetRight = widgetRectInScreen.maxX();
widgetRectInScreen.setWidth(widgetRectInScreen.maxX() - screen.x());
widgetRectInScreen.setX(widgetRight - widgetRectInScreen.width());
listBox()->setMaxWidthAndLayout(max(widgetRectInScreen.width() - kBorderSize * 2, 0));
} else if (widgetRectInScreen.maxX() > screen.maxX()) {
widgetRectInScreen.setWidth(screen.maxX() - widgetRectInScreen.x());
listBox()->setMaxWidthAndLayout(max(widgetRectInScreen.width() - kBorderSize * 2, 0));
}
}
if (widgetRectInScreen.maxY() > static_cast<int>(screen.maxY())) {
if (widgetRectInScreen.y() - widgetRectInScreen.height() - targetControlHeight > 0) {
widgetRectInScreen.move(0, -(widgetRectInScreen.height() + targetControlHeight));
} else {
int spaceAbove = widgetRectInScreen.y() - targetControlHeight;
int spaceBelow = screen.maxY() - widgetRectInScreen.y();
if (spaceAbove > spaceBelow)
m_listBox->setMaxHeight(spaceAbove);
else
m_listBox->setMaxHeight(spaceBelow);
layoutAndGetRTLOffset();
IntRect frameInScreen = chromeClient->rootViewToScreen(frameRect());
widgetRectInScreen.setY(frameInScreen.y());
widgetRectInScreen.setHeight(frameInScreen.height());
if (spaceAbove > spaceBelow)
widgetRectInScreen.move(0, -(widgetRectInScreen.height() + targetControlHeight));
}
}
}
return widgetRectInScreen;
}
void PopupContainer::showPopup(FrameView* view)
{
m_frameView = view;
listBox()->m_focusedNode = m_frameView->frame()->document()->focusedNode();
ChromeClientChromium* chromeClient = chromeClientChromium();
if (chromeClient) {
IntRect popupRect = m_originalFrameRect;
chromeClient->popupOpened(this, layoutAndCalculateWidgetRect(popupRect.height(), popupRect.location()), false);
m_popupOpen = true;
}
if (!m_listBox->parent())
addChild(m_listBox.get());
m_listBox->setVerticalScrollbarMode(ScrollbarAuto);
m_listBox->scrollToRevealSelection();
invalidate();
}
void PopupContainer::hidePopup()
{
listBox()->hidePopup();
}
void PopupContainer::notifyPopupHidden()
{
if (!m_popupOpen)
return;
m_popupOpen = false;
chromeClientChromium()->popupClosed(this);
}
int PopupContainer::layoutAndGetRTLOffset()
{
m_listBox->layout();
m_listBox->move(kBorderSize, kBorderSize);
int listBoxWidth = m_listBox->width() + kBorderSize * 2;
resize(listBoxWidth, m_listBox->height() + kBorderSize * 2);
invalidate();
return m_originalFrameRect.width() - listBoxWidth;
}
bool PopupContainer::handleMouseDownEvent(const PlatformMouseEvent& event)
{
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
return m_listBox->handleMouseDownEvent(
constructRelativeMouseEvent(event, this, m_listBox.get()));
}
bool PopupContainer::handleMouseMoveEvent(const PlatformMouseEvent& event)
{
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
return m_listBox->handleMouseMoveEvent(
constructRelativeMouseEvent(event, this, m_listBox.get()));
}
bool PopupContainer::handleMouseReleaseEvent(const PlatformMouseEvent& event)
{
RefPtr<PopupContainer> protect(this);
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
return m_listBox->handleMouseReleaseEvent(
constructRelativeMouseEvent(event, this, m_listBox.get()));
}
bool PopupContainer::handleWheelEvent(const PlatformWheelEvent& event)
{
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
return m_listBox->handleWheelEvent(
constructRelativeWheelEvent(event, this, m_listBox.get()));
}
#if ENABLE(TOUCH_EVENTS)
bool PopupContainer::handleTouchEvent(const PlatformTouchEvent&)
{
return false;
}
#endif
#if ENABLE(GESTURE_EVENTS)
bool PopupContainer::handleGestureEvent(const PlatformGestureEvent& gestureEvent)
{
switch (gestureEvent.type()) {
case PlatformEvent::GestureTap: {
PlatformMouseEvent fakeMouseMove(gestureEvent.position(), gestureEvent.globalPosition(), NoButton, PlatformEvent::MouseMoved, 1, gestureEvent.shiftKey(), gestureEvent.ctrlKey(), gestureEvent.altKey(), gestureEvent.metaKey(), gestureEvent.timestamp());
PlatformMouseEvent fakeMouseDown(gestureEvent.position(), gestureEvent.globalPosition(), LeftButton, PlatformEvent::MousePressed, 1, gestureEvent.shiftKey(), gestureEvent.ctrlKey(), gestureEvent.altKey(), gestureEvent.metaKey(), gestureEvent.timestamp());
PlatformMouseEvent fakeMouseUp(gestureEvent.position(), gestureEvent.globalPosition(), LeftButton, PlatformEvent::MouseReleased, 1, gestureEvent.shiftKey(), gestureEvent.ctrlKey(), gestureEvent.altKey(), gestureEvent.metaKey(), gestureEvent.timestamp());
handleMouseDownEvent(fakeMouseDown);
handleMouseReleaseEvent(fakeMouseUp);
return true;
}
case PlatformEvent::GestureDoubleTap:
break;
case PlatformEvent::GestureScrollUpdate: {
PlatformWheelEvent syntheticWheelEvent(gestureEvent.position(), gestureEvent.globalPosition(), gestureEvent.deltaX(), gestureEvent.deltaY(), gestureEvent.deltaX() / 120.0f, gestureEvent.deltaY() / 120.0f, ScrollByPixelWheelEvent, gestureEvent.shiftKey(), gestureEvent.ctrlKey(), gestureEvent.altKey(), gestureEvent.metaKey());
handleWheelEvent(syntheticWheelEvent);
return true;
}
case PlatformEvent::GestureScrollBegin:
case PlatformEvent::GestureScrollEnd:
case PlatformEvent::GestureTapDown:
break;
default:
ASSERT_NOT_REACHED();
}
return false;
}
#endif
bool PopupContainer::handleKeyEvent(const PlatformKeyboardEvent& event)
{
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
return m_listBox->handleKeyEvent(event);
}
void PopupContainer::hide()
{
m_listBox->abandon();
}
void PopupContainer::paint(GraphicsContext* gc, const IntRect& rect)
{
IntRect r = intersection(rect, frameRect());
int tx = x();
int ty = y();
r.move(-tx, -ty);
gc->translate(static_cast<float>(tx), static_cast<float>(ty));
m_listBox->paint(gc, r);
gc->translate(-static_cast<float>(tx), -static_cast<float>(ty));
paintBorder(gc, rect);
}
void PopupContainer::paintBorder(GraphicsContext* gc, const IntRect& rect)
{
Color borderColor(127, 157, 185);
gc->setStrokeStyle(NoStroke);
gc->setFillColor(borderColor, ColorSpaceDeviceRGB);
int tx = x();
int ty = y();
gc->drawRect(IntRect(tx, ty, width(), kBorderSize));
gc->drawRect(IntRect(tx, ty, kBorderSize, height()));
gc->drawRect(IntRect(tx, ty + height() - kBorderSize, width(), kBorderSize));
gc->drawRect(IntRect(tx + width() - kBorderSize, ty, kBorderSize, height()));
}
bool PopupContainer::isInterestedInEventForKey(int keyCode)
{
return m_listBox->isInterestedInEventForKey(keyCode);
}
ChromeClientChromium* PopupContainer::chromeClientChromium()
{
return static_cast<ChromeClientChromium*>(m_frameView->frame()->page()->chrome()->client());
}
void PopupContainer::showInRect(const IntRect& r, FrameView* v, int index)
{
listBox()->setBaseWidth(max(r.width() - kBorderSize * 2, 0));
listBox()->updateFromElement();
IntPoint location = v->contentsToWindow(r.location());
location.move(0, r.height());
m_originalFrameRect = IntRect(location, r.size());
setFrameRect(IntRect(IntPoint(), r.size()));
showPopup(v);
}
IntRect PopupContainer::refresh(const IntRect& targetControlRect)
{
listBox()->setBaseWidth(max(m_originalFrameRect.width() - kBorderSize * 2, 0));
listBox()->updateFromElement();
IntPoint locationInWindow = m_frameView->contentsToWindow(targetControlRect.location());
locationInWindow.move(0, targetControlRect.height());
IntRect widgetRectInScreen = layoutAndCalculateWidgetRect(targetControlRect.height(), locationInWindow);
if (size() != widgetRectInScreen.size())
resize(widgetRectInScreen.size());
invalidate();
return widgetRectInScreen;
}
inline bool PopupContainer::isRTL() const
{
return m_listBox->m_popupClient->menuStyle().textDirection() == RTL;
}
int PopupContainer::selectedIndex() const
{
return m_listBox->selectedIndex();
}
int PopupContainer::menuItemHeight() const
{
return m_listBox->getRowHeight(0);
}
int PopupContainer::menuItemFontSize() const
{
return m_listBox->getRowFont(0).size();
}
PopupMenuStyle PopupContainer::menuStyle() const
{
return m_listBox->m_popupClient->menuStyle();
}
const WTF::Vector<PopupItem*>& PopupContainer:: popupData() const
{
return m_listBox->items();
}
String PopupContainer::getSelectedItemToolTip()
{
return listBox()->m_popupClient->itemToolTip(listBox()->m_selectedIndex);
}
}