77d5b826b439acef18a781961970cbab78f0f5c3
[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 using namespace WebCore;
56
57 namespace WebKit {
58
59 // WebPopupMenu ---------------------------------------------------------------
60
61 WebPopupMenu* WebPopupMenu::create(WebWidgetClient* client)
62 {
63     // Pass the WebPopupMenuImpl's self-reference to the caller.
64     return adoptRef(new WebPopupMenuImpl(client)).leakRef();
65 }
66
67 // WebWidget ------------------------------------------------------------------
68
69 WebPopupMenuImpl::WebPopupMenuImpl(WebWidgetClient* client)
70     : m_client(client)
71     , m_widget(0)
72 {
73     // set to impossible point so we always get the first mouse pos
74     m_lastMousePosition = WebPoint(-1, -1);
75 }
76
77 WebPopupMenuImpl::~WebPopupMenuImpl()
78 {
79     if (m_widget)
80         m_widget->setClient(0);
81 }
82
83 void WebPopupMenuImpl::Init(FramelessScrollView* widget, const WebRect& bounds)
84 {
85     m_widget = widget;
86     m_widget->setClient(this);
87
88     if (m_client) {
89         m_client->setWindowRect(bounds);
90         m_client->show(WebNavigationPolicy());  // Policy is ignored
91     }
92 }
93
94 void WebPopupMenuImpl::MouseMove(const WebMouseEvent& event)
95 {
96     // don't send mouse move messages if the mouse hasn't moved.
97     if (event.x != m_lastMousePosition.x || event.y != m_lastMousePosition.y) {
98         m_lastMousePosition = WebPoint(event.x, event.y);
99         m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
100
101         // We cannot call setToolTipText() in PopupContainer, because PopupContainer is in WebCore, and we cannot refer to WebKit from Webcore.
102         WebCore::PopupContainer* container = static_cast<WebCore::PopupContainer*>(m_widget);
103         client()->setToolTipText(container->getSelectedItemToolTip(), container->menuStyle().textDirection() == WebCore::RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight);
104     }
105 }
106
107 void WebPopupMenuImpl::MouseLeave(const WebMouseEvent& event)
108 {
109     m_widget->handleMouseMoveEvent(PlatformMouseEventBuilder(m_widget, event));
110 }
111
112 void WebPopupMenuImpl::MouseDown(const WebMouseEvent& event)
113 {
114     m_widget->handleMouseDownEvent(PlatformMouseEventBuilder(m_widget, event));
115 }
116
117 void WebPopupMenuImpl::MouseUp(const WebMouseEvent& event)
118 {
119     mouseCaptureLost();
120     m_widget->handleMouseReleaseEvent(PlatformMouseEventBuilder(m_widget, event));
121 }
122
123 void WebPopupMenuImpl::MouseWheel(const WebMouseWheelEvent& event)
124 {
125     m_widget->handleWheelEvent(PlatformWheelEventBuilder(m_widget, event));
126 }
127
128 bool WebPopupMenuImpl::KeyEvent(const WebKeyboardEvent& event)
129 {
130     return m_widget->handleKeyEvent(PlatformKeyboardEventBuilder(event));
131 }
132
133 // WebWidget -------------------------------------------------------------------
134
135 void WebPopupMenuImpl::close()
136 {
137     if (m_widget)
138         m_widget->hide();
139
140     m_client = 0;
141
142     deref();  // Balances ref() from WebWidget::Create
143 }
144
145 void WebPopupMenuImpl::willStartLiveResize()
146 {
147 }
148
149 void WebPopupMenuImpl::resize(const WebSize& newSize)
150 {
151     if (m_size == newSize)
152         return;
153     m_size = newSize;
154
155     if (m_widget) {
156         IntRect newGeometry(0, 0, m_size.width, m_size.height);
157         m_widget->setFrameRect(newGeometry);
158     }
159
160     if (m_client) {
161         WebRect damagedRect(0, 0, m_size.width, m_size.height);
162         m_client->didInvalidateRect(damagedRect);
163     }
164 }
165
166 void WebPopupMenuImpl::willEndLiveResize()
167 {
168 }
169
170 void WebPopupMenuImpl::animate(double)
171 {
172 }
173
174 void WebPopupMenuImpl::layout()
175 {
176 }
177
178 void WebPopupMenuImpl::paint(WebCanvas* canvas, const WebRect& rect)
179 {
180     if (!m_widget)
181         return;
182
183     if (!rect.isEmpty())
184         m_widget->paint(&GraphicsContextBuilder(canvas).context(), rect);
185 }
186
187 void WebPopupMenuImpl::themeChanged()
188 {
189     notImplemented();
190 }
191
192 void WebPopupMenuImpl::composite(bool finish)
193 {
194     notImplemented();
195 }
196
197 bool WebPopupMenuImpl::handleInputEvent(const WebInputEvent& inputEvent)
198 {
199     if (!m_widget)
200         return false;
201
202     // TODO (jcampan): WebKit seems to always return false on mouse events
203     // methods. For now we'll assume it has processed them (as we are only
204     // interested in whether keyboard events are processed).
205     switch (inputEvent.type) {
206     case WebInputEvent::MouseMove:
207         MouseMove(*static_cast<const WebMouseEvent*>(&inputEvent));
208         return true;
209
210     case WebInputEvent::MouseLeave:
211         MouseLeave(*static_cast<const WebMouseEvent*>(&inputEvent));
212         return true;
213
214     case WebInputEvent::MouseWheel:
215         MouseWheel(*static_cast<const WebMouseWheelEvent*>(&inputEvent));
216         return true;
217
218     case WebInputEvent::MouseDown:
219         MouseDown(*static_cast<const WebMouseEvent*>(&inputEvent));
220         return true;
221
222     case WebInputEvent::MouseUp:
223         MouseUp(*static_cast<const WebMouseEvent*>(&inputEvent));
224         return true;
225
226     // In Windows, RawKeyDown only has information about the physical key, but
227     // for "selection", we need the information about the character the key
228     // translated into. For English, the physical key value and the character
229     // value are the same, hence, "selection" works for English. But for other
230     // languages, such as Hebrew, the character value is different from the
231     // physical key value. Thus, without accepting Char event type which
232     // contains the key's character value, the "selection" won't work for
233     // non-English languages, such as Hebrew.
234     case WebInputEvent::RawKeyDown:
235     case WebInputEvent::KeyDown:
236     case WebInputEvent::KeyUp:
237     case WebInputEvent::Char:
238         return KeyEvent(*static_cast<const WebKeyboardEvent*>(&inputEvent));
239
240     default:
241         break;
242     }
243     return false;
244 }
245
246 void WebPopupMenuImpl::mouseCaptureLost()
247 {
248 }
249
250 void WebPopupMenuImpl::setFocus(bool enable)
251 {
252 }
253
254 bool WebPopupMenuImpl::setComposition(
255     const WebString& text, const WebVector<WebCompositionUnderline>& underlines,
256     int selectionStart, int selectionEnd)
257 {
258     return false;
259 }
260
261 bool WebPopupMenuImpl::confirmComposition()
262 {
263     return false;
264 }
265
266 bool WebPopupMenuImpl::confirmComposition(const WebString& text)
267 {
268     return false;
269 }
270
271 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
272 {
273     *location = 0;
274     *length = 0;
275     return false;
276 }
277
278 WebTextInputType WebPopupMenuImpl::textInputType()
279 {
280     return WebTextInputTypeNone;
281 }
282
283 WebRect WebPopupMenuImpl::caretOrSelectionBounds()
284 {
285     return WebRect();
286 }
287
288 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
289 {
290     *location = 0;
291     *length = 0;
292     return false;
293 }
294
295 void WebPopupMenuImpl::setTextDirection(WebTextDirection direction)
296 {
297 }
298
299
300 //-----------------------------------------------------------------------------
301 // WebCore::HostWindow
302
303 void WebPopupMenuImpl::invalidateContents(const IntRect&, bool)
304 {
305     notImplemented();
306 }
307
308 void WebPopupMenuImpl::invalidateWindow(const IntRect&, bool)
309 {
310     notImplemented();
311 }
312
313 void WebPopupMenuImpl::invalidateContentsAndWindow(const IntRect& paintRect, bool /*immediate*/)
314 {
315     if (paintRect.isEmpty())
316         return;
317     if (m_client)
318         m_client->didInvalidateRect(paintRect);
319 }
320
321 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
322 {
323     invalidateContentsAndWindow(updateRect, immediate);
324 }
325
326 void WebPopupMenuImpl::scheduleAnimation()
327 {
328 }
329
330 void WebPopupMenuImpl::scroll(const IntSize& scrollDelta,
331                               const IntRect& scrollRect,
332                               const IntRect& clipRect)
333 {
334     if (m_client) {
335         int dx = scrollDelta.width();
336         int dy = scrollDelta.height();
337         m_client->didScrollRect(dx, dy, clipRect);
338     }
339 }
340
341 IntPoint WebPopupMenuImpl::screenToWindow(const IntPoint& point) const
342 {
343     notImplemented();
344     return IntPoint();
345 }
346
347 IntRect WebPopupMenuImpl::windowToScreen(const IntRect& rect) const
348 {
349     notImplemented();
350     return IntRect();
351 }
352
353 void WebPopupMenuImpl::scrollRectIntoView(const IntRect&, const ScrollView*) const
354 {
355     // Nothing to be done here since we do not have the concept of a container
356     // that implements its own scrolling.
357 }
358
359 void WebPopupMenuImpl::scrollbarsModeDidChange() const
360 {
361     // Nothing to be done since we have no concept of different scrollbar modes.
362 }
363
364 void WebPopupMenuImpl::setCursor(const WebCore::Cursor&)
365 {
366 }
367
368 void WebPopupMenuImpl::setCursorHiddenUntilMouseMoves(bool)
369 {
370 }
371
372 //-----------------------------------------------------------------------------
373 // WebCore::FramelessScrollViewClient
374
375 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
376 {
377     ASSERT(widget == m_widget);
378     if (m_widget) {
379         m_widget->setClient(0);
380         m_widget = 0;
381     }
382     if (m_client)
383         m_client->closeWidgetSoon();
384 }
385
386 } // namespace WebKit