add more spacing
[personal-kdebase.git] / workspace / libs / kworkspace / kwindowlistmenu.cpp
blobf7401ab96d906b74fafe4a8ebff0eabd4fe9eac6
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>
35 #include <klocale.h>
36 #include <kstringhandler.h>
37 #include <kstyle.h>
38 #include <kwindowsystem.h>
39 #include <netwm.h>
41 #undef Bool
42 #include "kwindowlistmenu.moc"
43 #include "kwin_interface.h"
45 static bool compareKWinWindowInfo( KWindowInfo* firstInfo, KWindowInfo* secondInfo )
47 QString firstTitle, secondTitle;
49 if ( firstInfo )
50 firstTitle = firstInfo->visibleNameWithState().toLower();
52 if ( secondInfo )
53 secondTitle = secondInfo->visibleNameWithState().toLower();
55 return firstTitle.compare( secondTitle ) >= 0;
58 class KWindowListMenu::Private
62 KWindowListMenu::KWindowListMenu( QWidget *parent )
63 : KMenu( parent ), d( new Private )
67 KWindowListMenu::~KWindowListMenu()
69 delete d;
72 static bool standaloneDialog( const KWindowInfo* info, const QList<KWindowInfo*>& list )
74 WId group = info->groupLeader();
76 if ( group == 0 )
77 return info->transientFor() == QX11Info::appRootWindow();
79 foreach ( KWindowInfo* info, list )
80 if ( info->groupLeader() == group )
81 return false;
83 return true;
86 void KWindowListMenu::init()
88 int numberOfDesktops = KWindowSystem::numberOfDesktops();
89 int currentDesktop = KWindowSystem::currentDesktop();
90 WId activeWindow = KWindowSystem::activeWindow();
92 // Make sure the popup is not too wide, otherwise clicking in the middle of kdesktop
93 // wouldn't leave any place for the popup, and release would activate some menu entry.
94 int maxwidth = qApp->desktop()->screenGeometry( this ).width() / 2 - 100;
96 clear();
98 QAction* unclutter = addAction( i18nc("Action that reorganizes the windows to try to have them less cluttered", "Unclutter Windows"),
99 this, SLOT( slotUnclutterWindows() ) );
100 QAction* cascade = addAction( i18nc("Action that reorganizes the windows so that they are in cascade", "Cascade Windows"),
101 this, SLOT( slotCascadeWindows() ) );
103 // if we only have one desktop we won't be showing titles, so put a separator in
104 if ( numberOfDesktops == 1 )
105 addSeparator();
107 QList<KWindowInfo> windows;
108 foreach ( WId id, KWindowSystem::windows() )
109 windows.append( KWindowSystem::windowInfo( id, NET::WMDesktop ) );
111 bool showAllDesktopsGroup = ( numberOfDesktops > 1 );
113 int i = 0;
114 for ( int j = 1; j <= numberOfDesktops + (showAllDesktopsGroup ? 1 : 0); j++ ) {
115 bool onAllDesktops = ( j > numberOfDesktops );
116 int items = 0;
118 // KDE4 porting - huh? didn't know you could set an item checked before it's created?
119 //if (!activeWindow && d == cd)
120 //setItemChecked(1000 + d, true);
122 QList<KWindowInfo*> list;
124 foreach (const KWindowInfo &wi, windows) {
125 if ( (wi.desktop() == j) || (onAllDesktops && wi.onAllDesktops())
126 || (!showAllDesktopsGroup && wi.onAllDesktops()) ) {
127 list.append( new KWindowInfo( wi.win(),
128 NET::WMVisibleName | NET::WMState | NET::XAWMState | NET::WMWindowType,
129 NET::WM2GroupLeader | NET::WM2TransientFor ) );
133 qStableSort( list.begin(), list.end(), compareKWinWindowInfo );
135 foreach ( KWindowInfo* info, list ) {
136 ++i;
137 QString itemText = fontMetrics().elidedText(info->visibleNameWithState(), Qt::ElideMiddle, maxwidth);
139 NET::WindowType windowType = info->windowType( NET::NormalMask | NET::DesktopMask
140 | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
141 | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask );
143 if ( (windowType == NET::Normal || windowType == NET::Unknown
144 || (windowType == NET::Dialog && standaloneDialog( info, list )))
145 && !(info->state() & NET::SkipTaskbar) ) {
147 QPixmap pm = KWindowSystem::icon( info->win(), 16, 16, true );
148 items++;
150 // ok, we have items on this desktop, let's show the title
151 if ( items == 1 && numberOfDesktops > 1 ) {
152 if( !onAllDesktops )
153 addTitle( KWindowSystem::desktopName( j ) );
154 else
155 addTitle( i18n( "On All Desktops" ) );
158 // Avoid creating unwanted accelerators.
159 itemText.replace( '&', QLatin1String( "&&" ));
161 QAction* action = addAction( pm, itemText, this, SLOT( slotForceActiveWindow() ) );
162 action->setData( (int)info->win() );
164 if ( info->win() == activeWindow )
165 action->setChecked( true );
169 if ( j == currentDesktop ) {
170 unclutter->setEnabled( items > 0 );
171 cascade->setEnabled( items > 0 );
174 qDeleteAll( list );
177 // no windows?
178 if ( i == 0 ) {
179 if ( numberOfDesktops > 1 )
180 addSeparator(); // because we don't have any titles, nor a separator
182 addAction( i18n( "No Windows" ) )->setEnabled( false );
186 void KWindowListMenu::slotForceActiveWindow()
188 QAction* window = qobject_cast<QAction*>(sender());
189 if (!window || !window->data().canConvert(QVariant::Int))
190 return;
192 KWindowSystem::forceActiveWindow(window->data().toInt());
195 void KWindowListMenu::slotSetCurrentDesktop()
197 QAction* window = qobject_cast<QAction*>(sender());
198 if (!window || !window->data().canConvert(QVariant::Int))
199 return;
201 KWindowSystem::setCurrentDesktop(window->data().toInt());
204 // This popup is much more useful from keyboard if it has the active
205 // window active by default - however, QPopupMenu tries hard to resist.
206 // QPopupMenu::popup() resets the active item, so this needs to be
207 // called after popup().
208 void KWindowListMenu::selectActiveWindow()
210 foreach (QAction* action, actions())
211 if (action->isChecked()) {
212 setActiveAction(action);
213 break;
217 void KWindowListMenu::slotUnclutterWindows()
219 org::kde::KWin kwin("org.kde.kwin", "/KWin", QDBusConnection::sessionBus());
220 kwin.unclutterDesktop();
223 void KWindowListMenu::slotCascadeWindows()
225 org::kde::KWin kwin("org.kde.kwin", "/KWin", QDBusConnection::sessionBus());
226 kwin.cascadeDesktop();