useractions.cpp 58.9 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
#include "client.h"
#include "workspace.h"
#include "effects.h"
38
#include "virtualdesktops.h"
39

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

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

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

51
52
53
54
#include <X11/extensions/Xrandr.h>
#ifndef KWIN_NO_XF86VM
#include <X11/extensions/xf86vmode.h>
#endif
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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"
73
#ifdef KWIN_BUILD_TABBOX
74
#include "tabbox.h"
75
#endif
76

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

83
84
85
namespace KWin
{

Cedric Bellegarde's avatar
Cedric Bellegarde committed
86
87
88
89
90
91
#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

92
93
94
95
96
97
98
99
UserActionsMenu::UserActionsMenu(QObject *parent)
    : QObject(parent)
    , m_menu(NULL)
    , m_desktopMenu(NULL)
    , m_screenMenu(NULL)
    , m_activityMenu(NULL)
    , m_addTabsMenu(NULL)
    , m_switchToTabMenu(NULL)
100
101
102
#ifdef KWIN_BUILD_SCRIPTING
    , m_scriptsMenu(NULL)
#endif
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    , 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>())
{
}
118

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

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

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

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

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

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

163
164
165
166
167
168
169
170
    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 {
171
        QRect area = ws->clientArea(ScreenArea, QPoint(x, y), VirtualDesktopManager::self()->current());
172
173
174
175
176
177
178
179
        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));
    }
}
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
223
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);
}
224

225

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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;
}
247

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

255
256
257
258
259
260
261
262
void UserActionsMenu::init()
{
    if (m_menu) {
        return;
    }
    m_menu = new QMenu;
    m_menu->setFont(KGlobalSettings::menuFont());
    connect(m_menu, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
263
    connect(m_menu, SIGNAL(triggered(QAction*)), this, SLOT(slotWindowOperation(QAction*)), Qt::QueuedConnection);
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
343

    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()));
    }
344

345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
    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"));
364
        if (kaction != 0)
365
366
            m_removeFromTabGroup->setShortcut(kaction->globalShortcut().primary());
        m_removeFromTabGroup->setData(Options::RemoveTabFromGroupOp);
367

368
369
370
371
372
373
        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);
374

375
376
        m_menu->addSeparator();
    }
377

378
    m_menu->addSeparator();
379

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

383
    m_menu->addSeparator();
384

385
386
387
388
389
390
    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);
391
392
}

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

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

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

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
    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
445
446
        initTabbingPopups();
    } else {
447
448
        delete m_addTabsMenu;
        m_addTabsMenu = 0;
449
    }
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470

#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
471
}
472

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

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

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

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

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

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

517
    m_switchToTabMenu->clear();
Thomas Lübking's avatar
Thomas Lübking committed
518
    // whatever happens "0x1" and "0x2" are no heap positions ;-)
519
520
    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
521

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

524
525
526
    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
527
            continue; // cannot tab there anyway
528
        m_switchToTabMenu->addAction(shortCaption((*i)->caption()))->setData(QVariant::fromValue(*i));
529
530
    }

531
532
}

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

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

549
    m_addTabsMenu->clear();
Thomas Lübking's avatar
Thomas Lübking committed
550
    QList<Client*> handled;
551
552
553
    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
554
            continue;
555
        m_addTabsMenu->addAction(shortCaption((*i)->caption()))->setData(QVariant::fromValue(*i));
Thomas Lübking's avatar
Thomas Lübking committed
556
    }
557
}
558

559
void UserActionsMenu::initTabbingPopups()
560
{
Thomas Lübking's avatar
Thomas Lübking committed
561
    bool needTabManagers = false;
562
    if (m_client.data()->tabGroup() && m_client.data()->tabGroup()->count() > 1) {
Thomas Lübking's avatar
Thomas Lübking committed
563
        needTabManagers = true;
564
565
566
567
568
569
        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);
570
        }
