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