2010-01-08 Yuzo Fujishima <yuzo@google.com>
[WebKit-https.git] / WebCore / platform / qt / QtFallbackWebPopup.cpp
1 /*
2  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20 #include "config.h"
21 #include "QtFallbackWebPopup.h"
22
23 #include "HostWindow.h"
24 #include "PopupMenuClient.h"
25 #include "QWebPageClient.h"
26 #include <QAbstractItemView>
27 #include <QApplication>
28 #include <QInputContext>
29 #include <QMouseEvent>
30 #include <QStandardItemModel>
31
32 namespace WebCore {
33
34 // QtFallbackWebPopup
35
36 QtFallbackWebPopup::QtFallbackWebPopup(PopupMenuClient* client)
37     : QtAbstractWebPopup(client)
38     , m_popupVisible(false)
39 {
40     setFont(QtAbstractWebPopup::client()->menuStyle().font().font());
41     connect(this, SIGNAL(activated(int)),
42             SLOT(activeChanged(int)), Qt::QueuedConnection);
43 }
44
45
46 void QtFallbackWebPopup::show(const QRect& geometry, int selectedIndex)
47 {
48     populate();
49     setCurrentIndex(selectedIndex);
50
51     QWidget* parent = 0;
52     if (client()->hostWindow() && client()->hostWindow()->platformPageClient())
53        parent = client()->hostWindow()->platformPageClient()->ownerWidget();
54
55     setParent(parent);
56     setGeometry(QRect(geometry.left(), geometry.top(), geometry.width(), sizeHint().height()));
57
58     QMouseEvent event(QEvent::MouseButtonPress, QCursor::pos(), Qt::LeftButton,
59                       Qt::LeftButton, Qt::NoModifier);
60     QCoreApplication::sendEvent(this, &event);
61 }
62
63 void QtFallbackWebPopup::populate()
64 {
65     clear();
66
67     QStandardItemModel* model = qobject_cast<QStandardItemModel*>(QComboBox::model());
68     Q_ASSERT(model);
69
70     int size = client()->listSize();
71     for (int i = 0; i < size; i++) {
72         if (client()->itemIsSeparator(i))
73             insertSeparator(i);
74         else {
75             insertItem(i, client()->itemText(i));
76
77             if (model && !client()->itemIsEnabled(i))
78                 model->item(i)->setEnabled(false);
79         }
80     }
81 }
82
83 void QtFallbackWebPopup::showPopup()
84 {
85     QComboBox::showPopup();
86     m_popupVisible = true;
87 }
88
89 void QtFallbackWebPopup::hidePopup()
90 {
91     QWidget* activeFocus = QApplication::focusWidget();
92     if (activeFocus && activeFocus == view()
93         && activeFocus->testAttribute(Qt::WA_InputMethodEnabled)) {
94         QInputContext* qic = activeFocus->inputContext();
95         if (qic) {
96             qic->reset();
97             qic->setFocusWidget(0);
98         }
99     }
100
101     QComboBox::hidePopup();
102     if (!m_popupVisible)
103         return;
104
105     m_popupVisible = false;
106     client()->popupDidHide(true);
107 }
108
109 void QtFallbackWebPopup::activeChanged(int index)
110 {
111     if (index < 0)
112         return;
113
114     client()->valueChanged(index);
115 }
116
117 }