2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "web/ExternalPopupMenu.h"
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"
53 ExternalPopupMenu::ExternalPopupMenu(LocalFrame& frame, PopupMenuClient* popupMenuClient, WebViewImpl& webView)
54 : m_popupMenuClient(popupMenuClient)
57 , m_dispatchEventTimer(this, &ExternalPopupMenu::dispatchEvent)
58 , m_webExternalPopupMenu(0)
62 ExternalPopupMenu::~ExternalPopupMenu()
66 void ExternalPopupMenu::trace(Visitor* visitor)
68 visitor->trace(m_localFrame);
69 PopupMenu::trace(visitor);
72 void ExternalPopupMenu::show(const FloatQuad& controlPosition, const IntSize&, int index)
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;
82 WebPopupMenuInfo info;
83 getPopupMenuInfo(info, *m_popupMenuClient);
84 if (info.items.isEmpty())
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);
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.
108 // The client might refuse to create a popup (when there is already one pending to be shown for example).
113 void ExternalPopupMenu::dispatchEvent(Timer<ExternalPopupMenu>*)
115 m_webView.handleInputEvent(*m_syntheticEvent);
116 m_syntheticEvent.clear();
119 void ExternalPopupMenu::hide()
121 if (m_popupMenuClient)
122 m_popupMenuClient->popupDidHide();
123 if (!m_webExternalPopupMenu)
125 m_webExternalPopupMenu->close();
126 m_webExternalPopupMenu = 0;
129 void ExternalPopupMenu::updateFromElement()
133 void ExternalPopupMenu::disconnectClient()
136 m_popupMenuClient = 0;
139 void ExternalPopupMenu::didChangeSelection(int index)
141 if (m_popupMenuClient)
142 m_popupMenuClient->selectionChanged(toPopupMenuItemIndex(index, *m_popupMenuClient));
145 void ExternalPopupMenu::didAcceptIndex(int index)
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
150 int popupMenuItemIndex = toPopupMenuItemIndex(index, *m_popupMenuClient);
151 RefPtrWillBeRawPtr<ExternalPopupMenu> guard(this);
153 if (m_popupMenuClient) {
154 m_popupMenuClient->popupDidHide();
155 m_popupMenuClient->valueChanged(popupMenuItemIndex);
157 m_webExternalPopupMenu = 0;
160 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
162 if (!m_popupMenuClient) {
163 m_webExternalPopupMenu = 0;
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
170 RefPtrWillBeRawPtr<ExternalPopupMenu> protect(this);
172 m_popupMenuClient->popupDidHide();
175 m_popupMenuClient->valueChanged(static_cast<unsigned>(-1), true);
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));
181 m_webExternalPopupMenu = 0;
184 void ExternalPopupMenu::didCancel()
186 // See comment in didAcceptIndex on why we need this.
187 RefPtrWillBeRawPtr<ExternalPopupMenu> guard(this);
189 if (m_popupMenuClient)
190 m_popupMenuClient->popupDidHide();
191 m_webExternalPopupMenu = 0;
194 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo& info, PopupMenuClient& popupMenuClient)
196 int itemCount = popupMenuClient.listSize();
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())
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;
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();
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)
230 int ExternalPopupMenu::toPopupMenuItemIndex(int externalPopupMenuItemIndex, PopupMenuClient& popupMenuClient)
232 if (externalPopupMenuItemIndex < 0)
233 return externalPopupMenuItemIndex;
235 int itemCount = popupMenuClient.listSize();
236 int indexTracker = 0;
237 for (int i = 0; i < itemCount ; ++i) {
238 if (popupMenuClient.itemStyle(i).isDisplayNone())
240 if (indexTracker++ == externalPopupMenuItemIndex)
246 int ExternalPopupMenu::toExternalPopupMenuItemIndex(int popupMenuItemIndex, PopupMenuClient& popupMenuClient)
248 if (popupMenuItemIndex < 0)
249 return popupMenuItemIndex;
251 int itemCount = popupMenuClient.listSize();
252 int indexTracker = 0;
253 for (int i = 0; i < itemCount; ++i) {
254 if (popupMenuClient.itemStyle(i).isDisplayNone())
256 if (popupMenuItemIndex == i)