Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / ExternalPopupMenu.cpp
1 /*
2  * Copyright (C) 2010 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/ExternalPopupMenu.h"
33
34 #include "core/frame/FrameHost.h"
35 #include "core/frame/FrameView.h"
36 #include "core/frame/LocalFrame.h"
37 #include "core/frame/PinchViewport.h"
38 #include "core/page/Page.h"
39 #include "platform/PopupMenuClient.h"
40 #include "platform/geometry/FloatQuad.h"
41 #include "platform/geometry/IntPoint.h"
42 #include "platform/text/TextDirection.h"
43 #include "public/platform/WebVector.h"
44 #include "public/web/WebExternalPopupMenu.h"
45 #include "public/web/WebFrameClient.h"
46 #include "public/web/WebMenuItemInfo.h"
47 #include "public/web/WebPopupMenuInfo.h"
48 #include "web/WebLocalFrameImpl.h"
49 #include "web/WebViewImpl.h"
50
51 namespace blink {
52
53 ExternalPopupMenu::ExternalPopupMenu(LocalFrame& frame, PopupMenuClient* popupMenuClient, WebViewImpl& webView)
54     : m_popupMenuClient(popupMenuClient)
55     , m_localFrame(frame)
56     , m_webView(webView)
57     , m_dispatchEventTimer(this, &ExternalPopupMenu::dispatchEvent)
58     , m_webExternalPopupMenu(0)
59 {
60 }
61
62 ExternalPopupMenu::~ExternalPopupMenu()
63 {
64 }
65
66 void ExternalPopupMenu::trace(Visitor* visitor)
67 {
68     visitor->trace(m_localFrame);
69     PopupMenu::trace(visitor);
70 }
71
72 void ExternalPopupMenu::show(const FloatQuad& controlPosition, const IntSize&, int index)
73 {
74     IntRect rect(controlPosition.enclosingBoundingBox());
75     // WebCore reuses the PopupMenu of an element.
76     // For simplicity, we do recreate the actual external popup everytime.
77     if (m_webExternalPopupMenu) {
78         m_webExternalPopupMenu->close();
79         m_webExternalPopupMenu = 0;
80     }
81
82     WebPopupMenuInfo info;
83     getPopupMenuInfo(info, *m_popupMenuClient);
84     if (info.items.isEmpty())
85         return;
86     WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(m_localFrame.get());
87     m_webExternalPopupMenu = webframe->client()->createExternalPopupMenu(info, this);
88     if (m_webExternalPopupMenu) {
89         // FIXME: Standardize viewport coordinate conversions. crbug.com/371902.
90         IntRect rectInViewport = m_localFrame->view()->contentsToWindow(rect);
91         if (m_webView.pinchVirtualViewportEnabled())
92             rectInViewport.moveBy(-flooredIntPoint(m_webView.page()->frameHost().pinchViewport().location()));
93         m_webExternalPopupMenu->show(rectInViewport);
94 #if OS(MACOSX)
95         const WebInputEvent* currentEvent = WebViewImpl::currentInputEvent();
96         if (currentEvent && currentEvent->type == WebInputEvent::MouseDown) {
97             m_syntheticEvent = adoptPtr(new WebMouseEvent);
98             *m_syntheticEvent = *static_cast<const WebMouseEvent*>(currentEvent);
99             m_syntheticEvent->type = WebInputEvent::MouseUp;
100             m_dispatchEventTimer.startOneShot(0, FROM_HERE);
101             // FIXME: show() is asynchronous. If preparing a popup is slow and
102             // a user released the mouse button before showing the popup,
103             // mouseup and click events are correctly dispatched. Dispatching
104             // the synthetic mouseup event is redundant in this case.
105         }
106 #endif
107     } else {
108         // The client might refuse to create a popup (when there is already one pending to be shown for example).
109         didCancel();
110     }
111 }
112
113 void ExternalPopupMenu::dispatchEvent(Timer<ExternalPopupMenu>*)
114 {
115     m_webView.handleInputEvent(*m_syntheticEvent);
116     m_syntheticEvent.clear();
117 }
118
119 void ExternalPopupMenu::hide()
120 {
121     if (m_popupMenuClient)
122         m_popupMenuClient->popupDidHide();
123     if (!m_webExternalPopupMenu)
124         return;
125     m_webExternalPopupMenu->close();
126     m_webExternalPopupMenu = 0;
127 }
128
129 void ExternalPopupMenu::updateFromElement()
130 {
131 }
132
133 void ExternalPopupMenu::disconnectClient()
134 {
135     hide();
136     m_popupMenuClient = 0;
137 }
138
139 void ExternalPopupMenu::didChangeSelection(int index)
140 {
141     if (m_popupMenuClient)
142         m_popupMenuClient->selectionChanged(toPopupMenuItemIndex(index, *m_popupMenuClient));
143 }
144
145 void ExternalPopupMenu::didAcceptIndex(int index)
146 {
147     // Calling methods on the PopupMenuClient might lead to this object being
148     // derefed. This ensures it does not get deleted while we are running this
149     // method.
150     int popupMenuItemIndex = toPopupMenuItemIndex(index, *m_popupMenuClient);
151     RefPtrWillBeRawPtr<ExternalPopupMenu> guard(this);
152
153     if (m_popupMenuClient) {
154         m_popupMenuClient->popupDidHide();
155         m_popupMenuClient->valueChanged(popupMenuItemIndex);
156     }
157     m_webExternalPopupMenu = 0;
158 }
159
160 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
161 {
162     if (!m_popupMenuClient) {
163         m_webExternalPopupMenu = 0;
164         return;
165     }
166
167     // Calling methods on the PopupMenuClient might lead to this object being
168     // derefed. This ensures it does not get deleted while we are running this
169     // method.
170     RefPtrWillBeRawPtr<ExternalPopupMenu> protect(this);
171
172     m_popupMenuClient->popupDidHide();
173
174     if (!indices.size())
175         m_popupMenuClient->valueChanged(static_cast<unsigned>(-1), true);
176     else {
177         for (size_t i = 0; i < indices.size(); ++i)
178             m_popupMenuClient->listBoxSelectItem(toPopupMenuItemIndex(indices[i], *m_popupMenuClient), (i > 0), false, (i == indices.size() - 1));
179     }
180
181     m_webExternalPopupMenu = 0;
182 }
183
184 void ExternalPopupMenu::didCancel()
185 {
186     // See comment in didAcceptIndex on why we need this.
187     RefPtrWillBeRawPtr<ExternalPopupMenu> guard(this);
188
189     if (m_popupMenuClient)
190         m_popupMenuClient->popupDidHide();
191     m_webExternalPopupMenu = 0;
192 }
193
194 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo& info, PopupMenuClient& popupMenuClient)
195 {
196     int itemCount = popupMenuClient.listSize();
197     int count = 0;
198     Vector<WebMenuItemInfo> items(static_cast<size_t>(itemCount));
199     for (int i = 0; i < itemCount; ++i) {
200         PopupMenuStyle style = popupMenuClient.itemStyle(i);
201         if (style.isDisplayNone())
202             continue;
203
204         WebMenuItemInfo& popupItem = items[count++];
205         popupItem.label = popupMenuClient.itemText(i);
206         popupItem.toolTip = popupMenuClient.itemToolTip(i);
207         if (popupMenuClient.itemIsSeparator(i))
208             popupItem.type = WebMenuItemInfo::Separator;
209         else if (popupMenuClient.itemIsLabel(i))
210             popupItem.type = WebMenuItemInfo::Group;
211         else
212             popupItem.type = WebMenuItemInfo::Option;
213         popupItem.enabled = popupMenuClient.itemIsEnabled(i);
214         popupItem.checked = popupMenuClient.itemIsSelected(i);
215         popupItem.textDirection = toWebTextDirection(style.textDirection());
216         popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
217     }
218
219     info.itemHeight = popupMenuClient.menuStyle().font().fontMetrics().height();
220     info.itemFontSize = static_cast<int>(popupMenuClient.menuStyle().font().fontDescription().computedSize());
221     info.selectedIndex = toExternalPopupMenuItemIndex(popupMenuClient.selectedIndex(), popupMenuClient);
222     info.rightAligned = popupMenuClient.menuStyle().textDirection() == RTL;
223     info.allowMultipleSelection = popupMenuClient.multiple();
224     if (count < itemCount)
225         items.shrink(count);
226     info.items = items;
227
228 }
229
230 int ExternalPopupMenu::toPopupMenuItemIndex(int externalPopupMenuItemIndex, PopupMenuClient& popupMenuClient)
231 {
232     if (externalPopupMenuItemIndex < 0)
233         return externalPopupMenuItemIndex;
234
235     int itemCount = popupMenuClient.listSize();
236     int indexTracker = 0;
237     for (int i = 0; i < itemCount ; ++i) {
238         if (popupMenuClient.itemStyle(i).isDisplayNone())
239             continue;
240         if (indexTracker++ == externalPopupMenuItemIndex)
241             return i;
242     }
243     return -1;
244 }
245
246 int ExternalPopupMenu::toExternalPopupMenuItemIndex(int popupMenuItemIndex, PopupMenuClient& popupMenuClient)
247 {
248     if (popupMenuItemIndex < 0)
249         return popupMenuItemIndex;
250
251     int itemCount = popupMenuClient.listSize();
252     int indexTracker = 0;
253     for (int i = 0; i < itemCount; ++i) {
254         if (popupMenuClient.itemStyle(i).isDisplayNone())
255             continue;
256         if (popupMenuItemIndex == i)
257             return indexTracker;
258         ++indexTracker;
259     }
260     return -1;
261 }
262
263 } // namespace blink