1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
24 #include <MUtils/Taskbar7.h>
25 #include <MUtils/OSSupport.h>
26 #include <MUtils/Exception.h>
29 #include "Utils_Win32.h"
34 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
35 #include <QtWinExtras>
40 #define WIN32_LEAN_AND_MEAN 1
44 ///////////////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////////////
48 #define INITIALIZE_TASKBAR() do \
54 if(!(MUTILS_BOOLIFY(p->initialized) || initialize())) \
56 qWarning("Taskbar initialization failed!"); \
62 ///////////////////////////////////////////////////////////////////////////////
64 ///////////////////////////////////////////////////////////////////////////////
68 class Taskbar7_Private
70 friend class Taskbar7;
73 Taskbar7_Private(void)
78 ITaskbarList3 *taskbarList;
80 QAtomicInt initialized;
84 ///////////////////////////////////////////////////////////////////////////////
85 // CONSTRUCTOR & DESTRUCTOR
86 ///////////////////////////////////////////////////////////////////////////////
88 MUtils::Taskbar7::Taskbar7(QWidget *const window)
90 p(new Taskbar7_Private()),
95 MUTILS_THROW("Taskbar7: Window pointer must not be NULL!");
99 if (OS::os_version() >= OS::Version::WINDOWS_WIN70)
105 qWarning("Taskbar7: Taskbar progress not supported on this platform.");
110 MUtils::Taskbar7::~Taskbar7(void)
114 p->taskbarList->Release();
115 p->taskbarList = NULL;
121 ///////////////////////////////////////////////////////////////////////////////
123 ///////////////////////////////////////////////////////////////////////////////
125 bool MUtils::Taskbar7::setTaskbarState(const taskbarState_t &state)
127 INITIALIZE_TASKBAR();
128 HRESULT result = HRESULT(-1);
132 case TASKBAR_STATE_NONE:
133 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NOPROGRESS);
135 case TASKBAR_STATE_NORMAL:
136 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NORMAL);
138 case TASKBAR_STATE_INTERMEDIATE:
139 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_INDETERMINATE);
141 case TASKBAR_STATE_PAUSED:
142 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_ERROR);
144 case TASKBAR_STATE_ERROR:
145 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_PAUSED);
148 MUTILS_THROW("Taskbar7: Invalid taskbar state specified!");
151 return SUCCEEDED(result);
154 bool MUtils::Taskbar7::setTaskbarProgress(const quint64 ¤tValue, const quint64 &maximumValue)
156 INITIALIZE_TASKBAR();
157 const HRESULT result = p->taskbarList->SetProgressValue(reinterpret_cast<HWND>(m_window->winId()), currentValue, maximumValue);
158 return SUCCEEDED(result);
161 bool MUtils::Taskbar7::setOverlayIcon(const QIcon *const icon, const QString &info)
163 INITIALIZE_TASKBAR();
164 HRESULT result = HRESULT(-1);
167 if(const HICON hIcon = (HICON)MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
169 result = p->taskbarList->SetOverlayIcon(reinterpret_cast<HWND>(m_window->winId()), hIcon, MUTILS_WCHR(info));
175 result = p->taskbarList->SetOverlayIcon(reinterpret_cast<HWND>(m_window->winId()), NULL, MUTILS_WCHR(info));
177 return SUCCEEDED(result);
180 ///////////////////////////////////////////////////////////////////////////////
182 ///////////////////////////////////////////////////////////////////////////////
184 bool MUtils::Taskbar7::initialize(void)
186 while(!p->taskbarList)
188 ITaskbarList3 *ptbl = NULL;
189 const HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ptbl));
192 qWarning("ITaskbarList3 could not be created!");
195 p->taskbarList = ptbl;
201 for(int i = 0; i < 8; i++)
203 if(SUCCEEDED(p->taskbarList->HrInit()))
212 qWarning("ITaskbarList3::HrInit() has failed!");
215 p->initialized.ref(); /*success*/