2 * Copyright (C) 2009 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.
32 #include "web/WebPopupMenuImpl.h"
34 #include "core/frame/FrameView.h"
35 #include "platform/Cursor.h"
36 #include "platform/NotImplemented.h"
37 #include "platform/PlatformGestureEvent.h"
38 #include "platform/PlatformKeyboardEvent.h"
39 #include "platform/PlatformMouseEvent.h"
40 #include "platform/PlatformWheelEvent.h"
41 #include "platform/geometry/IntRect.h"
42 #include "platform/graphics/GraphicsContext.h"
43 #include "platform/graphics/skia/SkiaUtils.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/WebCompositorSupport.h"
46 #include "public/platform/WebContentLayer.h"
47 #include "public/platform/WebFloatRect.h"
48 #include "public/platform/WebLayerTreeView.h"
49 #include "public/platform/WebRect.h"
50 #include "public/web/WebInputEvent.h"
51 #include "public/web/WebRange.h"
52 #include "public/web/WebViewClient.h"
53 #include "public/web/WebWidgetClient.h"
54 #include "web/PopupContainer.h"
55 #include "web/PopupMenuChromium.h"
56 #include "web/WebInputEventConversion.h"
57 #include <skia/ext/platform_canvas.h>
61 // WebPopupMenu ---------------------------------------------------------------
63 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
65 // Pass the WebPopupMenuImpl's self-reference to the caller.
66 return adoptRef(new WebPopupMenuImpl(client)).leakRef();
69 // WebWidget ------------------------------------------------------------------
71 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
74 // Set to impossible point so we always get the first mouse position.
75 , m_lastMousePosition(WebPoint(-1, -1))
80 WebPopupMenuImpl::~WebPopupMenuImpl()
83 m_widget->setClient(0);
86 void WebPopupMenuImpl::willCloseLayerTreeView()
91 void WebPopupMenuImpl::initialize(PopupContainer* widget, const WebRect& bounds)
94 m_widget->setClient(this);
98 m_client->setWindowRect(bounds);
99 m_client->show(WebNavigationPolicy()); // Policy is ignored.
101 m_client->initializeLayerTreeView();
102 m_layerTreeView = m_client->layerTreeView();
103 if (m_layerTreeView) {
104 m_layerTreeView->setVisible(true);
105 m_layerTreeView->setDeviceScaleFactor(m_client->deviceScaleFactor());
106 m_rootLayer = adoptPtr(Platform::current()->compositorSupport()->createContentLayer(this));
107 m_rootLayer->layer()->setBounds(m_size);
108 m_layerTreeView->setRootLayer(*m_rootLayer->layer());
112 void WebPopupMenuImpl::handleMouseMove(const WebMouseEvent& event)
114 // Don't send mouse move messages if the mouse hasn't moved.
115 if (event.x != m_lastMousePosition.x || event.y != m_lastMousePosition.y) {
116 m_lastMousePosition = WebPoint(event.x, event.y);
117 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
119 // We cannot call setToolTipText() in PopupContainer, because PopupContainer is in WebCore, and we cannot refer to WebKit from Webcore.
120 PopupContainer* container = static_cast<PopupContainer*>(m_widget);
121 client()->setToolTipText(container->getSelectedItemToolTip(), toWebTextDirection(container->menuStyle().textDirection()));
125 void WebPopupMenuImpl::handleMouseLeave(const WebMouseEvent& event)
127 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
130 void WebPopupMenuImpl::handleMouseDown(const WebMouseEvent& event)
132 m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
135 void WebPopupMenuImpl::handleMouseUp(const WebMouseEvent& event)
138 m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
141 void WebPopupMenuImpl::handleMouseWheel(const WebMouseWheelEvent& event)
143 m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
146 bool WebPopupMenuImpl::handleGestureEvent(const WebGestureEvent& event)
148 return m_widget->handleGestureEvent(PlatformGestureEventBuilder(m_widget, event));
151 bool WebPopupMenuImpl::handleTouchEvent(const WebTouchEvent& event)
154 PlatformTouchEventBuilder touchEventBuilder(m_widget, event);
155 bool defaultPrevented(m_widget->handleTouchEvent(touchEventBuilder));
156 return defaultPrevented;
159 bool WebPopupMenuImpl::handleKeyEvent(const WebKeyboardEvent& event)
161 return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
164 // WebWidget -------------------------------------------------------------------
166 void WebPopupMenuImpl::close()
173 deref(); // Balances ref() from WebPopupMenu::create.
176 void WebPopupMenuImpl::willStartLiveResize()
180 void WebPopupMenuImpl::resize(const WebSize& newSize)
182 if (m_size == newSize)
187 IntRect newGeometry(0, 0, m_size.width, m_size.height);
188 m_widget->setFrameRect(newGeometry);
192 WebRect damagedRect(0, 0, m_size.width, m_size.height);
193 m_client->didInvalidateRect(damagedRect);
197 m_rootLayer->layer()->setBounds(newSize);
200 void WebPopupMenuImpl::willEndLiveResize()
204 void WebPopupMenuImpl::beginFrame(const WebBeginFrameArgs&)
208 void WebPopupMenuImpl::layout()
212 void WebPopupMenuImpl::paintContents(WebCanvas* canvas, const WebRect& rect, bool, WebContentLayerClient::GraphicsContextStatus contextStatus)
217 if (!rect.isEmpty()) {
218 GraphicsContext context(canvas,
219 contextStatus == WebContentLayerClient::GraphicsContextEnabled ? GraphicsContext::NothingDisabled : GraphicsContext::FullyDisabled);
220 m_widget->paint(&context, rect);
224 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect)
229 if (!rect.isEmpty()) {
230 GraphicsContext context(canvas);
231 context.applyDeviceScaleFactor(m_client->deviceScaleFactor());
232 m_widget->paint(&context, rect);
236 void WebPopupMenuImpl::themeChanged()
241 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
246 // FIXME: WebKit seems to always return false on mouse events methods. For
247 // now we'll assume it has processed them (as we are only interested in
248 // whether keyboard events are processed).
249 switch (inputEvent.type) {
250 case WebInputEvent::MouseMove:
251 handleMouseMove(static_cast<const WebMouseEvent&>(inputEvent));
254 case WebInputEvent::MouseLeave:
255 handleMouseLeave(static_cast<const WebMouseEvent&>(inputEvent));
258 case WebInputEvent::MouseWheel:
259 handleMouseWheel(static_cast<const WebMouseWheelEvent&>(inputEvent));
262 case WebInputEvent::MouseDown:
263 handleMouseDown(static_cast<const WebMouseEvent&>(inputEvent));
266 case WebInputEvent::MouseUp:
267 handleMouseUp(static_cast<const WebMouseEvent&>(inputEvent));
270 // In Windows, RawKeyDown only has information about the physical key, but
271 // for "selection", we need the information about the character the key
272 // translated into. For English, the physical key value and the character
273 // value are the same, hence, "selection" works for English. But for other
274 // languages, such as Hebrew, the character value is different from the
275 // physical key value. Thus, without accepting Char event type which
276 // contains the key's character value, the "selection" won't work for
277 // non-English languages, such as Hebrew.
278 case WebInputEvent::RawKeyDown:
279 case WebInputEvent::KeyDown:
280 case WebInputEvent::KeyUp:
281 case WebInputEvent::Char:
282 return handleKeyEvent(static_cast<const WebKeyboardEvent&>(inputEvent));
284 case WebInputEvent::TouchStart:
285 case WebInputEvent::TouchMove:
286 case WebInputEvent::TouchEnd:
287 case WebInputEvent::TouchCancel:
288 return handleTouchEvent(static_cast<const WebTouchEvent&>(inputEvent));
290 case WebInputEvent::GestureScrollBegin:
291 case WebInputEvent::GestureScrollEnd:
292 case WebInputEvent::GestureScrollUpdate:
293 case WebInputEvent::GestureScrollUpdateWithoutPropagation:
294 case WebInputEvent::GestureFlingStart:
295 case WebInputEvent::GestureFlingCancel:
296 case WebInputEvent::GestureTap:
297 case WebInputEvent::GestureTapUnconfirmed:
298 case WebInputEvent::GestureTapDown:
299 case WebInputEvent::GestureShowPress:
300 case WebInputEvent::GestureTapCancel:
301 case WebInputEvent::GestureDoubleTap:
302 case WebInputEvent::GestureTwoFingerTap:
303 case WebInputEvent::GestureLongPress:
304 case WebInputEvent::GestureLongTap:
305 case WebInputEvent::GesturePinchBegin:
306 case WebInputEvent::GesturePinchEnd:
307 case WebInputEvent::GesturePinchUpdate:
308 return handleGestureEvent(static_cast<const WebGestureEvent&>(inputEvent));
310 case WebInputEvent::Undefined:
311 case WebInputEvent::MouseEnter:
312 case WebInputEvent::ContextMenu:
318 void WebPopupMenuImpl::mouseCaptureLost()
322 void WebPopupMenuImpl::setFocus(bool)
326 bool WebPopupMenuImpl::setComposition(const WebString&, const WebVector<WebCompositionUnderline>&, int, int)
331 bool WebPopupMenuImpl::confirmComposition()
336 bool WebPopupMenuImpl::confirmComposition(ConfirmCompositionBehavior)
341 bool WebPopupMenuImpl::confirmComposition(const WebString&)
346 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
353 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
360 void WebPopupMenuImpl::setTextDirection(WebTextDirection)
365 //-----------------------------------------------------------------------------
368 void WebPopupMenuImpl::invalidateContentsAndRootView(const IntRect& paintRect)
370 if (paintRect.isEmpty())
373 m_client->didInvalidateRect(paintRect);
375 m_rootLayer->layer()->invalidateRect(paintRect);
378 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect)
380 invalidateContentsAndRootView(updateRect);
383 void WebPopupMenuImpl::scheduleAnimation()
387 IntRect WebPopupMenuImpl::rootViewToScreen(const IntRect& rect) const
393 WebScreenInfo WebPopupMenuImpl::screenInfo() const
395 return WebScreenInfo();
398 void WebPopupMenuImpl::popupClosed(PopupContainer* widget)
400 ASSERT(widget == m_widget);
402 m_widget->setClient(0);
406 m_client->closeWidgetSoon();