4 * Copyright 2007 Mikolaj Zalewski
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/debug.h"
39 #include "wine/unicode.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(browseui);
46 #define CANCEL_MSG_LINE 2
48 /* Note: to avoid a deadlock we don't want to send messages to the dialog
49 * with the critical section held. Instead we only mark what fields should be
50 * updated and the dialog proc does the update */
51 #define UPDATE_PROGRESS 0x1
52 #define UPDATE_TITLE 0x2
53 #define UPDATE_LINE1 0x4
54 #define UPDATE_LINE2 (UPDATE_LINE1<<1)
55 #define UPDATE_LINE3 (UPDATE_LINE2<<2)
58 #define WM_DLG_UPDATE (WM_APP+1) /* set to the dialog when it should update */
59 #define WM_DLG_DESTROY (WM_APP+2) /* DestroyWindow must be called from the owning thread */
61 typedef struct tagProgressDialog {
62 IProgressDialog IProgressDialog_iface;
72 ULONGLONG ullCompleted;
74 HWND hwndDisabledParent; /* For modal dialog: the parent that need to be re-enabled when the dialog ends */
77 static inline ProgressDialog *impl_from_IProgressDialog(IProgressDialog *iface)
79 return CONTAINING_RECORD(iface, ProgressDialog, IProgressDialog_iface);
82 static void set_buffer(LPWSTR *buffer, LPCWSTR string)
84 static const WCHAR empty_string[] = {0};
89 string = empty_string;
90 CoGetMalloc(1, &malloc);
92 cb = (strlenW(string) + 1)*sizeof(WCHAR);
93 if (*buffer == NULL || cb > IMalloc_GetSize(malloc, *buffer))
94 *buffer = IMalloc_Realloc(malloc, *buffer, cb);
95 memcpy(*buffer, string, cb);
100 ProgressDialog *This;
105 static LPWSTR load_string(HINSTANCE hInstance, UINT uiResourceId)
110 LoadStringW(hInstance, uiResourceId, string, sizeof(string)/sizeof(string[0]));
111 ret = HeapAlloc(GetProcessHeap(), 0, (strlenW(string) + 1) * sizeof(WCHAR));
112 strcpyW(ret, string);
116 static void set_progress_marquee(ProgressDialog *This)
118 HWND hProgress = GetDlgItem(This->hwnd, IDC_PROGRESS_BAR);
119 SetWindowLongW(hProgress, GWL_STYLE,
120 GetWindowLongW(hProgress, GWL_STYLE)|PBS_MARQUEE);
123 static void update_dialog(ProgressDialog *This, DWORD dwUpdate)
127 if (dwUpdate & UPDATE_TITLE)
128 SetWindowTextW(This->hwnd, This->title);
130 if (dwUpdate & UPDATE_LINE1)
131 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE, (This->isCancelled ? empty : This->lines[0]));
132 if (dwUpdate & UPDATE_LINE2)
133 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+1, (This->isCancelled ? empty : This->lines[1]));
134 if (dwUpdate & UPDATE_LINE3)
135 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+2, (This->isCancelled ? This->cancelMsg : This->lines[2]));
137 if (dwUpdate & UPDATE_PROGRESS)
139 ULONGLONG ullTotal = This->ullTotal;
140 ULONGLONG ullCompleted = This->ullCompleted;
142 /* progress bar requires 32-bit coordinates */
143 while (ullTotal >> 32)
149 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETRANGE32, 0, (DWORD)ullTotal);
150 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETPOS, (DWORD)ullCompleted, 0);
154 static void end_dialog(ProgressDialog *This)
156 SendMessageW(This->hwnd, WM_DLG_DESTROY, 0, 0);
157 /* native doesn't reenable the window? */
158 if (This->hwndDisabledParent)
159 EnableWindow(This->hwndDisabledParent, TRUE);
163 static INT_PTR CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
165 ProgressDialog *This = (ProgressDialog *)GetWindowLongPtrW(hwnd, DWLP_USER);
171 struct create_params *params = (struct create_params *)lParam;
173 /* Note: until we set the hEvent, the object is protected by
174 * the critical section held by StartProgress */
175 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)params->This);
179 if (This->dwFlags & PROGDLG_NOPROGRESSBAR)
180 ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS_BAR), SW_HIDE);
181 if (This->dwFlags & PROGDLG_NOCANCEL)
182 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
183 if (This->dwFlags & PROGDLG_MARQUEEPROGRESS)
184 set_progress_marquee(This);
185 if (This->dwFlags & PROGDLG_NOMINIMIZE)
186 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & (~WS_MINIMIZEBOX));
188 update_dialog(This, 0xffffffff);
190 This->isCancelled = FALSE;
191 SetEvent(params->hEvent);
196 EnterCriticalSection(&This->cs);
197 update_dialog(This, This->dwUpdate);
199 LeaveCriticalSection(&This->cs);
204 PostThreadMessageW(GetCurrentThreadId(), WM_NULL, 0, 0); /* wake up the GetMessage */
209 if (msg == WM_CLOSE || wParam == IDCANCEL)
211 EnterCriticalSection(&This->cs);
212 This->isCancelled = TRUE;
214 if (!This->cancelMsg)
215 This->cancelMsg = load_string(BROWSEUI_hinstance, IDS_CANCELLING);
217 set_progress_marquee(This);
218 EnableWindow(GetDlgItem(This->hwnd, IDCANCEL), FALSE);
219 update_dialog(This, UPDATE_LINE1|UPDATE_LINE2|UPDATE_LINE3);
220 LeaveCriticalSection(&This->cs);
227 static DWORD WINAPI dialog_thread(LPVOID lpParameter)
229 /* Note: until we set the hEvent in WM_INITDIALOG, the ProgressDialog object
230 * is protected by the critical section held by StartProgress */
231 struct create_params *params = lpParameter;
235 hwnd = CreateDialogParamW(BROWSEUI_hinstance, MAKEINTRESOURCEW(IDD_PROGRESS_DLG),
236 params->hwndParent, dialog_proc, (LPARAM)params);
238 while (GetMessageW(&msg, NULL, 0, 0) > 0)
242 if(!IsDialogMessageW(hwnd, &msg))
244 TranslateMessage(&msg);
245 DispatchMessageW(&msg);
252 static void ProgressDialog_Destructor(ProgressDialog *This)
254 TRACE("destroying %p\n", This);
257 heap_free(This->lines[0]);
258 heap_free(This->lines[1]);
259 heap_free(This->lines[2]);
260 heap_free(This->cancelMsg);
261 heap_free(This->title);
263 This->cs.DebugInfo->Spare[0] = 0;
264 DeleteCriticalSection(&This->cs);
268 static HRESULT WINAPI ProgressDialog_QueryInterface(IProgressDialog *iface, REFIID iid, LPVOID *ppvOut)
270 ProgressDialog *This = impl_from_IProgressDialog(iface);
273 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IProgressDialog))
280 IUnknown_AddRef(iface);
284 WARN("unsupported interface: %s\n", debugstr_guid(iid));
285 return E_NOINTERFACE;
288 static ULONG WINAPI ProgressDialog_AddRef(IProgressDialog *iface)
290 ProgressDialog *This = impl_from_IProgressDialog(iface);
291 return InterlockedIncrement(&This->refCount);
294 static ULONG WINAPI ProgressDialog_Release(IProgressDialog *iface)
296 ProgressDialog *This = impl_from_IProgressDialog(iface);
299 ret = InterlockedDecrement(&This->refCount);
301 ProgressDialog_Destructor(This);
305 static HRESULT WINAPI ProgressDialog_StartProgressDialog(IProgressDialog *iface, HWND hwndParent, IUnknown *punkEnableModeless, DWORD dwFlags, LPCVOID reserved)
307 static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_ANIMATE_CLASS };
308 ProgressDialog *This = impl_from_IProgressDialog(iface);
309 struct create_params params;
312 TRACE("(%p, %p, %x, %p)\n", iface, punkEnableModeless, dwFlags, reserved);
313 if (punkEnableModeless || reserved)
314 FIXME("Reserved parameters not null (%p, %p)\n", punkEnableModeless, reserved);
315 if (dwFlags & PROGDLG_AUTOTIME)
316 FIXME("Flags PROGDLG_AUTOTIME not supported\n");
317 if (dwFlags & PROGDLG_NOTIME)
318 FIXME("Flags PROGDLG_NOTIME not supported\n");
320 InitCommonControlsEx( &init );
322 EnterCriticalSection(&This->cs);
326 LeaveCriticalSection(&This->cs);
327 return S_OK; /* as on XP */
329 This->dwFlags = dwFlags;
331 params.hwndParent = hwndParent;
332 params.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
334 hThread = CreateThread(NULL, 0, dialog_thread, ¶ms, 0, NULL);
335 WaitForSingleObject(params.hEvent, INFINITE);
336 CloseHandle(params.hEvent);
337 CloseHandle(hThread);
339 This->hwndDisabledParent = NULL;
340 if (hwndParent && (dwFlags & PROGDLG_MODAL))
342 HWND hwndDisable = GetAncestor(hwndParent, GA_ROOT);
343 if (EnableWindow(hwndDisable, FALSE))
344 This->hwndDisabledParent = hwndDisable;
347 LeaveCriticalSection(&This->cs);
352 static HRESULT WINAPI ProgressDialog_StopProgressDialog(IProgressDialog *iface)
354 ProgressDialog *This = impl_from_IProgressDialog(iface);
356 EnterCriticalSection(&This->cs);
359 LeaveCriticalSection(&This->cs);
364 static HRESULT WINAPI ProgressDialog_SetTitle(IProgressDialog *iface, LPCWSTR pwzTitle)
366 ProgressDialog *This = impl_from_IProgressDialog(iface);
369 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzTitle));
371 EnterCriticalSection(&This->cs);
372 set_buffer(&This->title, pwzTitle);
373 This->dwUpdate |= UPDATE_TITLE;
375 LeaveCriticalSection(&This->cs);
378 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
383 static HRESULT WINAPI ProgressDialog_SetAnimation(IProgressDialog *iface, HINSTANCE hInstance, UINT uiResourceId)
385 FIXME("(%p, %p, %d) - stub\n", iface, hInstance, uiResourceId);
389 static BOOL WINAPI ProgressDialog_HasUserCancelled(IProgressDialog *iface)
391 ProgressDialog *This = impl_from_IProgressDialog(iface);
392 return This->isCancelled;
395 static HRESULT WINAPI ProgressDialog_SetProgress64(IProgressDialog *iface, ULONGLONG ullCompleted, ULONGLONG ullTotal)
397 ProgressDialog *This = impl_from_IProgressDialog(iface);
400 TRACE("(%p, 0x%s, 0x%s)\n", This, wine_dbgstr_longlong(ullCompleted), wine_dbgstr_longlong(ullTotal));
402 EnterCriticalSection(&This->cs);
403 This->ullTotal = ullTotal;
404 This->ullCompleted = ullCompleted;
405 This->dwUpdate |= UPDATE_PROGRESS;
407 LeaveCriticalSection(&This->cs);
410 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
412 return S_OK; /* Windows sometimes returns S_FALSE */
415 static HRESULT WINAPI ProgressDialog_SetProgress(IProgressDialog *iface, DWORD dwCompleted, DWORD dwTotal)
417 return IProgressDialog_SetProgress64(iface, dwCompleted, dwTotal);
420 static HRESULT WINAPI ProgressDialog_SetLine(IProgressDialog *iface, DWORD dwLineNum, LPCWSTR pwzLine, BOOL bPath, LPCVOID reserved)
422 ProgressDialog *This = impl_from_IProgressDialog(iface);
425 TRACE("(%p, %d, %s, %d)\n", This, dwLineNum, wine_dbgstr_w(pwzLine), bPath);
428 FIXME("reserved pointer not null (%p)\n", reserved);
431 if (dwLineNum >= 3) /* Windows seems to do something like that */
434 EnterCriticalSection(&This->cs);
435 set_buffer(&This->lines[dwLineNum], pwzLine);
436 This->dwUpdate |= UPDATE_LINE1 << dwLineNum;
437 hwnd = (This->isCancelled ? NULL : This->hwnd); /* no sense to send the message if window cancelled */
438 LeaveCriticalSection(&This->cs);
441 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
446 static HRESULT WINAPI ProgressDialog_SetCancelMsg(IProgressDialog *iface, LPCWSTR pwzMsg, LPCVOID reserved)
448 ProgressDialog *This = impl_from_IProgressDialog(iface);
451 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzMsg));
454 FIXME("reserved pointer not null (%p)\n", reserved);
456 EnterCriticalSection(&This->cs);
457 set_buffer(&This->cancelMsg, pwzMsg);
458 This->dwUpdate |= UPDATE_LINE1 << CANCEL_MSG_LINE;
459 hwnd = (This->isCancelled ? This->hwnd : NULL); /* no sense to send the message if window not cancelled */
460 LeaveCriticalSection(&This->cs);
463 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
468 static HRESULT WINAPI ProgressDialog_Timer(IProgressDialog *iface, DWORD dwTimerAction, LPCVOID reserved)
470 ProgressDialog *This = impl_from_IProgressDialog(iface);
472 FIXME("(%p, %d, %p) - stub\n", This, dwTimerAction, reserved);
475 FIXME("Reserved field not NULL but %p\n", reserved);
480 static const IProgressDialogVtbl ProgressDialogVtbl =
482 ProgressDialog_QueryInterface,
483 ProgressDialog_AddRef,
484 ProgressDialog_Release,
486 ProgressDialog_StartProgressDialog,
487 ProgressDialog_StopProgressDialog,
488 ProgressDialog_SetTitle,
489 ProgressDialog_SetAnimation,
490 ProgressDialog_HasUserCancelled,
491 ProgressDialog_SetProgress,
492 ProgressDialog_SetProgress64,
493 ProgressDialog_SetLine,
494 ProgressDialog_SetCancelMsg,
498 HRESULT ProgressDialog_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
500 ProgressDialog *This;
502 return CLASS_E_NOAGGREGATION;
504 This = heap_alloc_zero(sizeof(ProgressDialog));
506 return E_OUTOFMEMORY;
508 This->IProgressDialog_iface.lpVtbl = &ProgressDialogVtbl;
510 InitializeCriticalSection(&This->cs);
511 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ProgressDialog.cs");
513 TRACE("returning %p\n", This);
514 *ppOut = (IUnknown *)This;