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/WebMenuItemInfo.h"
43 #include "public/web/WebPopupMenuInfo.h"
44 #include "public/web/WebViewClient.h"
45 #include "web/WebViewImpl.h"
49 ExternalPopupMenu::ExternalPopupMenu(LocalFrame& frame, PopupMenuClient* popupMenuClient, WebViewImpl& webView)
50 : m_popupMenuClient(popupMenuClient)
51 , m_frameView(frame.view())
53 , m_dispatchEventTimer(this, &ExternalPopupMenu::dispatchEvent)
54 , m_webExternalPopupMenu(0)
58 ExternalPopupMenu::~ExternalPopupMenu()
62 void ExternalPopupMenu::show(const FloatQuad& controlPosition, const IntSize&, int index)
64 IntRect rect(controlPosition.enclosingBoundingBox());
65 // WebCore reuses the PopupMenu of an element.
66 // For simplicity, we do recreate the actual external popup everytime.
67 if (m_webExternalPopupMenu) {
68 m_webExternalPopupMenu->close();
69 m_webExternalPopupMenu = 0;
72 WebPopupMenuInfo info;
73 getPopupMenuInfo(info, *m_popupMenuClient);
74 if (info.items.isEmpty())
76 m_webExternalPopupMenu = m_webView.client()->createExternalPopupMenu(info, this);
77 if (m_webExternalPopupMenu) {
78 m_webExternalPopupMenu->show(m_frameView->contentsToWindow(rect));
80 const WebInputEvent* currentEvent = WebViewImpl::currentInputEvent();
81 if (currentEvent && currentEvent->type == WebInputEvent::MouseDown) {
82 m_syntheticEvent = adoptPtr(new WebMouseEvent);
83 *m_syntheticEvent = *static_cast<const WebMouseEvent*>(currentEvent);
84 m_syntheticEvent->type = WebInputEvent::MouseUp;
85 m_dispatchEventTimer.startOneShot(0, FROM_HERE);
86 // FIXME: show() is asynchronous. If preparing a popup is slow and
87 // a user released the mouse button before showing the popup,
88 // mouseup and click events are correctly dispatched. Dispatching
89 // the synthetic mouseup event is redundant in this case.
93 // The client might refuse to create a popup (when there is already one pending to be shown for example).
98 void ExternalPopupMenu::dispatchEvent(Timer<ExternalPopupMenu>*)
100 m_webView.handleInputEvent(*m_syntheticEvent);
101 m_syntheticEvent.clear();
104 void ExternalPopupMenu::hide()
106 if (m_popupMenuClient)
107 m_popupMenuClient->popupDidHide();
108 if (!m_webExternalPopupMenu)
110 m_webExternalPopupMenu->close();
111 m_webExternalPopupMenu = 0;
114 void ExternalPopupMenu::updateFromElement()
118 void ExternalPopupMenu::disconnectClient()
121 m_popupMenuClient = 0;
124 void ExternalPopupMenu::didChangeSelection(int index)
126 if (m_popupMenuClient)
127 m_popupMenuClient->selectionChanged(toPopupMenuItemIndex(index, *m_popupMenuClient));
130 void ExternalPopupMenu::didAcceptIndex(int index)
132 // Calling methods on the PopupMenuClient might lead to this object being
133 // derefed. This ensures it does not get deleted while we are running this
135 int popupMenuItemIndex = toPopupMenuItemIndex(index, *m_popupMenuClient);
136 RefPtr<ExternalPopupMenu> guard(this);
138 if (m_popupMenuClient) {
139 m_popupMenuClient->popupDidHide();
140 m_popupMenuClient->valueChanged(popupMenuItemIndex);
142 m_webExternalPopupMenu = 0;
145 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
147 if (!m_popupMenuClient) {
148 m_webExternalPopupMenu = 0;
152 // Calling methods on the PopupMenuClient might lead to this object being
153 // derefed. This ensures it does not get deleted while we are running this
155 RefPtr<ExternalPopupMenu> protect(this);
158 m_popupMenuClient->valueChanged(static_cast<unsigned>(-1), true);
160 for (size_t i = 0; i < indices.size(); ++i)
161 m_popupMenuClient->listBoxSelectItem(toPopupMenuItemIndex(indices[i], *m_popupMenuClient), (i > 0), false, (i == indices.size() - 1));
164 // The call to valueChanged above might have lead to a call to
165 // disconnectClient, so we might not have a PopupMenuClient anymore.
166 if (m_popupMenuClient)
167 m_popupMenuClient->popupDidHide();
169 m_webExternalPopupMenu = 0;
172 void ExternalPopupMenu::didCancel()
174 // See comment in didAcceptIndex on why we need this.
175 RefPtr<ExternalPopupMenu> guard(this);
177 if (m_popupMenuClient)
178 m_popupMenuClient->popupDidHide();
179 m_webExternalPopupMenu = 0;
182 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo& info, PopupMenuClient& popupMenuClient)
184 int itemCount = popupMenuClient.listSize();
186 Vector<WebMenuItemInfo> items(static_cast<size_t>(itemCount));
187 for (int i = 0; i < itemCount; ++i) {
188 PopupMenuStyle style = popupMenuClient.itemStyle(i);
189 if (style.isDisplayNone())
192 WebMenuItemInfo& popupItem = items[count++];
193 popupItem.label = popupMenuClient.itemText(i);
194 popupItem.toolTip = popupMenuClient.itemToolTip(i);
195 if (popupMenuClient.itemIsSeparator(i))
196 popupItem.type = WebMenuItemInfo::Separator;
197 else if (popupMenuClient.itemIsLabel(i))
198 popupItem.type = WebMenuItemInfo::Group;
200 popupItem.type = WebMenuItemInfo::Option;
201 popupItem.enabled = popupMenuClient.itemIsEnabled(i);
202 popupItem.checked = popupMenuClient.itemIsSelected(i);
203 popupItem.textDirection = toWebTextDirection(style.textDirection());
204 popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
207 info.itemHeight = popupMenuClient.menuStyle().font().fontMetrics().height();
208 info.itemFontSize = static_cast<int>(popupMenuClient.menuStyle().font().fontDescription().computedSize());
209 info.selectedIndex = toExternalPopupMenuItemIndex(popupMenuClient.selectedIndex(), popupMenuClient);
210 info.rightAligned = popupMenuClient.menuStyle().textDirection() == RTL;
211 info.allowMultipleSelection = popupMenuClient.multiple();
212 if (count < itemCount)
218 int ExternalPopupMenu::toPopupMenuItemIndex(int externalPopupMenuItemIndex, PopupMenuClient& popupMenuClient)
220 if (externalPopupMenuItemIndex < 0)
221 return externalPopupMenuItemIndex;
223 int itemCount = popupMenuClient.listSize();
224 int indexTracker = 0;
225 for (int i = 0; i < itemCount ; ++i) {
226 if (popupMenuClient.itemStyle(i).isDisplayNone())
228 if (indexTracker++ == externalPopupMenuItemIndex)
234 int ExternalPopupMenu::toExternalPopupMenuItemIndex(int popupMenuItemIndex, PopupMenuClient& popupMenuClient)
236 if (popupMenuItemIndex < 0)
237 return popupMenuItemIndex;
239 int itemCount = popupMenuClient.listSize();
240 int indexTracker = 0;
241 for (int i = 0; i < itemCount; ++i) {
242 if (popupMenuClient.itemStyle(i).isDisplayNone())
244 if (popupMenuItemIndex == i)