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 34 #include "WebExternalPopupMenu.h" 35 #include "WebMenuItemInfo.h" 36 #include "WebPopupMenuInfo.h" 37 #include "WebViewClient.h" 38 #include "core/frame/Frame.h" 39 #include "core/frame/FrameView.h" 40 #include "platform/PopupMenuClient.h" 41 #include "platform/geometry/FloatQuad.h" 42 #include "platform/geometry/IntPoint.h" 43 #include "platform/text/TextDirection.h" 44 #include "public/platform/WebVector.h" 45 46 using namespace WebCore; 47 48 namespace blink { 49 50 ExternalPopupMenu::ExternalPopupMenu(Frame& frame, PopupMenuClient* popupMenuClient, WebViewClient* webViewClient) 51 : m_popupMenuClient(popupMenuClient) 52 , m_frameView(frame.view()) 53 , m_webViewClient(webViewClient) 54 , m_webExternalPopupMenu(0) 55 { 56 } 57 58 ExternalPopupMenu::~ExternalPopupMenu() 59 { 60 } 61 62 void ExternalPopupMenu::show(const FloatQuad& controlPosition, const IntSize&, int index) 63 { 64 IntRect rect(controlPosition.enclosingBoundingBox()); 65 // WebCore reuses the PopupMenu of a page. 66 // For simplicity, we do recreate the actual external popup everytime. 67 hide(); 68 69 WebPopupMenuInfo info; 70 getPopupMenuInfo(&info); 71 if (info.items.isEmpty()) 72 return; 73 m_webExternalPopupMenu = 74 m_webViewClient->createExternalPopupMenu(info, this); 75 if (m_webExternalPopupMenu) 76 m_webExternalPopupMenu->show(m_frameView->contentsToWindow(rect)); 77 else { 78 // The client might refuse to create a popup (when there is already one pending to be shown for example). 79 didCancel(); 80 } 81 } 82 83 void ExternalPopupMenu::hide() 84 { 85 if (m_popupMenuClient) 86 m_popupMenuClient->popupDidHide(); 87 if (!m_webExternalPopupMenu) 88 return; 89 m_webExternalPopupMenu->close(); 90 m_webExternalPopupMenu = 0; 91 } 92 93 void ExternalPopupMenu::updateFromElement() 94 { 95 } 96 97 void ExternalPopupMenu::disconnectClient() 98 { 99 hide(); 100 m_popupMenuClient = 0; 101 } 102 103 void ExternalPopupMenu::didChangeSelection(int index) 104 { 105 if (m_popupMenuClient) 106 m_popupMenuClient->selectionChanged(index); 107 } 108 109 void ExternalPopupMenu::didAcceptIndex(int index) 110 { 111 // Calling methods on the PopupMenuClient might lead to this object being 112 // derefed. This ensures it does not get deleted while we are running this 113 // method. 114 RefPtr<ExternalPopupMenu> guard(this); 115 116 if (m_popupMenuClient) { 117 m_popupMenuClient->valueChanged(index); 118 // The call to valueChanged above might have lead to a call to 119 // disconnectClient, so we might not have a PopupMenuClient anymore. 120 if (m_popupMenuClient) 121 m_popupMenuClient->popupDidHide(); 122 } 123 m_webExternalPopupMenu = 0; 124 } 125 126 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices) 127 { 128 if (!m_popupMenuClient) { 129 m_webExternalPopupMenu = 0; 130 return; 131 } 132 133 // Calling methods on the PopupMenuClient might lead to this object being 134 // derefed. This ensures it does not get deleted while we are running this 135 // method. 136 RefPtr<ExternalPopupMenu> protect(this); 137 138 if (!indices.size()) 139 m_popupMenuClient->valueChanged(-1, true); 140 else { 141 for (size_t i = 0; i < indices.size(); ++i) 142 m_popupMenuClient->listBoxSelectItem(indices[i], (i > 0), false, (i == indices.size() - 1)); 143 } 144 145 // The call to valueChanged above might have lead to a call to 146 // disconnectClient, so we might not have a PopupMenuClient anymore. 147 if (m_popupMenuClient) 148 m_popupMenuClient->popupDidHide(); 149 150 m_webExternalPopupMenu = 0; 151 } 152 153 void ExternalPopupMenu::didCancel() 154 { 155 // See comment in didAcceptIndex on why we need this. 156 RefPtr<ExternalPopupMenu> guard(this); 157 158 if (m_popupMenuClient) 159 m_popupMenuClient->popupDidHide(); 160 m_webExternalPopupMenu = 0; 161 } 162 163 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo* info) 164 { 165 int itemCount = m_popupMenuClient->listSize(); 166 WebVector<WebMenuItemInfo> items(static_cast<size_t>(itemCount)); 167 for (int i = 0; i < itemCount; ++i) { 168 WebMenuItemInfo& popupItem = items[i]; 169 popupItem.label = m_popupMenuClient->itemText(i); 170 popupItem.toolTip = m_popupMenuClient->itemToolTip(i); 171 if (m_popupMenuClient->itemIsSeparator(i)) 172 popupItem.type = WebMenuItemInfo::Separator; 173 else if (m_popupMenuClient->itemIsLabel(i)) 174 popupItem.type = WebMenuItemInfo::Group; 175 else 176 popupItem.type = WebMenuItemInfo::Option; 177 popupItem.enabled = m_popupMenuClient->itemIsEnabled(i); 178 popupItem.checked = m_popupMenuClient->itemIsSelected(i); 179 PopupMenuStyle style = m_popupMenuClient->itemStyle(i); 180 if (style.textDirection() == WebCore::RTL) 181 popupItem.textDirection = WebTextDirectionRightToLeft; 182 else 183 popupItem.textDirection = WebTextDirectionLeftToRight; 184 popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride(); 185 } 186 187 info->itemHeight = m_popupMenuClient->menuStyle().font().fontMetrics().height(); 188 info->itemFontSize = static_cast<int>(m_popupMenuClient->menuStyle().font().size()); 189 info->selectedIndex = m_popupMenuClient->selectedIndex(); 190 info->rightAligned = m_popupMenuClient->menuStyle().textDirection() == WebCore::RTL; 191 info->allowMultipleSelection = m_popupMenuClient->multiple(); 192 info->items.swap(items); 193 } 194 195 } 196