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:
SelectData(WebCore::PopupMenuClient * & data)38 SelectData(WebCore::PopupMenuClient*& data) : d(data) {}
39
40 virtual ItemType itemType(int) const;
itemText(int idx) const41 virtual QString itemText(int idx) const { return QString(d ? d->itemText(idx) : ""); }
itemToolTip(int idx) const42 virtual QString itemToolTip(int idx) const { return QString(d ? d->itemToolTip(idx) : ""); }
itemIsEnabled(int idx) const43 virtual bool itemIsEnabled(int idx) const { return d ? d->itemIsEnabled(idx) : false; }
itemCount() const44 virtual int itemCount() const { return d ? d->listSize() : 0; }
itemIsSelected(int idx) const45 virtual bool itemIsSelected(int idx) const { return d ? d->itemIsSelected(idx) : false; }
46 virtual bool multiple() const;
backgroundColor() const47 virtual QColor backgroundColor() const { return d ? QColor(d->menuStyle().backgroundColor()) : QColor(); }
foregroundColor() const48 virtual QColor foregroundColor() const { return d ? QColor(d->menuStyle().foregroundColor()) : QColor(); }
itemBackgroundColor(int idx) const49 virtual QColor itemBackgroundColor(int idx) const { return d ? QColor(d->itemStyle(idx).backgroundColor()) : QColor(); }
itemForegroundColor(int idx) const50 virtual QColor itemForegroundColor(int idx) const { return d ? QColor(d->itemStyle(idx).foregroundColor()) : QColor(); }
51
52 private:
53 WebCore::PopupMenuClient*& d;
54 };
55
multiple() const56 bool SelectData::multiple() const
57 {
58 if (!d)
59 return false;
60
61 #if ENABLE(NO_LISTBOX_RENDERING)
62 WebCore::ListPopupMenuClient* client = static_cast<WebCore::ListPopupMenuClient*>(d);
63 return client && client->multiple();
64 #else
65 return false;
66 #endif
67 }
68
itemType(int idx) const69 SelectData::ItemType SelectData::itemType(int idx) const
70 {
71 if (!d)
72 return SelectData::Option;
73
74 if (d->itemIsSeparator(idx))
75 return SelectData::Separator;
76 if (d->itemIsLabel(idx))
77 return SelectData::Group;
78 return SelectData::Option;
79 }
80
81 namespace WebCore {
82
PopupMenuQt(PopupMenuClient * client,const ChromeClientQt * chromeClient)83 PopupMenuQt::PopupMenuQt(PopupMenuClient* client, const ChromeClientQt* chromeClient)
84 : m_popupClient(client)
85 , m_popup(0)
86 , m_selectData(0)
87 , m_chromeClient(chromeClient)
88 {
89 }
90
~PopupMenuQt()91 PopupMenuQt::~PopupMenuQt()
92 {
93 delete m_selectData;
94 delete m_popup;
95 }
96
disconnectClient()97 void PopupMenuQt::disconnectClient()
98 {
99 m_popupClient = 0;
100 }
101
show(const IntRect & rect,FrameView * view,int index)102 void PopupMenuQt::show(const IntRect& rect, FrameView* view, int index)
103 {
104 #ifndef QT_NO_COMBOBOX
105 if (!m_popupClient)
106 return;
107
108 if (!m_popup) {
109 m_popup = m_chromeClient->createSelectPopup();
110 connect(m_popup, SIGNAL(didHide()), this, SLOT(didHide()));
111 connect(m_popup, SIGNAL(selectItem(int, bool, bool)), this, SLOT(selectItem(int, bool, bool)));
112 }
113
114 if (QtFallbackWebPopup* fallback = qobject_cast<QtFallbackWebPopup*>(m_popup)) {
115 QRect geometry(rect);
116 geometry.moveTopLeft(view->contentsToWindow(rect.location()));
117 fallback->setGeometry(geometry);
118 fallback->setFont(m_popupClient->menuStyle().font().font());
119 }
120
121 if (m_selectData)
122 delete m_selectData;
123 m_selectData = new SelectData(m_popupClient);
124 m_popup->show(*m_selectData);
125 #endif
126 }
127
didHide()128 void PopupMenuQt::didHide()
129 {
130 if (m_popupClient)
131 m_popupClient->popupDidHide();
132 }
133
hide()134 void PopupMenuQt::hide()
135 {
136 if (m_popup)
137 m_popup->hide();
138 }
139
updateFromElement()140 void PopupMenuQt::updateFromElement()
141 {
142 if (m_popupClient)
143 m_popupClient->setTextFromItem(m_popupClient->selectedIndex());
144 }
145
selectItem(int index,bool ctrl,bool shift)146 void PopupMenuQt::selectItem(int index, bool ctrl, bool shift)
147 {
148 if (!m_popupClient)
149 return;
150
151 #if ENABLE(NO_LISTBOX_RENDERING)
152 ListPopupMenuClient* client = static_cast<ListPopupMenuClient*>(m_popupClient);
153 if (client) {
154 client->listBoxSelectItem(index, ctrl, shift);
155 return;
156 }
157 #endif
158
159 m_popupClient->valueChanged(index);
160 }
161
162 }
163
164 #include "moc_PopupMenuQt.cpp"
165
166 // vim: ts=4 sw=4 et
167