~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/widgets/qsizegrip.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the widgets module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qsizegrip.h"
 
30
 
 
31
#ifndef QT_NO_SIZEGRIP
 
32
 
 
33
#include "qapplication.h"
 
34
#include "qevent.h"
 
35
#include "qpainter.h"
 
36
#include "qstyle.h"
 
37
#include "qstyleoption.h"
 
38
 
 
39
#if defined(Q_WS_X11)
 
40
#include <private/qt_x11_p.h>
 
41
#elif defined (Q_WS_WIN)
 
42
#include "qt_windows.h"
 
43
#endif
 
44
 
 
45
#include <private/qwidget_p.h>
 
46
 
 
47
class QSizeGripPrivate : public QWidgetPrivate
 
48
{
 
49
    Q_DECLARE_PUBLIC(QSizeGrip)
 
50
public:
 
51
    void init();
 
52
    QPoint p;
 
53
    QSize s;
 
54
    int d;
 
55
    QWidget *tlw;
 
56
    bool hiddenByUser;
 
57
};
 
58
 
 
59
static QWidget *qt_sizegrip_topLevelWidget(QWidget* w)
 
60
{
 
61
    QWidget *p = w->parentWidget();
 
62
    while (!w->isWindow() && p && !p->inherits("QWorkspace")) {
 
63
        w = p;
 
64
        p = p->parentWidget();
 
65
    }
 
66
    return w;
 
67
}
 
68
 
 
69
static QWidget* qt_sizegrip_workspace(QWidget* w)
 
70
{
 
71
    while (w && !w->inherits("QWorkspace")) {
 
72
        if (w->isWindow())
 
73
            return 0;
 
74
        w = w->parentWidget();
 
75
    }
 
76
    return w;
 
77
}
 
78
 
 
79
 
 
80
/*!
 
81
    \class QSizeGrip qsizegrip.h
 
82
 
 
83
    \brief The QSizeGrip class provides a corner-grip for resizing a top-level window.
 
84
 
 
85
    \ingroup application
 
86
    \ingroup basic
 
87
    \ingroup appearance
 
88
 
 
89
    This widget works like the standard Windows resize handle. In the
 
90
    X11 version this resize handle generally works differently from
 
91
    the one provided by the system; we hope to reduce this difference
 
92
    in the future.
 
93
 
 
94
    Put this widget anywhere in a widget tree and the user can use it
 
95
    to resize the top-level window. Generally, this should be in the
 
96
    lower right-hand corner. Note that QStatusBar already uses this
 
97
    widget, so if you have a status bar (e.g. you are using
 
98
    QMainWindow), then you don't need to use this widget explicitly.
 
99
 
 
100
    On some platforms the sizegrip automatically hides itself when the
 
101
    window is shown full screen or maximised.
 
102
 
 
103
    \inlineimage qsizegrip-m.png Screenshot in Motif style
 
104
    \inlineimage qsizegrip-w.png Screenshot in Windows style
 
105
 
 
106
    \sa QStatusBar QWidget::windowState()
 
107
*/
 
108
 
 
109
 
 
110
/*!
 
111
    Constructs a resize corner as a child widget of \a
 
112
    parent.
 
113
*/
 
114
QSizeGrip::QSizeGrip(QWidget * parent)
 
115
    : QWidget(*new QSizeGripPrivate, parent, 0)
 
116
{
 
117
    Q_D(QSizeGrip);
 
118
    d->init();
 
119
}
 
120
 
 
121
#ifdef QT3_SUPPORT
 
122
/*!
 
123
  \obsolete
 
124
 
 
125
    Constructs a resize corner called \a name, as a child widget of \a
 
126
    parent.
 
127
*/
 
128
QSizeGrip::QSizeGrip(QWidget * parent, const char* name)
 
129
    : QWidget(*new QSizeGripPrivate, parent, 0)
 
