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/FrameView.h"
35 #include "core/frame/LocalFrame.h"
36 #include "platform/PopupMenuClient.h"
37 #include "platform/geometry/FloatQuad.h"
38 #include "platform/geometry/IntPoint.h"
39 #include "platform/text/TextDirection.h"
40 #include "public/platform/WebVector.h"
41 #include "public/web/WebExternalPopupMenu.h"
42 #include "public/web/WebFrameClient.h"
43 #include "public/web/WebMenuItemInfo.h"
44 #include "public/web/WebPopupMenuInfo.h"
45 #include "web/WebLocalFrameImpl.h"
46 #include "web/WebViewImpl.h"
50 ExternalPopupMenu::ExternalPopupMenu(LocalFrame& frame, PopupMenuClient* popupMenuClient, WebViewImpl& webView)
51 : m_popupMenuClient(popupMenuClient)
54 , m_dispatchEventTimer(this, &ExternalPopupMenu::dispatchEvent)
55 , m_webExternalPopupMenu(0)
59 ExternalPopupMenu::~ExternalPopupMenu()
63 void ExternalPopupMenu::trace(Visitor* visitor)
65 visitor->trace(m_localFrame);
66 PopupMenu::trace(visitor);
69 void ExternalPopupMenu::show(const FloatQuad& controlPosition, const IntSize&, int index)
71 IntRect rect(controlPosition.enclosingBoundingBox());
72 // WebCore reuses the PopupMenu of an element.
73 // For simplicity, we do recreate the actual external popup everytime.
74 if (m_webExternalPopupMenu) {
75 m_webExternalPopupMenu->close();
76 m_webExternalPopupMenu = 0;
79 WebPopupMenuInfo info;
80 getPopupMenuInfo(info, *m_popupMenuClient);
81 if (info.items.isEmpty())
83 WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(m_localFrame.get());
84 m_webExternalPopupMenu = webframe->client()->createExternalPopupMenu(info, this);
85 if (m_webExternalPopupMenu) {
86 m_webExternalPopupMenu->show(m_localFrame->view()->contentsToWindow(rect));
88 const WebInputEvent* currentEvent = WebViewImpl::currentInputEvent();
89 if (currentEvent && currentEvent->type == WebInputEvent::MouseDown) {
90 m_syntheticEvent = adoptPtr(new WebMouseEvent);
91 *m_syntheticEvent = *static_cast<const WebMouseEvent*>(currentEvent);
92 m_syntheticEvent->type = WebInputEvent::MouseUp;
93 m_dispatchEventTimer.startOneShot(0, FROM_HERE);
94 // FIXME: show() is asynchronous. If preparing a popup is slow and
95 // a user released the mouse button before showing the popup,
96 // mouseup and click events are correctly dispatched. Dispatching
97 // the synthetic mouseup event is redundant in this case.
101 // The client might refuse to create a popup (when there is already one pending to be shown for example).
106 void ExternalPopupMenu::dispatchEvent(Timer<ExternalPopupMenu>*)
108 m_webView.handleInputEvent(*m_syntheticEvent);
109 m_syntheticEvent.clear();
112 void ExternalPopupMenu::hide()
114 if (m_popupMenuClient)
115 m_popupMenuClient->popupDidHide();
116 if (!m_webExternalPopupMenu)
118 m_webExternalPopupMenu->close();
119 m_webExternalPopupMenu = 0;
122 void ExternalPopupMenu::updateFromElement()
126 void ExternalPopupMenu::disconnectClient()
129 m_popupMenuClient = 0;
132 void ExternalPopupMenu::didChangeSelection(int index)
134 if (m_popupMenuClient)
135 m_popupMenuClient->selectionChanged(toPopupMenuItemIndex(index, *m_popupMenuClient));
138 void ExternalPopupMenu::didAcceptIndex(int index)
140 // Calling methods on the PopupMenuClient might lead to this object being
141 // derefed. This ensures it does not get deleted while we are running this
143 int popupMenuItemIndex = toPopupMenuItemIndex(index, *m_popupMenuClient);
144 RefPtrWillBeRawPtr<ExternalPopupMenu> guard(this);
146 if (m_popupMenuClient) {
147 m_popupMenuClient->popupDidHide();
148 m_popupMenuClient->valueChanged(popupMenuItemIndex);
150 m_webExternalPopupMenu = 0;
153 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
155 if (!m_popupMenuClient) {
156 m_webExternalPopupMenu = 0;
160 // Calling methods on the PopupMenuClient might lead to this object being
161 // derefed. This ensures it does not get deleted while we are running this
163 RefPtrWillBeRawPtr<ExternalPopupMenu> protect(this);
165 m_popupMenuClient->popupDidHide();
168 m_popupMenuClient->valueChanged(static_cast<unsigned>(-1), true);
170 for (size_t i = 0; i < indices.size(); ++i)
171 m_popupMenuClient->listBoxSelectItem(toPopupMenuItemIndex(indices[i], *m_popupMenuClient), (i > 0), false, (i == indices.size() - 1));
174 m_webExternalPopupMenu = 0;
177 void ExternalPopupMenu::didCancel()
179 // See comment in didAcceptIndex on why we need this.
180 RefPtrWillBeRawPtr<ExternalPopupMenu> guard(this);
182 if (m_popupMenuClient)
183 m_popupMenuClient->popupDidHide();
184 m_webExternalPopupMenu = 0;
187 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo& info, PopupMenuClient& popupMenuClient)
189 int itemCount = popupMenuClient.listSize();
191 Vector<WebMenuItemInfo> items(static_cast<size_t>(itemCount));
192 for (int i = 0; i < itemCount; ++i) {
193 PopupMenuStyle style = popupMenuClient.itemStyle(i);
194 if (style.isDisplayNone())
197 WebMenuItemInfo& popupItem = items[count++];
198 popupItem.label = popupMenuClient.itemText(i);
199 popupItem.toolTip = popupMenuClient.itemToolTip(i);
200 if (popupMenuClient.itemIsSeparator(i))
201 popupItem.type = WebMenuItemInfo::Separator;
202 else if (popupMenuClient.itemIsLabel(i))
203 popupItem.type = WebMenuItemInfo::Group;
205 popupItem.type = WebMenuItemInfo::Option;
206 popupItem.enabled = popupMenuClient.itemIsEnabled(i);
207 popupItem.checked = popupMenuClient.itemIsSelected(i);
208 popupItem.textDirection = toWebTextDirection(style.textDirection());
209 popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
212 info.itemHeight = popupMenuClient.menuStyle().font().fontMetrics().height();
213 info.itemFontSize = static_cast<int>(popupMenuClient.menuStyle().font().fontDescription().computedSize());
214 info.selectedIndex = toExternalPopupMenuItemIndex(popupMenuClient.selectedIndex(), popupMenuClient);
215 info.rightAligned = popupMenuClient.menuStyle().textDirection() == RTL;
216 info.allowMultipleSelection = popupMenuClient.multiple();
217 if (count < itemCount)
223 int ExternalPopupMenu::toPopupMenuItemIndex(int externalPopupMenuItemIndex, PopupMenuClient& popupMenuClient)
225 if (externalPopupMenuItemIndex < 0)
226 return externalPopupMenuItemIndex;
228 int itemCount = popupMenuClient.listSize();
229 int indexTracker = 0;
230 for (int i = 0; i < itemCount ; ++i) {
231 if (popupMenuClient.itemStyle(i).isDisplayNone())
233 if (indexTracker++ == externalPopupMenuItemIndex)
239 int ExternalPopupMenu::toExternalPopupMenuItemIndex(int popupMenuItemIndex, PopupMenuClient& popupMenuClient)
241 if (popupMenuItemIndex < 0)
242 return popupMenuItemIndex;
244 int itemCount = popupMenuClient.listSize();
245 int indexTracker = 0;
246 for (int i = 0; i < itemCount; ++i) {
247 if (popupMenuClient.itemStyle(i).isDisplayNone())
249 if (popupMenuItemIndex == i)