tizen beta release
[framework/web/webkit-efl.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 "PopupContainer.h"
44 #include "PopupMenuChromium.h"
45 #include "SkiaUtils.h"
46
47 #include "WebInputEvent.h"
48 #include "WebInputEventConversion.h"
49 #include "WebRange.h"
50 #include "WebRect.h"
51 #include "WebViewClient.h"
52 #include "WebWidgetClient.h"
53
54 #include <skia/ext/platform_canvas.h>
55
56 #if ENABLE(GESTURE_RECOGNIZER)
57 #include "PlatformGestureEvent.h"
58 #include "PlatformGestureRecognizer.h"
59 #endif
60
61 using namespace WebCore;
62
63 namespace WebKit {
64
65 // WebPopupMenu ---------------------------------------------------------------
66
67 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
68 {
69     // Pass the WebPopupMenuImpl's self-reference to the caller.
70     return adoptRef(new WebPopupMenuImpl(client)).leakRef();
71 }
72
73 // WebWidget ------------------------------------------------------------------
74
75 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
76     : m_client(client)
77     , m_widget(0)
78 #if ENABLE(GESTURE_RECOGNIZER)
79     , m_gestureRecognizer(WebCore::PlatformGestureRecognizer::create())
80 #endif
81 {
82     // set to impossible point so we always get the first mouse pos
83     m_lastMousePosition = WebPoint(-1, -1);
84 }
85
86 WebPopupMenuImpl::~WebPopupMenuImpl()
87 {
88     if (m_widget)
89         m_widget->setClient(0);
90 }
91
92 void WebPopupMenuImpl::Init(FramelessScrollView* widget, const WebRect& bounds)
93 {
94     m_widget = widget;
95     m_widget->setClient(this);
96
97     if (m_client) {
98         m_client->setWindowRect(bounds);
99         m_client->show(WebNavigationPolicy());  // Policy is ignored
100     }
101 }
102
103 void WebPopupMenuImpl::MouseMove(const WebMouseEvent& event)
104 {
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));
109
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);
113     }
114 }
115
116 void WebPopupMenuImpl::MouseLeave(const WebMouseEvent& event)
117 {
118     m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
119 }
120
121 void WebPopupMenuImpl::MouseDown(const WebMouseEvent& event)
122 {
123     m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
124 }
125
126 void WebPopupMenuImpl::MouseUp(const WebMouseEvent& event)
127 {
128     mouseCaptureLost();
129     m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
130 }
131
132 void WebPopupMenuImpl::MouseWheel(const WebMouseWheelEvent& event)
133 {
134     m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
135 }
136
137 bool WebPopupMenuImpl::GestureEvent(const WebGestureEvent& event)
138 {
139     return m_widget->handleGestureEvent(PlatformGestureEventBuilder(m_widget, event));
140 }
141
142 #if ENABLE(TOUCH_EVENTS)
143 bool WebPopupMenuImpl::TouchEvent(const WebTouchEvent& event)
144 {
145
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]);
152 #endif
153     return defaultPrevented;
154 }
155 #endif
156
157 bool WebPopupMenuImpl::KeyEvent(const WebKeyboardEvent& event)
158 {
159     return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
160 }
161
162 // WebWidget -------------------------------------------------------------------
163
164 void WebPopupMenuImpl::close()
165 {
166     if (m_widget)
167         m_widget->hide();
168
169     m_client = 0;
170
171     deref();  // Balances ref() from WebWidget::Create
172 }
173
174 void WebPopupMenuImpl::willStartLiveResize()
175 {
176 }
177
178 void WebPopupMenuImpl::resize(const WebSize& newSize)
179 {
180     if (m_size == newSize)
181         return;
182     m_size = newSize;
183
184     if (m_widget) {
185         IntRect newGeometry(0, 0, m_size.width, m_size.height);
186         m_widget->setFrameRect(newGeometry);
187     }
188
189     if (m_client) {
190         WebRect damagedRect(0, 0, m_size.width, m_size.height);
191         m_client->didInvalidateRect(damagedRect);
192     }
193 }
194
195 void WebPopupMenuImpl::willEndLiveResize()
196 {
197 }
198
199 void WebPopupMenuImpl::animate(double)
200 {
201 }
202
203 void WebPopupMenuImpl::layout()
204 {
205 }
206
207 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect)
208 {
209     if (!m_widget)
210         return;
211
212     if (!rect.isEmpty())
213         m_widget->paint(&GraphicsContextBuilder(canvas).context(), rect);
214 }
215
216 void WebPopupMenuImpl::themeChanged()
217 {
218     notImplemented();
219 }
220
221 void WebPopupMenuImpl::composite(bool finish)
222 {
223     notImplemented();
224 }
225
226 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
227 {
228     if (!m_widget)
229         return false;
230
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));
237         return true;
238
239     case WebInputEvent::MouseLeave:
240         MouseLeave(*static_cast<const WebMouseEvent*>(&inputEvent));
241         return true;
242
243     case WebInputEvent::MouseWheel:
244         MouseWheel(*static_cast<const WebMouseWheelEvent*>(&inputEvent));
245         return true;
246
247     case WebInputEvent::MouseDown:
248         MouseDown(*static_cast<const WebMouseEvent*>(&inputEvent));
249         return true;
250
251     case WebInputEvent::MouseUp:
252         MouseUp(*static_cast<const WebMouseEvent*>(&inputEvent));
253         return true;
254
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));
268
269     case WebInputEvent::TouchStart:
270     case WebInputEvent::TouchMove:
271     case WebInputEvent::TouchEnd:
272     case WebInputEvent::TouchCancel:
273         return TouchEvent(*static_cast<const WebTouchEvent*>(&inputEvent));
274
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));
282
283     case WebInputEvent::Undefined:
284     case WebInputEvent::MouseEnter:
285     case WebInputEvent::ContextMenu:
286         return false;
287     }
288     return false;
289 }
290
291 void WebPopupMenuImpl::mouseCaptureLost()
292 {
293 }
294
295 void WebPopupMenuImpl::setFocus(bool enable)
296 {
297 }
298
299 void WebPopupMenu::setMinimumRowHeight(int minimumRowHeight)
300 {
301     PopupMenuChromium::setMinimumRowHeight(minimumRowHeight);
302 }
303
304 bool WebPopupMenuImpl::setComposition(
305     const WebString& text, const WebVector<WebCompositionUnderline>& underlines,
306     int selectionStart, int selectionEnd)
307 {
308     return false;
309 }
310
311 bool WebPopupMenuImpl::confirmComposition()
312 {
313     return false;
314 }
315
316 bool WebPopupMenuImpl::confirmComposition(const WebString& text)
317 {
318     return false;
319 }
320
321 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
322 {
323     *location = 0;
324     *length = 0;
325     return false;
326 }
327
328 WebTextInputType WebPopupMenuImpl::textInputType()
329 {
330     return WebTextInputTypeNone;
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::invalidateRootView(const IntRect&, bool)
354 {
355     notImplemented();
356 }
357
358 void WebPopupMenuImpl::invalidateContentsAndRootView(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     invalidateContentsAndRootView(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::screenToRootView(const IntPoint& point) const
387 {
388     notImplemented();
389     return IntPoint();
390 }
391
392 IntRect WebPopupMenuImpl::rootViewToScreen(const IntRect& rect) const
393 {
394     notImplemented();
395     return IntRect();
396 }
397
398 void WebPopupMenuImpl::scrollRectIntoView(const IntRect&) 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