~ubuntu-branches/ubuntu/precise/kde-workspace/precise-security

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2009 Nikhil Marathe <nsm.nikhil@gmail.com>

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/>.
*********************************************************************/

#include "tilinglayout.h"

#include <QCursor>

#include "client.h"
#include "tile.h"
#include "workspace.h"

namespace KWin
{

TilingLayout::TilingLayout(Workspace *w)
    : m_workspace(w)
{
}

TilingLayout::~TilingLayout()
{
    qDeleteAll(m_tiles);
    m_tiles.clear();
}

int TilingLayout::findTilePos(Client *c) const
{
    int i = 0;
    foreach (Tile * t, m_tiles) {
        if (t->client() == c)
            return i;
        i++;
    }
    return -1;
}

Tile* TilingLayout::findTile(Client *c) const
{
    int i = findTilePos(c);
    if (i != -1)
        return m_tiles[ i ];
    return NULL;
}

void TilingLayout::clientMinimizeToggled(Client *c)
{
    // just rearrange since that will check for state
    Tile *t = findTile(c);
    if (t)
        arrange(layoutArea(t));
}

bool TilingLayout::clientResized(Client *c, const QRect &moveResizeGeom, const QRect &orig)
{
    if (moveResizeGeom == orig)
        return true;

    Tile *t = findTile(c);
    if (!t || t->ignoreGeometry()) {
        c->setGeometry(moveResizeGeom);
        return true;
    }

    return false;
}

// tries to swap the tile with the one in the new position right now
void TilingLayout::clientMoved(Client *c, const QRect &moveResizeGeom, const QRect &orig)
{
    if (moveResizeGeom == orig)
        return;

    Tile *t = findTile(c);
    if (!t) {
        c->setGeometry(moveResizeGeom);
        return;
    }
    if (t->floating()) {
        t->setGeometry(moveResizeGeom);
        t->commit();
        return;
    }

    Tile *r = findTileBelowPoint(QCursor::pos());
    // TODO: if the client moved in from another desktop, don't swap, add
    if (r && t) {
        swapTiles(r, t);
    }
}

void TilingLayout::swapTiles(Tile *a, Tile *b)
{
    if (a && b) {
        // t is the tile the user requested a move of
        // r is the tile below it
        int a_index = tiles().indexOf(a);
        int b_index = tiles().indexOf(b);

        // use m_tiles since tiles() is const
        // not sure how good an idea this is
        m_tiles.replace(a_index, b);
        m_tiles.replace(b_index, a);
        arrange(layoutArea(a));
    }
}

void TilingLayout::addTileNoArrange(Tile * t)
{
    if (findTile(t->client()))
        return;
    m_tiles.append(t);
    postAddTile(t);
}

void TilingLayout::addTile(Tile *t)
{
    addTileNoArrange(t);
    arrange(layoutArea(t));
}

void TilingLayout::addTile(Client *c)
{
    Q_UNUSED(c)
}

void TilingLayout::removeTileNoArrange(Tile * t)
{
    if (t == NULL)
        return;
    preRemoveTile(t);
    m_tiles.removeOne(t);
}

const QRect TilingLayout::layoutArea(Tile *t) const
{
    return m_workspace->clientArea(PlacementArea, t->client());
}

void TilingLayout::removeTile(Tile *t)
{
    if (t == NULL)
        return;
    removeTileNoArrange(t);
    if (!m_tiles.empty())
        arrange(layoutArea(m_tiles.first()));
}

void TilingLayout::removeTile(Client *c)
{
    removeTile(findTile(c));
}

void TilingLayout::toggleFloatTile(Client *c)
{
    Tile *t = findTile(c);
    if (t && t->floating())
        t->unfloatTile();
    else if (t)
        t->floatTile();

    if (t)
        arrange(layoutArea(t));
}

void TilingLayout::reconfigureTiling()
{
    //TODO also check 'untiled' windows to see if they are now requesting tiling
    foreach (Tile * t, tiles()) {
        if (t->client()->rules()->checkTilingOption(t->floating() ? 1 : 0) == 1)
            t->floatTile();
        else
            t->unfloatTile();
    }

    if (tiles().length() > 0)
        arrange(layoutArea(tiles().first()));

    foreach (Client * c, workspace()->stackingOrder()) {
        if (c->rules()->checkTilingOption(0) == 1)
            workspace()->createTile(c);
    }

}

Tile* TilingLayout::findTileBelowPoint(const QPoint &p) const
{
    foreach (Tile * t, tiles()) {
        if (t->floating())
            continue;
        if (t->geometry().contains(p))
            return t;
    }

    return NULL;
}

void TilingLayout::commit()
{
    foreach (Tile * t, m_tiles)
        t->commit();
}

/*
 * Default is to allow no resizing
 */
KDecorationDefines::Position TilingLayout::resizeMode(Client *c, KDecorationDefines::Position currentMode) const
{
    Tile *t = findTile(c);
    // if not tiled, allow resize
    if (!t)
        return currentMode;

    if (t && t->floating())
        return currentMode;
    // We return PositionCenter since it makes
    // no sense in resizing.
    return KDecorationDefines::PositionCenter;
}

} // end namespace