130
{
 
131
    Q_D(QSizeGrip);
 
132
    setObjectName(name);
 
133
    d->init();
 
134
}
 
135
#endif
 
136
 
 
137
void QSizeGripPrivate::init()
 
138
{
 
139
    Q_Q(QSizeGrip);
 
140
    hiddenByUser = false;
 
141
#ifndef QT_NO_CURSOR
 
142
#ifndef Q_WS_MAC
 
143
    q->setCursor(q->isRightToLeft() ? Qt::SizeBDiagCursor : Qt::SizeFDiagCursor);
 
144
#endif
 
145
#endif
 
146
    q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
 
147
#if defined(Q_WS_X11)
 
148
    if (!qt_sizegrip_workspace(q)) {
 
149
        WId id = q->winId();
 
150
        XChangeProperty(X11->display, q->window()->winId(),
 
151
                        ATOM(_QT_SIZEGRIP), XA_WINDOW, 32, PropModeReplace,
 
152
                        (unsigned char *)&id, 1);
 
153
    }
 
154
#endif
 
155
    tlw = qt_sizegrip_topLevelWidget(q);
 
156
    if (tlw)
 
157
        tlw->installEventFilter(q);
 
158
}
 
159
 
 
160
 
 
161
/*!
 
162
    Destroys the size grip.
 
163
*/
 
164
QSizeGrip::~QSizeGrip()
 
165
{
 
166
#if defined(Q_WS_X11)
 
167
    if (!QApplication::closingDown() && parentWidget()) {
 
168
        WId id = XNone;
 
169
        XChangeProperty(X11->display, window()->winId(),
 
170
                        ATOM(_QT_SIZEGRIP), XA_WINDOW, 32, PropModeReplace,
 
171
                        (unsigned char *)&id, 1);
 
172
    }
 
173
#endif
 
174
}
 
175
 
 
176
/*!
 
177
    Returns the size grip's size hint; this is a small size.
 
178
*/
 
179
QSize QSizeGrip::sizeHint() const
 
180
{
 
181
    QStyleOption opt(0);
 
182
    opt.init(this);
 
183
    return (style()->sizeFromContents(QStyle::CT_SizeGrip, &opt, QSize(13, 13), this).
 
184
            expandedTo(QApplication::globalStrut()));
 
185
}
 
186
 
 
187
/*!
 
188
    Paints the resize grip. Resize grips are usually rendered as small
 
189
    diagonal textured lines in the lower-right corner. The event is in
 
190
    \a event.
 
191
*/
 
192
void QSizeGrip::paintEvent(QPaintEvent *event)
 
193
{
 
194
    Q_UNUSED(event);
 
195
    QPainter painter(this);
 
196
    QStyleOption opt(0);
 
197
    opt.init(this);
 
198
    style()->drawControl(QStyle::CE_SizeGrip, &opt, &painter, this);
 
199
}
 
200
 
 
201
/*!
 
202
    Primes the resize operation. The event is in \a e.
 
203
*/
 
204
void QSizeGrip::mousePressEvent(QMouseEvent * e)
 
205
{
 
206
    Q_D(QSizeGrip);
 
207
    d->p = e->globalPos();
 
208
    d->s = qt_sizegrip_topLevelWidget(this)->size();
 
209
}
 
210
 
 
211
 
 
212
/*!
 
213
    Resizes the top-level widget containing this widget. The event is
 
214
    in \a e.
 
215
*/
 
216
void QSizeGrip::mouseMoveEvent(QMouseEvent * e)
 
