source: trunk/src/qt3support/widgets/q3titlebar.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 19.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt3Support module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qplatformdefs.h"
43
44#ifndef QT_NO_TITLEBAR
45
46#include "qapplication.h"
47#include "qcursor.h"
48#include "qdatetime.h"
49#include "qevent.h"
50#include "qimage.h"
51#include "qpainter.h"
52#include "qiodevice.h"
53#include "qpixmap.h"
54#include "qstyle.h"
55#include "qstyleoption.h"
56#include "qtimer.h"
57#include "qtooltip.h"
58#include "qdebug.h"
59#if defined(Q_WS_WIN)
60#include "qt_windows.h"
61#endif
62
63#include "private/qapplication_p.h"
64#include "private/q3titlebar_p.h"
65#include "private/qwidget_p.h"
66
67QT_BEGIN_NAMESPACE
68
69class Q3TitleBarPrivate : public QWidgetPrivate
70{
71 Q_DECLARE_PUBLIC(Q3TitleBar)
72public:
73 Q3TitleBarPrivate()
74 : toolTip(0), act(0), window(0), movable(1), pressed(0), autoraise(0), inevent(0)
75 {
76 }
77
78 Qt::WindowFlags flags;
79 QStyle::SubControl buttonDown;
80 QPoint moveOffset;
81 QToolTip *toolTip;
82 bool act :1;
83 QWidget* window;
84 bool movable :1;
85 bool pressed :1;
86 bool autoraise :1;
87 bool inevent : 1;
88
89 int titleBarState() const;
90 QStyleOptionTitleBar getStyleOption() const;
91 void readColors();
92};
93
94inline int Q3TitleBarPrivate::titleBarState() const
95{
96 uint state = window ? window->windowState() : static_cast<Qt::WindowStates>(Qt::WindowNoState);
97 state |= uint(act ? QStyle::State_Active : QStyle::State_None);
98 return (int)state;
99}
100
101QStyleOptionTitleBar Q3TitleBarPrivate::getStyleOption() const
102{
103 Q_Q(const Q3TitleBar);
104 QStyleOptionTitleBar opt;
105 opt.init(q);
106 opt.text = q->windowTitle();
107 //################
108 QIcon icon = q->windowIcon();
109 QSize s = icon.actualSize(QSize(64, 64));
110 opt.icon = icon.pixmap(s);
111 opt.subControls = QStyle::SC_All;
112 opt.activeSubControls = QStyle::SC_None;
113 opt.titleBarState = titleBarState();
114 opt.titleBarFlags = flags;
115 return opt;
116}
117
118Q3TitleBar::Q3TitleBar(QWidget *w, QWidget *parent, Qt::WindowFlags f)
119 : QWidget(*new Q3TitleBarPrivate, parent, Qt::WStyle_Customize | Qt::WStyle_NoBorder)
120{
121 Q_D(Q3TitleBar);
122 if (f == 0 && w)
123 f = w->windowFlags();
124 d->flags = f;
125 d->window = w;
126 d->buttonDown = QStyle::SC_None;
127 d->act = 0;
128 if (w) {
129 if (w->minimumSize() == w->maximumSize())
130 d->flags &= ~Qt::WindowMaximizeButtonHint;
131 setWindowTitle(w->windowTitle());
132 }
133
134 d->readColors();
135 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
136 setMouseTracking(true);
137 setAutoRaise(style()->styleHint(QStyle::SH_TitleBar_AutoRaise, 0, this));
138}
139
140void Q3TitleBar::setFakeWindowFlags(Qt::WindowFlags f)
141{
142 Q_D(Q3TitleBar);
143 d->flags = f;
144}
145
146Qt::WindowFlags Q3TitleBar::fakeWindowFlags() const
147{
148 Q_D(const Q3TitleBar);
149 return d->flags;
150}
151
152Q3TitleBar::~Q3TitleBar()
153{
154}
155
156QStyleOptionTitleBar Q3TitleBar::getStyleOption() const
157{
158 return d_func()->getStyleOption();
159}
160
161#ifdef Q_WS_WIN
162static inline QRgb colorref2qrgb(COLORREF col)
163{
164 return qRgb(GetRValue(col),GetGValue(col),GetBValue(col));
165}
166#endif
167
168void Q3TitleBarPrivate::readColors()
169{
170 Q_Q(Q3TitleBar);
171 QPalette pal = q->palette();
172
173 bool colorsInitialized = false;
174
175#ifdef Q_WS_WIN // ask system properties on windows
176#ifndef SPI_GETGRADIENTCAPTIONS
177#define SPI_GETGRADIENTCAPTIONS 0x1008
178#endif
179#ifndef COLOR_GRADIENTACTIVECAPTION
180#define COLOR_GRADIENTACTIVECAPTION 27
181#endif
182#ifndef COLOR_GRADIENTINACTIVECAPTION
183#define COLOR_GRADIENTINACTIVECAPTION 28
184#endif
185 if (QApplication::desktopSettingsAware()) {
186 pal.setColor(QPalette::Active, QPalette::Highlight, colorref2qrgb(GetSysColor(COLOR_ACTIVECAPTION)));
187 pal.setColor(QPalette::Inactive, QPalette::Highlight, colorref2qrgb(GetSysColor(COLOR_INACTIVECAPTION)));
188 pal.setColor(QPalette::Active, QPalette::HighlightedText, colorref2qrgb(GetSysColor(COLOR_CAPTIONTEXT)));
189 pal.setColor(QPalette::Inactive, QPalette::HighlightedText, colorref2qrgb(GetSysColor(COLOR_INACTIVECAPTIONTEXT)));
190 if (QSysInfo::WindowsVersion != QSysInfo::WV_95 && QSysInfo::WindowsVersion != QSysInfo::WV_NT) {
191 colorsInitialized = true;
192 BOOL gradient;
193 QT_WA({
194 SystemParametersInfo(SPI_GETGRADIENTCAPTIONS, 0, &gradient, 0);
195 } , {
196 SystemParametersInfoA(SPI_GETGRADIENTCAPTIONS, 0, &gradient, 0);
197 });
198 if (gradient) {
199 pal.setColor(QPalette::Active, QPalette::Base, colorref2qrgb(GetSysColor(COLOR_GRADIENTACTIVECAPTION)));
200 pal.setColor(QPalette::Inactive, QPalette::Base, colorref2qrgb(GetSysColor(COLOR_GRADIENTINACTIVECAPTION)));
201 } else {
202 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Active, QPalette::Highlight));
203 pal.setColor(QPalette::Inactive, QPalette::Base, pal.color(QPalette::Inactive, QPalette::Highlight));
204 }
205 }
206 }
207#endif // Q_WS_WIN
208 if (!colorsInitialized) {
209 pal.setColor(QPalette::Active, QPalette::Highlight,
210 pal.color(QPalette::Active, QPalette::Highlight));
211 pal.setColor(QPalette::Active, QPalette::Base,
212 pal.color(QPalette::Active, QPalette::Highlight));
213 pal.setColor(QPalette::Inactive, QPalette::Highlight,
214 pal.color(QPalette::Inactive, QPalette::Dark));
215 pal.setColor(QPalette::Inactive, QPalette::Base,
216 pal.color(QPalette::Inactive, QPalette::Dark));
217 pal.setColor(QPalette::Inactive, QPalette::HighlightedText,
218 pal.color(QPalette::Inactive, QPalette::Window));
219 }
220
221 q->setPalette(pal);
222 q->setActive(act);
223}
224
225void Q3TitleBar::changeEvent(QEvent *ev)
226{
227 if(ev->type() == QEvent::ModifiedChange)
228 update();
229 QWidget::changeEvent(ev);
230}
231
232void Q3TitleBar::mousePressEvent(QMouseEvent *e)
233{
234 Q_D(Q3TitleBar);
235 if (!d->act)
236 emit doActivate();
237 if (e->button() == Qt::LeftButton) {
238 d->pressed = true;
239 QStyleOptionTitleBar opt = d->getStyleOption();
240 QStyle::SubControl ctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt,
241 e->pos(), this);
242 switch (ctrl) {
243 case QStyle::SC_TitleBarSysMenu:
244 if (d->flags & Qt::WindowSystemMenuHint) {
245 d->buttonDown = QStyle::SC_None;
246 static QTime *t = 0;
247 static Q3TitleBar *tc = 0;
248 if (!t)
249 t = new QTime;
250 if (tc != this || t->elapsed() > QApplication::doubleClickInterval()) {
251 emit showOperationMenu();
252 t->start();
253 tc = this;
254 } else {
255 tc = 0;
256 emit doClose();
257 return;
258 }
259 }
260 break;
261
262 case QStyle::SC_TitleBarShadeButton:
263 case QStyle::SC_TitleBarUnshadeButton:
264 if (d->flags & Qt::WindowShadeButtonHint)
265 d->buttonDown = ctrl;
266 break;
267
268 case QStyle::SC_TitleBarNormalButton:
269 if (d->flags & Qt::WindowMinMaxButtonsHint)
270 d->buttonDown = ctrl;
271 break;
272
273 case QStyle::SC_TitleBarMinButton:
274 if (d->flags & Qt::WindowMinimizeButtonHint)
275 d->buttonDown = ctrl;
276 break;
277
278 case QStyle::SC_TitleBarMaxButton:
279 if (d->flags & Qt::WindowMaximizeButtonHint)
280 d->buttonDown = ctrl;
281 break;
282
283 case QStyle::SC_TitleBarCloseButton:
284 if (d->flags & Qt::WindowSystemMenuHint)
285 d->buttonDown = ctrl;
286 break;
287
288 case QStyle::SC_TitleBarLabel:
289 d->buttonDown = ctrl;
290 d->moveOffset = mapToParent(e->pos());
291 break;
292
293 default:
294 break;
295 }
296 repaint();
297 } else {
298 d->pressed = false;
299 }
300}
301
302void Q3TitleBar::contextMenuEvent(QContextMenuEvent *e)
303{
304 Q_D(Q3TitleBar);
305 QStyleOptionTitleBar opt = d->getStyleOption();
306 QStyle::SubControl ctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(),
307 this);
308 if(ctrl == QStyle::SC_TitleBarLabel || ctrl == QStyle::SC_TitleBarSysMenu) {
309 e->accept();
310 emit popupOperationMenu(e->globalPos());
311 } else {
312 e->ignore();
313 }
314}
315
316void Q3TitleBar::mouseReleaseEvent(QMouseEvent *e)
317{
318 Q_D(Q3TitleBar);
319 if (e->button() == Qt::LeftButton && d->pressed) {
320 e->accept();
321 QStyleOptionTitleBar opt = d->getStyleOption();
322 QStyle::SubControl ctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt,
323 e->pos(), this);
324 d->pressed = false;
325 if (ctrl == d->buttonDown) {
326 d->buttonDown = QStyle::SC_None;
327 repaint();
328 switch(ctrl) {
329 case QStyle::SC_TitleBarShadeButton:
330 case QStyle::SC_TitleBarUnshadeButton:
331 if(d->flags & Qt::WindowShadeButtonHint)
332 emit doShade();
333 break;
334
335 case QStyle::SC_TitleBarNormalButton:
336 if(d->flags & Qt::WindowMaximizeButtonHint)
337 emit doNormal();
338 break;
339
340 case QStyle::SC_TitleBarMinButton:
341 if(d->flags & Qt::WindowMinimizeButtonHint) {
342 if (d->window && d->window->isMinimized())
343 emit doNormal();
344 else
345 emit doMinimize();
346 }
347 break;
348
349 case QStyle::SC_TitleBarMaxButton:
350 if(d->flags & Qt::WindowMaximizeButtonHint) {
351 if(d->window && d->window->isMaximized())
352 emit doNormal();
353 else
354 emit doMaximize();
355 }
356 break;
357
358 case QStyle::SC_TitleBarCloseButton:
359 if(d->flags & Qt::WindowSystemMenuHint) {
360 d->buttonDown = QStyle::SC_None;
361 repaint();
362 emit doClose();
363 return;
364 }
365 break;
366
367 default:
368 break;
369 }
370 }
371 } else {
372 e->ignore();
373 }
374}
375
376void Q3TitleBar::mouseMoveEvent(QMouseEvent *e)
377{
378 Q_D(Q3TitleBar);
379 e->accept();
380 switch (d->buttonDown) {
381 case QStyle::SC_None:
382 if(autoRaise())
383 repaint();
384 break;
385 case QStyle::SC_TitleBarSysMenu:
386 break;
387 case QStyle::SC_TitleBarShadeButton:
388 case QStyle::SC_TitleBarUnshadeButton:
389 case QStyle::SC_TitleBarNormalButton:
390 case QStyle::SC_TitleBarMinButton:
391 case QStyle::SC_TitleBarMaxButton:
392 case QStyle::SC_TitleBarCloseButton:
393 {
394 QStyle::SubControl last_ctrl = d->buttonDown;
395 QStyleOptionTitleBar opt = d->getStyleOption();
396 d->buttonDown = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(), this);
397 if (d->buttonDown != last_ctrl)
398 d->buttonDown = QStyle::SC_None;
399 repaint();
400 d->buttonDown = last_ctrl;
401 }
402 break;
403
404 case QStyle::SC_TitleBarLabel:
405 if (d->buttonDown == QStyle::SC_TitleBarLabel && d->movable && d->pressed) {
406 if ((d->moveOffset - mapToParent(e->pos())).manhattanLength() >= 4) {
407 QPoint p = mapFromGlobal(e->globalPos());
408
409 QWidget *parent = d->window ? d->window->parentWidget() : 0;
410 if(parent && parent->inherits("Q3WorkspaceChild")) {
411 QWidget *workspace = parent->parentWidget();
412 p = workspace->mapFromGlobal(e->globalPos());
413 if (!workspace->rect().contains(p)) {
414 if (p.x() < 0)
415 p.rx() = 0;
416 if (p.y() < 0)
417 p.ry() = 0;
418 if (p.x() > workspace->width())
419 p.rx() = workspace->width();
420 if (p.y() > workspace->height())
421 p.ry() = workspace->height();
422 }
423 }
424
425 QPoint pp = p - d->moveOffset;
426 if (!parentWidget()->isMaximized())
427 parentWidget()->move(pp);
428 }
429 } else {
430 QStyle::SubControl last_ctrl = d->buttonDown;
431 d->buttonDown = QStyle::SC_None;
432 if(d->buttonDown != last_ctrl)
433 repaint();
434 }
435 break;
436 default:
437 break;
438 }
439}
440
441void Q3TitleBar::resizeEvent(QResizeEvent *r)
442{
443 QWidget::resizeEvent(r);
444 cutText();
445}
446
447bool Q3TitleBar::isTool() const
448{
449 return (d_func()->flags & Qt::WindowType_Mask) == Qt::Tool;
450}
451
452void Q3TitleBar::paintEvent(QPaintEvent *)
453{
454 Q_D(Q3TitleBar);
455 QStyleOptionTitleBar opt = d->getStyleOption();
456 opt.subControls = QStyle::SC_TitleBarLabel;
457 opt.activeSubControls = d->buttonDown;
458 if (d->flags & Qt::WindowSystemMenuHint) {
459 opt.subControls |= QStyle::SC_TitleBarSysMenu | QStyle::SC_TitleBarCloseButton;
460 if (d->window && (d->flags & Qt::WindowShadeButtonHint)) {
461 if (d->window->isMinimized())
462 opt.subControls |= QStyle::SC_TitleBarUnshadeButton;
463 else
464 opt.subControls |= QStyle::SC_TitleBarShadeButton;
465 }
466 if (d->window && (d->flags & Qt::WindowMinMaxButtonsHint)) {
467 if(d->window && d->window->isMinimized())
468 opt.subControls |= QStyle::SC_TitleBarNormalButton;
469 else
470 opt.subControls |= QStyle::SC_TitleBarMinButton;
471 }
472 if (d->window && (d->flags & Qt::WindowMaximizeButtonHint) && !d->window->isMaximized())
473 opt.subControls |= QStyle::SC_TitleBarMaxButton;
474 }
475 QStyle::SubControl under_mouse = QStyle::SC_None;
476
477 if (underMouse()) {
478 under_mouse = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt,
479 mapFromGlobal(QCursor::pos()), this);
480 opt.activeSubControls |= under_mouse;
481 if (d->pressed)
482 opt.state |= QStyle::State_Sunken;
483 else if(autoRaise())
484 opt.state |= QStyle::State_MouseOver;
485 }
486
487 opt.palette.setCurrentColorGroup(usesActiveColor() ? QPalette::Active : QPalette::Inactive);
488
489 QPainter p(this);
490 if (!windowTitle().isEmpty())
491 opt.titleBarFlags |= Qt::WindowTitleHint;
492 style()->drawComplexControl(QStyle::CC_TitleBar, &opt, &p, this);
493}
494
495void Q3TitleBar::mouseDoubleClickEvent(QMouseEvent *e)
496{
497 Q_D(Q3TitleBar);
498 if (e->button() != Qt::LeftButton) {
499 e->ignore();
500 return;
501 }
502 e->accept();
503 QStyleOptionTitleBar opt = d->getStyleOption();
504 switch (style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(), this)) {
505 case QStyle::SC_TitleBarLabel:
506 emit doubleClicked();
507 break;
508
509 case QStyle::SC_TitleBarSysMenu:
510 if (d->flags & Qt::WStyle_SysMenu)
511 emit doClose();
512 break;
513
514 default:
515 break;
516 }
517}
518
519void Q3TitleBar::cutText()
520{
521 Q_D(Q3TitleBar);
522 QFontMetrics fm(font());
523 QStyleOptionTitleBar opt = d->getStyleOption();
524 int maxw = style()->subControlRect(QStyle::CC_TitleBar, &opt, QStyle::SC_TitleBarLabel,
525 this).width();
526 if (!d->window)
527 return;
528
529 QString txt = d->window->windowTitle();
530 if (style()->styleHint(QStyle::SH_TitleBar_ModifyNotification, 0, this) && d->window
531 && d->window->isWindowModified())
532 txt += QLatin1String(" *");
533
534 QString cuttext = txt;
535 if (fm.width(txt + QLatin1Char('m')) > maxw) {
536 int i = txt.length();
537 int dotlength = fm.width(QLatin1String("..."));
538 while (i>0 && fm.width(txt.left(i)) + dotlength > maxw)
539 i--;
540 if(i != (int)txt.length())
541 cuttext = txt.left(i) + QLatin1String("...");
542 }
543
544 setWindowTitle(cuttext);
545}
546
547
548void Q3TitleBar::leaveEvent(QEvent *)
549{
550 if(autoRaise() && !d_func()->pressed)
551 repaint();
552}
553
554void Q3TitleBar::enterEvent(QEvent *)
555{
556 if(autoRaise() && !d_func()->pressed)
557 repaint();
558 QEvent e(QEvent::Leave);
559 QApplication::sendEvent(parentWidget(), &e);
560}
561
562void Q3TitleBar::setActive(bool active)
563{
564 Q_D(Q3TitleBar);
565 if (d->act == active)
566 return ;
567
568 d->act = active;
569 update();
570}
571
572bool Q3TitleBar::isActive() const
573{
574 return d_func()->act;
575}
576
577bool Q3TitleBar::usesActiveColor() const
578{
579 return (isActive() && isActiveWindow()) ||
580 (!window() && QWidget::window()->isActiveWindow());
581}
582
583QWidget *Q3TitleBar::window() const
584{
585 return d_func()->window;
586}
587
588bool Q3TitleBar::event(QEvent *e)
589{
590 Q_D(Q3TitleBar);
591 if (d->inevent)
592 return QWidget::event(e);
593 d->inevent = true;
594 if (e->type() == QEvent::ApplicationPaletteChange) {
595 d->readColors();
596 return true;
597 } else if (e->type() == QEvent::WindowActivate) {
598 setActive(d->act);
599 } else if (e->type() == QEvent::WindowDeactivate) {
600 bool wasActive = d->act;
601 setActive(false);
602 d->act = wasActive;
603 } else if (e->type() == QEvent::WindowIconChange) {
604 update();
605 } else if (e->type() == QEvent::WindowTitleChange) {
606 cutText();
607 update();
608 }
609
610 d->inevent = false;
611 return QWidget::event(e);
612}
613
614void Q3TitleBar::setMovable(bool b)
615{
616 d_func()->movable = b;
617}
618
619bool Q3TitleBar::isMovable() const
620{
621 return d_func()->movable;
622}
623
624void Q3TitleBar::setAutoRaise(bool b)
625{
626 d_func()->autoraise = b;
627}
628
629bool Q3TitleBar::autoRaise() const
630{
631 return d_func()->autoraise;
632}
633
634QSize Q3TitleBar::sizeHint() const
635{
636 ensurePolished();
637 QStyleOptionTitleBar opt = d_func()->getStyleOption();
638 QRect menur = style()->subControlRect(QStyle::CC_TitleBar, &opt,
639 QStyle::SC_TitleBarSysMenu, this);
640 return QSize(menur.width(), style()->pixelMetric(QStyle::PM_TitleBarHeight, &opt, this));
641}
642
643QT_END_NAMESPACE
644
645#endif //QT_NO_TITLEBAR
Note: See TracBrowser for help on using the repository browser.