Added support for Visual Studio 2019.6 (v16.6).
[MUtilities.git] / src / Taskbar7_Win32.cpp
blob258eebc7abde1f9059eb66b29ab45919a3c8ccf5
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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 ///////////////////////////////////////////////////////////////////////////////
23 //MUtils
24 #include <MUtils/Taskbar7.h>
25 #include <MUtils/OSSupport.h>
26 #include <MUtils/Exception.h>
28 //Internal
29 #include "Utils_Win32.h"
31 //Qt
32 #include <QWidget>
33 #include <QIcon>
34 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
35 #include <QtWinExtras>
36 #endif
38 //Windows includes
39 #define NOMINMAX
40 #define WIN32_LEAN_AND_MEAN 1
41 #include <Windows.h>
42 #include <ShObjIdl.h>
44 ///////////////////////////////////////////////////////////////////////////////
45 // UNTILITIES
46 ///////////////////////////////////////////////////////////////////////////////
48 #define INITIALIZE_TASKBAR() do \
49 { \
50 if(!p->supported) \
51 { \
52 return false; \
53 } \
54 if(!(MUTILS_BOOLIFY(p->initialized) || initialize())) \
55 { \
56 qWarning("Taskbar initialization failed!"); \
57 return false; \
58 } \
59 } \
60 while(0)
62 ///////////////////////////////////////////////////////////////////////////////
63 // PRIVATE DATA
64 ///////////////////////////////////////////////////////////////////////////////
66 namespace MUtils
68 class Taskbar7_Private
70 friend class Taskbar7;
72 protected:
73 Taskbar7_Private(void)
75 taskbarList = NULL;
78 ITaskbarList3 *taskbarList;
79 QAtomicInt supported;
80 QAtomicInt initialized;
84 ///////////////////////////////////////////////////////////////////////////////
85 // CONSTRUCTOR & DESTRUCTOR
86 ///////////////////////////////////////////////////////////////////////////////
88 MUtils::Taskbar7::Taskbar7(QWidget *const window)
90 p(new Taskbar7_Private()),
91 m_window(window)
93 if(!m_window)
95 MUTILS_THROW("Taskbar7: Window pointer must not be NULL!");
97 if (!p->supported)
99 if (OS::os_version() >= OS::Version::WINDOWS_WIN70)
101 p->supported.ref();
103 else
105 qWarning("Taskbar7: Taskbar progress not supported on this platform.");
110 MUtils::Taskbar7::~Taskbar7(void)
112 if(p->taskbarList)
114 p->taskbarList->Release();
115 p->taskbarList = NULL;
118 delete p;
121 ///////////////////////////////////////////////////////////////////////////////
122 // PUBLIC INTERFACE
123 ///////////////////////////////////////////////////////////////////////////////
125 bool MUtils::Taskbar7::setTaskbarState(const taskbarState_t &state)
127 INITIALIZE_TASKBAR();
128 HRESULT result = HRESULT(-1);
130 switch(state)
132 case TASKBAR_STATE_NONE:
133 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NOPROGRESS);
134 break;
135 case TASKBAR_STATE_NORMAL:
136 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NORMAL);
137 break;
138 case TASKBAR_STATE_INTERMEDIATE:
139 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_INDETERMINATE);
140 break;
141 case TASKBAR_STATE_PAUSED:
142 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_ERROR);
143 break;
144 case TASKBAR_STATE_ERROR:
145 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_PAUSED);
146 break;
147 default:
148 MUTILS_THROW("Taskbar7: Invalid taskbar state specified!");
151 return SUCCEEDED(result);
154 bool MUtils::Taskbar7::setTaskbarProgress(const quint64 &currentValue, 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);
165 if(icon)
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));
170 DestroyIcon(hIcon);
173 else
175 result = p->taskbarList->SetOverlayIcon(reinterpret_cast<HWND>(m_window->winId()), NULL, MUTILS_WCHR(info));
177 return SUCCEEDED(result);
180 ///////////////////////////////////////////////////////////////////////////////
181 // INTERNAL
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));
190 if(!SUCCEEDED(hr))
192 qWarning("ITaskbarList3 could not be created!");
193 return false;
195 p->taskbarList = ptbl;
198 if(!p->initialized)
200 bool okay = false;
201 for(int i = 0; i < 8; i++)
203 if(SUCCEEDED(p->taskbarList->HrInit()))
205 okay = true;
206 break;
208 Sleep(1);
210 if(!okay)
212 qWarning("ITaskbarList3::HrInit() has failed!");
213 return false;
215 p->initialized.ref(); /*success*/
218 return true;