217
{
 
218
    if (e->buttons() != Qt::LeftButton)
 
219
        return;
 
220
 
 
221
    Q_D(QSizeGrip);
 
222
    QWidget* tlw = qt_sizegrip_topLevelWidget(this);
 
223
    if (tlw->testAttribute(Qt::WA_WState_ConfigPending))
 
224
        return;
 
225
 
 
226
    QPoint np(e->globalPos());
 
227
 
 
228
    QWidget* ws = qt_sizegrip_workspace(this);
 
229
    if (ws) {
 
230
        QPoint tmp(ws->mapFromGlobal(np));
 
231
        if (tmp.x() > ws->width())
 
232
            tmp.setX(ws->width());
 
233
        if (tmp.y() > ws->height())
 
234
            tmp.setY(ws->height());
 
235
        np = ws->mapToGlobal(tmp);
 
236
    }
 
237
 
 
238
    int w;
 
239
    int h = np.y() - d->p.y() + d->s.height();
 
240
 
 
241
    if (isRightToLeft())
 
242
        w = d->s.width() - (np.x() - d->p.x());
 
243
    else
 
244
        w = np.x() - d->p.x() + d->s.width();
 
245
 
 
246
    if (w < 1)
 
247
        w = 1;
 
248
    if (h < 1)
 
249
        h = 1;
 
250
    QSize ms(tlw->minimumSize());
 
251
    ms = ms.expandedTo(minimumSize());
 
252
    if (w < ms.width())
 
253
        w = ms.width();
 
254
    if (h < ms.height())
 
255
        h = ms.height();
 
256
 
 
257
    if (isRightToLeft()) {
 
258
        d->tlw->resize(w, h);
 
259
        if (d->tlw->size() == QSize(w, h))
 
260
            d->tlw->move(tlw->x() + (np.x() - d->p.x()), tlw->y());
 
261
    } else {
 
262
        d->tlw->resize(w, h);
 
263
    }
 
264
#ifdef Q_WS_WIN
 
265
    MSG msg;
 
266
    while(PeekMessage(&msg, winId(), WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE))
 
267
      ;
 
268
#endif
 
269
    QApplication::syncX();
 
270
 
 
271
    if (isRightToLeft() && d->tlw->size() == QSize(w,h)) {
 
272
        d->s.rwidth() = tlw->size().width();
 
273
        d->p.rx() = np.x();
 
274
    }
 
275
}
 
276
 
 
277
/*! \reimp */
 
278
void QSizeGrip::setVisible(bool visible)
 
279
{
 
280
    Q_D(QSizeGrip);
 
281
    d->hiddenByUser = !visible;
 
282
    QWidget::setVisible(visible);
 
283
}
 
284
 
 
285
 
 
286
/*! \reimp */
 
287
bool QSizeGrip::eventFilter(QObject *o, QEvent *e)
 
288
{
 
289
    Q_D(QSizeGrip);
 
290
    if (!d->hiddenByUser && e->type() == QEvent::WindowStateChange && o == d->tlw) {
 
291
        QWidget::setVisible((d->tlw->windowState() &
 
292
                             (Qt::WindowFullScreen
 
293
#ifndef Q_WS_MAC
 
294
                              | Qt::WindowMaximized
 
295
#endif
 
296
                                 ))==0);
 
297
    }
 
298
    return false;
 
299
}
 
300
 
 
301
/*! \reimp */
 
302
bool QSizeGrip::event(QEvent *e)
 
303
{
 
304
    switch(e->type()) {
 
305
#if defined(Q_WS_MAC)
 
306
    case QEvent::Hide:
 
307
    case QEvent::Show:
 
308
        if(!QApplication::closingDown() && parentWidget() && !qt_sizegrip_workspace(this)) {
 
309
            if(QWidget *w = qt_sizegrip_topLevelWidget(this)) {
 
310
                if(w->isWindow())
 
311
                    QWidgetPrivate::qt_mac_update_sizer(w, e->type() == QEvent::Hide ? -1 : 1);
 
312
            }
 
313
        }
 
314
        break;
 
315
#endif
 
316
    default:
 
317
        break;
 
318
    }
 
319
    return QWidget::event(e);
 
320
}
 
321
 
 
322
#endif //QT_NO_SIZEGRIP