Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / 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 "web/WebPopupMenuImpl.h"
33
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>
59
60 namespace blink {
61
62 // WebPopupMenu ---------------------------------------------------------------
63
64 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
65 {
66     // Pass the WebPopupMenuImpl's self-reference to the caller.
67     return adoptRef(new WebPopupMenuImpl(client)).leakRef();
68 }
69
70 // WebWidget ------------------------------------------------------------------
71
72 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
73     : m_client(client)
74     , m_layerTreeView(0)
75     // Set to impossible point so we always get the first mouse position.
76     , m_lastMousePosition(WebPoint(-1, -1))
77     , m_widget(0)
78 {
79 }
80
81 WebPopupMenuImpl::~WebPopupMenuImpl()
82 {
83     if (m_widget)
84         m_widget->setClient(0);
85 }
86
87 void WebPopupMenuImpl::willCloseLayerTreeView()
88 {
89     m_layerTreeView = 0;
90 }
91
92 void WebPopupMenuImpl::initialize(FramelessScrollView* widget, const WebRect& bounds)
93 {
94     m_widget = widget;
95     m_widget->setClient(this);
96
97     if (!m_client)
98         return;
99     m_client->setWindowRect(bounds);
100     m_client->show(WebNavigationPolicy()); // Policy is ignored.
101
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());
110     }
111 }
112
113 void WebPopupMenuImpl::handleMouseMove(const WebMouseEvent& event)
114 {
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));
119
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()));
123     }
124 }
125
126 void WebPopupMenuImpl::handleMouseLeave(const WebMouseEvent& event)
127 {
128     m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
129 }
130
131 void WebPopupMenuImpl::handleMouseDown(const WebMouseEvent& event)
132 {
133     m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
134 }
135
136 void WebPopupMenuImpl::handleMouseUp(const WebMouseEvent& event)
137 {
138     mouseCaptureLost();
139     m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
140 }
141
142 void WebPopupMenuImpl::handleMouseWheel(const WebMouseWheelEvent& event)
143 {
144     m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
145 }
146
147 bool WebPopupMenuImpl::handleGestureEvent(const WebGestureEvent& event)
148 {
149     return m_widget->handleGestureEvent(PlatformGestureEventBuilder(m_widget, event));
150 }
151
152 bool WebPopupMenuImpl::handleTouchEvent(const WebTouchEvent& event)
153 {
154
155     PlatformTouchEventBuilder touchEventBuilder(m_widget, event);
156     bool defaultPrevented(m_widget->handleTouchEvent(touchEventBuilder));
157     return defaultPrevented;
158 }
159
160 bool WebPopupMenuImpl::handleKeyEvent(const WebKeyboardEvent& event)
161 {
162     return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
163 }
164
165 // WebWidget -------------------------------------------------------------------
166
167 void WebPopupMenuImpl::close()
168 {
169     if (m_widget)
170         m_widget->hide();
171
172     m_client = 0;
173
174     deref(); // Balances ref() from WebPopupMenu::create.
175 }
176
177 void WebPopupMenuImpl::willStartLiveResize()
178 {
179 }
180
181 void WebPopupMenuImpl::resize(const WebSize& newSize)
182 {
183     if (m_size == newSize)
184         return;
185     m_size = newSize;
186
187     if (m_widget) {
188         IntRect newGeometry(0, 0, m_size.width, m_size.height);
189         m_widget->setFrameRect(newGeometry);
190     }
191
192     if (m_client) {
193         WebRect damagedRect(0, 0, m_size.width, m_size.height);
194         m_client->didInvalidateRect(damagedRect);
195     }
196
197     if (m_rootLayer)
198         m_rootLayer->layer()->setBounds(newSize);
199 }
200
201 void WebPopupMenuImpl::willEndLiveResize()
202 {
203 }
204
205 void WebPopupMenuImpl::beginFrame(const WebBeginFrameArgs&)
206 {
207 }
208
209 void WebPopupMenuImpl::layout()
210 {
211 }
212
213 void WebPopupMenuImpl::paintContents(WebCanvas* canvas, const WebRect& rect, bool, WebFloatRect&,
214     WebContentLayerClient::GraphicsContextStatus contextStatus)
215 {
216     if (!m_widget)
217         return;
218
219     if (!rect.isEmpty()) {
220         GraphicsContext context(canvas,
221             contextStatus == WebContentLayerClient::GraphicsContextEnabled ? GraphicsContext::NothingDisabled : GraphicsContext::FullyDisabled);
222         m_widget->paint(&context, rect);
223     }
224 }
225
226 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect)
227 {
228     if (!m_widget)
229         return;
230
231     if (!rect.isEmpty()) {
232         GraphicsContext context(canvas);
233         context.applyDeviceScaleFactor(m_client->deviceScaleFactor());
234         m_widget->paint(&context, rect);
235     }
236 }
237
238 void WebPopupMenuImpl::themeChanged()
239 {
240     notImplemented();
241 }
242
243 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
244 {
245     if (!m_widget)
246         return false;
247
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));
254         return true;
255
256     case WebInputEvent::MouseLeave:
257         handleMouseLeave(static_cast<const WebMouseEvent&>(inputEvent));
258         return true;
259
260     case WebInputEvent::MouseWheel:
261         handleMouseWheel(static_cast<const WebMouseWheelEvent&>(inputEvent));
262         return true;
263
264     case WebInputEvent::MouseDown:
265         handleMouseDown(static_cast<const WebMouseEvent&>(inputEvent));
266         return true;
267
268     case WebInputEvent::MouseUp:
269         handleMouseUp(static_cast<const WebMouseEvent&>(inputEvent));
270         return true;
271
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));
285
286     case WebInputEvent::TouchStart:
287     case WebInputEvent::TouchMove:
288     case WebInputEvent::TouchEnd:
289     case WebInputEvent::TouchCancel:
290         return handleTouchEvent(static_cast<const WebTouchEvent&>(inputEvent));
291
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));
311
312     case WebInputEvent::Undefined:
313     case WebInputEvent::MouseEnter:
314     case WebInputEvent::ContextMenu:
315         return false;
316     }
317     return false;
318 }
319
320 void WebPopupMenuImpl::mouseCaptureLost()
321 {
322 }
323
324 void WebPopupMenuImpl::setFocus(bool)
325 {
326 }
327
328 bool WebPopupMenuImpl::setComposition(const WebString&, const WebVector<WebCompositionUnderline>&, int, int)
329 {
330     return false;
331 }
332
333 bool WebPopupMenuImpl::confirmComposition()
334 {
335     return false;
336 }
337
338 bool WebPopupMenuImpl::confirmComposition(ConfirmCompositionBehavior)
339 {
340     return false;
341 }
342
343 bool WebPopupMenuImpl::confirmComposition(const WebString&)
344 {
345     return false;
346 }
347
348 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
349 {
350     *location = 0;
351     *length = 0;
352     return false;
353 }
354
355 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
356 {
357     *location = 0;
358     *length = 0;
359     return false;
360 }
361
362 void WebPopupMenuImpl::setTextDirection(WebTextDirection)
363 {
364 }
365
366
367 //-----------------------------------------------------------------------------
368 // HostWindow
369
370 void WebPopupMenuImpl::invalidateContentsAndRootView(const IntRect& paintRect)
371 {
372     if (paintRect.isEmpty())
373         return;
374     if (m_client)
375         m_client->didInvalidateRect(paintRect);
376     if (m_rootLayer)
377         m_rootLayer->layer()->invalidateRect(FloatRect(paintRect));
378 }
379
380 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect)
381 {
382     invalidateContentsAndRootView(updateRect);
383 }
384
385 void WebPopupMenuImpl::scheduleAnimation()
386 {
387 }
388
389 void WebPopupMenuImpl::scroll()
390 {
391     if (m_rootLayer)
392         m_rootLayer->layer()->invalidate();
393 }
394
395 IntRect WebPopupMenuImpl::rootViewToScreen(const IntRect& rect) const
396 {
397     notImplemented();
398     return IntRect();
399 }
400
401 WebScreenInfo WebPopupMenuImpl::screenInfo() const
402 {
403     return WebScreenInfo();
404 }
405
406 //-----------------------------------------------------------------------------
407 // FramelessScrollViewClient
408
409 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
410 {
411     ASSERT(widget == m_widget);
412     if (m_widget) {
413         m_widget->setClient(0);
414         m_widget = 0;
415     }
416     if (m_client)
417         m_client->closeWidgetSoon();
418 }
419
420 } // namespace blink