OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebKit / qt / WebCoreSupport / PopupMenuQt.cpp
1 /*
2  * This file is part of the popup menu implementation for <select> elements in WebCore.
3  *
4  * Copyright (C) 2008, 2009, 2010 Nokia Corporation and/or its subsidiary(-ies)
5  * Copyright (C) 2006 Apple Computer, Inc.
6  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
7  * Coypright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25
26 #include "config.h"
27 #include "PopupMenuQt.h"
28
29 #include "ChromeClientQt.h"
30 #include "FrameView.h"
31 #include "PopupMenuClient.h"
32 #include "QtFallbackWebPopup.h"
33
34 #include "qwebkitplatformplugin.h"
35
36 class SelectData : public QWebSelectData {
37 public:
38     SelectData(WebCore::PopupMenuClient*& data) : d(data) {}
39
40     virtual ItemType itemType(int) const;
41     virtual QString itemText(int idx) const { return QString(d ? d->itemText(idx) : ""); }
42     virtual QString itemToolTip(int idx) const { return QString(d ? d->itemToolTip(idx) : ""); }
43     virtual bool itemIsEnabled(int idx) const { return d ? d->itemIsEnabled(idx) : false; }
44     virtual int itemCount() const { return d ? d->listSize() : 0; }
45     virtual bool itemIsSelected(int idx) const { return d ? d->itemIsSelected(idx) : false; }
46     virtual bool multiple() const;
47
48 private:
49     WebCore::PopupMenuClient*& d;
50 };
51
52 bool SelectData::multiple() const
53 {
54     if (!d)
55         return false;
56
57 #if ENABLE(NO_LISTBOX_RENDERING)
58     WebCore::ListPopupMenuClient* client = static_cast<WebCore::ListPopupMenuClient*>(d);
59     return client && client->multiple();
60 #else
61     return false;
62 #endif
63 }
64
65 SelectData::ItemType SelectData::itemType(int idx) const
66 {
67     if (!d)
68         return SelectData::Option;
69
70     if (d->itemIsSeparator(idx))
71         return SelectData::Separator;
72     if (d->itemIsLabel(idx))
73         return SelectData::Group;
74     return SelectData::Option;
75 }
76
77 namespace WebCore {
78
79 PopupMenuQt::PopupMenuQt(PopupMenuClient* client, const ChromeClientQt* chromeClient)
80     : m_popupClient(client)
81     , m_popup(0)
82     , m_selectData(0)
83     , m_chromeClient(chromeClient)
84 {
85 }
86
87 PopupMenuQt::~PopupMenuQt()
88 {
89     delete m_selectData;
90     delete m_popup;
91 }
92
93 void PopupMenuQt::disconnectClient()
94 {
95     m_popupClient = 0;
96 }
97
98 void PopupMenuQt::show(const IntRect& rect, FrameView* view, int index)
99 {
100 #ifndef QT_NO_COMBOBOX
101     if (!m_popupClient)
102         return;
103
104     if (!m_popup) {
105         m_popup = m_chromeClient->createSelectPopup();
106         connect(m_popup, SIGNAL(didHide()), this, SLOT(didHide()));
107         connect(m_popup, SIGNAL(selectItem(int, bool, bool)), this, SLOT(selectItem(int, bool, bool)));
108     }
109
110     if (QtFallbackWebPopup* fallback = qobject_cast<QtFallbackWebPopup*>(m_popup)) {
111         QRect geometry(rect);
112         geometry.moveTopLeft(view->contentsToWindow(rect.location()));
113         fallback->setGeometry(geometry);
114         fallback->setFont(m_popupClient->menuStyle().font().font());
115     }
116
117     if (m_selectData)
118         delete m_selectData;
119     m_selectData = new SelectData(m_popupClient);
120     m_popup->show(*m_selectData);
121 #endif
122 }
123
124 void PopupMenuQt::didHide()
125 {
126     if (m_popupClient)
127         m_popupClient->popupDidHide();
128 }
129
130 void PopupMenuQt::hide()
131 {
132     if (m_popup)
133         m_popup->hide();
134 }
135
136 void PopupMenuQt::updateFromElement()
137 {
138     if (m_popupClient)
139         m_popupClient->setTextFromItem(m_popupClient->selectedIndex());
140 }
141
142 void PopupMenuQt::selectItem(int index, bool ctrl, bool shift)
143 {
144     if (!m_popupClient)
145         return;
146
147 #if ENABLE(NO_LISTBOX_RENDERING)
148     ListPopupMenuClient* client = static_cast<ListPopupMenuClient*>(m_popupClient);
149     if (client) {
150         client->listBoxSelectItem(index, ctrl, shift);
151         return;
152     }
153 #endif
154
155     m_popupClient->valueChanged(index);
156 }
157
158 }
159
160 // vim: ts=4 sw=4 et