Cancel composition when focused node was changed
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebCoreSupport / WebPopupMenu.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
3  * Copyright (C) 2010 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #include "config.h"
23 #include "WebPopupMenu.h"
24
25 #include "PlatformPopupMenuData.h"
26 #include "WebCoreArgumentCoders.h"
27 #include "WebPage.h"
28 #include "WebPageProxyMessages.h"
29 #include "WebProcess.h"
30 #include <WebCore/FrameView.h>
31 #include <WebCore/PopupMenuClient.h>
32
33 #if ENABLE(TIZEN_LINK_EFFECT)
34 #include <WebCore/TizenLinkEffect.h>
35 #endif
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 PassRefPtr<WebPopupMenu> WebPopupMenu::create(WebPage* page, PopupMenuClient* client)
42 {
43     return adoptRef(new WebPopupMenu(page, client));
44 }
45
46 WebPopupMenu::WebPopupMenu(WebPage* page, PopupMenuClient* client)
47     : m_popupClient(client)
48     , m_page(page)
49 {
50 }
51
52 WebPopupMenu::~WebPopupMenu()
53 {
54 }
55
56 void WebPopupMenu::disconnectClient()
57 {
58 #if ENABLE(TIZEN_WEBKIT2_POPUP_INTERNAL)
59     TIZEN_LOGI("");
60
61     // Check whether it is valid on other platform
62     hide();
63 #endif
64     m_popupClient = 0;
65 }
66
67 void WebPopupMenu::didChangeSelectedIndex(int newIndex)
68 {
69     if (!m_popupClient)
70         return;
71
72 #if PLATFORM(QT)
73     if (newIndex >= 0)
74         m_popupClient->listBoxSelectItem(newIndex, m_popupClient->multiple(), false);
75 #else
76 #if ENABLE(TIZEN_MULTIPLE_SELECT)
77     TIZEN_LOGI("multiple:%d, newIndex:%d", m_popupClient->multiple(), newIndex);
78     if (m_popupClient->multiple()){
79         if (newIndex >= 0)
80                 m_popupClient->listBoxSelectItem(newIndex, m_popupClient->multiple(), false);
81         return;
82     }
83 #endif
84     m_popupClient->popupDidHide();
85     if (newIndex >= 0)
86         m_popupClient->valueChanged(newIndex);
87 #endif
88 }
89
90 void WebPopupMenu::setTextForIndex(int index)
91 {
92     if (!m_popupClient)
93         return;
94
95     m_popupClient->setTextFromItem(index);
96 }
97
98 Vector<WebPopupItem> WebPopupMenu::populateItems()
99 {
100     size_t size = m_popupClient->listSize();
101     TIZEN_LOGI("size:%d", size);
102
103     Vector<WebPopupItem> items;
104     items.reserveInitialCapacity(size);
105     
106     for (size_t i = 0; i < size; ++i) {
107         if (m_popupClient->itemIsSeparator(i))
108             items.append(WebPopupItem(WebPopupItem::Separator));
109         else {
110             // FIXME: Add support for styling the font.
111             // FIXME: Add support for styling the foreground and background colors.
112             // FIXME: Find a way to customize text color when an item is highlighted.
113             PopupMenuStyle itemStyle = m_popupClient->itemStyle(i);
114             items.append(WebPopupItem(WebPopupItem::Item, m_popupClient->itemText(i), itemStyle.textDirection(), itemStyle.hasTextDirectionOverride(), m_popupClient->itemToolTip(i), m_popupClient->itemAccessibilityText(i), m_popupClient->itemIsEnabled(i), m_popupClient->itemIsLabel(i), m_popupClient->itemIsSelected(i)));
115         }
116     }
117
118     return items;
119 }
120
121 void WebPopupMenu::show(const IntRect& rect, FrameView* view, int index)
122 {
123 #if ENABLE(TIZEN_LINK_EFFECT)
124     TizenLinkEffect::playLinkEffect();
125 #endif
126     TIZEN_LOGI("");
127     // FIXME: We should probably inform the client to also close the menu.
128     Vector<WebPopupItem> items = populateItems();
129
130     if (items.isEmpty() || !m_page) {
131         m_popupClient->popupDidHide();
132         return;
133     }
134
135     m_page->setActivePopupMenu(this);
136
137     // Move to page coordinates
138     IntRect pageCoordinates(view->contentsToWindow(rect.location()), rect.size());
139
140     PlatformPopupMenuData platformData;
141     setUpPlatformData(pageCoordinates, platformData);
142
143     WebProcess::shared().connection()->send(Messages::WebPageProxy::ShowPopupMenu(pageCoordinates, m_popupClient->menuStyle().textDirection(), items, index, platformData), m_page->pageID());
144 }
145
146 void WebPopupMenu::hide()
147 {
148     TIZEN_LOGI("");
149     if (!m_page || !m_popupClient)
150         return;
151
152     WebProcess::shared().connection()->send(Messages::WebPageProxy::HidePopupMenu(), m_page->pageID());
153 #if ENABLE(TIZEN_WEBKIT2_POPUP)
154     m_popupClient->popupDidHide();
155 #endif
156     m_page->setActivePopupMenu(0);
157 }
158
159 void WebPopupMenu::updateFromElement()
160 {
161 #if PLATFORM(WIN)
162     if (!m_page || !m_popupClient)
163         return;
164
165     int selectedIndex = m_popupClient->selectedIndex();
166     WebProcess::shared().connection()->send(Messages::WebPageProxy::SetPopupMenuSelectedIndex(selectedIndex), m_page->pageID());
167 #endif
168
169 #if ENABLE(TIZEN_WEBKIT2_POPUP_INTERNAL)
170     TIZEN_LOGI("");
171     if (!m_page || !m_popupClient)
172         return;
173
174     Vector<WebPopupItem> items = populateItems();
175     if (items.isEmpty()) {
176         WebProcess::shared().connection()->send(Messages::WebPageProxy::HidePopupMenu(), m_page->pageID());
177         m_popupClient->popupDidHide();
178         return;
179     }
180
181     WebProcess::shared().connection()->send(Messages::WebPageProxy::UpdatePopupMenu(m_popupClient->menuStyle().textDirection(), items, m_popupClient->selectedIndex()), m_page->pageID());
182 #endif
183 }
184
185 } // namespace WebKit