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 "platform/scroll/FramelessScrollView.h"
45 #include "public/platform/Platform.h"
46 #include "public/platform/WebCompositorSupport.h"
47 #include "public/platform/WebContentLayer.h"
48 #include "public/platform/WebFloatRect.h"
49 #include "public/platform/WebLayerTreeView.h"
50 #include "public/platform/WebRect.h"
51 #include "public/web/WebInputEvent.h"
52 #include "public/web/WebRange.h"
53 #include "public/web/WebViewClient.h"
54 #include "public/web/WebWidgetClient.h"
55 #include "web/PopupContainer.h"
56 #include "web/PopupMenuChromium.h"
57 #include "web/WebInputEventConversion.h"
58 #include <skia/ext/platform_canvas.h>
62 // WebPopupMenu ---------------------------------------------------------------
64 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
66 // Pass the WebPopupMenuImpl's self-reference to the caller.
67 return adoptRef(new WebPopupMenuImpl(client)).leakRef();
70 // WebWidget ------------------------------------------------------------------
72 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
75 // Set to impossible point so we always get the first mouse position.
76 , m_lastMousePosition(WebPoint(-1, -1))
81 WebPopupMenuImpl::~WebPopupMenuImpl()
84 m_widget->setClient(0);
87 void WebPopupMenuImpl::willCloseLayerTreeView()
92 void WebPopupMenuImpl::initialize(FramelessScrollView* widget, const WebRect& bounds)
95 m_widget->setClient(this);
99 m_client->setWindowRect(bounds);
100 m_client->show(WebNavigationPolicy()); // Policy is ignored.
102 m_client->initializeLayerTreeView();
103 m_layerTreeView = m_client->layerTreeView();
104 if (m_layerTreeView) {
105 m_layerTreeView->setVisible(true);
106 m_layerTreeView->setDeviceScaleFactor(m_client->deviceScaleFactor());
107 m_rootLayer = adoptPtr(Platform::current()->compositorSupport()->createContentLayer(this));
108 m_rootLayer->layer()->setBounds(m_size);
109 m_layerTreeView->setRootLayer(*m_rootLayer->layer());
113 void WebPopupMenuImpl::handleMouseMove(const WebMouseEvent& event)
115 // Don't send mouse move messages if the mouse hasn't moved.
116 if (event.x != m_lastMousePosition.x || event.y != m_lastMousePosition.y) {
117 m_lastMousePosition = WebPoint(event.x, event.y);
118 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
120 // We cannot call setToolTipText() in PopupContainer, because PopupContainer is in WebCore, and we cannot refer to WebKit from Webcore.
121 PopupContainer* container = static_cast<PopupContainer*>(m_widget);
122 client()->setToolTipText(container->getSelectedItemToolTip(), toWebTextDirection(container->menuStyle().textDirection()));
126 void WebPopupMenuImpl::handleMouseLeave(const WebMouseEvent& event)
128 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
131 void WebPopupMenuImpl::handleMouseDown(const WebMouseEvent& event)
133 m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
136 void WebPopupMenuImpl::handleMouseUp(const WebMouseEvent& event)
139 m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
142 void WebPopupMenuImpl::handleMouseWheel(const WebMouseWheelEvent& event)
144 m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
147 bool WebPopupMenuImpl::handleGestureEvent(const WebGestureEvent& event)
149 return m_widget->handleGestureEvent(PlatformGestureEventBuilder(m_widget, event));
152 bool WebPopupMenuImpl::handleTouchEvent(const WebTouchEvent& event)
155 PlatformTouchEventBuilder touchEventBuilder(m_widget, event);
156 bool defaultPrevented(m_widget->handleTouchEvent(touchEventBuilder));
157 return defaultPrevented;
160 bool WebPopupMenuImpl::handleKeyEvent(const WebKeyboardEvent& event)
162 return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
165 // WebWidget -------------------------------------------------------------------
167 void WebPopupMenuImpl::close()
174 deref(); // Balances ref() from WebPopupMenu::create.
177 void WebPopupMenuImpl::willStartLiveResize()
181 void WebPopupMenuImpl::resize(const WebSize& newSize)
183 if (m_size == newSize)
188 IntRect newGeometry(0, 0, m_size.width, m_size.height);
189 m_widget->setFrameRect(newGeometry);
193 WebRect damagedRect(0, 0, m_size.width, m_size.height);
194 m_client->didInvalidateRect(damagedRect);
198 m_rootLayer->layer()->setBounds(newSize);
201 void WebPopupMenuImpl::willEndLiveResize()
205 void WebPopupMenuImpl::beginFrame(const WebBeginFrameArgs&)
209 void WebPopupMenuImpl::layout()
213 void WebPopupMenuImpl::paintContents(WebCanvas* canvas, const WebRect& rect, bool, WebFloatRect&,
214 WebContentLayerClient::GraphicsContextStatus contextStatus)
219 if (!rect.isEmpty()) {
220 GraphicsContext context(canvas,
221 contextStatus == WebContentLayerClient::GraphicsContextEnabled ? GraphicsContext::NothingDisabled : GraphicsContext::FullyDisabled);
222 m_widget->paint(&context, rect);
226 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect)
231 if (!rect.isEmpty()) {
232 GraphicsContext context(canvas);
233 context.applyDeviceScaleFactor(m_client->deviceScaleFactor());
234 m_widget->paint(&context, rect);
238 void WebPopupMenuImpl::themeChanged()
243 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
248 // FIXME: WebKit seems to always return false on mouse events methods. For
249 // now we'll assume it has processed them (as we are only interested in
250 // whether keyboard events are processed).
251 switch (inputEvent.type) {
252 case WebInputEvent::MouseMove:
253 handleMouseMove(static_cast<const WebMouseEvent&>(inputEvent));
256 case WebInputEvent::MouseLeave:
257 handleMouseLeave(static_cast<const WebMouseEvent&>(inputEvent));
260 case WebInputEvent::MouseWheel:
261 handleMouseWheel(static_cast<const WebMouseWheelEvent&>(inputEvent));
264 case WebInputEvent::MouseDown:
265 handleMouseDown(static_cast<const WebMouseEvent&>(inputEvent));
268 case WebInputEvent::MouseUp:
269 handleMouseUp(static_cast<const WebMouseEvent&>(inputEvent));
272 // In Windows, RawKeyDown only has information about the physical key, but
273 // for "selection", we need the information about the character the key
274 // translated into. For English, the physical key value and the character
275 // value are the same, hence, "selection" works for English. But for other
276 // languages, such as Hebrew, the character value is different from the
277 // physical key value. Thus, without accepting Char event type which
278 // contains the key's character value, the "selection" won't work for
279 // non-English languages, such as Hebrew.
280 case WebInputEvent::RawKeyDown:
281 case WebInputEvent::KeyDown:
282 case WebInputEvent::KeyUp:
283 case WebInputEvent::Char:
284 return handleKeyEvent(static_cast<const WebKeyboardEvent&>(inputEvent));
286 case WebInputEvent::TouchStart:
287 case WebInputEvent::TouchMove:
288 case WebInputEvent::TouchEnd:
289 case WebInputEvent::TouchCancel:
290 return handleTouchEvent(static_cast<const WebTouchEvent&>(inputEvent));
292 case WebInputEvent::GestureScrollBegin:
293 case WebInputEvent::GestureScrollEnd:
294 case WebInputEvent::GestureScrollUpdate:
295 case WebInputEvent::GestureScrollUpdateWithoutPropagation:
296 case WebInputEvent::GestureFlingStart:
297 case WebInputEvent::GestureFlingCancel:
298 case WebInputEvent::GestureTap:
299 case WebInputEvent::GestureTapUnconfirmed:
300 case WebInputEvent::GestureTapDown:
301 case WebInputEvent::GestureShowPress:
302 case WebInputEvent::GestureTapCancel:
303 case WebInputEvent::GestureDoubleTap:
304 case WebInputEvent::GestureTwoFingerTap:
305 case WebInputEvent::GestureLongPress:
306 case WebInputEvent::GestureLongTap:
307 case WebInputEvent::GesturePinchBegin:
308 case WebInputEvent::GesturePinchEnd:
309 case WebInputEvent::GesturePinchUpdate:
310 return handleGestureEvent(static_cast<const WebGestureEvent&>(inputEvent));
312 case WebInputEvent::Undefined:
313 case WebInputEvent::MouseEnter:
314 case WebInputEvent::ContextMenu:
320 void WebPopupMenuImpl::mouseCaptureLost()
324 void WebPopupMenuImpl::setFocus(bool)
328 bool WebPopupMenuImpl::setComposition(const WebString&, const WebVector<WebCompositionUnderline>&, int, int)
333 bool WebPopupMenuImpl::confirmComposition()
338 bool WebPopupMenuImpl::confirmComposition(ConfirmCompositionBehavior)
343 bool WebPopupMenuImpl::confirmComposition(const WebString&)
348 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
355 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
362 void WebPopupMenuImpl::setTextDirection(WebTextDirection)
367 //-----------------------------------------------------------------------------
370 void WebPopupMenuImpl::invalidateContentsAndRootView(const IntRect& paintRect)
372 if (paintRect.isEmpty())
375 m_client->didInvalidateRect(paintRect);
377 m_rootLayer->layer()->invalidateRect(FloatRect(paintRect));
380 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect)
382 invalidateContentsAndRootView(updateRect);
385 void WebPopupMenuImpl::scheduleAnimation()
389 void WebPopupMenuImpl::scroll()
392 m_rootLayer->layer()->invalidate();
395 IntRect WebPopupMenuImpl::rootViewToScreen(const IntRect& rect) const
401 WebScreenInfo WebPopupMenuImpl::screenInfo() const
403 return WebScreenInfo();
406 //-----------------------------------------------------------------------------
407 // FramelessScrollViewClient
409 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
411 ASSERT(widget == m_widget);
413 m_widget->setClient(0);
417 m_client->closeWidgetSoon();