d3d11: Implement d3d11_device_CreateDepthStencilView().
[wine/multimedia.git] / dlls / browseui / progressdlg.c
blob70c78c97bcbc51b460a80c172158a8a69d6dde99
1 /*
2 * Progress dialog
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
21 #include "config.h"
23 #include <stdarg.h>
25 #define COBJMACROS
27 #include "wine/debug.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winuser.h"
32 #include "shlwapi.h"
33 #include "winerror.h"
34 #include "objbase.h"
36 #include "shlguid.h"
37 #include "shlobj.h"
39 #include "wine/unicode.h"
41 #include "browseui.h"
42 #include "resids.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;
64 LONG refCount;
65 CRITICAL_SECTION cs;
66 HWND hwnd;
67 DWORD dwFlags;
68 DWORD dwUpdate;
69 LPWSTR lines[3];
70 LPWSTR cancelMsg;
71 LPWSTR title;
72 BOOL isCancelled;
73 ULONGLONG ullCompleted;
74 ULONGLONG ullTotal;
75 HWND hwndDisabledParent; /* For modal dialog: the parent that need to be re-enabled when the dialog ends */
76 } ProgressDialog;
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};
91 IMalloc *malloc;
92 ULONG cb;
94 if (string == NULL)
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);
104 struct create_params
106 ProgressDialog *This;
107 HANDLE hEvent;
108 HWND hwndParent;
111 static LPWSTR load_string(HINSTANCE hInstance, UINT uiResourceId)
113 WCHAR string[256];
114 LPWSTR ret;
116 LoadStringW(hInstance, uiResourceId, string, sizeof(string)/sizeof(string[0]));
117 ret = HeapAlloc(GetProcessHeap(), 0, (strlenW(string) + 1) * sizeof(WCHAR));
118 strcpyW(ret, string);
119 return ret;
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)
131 WCHAR empty[] = {0};
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)
151 ullTotal >>= 1;
152 ullCompleted >>= 1;
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);
166 This->hwnd = NULL;
169 static INT_PTR CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
171 ProgressDialog *This = (ProgressDialog *)GetWindowLongPtrW(hwnd, DWLP_USER);
173 switch (msg)
175 case WM_INITDIALOG:
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);
182 This = params->This;
183 This->hwnd = hwnd;
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);
195 This->dwUpdate = 0;
196 This->isCancelled = FALSE;
197 SetEvent(params->hEvent);
198 return TRUE;
201 case WM_DLG_UPDATE:
202 EnterCriticalSection(&This->cs);
203 update_dialog(This, This->dwUpdate);
204 This->dwUpdate = 0;
205 LeaveCriticalSection(&This->cs);
206 return TRUE;
208 case WM_DLG_DESTROY:
209 DestroyWindow(hwnd);
210 PostThreadMessageW(GetCurrentThreadId(), WM_NULL, 0, 0); /* wake up the GetMessage */
211 return TRUE;
213 case WM_CLOSE:
214 case WM_COMMAND:
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);
228 return TRUE;
230 return FALSE;
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;
238 ProgressDialog *This = params->This;
239 HWND hwnd;
240 MSG msg;
242 hwnd = CreateDialogParamW(BROWSEUI_hinstance, MAKEINTRESOURCEW(IDD_PROGRESS_DLG),
243 params->hwndParent, dialog_proc, (LPARAM)params);
245 while (GetMessageW(&msg, NULL, 0, 0) > 0)
247 if (!IsWindow(hwnd))
248 break;
249 if(!IsDialogMessageW(hwnd, &msg))
251 TranslateMessage(&msg);
252 DispatchMessageW(&msg);
256 IProgressDialog_Release(&This->IProgressDialog_iface);
257 return 0;
260 static void ProgressDialog_Destructor(ProgressDialog *This)
262 TRACE("destroying %p\n", This);
263 if (This->hwnd)
264 end_dialog(This);
265 heap_free(This->lines[0]);
266 heap_free(This->lines[1]);
267 heap_free(This->lines[2]);
268 heap_free(This->cancelMsg);
269 heap_free(This->title);
270 This->cs.DebugInfo->Spare[0] = 0;
271 DeleteCriticalSection(&This->cs);
272 heap_free(This);
273 InterlockedDecrement(&BROWSEUI_refCount);
276 static HRESULT WINAPI ProgressDialog_QueryInterface(IProgressDialog *iface, REFIID iid, LPVOID *ppvOut)
278 ProgressDialog *This = impl_from_IProgressDialog(iface);
280 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(iid), ppvOut);
281 if (!ppvOut)
282 return E_POINTER;
284 *ppvOut = NULL;
285 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IProgressDialog))
287 *ppvOut = iface;
289 else if (IsEqualIID(iid, &IID_IOleWindow))
291 *ppvOut = &This->IOleWindow_iface;
294 if (*ppvOut)
296 IProgressDialog_AddRef(iface);
297 return S_OK;
300 WARN("unsupported interface: %s\n", debugstr_guid(iid));
301 return E_NOINTERFACE;
304 static ULONG WINAPI ProgressDialog_AddRef(IProgressDialog *iface)
306 ProgressDialog *This = impl_from_IProgressDialog(iface);
307 return InterlockedIncrement(&This->refCount);
310 static ULONG WINAPI ProgressDialog_Release(IProgressDialog *iface)
312 ProgressDialog *This = impl_from_IProgressDialog(iface);
313 ULONG ret;
315 ret = InterlockedDecrement(&This->refCount);
316 if (ret == 0)
317 ProgressDialog_Destructor(This);
318 return ret;
321 static HRESULT WINAPI ProgressDialog_StartProgressDialog(IProgressDialog *iface, HWND hwndParent, IUnknown *punkEnableModeless, DWORD dwFlags, LPCVOID reserved)
323 static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_ANIMATE_CLASS };
324 ProgressDialog *This = impl_from_IProgressDialog(iface);
325 struct create_params params;
326 HANDLE hThread;
328 TRACE("(%p, %p, %x, %p)\n", iface, punkEnableModeless, dwFlags, reserved);
329 if (punkEnableModeless || reserved)
330 FIXME("Reserved parameters not null (%p, %p)\n", punkEnableModeless, reserved);
331 if (dwFlags & PROGDLG_AUTOTIME)
332 FIXME("Flags PROGDLG_AUTOTIME not supported\n");
333 if (dwFlags & PROGDLG_NOTIME)
334 FIXME("Flags PROGDLG_NOTIME not supported\n");
336 InitCommonControlsEx( &init );
338 EnterCriticalSection(&This->cs);
340 if (This->hwnd)
342 LeaveCriticalSection(&This->cs);
343 return S_OK; /* as on XP */
345 This->dwFlags = dwFlags;
347 params.This = This;
348 params.hwndParent = hwndParent;
349 params.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
351 /* thread holds one reference to ensure clean shutdown */
352 IProgressDialog_AddRef(&This->IProgressDialog_iface);
354 hThread = CreateThread(NULL, 0, dialog_thread, &params, 0, NULL);
355 WaitForSingleObject(params.hEvent, INFINITE);
356 CloseHandle(params.hEvent);
357 CloseHandle(hThread);
359 This->hwndDisabledParent = NULL;
360 if (hwndParent && (dwFlags & PROGDLG_MODAL))
362 HWND hwndDisable = GetAncestor(hwndParent, GA_ROOT);
363 if (EnableWindow(hwndDisable, FALSE))
364 This->hwndDisabledParent = hwndDisable;
367 LeaveCriticalSection(&This->cs);
369 return S_OK;
372 static HRESULT WINAPI ProgressDialog_StopProgressDialog(IProgressDialog *iface)
374 ProgressDialog *This = impl_from_IProgressDialog(iface);
376 EnterCriticalSection(&This->cs);
377 if (This->hwnd)
378 end_dialog(This);
379 LeaveCriticalSection(&This->cs);
381 return S_OK;
384 static HRESULT WINAPI ProgressDialog_SetTitle(IProgressDialog *iface, LPCWSTR pwzTitle)
386 ProgressDialog *This = impl_from_IProgressDialog(iface);
387 HWND hwnd;
389 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzTitle));
391 EnterCriticalSection(&This->cs);
392 set_buffer(&This->title, pwzTitle);
393 This->dwUpdate |= UPDATE_TITLE;
394 hwnd = This->hwnd;
395 LeaveCriticalSection(&This->cs);
397 if (hwnd)
398 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
400 return S_OK;
403 static HRESULT WINAPI ProgressDialog_SetAnimation(IProgressDialog *iface, HINSTANCE hInstance, UINT uiResourceId)
405 FIXME("(%p, %p, %d) - stub\n", iface, hInstance, uiResourceId);
406 return S_OK;
409 static BOOL WINAPI ProgressDialog_HasUserCancelled(IProgressDialog *iface)
411 ProgressDialog *This = impl_from_IProgressDialog(iface);
412 return This->isCancelled;
415 static HRESULT WINAPI ProgressDialog_SetProgress64(IProgressDialog *iface, ULONGLONG ullCompleted, ULONGLONG ullTotal)
417 ProgressDialog *This = impl_from_IProgressDialog(iface);
418 HWND hwnd;
420 TRACE("(%p, 0x%s, 0x%s)\n", This, wine_dbgstr_longlong(ullCompleted), wine_dbgstr_longlong(ullTotal));
422 EnterCriticalSection(&This->cs);
423 This->ullTotal = ullTotal;
424 This->ullCompleted = ullCompleted;
425 This->dwUpdate |= UPDATE_PROGRESS;
426 hwnd = This->hwnd;
427 LeaveCriticalSection(&This->cs);
429 if (hwnd)
430 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
432 return S_OK; /* Windows sometimes returns S_FALSE */
435 static HRESULT WINAPI ProgressDialog_SetProgress(IProgressDialog *iface, DWORD dwCompleted, DWORD dwTotal)
437 return IProgressDialog_SetProgress64(iface, dwCompleted, dwTotal);
440 static HRESULT WINAPI ProgressDialog_SetLine(IProgressDialog *iface, DWORD dwLineNum, LPCWSTR pwzLine, BOOL bPath, LPCVOID reserved)
442 ProgressDialog *This = impl_from_IProgressDialog(iface);
443 HWND hwnd;
445 TRACE("(%p, %d, %s, %d)\n", This, dwLineNum, wine_dbgstr_w(pwzLine), bPath);
447 if (reserved)
448 FIXME("reserved pointer not null (%p)\n", reserved);
450 dwLineNum--;
451 if (dwLineNum >= 3) /* Windows seems to do something like that */
452 dwLineNum = 0;
454 EnterCriticalSection(&This->cs);
455 set_buffer(&This->lines[dwLineNum], pwzLine);
456 This->dwUpdate |= UPDATE_LINE1 << dwLineNum;
457 hwnd = (This->isCancelled ? NULL : This->hwnd); /* no sense to send the message if window cancelled */
458 LeaveCriticalSection(&This->cs);
460 if (hwnd)
461 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
463 return S_OK;
466 static HRESULT WINAPI ProgressDialog_SetCancelMsg(IProgressDialog *iface, LPCWSTR pwzMsg, LPCVOID reserved)
468 ProgressDialog *This = impl_from_IProgressDialog(iface);
469 HWND hwnd;
471 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzMsg));
473 if (reserved)
474 FIXME("reserved pointer not null (%p)\n", reserved);
476 EnterCriticalSection(&This->cs);
477 set_buffer(&This->cancelMsg, pwzMsg);
478 This->dwUpdate |= UPDATE_LINE1 << CANCEL_MSG_LINE;
479 hwnd = (This->isCancelled ? This->hwnd : NULL); /* no sense to send the message if window not cancelled */
480 LeaveCriticalSection(&This->cs);
482 if (hwnd)
483 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
485 return S_OK;
488 static HRESULT WINAPI ProgressDialog_Timer(IProgressDialog *iface, DWORD dwTimerAction, LPCVOID reserved)
490 ProgressDialog *This = impl_from_IProgressDialog(iface);
492 FIXME("(%p, %d, %p) - stub\n", This, dwTimerAction, reserved);
494 if (reserved)
495 FIXME("Reserved field not NULL but %p\n", reserved);
497 return S_OK;
500 static const IProgressDialogVtbl ProgressDialogVtbl =
502 ProgressDialog_QueryInterface,
503 ProgressDialog_AddRef,
504 ProgressDialog_Release,
506 ProgressDialog_StartProgressDialog,
507 ProgressDialog_StopProgressDialog,
508 ProgressDialog_SetTitle,
509 ProgressDialog_SetAnimation,
510 ProgressDialog_HasUserCancelled,
511 ProgressDialog_SetProgress,
512 ProgressDialog_SetProgress64,
513 ProgressDialog_SetLine,
514 ProgressDialog_SetCancelMsg,
515 ProgressDialog_Timer
518 static HRESULT WINAPI OleWindow_QueryInterface(IOleWindow *iface, REFIID iid, LPVOID *ppvOut)
520 ProgressDialog *This = impl_from_IOleWindow(iface);
521 return ProgressDialog_QueryInterface(&This->IProgressDialog_iface, iid, ppvOut);
524 static ULONG WINAPI OleWindow_AddRef(IOleWindow *iface)
526 ProgressDialog *This = impl_from_IOleWindow(iface);
527 return ProgressDialog_AddRef(&This->IProgressDialog_iface);
530 static ULONG WINAPI OleWindow_Release(IOleWindow *iface)
532 ProgressDialog *This = impl_from_IOleWindow(iface);
533 return ProgressDialog_Release(&This->IProgressDialog_iface);
536 static HRESULT WINAPI OleWindow_GetWindow(IOleWindow* iface, HWND* phwnd)
538 ProgressDialog *This = impl_from_IOleWindow(iface);
540 TRACE("(%p, %p)\n", This, phwnd);
541 EnterCriticalSection(&This->cs);
542 *phwnd = This->hwnd;
543 LeaveCriticalSection(&This->cs);
544 return S_OK;
547 static HRESULT WINAPI OleWindow_ContextSensitiveHelp(IOleWindow* iface, BOOL fEnterMode)
549 ProgressDialog *This = impl_from_IOleWindow(iface);
551 FIXME("(%p, %d): stub\n", This, fEnterMode);
552 return E_NOTIMPL;
555 static const IOleWindowVtbl OleWindowVtbl =
557 OleWindow_QueryInterface,
558 OleWindow_AddRef,
559 OleWindow_Release,
560 OleWindow_GetWindow,
561 OleWindow_ContextSensitiveHelp
565 HRESULT ProgressDialog_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
567 ProgressDialog *This;
568 if (pUnkOuter)
569 return CLASS_E_NOAGGREGATION;
571 This = heap_alloc_zero(sizeof(ProgressDialog));
572 if (This == NULL)
573 return E_OUTOFMEMORY;
575 This->IProgressDialog_iface.lpVtbl = &ProgressDialogVtbl;
576 This->IOleWindow_iface.lpVtbl = &OleWindowVtbl;
577 This->refCount = 1;
578 InitializeCriticalSection(&This->cs);
579 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ProgressDialog.cs");
581 TRACE("returning %p\n", This);
582 *ppOut = (IUnknown *)&This->IProgressDialog_iface;
583 InterlockedIncrement(&BROWSEUI_refCount);
584 return S_OK;