-
Notifications
You must be signed in to change notification settings - Fork 33
/
breezesizegrip.cpp
303 lines (231 loc) · 9.82 KB
/
breezesizegrip.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
/*************************************************************************
* Copyright (C) 2014 by Hugo Pereira Da Costa <[email protected]> *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
*************************************************************************/
#include "breezesizegrip.h"
#include <KDecoration2/DecoratedClient>
#include <QPainter>
#include <QPolygon>
#include <QTimer>
#if BREEZE_HAVE_X11
#include <QtGui/private/qtx11extras_p.h>
#endif
namespace Breeze
{
//* scoped pointer convenience typedef
template <typename T> using ScopedPointer = QScopedPointer<T, QScopedPointerPodDeleter>;
//_____________________________________________
SizeGrip::SizeGrip( Decoration* decoration ):QWidget(nullptr)
,m_decoration( decoration )
{
setAttribute(Qt::WA_NoSystemBackground );
setAutoFillBackground( false );
// cursor
setCursor( Qt::SizeFDiagCursor );
// size
setFixedSize( QSize( GripSize, GripSize ) );
// mask
setMask( QRegion( QVector<QPoint>{
QPoint( 0, GripSize ),
QPoint( GripSize, 0 ),
QPoint( GripSize, GripSize ),
QPoint( 0, GripSize )} ) );
// embed
embed();
updatePosition();
// connections
auto c = decoration->client();
connect( c, &KDecoration2::DecoratedClient::widthChanged, this, &SizeGrip::updatePosition );
connect( c, &KDecoration2::DecoratedClient::heightChanged, this, &SizeGrip::updatePosition );
connect( c, &KDecoration2::DecoratedClient::activeChanged, this, &SizeGrip::updateActiveState );
// show
show();
}
//_____________________________________________
SizeGrip::~SizeGrip()
{}
//_____________________________________________
void SizeGrip::updateActiveState()
{
#if BREEZE_HAVE_X11
if( QX11Info::isPlatformX11() )
{
const quint32 value = XCB_STACK_MODE_ABOVE;
xcb_configure_window( QX11Info::connection(), winId(), XCB_CONFIG_WINDOW_STACK_MODE, &value );
xcb_map_window( QX11Info::connection(), winId() );
}
#endif
update();
}
//_____________________________________________
void SizeGrip::embed()
{
#if BREEZE_HAVE_X11
if( !QX11Info::isPlatformX11() ) return;
auto c = m_decoration.data()->client();
xcb_window_t windowId = c->windowId();
if( windowId )
{
/*
find client's parent
we want the size grip to be at the same level as the client in the stack
*/
xcb_window_t current = windowId;
auto connection = QX11Info::connection();
xcb_query_tree_cookie_t cookie = xcb_query_tree_unchecked( connection, current );
ScopedPointer<xcb_query_tree_reply_t> tree(xcb_query_tree_reply( connection, cookie, nullptr ) );
if( !tree.isNull() && tree->parent ) current = tree->parent;
// reparent
xcb_reparent_window( connection, winId(), current, 0, 0 );
setWindowTitle( "Breeze::SizeGrip" );
} else {
hide();
}
#endif
}
//_____________________________________________
void SizeGrip::paintEvent( QPaintEvent* )
{
if( !m_decoration ) return;
// get relevant colors
const QColor backgroundColor( m_decoration.data()->titleBarColor() );
// create and configure painter
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing );
painter.setPen( Qt::NoPen );
painter.setBrush( backgroundColor );
// polygon
painter.drawPolygon( QVector<QPoint> {
QPoint( 0, GripSize ),
QPoint( GripSize, 0 ),
QPoint( GripSize, GripSize ),
QPoint( 0, GripSize )} );
}
//_____________________________________________
void SizeGrip::mousePressEvent( QMouseEvent* event )
{
switch (event->button())
{
case Qt::RightButton:
{
hide();
QTimer::singleShot(5000, this, &QWidget::show);
break;
}
case Qt::MiddleButton:
{
hide();
break;
}
case Qt::LeftButton:
if( rect().contains( event->pos() ) )
{ sendMoveResizeEvent( event->pos() ); }
break;
default: break;
}
}
//_______________________________________________________________________________
void SizeGrip::updatePosition()
{
#if BREEZE_HAVE_X11
if( !QX11Info::isPlatformX11() ) return;
auto c = m_decoration.data()->client();
QPoint position(
c->width() - GripSize - Offset,
c->height() - GripSize - Offset );
quint32 values[2] = { quint32(position.x()), quint32(position.y()) };
xcb_configure_window( QX11Info::connection(), winId(), XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values );
#endif
}
//_____________________________________________
void SizeGrip::sendMoveResizeEvent( QPoint position )
{
#if BREEZE_HAVE_X11
if( !QX11Info::isPlatformX11() ) return;
// pointer to connection
auto connection( QX11Info::connection() );
// client
auto c = m_decoration.data()->client();
/*
get root position matching position
need to use xcb because the embedding of the widget
breaks QT's mapToGlobal and other methods
*/
QPoint rootPosition( position );
xcb_get_geometry_cookie_t cookie( xcb_get_geometry( connection, winId() ) );
ScopedPointer<xcb_get_geometry_reply_t> reply( xcb_get_geometry_reply( connection, cookie, nullptr ) );
if( reply )
{
// translate coordinates
xcb_translate_coordinates_cookie_t coordCookie( xcb_translate_coordinates(
connection, winId(), reply.data()->root,
-reply.data()->border_width,
-reply.data()->border_width ) );
ScopedPointer< xcb_translate_coordinates_reply_t> coordReply( xcb_translate_coordinates_reply( connection, coordCookie, nullptr ) );
if( coordReply )
{
rootPosition.rx() += coordReply.data()->dst_x;
rootPosition.ry() += coordReply.data()->dst_y;
}
}
// move/resize atom
if( !m_moveResizeAtom )
{
// create atom if not found
const QString atomName( "_NET_WM_MOVERESIZE" );
xcb_intern_atom_cookie_t cookie( xcb_intern_atom( connection, false, atomName.size(), qPrintable( atomName ) ) );
ScopedPointer<xcb_intern_atom_reply_t> reply( xcb_intern_atom_reply( connection, cookie, nullptr ) );
m_moveResizeAtom = reply ? reply->atom:0;
}
if( !m_moveResizeAtom ) return;
// button release event
xcb_button_release_event_t releaseEvent;
memset(&releaseEvent, 0, sizeof(releaseEvent));
releaseEvent.response_type = XCB_BUTTON_RELEASE;
releaseEvent.event = winId();
releaseEvent.child = XCB_WINDOW_NONE;
releaseEvent.root = QX11Info::appRootWindow();
releaseEvent.event_x = position.x();
releaseEvent.event_y = position.y();
releaseEvent.root_x = rootPosition.x();
releaseEvent.root_y = rootPosition.y();
releaseEvent.detail = XCB_BUTTON_INDEX_1;
releaseEvent.state = XCB_BUTTON_MASK_1;
releaseEvent.time = XCB_CURRENT_TIME;
releaseEvent.same_screen = true;
xcb_send_event( connection, false, winId(), XCB_EVENT_MASK_BUTTON_RELEASE, reinterpret_cast<const char*>(&releaseEvent));
xcb_ungrab_pointer( connection, XCB_TIME_CURRENT_TIME );
// move resize event
xcb_client_message_event_t clientMessageEvent;
memset(&clientMessageEvent, 0, sizeof(clientMessageEvent));
clientMessageEvent.response_type = XCB_CLIENT_MESSAGE;
clientMessageEvent.type = m_moveResizeAtom;
clientMessageEvent.format = 32;
clientMessageEvent.window = c->windowId();
clientMessageEvent.data.data32[0] = rootPosition.x();
clientMessageEvent.data.data32[1] = rootPosition.y();
clientMessageEvent.data.data32[2] = 4; // bottom right
clientMessageEvent.data.data32[3] = Qt::LeftButton;
clientMessageEvent.data.data32[4] = 0;
xcb_send_event( connection, false, QX11Info::appRootWindow(),
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
reinterpret_cast<const char*>(&clientMessageEvent) );
xcb_flush( connection );
#endif
}
}