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