From 5146cdfa2fa690368777809ad0b70f7926816aa3 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Wed, 14 Oct 2020 22:53:08 +0200 Subject: [PATCH] Improve windows thumbbar code --- src/core/windows7thumbbar.cpp | 42 +++++++++++++++++++++-------------- src/core/windows7thumbbar.h | 20 ++++++++--------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/core/windows7thumbbar.cpp b/src/core/windows7thumbbar.cpp index f1902efdb..60a2a0ef7 100644 --- a/src/core/windows7thumbbar.cpp +++ b/src/core/windows7thumbbar.cpp @@ -30,9 +30,11 @@ #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0600 #endif + #include #include #include + extern HICON qt_pixmapToWinHICON(const QPixmap &p); #include "core/logging.h" @@ -47,7 +49,7 @@ Windows7ThumbBar::Windows7ThumbBar(QWidget *widget) button_created_message_id_(0), taskbar_list_(nullptr) {} -void Windows7ThumbBar::SetActions(const QList& actions) { +void Windows7ThumbBar::SetActions(const QList &actions) { qLog(Debug) << "Setting actions"; Q_ASSERT(actions.count() <= kMaxButtonCount); @@ -74,10 +76,10 @@ static void SetupButton(const QAction *action, THUMBBUTTON *button) { if (!action->isVisible()) { button->dwFlags = THUMBBUTTONFLAGS(button->dwFlags | THBF_HIDDEN); } - button->dwMask = THUMBBUTTONMASK(THB_ICON | THB_TOOLTIP | THB_FLAGS); + button->dwMask = THUMBBUTTONMASK(THB_ICON |THB_TOOLTIP | THB_FLAGS); } else { - button->hIcon = 0; + button->hIcon = nullptr; button->szTip[0] = L'\0'; button->dwFlags = THBF_NOBACKGROUND; button->dwMask = THUMBBUTTONMASK(THB_FLAGS); @@ -99,7 +101,7 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) { // Unref the old taskbar list if we had one if (taskbar_list_) { qLog(Debug) << "Releasing old taskbar list"; - reinterpret_cast(taskbar_list_)->Release(); + taskbar_list_->Release(); taskbar_list_ = nullptr; } @@ -112,11 +114,10 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) { return; } - ITaskbarList3 *taskbar_list = reinterpret_cast(taskbar_list_); - hr = taskbar_list->HrInit(); + hr = taskbar_list_->HrInit(); if (hr != S_OK) { qLog(Warning) << "Error initialising taskbar list" << Qt::hex << DWORD (hr); - taskbar_list->Release(); + taskbar_list_->Release(); taskbar_list_ = nullptr; return; } @@ -124,7 +125,7 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) { // Add the buttons qLog(Debug) << "Initialising" << actions_.count() << "buttons"; THUMBBUTTON buttons[kMaxButtonCount]; - for (int i = 0; i < actions_.count(); ++i) { + for (int i = 0 ; i < actions_.count() ; ++i) { const QAction *action = actions_[i]; THUMBBUTTON *button = &buttons[i]; button->iId = i; @@ -132,12 +133,14 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) { } qLog(Debug) << "Adding buttons"; - hr = taskbar_list->ThumbBarAddButtons((HWND)widget_->winId(), actions_.count(), buttons); - if (hr != S_OK) + hr = taskbar_list_->ThumbBarAddButtons(reinterpret_cast(widget_->winId()), actions_.count(), buttons); + if (hr != S_OK) { qLog(Debug) << "Failed to add buttons" << Qt::hex << DWORD (hr); - for (int i = 0; i < actions_.count(); i++) { - if (buttons[i].hIcon) + } + for (int i = 0 ; i < actions_.count() ; ++i) { + if (buttons[i].hIcon) { DestroyIcon (buttons[i].hIcon); + } } } else if (msg->message == WM_COMMAND) { @@ -156,18 +159,23 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) { void Windows7ThumbBar::ActionChanged() { if (!taskbar_list_) return; - ITaskbarList3 *taskbar_list = reinterpret_cast(taskbar_list_); THUMBBUTTON buttons[kMaxButtonCount]; - for (int i = 0; i < actions_.count(); ++i) { - const QAction *action = actions_[i]; + for (int i = 0 ; i < actions_.count() ; ++i) { + QAction *action = actions_[i]; THUMBBUTTON *button = &buttons[i]; button->iId = i; SetupButton(action, button); - if (buttons->hIcon) DestroyIcon(buttons->hIcon); + } - taskbar_list->ThumbBarUpdateButtons((HWND)widget_->winId(), actions_.count(), buttons); + taskbar_list_->ThumbBarUpdateButtons(reinterpret_cast(widget_->winId()), actions_.count(), buttons); + + for (int i = 0 ; i < actions_.count() ; ++i) { + if (buttons[i].hIcon) { + DestroyIcon(buttons[i].hIcon); + } + } } diff --git a/src/core/windows7thumbbar.h b/src/core/windows7thumbbar.h index e8cd47f1b..d40cf00d4 100644 --- a/src/core/windows7thumbbar.h +++ b/src/core/windows7thumbbar.h @@ -23,22 +23,21 @@ #include "config.h" +#include +#include + #include #include #include #include #include -#ifndef Q_OS_WIN32 - typedef void MSG; -#endif // Q_OS_WIN32 - class Windows7ThumbBar : public QObject { Q_OBJECT -public: - // Creates a list of buttons in the taskbar icon for this window. Does nothing and is safe to use on other operating systems too. - explicit Windows7ThumbBar(QWidget *widget = 0); + public: + // Creates a list of buttons in the taskbar icon for this window. + explicit Windows7ThumbBar(QWidget *widget = nullptr); static const int kIconSize; static const int kMaxButtonCount; @@ -49,17 +48,16 @@ public: // Call this from the parent's winEvent() function. void HandleWinEvent(MSG *msg); -private slots: + private slots: void ActionChanged(); -private: + private: QWidget *widget_; QList actions_; unsigned int button_created_message_id_; - // Really an ITaskbarList3* but I don't want to have to include windows.h here - void *taskbar_list_; + ITaskbarList3 *taskbar_list_; }; #endif // WINDOWS7THUMBBAR_H