Thomas Lübking's avatar
Thomas Lübking committed
571
    } else {
572
573
        delete m_switchToTabMenu;
        m_switchToTabMenu = 0;
Thomas Lübking's avatar
Thomas Lübking committed
574
575
    }

576
577
578
579
580
581
    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);
582
    }
Thomas Lübking's avatar
Thomas Lübking committed
583

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

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

593
594
595
596
    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()));
597

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

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

610
611
612
613
    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()));
614

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

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

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

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

639
void UserActionsMenu::desktopPopupAboutToShow()
640
{
641
    if (!m_desktopMenu)
642
        return;
643
    const VirtualDesktopManager *vds = VirtualDesktopManager::self();
644

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

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

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

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

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

676
    if (vds->count() >= vds->maximum())
677
        action->setEnabled(false);
678
}
679

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

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

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

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

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

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

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

726
727
        if (!m_client.isNull() &&
                !m_client.data()->isOnAllActivities() && m_client.data()->isOnActivity(id))
728
            action->setChecked(true);
729
    }
730
#endif
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
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)
{
762
763
764
765
766
    bool ok = false;
    uint desk = action->data().toUInt(&ok);
    if (!ok) {
        return;
    }
767
768
769
    if (m_client.isNull())
        return;
    Workspace *ws = Workspace::self();
770
    VirtualDesktopManager *vds = VirtualDesktopManager::self();
771
772
773
774
    if (desk == 0) {
        // the 'on_all_desktops' menu entry
        m_client.data()->setOnAllDesktops(!m_client.data()->isOnAllDesktops());
        return;
775
776
    } else if (desk > vds->count()) {
        vds->setCount(desk);
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
824
825
826
827
828
829
    }

    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));
}

830
void Workspace::closeActivePopup()
831
832
{
    if (active_popup) {
833
834
835
836
        active_popup->close();
        active_popup = NULL;
        active_popup_client = NULL;
    }
837
    m_userActionsMenu->close();
838
}
839
840
841
842
843

/*!
  Create the global accel object \c keys.
 */
void Workspace::initShortcuts()
844
845
{
    keys = new KActionCollection(this);
846
847
848
849
850
    KActionCollection* actionCollection = keys;
    QAction* a = 0L;

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

866
867
868
void Workspace::setupWindowShortcut(Client* c)
{
    assert(client_keys_dialog == NULL);
Lucas Murray's avatar
Lucas Murray committed
869
870
871
872
    // TODO: PORT ME (KGlobalAccel related)
    //keys->setEnabled( false );
    //disable_shortcuts_keys->setEnabled( false );
    //client_keys->setEnabled( false );
873
    client_keys_dialog = new ShortcutDialog(c->shortcut().primary());
874
    client_keys_client = c;
875
876
    connect(client_keys_dialog, SIGNAL(dialogDone(bool)), SLOT(setupWindowShortcutDone(bool)));
    QRect r = clientArea(ScreenArea, c);
877
878
    QSize size = client_keys_dialog->sizeHint();
    QPoint pos = c->pos() + c->clientPos();
879
880
881
882
883
    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);
884
885
886
    client_keys_dialog->show();
    active_popup = client_keys_dialog;
    active_popup_client = c;
887
}
888

889
890
void Workspace::setupWindowShortcutDone(bool ok)
{
891
892
893
//    keys->setEnabled( true );
//    disable_shortcuts_keys->setEnabled( true );
//    client_keys->setEnabled( true );
894
895
    if (ok)
        client_keys_client->setShortcut(KShortcut(client_keys_dialog->shortcut()).toString());
896
    closeActivePopup();
897
    client_keys_dialog->deleteLater();
898
899
    client_keys_dialog = NULL;
    client_keys_client = NULL;
900
901
    if (active_client)
        active_client->takeFocus(Allowed);
902
}
903

904
905
906
907
908
909
910
911
912
913
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
914

