2d1943ffa0392fcff317ae91f19ddcb7f1d18036
[quassel.git] / src / uisupport / graphicalui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This contains code from KStatusNotifierItem, part of the KDE libs     *
6  *   Copyright (C) 2009 Marco Martin <notmart@gmail.com>                   *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) version 3.                                           *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23
24 #include "graphicalui.h"
25
26 #include "actioncollection.h"
27 #include "contextmenuactionprovider.h"
28
29 #ifdef Q_WS_X11
30 #  include <QX11Info>
31 #endif
32 #ifdef HAVE_KDE
33 #  include <KWindowInfo>
34 #  include <KWindowSystem>
35 #endif
36
37 GraphicalUi *GraphicalUi::_instance = 0;
38 QWidget *GraphicalUi::_mainWidget = 0;
39 QHash<QString, ActionCollection *> GraphicalUi::_actionCollections;
40 ContextMenuActionProvider *GraphicalUi::_contextMenuActionProvider = 0;
41 ToolBarActionProvider *GraphicalUi::_toolBarActionProvider = 0;
42 UiStyle *GraphicalUi::_uiStyle = 0;
43 bool GraphicalUi::_onAllDesktops = false;
44
45 GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent) {
46   Q_ASSERT(!_instance);
47   _instance = this;
48
49 #ifdef Q_WS_WIN
50   _dwTickCount = 0;
51 #endif
52 }
53
54 void GraphicalUi::init() {
55 #ifdef Q_WS_WIN
56   mainWidget()->installEventFilter(this);
57 #endif
58 }
59
60 ActionCollection *GraphicalUi::actionCollection(const QString &category) {
61   if(_actionCollections.contains(category))
62     return _actionCollections.value(category);
63   ActionCollection *coll = new ActionCollection(_mainWidget);
64   if(_mainWidget)
65     coll->addAssociatedWidget(_mainWidget);
66   _actionCollections.insert(category, coll);
67   return coll;
68 }
69
70 void GraphicalUi::setMainWidget(QWidget *widget) {
71   _mainWidget = widget;
72 }
73
74 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider) {
75   _contextMenuActionProvider = provider;
76 }
77
78 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider) {
79   _toolBarActionProvider = provider;
80 }
81
82 void GraphicalUi::setUiStyle(UiStyle *style) {
83   _uiStyle = style;
84 }
85
86 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event) {
87 #ifdef Q_WS_WIN
88   if(obj == mainWidget() && event->type() == QEvent::ActivationChange) {
89     _dwTickCount = GetTickCount();
90   }
91 #endif
92   return AbstractUi::eventFilter(obj, event);
93 }
94
95 // Code taken from KStatusNotifierItem for handling minimize/restore
96
97 bool GraphicalUi::checkMainWidgetVisibility(bool perform) {
98 #ifdef Q_WS_WIN
99   // the problem is that we lose focus when the systray icon is activated
100   // and we don't know the former active window
101   // therefore we watch for activation event and use our stopwatch :)
102   if(GetTickCount() - _dwTickCount < 300) {
103     // we were active in the last 300ms -> hide it
104     minimizeRestore(false);
105   } else {
106     minimizeRestore(true);
107   }
108
109 #elif defined(HAVE_KDE) && defined(Q_WS_X11)
110   KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
111   // mapped = visible (but possibly obscured)
112   bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
113
114   //    - not mapped -> show, raise, focus
115   //    - mapped
116   //        - obscured -> raise, focus
117   //        - not obscured -> hide
118   //info1.mappingState() != NET::Visible -> window on another desktop?
119   if(!mapped) {
120     if(perform)
121       minimizeRestore(true);
122     return true;
123
124   } else {
125     QListIterator< WId > it (KWindowSystem::stackingOrder());
126     it.toBack();
127     while(it.hasPrevious()) {
128       WId id = it.previous();
129       if(id == mainWidget()->winId())
130         break;
131
132       KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
133
134       if(info2.mappingState() != NET::Visible)
135         continue; // not visible on current desktop -> ignore
136
137       if(!info2.geometry().intersects(mainWidget()->geometry()))
138         continue; // not obscuring the window -> ignore
139
140       if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
141         continue; // obscured by window kept above -> ignore
142
143       NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
144                                               | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
145                                               | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
146
147       if(type == NET::Dock || type == NET::TopMenu)
148         continue; // obscured by dock or topmenu -> ignore
149
150       if(perform) {
151         KWindowSystem::raiseWindow(mainWidget()->winId());
152         KWindowSystem::activateWindow(mainWidget()->winId());
153       }
154       return true;
155     }
156
157     //not on current desktop?
158     if(!info1.isOnCurrentDesktop()) {
159       if(perform)
160         KWindowSystem::activateWindow(mainWidget()->winId());
161       return true;
162     }
163
164     if(perform)
165       minimizeRestore(false); // hide
166     return false;
167   }
168 #else
169
170   if(!mainWidget()->isVisible() || mainWidget()->isMinimized()) {
171     if(perform)
172       minimizeRestore(true);
173     return true;
174   } else {
175     if(perform)
176       minimizeRestore(false);
177     return false;
178   }
179
180 #endif
181
182   return true;
183 }
184
185 void GraphicalUi::minimizeRestore(bool show) {
186   if(show)
187     activateMainWidget();
188   else
189     hideMainWidget();
190 }
191
192 void GraphicalUi::activateMainWidget() {
193 #ifdef HAVE_KDE
194 #  ifdef Q_WS_X11
195     KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
196     if(_onAllDesktops) {
197       KWindowSystem::setOnAllDesktops(mainWidget()->winId(), true);
198     } else {
199       KWindowSystem::setCurrentDesktop(info.desktop());
200     }
201
202     mainWidget()->move(info.frameGeometry().topLeft()); // avoid placement policies
203     mainWidget()->show();
204     mainWidget()->raise();
205     KWindowSystem::raiseWindow(mainWidget()->winId());
206     KWindowSystem::activateWindow(mainWidget()->winId());
207 #  else
208     mainWidget()->show();
209     KWindowSystem::raiseWindow(mainWidget()->winId());
210     KWindowSystem::forceActiveWindow(mainWidget()->winId());
211 #  endif
212
213 #else /* HAVE_KDE */
214
215 #ifdef Q_WS_X11
216   // Bypass focus stealing prevention
217   QX11Info::setAppUserTime(QX11Info::appTime());
218 #endif
219
220   if(mainWidget()->windowState() & Qt::WindowMinimized) {
221     // restore
222     mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
223   }
224
225   // this does not actually work on all platforms... and causes more evil than good
226   // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
227   mainWidget()->show();
228   mainWidget()->raise();
229   mainWidget()->activateWindow();
230
231 #endif /* HAVE_KDE */
232 }
233
234 void GraphicalUi::hideMainWidget() {
235
236 #if defined(HAVE_KDE) && defined(Q_WS_X11)
237   KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
238   _onAllDesktops = info.onAllDesktops();
239 #endif
240
241   if(instance()->isHidingMainWidgetAllowed())
242     mainWidget()->hide();
243 }
244
245 void GraphicalUi::toggleMainWidget() {
246   instance()->checkMainWidgetVisibility(true);
247 }