Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / taskmanager / taskrmbmenu.cpp
blob376a8957c977b79fb678ca3a35ac99a4651ff075
1 /*****************************************************************
3 Copyright (c) 2001 Matthias Elter <elter@kde.org>
4 Copyright (c) 2001 John Firebaugh <jfirebaugh@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 // Own
26 #include "taskmanager.h"
28 // System
29 #include <assert.h>
31 // KDE
32 #include <kicon.h>
33 #include <klocale.h>
35 #include "config-X11.h"
37 #if defined(HAVE_XCOMPOSITE) && \
38 defined(HAVE_XRENDER) && \
39 defined(HAVE_XFIXES)
40 #include <fixx11h.h>
41 #endif
43 #include "taskrmbmenu.h"
45 namespace TaskManager
48 TaskRMBMenu::TaskRMBMenu(const TaskList& theTasks, bool show, QWidget *parent)
49 : QMenu( parent )
50 , tasks( theTasks )
51 , showAll( show )
53 assert(tasks.count() > 0);
54 if (tasks.count() == 1)
56 fillMenu(tasks.first());
58 else
60 fillMenu();
64 TaskRMBMenu::TaskRMBMenu(TaskPtr task, bool show, QWidget *parent)
65 : QMenu( parent )
66 , showAll( show )
68 fillMenu(task);
71 void TaskRMBMenu::fillMenu(TaskPtr t)
73 QAction *a;
75 addMenu(makeAdvancedMenu(t));
76 bool checkActions = KWindowSystem::allowedActionsSupported();
78 if (TaskManager::self()->numberOfDesktops() > 1)
80 a = addMenu(makeDesktopsMenu(t));
82 if (showAll)
84 a = addAction(i18n("&To Current Desktop"),
85 t.data(), SLOT(toCurrentDesktop()));
86 a->setEnabled(!t->isOnCurrentDesktop());
89 if (checkActions)
91 a->setEnabled(t->info().actionSupported(NET::ActionChangeDesktop));
95 a = addAction(KIcon("transform-move"), i18n("&Move"), t.data(), SLOT(move()));
96 a->setEnabled(!checkActions || t->info().actionSupported(NET::ActionMove));
98 a = addAction(i18n("Re&size"), t.data(), SLOT(resize()));
99 a->setEnabled(!checkActions || t->info().actionSupported(NET::ActionResize));
101 a = addAction(i18n("Mi&nimize"), t.data(), SLOT(toggleIconified()));
102 a->setCheckable(true);
103 a->setChecked(t->isIconified());
104 a->setEnabled(!checkActions || t->info().actionSupported(NET::ActionMinimize));
106 a = addAction(i18n("Ma&ximize"), t.data(), SLOT(toggleMaximized()));
107 a->setCheckable(true);
108 a->setChecked(t->isMaximized());
109 a->setEnabled(!checkActions || t->info().actionSupported(NET::ActionMax));
111 a = addAction(i18n("&Shade"), t.data(), SLOT(toggleShaded()));
112 a->setCheckable(true);
113 a->setChecked(t->isShaded());
114 a->setEnabled(!checkActions || t->info().actionSupported(NET::ActionShade));
116 addSeparator();
118 a = addAction(KIcon("window-close"), i18n("&Close"), t.data(), SLOT(close()));
119 a->setEnabled(!checkActions || t->info().actionSupported(NET::ActionClose));
122 void TaskRMBMenu::fillMenu()
124 QAction *a;
126 TaskList::iterator itEnd = tasks.end();
127 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
129 TaskPtr t = (*it);
131 a = addMenu( new TaskRMBMenu(t, this) );
132 a->setIcon( QIcon( t->pixmap() ) );
133 a->setText( t->visibleNameWithState() );
134 a->setChecked(t->isActive());
135 connect( a, SIGNAL(triggered()), t.data(), SLOT( activateRaiseOrIconify() ) );
138 addSeparator();
140 bool enable = false;
142 if (TaskManager::self()->numberOfDesktops() > 1)
144 a = addMenu(makeDesktopsMenu());
146 a = addAction(i18n("All &to Current Desktop"), this, SLOT(slotAllToCurrentDesktop()));
147 TaskList::iterator itEnd = tasks.end();
148 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
150 if (!(*it)->isOnCurrentDesktop())
152 enable = true;
153 break;
156 a->setEnabled(enable);
159 enable = false;
161 a = addAction( i18n( "Mi&nimize All" ), this, SLOT( slotMinimizeAll() ) );
162 itEnd = tasks.end();
163 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
165 if( !(*it)->isIconified() ) {
166 enable = true;
167 break;
170 a->setEnabled( enable );
172 enable = false;
174 a = addAction( i18n( "Ma&ximize All" ), this, SLOT( slotMaximizeAll() ) );
175 itEnd = tasks.end();
176 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
178 if( !(*it)->isMaximized() ) {
179 enable = true;
180 break;
183 a->setEnabled( enable );
185 enable = false;
187 a = addAction( i18n( "&Restore All" ), this, SLOT( slotRestoreAll() ) );
188 itEnd = tasks.end();
189 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
191 if( (*it)->isIconified() || (*it)->isMaximized() ) {
192 enable = true;
193 break;
196 a->setEnabled( enable );
198 addSeparator();
200 enable = false;
202 addAction( KIcon( "list-remove" ), i18n( "&Close All" ), this, SLOT( slotCloseAll() ) );
205 QMenu* TaskRMBMenu::makeAdvancedMenu(TaskPtr t)
207 QAction *a;
208 QMenu* menu = new QMenu(this);
209 menu->setTitle(i18n("Ad&vanced"));
211 a = menu->addAction(KIcon("go-up"),
212 i18n("Keep &Above Others"),
213 t.data(), SLOT(toggleAlwaysOnTop()));
214 a->setCheckable(true);
215 a->setChecked(t->isAlwaysOnTop());
217 a = menu->addAction(KIcon("go-down"),
218 i18n("Keep &Below Others"),
219 t.data(), SLOT(toggleKeptBelowOthers()));
220 a->setCheckable(true);
221 a->setChecked(t->isKeptBelowOthers());
223 a = menu->addAction(KIcon("view-fullscreen"),
224 i18n("&Fullscreen"),
225 t.data(), SLOT(toggleFullScreen()));
226 a->setCheckable(true);
227 a->setChecked(t->isFullScreen());
229 if (KWindowSystem::allowedActionsSupported())
231 a->setEnabled(t->info().actionSupported(NET::ActionFullScreen));
234 return menu;
237 QMenu* TaskRMBMenu::makeDesktopsMenu(TaskPtr t)
239 QMenu* m = new QMenu( this );
240 m->setTitle( i18n("To &Desktop") );
242 QAction *a = m->addAction( i18n("&All Desktops"), this, SLOT( slotToDesktop() ) );
243 a->setCheckable(true);
244 toDesktopMap.append( QPair<TaskPtr, int>( t, 0 ) ); // 0 means all desktops
245 a->setData( 0 );
246 a->setChecked( t->isOnAllDesktops() );
248 m->addSeparator();
250 for (int i = 1; i <= TaskManager::self()->numberOfDesktops(); i++) {
251 QString name = QString("&%1 %2").arg(i).arg(TaskManager::self()->desktopName(i).replace('&', "&&"));
252 a = m->addAction( name, this, SLOT( slotToDesktop() ) );
253 a->setCheckable(true);
254 toDesktopMap.append( QPair<TaskPtr, int>( t, i ) );
255 a->setData( i );
256 a->setChecked( !t->isOnAllDesktops() && t->desktop() == i );
259 return m;
262 QMenu* TaskRMBMenu::makeDesktopsMenu()
264 QMenu* m = new QMenu( this );
265 QAction *a;
267 a = m->addAction( i18n("&All Desktops"), this, SLOT( slotAllToDesktop() ) );
268 a->setData( 0 ); // 0 means all desktops
270 m->addSeparator();
272 for (int i = 1; i <= TaskManager::self()->numberOfDesktops(); i++) {
273 QString name = QString("&%1 %2").arg(i).arg(TaskManager::self()->desktopName(i).replace('&', "&&"));
274 a = m->addAction( name, this, SLOT( slotAllToDesktop() ) );
275 a->setData( i );
278 return m;
281 void TaskRMBMenu::slotMinimizeAll()
283 TaskList::iterator itEnd = tasks.end();
284 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
286 (*it)->setIconified(true);
290 void TaskRMBMenu::slotMaximizeAll()
292 TaskList::iterator itEnd = tasks.end();
293 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
295 (*it)->setMaximized(true);
299 void TaskRMBMenu::slotRestoreAll()
301 TaskList::iterator itEnd = tasks.end();
302 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
304 (*it)->restore();
308 void TaskRMBMenu::slotShadeAll()
310 TaskList::iterator itEnd = tasks.end();
311 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
313 (*it)->setShaded( !(*it)->isShaded() );
317 void TaskRMBMenu::slotCloseAll()
319 TaskList::iterator itEnd = tasks.end();
320 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
322 (*it)->close();
326 void TaskRMBMenu::slotAllToDesktop()
328 QAction *action = qobject_cast<QAction *>( sender() );
329 if( action ) {
330 int desktop = action->data().toInt();
331 TaskList::iterator itEnd = tasks.end();
332 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
334 (*it)->toDesktop( desktop );
339 void TaskRMBMenu::slotAllToCurrentDesktop()
341 TaskList::iterator itEnd = tasks.end();
342 for (TaskList::iterator it = tasks.begin(); it != itEnd; ++it)
344 (*it)->toCurrentDesktop();
348 void TaskRMBMenu::slotToDesktop()
350 QAction *action = qobject_cast<QAction *>( sender() );
351 if( action ) {
352 QPair<TaskPtr, int> pair = toDesktopMap[action->data().toInt()];
353 pair.first->toDesktop( pair.second );
357 } // TaskManager namespace
360 #include "taskrmbmenu.moc"