e2668b1b84d9f42adcabf31904bc8b68d0503f2a
[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         return GestureEvent(*static_cast<const WebGestureEvent*>(&inputEvent));
277
278     case WebInputEvent::Undefined:
279     case WebInputEvent::MouseEnter:
280     case WebInputEvent::ContextMenu:
281         return false;
282     }
283     return false;
284 }
285
286 void WebPopupMenuImpl::mouseCaptureLost()
287 {
288 }
289
290 void WebPopupMenuImpl::setFocus(bool enable)
291 {
292 }
293
294 void WebPopupMenu::setMinimumRowHeight(int minimumRowHeight)
295 {
296     PopupMenuChromium::setMinimumRowHeight(minimumRowHeight);
297 }
298
299 bool WebPopupMenuImpl::setComposition(
300     const WebString& text, const WebVector<WebCompositionUnderline>& underlines,
301     int selectionStart, int selectionEnd)
302 {
303     return false;
304 }
305
306 bool WebPopupMenuImpl::confirmComposition()
307 {
308     return false;
309 }
310
311 bool WebPopupMenuImpl::confirmComposition(const WebString& text)
312 {
313     return false;
314 }
315
316 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
317 {
318     *location = 0;
319     *length = 0;
320     return false;
321 }
322
323 WebTextInputType WebPopupMenuImpl::textInputType()
324 {
325     return WebTextInputTypeNone;
326 }
327
328 WebRect WebPopupMenuImpl::caretOrSelectionBounds()
329 {
330     return WebRect();
331 }
332
333 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
334 {
335     *location = 0;
336     *length = 0;
337     return false;
338 }
339
340 void WebPopupMenuImpl::setTextDirection(WebTextDirection direction)
341 {
342 }
343
344
345 //-----------------------------------------------------------------------------
346 // WebCore::HostWindow
347
348 void WebPopupMenuImpl::invalidateContents(const IntRect&, bool)
349 {
350     notImplemented();
351 }
352
353 void WebPopupMenuImpl::invalidateWindow(const IntRect&, bool)
354 {
355     notImplemented();
356 }
357
358 void WebPopupMenuImpl::invalidateContentsAndWindow(const IntRect& paintRect, bool /*immediate*/)
359 {
360     if (paintRect.isEmpty())
361         return;
362     if (m_client)
363         m_client->didInvalidateRect(paintRect);
364 }
365
366 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
367 {
368     invalidateContentsAndWindow(updateRect, immediate);
369 }
370
371 void WebPopupMenuImpl::scheduleAnimation()
372 {
373 }
374
375 void WebPopupMenuImpl::scroll(const IntSize& scrollDelta,
376                               const IntRect& scrollRect,
377                               const IntRect& clipRect)
378 {
379     if (m_client) {
380         int dx = scrollDelta.width();
381         int dy = scrollDelta.height();
382         m_client->didScrollRect(dx, dy, clipRect);
383     }
384 }
385
386 IntPoint WebPopupMenuImpl::screenToWindow(const IntPoint& point) const
387 {
388     notImplemented();
389     return IntPoint();
390 }
391
392 IntRect WebPopupMenuImpl::windowToScreen(const IntRect& rect) const
393 {
394     notImplemented();
395     return IntRect();
396 }
397
398 void WebPopupMenuImpl::scrollRectIntoView(const IntRect&, const ScrollView*) const
399 {
400     // Nothing to be done here since we do not have the concept of a container
401     // that implements its own scrolling.
402 }
403
404 void WebPopupMenuImpl::scrollbarsModeDidChange() const
405 {
406     // Nothing to be done since we have no concept of different scrollbar modes.
407 }
408
409 void WebPopupMenuImpl::setCursor(const WebCore::Cursor&)
410 {
411 }
412
413 void WebPopupMenuImpl::setCursorHiddenUntilMouseMoves(bool)
414 {
415 }
416
417 //-----------------------------------------------------------------------------
418 // WebCore::FramelessScrollViewClient
419
420 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
421 {
422     ASSERT(widget == m_widget);
423     if (m_widget) {
424         m_widget->setClient(0);
425         m_widget = 0;
426     }
427     if (m_client)
428         m_client->closeWidgetSoon();
429 }
430
431 } // namespace WebKit