OSDN Git Service

Use the THROW macro instead of plain "throw" in the complete project.
[lamexp/LameXP.git] / src / WinSevenTaskbar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "WinSevenTaskbar.h"
23
24 #include <QWidget>
25 #include <QIcon>
26
27 //Windows includes
28 #define NOMINMAX
29 #define WIN32_LEAN_AND_MEAN
30 #include <Windows.h>
31 #include <ShObjIdl.h>
32
33 static UINT s_winMsg = 0;
34 static ITaskbarList3 *s_ptbl = NULL;
35
36 WinSevenTaskbar::WinSevenTaskbar(void)
37 {
38         THROW("Cannot create instance of this class!");
39 }
40
41 WinSevenTaskbar::~WinSevenTaskbar(void)
42 {
43 }
44
45 ////////////////////////////////////////////////////////////
46
47 #ifdef __ITaskbarList3_INTERFACE_DEFINED__      
48
49 void WinSevenTaskbar::init(void)
50 {
51         s_winMsg = RegisterWindowMessageW(L"TaskbarButtonCreated");
52         s_ptbl = NULL;
53 }
54         
55 void WinSevenTaskbar::uninit(void)
56 {
57         if(s_ptbl)
58         {
59                 s_ptbl->Release();
60                 s_ptbl = NULL;
61         }
62 }
63
64 bool WinSevenTaskbar::handleWinEvent(void *message, long *result)
65 {
66         bool stopEvent = false;
67
68         if(((MSG*)message)->message == s_winMsg)
69         {
70                 if(!s_ptbl) createInterface();
71                 *result = (s_ptbl) ? S_OK : S_FALSE;
72                 stopEvent = true;
73         }
74
75         return stopEvent;
76 }
77
78 void WinSevenTaskbar::createInterface(void)
79 {
80         if(!s_ptbl)
81         {
82                 ITaskbarList3 *ptbl = NULL;
83                 HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ptbl));
84
85                 if (SUCCEEDED(hr))
86                 {
87                         HRESULT hr2 = ptbl->HrInit();
88                         if(SUCCEEDED(hr2))
89                         {
90                                 s_ptbl = ptbl;
91                                 /*qDebug("ITaskbarList3::HrInit() succeeded.");*/
92                         }
93                         else
94                         {
95                                 ptbl->Release();
96                                 qWarning("ITaskbarList3::HrInit() has failed.");
97                         }
98                 }
99                 else
100                 {
101                         qWarning("ITaskbarList3 could not be created.");
102                 }
103         }
104 }
105
106 bool WinSevenTaskbar::setTaskbarState(QWidget *window, WinSevenTaskbarState state)
107 {
108         bool result = false;
109         
110         if(s_ptbl && window)
111         {
112                 HRESULT hr = HRESULT(-1);
113
114                 switch(state)
115                 {
116                 case WinSevenTaskbarNoState:
117                         hr = s_ptbl->SetProgressState(reinterpret_cast<HWND>(window->winId()), TBPF_NOPROGRESS);
118                         break;
119                 case WinSevenTaskbarNormalState:
120                         hr = s_ptbl->SetProgressState(reinterpret_cast<HWND>(window->winId()), TBPF_NORMAL);
121                         break;
122                 case WinSevenTaskbarIndeterminateState:
123                         hr = s_ptbl->SetProgressState(reinterpret_cast<HWND>(window->winId()), TBPF_INDETERMINATE);
124                         break;
125                 case WinSevenTaskbarErrorState:
126                         hr = s_ptbl->SetProgressState(reinterpret_cast<HWND>(window->winId()), TBPF_ERROR);
127                         break;
128                 case WinSevenTaskbarPausedState:
129                         hr = s_ptbl->SetProgressState(reinterpret_cast<HWND>(window->winId()), TBPF_PAUSED);
130                         break;
131                 }
132
133                 result = SUCCEEDED(hr);
134         }
135
136         return result;
137 }
138
139 void WinSevenTaskbar::setTaskbarProgress(QWidget *window, unsigned __int64 currentValue, unsigned __int64 maximumValue)
140 {
141         if(s_ptbl && window)
142         {
143                 s_ptbl->SetProgressValue(reinterpret_cast<HWND>(window->winId()), currentValue, maximumValue);
144         }
145 }
146
147 void WinSevenTaskbar::setOverlayIcon(QWidget *window, QIcon *icon)
148 {
149 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
150         if(s_ptbl && window)
151         {
152                 s_ptbl->SetOverlayIcon(window->winId(), (icon ? icon->pixmap(16,16).toWinHICON() : NULL), L"LameXP");
153         }
154 #endif
155 }
156
157 #else //__ITaskbarList3_INTERFACE_DEFINED__
158
159 LAMEXP_COMPILER_WARNING("ITaskbarList3 not defined. Compiling *without* support for Win7 taskbar!")
160 void WinSevenTaskbar::init(void) {}
161 void WinSevenTaskbar::uninit(void) {}
162 bool WinSevenTaskbar::handleWinEvent(MSG *message, long *result) { return false; }
163 void WinSevenTaskbar::createInterface(void) {}
164 bool WinSevenTaskbar::setTaskbarState(QWidget *window, WinSevenTaskbarState state) { return false; }
165 void WinSevenTaskbar::setTaskbarProgress(QWidget *window, unsigned __int64 currentValue, unsigned __int64 maximumValue) {}
166 void WinSevenTaskbar::setOverlayIcon(QWidget *window, QIcon *icon) {}
167
168 #endif //__ITaskbarList3_INTERFACE_DEFINED__