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"
35 #include "FramelessScrollView.h"
36 #include "FrameView.h"
38 #include "NotImplemented.h"
39 #include "painting/GraphicsContextBuilder.h"
40 #include "PlatformKeyboardEvent.h"
41 #include "PlatformMouseEvent.h"
42 #include "PlatformWheelEvent.h"
43 #include "PopupContainer.h"
44 #include "PopupMenuChromium.h"
45 #include "SkiaUtils.h"
47 #include "WebInputEvent.h"
48 #include "WebInputEventConversion.h"
51 #include "WebViewClient.h"
52 #include "WebWidgetClient.h"
54 #include <skia/ext/platform_canvas.h>
56 #if ENABLE(GESTURE_RECOGNIZER)
57 #include "PlatformGestureEvent.h"
58 #include "PlatformGestureRecognizer.h"
61 using namespace WebCore;
65 // WebPopupMenu ---------------------------------------------------------------
67 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
69 // Pass the WebPopupMenuImpl's self-reference to the caller.
70 return adoptRef(new WebPopupMenuImpl(client)).leakRef();
73 // WebWidget ------------------------------------------------------------------
75 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
78 #if ENABLE(GESTURE_RECOGNIZER)
79 , m_gestureRecognizer(WebCore::PlatformGestureRecognizer::create())
82 // set to impossible point so we always get the first mouse pos
83 m_lastMousePosition = WebPoint(-1, -1);
86 WebPopupMenuImpl::~WebPopupMenuImpl()
89 m_widget->setClient(0);
92 void WebPopupMenuImpl::Init(FramelessScrollView* widget, const WebRect& bounds)
95 m_widget->setClient(this);
98 m_client->setWindowRect(bounds);
99 m_client->show(WebNavigationPolicy()); // Policy is ignored
103 void WebPopupMenuImpl::MouseMove(const WebMouseEvent& event)
105 // don't send mouse move messages if the mouse hasn't moved.
106 if (event.x != m_lastMousePosition.x || event.y != m_lastMousePosition.y) {
107 m_lastMousePosition = WebPoint(event.x, event.y);
108 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
110 // We cannot call setToolTipText() in PopupContainer, because PopupContainer is in WebCore, and we cannot refer to WebKit from Webcore.
111 WebCore::PopupContainer* container = static_cast<WebCore::PopupContainer*>(m_widget);
112 client()->setToolTipText(container->getSelectedItemToolTip(), container->menuStyle().textDirection() == WebCore::RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight);
116 void WebPopupMenuImpl::MouseLeave(const WebMouseEvent& event)
118 m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
121 void WebPopupMenuImpl::MouseDown(const WebMouseEvent& event)
123 m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
126 void WebPopupMenuImpl::MouseUp(const WebMouseEvent& event)
129 m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
132 void WebPopupMenuImpl::MouseWheel(const WebMouseWheelEvent& event)
134 m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
137 bool WebPopupMenuImpl::GestureEvent(const WebGestureEvent& event)
139 return m_widget->handleGestureEvent(PlatformGestureEventBuilder(m_widget, event));
142 #if ENABLE(TOUCH_EVENTS)
143 bool WebPopupMenuImpl::TouchEvent(const WebTouchEvent& event)
146 PlatformTouchEventBuilder touchEventBuilder(m_widget, event);
147 bool defaultPrevented(m_widget->handleTouchEvent(touchEventBuilder));
148 #if ENABLE(GESTURE_RECOGNIZER)
149 OwnPtr<Vector<WebCore::PlatformGestureEvent> > gestureEvents(m_gestureRecognizer->processTouchEventForGestures(touchEventBuilder, defaultPrevented));
150 for (unsigned int i = 0; i < gestureEvents->size(); i++)
151 m_widget->handleGestureEvent((*gestureEvents)[i]);
153 return defaultPrevented;
157 bool WebPopupMenuImpl::KeyEvent(const WebKeyboardEvent& event)
159 return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
162 // WebWidget -------------------------------------------------------------------
164 void WebPopupMenuImpl::close()
171 deref(); // Balances ref() from WebWidget::Create
174 void WebPopupMenuImpl::willStartLiveResize()
178 void WebPopupMenuImpl::resize(const WebSize& newSize)
180 if (m_size == newSize)
185 IntRect newGeometry(0, 0, m_size.width, m_size.height);
186 m_widget->setFrameRect(newGeometry);
190 WebRect damagedRect(0, 0, m_size.width, m_size.height);
191 m_client->didInvalidateRect(damagedRect);
195 void WebPopupMenuImpl::willEndLiveResize()
199 void WebPopupMenuImpl::animate(double)
203 void WebPopupMenuImpl::layout()
207 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect)
213 m_widget->paint(&GraphicsContextBuilder(canvas).context(), rect);
216 void WebPopupMenuImpl::themeChanged()
221 void WebPopupMenuImpl::composite(bool finish)
226 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
231 // TODO (jcampan): WebKit seems to always return false on mouse events
232 // methods. For now we'll assume it has processed them (as we are only
233 // interested in whether keyboard events are processed).
234 switch (inputEvent.type) {
235 case WebInputEvent::MouseMove:
236 MouseMove(*static_cast<const WebMouseEvent*>(&inputEvent));
239 case WebInputEvent::MouseLeave:
240 MouseLeave(*static_cast<const WebMouseEvent*>(&inputEvent));
243 case WebInputEvent::MouseWheel:
244 MouseWheel(*static_cast<const WebMouseWheelEvent*>(&inputEvent));
247 case WebInputEvent::MouseDown:
248 MouseDown(*static_cast<const WebMouseEvent*>(&inputEvent));
251 case WebInputEvent::MouseUp:
252 MouseUp(*static_cast<const WebMouseEvent*>(&inputEvent));
255 // In Windows, RawKeyDown only has information about the physical key, but
256 // for "selection", we need the information about the character the key
257 // translated into. For English, the physical key value and the character
258 // value are the same, hence, "selection" works for English. But for other
259 // languages, such as Hebrew, the character value is different from the
260 // physical key value. Thus, without accepting Char event type which
261 // contains the key's character value, the "selection" won't work for
262 // non-English languages, such as Hebrew.
263 case WebInputEvent::RawKeyDown:
264 case WebInputEvent::KeyDown:
265 case WebInputEvent::KeyUp:
266 case WebInputEvent::Char:
267 return KeyEvent(*static_cast<const WebKeyboardEvent*>(&inputEvent));
269 case WebInputEvent::TouchStart:
270 case WebInputEvent::TouchMove:
271 case WebInputEvent::TouchEnd:
272 case WebInputEvent::TouchCancel:
273 return TouchEvent(*static_cast<const WebTouchEvent*>(&inputEvent));
275 case WebInputEvent::GestureScrollBegin:
276 case WebInputEvent::GestureScrollEnd:
277 case WebInputEvent::GestureScrollUpdate:
278 case WebInputEvent::GestureFlingStart:
279 case WebInputEvent::GestureFlingCancel:
280 case WebInputEvent::GestureTap:
281 return GestureEvent(*static_cast<const WebGestureEvent*>(&inputEvent));
283 case WebInputEvent::Undefined:
284 case WebInputEvent::MouseEnter:
285 case WebInputEvent::ContextMenu:
291 void WebPopupMenuImpl::mouseCaptureLost()
295 void WebPopupMenuImpl::setFocus(bool enable)
299 void WebPopupMenu::setMinimumRowHeight(int minimumRowHeight)
301 PopupMenuChromium::setMinimumRowHeight(minimumRowHeight);
304 bool WebPopupMenuImpl::setComposition(
305 const WebString& text, const WebVector<WebCompositionUnderline>& underlines,
306 int selectionStart, int selectionEnd)
311 bool WebPopupMenuImpl::confirmComposition()
316 bool WebPopupMenuImpl::confirmComposition(const WebString& text)
321 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
328 WebTextInputType WebPopupMenuImpl::textInputType()
330 return WebTextInputTypeNone;
333 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
340 void WebPopupMenuImpl::setTextDirection(WebTextDirection direction)
345 //-----------------------------------------------------------------------------
346 // WebCore::HostWindow
348 void WebPopupMenuImpl::invalidateContents(const IntRect&, bool)
353 void WebPopupMenuImpl::invalidateRootView(const IntRect&, bool)
358 void WebPopupMenuImpl::invalidateContentsAndRootView(const IntRect& paintRect, bool /*immediate*/)
360 if (paintRect.isEmpty())
363 m_client->didInvalidateRect(paintRect);
366 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
368 invalidateContentsAndRootView(updateRect, immediate);
371 void WebPopupMenuImpl::scheduleAnimation()
375 void WebPopupMenuImpl::scroll(const IntSize& scrollDelta,
376 const IntRect& scrollRect,
377 const IntRect& clipRect)
380 int dx = scrollDelta.width();
381 int dy = scrollDelta.height();
382 m_client->didScrollRect(dx, dy, clipRect);
386 IntPoint WebPopupMenuImpl::screenToRootView(const IntPoint& point) const
392 IntRect WebPopupMenuImpl::rootViewToScreen(const IntRect& rect) const
398 void WebPopupMenuImpl::scrollRectIntoView(const IntRect&) const
400 // Nothing to be done here since we do not have the concept of a container
401 // that implements its own scrolling.
404 void WebPopupMenuImpl::scrollbarsModeDidChange() const
406 // Nothing to be done since we have no concept of different scrollbar modes.
409 void WebPopupMenuImpl::setCursor(const WebCore::Cursor&)
413 void WebPopupMenuImpl::setCursorHiddenUntilMouseMoves(bool)
417 //-----------------------------------------------------------------------------
418 // WebCore::FramelessScrollViewClient
420 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
422 ASSERT(widget == m_widget);
424 m_widget->setClient(0);
428 m_client->closeWidgetSoon();
431 } // namespace WebKit