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;
63 IOleWindow IOleWindow_iface;
73 ULONGLONG ullCompleted;
75 HWND hwndDisabledParent; /* For modal dialog: the parent that need to be re-enabled when the dialog ends */
78 static inline ProgressDialog *impl_from_IProgressDialog(IProgressDialog *iface)
80 return CONTAINING_RECORD(iface, ProgressDialog, IProgressDialog_iface);
83 static inline ProgressDialog *impl_from_IOleWindow(IOleWindow *iface)
85 return CONTAINING_RECORD(iface, ProgressDialog, IOleWindow_iface);
88 static void set_buffer(LPWSTR *buffer, LPCWSTR string)
90 static const WCHAR empty_string[] = {0};
95 string = empty_string;
96 CoGetMalloc(1, &malloc);
98 cb = (strlenW(string) + 1)*sizeof(WCHAR);
99 if (*buffer == NULL || cb > IMalloc_GetSize(malloc, *buffer))
100 *buffer = IMalloc_Realloc(malloc, *buffer, cb);
101 memcpy(*buffer, string, cb);
106 ProgressDialog *This;
111 static LPWSTR load_string(HINSTANCE hInstance, UINT uiResourceId)
116 LoadStringW(hInstance, uiResourceId, string, sizeof(string)/sizeof(string[0]));
117 ret = HeapAlloc(GetProcessHeap(), 0, (strlenW(string) + 1) * sizeof(WCHAR));
118 strcpyW(ret, string);
122 static void set_progress_marquee(ProgressDialog *This)
124 HWND hProgress = GetDlgItem(This->hwnd, IDC_PROGRESS_BAR);
125 SetWindowLongW(hProgress, GWL_STYLE,
126 GetWindowLongW(hProgress, GWL_STYLE)|PBS_MARQUEE);
129 static void update_dialog(ProgressDialog *This, DWORD dwUpdate)
133 if (dwUpdate & UPDATE_TITLE)
134 SetWindowTextW(This->hwnd, This->title);
136 if (dwUpdate & UPDATE_LINE1)
137 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE, (This->isCancelled ? empty : This->lines[0]));
138 if (dwUpdate & UPDATE_LINE2)
139 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+1, (This->isCancelled ? empty : This->lines[1]));
140 if (dwUpdate & UPDATE_LINE3)
141 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+2, (This->isCancelled ? This->cancelMsg : This->lines[2]));
143 if (dwUpdate & UPDATE_PROGRESS)
145 ULONGLONG ullTotal = This->ullTotal;
146 ULONGLONG ullCompleted = This->ullCompleted;
148 /* progress bar requires 32-bit coordinates */
149 while (ullTotal >> 32)
155 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETRANGE32, 0, (DWORD)ullTotal);
156 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETPOS, (DWORD)ullCompleted, 0);
160 static void end_dialog(ProgressDialog *This)
162 SendMessageW(This->hwnd, WM_DLG_DESTROY, 0, 0);
163 /* native doesn't re-enable the window? */
164 if (This->hwndDisabledParent)
165 EnableWindow(This->hwndDisabledParent, TRUE);
169 static INT_PTR CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
171 ProgressDialog *This = (ProgressDialog *)GetWindowLongPtrW(hwnd, DWLP_USER);
177 struct create_params *params = (struct create_params *)lParam;
179 /* Note: until we set the hEvent, the object is protected by
180 * the critical section held by StartProgress */
181 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)params->This);
185 if (This->dwFlags & PROGDLG_NOPROGRESSBAR)
186 ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS_BAR), SW_HIDE);
187 if (This->dwFlags & PROGDLG_NOCANCEL)
188 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
189 if (This->dwFlags & PROGDLG_MARQUEEPROGRESS)
190 set_progress_marquee(This);
191 if (This->dwFlags & PROGDLG_NOMINIMIZE)
192 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & (~WS_MINIMIZEBOX));
194 update_dialog(This, 0xffffffff);
196 This->isCancelled = FALSE;
197 SetEvent(params->hEvent);
202 EnterCriticalSection(&This->cs);
203 update_dialog(This, This->dwUpdate);
205 LeaveCriticalSection(&This->cs);
210 PostThreadMessageW(GetCurrentThreadId(), WM_NULL, 0, 0); /* wake up the GetMessage */
215 if (msg == WM_CLOSE || wParam == IDCANCEL)
217 EnterCriticalSection(&This->cs);
218 This->isCancelled = TRUE;
220 if (!This->cancelMsg)
221 This->cancelMsg = load_string(BROWSEUI_hinstance, IDS_CANCELLING);
223 set_progress_marquee(This);
224 EnableWindow(GetDlgItem(This->hwnd, IDCANCEL), FALSE);
225 update_dialog(This, UPDATE_LINE1|UPDATE_LINE2|UPDATE_LINE3);
226 LeaveCriticalSection(&This->cs);
233 static DWORD WINAPI dialog_thread(LPVOID lpParameter)
235 /* Note: until we set the hEvent in WM_INITDIALOG, the ProgressDialog object
236 * is protected by the critical section held by StartProgress */
237 struct create_params *params = lpParameter;
241 hwnd = CreateDialogParamW(BROWSEUI_hinstance, MAKEINTRESOURCEW(IDD_PROGRESS_DLG),
242 params->hwndParent, dialog_proc, (LPARAM)params);
244 while (GetMessageW(&msg, NULL, 0, 0) > 0)
248 if(!IsDialogMessageW(hwnd, &msg))
250 TranslateMessage(&msg);
251 DispatchMessageW(&msg);
258 static void ProgressDialog_Destructor(ProgressDialog *This)
260 TRACE("destroying %p\n", This);
263 heap_free(This->lines[0]);
264 heap_free(This->lines[1]);
265 heap_free(This->lines[2]);
266 heap_free(This->cancelMsg);
267 heap_free(This->title);
269 This->cs.DebugInfo->Spare[0] = 0;
270 DeleteCriticalSection(&This->cs);
274 static HRESULT WINAPI ProgressDialog_QueryInterface(IProgressDialog *iface, REFIID iid, LPVOID *ppvOut)
276 ProgressDialog *This = impl_from_IProgressDialog(iface);
278 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(iid), ppvOut);
283 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IProgressDialog))
287 else if (IsEqualIID(iid, &IID_IOleWindow))
289 *ppvOut = &This->IOleWindow_iface;
294 IUnknown_AddRef(iface);
298 WARN("unsupported interface: %s\n", debugstr_guid(iid));
299 return E_NOINTERFACE;
302 static ULONG WINAPI ProgressDialog_AddRef(IProgressDialog *iface)
304 ProgressDialog *This = impl_from_IProgressDialog(iface);
305 return InterlockedIncrement(&This->refCount);
308 static ULONG WINAPI ProgressDialog_Release(IProgressDialog *iface)
310 ProgressDialog *This = impl_from_IProgressDialog(iface);
313 ret = InterlockedDecrement(&This->refCount);
315 ProgressDialog_Destructor(This);
319 static HRESULT WINAPI ProgressDialog_StartProgressDialog(IProgressDialog *iface, HWND hwndParent, IUnknown *punkEnableModeless, DWORD dwFlags, LPCVOID reserved)
321 static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_ANIMATE_CLASS };
322 ProgressDialog *This = impl_from_IProgressDialog(iface);
323 struct create_params params;
326 TRACE("(%p, %p, %x, %p)\n", iface, punkEnableModeless, dwFlags, reserved);
327 if (punkEnableModeless || reserved)
328 FIXME("Reserved parameters not null (%p, %p)\n", punkEnableModeless, reserved);
329 if (dwFlags & PROGDLG_AUTOTIME)
330 FIXME("Flags PROGDLG_AUTOTIME not supported\n");
331 if (dwFlags & PROGDLG_NOTIME)
332 FIXME("Flags PROGDLG_NOTIME not supported\n");
334 InitCommonControlsEx( &init );
336 EnterCriticalSection(&This->cs);
340 LeaveCriticalSection(&This->cs);
341 return S_OK; /* as on XP */
343 This->dwFlags = dwFlags;
345 params.hwndParent = hwndParent;
346 params.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
348 hThread = CreateThread(NULL, 0, dialog_thread, ¶ms, 0, NULL);
349 WaitForSingleObject(params.hEvent, INFINITE);
350 CloseHandle(params.hEvent);
351 CloseHandle(hThread);
353 This->hwndDisabledParent = NULL;
354 if (hwndParent && (dwFlags & PROGDLG_MODAL))
356 HWND hwndDisable = GetAncestor(hwndParent, GA_ROOT);
357 if (EnableWindow(hwndDisable, FALSE))
358 This->hwndDisabledParent = hwndDisable;
361 LeaveCriticalSection(&This->cs);
366 static HRESULT WINAPI ProgressDialog_StopProgressDialog(IProgressDialog *iface)
368 ProgressDialog *This = impl_from_IProgressDialog(iface);
370 EnterCriticalSection(&This->cs);
373 LeaveCriticalSection(&This->cs);
378 static HRESULT WINAPI ProgressDialog_SetTitle(IProgressDialog *iface, LPCWSTR pwzTitle)
380 ProgressDialog *This = impl_from_IProgressDialog(iface);
383 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzTitle));
385 EnterCriticalSection(&This->cs);
386 set_buffer(&This->title, pwzTitle);
387 This->dwUpdate |= UPDATE_TITLE;
389 LeaveCriticalSection(&This->cs);
392 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
397 static HRESULT WINAPI ProgressDialog_SetAnimation(IProgressDialog *iface, HINSTANCE hInstance, UINT uiResourceId)
399 FIXME("(%p, %p, %d) - stub\n", iface, hInstance, uiResourceId);
403 static BOOL WINAPI ProgressDialog_HasUserCancelled(IProgressDialog *iface)
405 ProgressDialog *This = impl_from_IProgressDialog(iface);
406 return This->isCancelled;
409 static HRESULT WINAPI ProgressDialog_SetProgress64(IProgressDialog *iface, ULONGLONG ullCompleted, ULONGLONG ullTotal)
411 ProgressDialog *This = impl_from_IProgressDialog(iface);
414 TRACE("(%p, 0x%s, 0x%s)\n", This, wine_dbgstr_longlong(ullCompleted), wine_dbgstr_longlong(ullTotal));
416 EnterCriticalSection(&This->cs);
417 This->ullTotal = ullTotal;
418 This->ullCompleted = ullCompleted;
419 This->dwUpdate |= UPDATE_PROGRESS;
421 LeaveCriticalSection(&This->cs);
424 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
426 return S_OK; /* Windows sometimes returns S_FALSE */
429 static HRESULT WINAPI ProgressDialog_SetProgress(IProgressDialog *iface, DWORD dwCompleted, DWORD dwTotal)
431 return IProgressDialog_SetProgress64(iface, dwCompleted, dwTotal);
434 static HRESULT WINAPI ProgressDialog_SetLine(IProgressDialog *iface, DWORD dwLineNum, LPCWSTR pwzLine, BOOL bPath, LPCVOID reserved)
436 ProgressDialog *This = impl_from_IProgressDialog(iface);
439 TRACE("(%p, %d, %s, %d)\n", This, dwLineNum, wine_dbgstr_w(pwzLine), bPath);
442 FIXME("reserved pointer not null (%p)\n", reserved);
445 if (dwLineNum >= 3) /* Windows seems to do something like that */
448 EnterCriticalSection(&This->cs);
449 set_buffer(&This->lines[dwLineNum], pwzLine);
450 This->dwUpdate |= UPDATE_LINE1 << dwLineNum;
451 hwnd = (This->isCancelled ? NULL : This->hwnd); /* no sense to send the message if window cancelled */
452 LeaveCriticalSection(&This->cs);
455 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
460 static HRESULT WINAPI ProgressDialog_SetCancelMsg(IProgressDialog *iface, LPCWSTR pwzMsg, LPCVOID reserved)
462 ProgressDialog *This = impl_from_IProgressDialog(iface);
465 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzMsg));
468 FIXME("reserved pointer not null (%p)\n", reserved);
470 EnterCriticalSection(&This->cs);
471 set_buffer(&This->cancelMsg, pwzMsg);
472 This->dwUpdate |= UPDATE_LINE1 << CANCEL_MSG_LINE;
473 hwnd = (This->isCancelled ? This->hwnd : NULL); /* no sense to send the message if window not cancelled */
474 LeaveCriticalSection(&This->cs);
477 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
482 static HRESULT WINAPI ProgressDialog_Timer(IProgressDialog *iface, DWORD dwTimerAction, LPCVOID reserved)
484 ProgressDialog *This = impl_from_IProgressDialog(iface);
486 FIXME("(%p, %d, %p) - stub\n", This, dwTimerAction, reserved);
489 FIXME("Reserved field not NULL but %p\n", reserved);
494 static const IProgressDialogVtbl ProgressDialogVtbl =
496 ProgressDialog_QueryInterface,
497 ProgressDialog_AddRef,
498 ProgressDialog_Release,
500 ProgressDialog_StartProgressDialog,
501 ProgressDialog_StopProgressDialog,
502 ProgressDialog_SetTitle,
503 ProgressDialog_SetAnimation,
504 ProgressDialog_HasUserCancelled,
505 ProgressDialog_SetProgress,
506 ProgressDialog_SetProgress64,
507 ProgressDialog_SetLine,
508 ProgressDialog_SetCancelMsg,
512 static HRESULT WINAPI OleWindow_QueryInterface(IOleWindow *iface, REFIID iid, LPVOID *ppvOut)
514 ProgressDialog *This = impl_from_IOleWindow(iface);
515 return ProgressDialog_QueryInterface(&This->IProgressDialog_iface, iid, ppvOut);
518 static ULONG WINAPI OleWindow_AddRef(IOleWindow *iface)
520 ProgressDialog *This = impl_from_IOleWindow(iface);
521 return ProgressDialog_AddRef(&This->IProgressDialog_iface);
524 static ULONG WINAPI OleWindow_Release(IOleWindow *iface)
526 ProgressDialog *This = impl_from_IOleWindow(iface);
527 return ProgressDialog_Release(&This->IProgressDialog_iface);
530 static HRESULT WINAPI OleWindow_GetWindow(IOleWindow* iface, HWND* phwnd)
532 ProgressDialog *This = impl_from_IOleWindow(iface);
534 TRACE("(%p, %p)\n", This, phwnd);
535 EnterCriticalSection(&This->cs);
537 LeaveCriticalSection(&This->cs);
541 static HRESULT WINAPI OleWindow_ContextSensitiveHelp(IOleWindow* iface, BOOL fEnterMode)
543 ProgressDialog *This = impl_from_IOleWindow(iface);
545 FIXME("(%p, %d): stub\n", This, fEnterMode);
549 static const IOleWindowVtbl OleWindowVtbl =
551 OleWindow_QueryInterface,
555 OleWindow_ContextSensitiveHelp
559 HRESULT ProgressDialog_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
561 ProgressDialog *This;
563 return CLASS_E_NOAGGREGATION;
565 This = heap_alloc_zero(sizeof(ProgressDialog));
567 return E_OUTOFMEMORY;
569 This->IProgressDialog_iface.lpVtbl = &ProgressDialogVtbl;
570 This->IOleWindow_iface.lpVtbl = &OleWindowVtbl;
572 InitializeCriticalSection(&This->cs);
573 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ProgressDialog.cs");
575 TRACE("returning %p\n", This);
576 *ppOut = (IUnknown *)This;