1/*
2 * Copyright (C) 2010 Girish Ramakrishnan <girish@forwardbias.in>
3 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
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#include "config.h"
22#include "QtFallbackWebPopup.h"
23
24#ifndef QT_NO_COMBOBOX
25
26#include "QWebPageAdapter.h"
27#include "QWebPageClient.h"
28#include "QtWebComboBox.h"
29#include "qgraphicswebview.h"
30#include <QGraphicsProxyWidget>
31#include <QtGui/QStandardItemModel>
32
33namespace WebCore {
34
35QtFallbackWebPopup::QtFallbackWebPopup(const QWebPageAdapter* page)
36    : m_combo(0)
37    , m_page(page)
38{
39}
40
41QtFallbackWebPopup::~QtFallbackWebPopup()
42{
43    deleteComboBox();
44}
45
46void QtFallbackWebPopup::show(const QWebSelectData& data)
47{
48    if (!pageClient())
49        return;
50
51    deleteComboBox();
52
53    m_combo = new QtWebComboBox();
54    connect(m_combo, SIGNAL(activated(int)), SLOT(activeChanged(int)), Qt::QueuedConnection);
55    connect(m_combo, SIGNAL(didHide()), SLOT(deleteComboBox()));
56    connect(m_combo, SIGNAL(didHide()), SIGNAL(didHide()));
57
58    populate(data);
59
60    QRect rect = geometry();
61    if (QGraphicsWebView *webView = qobject_cast<QGraphicsWebView*>(pageClient()->pluginParent())) {
62        QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget(webView);
63        proxy->setWidget(m_combo);
64        proxy->setGeometry(rect);
65    } else {
66        m_combo->setParent(qobject_cast<QWidget*>(pageClient()->ownerWidget()));
67        m_combo->setGeometry(QRect(rect.left(), rect.top(), rect.width(), m_combo->sizeHint().height()));
68    }
69
70    m_combo->showPopupAtCursorPosition();
71}
72
73void QtFallbackWebPopup::hide()
74{
75    // Destroying the QComboBox here cause problems if the popup is in the
76    // middle of its show animation. Instead we rely on the fact that the
77    // Qt::Popup window will hide itself on mouse events outside its window.
78}
79
80void QtFallbackWebPopup::populate(const QWebSelectData& data)
81{
82    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(m_combo->model());
83    Q_ASSERT(model);
84
85    m_combo->setFont(font());
86
87    int currentIndex = -1;
88    for (int i = 0; i < data.itemCount(); ++i) {
89        switch (data.itemType(i)) {
90        case QWebSelectData::Separator:
91            m_combo->insertSeparator(i);
92            break;
93        case QWebSelectData::Group:
94            m_combo->insertItem(i, data.itemText(i));
95            model->item(i)->setEnabled(false);
96            break;
97        case QWebSelectData::Option:
98            m_combo->insertItem(i, data.itemText(i));
99            model->item(i)->setEnabled(data.itemIsEnabled(i));
100#ifndef QT_NO_TOOLTIP
101            model->item(i)->setToolTip(data.itemToolTip(i));
102#endif
103            model->item(i)->setBackground(data.itemBackgroundColor(i));
104            model->item(i)->setForeground(data.itemForegroundColor(i));
105            if (data.itemIsSelected(i))
106                currentIndex = i;
107            break;
108        }
109    }
110
111    if (currentIndex >= 0)
112        m_combo->setCurrentIndex(currentIndex);
113}
114
115void QtFallbackWebPopup::activeChanged(int index)
116{
117    if (index < 0)
118        return;
119
120    emit selectItem(index, false, false);
121}
122
123void QtFallbackWebPopup::deleteComboBox()
124{
125    if (!m_combo)
126        return;
127    m_combo->deleteLater();
128    m_combo = 0;
129}
130
131QWebPageClient* QtFallbackWebPopup::pageClient() const
132{
133    return m_page->client.data();
134}
135
136}
137
138#endif // QT_NO_COMBOBOX
139