~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/kworkspace/kwindowlistmenu.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
Copyright (c) 2000 Matthias Elter <elter@kde.org>
 
4
                   Matthias Ettrich <ettrich@kde.org>
 
5
 
 
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:
 
12
 
 
13
The above copyright notice and this permission notice shall be included in
 
14
all copies or substantial portions of the Software.
 
15
 
 
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.
 
22
 
 
23
******************************************************************/
 
24
 
 
25
#include "kwindowlistmenu.h"
 
26
 
 
27
#include <QtCore/QBool>
 
28
 
 
29
#include <QtGui/QApplication>
 
30
#include <QtGui/QDesktopWidget>
 
31
#include <QtGui/QPainter>
 
32
#include <QtGui/QX11Info>
 
33
 
 
34
#include <QtDBus/QtDBus>
 
35
#include <klocale.h>
 
36
#include <kstringhandler.h>
 
37
#ifndef WIN32
 
38
#include <fixx11h.h>
 
39
#endif
 
40
#include <kstyle.h>
 
41
#include <kwindowsystem.h>
 
42
#include <netwm.h>
 
43
 
 
44
#undef Bool
 
45
#include "kwindowlistmenu.moc"
 
46
#include "kwin_interface.h"
 
47
 
 
48
static bool compareKWinWindowInfo( KWindowInfo* firstInfo, KWindowInfo* secondInfo )
 
49
{
 
50
  QString firstTitle, secondTitle;
 
51
 
 
52
  if ( firstInfo )
 
53
    firstTitle = firstInfo->visibleNameWithState().toLower();
 
54
 
 
55
  if ( secondInfo )
 
56
    secondTitle = secondInfo->visibleNameWithState().toLower();
 
57
 
 
58
  return firstTitle.compare( secondTitle ) >= 0;
 
59
}
 
60
 
 
61
class KWindowListMenu::Private
 
62
{
 
63
};
 
64
 
 
65
KWindowListMenu::KWindowListMenu( QWidget *parent )
 
66
  : KMenu( parent ), d( new Private )
 
67
{
 
68
}
 
69
 
 
70
KWindowListMenu::~KWindowListMenu()
 
71
{
 
72
  delete d;
 
73
}
 
74
 
 
75
static bool standaloneDialog( const KWindowInfo* info, const QList<KWindowInfo*>& list )
 
76
{
 
77
#ifndef Q_WS_WIN
 
78
  WId group = info->groupLeader();
 
79
 
 
80
  if ( group == 0 )
 
81
  return info->transientFor() == QX11Info::appRootWindow();
 
82
 
 
83
  foreach ( KWindowInfo* info, list )
 
84
    if ( info->groupLeader() == group )
 
85
      return false;
 
86
#endif
 
87
  return true;
 
88
}
 
89
 
 
90
void KWindowListMenu::init()
 
91
{
 
92
  int numberOfDesktops = KWindowSystem::numberOfDesktops();
 
93
  int currentDesktop = KWindowSystem::currentDesktop();
 
94
  WId activeWindow = KWindowSystem::activeWindow();
 
95
 
 
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;
 
99
 
 
100
  clear();
 
101
 
 
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() ) );
 
106
 
 
107
  // if we only have one desktop we won't be showing titles, so put a separator in
 
108
  if ( numberOfDesktops == 1 )
 
109
    addSeparator();
 
110
 
 
111
  QList<KWindowInfo> windows;
 
112
  foreach ( WId id, KWindowSystem::windows() )
 
113
    windows.append( KWindowSystem::windowInfo( id, NET::WMDesktop ) );
 
114
 
 
115
  bool showAllDesktopsGroup = ( numberOfDesktops > 1 );
 
116
 
 
117
  int i = 0;
 
118
  for ( int j = 1; j <= numberOfDesktops + (showAllDesktopsGroup ? 1 : 0); j++ ) {
 
119
    bool onAllDesktops = ( j > numberOfDesktops );
 
120
    int items = 0;
 
121
 
 
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);
 
