strawberry-audio-player-win.../src/core/windows7thumbbar.cpp

202 lines
5.7 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2020-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <QObject>
#include <QWidget>
#include <QList>
#include <QPixmap>
2018-02-27 18:06:05 +01:00
#include <QAction>
2020-10-17 04:21:11 +02:00
#include <QTimer>
2018-02-27 18:06:05 +01:00
2019-08-29 21:32:52 +02:00
#include <windows.h>
#include <commctrl.h>
#include <shobjidl.h>
2020-10-14 22:53:08 +02:00
2019-08-29 21:32:52 +02:00
extern HICON qt_pixmapToWinHICON(const QPixmap &p);
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "windows7thumbbar.h"
2018-02-27 18:06:05 +01:00
const int Windows7ThumbBar::kIconSize = 16;
const int Windows7ThumbBar::kMaxButtonCount = 7;
Windows7ThumbBar::Windows7ThumbBar(QWidget *widget)
: QObject(widget),
widget_(widget),
2020-10-17 04:21:11 +02:00
timer_(new QTimer(this)),
button_created_message_id_(0),
taskbar_list_(nullptr) {
2020-10-17 04:21:11 +02:00
timer_->setSingleShot(true);
timer_->setInterval(300);
2021-01-26 16:48:04 +01:00
QObject::connect(timer_, &QTimer::timeout, this, &Windows7ThumbBar::ActionChanged);
2020-10-17 04:21:11 +02:00
}
2018-02-27 18:06:05 +01:00
2020-10-14 22:53:08 +02:00
void Windows7ThumbBar::SetActions(const QList<QAction*> &actions) {
2018-02-27 18:06:05 +01:00
qLog(Debug) << "Setting actions";
Q_ASSERT(actions.count() <= kMaxButtonCount);
actions_ = actions;
for (QAction *action : actions) {
if (action) {
2021-01-26 16:48:04 +01:00
QObject::connect(action, &QAction::changed, this, &Windows7ThumbBar::ActionChangedTriggered);
2018-02-27 18:06:05 +01:00
}
}
qLog(Debug) << "Done";
}
2020-10-17 03:37:39 +02:00
ITaskbarList3 *Windows7ThumbBar::CreateTaskbarList() {
ITaskbarList3 *taskbar_list = nullptr;
// Copied from win7 SDK shobjidl.h
2022-03-22 21:09:05 +01:00
static const GUID CLSID_ITaskbarList = { 0x56FDF344, 0xFD6D, 0x11d0, { 0x95, 0x8A, 0x00, 0x60, 0x97, 0xC9, 0xA0, 0x90 } };
2020-10-17 03:37:39 +02:00
// Create the taskbar list
2021-10-11 22:28:28 +02:00
HRESULT hr = CoCreateInstance(CLSID_ITaskbarList, nullptr, CLSCTX_ALL, IID_ITaskbarList3, reinterpret_cast<void**>(&taskbar_list));
2020-10-17 03:37:39 +02:00
if (hr != S_OK) {
2022-03-22 21:09:05 +01:00
qLog(Warning) << "Error creating the ITaskbarList3 interface" << Qt::hex << DWORD(hr);
2020-10-17 03:37:39 +02:00
return nullptr;
}
hr = taskbar_list->HrInit();
if (hr != S_OK) {
2022-03-22 21:09:05 +01:00
qLog(Warning) << "Error initializing taskbar list" << Qt::hex << DWORD(hr);
2020-10-17 03:37:39 +02:00
taskbar_list->Release();
return nullptr;
}
return taskbar_list;
2022-03-22 21:19:59 +01:00
2020-10-17 03:37:39 +02:00
}
void Windows7ThumbBar::SetupButton(const QAction *action, THUMBBUTTON *button) {
2018-02-27 18:06:05 +01:00
if (action) {
2018-04-02 18:35:58 +02:00
button->hIcon = qt_pixmapToWinHICON(action->icon().pixmap(Windows7ThumbBar::kIconSize));
2018-02-27 18:06:05 +01:00
button->dwFlags = action->isEnabled() ? THBF_ENABLED : THBF_DISABLED;
// This is unsafe - doesn't obey 260-char restriction
action->text().toWCharArray(button->szTip);
button->szTip[action->text().length()] = L'\0';
2018-02-27 18:06:05 +01:00
if (!action->isVisible()) {
button->dwFlags = THUMBBUTTONFLAGS(button->dwFlags | THBF_HIDDEN);
}
2020-10-14 22:53:58 +02:00
button->dwMask = THUMBBUTTONMASK(THB_ICON | THB_TOOLTIP | THB_FLAGS);
2018-02-27 18:06:05 +01:00
}
else {
2020-10-14 22:53:08 +02:00
button->hIcon = nullptr;
2018-02-27 18:06:05 +01:00
button->szTip[0] = L'\0';
button->dwFlags = THBF_NOBACKGROUND;
button->dwMask = THUMBBUTTONMASK(THB_FLAGS);
}
2019-08-29 21:32:52 +02:00
2018-02-27 18:06:05 +01:00
}
void Windows7ThumbBar::HandleWinEvent(MSG *msg) {
if (button_created_message_id_ == 0) {
// Compute the value for the TaskbarButtonCreated message
2020-12-12 12:51:01 +01:00
button_created_message_id_ = RegisterWindowMessageA(LPCSTR("TaskbarButtonCreated"));
2018-02-27 18:06:05 +01:00
qLog(Debug) << "TaskbarButtonCreated message ID registered" << button_created_message_id_;
}
if (msg->message == button_created_message_id_) {
if (taskbar_list_) {
taskbar_list_->Release();
}
taskbar_list_ = CreateTaskbarList();
if (!taskbar_list_) return;
2018-02-27 18:06:05 +01:00
// Add the buttons
THUMBBUTTON buttons[kMaxButtonCount];
2021-08-23 21:21:08 +02:00
for (int i = 0; i < actions_.count(); ++i) {
2018-02-27 18:06:05 +01:00
const QAction *action = actions_[i];
THUMBBUTTON *button = &buttons[i];
button->iId = i;
SetupButton(action, button);
}
2020-10-17 03:37:39 +02:00
qLog(Debug) << "Adding" << actions_.count() << "buttons";
HRESULT hr = taskbar_list_->ThumbBarAddButtons(reinterpret_cast<HWND>(widget_->winId()), actions_.count(), buttons);
2020-10-14 22:53:08 +02:00
if (hr != S_OK) {
2022-03-22 21:09:05 +01:00
qLog(Debug) << "Failed to add buttons" << Qt::hex << DWORD(hr);
2020-10-14 22:53:08 +02:00
}
2020-10-17 03:37:39 +02:00
2021-08-23 21:21:08 +02:00
for (int i = 0; i < actions_.count(); ++i) {
2020-10-14 22:53:08 +02:00
if (buttons[i].hIcon) {
2021-07-11 09:49:38 +02:00
DestroyIcon(buttons[i].hIcon);
2020-10-14 22:53:08 +02:00
}
2018-02-27 18:06:05 +01:00
}
2020-10-17 03:37:39 +02:00
2019-09-19 17:44:14 +02:00
}
else if (msg->message == WM_COMMAND) {
2018-02-27 18:06:05 +01:00
const int button_id = LOWORD(msg->wParam);
if (button_id >= 0 && button_id < actions_.count()) {
if (actions_[button_id]) {
qLog(Debug) << "Button activated";
actions_[button_id]->activate(QAction::Trigger);
}
}
}
}
2020-10-17 04:21:11 +02:00
void Windows7ThumbBar::ActionChangedTriggered() {
if (!timer_->isActive()) timer_->start();
}
2018-02-27 18:06:05 +01:00
void Windows7ThumbBar::ActionChanged() {
if (!taskbar_list_) return;
2020-10-17 03:37:39 +02:00
qLog(Debug) << "Updating" << actions_.count() << "buttons";
2018-02-27 18:06:05 +01:00
THUMBBUTTON buttons[kMaxButtonCount];
2021-08-23 21:21:08 +02:00
for (int i = 0; i < actions_.count(); ++i) {
2020-10-14 22:53:08 +02:00
QAction *action = actions_[i];
2018-02-27 18:06:05 +01:00
THUMBBUTTON *button = &buttons[i];
button->iId = i;
SetupButton(action, button);
}
HRESULT hr = taskbar_list_->ThumbBarUpdateButtons(reinterpret_cast<HWND>(widget_->winId()), actions_.count(), buttons);
2020-10-17 03:37:39 +02:00
if (hr != S_OK) {
2022-03-22 21:09:05 +01:00
qLog(Debug) << "Failed to update buttons" << Qt::hex << DWORD(hr);
2020-10-17 03:37:39 +02:00
}
2020-10-14 22:53:08 +02:00
2021-08-23 21:21:08 +02:00
for (int i = 0; i < actions_.count(); ++i) {
2020-10-14 22:53:08 +02:00
if (buttons[i].hIcon) {
DestroyIcon(buttons[i].hIcon);
}
}
2018-02-27 18:06:05 +01:00
}