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 shoud 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 const IProgressDialogVtbl *vtbl;
72 ULONGLONG ullCompleted;
74 HWND hwndDisabledParent; /* For modal dialog: the parent that need to be re-enabled when the dialog ends */
77 static const IProgressDialogVtbl ProgressDialogVtbl;
79 static void set_buffer(LPWSTR *buffer, LPCWSTR string)
81 const WCHAR empty_string = {0};
86 string = empty_string;
87 CoGetMalloc(1, &malloc);
89 cb = (strlenW(string) + 1)*sizeof(WCHAR);
90 if (*buffer == NULL || cb > IMalloc_GetSize(malloc, *buffer))
91 *buffer = IMalloc_Realloc(malloc, *buffer, cb);
92 memcpy(*buffer, string, cb);
102 static LPWSTR load_string(HINSTANCE hInstance, UINT uiResourceId)
107 LoadStringW(hInstance, uiResourceId, string, sizeof(string)/sizeof(string[0]));
108 ret = HeapAlloc(GetProcessHeap(), 0, (strlenW(string) + 1) * sizeof(WCHAR));
109 strcpyW(ret, string);
113 static void set_progress_marquee(ProgressDialog *This)
115 HWND hProgress = GetDlgItem(This->hwnd, IDC_PROGRESS_BAR);
116 SetWindowLongW(hProgress, GWL_STYLE,
117 GetWindowLongW(hProgress, GWL_STYLE)|PBS_MARQUEE);
120 void update_dialog(ProgressDialog *This, DWORD dwUpdate)
124 if (dwUpdate & UPDATE_TITLE)
125 SetWindowTextW(This->hwnd, This->title);
127 if (dwUpdate & UPDATE_LINE1)
128 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE, (This->isCancelled ? empty : This->lines[0]));
129 if (dwUpdate & UPDATE_LINE2)
130 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+1, (This->isCancelled ? empty : This->lines[1]));
131 if (dwUpdate & UPDATE_LINE3)
132 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+2, (This->isCancelled ? This->cancelMsg : This->lines[2]));
134 if (dwUpdate & UPDATE_PROGRESS)
136 ULONGLONG ullTotal = This->ullTotal;
137 ULONGLONG ullCompleted = This->ullCompleted;
139 /* progress bar requires 32-bit coordinates */
140 while (ullTotal >> 32)
146 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETRANGE32, 0, (DWORD)ullTotal);
147 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETPOS, (DWORD)ullCompleted, 0);
151 static void end_dialog(ProgressDialog *This)
153 SendMessageW(This->hwnd, WM_DLG_DESTROY, 0, 0);
154 /* native doesn't reenable the window? */
155 if (This->hwndDisabledParent)
156 EnableWindow(This->hwndDisabledParent, TRUE);
160 static INT_PTR CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
162 ProgressDialog *This = (ProgressDialog *)GetWindowLongPtrW(hwnd, DWLP_USER);
168 struct create_params *params = (struct create_params *)lParam;
170 /* Note: until we set the hEvent, the object is protected by
171 * the critical section held by StartProgress */
172 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)params->This);
176 if (This->dwFlags & PROGDLG_NOPROGRESSBAR)
177 ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS_BAR), SW_HIDE);
178 if (This->dwFlags & PROGDLG_NOCANCEL)
179 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
180 if (This->dwFlags & PROGDLG_MARQUEEPROGRESS)
181 set_progress_marquee(This);
183 update_dialog(This, 0xffffffff);
185 This->isCancelled = FALSE;
186 SetEvent(params->hEvent);
191 EnterCriticalSection(&This->cs);
192 update_dialog(This, This->dwUpdate);
194 LeaveCriticalSection(&This->cs);
199 PostThreadMessageW(GetCurrentThreadId(), WM_NULL, 0, 0); /* wake up the GetMessage */
204 if (msg == WM_CLOSE || wParam == IDCANCEL)
206 EnterCriticalSection(&This->cs);
207 This->isCancelled = TRUE;
209 if (!This->cancelMsg)
210 This->cancelMsg = load_string(BROWSEUI_hinstance, IDS_CANCELLING);
212 set_progress_marquee(This);
213 EnableWindow(GetDlgItem(This->hwnd, IDCANCEL), FALSE);
214 update_dialog(This, UPDATE_LINE1|UPDATE_LINE2|UPDATE_LINE3);
215 LeaveCriticalSection(&This->cs);
222 static DWORD WINAPI dialog_thread(LPVOID lpParameter)
224 /* Note: until we set the hEvent in WM_INITDIALOG, the ProgressDialog object
225 * is protected by the critical section held by StartProgress */
226 struct create_params *params = (struct create_params *)lpParameter;
230 hwnd = CreateDialogParamW(BROWSEUI_hinstance, MAKEINTRESOURCEW(IDD_PROGRESS_DLG),
231 params->hwndParent, dialog_proc, (LPARAM)params);
233 while (GetMessageW(&msg, NULL, 0, 0) > 0)
237 if(!IsDialogMessageW(hwnd, &msg))
239 TranslateMessage(&msg);
240 DispatchMessageW(&msg);
247 HRESULT WINAPI ProgressDialog_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
249 ProgressDialog *This;
251 return CLASS_E_NOAGGREGATION;
253 This = CoTaskMemAlloc(sizeof(ProgressDialog));
255 return E_OUTOFMEMORY;
256 ZeroMemory(This, sizeof(*This));
257 This->vtbl = &ProgressDialogVtbl;
259 InitializeCriticalSection(&This->cs);
261 TRACE("returning %p\n", This);
262 *ppOut = (IUnknown *)This;
267 static void WINAPI ProgressDialog_Destructor(ProgressDialog *This)
269 TRACE("destroying %p\n", This);
272 CoTaskMemFree(This->lines[0]);
273 CoTaskMemFree(This->lines[1]);
274 CoTaskMemFree(This->lines[2]);
275 CoTaskMemFree(This->cancelMsg);
276 CoTaskMemFree(This->title);
281 static HRESULT WINAPI ProgressDialog_QueryInterface(IProgressDialog *iface, REFIID iid, LPVOID *ppvOut)
283 ProgressDialog *This = (ProgressDialog *)iface;
286 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IProgressDialog))
293 IUnknown_AddRef(iface);
297 WARN("unsupported interface: %s\n", debugstr_guid(iid));
298 return E_NOINTERFACE;
301 static ULONG WINAPI ProgressDialog_AddRef(IProgressDialog *iface)
303 ProgressDialog *This = (ProgressDialog *)iface;
304 return InterlockedIncrement(&This->refCount);
307 static ULONG WINAPI ProgressDialog_Release(IProgressDialog *iface)
309 ProgressDialog *This = (ProgressDialog *)iface;
312 ret = InterlockedDecrement(&This->refCount);
314 ProgressDialog_Destructor(This);
318 static HRESULT WINAPI ProgressDialog_StartProgressDialog(IProgressDialog *iface, HWND hwndParent, IUnknown *punkEnableModeless, DWORD dwFlags, LPCVOID reserved)
320 ProgressDialog *This = (ProgressDialog *)iface;
321 struct create_params params;
324 TRACE("(%p, %p, %x, %p)\n", iface, punkEnableModeless, dwFlags, reserved);
325 if (punkEnableModeless || reserved)
326 FIXME("Reserved parameters not null (%p, %p)\n", punkEnableModeless, reserved);
327 if (dwFlags & PROGDLG_AUTOTIME)
328 FIXME("Flags PROGDLG_AUTOTIME not supported\n");
329 if (dwFlags & PROGDLG_NOTIME)
330 FIXME("Flags PROGDLG_NOTIME not supported\n");
331 if (dwFlags & PROGDLG_NOMINIMIZE)
332 FIXME("Flags PROGDLG_NOMINIMIZE not supported\n");
334 EnterCriticalSection(&This->cs);
338 LeaveCriticalSection(&This->cs);
339 return S_OK; /* as on XP */
341 This->dwFlags = dwFlags;
343 params.hwndParent = hwndParent;
344 params.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
346 hThread = CreateThread(NULL, 0, dialog_thread, ¶ms, 0, NULL);
347 WaitForSingleObject(params.hEvent, INFINITE);
349 This->hwndDisabledParent = NULL;
350 if (hwndParent && (dwFlags & PROGDLG_MODAL))
352 HWND hwndDisable = GetAncestor(hwndParent, GA_ROOT);
353 if (EnableWindow(hwndDisable, FALSE))
354 This->hwndDisabledParent = hwndDisable;
357 LeaveCriticalSection(&This->cs);
362 static HRESULT WINAPI ProgressDialog_StopProgressDialog(IProgressDialog *iface)
364 ProgressDialog *This = (ProgressDialog *)iface;
366 EnterCriticalSection(&This->cs);
369 LeaveCriticalSection(&This->cs);
374 static HRESULT WINAPI ProgressDialog_SetTitle(IProgressDialog *iface, LPCWSTR pwzTitle)
376 ProgressDialog *This = (ProgressDialog *)iface;
379 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzTitle));
381 EnterCriticalSection(&This->cs);
382 set_buffer(&This->title, pwzTitle);
383 This->dwUpdate |= UPDATE_TITLE;
385 LeaveCriticalSection(&This->cs);
388 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
393 static HRESULT WINAPI ProgressDialog_SetAnimation(IProgressDialog *iface, HINSTANCE hInstance, UINT uiResourceId)
395 FIXME("(%p, %p, %d) - stub\n", iface, hInstance, uiResourceId);
399 static BOOL WINAPI ProgressDialog_HasUserCancelled(IProgressDialog *iface)
401 ProgressDialog *This = (ProgressDialog *)iface;
402 return This->isCancelled;
405 static HRESULT WINAPI ProgressDialog_SetProgress64(IProgressDialog *iface, ULONGLONG ullCompleted, ULONGLONG ullTotal)
407 ProgressDialog *This = (ProgressDialog *)iface;
410 TRACE("(%p, 0x%s, 0x%s)\n", This, wine_dbgstr_longlong(ullCompleted), wine_dbgstr_longlong(ullTotal));
412 EnterCriticalSection(&This->cs);
413 This->ullTotal = ullTotal;
414 This->ullCompleted = ullCompleted;
415 This->dwUpdate |= UPDATE_PROGRESS;
417 LeaveCriticalSection(&This->cs);
420 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
422 return S_OK; /* Windows sometimes returns S_FALSE */
425 static HRESULT WINAPI ProgressDialog_SetProgress(IProgressDialog *iface, DWORD dwCompleted, DWORD dwTotal)
427 return IProgressDialog_SetProgress64(iface, dwCompleted, dwTotal);
430 static HRESULT WINAPI ProgressDialog_SetLine(IProgressDialog *iface, DWORD dwLineNum, LPCWSTR pwzLine, BOOL bPath, LPCVOID reserved)
432 ProgressDialog *This = (ProgressDialog *)iface;
435 TRACE("(%p, %d, %s, %d)\n", This, dwLineNum, wine_dbgstr_w(pwzLine), bPath);
438 FIXME("reserved pointer not null (%p)\n", reserved);
441 if (dwLineNum >= 3) /* Windows seems to do something like that */
444 EnterCriticalSection(&This->cs);
445 set_buffer(&This->lines[dwLineNum], pwzLine);
446 This->dwUpdate |= UPDATE_LINE1 << dwLineNum;
447 hwnd = (This->isCancelled ? NULL : This->hwnd); /* no sense to send the message if window cancelled */
448 LeaveCriticalSection(&This->cs);
451 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
456 static HRESULT WINAPI ProgressDialog_SetCancelMsg(IProgressDialog *iface, LPCWSTR pwzMsg, LPCVOID reserved)
458 ProgressDialog *This = (ProgressDialog *)iface;
461 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzMsg));
464 FIXME("reserved pointer not null (%p)\n", reserved);
466 EnterCriticalSection(&This->cs);
467 set_buffer(&This->cancelMsg, pwzMsg);
468 This->dwUpdate |= UPDATE_LINE1 << CANCEL_MSG_LINE;
469 hwnd = (This->isCancelled ? This->hwnd : NULL); /* no sense to send the message if window not cancelled */
470 LeaveCriticalSection(&This->cs);
473 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
478 static HRESULT WINAPI ProgressDialog_Timer(IProgressDialog *iface, DWORD dwTimerAction, LPCVOID reserved)
480 ProgressDialog *This = (ProgressDialog *)iface;
482 FIXME("(%p, %d, %p) - stub\n", This, dwTimerAction, reserved);
485 FIXME("Reserved field not NULL but %p\n", reserved);
490 static const IProgressDialogVtbl ProgressDialogVtbl =
492 ProgressDialog_QueryInterface,
493 ProgressDialog_AddRef,
494 ProgressDialog_Release,
496 ProgressDialog_StartProgressDialog,
497 ProgressDialog_StopProgressDialog,
498 ProgressDialog_SetTitle,
499 ProgressDialog_SetAnimation,
500 ProgressDialog_HasUserCancelled,
501 ProgressDialog_SetProgress,
502 ProgressDialog_SetProgress64,
503 ProgressDialog_SetLine,
504 ProgressDialog_SetCancelMsg,