1
/*****************************************************************
3
Copyright (c) 2000 Matthias Elter <elter@kde.org>
4
Matthias Ettrich <ettrich@kde.org>
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
******************************************************************/
25
#include "kwindowlistmenu.h"
27
#include <QtCore/QBool>
29
#include <QtGui/QApplication>
30
#include <QtGui/QDesktopWidget>
31
#include <QtGui/QPainter>
32
#include <QtGui/QX11Info>
34
#include <QtDBus/QtDBus>
36
#include <kstringhandler.h>
41
#include <kwindowsystem.h>
45
#include "kwindowlistmenu.moc"
46
#include "kwin_interface.h"
48
static bool compareKWinWindowInfo( KWindowInfo* firstInfo, KWindowInfo* secondInfo )
50
QString firstTitle, secondTitle;
53
firstTitle = firstInfo->visibleNameWithState().toLower();
56
secondTitle = secondInfo->visibleNameWithState().toLower();
58
return firstTitle.compare( secondTitle ) >= 0;
61
class KWindowListMenu::Private
65
KWindowListMenu::KWindowListMenu( QWidget *parent )
66
: KMenu( parent ), d( new Private )
70
KWindowListMenu::~KWindowListMenu()
75
static bool standaloneDialog( const KWindowInfo* info, const QList<KWindowInfo*>& list )
78
WId group = info->groupLeader();
81
return info->transientFor() == QX11Info::appRootWindow();
83
foreach ( KWindowInfo* info, list )
84
if ( info->groupLeader() == group )
90
void KWindowListMenu::init()
92
int numberOfDesktops = KWindowSystem::numberOfDesktops();
93
int currentDesktop = KWindowSystem::currentDesktop();
94
WId activeWindow = KWindowSystem::activeWindow();
96
// Make sure the popup is not too wide, otherwise clicking in the middle of kdesktop
97
// wouldn't leave any place for the popup, and release would activate some menu entry.
98
int maxwidth = qApp->desktop()->screenGeometry( this ).width() / 2 - 100;
102
QAction* unclutter = addAction( i18nc("Action that reorganizes the windows to try to have them less cluttered", "Unclutter Windows"),
103
this, SLOT( slotUnclutterWindows() ) );
104
QAction* cascade = addAction( i18nc("Action that reorganizes the windows so that they are in cascade", "Cascade Windows"),
105
this, SLOT( slotCascadeWindows() ) );
107
// if we only have one desktop we won't be showing titles, so put a separator in
108
if ( numberOfDesktops == 1 )
111
QList<KWindowInfo> windows;
112
foreach ( WId id, KWindowSystem::windows() )
113
windows.append( KWindowSystem::windowInfo( id, NET::WMDesktop ) );
115
bool showAllDesktopsGroup = ( numberOfDesktops > 1 );
118
for ( int j = 1; j <= numberOfDesktops + (showAllDesktopsGroup ? 1 : 0); j++ ) {
119
bool onAllDesktops = ( j > numberOfDesktops );
122
// KDE4 porting - huh? didn't know you could set an item checked before it's created?
123
//if (!activeWindow && d == cd)
124
//setItemChecked(1000 + d, true);
126
QList<KWindowInfo*> list;
128
foreach (const KWindowInfo &wi, windows) {
129
if ( (wi.desktop() == j) || (onAllDesktops && wi.onAllDesktops())
130
|| (!showAllDesktopsGroup && wi.onAllDesktops()) ) {
131
list.append( new KWindowInfo( wi.win(),
132
NET::WMVisibleName | NET::WMState | NET::XAWMState | NET::WMWindowType,
133
NET::WM2GroupLeader | NET::WM2TransientFor ) );
137
qStableSort( list.begin(), list.end(), compareKWinWindowInfo );
139
foreach ( KWindowInfo* info, list ) {
141
QString itemText = fontMetrics().elidedText(info->visibleNameWithState(), Qt::ElideMiddle, maxwidth);
143
NET::WindowType windowType = info->windowType( NET::NormalMask | NET::DesktopMask
144
| NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
145
| NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask );
147
if ( (windowType == NET::Normal || windowType == NET::Unknown
148
|| (windowType == NET::Dialog && standaloneDialog( info, list )))
149
&& !(info->state() & NET::SkipTaskbar) ) {
151
QPixmap pm = KWindowSystem::icon( info->win(), 16, 16, true );
154
// ok, we have items on this desktop, let's show the title
155
if ( items == 1 && numberOfDesktops > 1 ) {
157
addTitle( KWindowSystem::desktopName( j ) );
159
addTitle( i18n( "On All Desktops" ) );
162
// Avoid creating unwanted accelerators.
163
itemText.replace( '&', QLatin1String( "&&" ));
165
QAction* action = addAction( pm, itemText, this, SLOT( slotForceActiveWindow() ) );
166
action->setData( (int)info->win() );
168
if ( info->win() == activeWindow )
169
action->setChecked( true );
173
if ( j == currentDesktop ) {
174
unclutter->setEnabled( items > 0 );
175
cascade->setEnabled( items > 0 );
183
if ( numberOfDesktops > 1 )
184
addSeparator(); // because we don't have any titles, nor a separator
186
addAction( i18n( "No Windows" ) )->setEnabled( false );
190
void KWindowListMenu::slotForceActiveWindow()
192
QAction* window = qobject_cast<QAction*>(sender());
193
if (!window || !window->data().canConvert(QVariant::Int))
196
KWindowSystem::forceActiveWindow((WId)window->data().toInt());
199
void KWindowListMenu::slotSetCurrentDesktop()
201
QAction* window = qobject_cast<QAction*>(sender());
202
if (!window || !window->data().canConvert(QVariant::Int))
205
KWindowSystem::setCurrentDesktop(window->data().toInt());
208
// This popup is much more useful from keyboard if it has the active
209
// window active by default - however, QPopupMenu tries hard to resist.
210
// QPopupMenu::popup() resets the active item, so this needs to be
211
// called after popup().
212
void KWindowListMenu::selectActiveWindow()
214
foreach (QAction* action, actions())
215
if (action->isChecked()) {
216
setActiveAction(action);
221
void KWindowListMenu::slotUnclutterWindows()
223
org::kde::KWin kwin("org.kde.kwin", "/KWin", QDBusConnection::sessionBus());
224
kwin.unclutterDesktop();
227
void KWindowListMenu::slotCascadeWindows()
229
org::kde::KWin kwin("org.kde.kwin", "/KWin", QDBusConnection::sessionBus());
230
kwin.cascadeDesktop();