initial import
[vuplus_webkit] / Source / WebKit / chromium / src / ExternalPopupMenu.cpp
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
48 ExternalPopupMenu::ExternalPopupMenu(PopupMenuClient* popupMenuClient,
49                                      WebViewClient* webViewClient)
50     : m_popupMenuClient(popupMenuClient)
51     , m_webViewClient(webViewClient)
52     , m_webExternalPopupMenu(0)
53 {
54 }
55
56 ExternalPopupMenu::~ExternalPopupMenu()
57 {
58 }
59
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
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
85 void ExternalPopupMenu::updateFromElement()
86 {
87 }
88
89 void ExternalPopupMenu::disconnectClient()
90 {
91     hide();
92     m_popupMenuClient = 0;
93 }
94
95 void ExternalPopupMenu::didChangeSelection(int index)
96 {
97     if (m_popupMenuClient)
98         m_popupMenuClient->selectionChanged(index);
99 }
100
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
118 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
119 {
120 #if ENABLE(NO_LISTBOX_RENDERING)
121     if (!m_popupMenuClient) {
122         m_webExternalPopupMenu = 0;
123         return
124     }
125
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
128     // method.
129     RefPtr<ExternalPopupMenu> protect(this);
130     ListPopupMenuClient* listPopupMenuClient = static_cast<ListPopupMenuClient*>(m_popupMenuClient);
131
132     if (!indices.size())
133         listPopupMenuClient->valueChanged(-1, true);
134     else {
135         for (size_t i = 0; i < indices.size(); ++i)
136             listPopupMenuClient->listBoxSelectItem(indices[i], (i > 0), false, (i == indices.size() - 1));
137     }
138
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();
143
144     m_webExternalPopupMenu = 0;
145
146 #else
147     ASSERT_NOT_REACHED();
148 #endif
149 }
150
151 void ExternalPopupMenu::didCancel()
152 {
153     // See comment in didAcceptIndex on why we need this.
154     RefPtr<ExternalPopupMenu> guard(this);
155
156     if (m_popupMenuClient)
157         m_popupMenuClient->popupDidHide();
158     m_webExternalPopupMenu = 0;
159 }
160
161 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo* info)
162 {
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;
173         else
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;
180         else
181             popupItem.textDirection = WebTextDirectionLeftToRight;
182         popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
183     }
184
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();
193 #endif
194     info->items.swap(items);
195 }
196
197 }