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_chromeClient(chromeClient)
86 {
87 }
88 
~PopupMenuQt()89 PopupMenuQt::~PopupMenuQt()
90 {
91 }
92 
disconnectClient()93 void PopupMenuQt::disconnectClient()
94 {
95     m_popupClient = 0;
96 }
97 
show(const IntRect & rect,FrameView * view,int index)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.get(), SIGNAL(didHide()), this, SLOT(didHide()));
107         connect(m_popup.get(), SIGNAL(selectItem(int, bool, bool)), this, SLOT(selectItem(int, bool, bool)));
108     }
109 
110     if (QtFallbackWebPopup* fallback = qobject_cast<QtFallbackWebPopup*>(m_popup.get())) {
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     m_selectData = adoptPtr(new SelectData(m_popupClient));
118     m_popup->show(*m_selectData.get());
119 #endif
120 }
121 
didHide()122 void PopupMenuQt::didHide()
123 {
124     if (m_popupClient)
125         m_popupClient->popupDidHide();
126 }
127 
hide()128 void PopupMenuQt::hide()
129 {
130     if (m_popup)
131         m_popup->hide();
132 }
133 
updateFromElement()134 void PopupMenuQt::updateFromElement()
135 {
136     if (m_popupClient)
137         m_popupClient->setTextFromItem(m_popupClient->selectedIndex());
138 }
139 
selectItem(int index,bool ctrl,bool shift)140 void PopupMenuQt::selectItem(int index, bool ctrl, bool shift)
141 {
142     if (!m_popupClient)
143         return;
144 
145 #if ENABLE(NO_LISTBOX_RENDERING)
146     ListPopupMenuClient* client = static_cast<ListPopupMenuClient*>(m_popupClient);
147     if (client) {
148         client->listBoxSelectItem(index, ctrl, shift);
149         return;
150     }
151 #endif
152 
153     m_popupClient->valueChanged(index);
154 }
155 
156 }
157 
158 #include "moc_PopupMenuQt.cpp"
159 
160 // vim: ts=4 sw=4 et
161