useractions.cpp 60.3 KB
Newer Older
1
/********************************************************************
2
3
4
5
6
7
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>

8
9
10
11
12
13
14
15
16
17
18
19
20
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
21
22
23
24
25
26
27
28
29

/*

 This file contains things relevant to direct user actions, such as
 responses to global keyboard shortcuts, or selecting actions
 from the window operations menu.

*/

30
31
32
33
///////////////////////////////////////////////////////////////////////////////
// NOTE: if you change the menu, keep kde-workspace/libs/taskmanager/taskactions.cpp in sync
//////////////////////////////////////////////////////////////////////////////

34
#include "useractions.h"
35
36
37
38
#include "client.h"
#include "workspace.h"
#include "effects.h"

39
40
41
42
#ifdef KWIN_BUILD_SCRIPTING
#include "scripting/scripting.h"
#endif

43
#ifdef KWIN_BUILD_ACTIVITIES
Christophe Giboudeaux's avatar
Christophe Giboudeaux committed
44
#include <KActivities/Info>
45
#endif
46

47
48
49
#include <KDE/KProcess>
#include <KDE/KToolInvocation>

50
51
52
53
#include <X11/extensions/Xrandr.h>
#ifndef KWIN_NO_XF86VM
#include <X11/extensions/xf86vmode.h>
#endif
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <fixx11h.h>
#include <QPushButton>
#include <QSlider>

#include <kglobalsettings.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kconfig.h>
#include <kglobalaccel.h>
#include <kapplication.h>
#include <QRegExp>
#include <QMenu>
#include <QVBoxLayout>
#include <kauthorized.h>
#include <kactioncollection.h>
#include <kaction.h>

#include "killwindow.h"
72
#ifdef KWIN_BUILD_TABBOX
73
#include "tabbox.h"
74
#endif
75

Cedric Bellegarde's avatar
Cedric Bellegarde committed
76
77
78
79
80
81
#ifdef KWIN_BUILD_KAPPMENU
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusPendingCall>
#endif

