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 "ExternalPopupMenu.h"
34 #include "WebExternalPopupMenu.h"
35 #include "WebMenuItemInfo.h"
36 #include "WebPopupMenuInfo.h"
37 #include "WebViewClient.h"
38 #include "WebViewImpl.h"
39 #include "core/frame/Frame.h"
40 #include "core/frame/FrameView.h"
41 #include "platform/PopupMenuClient.h"
42 #include "platform/geometry/FloatQuad.h"
43 #include "platform/geometry/IntPoint.h"
44 #include "platform/text/TextDirection.h"
45 #include "public/platform/WebVector.h"
47 using namespace WebCore;
51 ExternalPopupMenu::ExternalPopupMenu(Frame& frame, PopupMenuClient* popupMenuClient, WebViewImpl& webView)
52 : m_popupMenuClient(popupMenuClient)
53 , m_frameView(frame.view())
55 , m_dispatchEventTimer(this, &ExternalPopupMenu::dispatchEvent)
56 , m_webExternalPopupMenu(0)
60 ExternalPopupMenu::~ExternalPopupMenu()
64 void ExternalPopupMenu::show(const FloatQuad& controlPosition, const IntSize&, int index)
66 IntRect rect(controlPosition.enclosingBoundingBox());
67 // WebCore reuses the PopupMenu of a page.
68 // For simplicity, we do recreate the actual external popup everytime.
71 WebPopupMenuInfo info;
72 getPopupMenuInfo(&info);
73 if (info.items.isEmpty())
75 m_webExternalPopupMenu = m_webView.client()->createExternalPopupMenu(info, this);
76 if (m_webExternalPopupMenu) {
77 m_webExternalPopupMenu->show(m_frameView->contentsToWindow(rect));
79 const WebInputEvent* currentEvent = WebViewImpl::currentInputEvent();
80 if (currentEvent && currentEvent->type == WebInputEvent::MouseDown) {
81 m_syntheticEvent = adoptPtr(new WebMouseEvent);
82 *m_syntheticEvent = *static_cast<const WebMouseEvent*>(currentEvent);
83 m_syntheticEvent->type = WebInputEvent::MouseUp;
84 m_dispatchEventTimer.startOneShot(0);
85 // FIXME: show() is asynchronous. If preparing a popup is slow and
86 // a user released the mouse button before showing the popup,
87 // mouseup and click events are correctly dispatched. Dispatching
88 // the synthetic mouseup event is redundant in this case.
92 // The client might refuse to create a popup (when there is already one pending to be shown for example).
97 void ExternalPopupMenu::dispatchEvent(Timer<ExternalPopupMenu>*)
99 m_webView.handleInputEvent(*m_syntheticEvent);
100 m_syntheticEvent.clear();
103 void ExternalPopupMenu::hide()
105 if (m_popupMenuClient)
106 m_popupMenuClient->popupDidHide();
107 if (!m_webExternalPopupMenu)
109 m_webExternalPopupMenu->close();
110 m_webExternalPopupMenu = 0;
113 void ExternalPopupMenu::updateFromElement()
117 void ExternalPopupMenu::disconnectClient()
120 m_popupMenuClient = 0;
123 void ExternalPopupMenu::didChangeSelection(int index)
125 if (m_popupMenuClient)
126 m_popupMenuClient->selectionChanged(index);
129 void ExternalPopupMenu::didAcceptIndex(int index)
131 // Calling methods on the PopupMenuClient might lead to this object being
132 // derefed. This ensures it does not get deleted while we are running this
134 RefPtr<ExternalPopupMenu> guard(this);
136 if (m_popupMenuClient) {
137 m_popupMenuClient->valueChanged(index);
138 // The call to valueChanged above might have lead to a call to
139 // disconnectClient, so we might not have a PopupMenuClient anymore.
140 if (m_popupMenuClient)
141 m_popupMenuClient->popupDidHide();
143 m_webExternalPopupMenu = 0;
146 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
148 if (!m_popupMenuClient) {
149 m_webExternalPopupMenu = 0;
153 // Calling methods on the PopupMenuClient might lead to this object being
154 // derefed. This ensures it does not get deleted while we are running this
156 RefPtr<ExternalPopupMenu> protect(this);
159 m_popupMenuClient->valueChanged(-1, true);
161 for (size_t i = 0; i < indices.size(); ++i)
162 m_popupMenuClient->listBoxSelectItem(indices[i], (i > 0), false, (i == indices.size() - 1));
165 // The call to valueChanged above might have lead to a call to
166 // disconnectClient, so we might not have a PopupMenuClient anymore.
167 if (m_popupMenuClient)
168 m_popupMenuClient->popupDidHide();
170 m_webExternalPopupMenu = 0;
173 void ExternalPopupMenu::didCancel()
175 // See comment in didAcceptIndex on why we need this.
176 RefPtr<ExternalPopupMenu> guard(this);
178 if (m_popupMenuClient)
179 m_popupMenuClient->popupDidHide();
180 m_webExternalPopupMenu = 0;
183 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo* info)
185 int itemCount = m_popupMenuClient->listSize();
186 WebVector<WebMenuItemInfo> items(static_cast<size_t>(itemCount));
187 for (int i = 0; i < itemCount; ++i) {
188 WebMenuItemInfo& popupItem = items[i];
189 popupItem.label = m_popupMenuClient->itemText(i);
190 popupItem.toolTip = m_popupMenuClient->itemToolTip(i);
191 if (m_popupMenuClient->itemIsSeparator(i))
192 popupItem.type = WebMenuItemInfo::Separator;
193 else if (m_popupMenuClient->itemIsLabel(i))
194 popupItem.type = WebMenuItemInfo::Group;
196 popupItem.type = WebMenuItemInfo::Option;
197 popupItem.enabled = m_popupMenuClient->itemIsEnabled(i);
198 popupItem.checked = m_popupMenuClient->itemIsSelected(i);
199 PopupMenuStyle style = m_popupMenuClient->itemStyle(i);
200 if (style.textDirection() == WebCore::RTL)
201 popupItem.textDirection = WebTextDirectionRightToLeft;
203 popupItem.textDirection = WebTextDirectionLeftToRight;
204 popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
207 info->itemHeight = m_popupMenuClient->menuStyle().font().fontMetrics().height();
208 info->itemFontSize = static_cast<int>(m_popupMenuClient->menuStyle().font().fontDescription().computedSize());
209 info->selectedIndex = m_popupMenuClient->selectedIndex();
210 info->rightAligned = m_popupMenuClient->menuStyle().textDirection() == WebCore::RTL;
211 info->allowMultipleSelection = m_popupMenuClient->multiple();
212 info->items.swap(items);