125
 
 
126
    QList<KWindowInfo*> list;
 
127
 
 
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 ) );
 
134
      }
 
135
    }
 
136
 
 
137
    qStableSort( list.begin(), list.end(), compareKWinWindowInfo );
 
138
 
 
139
    foreach ( KWindowInfo* info, list ) {
 
140
      ++i;
 
141
      QString itemText = fontMetrics().elidedText(info->visibleNameWithState(), Qt::ElideMiddle, maxwidth);
 
142
 
 
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 );
 
146
 
 
147
      if ( (windowType == NET::Normal || windowType == NET::Unknown
 
148
              || (windowType == NET::Dialog && standaloneDialog( info, list )))
 
149
              && !(info->state() & NET::SkipTaskbar) ) {
 
150
 
 
151
        QPixmap pm = KWindowSystem::icon( info->win(), 16, 16, true );
 
152
        items++;
 
153
 
 
154
        // ok, we have items on this desktop, let's show the title
 
155
        if ( items == 1 && numberOfDesktops > 1 ) {
 
156
          if( !onAllDesktops )
 
157
              addTitle( KWindowSystem::desktopName( j ) );
 
158
          else
 
159
              addTitle( i18n( "On All Desktops" ) );
 
160
        }
 
161
 
 
162
        // Avoid creating unwanted accelerators.
 
163
        itemText.replace( '&', QLatin1String( "&&" ));
 
164
 
 
165
        QAction* action = addAction( pm, itemText, this, SLOT( slotForceActiveWindow() ) );
 
166
        action->setData( (int)info->win() );
 
167
 
 
168
        if ( info->win() == activeWindow )
 
169
          action->setChecked( true );
 
170
      }
 
171
    }
 
172
 
 
173
    if ( j == currentDesktop ) {
 
174
      unclutter->setEnabled( items > 0 );
 
175
      cascade->setEnabled( items > 0 );
 
176
    }
 
177
 
 
178
    qDeleteAll( list );
 
179
  }
 
180
 
 
181
  // no windows?
 
182
  if ( i == 0 ) {
 
183
    if ( numberOfDesktops > 1 )
 
184
      addSeparator(); // because we don't have any titles, nor a separator
 
185
 
 
186
    addAction( i18n( "No Windows" ) )->setEnabled( false );
 
187
  }
 
188
}
 
189
 
 
190
void KWindowListMenu::slotForceActiveWindow()
 
191
{
 
192
    QAction* window = qobject_cast<QAction*>(sender());
 
193
    if (!window || !window->data().canConvert(QVariant::Int))
 
194
        return;
 
195
 
 
196
    KWindowSystem::forceActiveWindow((WId)window->data().toInt());
 
197
}
 
198
 
 
199
void KWindowListMenu::slotSetCurrentDesktop()
 
200
{
 
201
    QAction* window = qobject_cast<QAction*>(sender());
 
202
    if (!window || !window->data().canConvert(QVariant::Int))
 
203
        return;
 
204
 
 
205
    KWindowSystem::setCurrentDesktop(window->data().toInt());
 
206
}
 
207
 
 
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()
 
213
{
 
214
    foreach (QAction* action, actions())
 
215
        if (action->isChecked()) {
 
216
            setActiveAction(action);
 
217
            break;
 
218
        }
 
219
}
 
220
 
 
221
void KWindowListMenu::slotUnclutterWindows()
 
222
{
 
223
    org::kde::KWin kwin("org.kde.kwin", "/KWin", QDBusConnection::sessionBus());
 
224
    kwin.unclutterDesktop();
 
225
}
 
226
 
 
227
void KWindowListMenu::slotCascadeWindows()
 
228
{
 
229
    org::kde::KWin kwin("org.kde.kwin", "/KWin", QDBusConnection::sessionBus());
 
230
    kwin.cascadeDesktop();
 
231
}
 
232