Get it to compile and run (buggily) on Windows Vista with MinGW in Firefox 3.
[plasmaplugin.git] / qtbrowserplugin / qtbrowserplugin_win.cpp
blob47e8fad0f300bef6811813a987f7923196c45313
1 /****************************************************************************
2 **
3 ** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of a Qt Solutions component.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** Trolltech sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
23 #include <QtGui>
25 #include "qtbrowserplugin.h"
26 #include "qtbrowserplugin_p.h"
28 #include <kapplication.h>
29 #include <kaboutdata.h>
30 #include <KCmdLineArgs>
32 #include <windows.h>
33 #include "qtnpapi.h"
35 static HHOOK hhook = 0;
36 static bool ownsqapp = false;
37 Q_GUI_EXPORT int qt_translateKeyCode(int);
39 LRESULT CALLBACK FilterProc( int nCode, WPARAM wParam, LPARAM lParam )
41 if (qApp)
42 qApp->sendPostedEvents(0, -1);
44 if (nCode < 0 || !(wParam & PM_REMOVE))
45 return CallNextHookEx(hhook, nCode, wParam, lParam);
47 MSG *msg = (MSG*)lParam;
48 bool processed = false;
50 // (some) support for key-sequences via QAction and QShortcut
51 if(msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN) {
52 QWidget *focusWidget = QWidget::find(msg->hwnd);
53 if (focusWidget) {
54 int key = msg->wParam;
55 if (!(key >= 'A' && key <= 'Z') && !(key >= '0' && key <= '9'))
56 key = qt_translateKeyCode(msg->wParam);
58 Qt::KeyboardModifiers modifiers = 0;
59 int modifierKey = 0;
60 if (GetKeyState(VK_SHIFT) < 0) {
61 modifierKey |= Qt::SHIFT;
62 modifiers |= Qt::ShiftModifier;
64 if (GetKeyState(VK_CONTROL) < 0) {
65 modifierKey |= Qt::CTRL;
66 modifiers |= Qt::ControlModifier;
68 if (GetKeyState(VK_MENU) < 0) {
69 modifierKey |= Qt::ALT;
70 modifiers |= Qt::AltModifier;
72 QKeyEvent override(QEvent::ShortcutOverride, key, modifiers);
73 override.ignore();
74 QApplication::sendEvent(focusWidget, &override);
75 processed = override.isAccepted();
77 QKeySequence shortcutKey(modifierKey + key);
78 if (!processed) {
79 QList<QAction*> actions = qFindChildren<QAction*>(focusWidget->window());
80 for (int i = 0; i < actions.count() && !processed; ++i) {
81 QAction *action = actions.at(i);
82 if (!action->isEnabled() || action->shortcut() != shortcutKey)
83 continue;
84 QShortcutEvent event(shortcutKey, 0);
85 processed = QApplication::sendEvent(action, &event);
88 if (!processed) {
89 QList<QShortcut*> shortcuts = qFindChildren<QShortcut*>(focusWidget->window());
90 for (int i = 0; i < shortcuts.count() && !processed; ++i) {
91 QShortcut *shortcut = shortcuts.at(i);
92 if (!shortcut->isEnabled() || shortcut->key() != shortcutKey)
93 continue;
94 QShortcutEvent event(shortcutKey, shortcut->id());
95 processed = QApplication::sendEvent(shortcut, &event);
101 return CallNextHookEx(hhook, nCode, wParam, lParam);
104 extern "C" bool qtns_event(QtNPInstance *, NPEvent *)
106 return false;
109 extern Q_CORE_EXPORT void qWinMsgHandler(QtMsgType t, const char* str);
111 extern "C" void qtns_initialize(QtNPInstance*)
113 if (!qApp) {
114 qInstallMsgHandler(qWinMsgHandler);
115 ownsqapp = true;
116 static int argc=1;
117 static char **argv = new char*;
118 argv[0] = "b";
119 //(void)new QApplication(argc, argv);
121 //BEGIN: PLASMAPLUGIN ALTERED CODE
122 KAboutData aboutData( "plasmoidviewer", 0, ki18n( "Plasma Applet Viewer" ),
123 "1.0", ki18n( "blaaaaaa" ), KAboutData::License_BSD,
124 ki18n( "(C) 2007, The KDE Team" ) );
125 aboutData.setProgramIconName( "plasma" );
126 aboutData.addAuthor( ki18n( "Frerich Raabe" ),
127 ki18n( "Original author" ),
128 "raabe@kde.org" );
129 KCmdLineArgs::init( argc, argv, &aboutData );
130 (void)new KApplication;
131 //END: PLASMAPLUGIN ALTERED CODE
133 QT_WA({
134 hhook = SetWindowsHookExW( WH_GETMESSAGE, FilterProc, 0, GetCurrentThreadId() );
135 }, {
136 hhook = SetWindowsHookExA( WH_GETMESSAGE, FilterProc, 0, GetCurrentThreadId() );
141 extern "C" void qtns_destroy(QtNPInstance *)
145 extern "C" void qtns_shutdown()
147 if (!ownsqapp)
148 return;
150 // check if qApp still runs widgets (in other DLLs)
151 QWidgetList widgets = qApp->allWidgets();
152 int count = widgets.count();
153 for (int w = 0; w < widgets.count(); ++w) {
154 // ignore all Qt generated widgets
155 QWidget *widget = widgets.at(w);
156 if (widget->windowFlags() & Qt::Desktop)
157 count--;
159 if (count) // qApp still used
160 return;
162 delete qApp;
163 ownsqapp = false;
164 if ( hhook )
165 UnhookWindowsHookEx( hhook );
166 hhook = 0;
169 extern "C" void qtns_embed(QtNPInstance *This)
171 Q_ASSERT(qobject_cast<QWidget*>(This->qt.object));
173 LONG oldLong = GetWindowLong(This->window, GWL_STYLE);
174 ::SetWindowLong(This->window, GWL_STYLE, oldLong | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
175 ::SetWindowLong(This->qt.widget->winId(), GWL_STYLE, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
176 ::SetParent(This->qt.widget->winId(), This->window);
179 extern "C" void qtns_setGeometry(QtNPInstance *This, const QRect &rect, const QRect &)
181 Q_ASSERT(qobject_cast<QWidget*>(This->qt.object));
183 This->qt.widget->setGeometry(QRect(0, 0, rect.width(), rect.height()));
187 extern "C" void qtns_print(QtNPInstance * This, NPPrint *printInfo)
189 NPWindow* printWindow = &(printInfo->print.embedPrint.window);
190 void* platformPrint = printInfo->print.embedPrint.platformPrint;
191 // #### Nothing yet.