Source/WebCore: Popups on Chromium now check the minimum row height set through the...
[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 void WebPopupMenu::setMinimumRowHeight(int minimumRowHeight)
255 {
256     PopupMenuChromium::setMinimumRowHeight(minimumRowHeight);
257 }
258
259 bool WebPopupMenuImpl::setComposition(
260     const WebString& text, const WebVector<WebCompositionUnderline>& underlines,
261     int selectionStart, int selectionEnd)
262 {
263     return false;
264 }
265
266 bool WebPopupMenuImpl::confirmComposition()
267 {
268     return false;
269 }
270
271 bool WebPopupMenuImpl::confirmComposition(const WebString& text)
272 {
273     return false;
274 }
275
276 bool WebPopupMenuImpl::compositionRange(size_t* location, size_t* length)
277 {
278     *location = 0;
279     *length = 0;
280     return false;
281 }
282
283 WebTextInputType WebPopupMenuImpl::textInputType()
284 {
285     return WebTextInputTypeNone;
286 }
287
288 WebRect WebPopupMenuImpl::caretOrSelectionBounds()
289 {
290     return WebRect();
291 }
292
293 bool WebPopupMenuImpl::caretOrSelectionRange(size_t* location, size_t* length)
294 {
295     *location = 0;
296     *length = 0;
297     return false;
298 }
299
300 void WebPopupMenuImpl::setTextDirection(WebTextDirection direction)
301 {
302 }
303
304
305 //-----------------------------------------------------------------------------
306 // WebCore::HostWindow
307
308 void WebPopupMenuImpl::invalidateContents(const IntRect&, bool)
309 {
310     notImplemented();
311 }
312
313 void WebPopupMenuImpl::invalidateWindow(const IntRect&, bool)
314 {
315     notImplemented();
316 }
317
318 void WebPopupMenuImpl::invalidateContentsAndWindow(const IntRect& paintRect, bool /*immediate*/)
319 {
320     if (paintRect.isEmpty())
321         return;
322     if (m_client)
323         m_client->didInvalidateRect(paintRect);
324 }
325
326 void WebPopupMenuImpl::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
327 {
328     invalidateContentsAndWindow(updateRect, immediate);
329 }
330
331 void WebPopupMenuImpl::scheduleAnimation()
332 {
333 }
334
335 void WebPopupMenuImpl::scroll(const IntSize& scrollDelta,
336                               const IntRect& scrollRect,
337                               const IntRect& clipRect)
338 {
339     if (m_client) {
340         int dx = scrollDelta.width();
341         int dy = scrollDelta.height();
342         m_client->didScrollRect(dx, dy, clipRect);
343     }
344 }
345
346 IntPoint WebPopupMenuImpl::screenToWindow(const IntPoint& point) const
347 {
348     notImplemented();
349     return IntPoint();
350 }
351
352 IntRect WebPopupMenuImpl::windowToScreen(const IntRect& rect) const
353 {
354     notImplemented();
355     return IntRect();
356 }
357
358 void WebPopupMenuImpl::scrollRectIntoView(const IntRect&, const ScrollView*) const
359 {
360     // Nothing to be done here since we do not have the concept of a container
361     // that implements its own scrolling.
362 }
363
364 void WebPopupMenuImpl::scrollbarsModeDidChange() const
365 {
366     // Nothing to be done since we have no concept of different scrollbar modes.
367 }
368
369 void WebPopupMenuImpl::setCursor(const WebCore::Cursor&)
370 {
371 }
372
373 void WebPopupMenuImpl::setCursorHiddenUntilMouseMoves(bool)
374 {
375 }
376
377 //-----------------------------------------------------------------------------
378 // WebCore::FramelessScrollViewClient
379
380 void WebPopupMenuImpl::popupClosed(FramelessScrollView* widget)
381 {
382     ASSERT(widget == m_widget);
383     if (m_widget) {
384         m_widget->setClient(0);
385         m_widget = 0;
386     }
387     if (m_client)
388         m_client->closeWidgetSoon();
389 }
390
391 } // namespace WebKit