summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/webkit/WebCore/plugins/qt/PluginContainerQt.cpp
blob: f9693aa5ff03a49e72038070dfd723dc094375ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
    Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"
#include "PluginContainerQt.h"

#include "FocusController.h"
#include "Frame.h"
#include "FrameView.h"
#include "Page.h"
#include "PlatformKeyboardEvent.h"
#include "PlatformWheelEvent.h"
#include "PluginView.h"
#include <QApplication>
#include <QX11Info>

using namespace WebCore;

PluginClientWrapper::PluginClientWrapper(QWidget* parent, WId client)
    : QWidget(0, Qt::Popup)
{
    // create a QWidget that adopts the plugin window id, do not give it
    // a parent so that we don't end up handling events supposed to be
    // handled by the QX11EmbedContainer.

    // without the parent this will be considered a toplevel widget,
    // and thus make Qt not quit the event loop after the last window
    // has been closed. In order to work around this, we set the window
    // type to Qt::Popup.

    create(client, false, true);
    m_parent = parent;
}

PluginClientWrapper::~PluginClientWrapper()
{
    destroy(false, false);
}

bool PluginClientWrapper::x11Event(XEvent* event)
{
    // modify the event window id and insert it into the Qt event system.
    event->xany.window = m_parent->parentWidget()->winId();
    static_cast<QApplication*>(QApplication::instance())->x11ProcessEvent(event);
    return true;
}

PluginContainerQt::PluginContainerQt(PluginView* view, QWidget* parent)
    : QX11EmbedContainer(parent)
    , m_pluginView(view)
    , m_clientWrapper(0)
{
    connect(this, SIGNAL(clientClosed()), this, SLOT(on_clientClosed()));
    connect(this, SIGNAL(clientIsEmbedded()), this, SLOT(on_clientIsEmbedded()));
}

PluginContainerQt::~PluginContainerQt()
{
    delete m_clientWrapper;
    m_pluginView->setPlatformPluginWidget(0);
}

void PluginContainerQt::on_clientClosed()
{
    delete m_clientWrapper;
    m_clientWrapper = 0;
}

void PluginContainerQt::on_clientIsEmbedded()
{
    delete m_clientWrapper;
    m_clientWrapper = 0;

    // Only create a QWidget wrapper for the plugin in the case it isn't in the
    // Qt window mapper, and thus receiving events from the Qt event system.
    // This way the PluginClientWrapper receives the scroll events and passes
    // them to the parent. NOTICE: Native Qt based plugins running in process,
    // will already be in the window mapper, and thus creating a wrapper, stops
    // them from getting events from Qt, as they are redirected to the wrapper.
    if (!QWidget::find(clientWinId()))
        m_clientWrapper = new PluginClientWrapper(this, clientWinId());
}

void PluginContainerQt::redirectWheelEventsToParent(bool enable)
{
    // steal wheel events from the plugin as we want to handle it. When doing this
    // all button 4, 5, 6, and 7, ButtonPress and ButtonRelease events are passed
    // to the x11Event handler of the PluginClientWrapper, which then changes the
    // window id of the event to the parent of PluginContainer and puts the event
    // back into the Qt event loop, so that we will actually scroll the parent
    // frame.
    for (int buttonNo = 4; buttonNo < 8; buttonNo++) {
        if (enable)
            XGrabButton(x11Info().display(), buttonNo, AnyModifier, clientWinId(),
                false, ButtonPressMask, GrabModeAsync, GrabModeAsync, 0L, 0L);
        else
            XUngrabButton(x11Info().display(), buttonNo, AnyModifier, clientWinId());
    }
}

bool PluginContainerQt::x11Event(XEvent* event)
{
    switch (event->type) {
    case EnterNotify:
        // if the plugin window doesn't have focus we do not want to send wheel
        // events to it, but to the parent frame, so let's redirect here.
        redirectWheelEventsToParent(!hasFocus());
        break;
    case LeaveNotify:
        // it is always safe to ungrab wheel events when the mouse leaves the
        // plugin window.
        redirectWheelEventsToParent(false);
        break;
    }

    return QX11EmbedContainer::x11Event(event);
}

void PluginContainerQt::focusInEvent(QFocusEvent* event)
{
    // we got focus, stop redirecting the wheel events
    redirectWheelEventsToParent(false);

    if (Page* page = m_pluginView->parentFrame()->page())
        page->focusController()->setActive(true);

    m_pluginView->focusPluginElement();
}

void PluginContainerQt::focusOutEvent(QFocusEvent*)
{
    if (Page* page = m_pluginView->parentFrame()->page())
        page->focusController()->setActive(false);
}