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 "WebPopupMenuImpl.h"
34 #include "PopupContainer.h"
35 #include "PopupMenuChromium.h"
36 #include "WebInputEvent.h"
37 #include "WebInputEventConversion.h"
39 #include "WebViewClient.h"
40 #include "WebWidgetClient.h"
41 #include "core/frame/FrameView.h"
42 #include "core/platform/Cursor.h"
43 #include "platform/NotImplemented.h"
44 #include "core/platform/chromium/FramelessScrollView.h"
45 #include "core/platform/graphics/GraphicsContext.h"
46 #include "core/platform/graphics/skia/SkiaUtils.h"
47 #include "platform/PlatformGestureEvent.h"
48 #include "platform/PlatformKeyboardEvent.h"
49 #include "platform/PlatformMouseEvent.h"
50 #include "platform/PlatformWheelEvent.h"
51 #include "platform/geometry/IntRect.h"
52 #include "public/platform/WebRect.h"
53 #include <skia/ext/platform_canvas.h>
55 using namespace WebCore;
59 // WebPopupMenu ---------------------------------------------------------------
61 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
63 // Pass the WebPopupMenuImpl's self-reference to the caller.
64 return adoptRef(new WebPopupMenuImpl(client)).leakRef();
67 // WebWidget ------------------------------------------------------------------
69 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
73 // Set to impossible point so we always get the first mouse position.
74 m_lastMousePosition = WebPoint(-1, -1);
77 WebPopupMenuImpl::~WebPopupMenuImpl()
80 m_widget->setClient(0);
83 void WebPopupMenuImpl::initialize(FramelessScrollView* widget, const WebRect& bounds)
86 m_widget->setClient(this);
89 m_client->setWindowRect(bounds);
90 m_client->show(WebNavigationPolicy()); // Policy is ignored.
94 void WebPopupMenuImpl::handleMouseMove(const WebMouseEvent& event)
96 // Don't send mouse move messages if the mouse hasn't moved.
97 if (event.x != m_lastMousePosition.x || event.y != m_lastMousePosition.y) {
98 m_lastMousePosition = WebPoint(event.x, event.y);
99 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
101 // We cannot call setToolTipText() in PopupContainer, because PopupContainer is in WebCore, and we cannot refer to WebKit from Webcore.
102 WebCore::PopupContainer* container = static_cast<WebCore::PopupContainer*>(m_widget);
103 client()->setToolTipText(container->getSelectedItemToolTip(), container->menuStyle().textDirection() == WebCore::RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight);
107 void WebPopupMenuImpl::handleMouseLeave(const WebMouseEvent& event)
109 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
112 void WebPopupMenuImpl::handleMouseDown(const WebMouseEvent& event)
114 m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
117 void WebPopupMenuImpl::handleMouseUp(const WebMouseEvent& event)
120 m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
123 void WebPopupMenuImpl::handleMouseWheel(const WebMouseWheelEvent& event)
125 m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
128 bool WebPopupMenuImpl::handleGestureEvent(const WebGestureEvent& event)
130 return m_widget->handleGestureEvent(PlatformGestureEventBuilder(m_widget, event));
133 bool WebPopupMenuImpl::handleTouchEvent(const WebTouchEvent& event)
136 PlatformTouchEventBuilder touchEventBuilder(m_widget, event);
137 bool defaultPrevented(m_widget->handleTouchEvent(touchEventBuilder));
138 return defaultPrevented;
141 bool WebPopupMenuImpl::handleKeyEvent(const WebKeyboardEvent& event)
143 return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
146 // WebWidget -------------------------------------------------------------------
148 void WebPopupMenuImpl::close()
155 deref(); // Balances ref() from WebPopupMenu::create.
158 void WebPopupMenuImpl::willStartLiveResize()
162 void WebPopupMenuImpl::resize(const WebSize& newSize)
164 if (m_size == newSize)
169 IntRect newGeometry(0, 0, m_size.width, m_size.height);
170 m_widget->setFrameRect(newGeometry);
174 WebRect damagedRect(0, 0, m_size.width, m_size.height);
175 m_client->didInvalidateRect(damagedRect);
179 void WebPopupMenuImpl::willEndLiveResize()
183 void WebPopupMenuImpl::animate(double)
187 void WebPopupMenuImpl::layout()
191 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect, PaintOptions)
196 if (!rect.isEmpty()) {
197 GraphicsContext context(canvas);
198 context.applyDeviceScaleFactor(m_client->deviceScaleFactor());
199 m_widget->paint(&context, rect);
203 void WebPopupMenuImpl::themeChanged()
208 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
213 // FIXME: WebKit seems to always return false on mouse events methods. For
214 // now we'll assume it has processed them (as we are only interested in
215 // whether keyboard events are processed).
216 switch (inputEvent.type) {
217 case WebInputEvent::MouseMove:
218 handleMouseMove(*static_cast<const WebMouseEvent*>(&inputEvent));
221 case WebInputEvent::MouseLeave:
222 handleMouseLeave(*static_cast<const WebMouseEvent*>(&inputEvent));
225 case WebInputEvent::MouseWheel:
226 handleMouseWheel(*static_cast<const WebMouseWheelEvent*>(&inputEvent));
229 case WebInputEvent::MouseDown:
230 handleMouseDown(*static_cast<const WebMouseEvent*>(&inputEvent));
233 case WebInputEvent::MouseUp:
234 handleMouseUp(*static_cast<const WebMouseEvent*>(&inputEvent));
237 // In Windows, RawKeyDown only has information about the physical key, but
238 // for "selection", we need the information about the character the key
239 // translated into. For English, the physical key value and the character
240 // value are the same, hence, "selection" works for English. But for other
241 // languages, such as Hebrew, the character value is different from the
242 // physical key value. Thus, without accepting Char event type which
243 // contains the key's character value, the "selection" won't work for
244 // non-English languages, such as Hebrew.
245 case WebInputEvent::RawKeyDown:
246 case WebInputEvent::KeyDown:
247 case WebInputEvent::KeyUp:
248 case WebInputEvent::Char:
249 return handleKeyEvent(*static_cast<const WebKeyboardEvent*>(&inputEvent));
251 case WebInputEvent::TouchStart:
252 case WebInputEvent::TouchMove:
253 case WebInputEvent::TouchEnd:
254 case WebInputEvent::TouchCancel:
255 return handleTouchEvent(*static_cast<const WebTouchEvent*>(&inputEvent));
257 case WebInputEvent::GestureScrollBegin:
258 case WebInputEvent::GestureScrollEnd:
259 case WebInputEvent::GestureScrollUpdate:
260 case WebInputEvent::GestureScrollUpdateWithoutPropagation:
261 case WebInputEvent::GestureFlingStart:
262 case WebInputEvent::GestureFlingCancel:
263 case WebInputEvent::GestureTap:
264 case WebInputEvent::GestureTapUnconfirmed:
265 case WebInputEvent::GestureTapDown:
266 case WebInputEvent::GestureShowPress:
267 case WebInputEvent::GestureTapCancel:
268 case WebInputEvent::GestureDoubleTap:
269 case WebInputEvent::GestureTwoFingerTap:
270 case WebInputEvent::GestureLongPress:
271 case WebInputEvent::GestureLongTap:
272 case WebInputEvent::GesturePinchBegin:
273 case WebInputEvent::GesturePinchEnd:
274 case WebInputEvent::GesturePinchUpdate:
275 return handleGestureEvent(*static_cast<const WebGestureEvent*>(&inputEvent));
277 case WebInputEvent::Undefined:
278 case WebInputEvent::MouseEnter:
279 case WebInputEvent::ContextMenu:
285 void WebPopupMenuImpl::mouseCaptureLost()
289 void WebPopupMenuImpl::setFocus(bool)
293 void WebPopupMenu::setMinimumRowHeight(int minimumRowHeight)
295 PopupMenuChromium::setMinimumRowHeight(minimumRowHeight);
298 bool WebPopupMenuImpl::setComposition(const WebString&, const WebVector<WebCompositionUnderline>&, int, int)
303 bool WebPopupMenuImpl::confirmComposition()
308 bool WebPopupMenuImpl::confirmComposition(ConfirmCompositionBehavior)
313 bool WebPopupMenuImpl::confirmComposition(const WebString&)
318 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
325 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
332 void WebPopupMenuImpl::setTextDirection(WebTextDirection)
337 //-----------------------------------------------------------------------------
338 // WebCore::HostWindow
340 void WebPopupMenuImpl::invalidateContentsAndRootView(const IntRect& paintRect)
342 if (paintRect.isEmpty())
345 m_client->didInvalidateRect(paintRect);
348 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect)
350 invalidateContentsAndRootView(updateRect);
353 void WebPopupMenuImpl::scheduleAnimation()
357 void WebPopupMenuImpl::scroll(const IntSize& scrollDelta, const IntRect& scrollRect, const IntRect& clipRect)
360 int dx = scrollDelta.width();
361 int dy = scrollDelta.height();
362 m_client->didScrollRect(dx, dy, clipRect);
366 IntPoint WebPopupMenuImpl::screenToRootView(const IntPoint& point) const
372 IntRect WebPopupMenuImpl::rootViewToScreen(const IntRect& rect) const
378 WebScreenInfo WebPopupMenuImpl::screenInfo() const
380 return WebScreenInfo();
383 //-----------------------------------------------------------------------------
384 // WebCore::FramelessScrollViewClient
386 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
388 ASSERT(widget == m_widget);
390 m_widget->setClient(0);
394 m_client->closeWidgetSoon();
397 } // namespace WebKit