📄 qwindowsstyle.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qwindowsstyle.h"#include "qwindowsstyle_p.h"#include <private/qpixmap_p.h>#if !defined(QT_NO_STYLE_WINDOWS) || defined(QT_PLUGIN)#include "qlibrary.h"#include "qapplication.h"#include "qbitmap.h"#include "qdrawutil.h" // for now#include "qevent.h"#include "qmenu.h"#include "qmenubar.h"#include "qpaintengine.h"#include "qpainter.h"#include "qprogressbar.h"#include "qrubberband.h"#include "qstyleoption.h"#include "qtabbar.h"#include "qwidget.h"#include "qdebug.h"#include "qmainwindow.h"#include "qfile.h"#include "qtextstream.h"#include "qpixmapcache.h"#include "qwizard.h"#ifdef Q_WS_X11#include "qfileinfo.h"#include "qdir.h"#include <private/qt_x11_p.h>#endif#if defined(Q_WS_WIN)#include "qt_windows.h"# ifndef COLOR_GRADIENTACTIVECAPTION# define COLOR_GRADIENTACTIVECAPTION 27# endif# ifndef COLOR_GRADIENTINACTIVECAPTION# define COLOR_GRADIENTINACTIVECAPTION 28# endiftypedef struct{ DWORD cbSize; HICON hIcon; int iSysImageIndex; int iIcon; WCHAR szPath[MAX_PATH];} QSHSTOCKICONINFO;#define _SHGFI_SMALLICON 0x000000001#define _SHGFI_LARGEICON 0x000000000#define _SHGFI_ICON 0x000000100#define _SIID_SHIELD 77typedef HRESULT (WINAPI *PtrSHGetStockIconInfo)(int siid, int uFlags, QSHSTOCKICONINFO *psii);static PtrSHGetStockIconInfo pSHGetStockIconInfo = 0;#endif //Q_WS_WIN#include <limits.h>static const int windowsItemFrame = 2; // menu item frame widthstatic const int windowsSepHeight = 9; // separator item heightstatic const int windowsItemHMargin = 3; // menu item hor text marginstatic const int windowsItemVMargin = 2; // menu item ver text marginstatic const int windowsArrowHMargin = 6; // arrow horizontal marginstatic const int windowsTabSpacing = 12; // space between text and tabstatic const int windowsCheckMarkHMargin = 2; // horiz. margins of check markstatic const int windowsRightBorder = 15; // right border on windowsstatic const int windowsCheckMarkWidth = 12; // checkmarks width on windowsstatic bool use2000style = true;enum QSliderDirection { SlUp, SlDown, SlLeft, SlRight };/* \internal*/QWindowsStylePrivate::QWindowsStylePrivate() : alt_down(false), menuBarTimer(0), animationFps(10), animateTimer(0), animateStep(0){#ifdef Q_WS_WIN if ((QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA && QSysInfo::WindowsVersion < QSysInfo::WV_NT_based)) { QLibrary shellLib(QLatin1String("shell32")); pSHGetStockIconInfo = (PtrSHGetStockIconInfo)shellLib.resolve("SHGetStockIconInfo"); }#endif}// Returns true if the toplevel parent of \a widget has seen the Alt-keybool QWindowsStylePrivate::hasSeenAlt(const QWidget *widget) const{ widget = widget->window(); return seenAlt.contains(widget);}/*! \reimp*/void QWindowsStyle::timerEvent(QTimerEvent *event){#ifndef QT_NO_PROGRESSBAR Q_D(QWindowsStyle); if (event->timerId() == d->animateTimer) { Q_ASSERT(d->animationFps> 0); d->animateStep = d->startTime.elapsed() / (1000 / d->animationFps); foreach (QProgressBar *bar, d->bars) { if ((bar->minimum() == 0 && bar->maximum() == 0)) bar->update(); } }#endif // QT_NO_PROGRESSBAR event->ignore();}/*! \reimp*/bool QWindowsStyle::eventFilter(QObject *o, QEvent *e){ // Records Alt- and Focus events if (!o->isWidgetType()) return QObject::eventFilter(o, e); QWidget *widget = ::qobject_cast<QWidget*>(o); Q_D(QWindowsStyle); switch(e->type()) { case QEvent::KeyPress: if (static_cast<QKeyEvent *>(e)->key() == Qt::Key_Alt) { widget = widget->window(); // Alt has been pressed - find all widgets that care QList<QWidget *> l = qFindChildren<QWidget *>(widget); for (int pos=0 ; pos < l.size() ; ++pos) { QWidget *w = l.at(pos); if (w->isWindow() || !w->isVisible() || w->style()->styleHint(SH_UnderlineShortcut, 0, w)) l.removeAt(pos); } // Update states before repainting d->seenAlt.append(widget); d->alt_down = true; // Repaint all relevant widgets for (int pos = 0; pos < l.size(); ++pos) l.at(pos)->update(); } break; case QEvent::KeyRelease: if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Alt) { widget = widget->window(); // Update state and repaint the menu bars. d->alt_down = false;#ifndef QT_NO_MENUBAR QList<QMenuBar *> l = qFindChildren<QMenuBar *>(widget); for (int i = 0; i < l.size(); ++i) l.at(i)->update();#endif } break; case QEvent::Close: // Reset widget when closing d->seenAlt.removeAll(widget); d->seenAlt.removeAll(widget->window()); break;#ifndef QT_NO_PROGRESSBAR case QEvent::StyleChange: case QEvent::Show: if (QProgressBar *bar = qobject_cast<QProgressBar *>(o)) { d->bars << bar; if (d->bars.size() == 1) { Q_ASSERT(d->animationFps> 0); d->animateTimer = startTimer(1000 / d->animationFps); } } break; case QEvent::Destroy: case QEvent::Hide: // reinterpret_cast because there is no type info when getting // the destroy event. We know that it is a QProgressBar. if (QProgressBar *bar = reinterpret_cast<QProgressBar *>(o)) { d->bars.removeAll(bar); if (d->bars.isEmpty() && d->animateTimer) { killTimer(d->animateTimer); d->animateTimer = 0; } } break;#endif // QT_NO_PROGRESSBAR default: break; } return QCommonStyle::eventFilter(o, e);}/*! \class QWindowsStyle qwindowsstyle.h \brief The QWindowsStyle class provides a Microsoft Windows-like look and feel. \ingroup appearance This style is Qt's default GUI style on Windows. \img qwindowsstyle.png \sa QWindowsXPStyle, QMacStyle, QPlastiqueStyle, QCDEStyle, QMotifStyle*//*! Constructs a QWindowsStyle object.*/QWindowsStyle::QWindowsStyle() : QCommonStyle(*new QWindowsStylePrivate){#if defined(Q_OS_WIN32) use2000style = QSysInfo::WindowsVersion != QSysInfo::WV_NT && QSysInfo::WindowsVersion != QSysInfo::WV_95;#endif}/*! \internal Constructs a QWindowsStyle object.*/QWindowsStyle::QWindowsStyle(QWindowsStylePrivate &dd) : QCommonStyle(dd){#if defined(Q_OS_WIN32) use2000style = QSysInfo::WindowsVersion != QSysInfo::WV_NT && QSysInfo::WindowsVersion != QSysInfo::WV_95;#endif}/*! Destroys the QWindowsStyle object. */QWindowsStyle::~QWindowsStyle(){}#ifdef Q_WS_WINstatic inline QRgb colorref2qrgb(COLORREF col){ return qRgb(GetRValue(col), GetGValue(col), GetBValue(col));}#endif/*! \reimp */void QWindowsStyle::polish(QApplication *app){ QWindowsStylePrivate *d = const_cast<QWindowsStylePrivate*>(d_func()); // We only need the overhead when shortcuts are sometimes hidden if (!styleHint(SH_UnderlineShortcut, 0) && app) app->installEventFilter(this); d->activeCaptionColor = app->palette().highlight().color(); d->activeGradientCaptionColor = app->palette().highlight() .color(); d->inactiveCaptionColor = app->palette().dark().color(); d->inactiveGradientCaptionColor = app->palette().dark().color(); d->inactiveCaptionText = app->palette().background().color();#if defined(Q_WS_WIN) //fetch native title bar colors if(app->desktopSettingsAware()){ DWORD activeCaption = GetSysColor(COLOR_ACTIVECAPTION); DWORD gradientActiveCaption = GetSysColor(COLOR_GRADIENTACTIVECAPTION); DWORD inactiveCaption = GetSysColor(COLOR_INACTIVECAPTION); DWORD gradientInactiveCaption = GetSysColor(COLOR_GRADIENTINACTIVECAPTION); DWORD inactiveCaptionText = GetSysColor(COLOR_INACTIVECAPTIONTEXT); d->activeCaptionColor = colorref2qrgb(activeCaption); d->activeGradientCaptionColor = colorref2qrgb(gradientActiveCaption); d->inactiveCaptionColor = colorref2qrgb(inactiveCaption); d->inactiveGradientCaptionColor = colorref2qrgb(gradientInactiveCaption); d->inactiveCaptionText = colorref2qrgb(inactiveCaptionText); }#endif}/*! \reimp */void QWindowsStyle::unpolish(QApplication *app){ app->removeEventFilter(this);}/*! \reimp */void QWindowsStyle::polish(QWidget *widget){ QCommonStyle::polish(widget);#ifndef QT_NO_PROGRESSBAR if (qobject_cast<QProgressBar *>(widget)) widget->installEventFilter(this);#endif}/*! \reimp */void QWindowsStyle::unpolish(QWidget *widget){ QCommonStyle::unpolish(widget);#ifndef QT_NO_PROGRESSBAR if (QProgressBar *bar=qobject_cast<QProgressBar *>(widget)) { Q_D(QWindowsStyle); widget->removeEventFilter(this); d->bars.removeAll(bar); }#endif}/*! \reimp */void QWindowsStyle::polish(QPalette &pal){ QCommonStyle::polish(pal);}/*! \reimp*/int QWindowsStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QWidget *widget) const{ int ret; switch (pm) { case PM_ButtonDefaultIndicator: case PM_ButtonShiftHorizontal: case PM_ButtonShiftVertical: ret = 1; break;#ifndef QT_NO_TABBAR case PM_TabBarTabShiftHorizontal: ret = 0; break; case PM_TabBarTabShiftVertical: ret = 2; break;#endif case PM_MaximumDragDistance: ret = 60; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -