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 "FrameView.h"
35 #include "IntPoint.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"
43
44 using namespace WebCore;
45
46 namespace WebKit {
47
ExternalPopupMenu(PopupMenuClient * popupMenuClient,WebViewClient * webViewClient)48 ExternalPopupMenu::ExternalPopupMenu(PopupMenuClient* popupMenuClient,
49 WebViewClient* webViewClient)
50 : m_popupMenuClient(popupMenuClient)
51 , m_webViewClient(webViewClient)
52 , m_webExternalPopupMenu(0)
53 {
54 }
55
~ExternalPopupMenu()56 ExternalPopupMenu::~ExternalPopupMenu()
57 {
58 }
59
show(const IntRect & rect,FrameView * v,int index)60 void ExternalPopupMenu::show(const IntRect& rect, FrameView* v, int index)
61 {
62 // WebCore reuses the PopupMenu of a page.
63 // For simplicity, we do recreate the actual external popup everytime.
64 hide();
65
66 WebPopupMenuInfo info;
67 getPopupMenuInfo(&info);
68 if (info.items.isEmpty())
69 return;
70 m_webExternalPopupMenu =
71 m_webViewClient->createExternalPopupMenu(info, this);
72 m_webExternalPopupMenu->show(v->contentsToWindow(rect));
73 }
74
hide()75 void ExternalPopupMenu::hide()
76 {
77 if (m_popupMenuClient)
78 m_popupMenuClient->popupDidHide();
79 if (!m_webExternalPopupMenu)
80 return;
81 m_webExternalPopupMenu->close();
82 m_webExternalPopupMenu = 0;
83 }
84
updateFromElement()85 void ExternalPopupMenu::updateFromElement()
86 {
87 }
88
disconnectClient()89 void ExternalPopupMenu::disconnectClient()
90 {
91 hide();
92 m_popupMenuClient = 0;
93 }
94
didChangeSelection(int index)95 void ExternalPopupMenu::didChangeSelection(int index)
96 {
97 if (m_popupMenuClient)
98 m_popupMenuClient->selectionChanged(index);
99 }
100
didAcceptIndex(int index)101 void ExternalPopupMenu::didAcceptIndex(int index)
102 {
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
105 // method.
106 RefPtr<ExternalPopupMenu> guard(this);
107
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();
114 }
115 m_webExternalPopupMenu = 0;
116 }
117
didCancel()118 void ExternalPopupMenu::didCancel()
119 {
120 // See comment in didAcceptIndex on why we need this.
121 RefPtr<ExternalPopupMenu> guard(this);
122
123 if (m_popupMenuClient)
124 m_popupMenuClient->popupDidHide();
125 m_webExternalPopupMenu = 0;
126 }
127
getPopupMenuInfo(WebPopupMenuInfo * info)128 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo* info)
129 {
130 int itemCount = m_popupMenuClient->listSize();
131 WebVector<WebMenuItemInfo> items(
132 static_cast<size_t>(itemCount));
133 for (int i = 0; i < itemCount; ++i) {
134 WebMenuItemInfo& popupItem = items[i];
135 popupItem.label = m_popupMenuClient->itemText(i);
136 if (m_popupMenuClient->itemIsSeparator(i))
137 popupItem.type = WebMenuItemInfo::Separator;
138 else if (m_popupMenuClient->itemIsLabel(i))
139 popupItem.type = WebMenuItemInfo::Group;
140 else
141 popupItem.type = WebMenuItemInfo::Option;
142 popupItem.enabled = m_popupMenuClient->itemIsEnabled(i);
143 PopupMenuStyle style = m_popupMenuClient->itemStyle(i);
144 if (style.textDirection() == WebCore::RTL)
145 popupItem.textDirection = WebTextDirectionRightToLeft;
146 else
147 popupItem.textDirection = WebTextDirectionLeftToRight;
148 popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
149 }
150
151 info->itemHeight = m_popupMenuClient->menuStyle().font().fontMetrics().height();
152 info->itemFontSize =
153 static_cast<int>(m_popupMenuClient->menuStyle().font().size());
154 info->selectedIndex = m_popupMenuClient->selectedIndex();
155 info->rightAligned =
156 m_popupMenuClient->menuStyle().textDirection() == WebCore::RTL;
157 info->items.swap(items);
158 }
159
160 } // namespace WebKit
161