82
83
84
namespace KWin
{

Cedric Bellegarde's avatar
Cedric Bellegarde committed
85
86
87
88
89
90
#ifdef KWIN_BUILD_KAPPMENU
static const char *KDED_SERVICE = "org.kde.kded";
static const char *KDED_APPMENU_PATH = "/modules/appmenu";
static const char *KDED_INTERFACE = "org.kde.kded";
#endif

91
92
93
94
95
96
97
98
UserActionsMenu::UserActionsMenu(QObject *parent)
    : QObject(parent)
    , m_menu(NULL)
    , m_desktopMenu(NULL)
    , m_screenMenu(NULL)
    , m_activityMenu(NULL)
    , m_addTabsMenu(NULL)
    , m_switchToTabMenu(NULL)
99
100
101
#ifdef KWIN_BUILD_SCRIPTING
    , m_scriptsMenu(NULL)
#endif
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    , m_resizeOperation(NULL)
    , m_moveOperation(NULL)
    , m_maximizeOperation(NULL)
    , m_shadeOperation(NULL)
    , m_keepAboveOperation(NULL)
    , m_keepBelowOperation(NULL)
    , m_fullScreenOperation(NULL)
    , m_noBorderOperation(NULL)
    , m_minimizeOperation(NULL)
    , m_closeOperation(NULL)
    , m_removeFromTabGroup(NULL)
    , m_closeTabGroup(NULL)
    , m_client(QWeakPointer<Client>())
{
}
117

118
UserActionsMenu::~UserActionsMenu()
119
{
120
121
    discard();
}
122

123
124
125
126
bool UserActionsMenu::isShown() const
{
    return m_menu && m_menu->isVisible();
}
127

128
129
bool UserActionsMenu::hasClient() const
{
130
    return !m_client.isNull() && isShown();
131
}
132

133
134
135
136
137
138
void UserActionsMenu::close()
{
    if (!m_menu) {
        return;
    }
    m_menu->close();
139
    m_client.clear();
140
}
141

142
143
144
145
146
147
148
bool UserActionsMenu::isMenuClient(const Client *c) const
{
    if (!c || m_client.isNull()) {
        return false;
    }
    return c == m_client.data();
}
149

150
151
152
153
154
155
void UserActionsMenu::show(const QRect &pos, const QWeakPointer<Client> &cl)
{
    if (!KAuthorized::authorizeKAction("kwin_rmb"))
        return;
    if (cl.isNull())
        return;
156
    if (isShown())   // recursion
157
158
159
160
        return;
    if (cl.data()->isDesktop()
            || cl.data()->isDock())
        return;
161

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    m_client = cl;
    init();
    Workspace *ws = Workspace::self();
    int x = pos.left();
    int y = pos.bottom();
    if (y == pos.top())
        m_menu->exec(QPoint(x, y));
    else {
        QRect area = ws->clientArea(ScreenArea, QPoint(x, y), ws->currentDesktop());
        menuAboutToShow(); // needed for sizeHint() to be correct :-/
        int popupHeight = m_menu->sizeHint().height();
        if (y + popupHeight < area.height())
            m_menu->exec(QPoint(x, y));
        else
            m_menu->exec(QPoint(x, pos.top() - popupHeight));
    }
}
179

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
void UserActionsMenu::helperDialog(const QString& message, const QWeakPointer<Client> &c)
{
    QStringList args;
    QString type;
    KActionCollection *keys = Workspace::self()->actionCollection();
    if (message == "noborderaltf3") {
        KAction* action = qobject_cast<KAction*>(keys->action("Window Operations Menu"));
        assert(action != NULL);
        QString shortcut = QString("%1 (%2)").arg(action->text())
                           .arg(action->globalShortcut().primary().toString(QKeySequence::NativeText));
        args << "--msgbox" << i18n(
                 "You have selected to show a window without its border.\n"
                 "Without the border, you will not be able to enable the border "
                 "again using the mouse: use the window operations menu instead, "
                 "activated using the %1 keyboard shortcut.",
                 shortcut);
        type = "altf3warning";
    } else if (message == "fullscreenaltf3") {
        KAction* action = qobject_cast<KAction*>(keys->action("Window Operations Menu"));
        assert(action != NULL);
        QString shortcut = QString("%1 (%2)").arg(action->text())
                           .arg(action->globalShortcut().primary().toString(QKeySequence::NativeText));
        args << "--msgbox" << i18n(
                 "You have selected to show a window in fullscreen mode.\n"
                 "If the application itself does not have an option to turn the fullscreen "
                 "mode off you will not be able to disable it "
                 "again using the mouse: use the window operations menu instead, "
                 "activated using the %1 keyboard shortcut.",
                 shortcut);
        type = "altf3warning";
    } else
        abort();
    if (!type.isEmpty()) {
        KConfig cfg("kwin_dialogsrc");
        KConfigGroup cg(&cfg, "Notification Messages");  // Depends on KMessageBox
        if (!cg.readEntry(type, true))
            return;
        args << "--dontagain" << "kwin_dialogsrc:" + type;
    }
    if (!c.isNull())
        args << "--embed" << QString::number(c.data()->window());
    KProcess::startDetached("kdialog", args);
}
223

224

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
QStringList configModules(bool controlCenter)
{
    QStringList args;
    args <<  "kwindecoration";
    if (controlCenter)
        args << "kwinoptions";
    else if (KAuthorized::authorizeControlModule("kde-kwinoptions.desktop"))
        args << "kwinactions" << "kwinfocus" <<  "kwinmoving" << "kwinadvanced"
             << "kwinrules" << "kwincompositing"
#ifdef KWIN_BUILD_TABBOX
             << "kwintabbox"
#endif
#ifdef KWIN_BUILD_SCREENEDGES
             << "kwinscreenedges"
#endif
#ifdef KWIN_BUILD_SCRIPTING
             << "kwinscripts"
#endif
             ;
    return args;
}
246

247
248
249
250
251
252
void UserActionsMenu::configureWM()
{
    QStringList args;
    args << "--icon" << "preferences-system-windows" << configModules(false);
    KToolInvocation::kdeinitExec("kcmshell4", args);
}
253

254
255
256
257
258
259
260
261
void UserActionsMenu::init()
{
    if (m_menu) {
        return;
    }
    m_menu = new QMenu;
    m_menu->setFont(KGlobalSettings::menuFont());
    connect(m_menu, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
262
    connect(m_menu, SIGNAL(triggered(QAction*)), this, SLOT(slotWindowOperation(QAction*)), Qt::QueuedConnection);
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

    QMenu *advancedMenu = new QMenu(m_menu);
    advancedMenu->setFont(KGlobalSettings::menuFont());

    m_moveOperation = advancedMenu->addAction(i18n("&Move"));
    m_moveOperation->setIcon(KIcon("transform-move"));
    KActionCollection *keys = Workspace::self()->actionCollection();
    KAction *kaction = qobject_cast<KAction*>(keys->action("Window Move"));
    if (kaction != 0)
        m_moveOperation->setShortcut(kaction->globalShortcut().primary());
    m_moveOperation->setData(Options::UnrestrictedMoveOp);

    m_resizeOperation = advancedMenu->addAction(i18n("Re&size"));
    kaction = qobject_cast<KAction*>(keys->action("Window Resize"));
    if (kaction != 0)
        m_resizeOperation->setShortcut(kaction->globalShortcut().primary());
    m_resizeOperation->setData(Options::ResizeOp);

    m_keepAboveOperation = advancedMenu->addAction(i18n("Keep &Above Others"));
    m_keepAboveOperation->setIcon(KIcon("go-up"));
    kaction = qobject_cast<KAction*>(keys->action("Window Above Other Windows"));
    if (kaction != 0)
        m_keepAboveOperation->setShortcut(kaction->globalShortcut().primary());
    m_keepAboveOperation->setCheckable(true);
    m_keepAboveOperation->setData(Options::KeepAboveOp);

    m_keepBelowOperation = advancedMenu->addAction(i18n("Keep &Below Others"));
    m_keepBelowOperation->setIcon(KIcon("go-down"));
    kaction = qobject_cast<KAction*>(keys->action("Window Below Other Windows"));
    if (kaction != 0)
        m_keepBelowOperation->setShortcut(kaction->globalShortcut().primary());
    m_keepBelowOperation->setCheckable(true);
    m_keepBelowOperation->setData(Options::KeepBelowOp);

    m_fullScreenOperation = advancedMenu->addAction(i18n("&Fullscreen"));
    m_fullScreenOperation->setIcon(KIcon("view-fullscreen"));
    kaction = qobject_cast<KAction*>(keys->action("Window Fullscreen"));
    if (kaction != 0)
        m_fullScreenOperation->setShortcut(kaction->globalShortcut().primary());
    m_fullScreenOperation->setCheckable(true);
    m_fullScreenOperation->setData(Options::FullScreenOp);

    m_shadeOperation = advancedMenu->addAction(i18n("Sh&ade"));
    kaction = qobject_cast<KAction*>(keys->action("Window Shade"));
    if (kaction != 0)
        m_shadeOperation->setShortcut(kaction->globalShortcut().primary());
    m_shadeOperation->setCheckable(true);
    m_shadeOperation->setData(Options::ShadeOp);

    m_noBorderOperation = advancedMenu->addAction(i18n("&No Border"));
    kaction = qobject_cast<KAction*>(keys->action("Window No Border"));
    if (kaction != 0)
        m_noBorderOperation->setShortcut(kaction->globalShortcut().primary());
    m_noBorderOperation->setCheckable(true);
    m_noBorderOperation->setData(Options::NoBorderOp);

    advancedMenu->addSeparator();

    QAction *action = advancedMenu->addAction(i18n("Window &Shortcut..."));
    action->setIcon(KIcon("configure-shortcuts"));
    kaction = qobject_cast<KAction*>(keys->action("Setup Window Shortcut"));
    if (kaction != 0)
        action->setShortcut(kaction->globalShortcut().primary());
    action->setData(Options::SetupWindowShortcutOp);

    action = advancedMenu->addAction(i18n("&Special Window Settings..."));
    action->setIcon(KIcon("preferences-system-windows-actions"));
    action->setData(Options::WindowRulesOp);

    action = advancedMenu->addAction(i18n("S&pecial Application Settings..."));
    action->setIcon(KIcon("preferences-system-windows-actions"));
    action->setData(Options::ApplicationRulesOp);
    if (!KGlobal::config()->isImmutable() &&
            !KAuthorized::authorizeControlModules(configModules(true)).isEmpty()) {
        advancedMenu->addSeparator();
        action = advancedMenu->addAction(i18nc("Entry in context menu of window decoration to open the configuration module of KWin",
                                        "Window &Manager Settings..."));
        action->setIcon(KIcon("configure"));
        connect(action, SIGNAL(triggered()), this, SLOT(configureWM()));
    }
343

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    m_minimizeOperation = m_menu->addAction(i18n("Mi&nimize"));
    kaction = qobject_cast<KAction*>(keys->action("Window Minimize"));
    if (kaction != 0)
        m_minimizeOperation->setShortcut(kaction->globalShortcut().primary());
    m_minimizeOperation->setData(Options::MinimizeOp);

    m_maximizeOperation = m_menu->addAction(i18n("Ma&ximize"));
    kaction = qobject_cast<KAction*>(keys->action("Window Maximize"));
    if (kaction != 0)
        m_maximizeOperation->setShortcut(kaction->globalShortcut().primary());
    m_maximizeOperation->setCheckable(true);
    m_maximizeOperation->setData(Options::MaximizeOp);

    m_menu->addSeparator();

    // Actions for window tabbing
    if (Workspace::self()->decorationSupportsTabbing()) {
        m_removeFromTabGroup = m_menu->addAction(i18n("&Untab"));
        kaction = qobject_cast<KAction*>(keys->action("Untab"));
363
        if (kaction != 0)
364
365
            m_removeFromTabGroup->setShortcut(kaction->globalShortcut().primary());
        m_removeFromTabGroup->setData(Options::RemoveTabFromGroupOp);
366

367
368
369
370
371
372
        m_closeTabGroup = m_menu->addAction(i18n("Close Entire &Group"));
        m_closeTabGroup->setIcon(KIcon("window-close"));
        kaction = qobject_cast<KAction*>(keys->action("Close TabGroup"));
        if (kaction != 0)
            m_closeTabGroup->setShortcut(kaction->globalShortcut().primary());
        m_closeTabGroup->setData(Options::CloseTabGroupOp);
373

374
375
        m_menu->addSeparator();
    }
376

377
    m_menu->addSeparator();
378

379
380
    action = m_menu->addMenu(advancedMenu);
    action->setText(i18n("More Actions"));
381

382
    m_menu->addSeparator();
383

384
385
386
387
388
389
    m_closeOperation = m_menu->addAction(i18n("&Close"));
    m_closeOperation->setIcon(KIcon("window-close"));
    kaction = qobject_cast<KAction*>(keys->action("Window Close"));
    if (kaction != 0)
        m_closeOperation->setShortcut(kaction->globalShortcut().primary());
    m_closeOperation->setData(Options::CloseOp);
390
391
}

392
void UserActionsMenu::discard()
393
{
394
395
396
397
398
399
400
    delete m_menu;
    m_menu = NULL;
    m_desktopMenu = NULL;
    m_screenMenu = NULL;
    m_activityMenu = NULL;
    m_switchToTabMenu = NULL;
    m_addTabsMenu = NULL;
401
402
403
#ifdef KWIN_BUILD_SCRIPTING
    m_scriptsMenu = NULL;
#endif
404
405
}

406
void UserActionsMenu::menuAboutToShow()
407
{
408
    if (m_client.isNull() || !m_menu)
409
        return;
410
    Workspace *ws = Workspace::self();
411

412
413
414
    if (ws->numberOfDesktops() == 1) {
        delete m_desktopMenu;
        m_desktopMenu = 0;
415
    } else {
416
        initDesktopPopup();
417
    }
418
419
420
    if (ws->numScreens() == 1 || (!m_client.data()->isMovable() && !m_client.data()->isMovableAcrossScreens())) {
        delete m_screenMenu;
        m_screenMenu = NULL;
421
422
423
    } else {
        initScreenPopup();
    }
424
#ifdef KWIN_BUILD_ACTIVITIES
425
    ws->updateActivityList(true, false, this, "showHideActivityMenu");
426
#endif
427

428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
    m_resizeOperation->setEnabled(m_client.data()->isResizable());
    m_moveOperation->setEnabled(m_client.data()->isMovableAcrossScreens());
    m_maximizeOperation->setEnabled(m_client.data()->isMaximizable());
    m_maximizeOperation->setChecked(m_client.data()->maximizeMode() == Client::MaximizeFull);
    m_shadeOperation->setEnabled(m_client.data()->isShadeable());
    m_shadeOperation->setChecked(m_client.data()->shadeMode() != ShadeNone);
    m_keepAboveOperation->setChecked(m_client.data()->keepAbove());
    m_keepBelowOperation->setChecked(m_client.data()->keepBelow());
    m_fullScreenOperation->setEnabled(m_client.data()->userCanSetFullScreen());
    m_fullScreenOperation->setChecked(m_client.data()->isFullScreen());
    m_noBorderOperation->setEnabled(m_client.data()->userCanSetNoBorder());
    m_noBorderOperation->setChecked(m_client.data()->noBorder());
    m_minimizeOperation->setEnabled(m_client.data()->isMinimizable());
    m_closeOperation->setEnabled(m_client.data()->isCloseable());

    if (ws->decorationSupportsTabbing()) {
Thomas Lübking's avatar
Thomas Lübking committed
444
445
        initTabbingPopups();
    } else {
446
447
        delete m_addTabsMenu;
        m_addTabsMenu = 0;
448
    }
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469

#ifdef KWIN_BUILD_SCRIPTING
    // drop the existing scripts menu
    delete m_scriptsMenu;
    m_scriptsMenu = NULL;
    // ask scripts whether they want to add entries for the given Client
    m_scriptsMenu = new QMenu(m_menu);
    QList<QAction*> scriptActions = ws->scripting()->actionsForUserActionMenu(m_client.data(), m_scriptsMenu);
    if (!scriptActions.isEmpty()) {
        m_scriptsMenu->setFont(KGlobalSettings::menuFont());
        m_scriptsMenu->addActions(scriptActions);

        QAction *action = m_scriptsMenu->menuAction();
        // set it as the first item after desktop
        m_menu->insertAction(m_closeOperation, action);
        action->setText(i18n("&Extensions"));
    } else {
        delete m_scriptsMenu;
        m_scriptsMenu = NULL;
    }
#endif
470
}
471

472
void UserActionsMenu::showHideActivityMenu()
473
474
{
#ifdef KWIN_BUILD_ACTIVITIES
475
    const QStringList &openActivities_ = Workspace::self()->openActivities();
476
477
    kDebug() << "activities:" << openActivities_.size();
    if (openActivities_.size() < 2) {
478
479
        delete m_activityMenu;
        m_activityMenu = 0;
480
481
482
483
484
485
    } else {
        initActivityPopup();
    }
#endif
}

486
void UserActionsMenu::selectPopupClientTab(QAction* action)
487
{
488
    if (!(!m_client.isNull() && m_client.data()->tabGroup()) || !action->data().isValid())
Thomas Lübking's avatar
Thomas Lübking committed
489
490
491
        return;

    if (Client *other = action->data().value<Client*>()) {
492
        m_client.data()->tabGroup()->setCurrent(other);
493
494
        return;
    }
Thomas Lübking's avatar
Thomas Lübking committed
495
496
497
498

    // failed conversion, try "1" & "2", being prev and next
    int direction = action->data().toInt();
    if (direction == 1)
499
        m_client.data()->tabGroup()->activatePrev();
Thomas Lübking's avatar
Thomas Lübking committed
500
    else if (direction == 2)
501
        m_client.data()->tabGroup()->activateNext();
502
}
503

Thomas Lübking's avatar
Thomas Lübking committed
504
static QString shortCaption(const QString &s)
505
{
Thomas Lübking's avatar
Thomas Lübking committed
506
507
508
509
510
511
    if (s.length() < 64)
        return s;
    QString ss = s;
    return ss.replace(32,s.length()-64,"...");
}

512
void UserActionsMenu::rebuildTabListPopup()
Thomas Lübking's avatar
Thomas Lübking committed
513
{
514
    Q_ASSERT(m_switchToTabMenu);
Thomas Lübking's avatar
Thomas Lübking committed
515

516
    m_switchToTabMenu->clear();
Thomas Lübking's avatar
Thomas Lübking committed
517
    // whatever happens "0x1" and "0x2" are no heap positions ;-)
518
519
    m_switchToTabMenu->addAction(i18nc("Switch to tab -> Previous", "Previous"))->setData(1);
    m_switchToTabMenu->addAction(i18nc("Switch to tab -> Next", "Next"))->setData(2);
Thomas Lübking's avatar
Thomas Lübking committed
520

521
    m_switchToTabMenu->addSeparator();
Thomas Lübking's avatar
Thomas Lübking committed
522

523
524
525
    for (QList<Client*>::const_iterator i = m_client.data()->tabGroup()->clients().constBegin(),
                                        end = m_client.data()->tabGroup()->clients().constEnd(); i != end; ++i) {
        if ((*i)->noBorder() || *i == m_client.data()->tabGroup()->current())
Thomas Lübking's avatar
Thomas Lübking committed
526
            continue; // cannot tab there anyway
527
        m_switchToTabMenu->addAction(shortCaption((*i)->caption()))->setData(QVariant::fromValue(*i));
528
529
    }

530
531
}

532
void UserActionsMenu::entabPopupClient(QAction* action)
533
{
534
    if (m_client.isNull() || !action->data().isValid())
Thomas Lübking's avatar
Thomas Lübking committed
535
        return;
Thomas Lübking's avatar
Thomas Lübking committed
536
    Client *other = action->data().value<Client*>();
537
    if (!Workspace::self()->clientList().contains(other)) // might have been lost betwenn pop-up and selection
Thomas Lübking's avatar
Thomas Lübking committed
538
        return;
539
    m_client.data()->tabBehind(other, true);
Thomas Lübking's avatar
Thomas Lübking committed
540
    if (options->focusPolicyIsReasonable())
541
        Workspace::self()->requestFocus(m_client.data());
542
}
543

544
void UserActionsMenu::rebuildTabGroupPopup()
545
{
546
    Q_ASSERT(m_addTabsMenu);
Thomas Lübking's avatar
Thomas Lübking committed
547

548
    m_addTabsMenu->clear();
Thomas Lübking's avatar
Thomas Lübking committed
549
    QList<Client*> handled;
550
551
552
    const ClientList &clientList = Workspace::self()->clientList();
    for (QList<Client*>::const_iterator i = clientList.constBegin(), end = clientList.constEnd(); i != end; ++i) {
        if (*i == m_client.data() || (*i)->noBorder())
Thomas Lübking's avatar
Thomas Lübking committed
553
            continue;
554
        m_addTabsMenu->addAction(shortCaption((*i)->caption()))->setData(QVariant::fromValue(*i));
Thomas Lübking's avatar
Thomas Lübking committed
555
    }
556
}
557

558
void UserActionsMenu::initTabbingPopups()
559
{
Thomas Lübking's avatar
Thomas Lübking committed
560
    bool needTabManagers = false;
561
    if (m_client.data()->tabGroup() && m_client.data()->tabGroup()->count() > 1) {
Thomas Lübking's avatar
Thomas Lübking committed
562
        needTabManagers = true;
563
564
565
566
567
568
        if (!m_switchToTabMenu) {
            m_switchToTabMenu = new QMenu(i18n("Switch to Tab"), m_menu);
            m_switchToTabMenu->setFont(KGlobalSettings::menuFont());
            connect(m_switchToTabMenu, SIGNAL(triggered(QAction*)), SLOT(selectPopupClientTab(QAction*)));
            connect(m_switchToTabMenu, SIGNAL(aboutToShow()), SLOT(rebuildTabListPopup()));
            m_menu->insertMenu(m_removeFromTabGroup, m_switchToTabMenu);
569
        }
Thomas Lübking's avatar
Thomas Lübking committed
570
    } else {
571
572
        delete m_switchToTabMenu;
        m_switchToTabMenu = 0;
Thomas Lübking's avatar
Thomas Lübking committed
573
574
    }

575
576
577
578
579
580
    if (!m_addTabsMenu) {
        m_addTabsMenu = new QMenu(i18n("Attach as tab to"), m_menu);
        m_addTabsMenu->setFont(KGlobalSettings::menuFont());
        connect(m_addTabsMenu, SIGNAL(triggered(QAction*)), SLOT(entabPopupClient(QAction*)));
        connect(m_addTabsMenu, SIGNAL(aboutToShow()), SLOT(rebuildTabGroupPopup()));
        m_menu->insertMenu(m_removeFromTabGroup, m_addTabsMenu);
581
    }
Thomas Lübking's avatar
Thomas Lübking committed
582

583
584
    m_removeFromTabGroup->setVisible(needTabManagers);
    m_closeTabGroup->setVisible(needTabManagers);
585
}
586

587
void UserActionsMenu::initDesktopPopup()
588
{
589
    if (m_desktopMenu)
590
591
        return;

592
593
594
595
    m_desktopMenu = new QMenu(m_menu);
    m_desktopMenu->setFont(KGlobalSettings::menuFont());
    connect(m_desktopMenu, SIGNAL(triggered(QAction*)), SLOT(slotSendToDesktop(QAction*)));
    connect(m_desktopMenu, SIGNAL(aboutToShow()), SLOT(desktopPopupAboutToShow()));
596

597
    QAction *action = m_desktopMenu->menuAction();
598
    // set it as the first item
599
    m_menu->insertAction(m_minimizeOperation, action);
600
    action->setText(i18n("Move To &Desktop"));
601
}
602

603
void UserActionsMenu::initScreenPopup()
604
{
605
    if (m_screenMenu) {
606
607
608
        return;
    }

609
610
611
612
    m_screenMenu = new QMenu(m_menu);
    m_screenMenu->setFont(KGlobalSettings::menuFont());
    connect(m_screenMenu, SIGNAL(triggered(QAction*)), SLOT(slotSendToScreen(QAction*)));
    connect(m_screenMenu, SIGNAL(aboutToShow()), SLOT(screenPopupAboutToShow()));
613

614
    QAction *action = m_screenMenu->menuAction();
615
    // set it as the first item after desktop
616
    m_menu->insertAction(m_activityMenu ? m_activityMenu->menuAction() : m_minimizeOperation, action);
617
618
619
    action->setText(i18n("Move To &Screen"));
}

620
void UserActionsMenu::initActivityPopup()
621
{
622
    if (m_activityMenu)
623
624
        return;

625
626
627
    m_activityMenu = new QMenu(m_menu);
    m_activityMenu->setFont(KGlobalSettings::menuFont());
    connect(m_activityMenu, SIGNAL(triggered(QAction*)),
628
            this, SLOT(slotToggleOnActivity(QAction*)));
629
    connect(m_activityMenu, SIGNAL(aboutToShow()),
630
            this, SLOT(activityPopupAboutToShow()));
631

632
    QAction *action = m_activityMenu->menuAction();
633
    // set it as the first item
634
    m_menu->insertAction(m_minimizeOperation, action);
635
636
    action->setText(i18n("Ac&tivities"));   //FIXME is that a good string?
}
637

638
void UserActionsMenu::desktopPopupAboutToShow()
639
{
640
    if (!m_desktopMenu)
641
        return;
642
    const Workspace *ws = Workspace::self();
643

644
645
646
    m_desktopMenu->clear();
    QActionGroup *group = new QActionGroup(m_desktopMenu);
    QAction *action = m_desktopMenu->addAction(i18n("&All Desktops"));
647
648
    action->setData(0);
    action->setCheckable(true);
649
    group->addAction(action);
650

651
    if (!m_client.isNull() && m_client.data()->isOnAllDesktops())
652
        action->setChecked(true);
653
    m_desktopMenu->addSeparator();
654
655

    const int BASE = 10;
656
    for (int i = 1; i <= ws->numberOfDesktops(); ++i) {
657
        QString basic_name("%1  %2");
658
        if (i < BASE) {
659
660
            basic_name.prepend('&');
        }
661
        action = m_desktopMenu->addAction(basic_name.arg(i).arg(ws->desktopName(i).replace('&', "&&")));
662
663
        action->setData(i);
        action->setCheckable(true);
664
        group->addAction(action);
665

666
667
        if (!m_client.isNull() &&
                !m_client.data()->isOnAllDesktops() && m_client.data()->desktop()  == i)
668
            action->setChecked(true);
669
    }
670

671
672
673
    m_desktopMenu->addSeparator();
    action = m_desktopMenu->addAction(i18nc("Create a new desktop and move there the window", "&New Desktop"));
    action->setData(ws->numberOfDesktops() + 1);
674

675
    if (ws->numberOfDesktops() >= Workspace::self()->maxNumberOfDesktops())
676
        action->setEnabled(false);
677
}
678

679
void UserActionsMenu::screenPopupAboutToShow()
680
{
681
    if (!m_screenMenu) {
682
683
684
        return;
    }

685
686
    m_screenMenu->clear();
    QActionGroup *group = new QActionGroup(m_screenMenu);
687

688
    for (int i = 0; i<Workspace::self()->numScreens(); ++i) {
689
690
        // TODO: retrieve the screen name?
        // assumption: there are not more than 9 screens attached.
691
        QAction *action = m_screenMenu->addAction(i18nc("@item:inmenu List of all Screens to send a window to",
692
693
694
                                                        "Screen &%1", (i+1)));
        action->setData(i);
        action->setCheckable(true);
695
        if (!m_client.isNull() && i == m_client.data()->screen()) {
696
697
698
699
700
701
            action->setChecked(true);
        }
        group->addAction(action);
    }
}

702
void UserActionsMenu::activityPopupAboutToShow()
703
{
704
    if (!m_activityMenu)
705
706
        return;

707
#ifdef KWIN_BUILD_ACTIVITIES
708
709
    m_activityMenu->clear();
    QAction *action = m_activityMenu->addAction(i18n("&All Activities"));
710
711
    action->setData(QString());
    action->setCheckable(true);
712

713
    if (!m_client.isNull() && m_client.data()->isOnAllActivities())
714
        action->setChecked(true);
715
    m_activityMenu->addSeparator();
716

717
    foreach (const QString &id, Workspace::self()->openActivities()) {
Aaron J. Seigo's avatar
Aaron J. Seigo committed
718
        KActivities::Info activity(id);
719
        QString name = activity.name();
720
        name.replace('&', "&&");
721
        action = m_activityMenu->addAction(KIcon(activity.icon()), name);
722
723
        action->setData(id);
        action->setCheckable(true);
724

725
726
        if (!m_client.isNull() &&
                !m_client.data()->isOnAllActivities() && m_client.data()->isOnActivity(id))
727
            action->setChecked(true);
728
    }
729
#endif
730
}
731

732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
void UserActionsMenu::slotWindowOperation(QAction *action)
{
    if (!action->data().isValid())
        return;

    Options::WindowOperation op = static_cast< Options::WindowOperation >(action->data().toInt());
    QWeakPointer<Client> c = (!m_client.isNull()) ? m_client : QWeakPointer<Client>(Workspace::self()->activeClient());
    if (c.isNull())
        return;
    QString type;
    switch(op) {
    case Options::FullScreenOp:
        if (!c.data()->isFullScreen() && c.data()->userCanSetFullScreen())
            type = "fullscreenaltf3";
        break;
    case Options::NoBorderOp:
        if (!c.data()->noBorder() && c.data()->userCanSetNoBorder())
            type = "noborderaltf3";
        break;
    default:
        break;
    };
    if (!type.isEmpty())
        helperDialog(type, c);
    Workspace::self()->performWindowOperation(c.data(), op);
}

void UserActionsMenu::slotSendToDesktop(QAction *action)
{
    int desk = action->data().toInt();
    if (m_client.isNull())
        return;
    Workspace *ws = Workspace::self();
    if (desk == 0) {
        // the 'on_all_desktops' menu entry
        m_client.data()->setOnAllDesktops(!m_client.data()->isOnAllDesktops());
        return;
    } else if (desk > ws->numberOfDesktops()) {
        ws->setNumberOfDesktops(desk);
    }

    ws->sendClientToDesktop(m_client.data(), desk, false);
}

void UserActionsMenu::slotSendToScreen(QAction *action)
{
    const int screen = action->data().toInt();
    if (m_client.isNull()) {
        return;
    }
    Workspace *ws = Workspace::self();
    if (screen >= ws->numScreens()) {
        return;
    }

    ws->sendClientToScreen(m_client.data(), screen);
}

void UserActionsMenu::slotToggleOnActivity(QAction *action)
{
    QString activity = action->data().toString();
    if (m_client.isNull())
        return;
    if (activity.isEmpty()) {
        // the 'on_all_activities' menu entry
        m_client.data()->setOnAllActivities(!m_client.data()->isOnAllActivities());
        return;
    }

    Workspace::self()->toggleClientOnActivity(m_client.data(), activity, false);
}

//****************************************
// Workspace
//****************************************

void Workspace::slotIncreaseWindowOpacity()
{
    if (!active_client) {
        return;
    }
    active_client->setOpacity(qMin(active_client->opacity() + 0.05, 1.0));
}

void Workspace::slotLowerWindowOpacity()
{
    if (!active_client) {
        return;
    }
    active_client->setOpacity(qMax(active_client->opacity() - 0.05, 0.05));
}

824
void Workspace::closeActivePopup()
825
826
{
    if (active_popup) {
827
828
829
830
        active_popup->close();
        active_popup = NULL;
        active_popup_client = NULL;
    }
831
    m_userActionsMenu->close();
832
}
833
834
835
836
837

/*!
  Create the global accel object \c keys.
 */
void Workspace::initShortcuts()
838
839
{
    keys = new KActionCollection(this);
840
841
842
843
844
    KActionCollection* actionCollection = keys;
    QAction* a = 0L;

    // a separate KActionCollection is needed for the shortcut for disabling global shortcuts,
    // otherwise it would also disable itself
845
    disable_shortcuts_keys = new KActionCollection(this);
Lucas Murray's avatar
Lucas Murray committed
846
    // TODO: PORT ME (KGlobalAccel related)
847
848
849
850
    // FIXME KAccel port... needed?
    //disable_shortcuts_keys->disableBlocking( true );
#define IN_KWIN
#include "kwinbindings.cpp"
851
#ifdef KWIN_BUILD_TABBOX
Arthur Arlt's avatar
Arthur Arlt committed
852
853
    if (tab_box) {
        tab_box->initShortcuts(actionCollection);
854
    }
855
#endif
856
    m_userActionsMenu->discard(); // so that it's recreated next time
857
}
858

859
860
861
void Workspace::setupWindowShortcut(Client* c)
{
    assert(client_keys_dialog == NULL);
Lucas Murray's avatar
Lucas Murray committed
862
863
864
865
    // TODO: PORT ME (KGlobalAccel related)
    //keys->setEnabled( false );
    //disable_shortcuts_keys->setEnabled( false );
    //client_keys->setEnabled( false );
866
    client_keys_dialog = new ShortcutDialog(c->shortcut().primary());
867
    client_keys_client = c;
868
869
    connect(client_keys_dialog, SIGNAL(dialogDone(bool)), SLOT(setupWindowShortcutDone(bool)));
    QRect r = clientArea(ScreenArea, c);
870
871
    QSize size = client_keys_dialog->sizeHint();
    QPoint pos = c->pos() + c->clientPos();
872
873
874
875
876
    if (pos.x() + size.width() >= r.right())
        pos.setX(r.right() - size.width());
    if (pos.y() + size.height() >= r.bottom())
        pos.setY(r.bottom() - size.height());
    client_keys_dialog->move(pos);
877
878
879
    client_keys_dialog->show();
    active_popup = client_keys_dialog;
    active_popup_client = c;
880
}
881

882
883
void Workspace::setupWindowShortcutDone(bool ok)
{
884
885
886
//    keys->setEnabled( true );
//    disable_shortcuts_keys->setEnabled( true );
//    client_keys->setEnabled( true );
887
888
    if (ok)
        client_keys_client->setShortcut(KShortcut(client_keys_dialog->shortcut()).toString());
889
    closeActivePopup();
890
    client_keys_dialog->deleteLater();
891
892
    client_keys_dialog = NULL;
    client_keys_client = NULL;
893
}
894

895
896
897
898
899
900
901
902
903
904
void Workspace::clientShortcutUpdated(Client* c)
{
    QString key = QString("_k_session:%1").arg(c->window());
    QAction* action = client_keys->action(key.toLatin1().constData());
    if (!c->shortcut().isEmpty()) {
        if (action == NULL) { // new shortcut
            action = client_keys->addAction(QString(key));
            action->setText(i18n("Activate Window (%1)", c->caption()));
            connect(action, SIGNAL(triggered(bool)), c, SLOT(shortcutActivated()));
        }
Michael Jansen's avatar
Michael Jansen committed
905

906
        KAction *kaction = qobject_cast<KAction*>(action);
907
908
        // no autoloading, since it's configured explicitly here and is not meant to be reused
        // (the key is the window id anyway, which is kind of random)
Michael Jansen's avatar
Michael Jansen committed
909
        kaction->setGlobalShortcut(
910
911
912
913
914
            c->shortcut(), KAction::ActiveShortcut, KAction::NoAutoloading);
        kaction->setEnabled(true);
    } else {
        KAction *kaction = qobject_cast<KAction*>(action);
        if (kaction) {
Michael Jansen's avatar
Michael Jansen committed
915
            kaction->forgetGlobalShortcut();
916
        }
917
        delete action;
918
    }
919
}
920

921
922
923
void Workspace::performWindowOperation(Client* c, Options::WindowOperation op)
{
    if (!c)
924
        return;
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
    if (op == Options::MoveOp || op == Options::UnrestrictedMoveOp)
        QCursor::setPos(c->geometry().center());
    if (op == Options::ResizeOp || op == Options::UnrestrictedResizeOp)
        QCursor::setPos(c->geometry().bottomRight());
    switch(op) {
    case Options::MoveOp:
        c->performMouseCommand(Options::MouseMove, cursorPos());
        break;
    case Options::UnrestrictedMoveOp:
        c->performMouseCommand(Options::MouseUnrestrictedMove, cursorPos());
        break;
    case Options::ResizeOp:
        c->performMouseCommand(Options::MouseResize, cursorPos());
        break;
    case Options::UnrestrictedResizeOp:
        c->performMouseCommand(Options::MouseUnrestrictedResize, cursorPos());
        break;
    case Options::CloseOp:
943
        QMetaObject::invokeMethod(c, "closeWindow", Qt::QueuedConnection);
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
        break;
    case Options::MaximizeOp:
        c->maximize(c->maximizeMode() == Client::MaximizeFull
                    ? Client::MaximizeRestore : Client::MaximizeFull);
        break;
    case Options::HMaximizeOp:
        c->maximize(c->maximizeMode() ^ Client::MaximizeHorizontal);
        break;
    case Options::VMaximizeOp:
        c->maximize(c->maximizeMode() ^ Client::MaximizeVertical);
        break;
    case Options::RestoreOp:
        c->maximize(Client::MaximizeRestore);
        break;
    case Options::MinimizeOp:
        c->minimize();
        break;
    case Options::ShadeOp:
        c->performMouseCommand(Options::MouseShade, cursorPos());
        break;
    case Options::OnAllDesktopsOp:
        c->setOnAllDesktops(!c->isOnAllDesktops());
        break;
    case Options::FullScreenOp:
        c->setFullScreen(!c->isFullScreen(), true);
        break;
    case Options::NoBorderOp:
        c->setNoBorder(!c->noBorder());
        break;
    case Options::KeepAboveOp: {
        StackingUpdatesBlocker blocker(this);
        bool was = c->keepAbove();
        c->setKeepAbove(!c->keepAbove());
        if (was && !c->keepAbove())
            raiseClient(c);
        break;
    }
    case Options::KeepBelowOp: {
        StackingUpdatesBlocker blocker(this);
        bool was = c->keepBelow();
        c->setKeepBelow(!c->keepBelow());
        if (was && !c->keepBelow())
986
            lowerClient(c);
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
        break;
    }
    case Options::OperationsOp:
        c->performMouseCommand(Options::MouseShade, cursorPos());
        break;
    case Options::WindowRulesOp:
        editWindowRules(c, false);
        break;
    case Options::ApplicationRulesOp:
        editWindowRules(c, true);
        break;
    case Options::SetupWindowShortcutOp:
        setupWindowShortcut(c);
        break;
    case Options::LowerOp:
        lowerClient(c);
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1004
    case Options::TabDragOp: // Handled by decoration itself
1005
1006
    case Options::NoOp:
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1007
    case Options::RemoveTabFromGroupOp:
Thomas Lübking's avatar
Thomas Lübking committed
1008
        if (c->untab(c->geometry().translated(cascadeOffset(c))) && options->focusPolicyIsReasonable())
Thomas Lübking's avatar
Thomas Lübking committed
1009
             takeActivity(c, ActivityFocus | ActivityRaise, true);
1010
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1011
1012
1013
    case Options::ActivateNextTabOp:
        if (c->tabGroup())
            c->tabGroup()->activateNext();
1014
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1015
1016
1017
    case Options::ActivatePreviousTabOp:
        if (c->tabGroup())
            c->tabGroup()->activatePrev();
1018
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1019
1020
    case Options::CloseTabGroupOp:
        c->tabGroup()->closeAll();
1021
        break;
1022
    }
1023
}
1024

1025
1026
1027
1028
/**
 * Called by the decoration in the new API to determine what buttons the user has configured for
 * window tab dragging and the operations menu.
 */
1029
1030
Options::WindowOperation Client::mouseButtonToWindowOperation(Qt::MouseButtons button)
{
1031
1032
    Options::MouseCommand com = Options::MouseNothing;
    bool active = isActive();
1033
    if (!wantsInput())   // we cannot be active, use it anyway
1034
1035
        active = true;

1036
    if (button == Qt::LeftButton)
1037
        com = active ? options->commandActiveTitlebar1() : options->commandInactiveTitlebar1();
1038
    else if (button == Qt::MidButton)
1039
        com = active ? options->commandActiveTitlebar2() : options->commandInactiveTitlebar2();
1040
    else if (button == Qt::RightButton)
1041
1042
1043
        com = active ? options->commandActiveTitlebar3() : options->commandInactiveTitlebar3();

    // TODO: Complete the list
Thomas Lübking's avatar
Thomas Lübking committed
1044
1045
    if (com == Options::MouseDragTab)
        return Options::TabDragOp;
1046
    if (com == Options::MouseOperationsMenu)
1047
1048
        return Options::OperationsOp;
    return Options::NoOp;
1049
}
1050

1051
1052
1053
/*!
  Performs a mouse command on this client (see options.h)
 */
1054
1055
bool Client::performMouseCommand(Options::MouseCommand command, const QPoint &globalPos, bool handled)
{
1056
    bool replay = false;
1057
1058
1059
1060
    switch(command) {
    case Options::MouseRaise:
        workspace()->raiseClient(this);
        break;
1061
    case Options::MouseLower: {
1062
        workspace()->lowerClient(this);
1063
1064
        // used to be activateNextClient(this), then topClientOnDesktop
        // since this is a mouseOp it's however safe to use the client under the mouse instead
1065
        if (isActive() && options->focusPolicyIsReasonable()) {
1066
1067
1068
1069
            Client *next = workspace()->clientUnderMouse(screen());
            if (next && next != this)
                workspace()->requestFocus(next, false);
        }
1070
        break;
1071
    }
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
    case Options::MouseShade :
        toggleShade();
        cancelShadeHoverTimer();
        break;
    case Options::MouseSetShade:
        setShade(ShadeNormal);
        cancelShadeHoverTimer();
        break;
    case Options::MouseUnsetShade:
        setShade(ShadeNone);
        cancelShadeHoverTimer();
        break;
    case Options::MouseOperationsMenu:
1085
        if (isActive() && options->isClickRaise())
1086
1087
1088
1089
1090
1091
            autoRaise();
        workspace()->showWindowMenu(globalPos, this);
        break;
    case Options::MouseToggleRaiseAndLower:
        workspace()->raiseOrLowerClient(this);
        break;
1092
    case Options::MouseActivateAndRaise: {
1093
        replay = isActive(); // for clickraise mode
1094
1095
        bool mustReplay = !rules()->checkAcceptFocus(input);
        if (mustReplay) {
1096
            ToplevelList::const_iterator  it = workspace()->stackingOrder().constEnd(),
1097
1098
                                     begin = workspace()->stackingOrder().constBegin();
            while (mustReplay && --it != begin && *it != this) {
1099
1100
                Client *c = qobject_cast<Client*>(*it);
                if (!c || (c->keepAbove() && !keepAbove()) || (keepBelow() && !c->keepBelow()))
1101
                    continue; // can never raise above "it"
1102
                mustReplay = !(c->isOnCurrentDesktop() && c->isOnCurrentActivity() && c->geometry().intersects(geometry()));