915
        KAction *kaction = qobject_cast<KAction*>(action);
916
917
        // 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
918
        kaction->setGlobalShortcut(
919
920
921
922
923
            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
924
            kaction->forgetGlobalShortcut();
925
        }
926
        delete action;
927
    }
928
}
929

930
931
932
void Workspace::performWindowOperation(Client* c, Options::WindowOperation op)
{
    if (!c)
933
        return;
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
    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:
952
        QMetaObject::invokeMethod(c, "closeWindow", Qt::QueuedConnection);
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
986
987
988
989
990
991
992
993
994
        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())
995
            lowerClient(c);
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
        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
1013
    case Options::TabDragOp: // Handled by decoration itself
1014
1015
    case Options::NoOp:
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1016
    case Options::RemoveTabFromGroupOp:
Thomas Lübking's avatar
Thomas Lübking committed
1017
        if (c->untab(c->geometry().translated(cascadeOffset(c))) && options->focusPolicyIsReasonable())
Thomas Lübking's avatar
Thomas Lübking committed
1018
             takeActivity(c, ActivityFocus | ActivityRaise, true);
1019
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1020
1021
1022
    case Options::ActivateNextTabOp:
        if (c->tabGroup())
            c->tabGroup()->activateNext();
1023
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1024
1025
1026
    case Options::ActivatePreviousTabOp:
        if (c->tabGroup())
            c->tabGroup()->activatePrev();
1027
        break;
Thomas Lübking's avatar
Thomas Lübking committed
1028
1029
    case Options::CloseTabGroupOp:
        c->tabGroup()->closeAll();
1030
        break;
1031
    }
1032
}
1033

1034
1035
1036
1037
/**
 * Called by the decoration in the new API to determine what buttons the user has configured for
 * window tab dragging and the operations menu.
 */
1038
1039
Options::WindowOperation Client::mouseButtonToWindowOperation(Qt::MouseButtons button)
{
1040
1041
    Options::MouseCommand com = Options::MouseNothing;
    bool active = isActive();
1042
    if (!wantsInput())   // we cannot be active, use it anyway
1043
1044
        active = true;

1045
    if (button == Qt::LeftButton)
1046
        com = active ? options->commandActiveTitlebar1() : options->commandInactiveTitlebar1();
1047
    else if (button == Qt::MidButton)
1048
        com = active ? options->commandActiveTitlebar2() : options->commandInactiveTitlebar2();
1049
    else if (button == Qt::RightButton)
1050
1051
1052
        com = active ? options->commandActiveTitlebar3() : options->commandInactiveTitlebar3();

    // TODO: Complete the list
Thomas Lübking's avatar
Thomas Lübking committed
1053
1054
    if (com == Options::MouseDragTab)
        return Options::TabDragOp;
1055
    if (com == Options::MouseOperationsMenu)
1056
1057
        return Options::OperationsOp;
    return Options::NoOp;
1058
}
1059

1060
1061
1062
/*!
  Performs a mouse command on this client (see options.h)
 */
1063
1064
bool Client::performMouseCommand(Options::MouseCommand command, const QPoint &globalPos, bool handled)
{
1065
    bool replay = false;
1066
1067
1068
1069
    switch(command) {
    case Options::MouseRaise:
        workspace()->raiseClient(this);
        break;
1070
    case Options::MouseLower: {
1071
        workspace()->lowerClient(this);
1072
1073
        // used to be activateNextClient(this), then topClientOnDesktop
        // since this is a mouseOp it's however safe to use the client under the mouse instead
1074
        if (isActive() && options->focusPolicyIsReasonable()) {
1075
1076
1077
1078
            Client *next = workspace()->clientUnderMouse(screen());
            if (next && next != this)
                workspace()->requestFocus(next, false);
        }
1079
        break;
1080
    }
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
    case Options::MouseShade :
        toggleShade();
        cancelShadeHoverTimer();
        break