Improve windows thumbbar code
This commit is contained in:
@@ -30,9 +30,11 @@
|
|||||||
#ifndef _WIN32_WINNT
|
#ifndef _WIN32_WINNT
|
||||||
#define _WIN32_WINNT 0x0600
|
#define _WIN32_WINNT 0x0600
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <commctrl.h>
|
#include <commctrl.h>
|
||||||
#include <shobjidl.h>
|
#include <shobjidl.h>
|
||||||
|
|
||||||
extern HICON qt_pixmapToWinHICON(const QPixmap &p);
|
extern HICON qt_pixmapToWinHICON(const QPixmap &p);
|
||||||
|
|
||||||
#include "core/logging.h"
|
#include "core/logging.h"
|
||||||
@@ -47,7 +49,7 @@ Windows7ThumbBar::Windows7ThumbBar(QWidget *widget)
|
|||||||
button_created_message_id_(0),
|
button_created_message_id_(0),
|
||||||
taskbar_list_(nullptr) {}
|
taskbar_list_(nullptr) {}
|
||||||
|
|
||||||
void Windows7ThumbBar::SetActions(const QList<QAction*>& actions) {
|
void Windows7ThumbBar::SetActions(const QList<QAction*> &actions) {
|
||||||
|
|
||||||
qLog(Debug) << "Setting actions";
|
qLog(Debug) << "Setting actions";
|
||||||
Q_ASSERT(actions.count() <= kMaxButtonCount);
|
Q_ASSERT(actions.count() <= kMaxButtonCount);
|
||||||
@@ -74,10 +76,10 @@ static void SetupButton(const QAction *action, THUMBBUTTON *button) {
|
|||||||
if (!action->isVisible()) {
|
if (!action->isVisible()) {
|
||||||
button->dwFlags = THUMBBUTTONFLAGS(button->dwFlags | THBF_HIDDEN);
|
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 {
|
else {
|
||||||
button->hIcon = 0;
|
button->hIcon = nullptr;
|
||||||
button->szTip[0] = L'\0';
|
button->szTip[0] = L'\0';
|
||||||
button->dwFlags = THBF_NOBACKGROUND;
|
button->dwFlags = THBF_NOBACKGROUND;
|
||||||
button->dwMask = THUMBBUTTONMASK(THB_FLAGS);
|
button->dwMask = THUMBBUTTONMASK(THB_FLAGS);
|
||||||
@@ -99,7 +101,7 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) {
|
|||||||
// Unref the old taskbar list if we had one
|
// Unref the old taskbar list if we had one
|
||||||
if (taskbar_list_) {
|
if (taskbar_list_) {
|
||||||
qLog(Debug) << "Releasing old taskbar list";
|
qLog(Debug) << "Releasing old taskbar list";
|
||||||
reinterpret_cast<ITaskbarList3*>(taskbar_list_)->Release();
|
taskbar_list_->Release();
|
||||||
taskbar_list_ = nullptr;
|
taskbar_list_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,11 +114,10 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITaskbarList3 *taskbar_list = reinterpret_cast<ITaskbarList3*>(taskbar_list_);
|
hr = taskbar_list_->HrInit();
|
||||||
hr = taskbar_list->HrInit();
|
|
||||||
if (hr != S_OK) {
|
if (hr != S_OK) {
|
||||||
qLog(Warning) << "Error initialising taskbar list" << Qt::hex << DWORD (hr);
|
qLog(Warning) << "Error initialising taskbar list" << Qt::hex << DWORD (hr);
|
||||||
taskbar_list->Release();
|
taskbar_list_->Release();
|
||||||
taskbar_list_ = nullptr;
|
taskbar_list_ = nullptr;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -124,7 +125,7 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) {
|
|||||||
// Add the buttons
|
// Add the buttons
|
||||||
qLog(Debug) << "Initialising" << actions_.count() << "buttons";
|
qLog(Debug) << "Initialising" << actions_.count() << "buttons";
|
||||||
THUMBBUTTON buttons[kMaxButtonCount];
|
THUMBBUTTON buttons[kMaxButtonCount];
|
||||||
for (int i = 0; i < actions_.count(); ++i) {
|
for (int i = 0 ; i < actions_.count() ; ++i) {
|
||||||
const QAction *action = actions_[i];
|
const QAction *action = actions_[i];
|
||||||
THUMBBUTTON *button = &buttons[i];
|
THUMBBUTTON *button = &buttons[i];
|
||||||
button->iId = i;
|
button->iId = i;
|
||||||
@@ -132,12 +133,14 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
qLog(Debug) << "Adding buttons";
|
qLog(Debug) << "Adding buttons";
|
||||||
hr = taskbar_list->ThumbBarAddButtons((HWND)widget_->winId(), actions_.count(), buttons);
|
hr = taskbar_list_->ThumbBarAddButtons(reinterpret_cast<HWND>(widget_->winId()), actions_.count(), buttons);
|
||||||
if (hr != S_OK)
|
if (hr != S_OK) {
|
||||||
qLog(Debug) << "Failed to add buttons" << Qt::hex << DWORD (hr);
|
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);
|
DestroyIcon (buttons[i].hIcon);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (msg->message == WM_COMMAND) {
|
else if (msg->message == WM_COMMAND) {
|
||||||
@@ -156,18 +159,23 @@ void Windows7ThumbBar::HandleWinEvent(MSG *msg) {
|
|||||||
void Windows7ThumbBar::ActionChanged() {
|
void Windows7ThumbBar::ActionChanged() {
|
||||||
|
|
||||||
if (!taskbar_list_) return;
|
if (!taskbar_list_) return;
|
||||||
ITaskbarList3 *taskbar_list = reinterpret_cast<ITaskbarList3*>(taskbar_list_);
|
|
||||||
|
|
||||||
THUMBBUTTON buttons[kMaxButtonCount];
|
THUMBBUTTON buttons[kMaxButtonCount];
|
||||||
for (int i = 0; i < actions_.count(); ++i) {
|
for (int i = 0 ; i < actions_.count() ; ++i) {
|
||||||
const QAction *action = actions_[i];
|
QAction *action = actions_[i];
|
||||||
THUMBBUTTON *button = &buttons[i];
|
THUMBBUTTON *button = &buttons[i];
|
||||||
|
|
||||||
button->iId = i;
|
button->iId = i;
|
||||||
SetupButton(action, button);
|
SetupButton(action, button);
|
||||||
if (buttons->hIcon) DestroyIcon(buttons->hIcon);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
taskbar_list->ThumbBarUpdateButtons((HWND)widget_->winId(), actions_.count(), buttons);
|
taskbar_list_->ThumbBarUpdateButtons(reinterpret_cast<HWND>(widget_->winId()), actions_.count(), buttons);
|
||||||
|
|
||||||
|
for (int i = 0 ; i < actions_.count() ; ++i) {
|
||||||
|
if (buttons[i].hIcon) {
|
||||||
|
DestroyIcon(buttons[i].hIcon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,22 +23,21 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <shobjidl.h>
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QAction>
|
#include <QAction>
|
||||||
|
|
||||||
#ifndef Q_OS_WIN32
|
|
||||||
typedef void MSG;
|
|
||||||
#endif // Q_OS_WIN32
|
|
||||||
|
|
||||||
class Windows7ThumbBar : public QObject {
|
class Windows7ThumbBar : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
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.
|
// Creates a list of buttons in the taskbar icon for this window.
|
||||||
explicit Windows7ThumbBar(QWidget *widget = 0);
|
explicit Windows7ThumbBar(QWidget *widget = nullptr);
|
||||||
|
|
||||||
static const int kIconSize;
|
static const int kIconSize;
|
||||||
static const int kMaxButtonCount;
|
static const int kMaxButtonCount;
|
||||||
@@ -49,17 +48,16 @@ public:
|
|||||||
// Call this from the parent's winEvent() function.
|
// Call this from the parent's winEvent() function.
|
||||||
void HandleWinEvent(MSG *msg);
|
void HandleWinEvent(MSG *msg);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void ActionChanged();
|
void ActionChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWidget *widget_;
|
QWidget *widget_;
|
||||||
QList<QAction*> actions_;
|
QList<QAction*> actions_;
|
||||||
|
|
||||||
unsigned int button_created_message_id_;
|
unsigned int button_created_message_id_;
|
||||||
|
|
||||||
// Really an ITaskbarList3* but I don't want to have to include windows.h here
|
ITaskbarList3 *taskbar_list_;
|
||||||
void *taskbar_list_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WINDOWS7THUMBBAR_H
|
#endif // WINDOWS7THUMBBAR_H
|
||||||
|
|||||||
Reference in New Issue
Block a user