Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / 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 "web/ExternalPopupMenu.h"
33
34 #include "core/frame/FrameView.h"
35 #include "core/frame/LocalFrame.h"
36 #include "platform/PopupMenuClient.h"
37 #include "platform/geometry/FloatQuad.h"
38 #include "platform/geometry/IntPoint.h"
39 #include "platform/text/TextDirection.h"
40 #include "public/platform/WebVector.h"
41 #include "public/web/WebExternalPopupMenu.h"
42 #include "public/web/WebMenuItemInfo.h"
43 #include "public/web/WebPopupMenuInfo.h"
44 #include "public/web/WebViewClient.h"
45 #include "web/WebViewImpl.h"
46
47 namespace blink {
48
49 ExternalPopupMenu::ExternalPopupMenu(LocalFrame& frame, PopupMenuClient* popupMenuClient, WebViewImpl& webView)
50     : m_popupMenuClient(popupMenuClient)
51     , m_frameView(frame.view())
52     , m_webView(webView)
53     , m_dispatchEventTimer(this, &ExternalPopupMenu::dispatchEvent)
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 an element.
66     // For simplicity, we do recreate the actual external popup everytime.
67     if (m_webExternalPopupMenu) {
68         m_webExternalPopupMenu->close();
69         m_webExternalPopupMenu = 0;
70     }
71
72     WebPopupMenuInfo info;
73     getPopupMenuInfo(info, *m_popupMenuClient);
74     if (info.items.isEmpty())
75         return;
76     m_webExternalPopupMenu = m_webView.client()->createExternalPopupMenu(info, this);
77     if (m_webExternalPopupMenu) {
78         m_webExternalPopupMenu->show(m_frameView->contentsToWindow(rect));
79 #if OS(MACOSX)
80         const WebInputEvent* currentEvent = WebViewImpl::currentInputEvent();
81         if (currentEvent && currentEvent->type == WebInputEvent::MouseDown) {
82             m_syntheticEvent = adoptPtr(new WebMouseEvent);
83             *m_syntheticEvent = *static_cast<const WebMouseEvent*>(currentEvent);
84             m_syntheticEvent->type = WebInputEvent::MouseUp;
85             m_dispatchEventTimer.startOneShot(0, FROM_HERE);
86             // FIXME: show() is asynchronous. If preparing a popup is slow and
87             // a user released the mouse button before showing the popup,
88             // mouseup and click events are correctly dispatched. Dispatching
89             // the synthetic mouseup event is redundant in this case.
90         }
91 #endif
92     } else {
93         // The client might refuse to create a popup (when there is already one pending to be shown for example).
94         didCancel();
95     }
96 }
97
98 void ExternalPopupMenu::dispatchEvent(Timer<ExternalPopupMenu>*)
99 {
100     m_webView.handleInputEvent(*m_syntheticEvent);
101     m_syntheticEvent.clear();
102 }
103
104 void ExternalPopupMenu::hide()
105 {
106     if (m_popupMenuClient)
107         m_popupMenuClient->popupDidHide();
108     if (!m_webExternalPopupMenu)
109         return;
110     m_webExternalPopupMenu->close();
111     m_webExternalPopupMenu = 0;
112 }
113
114 void ExternalPopupMenu::updateFromElement()
115 {
116 }
117
118 void ExternalPopupMenu::disconnectClient()
119 {
120     hide();
121     m_popupMenuClient = 0;
122 }
123
124 void ExternalPopupMenu::didChangeSelection(int index)
125 {
126     if (m_popupMenuClient)
127         m_popupMenuClient->selectionChanged(toPopupMenuItemIndex(index, *m_popupMenuClient));
128 }
129
130 void ExternalPopupMenu::didAcceptIndex(int index)
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     int popupMenuItemIndex = toPopupMenuItemIndex(index, *m_popupMenuClient);
136     RefPtr<ExternalPopupMenu> guard(this);
137
138     if (m_popupMenuClient) {
139         m_popupMenuClient->popupDidHide();
140         m_popupMenuClient->valueChanged(popupMenuItemIndex);
141     }
142     m_webExternalPopupMenu = 0;
143 }
144
145 void ExternalPopupMenu::didAcceptIndices(const WebVector<int>& indices)
146 {
147     if (!m_popupMenuClient) {
148         m_webExternalPopupMenu = 0;
149         return;
150     }
151
152     // Calling methods on the PopupMenuClient might lead to this object being
153     // derefed. This ensures it does not get deleted while we are running this
154     // method.
155     RefPtr<ExternalPopupMenu> protect(this);
156
157     if (!indices.size())
158         m_popupMenuClient->valueChanged(static_cast<unsigned>(-1), true);
159     else {
160         for (size_t i = 0; i < indices.size(); ++i)
161             m_popupMenuClient->listBoxSelectItem(toPopupMenuItemIndex(indices[i], *m_popupMenuClient), (i > 0), false, (i == indices.size() - 1));
162     }
163
164     // The call to valueChanged above might have lead to a call to
165     // disconnectClient, so we might not have a PopupMenuClient anymore.
166     if (m_popupMenuClient)
167         m_popupMenuClient->popupDidHide();
168
169     m_webExternalPopupMenu = 0;
170 }
171
172 void ExternalPopupMenu::didCancel()
173 {
174     // See comment in didAcceptIndex on why we need this.
175     RefPtr<ExternalPopupMenu> guard(this);
176
177     if (m_popupMenuClient)
178         m_popupMenuClient->popupDidHide();
179     m_webExternalPopupMenu = 0;
180 }
181
182 void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo& info, PopupMenuClient& popupMenuClient)
183 {
184     int itemCount = popupMenuClient.listSize();
185     int count = 0;
186     Vector<WebMenuItemInfo> items(static_cast<size_t>(itemCount));
187     for (int i = 0; i < itemCount; ++i) {
188         PopupMenuStyle style = popupMenuClient.itemStyle(i);
189         if (style.isDisplayNone())
190             continue;
191
192         WebMenuItemInfo& popupItem = items[count++];
193         popupItem.label = popupMenuClient.itemText(i);
194         popupItem.toolTip = popupMenuClient.itemToolTip(i);
195         if (popupMenuClient.itemIsSeparator(i))
196             popupItem.type = WebMenuItemInfo::Separator;
197         else if (popupMenuClient.itemIsLabel(i))
198             popupItem.type = WebMenuItemInfo::Group;
199         else
200             popupItem.type = WebMenuItemInfo::Option;
201         popupItem.enabled = popupMenuClient.itemIsEnabled(i);
202         popupItem.checked = popupMenuClient.itemIsSelected(i);
203         popupItem.textDirection = toWebTextDirection(style.textDirection());
204         popupItem.hasTextDirectionOverride = style.hasTextDirectionOverride();
205     }
206
207     info.itemHeight = popupMenuClient.menuStyle().font().fontMetrics().height();
208     info.itemFontSize = static_cast<int>(popupMenuClient.menuStyle().font().fontDescription().computedSize());
209     info.selectedIndex = toExternalPopupMenuItemIndex(popupMenuClient.selectedIndex(), popupMenuClient);
210     info.rightAligned = popupMenuClient.menuStyle().textDirection() == RTL;
211     info.allowMultipleSelection = popupMenuClient.multiple();
212     if (count < itemCount)
213         items.shrink(count);
214     info.items = items;
215
216 }
217
218 int ExternalPopupMenu::toPopupMenuItemIndex(int externalPopupMenuItemIndex, PopupMenuClient& popupMenuClient)
219 {
220     if (externalPopupMenuItemIndex < 0)
221         return externalPopupMenuItemIndex;
222
223     int itemCount = popupMenuClient.listSize();
224     int indexTracker = 0;
225     for (int i = 0; i < itemCount ; ++i) {
226         if (popupMenuClient.itemStyle(i).isDisplayNone())
227             continue;
228         if (indexTracker++ == externalPopupMenuItemIndex)
229             return i;
230     }
231     return -1;
232 }
233
234 int ExternalPopupMenu::toExternalPopupMenuItemIndex(int popupMenuItemIndex, PopupMenuClient& popupMenuClient)
235 {
236     if (popupMenuItemIndex < 0)
237         return popupMenuItemIndex;
238
239     int itemCount = popupMenuClient.listSize();
240     int indexTracker = 0;
241     for (int i = 0; i < itemCount; ++i) {
242         if (popupMenuClient.itemStyle(i).isDisplayNone())
243             continue;
244         if (popupMenuItemIndex == i)
245             return indexTracker;
246         ++indexTracker;
247     }
248     return -1;
249 }
250
251 } // namespace blink