Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 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 |
| 13 | * distribution. |
| 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. |
| 17 | * |
| 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. |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | #include "ExternalPopupMenu.h" |
| 33 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 34 | #include "WebExternalPopupMenu.h" |
| 35 | #include "WebMenuItemInfo.h" |
| 36 | #include "WebPopupMenuInfo.h" |
| 37 | #include "WebViewClient.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 38 | #include "WebViewImpl.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 39 | #include "core/frame/FrameView.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 40 | #include "core/frame/LocalFrame.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 41 | #include "platform/PopupMenuClient.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 42 | #include "platform/geometry/FloatQuad.h" |
| 43 | #include "platform/geometry/IntPoint.h" |
| 44 | #include "platform/text/TextDirection.h" |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 45 | #include "public/platform/WebVector.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 46 | |
| 47 | using namespace WebCore; |
| 48 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 49 | namespace blink { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 50 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 51 | ExternalPopupMenu::ExternalPopupMenu(LocalFrame& frame, PopupMenuClient* popupMenuClient, WebViewImpl& webView) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 52 | : m_popupMenuClient(popupMenuClient) |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 53 | , m_frameView(frame.view()) |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 54 | , m_webView(webView) |
| 55 | , m_dispatchEventTimer(this, &ExternalPopupMenu::dispatchEvent) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 56 | , m_webExternalPopupMenu(0) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | ExternalPopupMenu::~ExternalPopupMenu() |
| 61 | { |
| 62 | } |
| 63 | |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 64 | void ExternalPopupMenu::show(const FloatQuad& controlPosition, const IntSize&, int index) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 65 | { |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 66 | IntRect rect(controlPosition.enclosingBoundingBox()); |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 67 | // WebCore reuses the PopupMenu of an element. |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 68 | // For simplicity, we do recreate the actual external popup everytime. |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 69 | if (m_webExternalPopupMenu) { |
| 70 | m_webExternalPopupMenu->close(); |
| 71 | m_webExternalPopupMenu = 0; |
| 72 | } |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 73 | |
| 74 | WebPopupMenuInfo info; |
| 75 | getPopupMenuInfo(&info); |
| 76 | if (info.items.isEmpty()) |
| 77 | return; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 78 | m_webExternalPopupMenu = m_webView.client()->createExternalPopupMenu(info, this); |
| 79 | if (m_webExternalPopupMenu) { |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 80 | m_webExternalPopupMenu->show(m_frameView->contentsToWindow(rect)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 81 | #if OS(MACOSX) |
| 82 | const WebInputEvent* currentEvent = WebViewImpl::currentInputEvent(); |
| 83 | if (currentEvent && currentEvent->type == WebInputEvent::MouseDown) { |
| 84 | m_syntheticEvent = adoptPtr(new WebMouseEvent); |
| 85 | *m_syntheticEvent = *static_cast<const WebMouseEvent*>(currentEvent); |
| 86 | m_syntheticEvent->type = WebInputEvent::MouseUp; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 87 | m_dispatchEventTimer.startOneShot(0, FROM_HERE); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 88 | // FIXME: show() is asynchronous. If preparing a popup is slow and |
| 89 | // a user released the mouse button before showing the popup, |
| 90 | // mouseup and click events are correctly dispatched. Dispatching |
| 91 | // the synthetic mouseup event is redundant in this case. |
| 92 | } |
| 93 | #endif |
| 94 | } else { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 95 | // The client might refuse to create a popup (when there is already one pending to be shown for example). |
| 96 | didCancel(); |
| 97 | } |
| 98 | } |
| 99 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 100 | void ExternalPopupMenu::dispatchEvent(Timer<ExternalPopupMenu>*) |
| 101 | { |
| 102 | m_webView.handleInputEvent(*m_syntheticEvent); |
| 103 | m_syntheticEvent.clear(); |
| 104 | } |
| 105 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 106 | void ExternalPopupMenu::hide() |
| 107 | { |
| 108 | if (m_popupMenuClient) |
| 109 | m_popupMenuClient->popupDidHide(); |
| 110 | if (!m_webExternalPopupMenu) |
| 111 | return; |
| 112 | m_webExternalPopupMenu->close(); |
| 113 | m_webExternalPopupMenu = 0; |
| 114 | } |
| 115 | |
| 116 | void ExternalPopupMenu::updateFromElement() |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | void ExternalPopupMenu::disconnectClient() |
| 121 | { |
| 122 | hide(); |
| 123 | m_popupMenuClient = 0; |
| 124 | } |
| 125 | |
| 126 | void ExternalPopupMenu::didChangeSelection(int index) |
| 127 | { |
| 128 | if (m_popupMenuClient) |
| 129 | m_popupMenuClient->selectionChanged(index); |
| 130 | } |
| 131 | |
| 132 | void ExternalPopupMenu::didAcceptIndex(int index) |
| 133 | { |
| 134 | // Calling methods on the PopupMenuClient might lead to this object being |
| 135 | // derefed. This ensures it does not get deleted while we are running this |
| 136 | // method. |
| 137 | RefPtr<ExternalPopupMenu> guard(this); |
| 138 | |
| 139 | if (m_popupMenuClient) { |
| 140 | m_popupMenuClient->valueChanged(index); |
| 141 | // The call to valueChanged above might have lead to a call to |
| 142 | // disconnectClient, so we might not have a PopupMenuClient anymore. |
| 143 | if (m_popupMenuClient) |
| 144 | m_popupMenuClient->popupDidHide(); |
| 145 | } |
| 146 | m_webExternalPopupMenu = 0; |
| 147 | } |
| 148 | |
| 149 | void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices) |
| 150 | { |
| 151 | if (!m_popupMenuClient) { |
| 152 | m_webExternalPopupMenu = 0; |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | // Calling methods on the PopupMenuClient might lead to this object being |
| 157 | // derefed. This ensures it does not get deleted while we are running this |
| 158 | // method. |
| 159 | RefPtr<ExternalPopupMenu> protect(this); |
| 160 | |
| 161 | if (!indices.size()) |
| 162 | m_popupMenuClient->valueChanged(-1, true); |
| 163 | else { |
| 164 | for (size_t i = 0; i < indices.size(); ++i) |
| 165 | m_popupMenuClient->listBoxSelectItem(indices[i], (i > 0), false, (i == indices.size() - 1)); |
| 166 | } |
| 167 | |
| 168 | // The call to valueChanged above might have lead to a call to |
| 169 | // disconnectClient, so we might not have a PopupMenuClient anymore. |
| 170 | if (m_popupMenuClient) |
| 171 | m_popupMenuClient->popupDidHide(); |
| 172 | |
| 173 | m_webExternalPopupMenu = 0; |
| 174 | } |
| 175 | |
| 176 | void ExternalPopupMenu::didCancel() |
| 177 | { |
| 178 | // See comment in didAcceptIndex on why we need this. |
| 179 | RefPtr<ExternalPopupMenu> guard(this); |
| 180 | |
| 181 | if (m_popupMenuClient) |
| 182 | m_popupMenuClient->popupDidHide(); |
| 183 | m_webExternalPopupMenu = 0; |
| 184 | } |
| 185 | |
| 186 | void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo* info) |
| 187 | { |
| 188 | int itemCount = m_popupMenuClient->listSize(); |
| 189 | WebVector<WebMenuItemInfo> items(static_cast<size_t>(itemCount)); |
| 190 | for (int i = 0; i < itemCount; ++i) { |
| 191 | WebMenuItemInfo& popupItem = items[i]; |
| 192 | popupItem.label = m_popupMenuClient->itemText(i); |
| 193 | popupItem.toolTip = m_popupMenuClient->itemToolTip(i); |
| 194 | if (m_popupMenuClient->itemIsSeparator(i)) |
| 195 | popupItem.type = WebMenuItemInfo::Separator; |
| 196 | else if (m_popupMenuClient->itemIsLabel(i)) |
| 197 | popupItem.type = WebMenuItemInfo::Group; |
| 198 | else |
| 199 | popupItem.type = WebMenuItemInfo::Option; |
| 200 | popupItem.enabled = m_popupMenuClient->itemIsEnabled(i); |
| 201 | popupItem.checked = m_popupMenuClient->itemIsSelected(i); |
| 202 | PopupMenuStyle style = m_popupMenuClient->itemStyle(i); |
| 203 | if (style.textDirection() == WebCore::RTL) |
| 204 | popupItem.textDirection = WebTextDirectionRightToLeft; |
| 205 | else |
| 206 | popupItem.textDirection = WebTextDirectionLeftToRight; |
| 207 | popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride(); |
| 208 | } |
| 209 | |
| 210 | info->itemHeight = m_popupMenuClient->menuStyle().font().fontMetrics().height(); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 211 | info->itemFontSize = static_cast<int>(m_popupMenuClient->menuStyle().font().fontDescription().computedSize()); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 212 | info->selectedIndex = m_popupMenuClient->selectedIndex(); |
| 213 | info->rightAligned = m_popupMenuClient->menuStyle().textDirection() == WebCore::RTL; |
| 214 | info->allowMultipleSelection = m_popupMenuClient->multiple(); |
| 215 | info->items.swap(items); |
| 216 | } |
| 217 | |
| 218 | } |