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