838238d257861a480a77d9d5b4722d8406088399
[WebKit-https.git] / Source / WebKit / chromium / src / WebPopupMenuImpl.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "WebPopupMenuImpl.h"
33
34 #include "Cursor.h"
35 #include "FramelessScrollView.h"
36 #include "FrameView.h"
37 #include "IntRect.h"
38 #include "NotImplemented.h"
39 #include "painting/GraphicsContextBuilder.h"
40 #include "PlatformKeyboardEvent.h"
41 #include "PlatformMouseEvent.h"
42 #include "PlatformWheelEvent.h"
43 #include "PopupMenuChromium.h"
44 #include "SkiaUtils.h"
45
46 #include "WebInputEvent.h"
47 #include "WebInputEventConversion.h"
48 #include "WebRange.h"
49 #include "WebRect.h"
50 #include "WebViewClient.h"
51 #include "WebWidgetClient.h"
52
53 #include <skia/ext/platform_canvas.h>
54
55 #if ENABLE(GESTURE_RECOGNIZER)
56 #include "PlatformGestureEvent.h"
57 #include "PlatformGestureRecognizer.h"
58 #endif
59
60 using namespace WebCore;
61
62 namespace WebKit {
63
64 // WebPopupMenu ---------------------------------------------------------------
65
66 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
67 {
68     // Pass the WebPopupMenuImpl's self-reference to the caller.
69     return adoptRef(new WebPopupMenuImpl(client)).leakRef();
70 }
71
72 // WebWidget ------------------------------------------------------------------
73
74 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
75     : m_client(client)
76     , m_widget(0)
77 #if ENABLE(GESTURE_RECOGNIZER)
78     , m_gestureRecognizer(WebCore::PlatformGestureRecognizer::create())
79 #endif
80 {
81     // set to impossible point so we always get the first mouse pos
82     m_lastMousePosition = WebPoint(-1, -1);
83 }
84
85 WebPopupMenuImpl::~WebPopupMenuImpl()
86 {
87     if (m_widget)
88         m_widget->setClient(0);
89 }
90
91 void WebPopupMenuImpl::Init(FramelessScrollView* widget, const WebRect& bounds)
92 {
93     m_widget = widget;
94     m_widget->setClient(this);
95
96     if (m_client) {
97         m_client->setWindowRect(bounds);
98         m_client->show(WebNavigationPolicy());  // Policy is ignored
99     }
100 }
101
102 void WebPopupMenuImpl::MouseMove(const WebMouseEvent& event)
103 {
104     // don't send mouse move messages if the mouse hasn't moved.
105     if (event.x != m_lastMousePosition.x || event.y != m_lastMousePosition.y) {
106         m_lastMousePosition = WebPoint(event.x, event.y);
107         m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
108
109         // We cannot call setToolTipText() in PopupContainer, because PopupContainer is in WebCore, and we cannot refer to WebKit from Webcore.
110         WebCore::PopupContainer* container = static_cast<WebCore::PopupContainer*>(m_widget);
111         client()->setToolTipText(container->getSelectedItemToolTip(), container->menuStyle().textDirection() == WebCore::RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight);
112     }
113 }
114
115 void WebPopupMenuImpl::MouseLeave(const WebMouseEvent& event)
116 {
117     m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
118 }
119
120 void WebPopupMenuImpl::MouseDown(const WebMouseEvent& event)
121 {
122     m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
123 }
124
125 void WebPopupMenuImpl::MouseUp(const WebMouseEvent& event)
126 {
127     mouseCaptureLost();
128     m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
129 }
130
131 void WebPopupMenuImpl::MouseWheel(const WebMouseWheelEvent& event)
132 {
133     m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
134 }
135
136 bool WebPopupMenuImpl::GestureEvent(const WebGestureEvent& event)
137 {
138     return m_widget->handleGestureEvent(PlatformGestureEventBuilder(m_widget, event));
139 }
140
141 #if ENABLE(TOUCH_EVENTS)
142 bool WebPopupMenuImpl::TouchEvent(const WebTouchEvent& event)
143 {
144
145     PlatformTouchEventBuilder touchEventBuilder(m_widget, event);
146     bool defaultPrevented(m_widget->handleTouchEvent(touchEventBuilder));
147 #if ENABLE(GESTURE_RECOGNIZER)
148     OwnPtr<Vector<WebCore::PlatformGestureEvent> > gestureEvents(m_gestureRecognizer->processTouchEventForGestures(touchEventBuilder, defaultPrevented));
149     for (unsigned int  i = 0; i < gestureEvents->size(); i++)
150         m_widget->handleGestureEvent((*gestureEvents)[i]);
151 #endif
152     return defaultPrevented;
153 }
154 #endif
155
156 bool WebPopupMenuImpl::KeyEvent(const WebKeyboardEvent& event)
157 {
158     return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
159 }
160
161 // WebWidget -------------------------------------------------------------------
162
163 void WebPopupMenuImpl::close()
164 {
165     if (m_widget)
166         m_widget->hide();
167
168     m_client = 0;
169
170     deref();  // Balances ref() from WebWidget::Create
171 }
172
173 void WebPopupMenuImpl::willStartLiveResize()
174 {
175 }
176
177 void WebPopupMenuImpl::resize(const WebSize& newSize)
178 {
179     if (m_size == newSize)
180         return;
181     m_size = newSize;
182
183     if (m_widget) {
184         IntRect newGeometry(0, 0, m_size.width, m_size.height);
185         m_widget->setFrameRect(newGeometry);
186     }
187
188     if (m_client) {
189         WebRect damagedRect(0, 0, m_size.width, m_size.height);
190         m_client->didInvalidateRect(damagedRect);
191     }
192 }
193
194 void WebPopupMenuImpl::willEndLiveResize()
195 {
196 }
197
198 void WebPopupMenuImpl::animate(double)
199 {
200 }
201
202 void WebPopupMenuImpl::layout()
203 {
204 }
205
206 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect)
207 {
208     if (!m_widget)
209         return;
210
211     if (!rect.isEmpty())
212         m_widget->paint(&GraphicsContextBuilder(canvas).context(), rect);
213 }
214
215 void WebPopupMenuImpl::themeChanged()
216 {
217     notImplemented();
218 }
219
220 void WebPopupMenuImpl::composite(bool finish)
221 {
222     notImplemented();
223 }
224
225 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
226 {
227     if (!m_widget)
228         return false;
229
230     // TODO (jcampan): WebKit seems to always return false on mouse events
231     // methods. For now we'll assume it has processed them (as we are only
232     // interested in whether keyboard events are processed).
233     switch (inputEvent.type) {
234     case WebInputEvent::MouseMove:
235         MouseMove(*static_cast<const WebMouseEvent*>(&inputEvent));
236         return true;
237
238     case WebInputEvent::MouseLeave:
239         MouseLeave(*static_cast<const WebMouseEvent*>(&inputEvent));
240         return true;
241
242     case WebInputEvent::MouseWheel:
243         MouseWheel(*static_cast<const WebMouseWheelEvent*>(&inputEvent));
244         return true;
245
246     case WebInputEvent::MouseDown:
247         MouseDown(*static_cast<const WebMouseEvent*>(&inputEvent));
248         return true;
249
250     case WebInputEvent::MouseUp:
251         MouseUp(*static_cast<const WebMouseEvent*>(&inputEvent));
252         return true;
253
254     // In Windows, RawKeyDown only has information about the physical key, but
255     // for "selection", we need the information about the character the key
256     // translated into. For English, the physical key value and the character
257     // value are the same, hence, "selection" works for English. But for other
258     // languages, such as Hebrew, the character value is different from the
259     // physical key value. Thus, without accepting Char event type which
260     // contains the key's character value, the "selection" won't work for
261     // non-English languages, such as Hebrew.
262     case WebInputEvent::RawKeyDown:
263     case WebInputEvent::KeyDown:
264     case WebInputEvent::KeyUp:
265     case WebInputEvent::Char:
266         return KeyEvent(*static_cast<const WebKeyboardEvent*>(&inputEvent));
267
268     case WebInputEvent::TouchStart:
269     case WebInputEvent::TouchMove:
270     case WebInputEvent::TouchEnd:
271     case WebInputEvent::TouchCancel:
272         return TouchEvent(*static_cast<const WebTouchEvent*>(&inputEvent));
273
274     case WebInputEvent::GestureScrollBegin:
275     case WebInputEvent::GestureScrollEnd:
276     case WebInputEvent::GestureTap:
277         return GestureEvent(*static_cast<const WebGestureEvent*>(&inputEvent));
278
279     case WebInputEvent::Undefined:
280     case WebInputEvent::MouseEnter:
281     case WebInputEvent::ContextMenu:
282         return false;
283     }
284     return false;
285 }
286
287 void WebPopupMenuImpl::mouseCaptureLost()
288 {
289 }
290
291 void WebPopupMenuImpl::setFocus(bool enable)
292 {
293 }
294
295 void WebPopupMenu::setMinimumRowHeight(int minimumRowHeight)
296 {
297     PopupMenuChromium::setMinimumRowHeight(minimumRowHeight);
298 }
299
300 bool WebPopupMenuImpl::setComposition(
301     const WebString& text, const WebVector<WebCompositionUnderline>& underlines,
302     int selectionStart, int selectionEnd)
303 {
304     return false;
305 }
306
307 bool WebPopupMenuImpl::confirmComposition()
308 {
309     return false;
310 }
311
312 bool WebPopupMenuImpl::confirmComposition(const WebString& text)
313 {
314     return false;
315 }
316
317 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
318 {
319     *location = 0;
320     *length = 0;
321     return false;
322 }
323
324 WebTextInputType WebPopupMenuImpl::textInputType()
325 {
326     return WebTextInputTypeNone;
327 }
328
329 WebRect WebPopupMenuImpl::caretOrSelectionBounds()
330 {
331     return WebRect();
332 }
333
334 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
335 {
336     *location = 0;
337     *length = 0;
338     return false;
339 }
340
341 void WebPopupMenuImpl::setTextDirection(WebTextDirection direction)
342 {
343 }
344
345
346 //-----------------------------------------------------------------------------
347 // WebCore::HostWindow
348
349 void WebPopupMenuImpl::invalidateContents(const IntRect&, bool)
350 {
351     notImplemented();
352 }
353
354 void WebPopupMenuImpl::invalidateWindow(const IntRect&, bool)
355 {
356     notImplemented();
357 }
358
359 void WebPopupMenuImpl::invalidateContentsAndWindow(const IntRect& paintRect, bool /*immediate*/)
360 {
361     if (paintRect.isEmpty())
362         return;
363     if (m_client)
364         m_client->didInvalidateRect(paintRect);
365 }
366
367 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
368 {
369     invalidateContentsAndWindow(updateRect, immediate);
370 }
371
372 void WebPopupMenuImpl::scheduleAnimation()
373 {
374 }
375
376 void WebPopupMenuImpl::scroll(const IntSize& scrollDelta,
377                               const IntRect& scrollRect,
378                               const IntRect& clipRect)
379 {
380     if (m_client) {
381         int dx = scrollDelta.width();
382         int dy = scrollDelta.height();
383         m_client->didScrollRect(dx, dy, clipRect);
384     }
385 }
386
387 IntPoint WebPopupMenuImpl::screenToWindow(const IntPoint& point) const
388 {
389     notImplemented();
390     return IntPoint();
391 }
392
393 IntRect WebPopupMenuImpl::windowToScreen(const IntRect& rect) const
394 {
395     notImplemented();
396     return IntRect();
397 }
398
399 void WebPopupMenuImpl::scrollRectIntoView(const IntRect&, const ScrollView*) const
400 {
401     // Nothing to be done here since we do not have the concept of a container
402     // that implements its own scrolling.
403 }
404
405 void WebPopupMenuImpl::scrollbarsModeDidChange() const
406 {
407     // Nothing to be done since we have no concept of different scrollbar modes.
408 }
409
410 void WebPopupMenuImpl::setCursor(const WebCore::Cursor&)
411 {
412 }
413
414 void WebPopupMenuImpl::setCursorHiddenUntilMouseMoves(bool)
415 {
416 }
417
418 //-----------------------------------------------------------------------------
419 // WebCore::FramelessScrollViewClient
420
421 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
422 {
423     ASSERT(widget == m_widget);
424     if (m_widget) {
425         m_widget->setClient(0);
426         m_widget = 0;
427     }
428     if (m_client)
429         m_client->closeWidgetSoon();
430 }
431
432 } // namespace WebKit