From af13c79b30c1c8d5a5de6fd0e942c57b53fab9b4 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 16 Oct 2012 13:06:43 +0300 Subject: Implement setWindowIcon() for Windows. Platform implementation for QWindow::setWindowIcon() was missing from Windows platform plugin. Task-number: QTBUG-27175 Change-Id: I6807d1ded057b7788a9f2fa5b143e212a666029b Reviewed-by: Friedemann Kleint --- src/plugins/platforms/windows/qwindowswindow.cpp | 39 ++++++++++++++++++++++-- src/plugins/platforms/windows/qwindowswindow.h | 16 ++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp index f3830eb962..a31b09d2bf 100644 --- a/src/plugins/platforms/windows/qwindowswindow.cpp +++ b/src/plugins/platforms/windows/qwindowswindow.cpp @@ -61,6 +61,8 @@ QT_BEGIN_NAMESPACE +Q_GUI_EXPORT HICON qt_pixmapToWinHICON(const QPixmap &); + static QByteArray debugWinStyle(DWORD style) { QByteArray rc = "0x"; @@ -699,13 +701,15 @@ QWindowsWindow::QWindowsWindow(QWindow *aWindow, const WindowData &data) : m_cursor(QWindowsScreen::screenOf(aWindow)->windowsCursor()->standardWindowCursor()), m_dropTarget(0), m_savedStyle(0), - m_format(aWindow->format()) + m_format(aWindow->format()), #ifdef QT_OPENGL_ES_2 - , m_eglSurface(0) + m_eglSurface(0), #endif #ifdef Q_OS_WINCE - , m_previouslyHidden(false) + m_previouslyHidden(false), #endif + m_iconBig(0), + m_iconSmall(0) { if (aWindow->surfaceType() == QWindow::OpenGLSurface) setFlag(OpenGLSurface); @@ -730,6 +734,7 @@ QWindowsWindow::QWindowsWindow(QWindow *aWindow, const WindowData &data) : QWindowsWindow::~QWindowsWindow() { destroyWindow(); + destroyIcon(); } void QWindowsWindow::destroyWindow() @@ -1754,4 +1759,32 @@ QByteArray QWindowsWindow::debugWindowFlags(Qt::WindowFlags wf) return rc; } +static HICON createHIcon(const QIcon &icon, int xSize, int ySize) +{ + if (!icon.isNull()) { + const QPixmap pm = icon.pixmap(icon.actualSize(QSize(xSize, ySize))); + if (!pm.isNull()) + return qt_pixmapToWinHICON(pm); + } + return 0; +} + +void QWindowsWindow::setWindowIcon(const QIcon &icon) +{ + if (m_data.hwnd) { + destroyIcon(); + + m_iconSmall = createHIcon(icon, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)); + m_iconBig = createHIcon(icon, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)); + + if (m_iconBig) { + SendMessage(m_data.hwnd, WM_SETICON, 0 /* ICON_SMALL */, (LPARAM)m_iconSmall); + SendMessage(m_data.hwnd, WM_SETICON, 1 /* ICON_BIG */, (LPARAM)m_iconBig); + } else { + SendMessage(m_data.hwnd, WM_SETICON, 0 /* ICON_SMALL */, (LPARAM)m_iconSmall); + SendMessage(m_data.hwnd, WM_SETICON, 1 /* ICON_BIG */, (LPARAM)m_iconSmall); + } + } +} + QT_END_NAMESPACE diff --git a/src/plugins/platforms/windows/qwindowswindow.h b/src/plugins/platforms/windows/qwindowswindow.h index 3b7666cf32..5d898a2d41 100644 --- a/src/plugins/platforms/windows/qwindowswindow.h +++ b/src/plugins/platforms/windows/qwindowswindow.h @@ -238,6 +238,7 @@ public: void setEnabled(bool enabled); bool isEnabled() const; + void setWindowIcon(const QIcon &icon); #ifndef Q_OS_WINCE void alertWindow(int durationMs = 0); @@ -259,6 +260,7 @@ private: void unregisterDropSite(); void handleGeometryChange(); void handleWindowStateChange(Qt::WindowState state); + inline void destroyIcon(); mutable WindowData m_data; mutable unsigned m_flags; @@ -277,6 +279,8 @@ private: #ifdef Q_OS_WINCE bool m_previouslyHidden; #endif + HICON m_iconSmall; + HICON m_iconBig; }; // Conveniences for window frames. @@ -346,6 +350,18 @@ void QWindowsWindow::setUserDataOf(HWND hwnd, void *ud) SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG_PTR(ud)); } +inline void QWindowsWindow::destroyIcon() +{ + if (m_iconBig) { + DestroyIcon(m_iconBig); + m_iconBig = 0; + } + if (m_iconSmall) { + DestroyIcon(m_iconSmall); + m_iconSmall = 0; + } +} + QT_END_NAMESPACE #endif // QWINDOWSWINDOW_H -- cgit v1.2.3