2011-01-16 01:39:51 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "windows7thumbbar.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2011-01-16 01:39:51 +01:00
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
2014-02-07 16:34:20 +01:00
|
|
|
#define _WIN32_WINNT 0x0600
|
|
|
|
#include <windows.h>
|
|
|
|
#include <commctrl.h>
|
|
|
|
#include <shobjidl.h>
|
|
|
|
#endif // Q_OS_WIN32
|
2011-01-16 01:39:51 +01:00
|
|
|
|
|
|
|
const int Windows7ThumbBar::kIconSize = 16;
|
|
|
|
const int Windows7ThumbBar::kMaxButtonCount = 7;
|
|
|
|
|
|
|
|
Windows7ThumbBar::Windows7ThumbBar(QWidget* widget)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(widget),
|
|
|
|
widget_(widget),
|
|
|
|
button_created_message_id_(0),
|
|
|
|
taskbar_list_(nullptr) {}
|
2011-01-16 01:39:51 +01:00
|
|
|
|
|
|
|
void Windows7ThumbBar::SetActions(const QList<QAction*>& actions) {
|
|
|
|
#ifdef Q_OS_WIN32
|
2011-04-24 20:07:09 +02:00
|
|
|
qLog(Debug) << "Setting actions";
|
2011-01-16 01:39:51 +01:00
|
|
|
Q_ASSERT(actions.count() <= kMaxButtonCount);
|
|
|
|
|
|
|
|
actions_ = actions;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QAction* action : actions) {
|
2011-01-16 01:39:51 +01:00
|
|
|
if (action) {
|
|
|
|
connect(action, SIGNAL(changed()), SLOT(ActionChanged()));
|
|
|
|
}
|
|
|
|
}
|
2011-04-24 20:07:09 +02:00
|
|
|
qLog(Debug) << "Done";
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // Q_OS_WIN32
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
static void SetupButton(const QAction* action, THUMBBUTTON* button) {
|
|
|
|
if (action) {
|
2014-02-07 16:34:20 +01:00
|
|
|
button->hIcon =
|
|
|
|
action->icon().pixmap(Windows7ThumbBar::kIconSize).toWinHICON();
|
2011-01-16 01:39:51 +01:00
|
|
|
button->dwFlags = action->isEnabled() ? THBF_ENABLED : THBF_DISABLED;
|
2012-01-16 23:58:25 +01:00
|
|
|
// This is unsafe - doesn't obey 260-char restriction
|
2011-01-16 01:39:51 +01:00
|
|
|
action->text().toWCharArray(button->szTip);
|
2012-01-16 23:58:25 +01:00
|
|
|
button->szTip[action->text().count()] = L'\0';
|
2011-01-16 01:39:51 +01:00
|
|
|
|
|
|
|
if (!action->isVisible()) {
|
|
|
|
button->dwFlags = THUMBBUTTONFLAGS(button->dwFlags | THBF_HIDDEN);
|
|
|
|
}
|
2012-01-16 23:58:25 +01:00
|
|
|
button->dwMask = THUMBBUTTONMASK(THB_ICON | THB_TOOLTIP | THB_FLAGS);
|
2011-01-16 01:39:51 +01:00
|
|
|
} else {
|
|
|
|
button->hIcon = 0;
|
2012-01-16 18:45:06 +01:00
|
|
|
button->szTip[0] = L'\0';
|
2011-01-16 01:39:51 +01:00
|
|
|
button->dwFlags = THBF_NOBACKGROUND;
|
2012-01-16 23:58:25 +01:00
|
|
|
button->dwMask = THUMBBUTTONMASK(THB_FLAGS);
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // Q_OS_WIN32
|
2011-01-16 01:39:51 +01:00
|
|
|
|
|
|
|
void Windows7ThumbBar::HandleWinEvent(MSG* msg) {
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
if (button_created_message_id_ == 0) {
|
|
|
|
// Compute the value for the TaskbarButtonCreated message
|
|
|
|
button_created_message_id_ = RegisterWindowMessage("TaskbarButtonCreated");
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "TaskbarButtonCreated message ID registered"
|
|
|
|
<< button_created_message_id_;
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->message == button_created_message_id_) {
|
2012-01-16 23:58:25 +01:00
|
|
|
HRESULT hr;
|
2011-04-24 20:07:09 +02:00
|
|
|
qLog(Debug) << "Button created";
|
2011-03-09 01:13:50 +01:00
|
|
|
// Unref the old taskbar list if we had one
|
|
|
|
if (taskbar_list_) {
|
2011-04-24 20:07:09 +02:00
|
|
|
qLog(Debug) << "Releasing old taskbar list";
|
2011-03-09 01:13:50 +01:00
|
|
|
reinterpret_cast<ITaskbarList3*>(taskbar_list_)->Release();
|
2014-02-06 16:49:49 +01:00
|
|
|
taskbar_list_ = nullptr;
|
2011-03-09 01:13:50 +01:00
|
|
|
}
|
|
|
|
|
2012-11-13 17:02:42 +01:00
|
|
|
// Copied from win7 SDK shobjidl.h
|
2014-02-07 16:34:20 +01:00
|
|
|
static const GUID CLSID_ITaskbarList = {
|
|
|
|
0x56FDF344,
|
|
|
|
0xFD6D,
|
|
|
|
0x11d0,
|
|
|
|
{0x95, 0x8A, 0x00, 0x60, 0x97, 0xC9, 0xA0, 0x90}};
|
2011-04-24 20:07:09 +02:00
|
|
|
// Create the taskbar list
|
2014-02-06 16:49:49 +01:00
|
|
|
hr = CoCreateInstance(CLSID_ITaskbarList, nullptr, CLSCTX_ALL,
|
2014-02-07 16:34:20 +01:00
|
|
|
IID_ITaskbarList3, (void**)&taskbar_list_);
|
2012-01-16 23:58:25 +01:00
|
|
|
if (hr != S_OK) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Warning) << "Error creating the ITaskbarList3 interface" << hex
|
|
|
|
<< DWORD(hr);
|
2011-04-24 20:07:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
ITaskbarList3* taskbar_list =
|
|
|
|
reinterpret_cast<ITaskbarList3*>(taskbar_list_);
|
2012-01-16 23:58:25 +01:00
|
|
|
hr = taskbar_list->HrInit();
|
|
|
|
if (hr != S_OK) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Warning) << "Error initialising taskbar list" << hex << DWORD(hr);
|
2011-04-24 20:07:09 +02:00
|
|
|
taskbar_list->Release();
|
2014-02-06 16:49:49 +01:00
|
|
|
taskbar_list_ = nullptr;
|
2011-04-24 20:07:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the buttons
|
|
|
|
qLog(Debug) << "Initialising" << actions_.count() << "buttons";
|
|
|
|
THUMBBUTTON buttons[kMaxButtonCount];
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < actions_.count(); ++i) {
|
2011-04-24 20:07:09 +02:00
|
|
|
const QAction* action = actions_[i];
|
|
|
|
THUMBBUTTON* button = &buttons[i];
|
|
|
|
button->iId = i;
|
|
|
|
SetupButton(action, button);
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|
2011-04-24 20:07:09 +02:00
|
|
|
|
|
|
|
qLog(Debug) << "Adding buttons";
|
2014-02-07 16:34:20 +01:00
|
|
|
hr = taskbar_list->ThumbBarAddButtons(widget_->winId(), actions_.count(),
|
|
|
|
buttons);
|
|
|
|
if (hr != S_OK) qLog(Debug) << "Failed to add buttons" << hex << DWORD(hr);
|
2012-01-16 23:58:25 +01:00
|
|
|
for (int i = 0; i < actions_.count(); i++) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (buttons[i].hIcon > 0) DestroyIcon(buttons[i].hIcon);
|
2012-01-16 23:58:25 +01:00
|
|
|
}
|
2011-01-16 01:39:51 +01:00
|
|
|
} else if (msg->message == WM_COMMAND) {
|
|
|
|
const int button_id = LOWORD(msg->wParam);
|
|
|
|
|
|
|
|
if (button_id >= 0 && button_id < actions_.count()) {
|
2012-01-29 16:46:10 +01:00
|
|
|
if (actions_[button_id]) {
|
|
|
|
qLog(Debug) << "Button activated";
|
|
|
|
actions_[button_id]->activate(QAction::Trigger);
|
|
|
|
}
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // Q_OS_WIN32
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Windows7ThumbBar::ActionChanged() {
|
|
|
|
#ifdef Q_OS_WIN32
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!taskbar_list_) return;
|
2011-01-16 01:39:51 +01:00
|
|
|
ITaskbarList3* taskbar_list = reinterpret_cast<ITaskbarList3*>(taskbar_list_);
|
|
|
|
|
|
|
|
THUMBBUTTON buttons[kMaxButtonCount];
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < actions_.count(); ++i) {
|
2011-01-16 01:39:51 +01:00
|
|
|
const QAction* action = actions_[i];
|
|
|
|
THUMBBUTTON* button = &buttons[i];
|
|
|
|
|
|
|
|
button->iId = i;
|
|
|
|
SetupButton(action, button);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (buttons->hIcon > 0) DestroyIcon(buttons->hIcon);
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
taskbar_list->ThumbBarUpdateButtons(widget_->winId(), actions_.count(),
|
|
|
|
buttons);
|
|
|
|
#endif // Q_OS_WIN32
|
2011-01-16 01:39:51 +01:00
|
|
|
}
|