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 "FrameView.h"
36 #include "PopupMenuClient.h"
37 #include "TextDirection.h"
38 #include "WebExternalPopupMenu.h"
39 #include "WebMenuItemInfo.h"
40 #include "WebPopupMenuInfo.h"
41 #include "WebVector.h"
42 #include "WebViewClient.h"
44 using namespace WebCore;
48 ExternalPopupMenu::ExternalPopupMenu(PopupMenuClient* popupMenuClient,
49 WebViewClient* webViewClient)
50 : m_popupMenuClient(popupMenuClient)
51 , m_webViewClient(webViewClient)
52 , m_webExternalPopupMenu(0)
56 ExternalPopupMenu::~ExternalPopupMenu()
60 void ExternalPopupMenu::show(const IntRect& rect, FrameView* v, int index)
62 // WebCore reuses the PopupMenu of a page.
63 // For simplicity, we do recreate the actual external popup everytime.
66 WebPopupMenuInfo info;
67 getPopupMenuInfo(&info);
68 if (info.items.isEmpty())
70 m_webExternalPopupMenu =
71 m_webViewClient->createExternalPopupMenu(info, this);
72 m_webExternalPopupMenu->show(v->contentsToWindow(rect));
75 void ExternalPopupMenu::hide()
77 if (m_popupMenuClient)
78 m_popupMenuClient->popupDidHide();
79 if (!m_webExternalPopupMenu)
81 m_webExternalPopupMenu->close();
82 m_webExternalPopupMenu = 0;
85 void ExternalPopupMenu::updateFromElement()
89 void ExternalPopupMenu::disconnectClient()
92 m_popupMenuClient = 0;
95 void ExternalPopupMenu::didChangeSelection(int index)
97 if (m_popupMenuClient)
98 m_popupMenuClient->selectionChanged(index);
101 void ExternalPopupMenu::didAcceptIndex(int index)
103 // Calling methods on the PopupMenuClient might lead to this object being
104 // derefed. This ensures it does not get deleted while we are running this
106 RefPtr<ExternalPopupMenu> guard(this);
108 if (m_popupMenuClient) {
109 m_popupMenuClient->valueChanged(index);
110 // The call to valueChanged above might have lead to a call to
111 // disconnectClient, so we might not have a PopupMenuClient anymore.
112 if (m_popupMenuClient)
113 m_popupMenuClient->popupDidHide();
115 m_webExternalPopupMenu = 0;
118 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
120 #if ENABLE(NO_LISTBOX_RENDERING)
121 if (!m_popupMenuClient) {
122 m_webExternalPopupMenu = 0;
126 // Calling methods on the PopupMenuClient might lead to this object being
127 // derefed. This ensures it does not get deleted while we are running this
129 RefPtr<ExternalPopupMenu> protect(this);
130 ListPopupMenuClient* listPopupMenuClient = static_cast<ListPopupMenuClient*>(m_popupMenuClient);
133 listPopupMenuClient->valueChanged(-1, true);
135 for (size_t i = 0; i < indices.size(); ++i)
136 listPopupMenuClient->listBoxSelectItem(indices[i], (i > 0), false, (i == indices.size() - 1));
139 // The call to valueChanged above might have lead to a call to
140 // disconnectClient, so we might not have a PopupMenuClient anymore.
141 if (m_popupMenuClient)
142 m_popupMenuClient->popupDidHide();
144 m_webExternalPopupMenu = 0;
147 ASSERT_NOT_REACHED();
151 void ExternalPopupMenu::didCancel()
153 // See comment in didAcceptIndex on why we need this.
154 RefPtr<ExternalPopupMenu> guard(this);
156 if (m_popupMenuClient)
157 m_popupMenuClient->popupDidHide();
158 m_webExternalPopupMenu = 0;
161 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo* info)
163 int itemCount = m_popupMenuClient->listSize();
164 WebVector<WebMenuItemInfo> items(static_cast<size_t>(itemCount));
165 for (int i = 0; i < itemCount; ++i) {
166 WebMenuItemInfo& popupItem = items[i];
167 popupItem.label = m_popupMenuClient->itemText(i);
168 popupItem.toolTip = m_popupMenuClient->itemToolTip(i);
169 if (m_popupMenuClient->itemIsSeparator(i))
170 popupItem.type = WebMenuItemInfo::Separator;
171 else if (m_popupMenuClient->itemIsLabel(i))
172 popupItem.type = WebMenuItemInfo::Group;
174 popupItem.type = WebMenuItemInfo::Option;
175 popupItem.enabled = m_popupMenuClient->itemIsEnabled(i);
176 popupItem.checked = m_popupMenuClient->itemIsSelected(i);
177 PopupMenuStyle style = m_popupMenuClient->itemStyle(i);
178 if (style.textDirection() == WebCore::RTL)
179 popupItem.textDirection = WebTextDirectionRightToLeft;
181 popupItem.textDirection = WebTextDirectionLeftToRight;
182 popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
185 info->itemHeight = m_popupMenuClient->menuStyle().font().fontMetrics().height();
186 info->itemFontSize = static_cast<int>(m_popupMenuClient->menuStyle().font().size());
187 info->selectedIndex = m_popupMenuClient->selectedIndex();
188 info->rightAligned = m_popupMenuClient->menuStyle().textDirection() == WebCore::RTL;
189 #if ENABLE(NO_LISTBOX_RENDERING)
190 // FIXME: Why is this cast safe?
191 ListPopupMenuClient* client = static_cast<ListPopupMenuClient*>(m_popupMenuClient);
192 info->allowMultipleSelection = client->multiple();
194 info->items.swap(items);