1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/* vim: se cin sw=2 ts=2 et : */
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ArrayUtils.h"
#include <nsITaskbarPreviewController.h>
#include "TaskbarWindowPreview.h"
#include "WindowHook.h"
#include "nsUXThemeData.h"
#include "TaskbarPreviewButton.h"
#include "nsWindow.h"
#include "nsWindowGfx.h"
namespace mozilla {
namespace widget {
namespace {
bool WindowHookProc(void* aContext, HWND hWnd, UINT nMsg, WPARAM wParam,
LPARAM lParam, LRESULT* aResult) {
TaskbarWindowPreview* preview =
reinterpret_cast<TaskbarWindowPreview*>(aContext);
*aResult = preview->WndProc(nMsg, wParam, lParam);
return true;
}
} // namespace
NS_IMPL_ISUPPORTS(TaskbarWindowPreview, nsITaskbarWindowPreview,
nsITaskbarProgress, nsITaskbarOverlayIconController,
nsISupportsWeakReference)
/**
* These correspond directly to the states defined in nsITaskbarProgress.idl, so
* they should be kept in sync.
*/
static TBPFLAG sNativeStates[] = {TBPF_NOPROGRESS, TBPF_INDETERMINATE,
TBPF_NORMAL, TBPF_ERROR, TBPF_PAUSED};
TaskbarWindowPreview::TaskbarWindowPreview(
ITaskbarList4* aTaskbar, nsITaskbarPreviewController* aController,
HWND aHWND, nsIDocShell* aShell)
: TaskbarPreview(aTaskbar, aController, aHWND, aShell),
mCustomDrawing(false),
mHaveButtons(false),
mState(TBPF_NOPROGRESS),
mCurrentValue(0),
mMaxValue(0),
mOverlayIcon(nullptr) {
// Window previews are visible by default
(void)SetVisible(true);
memset(mThumbButtons, 0, sizeof mThumbButtons);
for (int32_t i = 0; i < nsITaskbarWindowPreview::NUM_TOOLBAR_BUTTONS; i++) {
mThumbButtons[i].dwMask = THB_FLAGS | THB_ICON | THB_TOOLTIP;
mThumbButtons[i].iId = i;
mThumbButtons[i].dwFlags = THBF_HIDDEN;
}
}
TaskbarWindowPreview::~TaskbarWindowPreview() {
if (mOverlayIcon) {
::DestroyIcon(mOverlayIcon);
mOverlayIcon = nullptr;
}
// We need to clean up a hook associated with the "this" pointer.
SetVisible(false);
if (IsWindowAvailable()) {
DetachFromNSWindow();
} else {
mWnd = nullptr;
}
}
nsresult TaskbarWindowPreview::Init() {
nsresult rv = TaskbarPreview::Init();
if (NS_FAILED(rv)) {
return rv;
}
if (CanMakeTaskbarCalls()) {
return NS_OK;
}
WindowHook* hook = GetWindowHook();
if (!hook) {
return NS_ERROR_NOT_AVAILABLE;
}
return hook->AddMonitor(nsAppShell::GetTaskbarButtonCreatedMessage(),
TaskbarWindowHook, this);
}
nsresult TaskbarWindowPreview::ShowActive(bool active) {
return FAILED(mTaskbar->ActivateTab(active ? mWnd : nullptr))
? NS_ERROR_FAILURE
: NS_OK;
}
HWND& TaskbarWindowPreview::PreviewWindow() { return mWnd; }
nsresult TaskbarWindowPreview::GetButton(uint32_t index,
nsITaskbarPreviewButton** _retVal) {
if (index >= nsITaskbarWindowPreview::NUM_TOOLBAR_BUTTONS)
return NS_ERROR_INVALID_ARG;
nsCOMPtr<nsITaskbarPreviewButton> button(
do_QueryReferent(mWeakButtons[index]));
if (!button) {
// Lost reference
button = new TaskbarPreviewButton(this, index);
if (!button) {
return NS_ERROR_OUT_OF_MEMORY;
}
mWeakButtons[index] = do_GetWeakReference(button);
}
if (!mHaveButtons) {
mHaveButtons = true;
WindowHook* hook = GetWindowHook();
if (!hook) {
return NS_ERROR_NOT_AVAILABLE;
}
(void)hook->AddHook(WM_COMMAND, WindowHookProc, this);
if (mVisible && FAILED(mTaskbar->ThumbBarAddButtons(
mWnd, nsITaskbarWindowPreview::NUM_TOOLBAR_BUTTONS,
mThumbButtons))) {
return NS_ERROR_FAILURE;
}
}
button.forget(_retVal);
return NS_OK;
}
NS_IMETHODIMP
TaskbarWindowPreview::SetEnableCustomDrawing(bool aEnable) {
if (aEnable == mCustomDrawing) return NS_OK;
WindowHook* hook = GetWindowHook();
if (!hook) {
return NS_ERROR_NOT_AVAILABLE;
}
mCustomDrawing = aEnable;
TaskbarPreview::EnableCustomDrawing(mWnd, aEnable);
if (aEnable) {
(void)hook->AddHook(WM_DWMSENDICONICTHUMBNAIL, WindowHookProc, this);
(void)hook->AddHook(WM_DWMSENDICONICLIVEPREVIEWBITMAP, WindowHookProc,
this);
} else {
(void)hook->RemoveHook(WM_DWMSENDICONICLIVEPREVIEWBITMAP, WindowHookProc,
this);
(void)hook->RemoveHook(WM_DWMSENDICONICTHUMBNAIL, WindowHookProc, this);
}
return NS_OK;
}
NS_IMETHODIMP
TaskbarWindowPreview::GetEnableCustomDrawing(bool* aEnable) {
*aEnable = mCustomDrawing;
return NS_OK;
}
NS_IMETHODIMP
TaskbarWindowPreview::SetProgressState(nsTaskbarProgressState aState,
uint64_t aCurrentValue,
uint64_t aMaxValue) {
NS_ENSURE_ARG_RANGE(aState, nsTaskbarProgressState(0),
nsTaskbarProgressState(ArrayLength(sNativeStates) - 1));
TBPFLAG nativeState = sNativeStates[aState];
if (nativeState == TBPF_NOPROGRESS || nativeState == TBPF_INDETERMINATE) {
NS_ENSURE_TRUE(aCurrentValue == 0, NS_ERROR_INVALID_ARG);
NS_ENSURE_TRUE(aMaxValue == 0, NS_ERROR_INVALID_ARG);
}
if (aCurrentValue > aMaxValue) return NS_ERROR_ILLEGAL_VALUE;
mState = nativeState;
mCurrentValue = aCurrentValue;
mMaxValue = aMaxValue;
// Only update if we can
return CanMakeTaskbarCalls() ? UpdateTaskbarProgress() : NS_OK;
}
NS_IMETHODIMP
TaskbarWindowPreview::SetOverlayIcon(imgIContainer* aStatusIcon,
const nsAString& aStatusDescription) {
nsresult rv;
if (aStatusIcon) {
// The image shouldn't be animated
bool isAnimated;
rv = aStatusIcon->GetAnimated(&isAnimated);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_FALSE(isAnimated, NS_ERROR_INVALID_ARG);
}
HICON hIcon = nullptr;
if (aStatusIcon) {
rv = nsWindowGfx::CreateIcon(
aStatusIcon, false, LayoutDeviceIntPoint(),
nsWindowGfx::GetIconMetrics(nsWindowGfx::kSmallIcon), &hIcon);
NS_ENSURE_SUCCESS(rv, rv);
}
if (mOverlayIcon) ::DestroyIcon(mOverlayIcon);
mOverlayIcon = hIcon;
mIconDescription = aStatusDescription;
// Only update if we can
return CanMakeTaskbarCalls() ? UpdateOverlayIcon() : NS_OK;
}
nsresult TaskbarWindowPreview::UpdateTaskbarProperties() {
if (mHaveButtons) {
if (FAILED(mTaskbar->ThumbBarAddButtons(
mWnd, nsITaskbarWindowPreview::NUM_TOOLBAR_BUTTONS, mThumbButtons)))
return NS_ERROR_FAILURE;
}
nsresult rv = UpdateTaskbarProgress();
NS_ENSURE_SUCCESS(rv, rv);
rv = UpdateOverlayIcon();
NS_ENSURE_SUCCESS(rv, rv);
return TaskbarPreview::UpdateTaskbarProperties();
}
nsresult TaskbarWindowPreview::UpdateTaskbarProgress() {
HRESULT hr = mTaskbar->SetProgressState(mWnd, mState);
if (SUCCEEDED(hr) && mState != TBPF_NOPROGRESS &&
mState != TBPF_INDETERMINATE)
hr = mTaskbar->SetProgressValue(mWnd, mCurrentValue, mMaxValue);
return SUCCEEDED(hr) ? NS_OK : NS_ERROR_FAILURE;
}
nsresult TaskbarWindowPreview::UpdateOverlayIcon() {
HRESULT hr =
mTaskbar->SetOverlayIcon(mWnd, mOverlayIcon, mIconDescription.get());
return SUCCEEDED(hr) ? NS_OK : NS_ERROR_FAILURE;
}
LRESULT
TaskbarWindowPreview::WndProc(UINT nMsg, WPARAM wParam, LPARAM lParam) {
RefPtr<TaskbarWindowPreview> kungFuDeathGrip(this);
switch (nMsg) {
case WM_COMMAND: {
uint32_t id = LOWORD(wParam);
uint32_t index = id;
nsCOMPtr<nsITaskbarPreviewButton> button;
nsresult rv = GetButton(index, getter_AddRefs(button));
if (NS_SUCCEEDED(rv)) mController->OnClick(button);
}
return 0;
}
return TaskbarPreview::WndProc(nMsg, wParam, lParam);
}
/* static */
bool TaskbarWindowPreview::TaskbarWindowHook(void* aContext, HWND hWnd,
UINT nMsg, WPARAM wParam,
LPARAM lParam, LRESULT* aResult) {
NS_ASSERTION(nMsg == nsAppShell::GetTaskbarButtonCreatedMessage(),
"Window hook proc called with wrong message");
TaskbarWindowPreview* preview =
reinterpret_cast<TaskbarWindowPreview*>(aContext);
// Now we can make all the calls to mTaskbar
preview->UpdateTaskbarProperties();
return false;
}
nsresult TaskbarWindowPreview::Enable() {
nsresult rv = TaskbarPreview::Enable();
NS_ENSURE_SUCCESS(rv, rv);
return FAILED(mTaskbar->AddTab(mWnd)) ? NS_ERROR_FAILURE : NS_OK;
}
nsresult TaskbarWindowPreview::Disable() {
nsresult rv = TaskbarPreview::Disable();
NS_ENSURE_SUCCESS(rv, rv);
return FAILED(mTaskbar->DeleteTab(mWnd)) ? NS_ERROR_FAILURE : NS_OK;
}
void TaskbarWindowPreview::DetachFromNSWindow() {
// Remove the hooks we have for drawing
SetEnableCustomDrawing(false);
if (WindowHook* hook = GetWindowHook()) {
(void)hook->RemoveHook(WM_COMMAND, WindowHookProc, this);
(void)hook->RemoveMonitor(nsAppShell::GetTaskbarButtonCreatedMessage(),
TaskbarWindowHook, this);
}
TaskbarPreview::DetachFromNSWindow();
}
nsresult TaskbarWindowPreview::UpdateButtons() {
NS_ASSERTION(mVisible, "UpdateButtons called on invisible preview");
if (FAILED(mTaskbar->ThumbBarUpdateButtons(
mWnd, nsITaskbarWindowPreview::NUM_TOOLBAR_BUTTONS, mThumbButtons)))
return NS_ERROR_FAILURE;
return NS_OK;
}
nsresult TaskbarWindowPreview::UpdateButton(uint32_t index) {
if (index >= nsITaskbarWindowPreview::NUM_TOOLBAR_BUTTONS)
return NS_ERROR_INVALID_ARG;
if (mVisible) {
if (FAILED(mTaskbar->ThumbBarUpdateButtons(mWnd, 1, &mThumbButtons[index])))
return NS_ERROR_FAILURE;
}
return NS_OK;
}
} // namespace widget
} // namespace mozilla
|