crypt32: Support constructed strings in octet strings decoder.
[wine.git] / dlls / browseui / progressdlg.c
blob09c1996a597373ac96a079d4cf07474374d94fed
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/heap.h"
40 #include "wine/unicode.h"
42 #include "browseui.h"
43 #include "resids.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(browseui);
47 #define CANCEL_MSG_LINE 2
49 /* Note: to avoid a deadlock we don't want to send messages to the dialog
50 * with the critical section held. Instead we only mark what fields should be
51 * updated and the dialog proc does the update */
52 #define UPDATE_PROGRESS 0x1
53 #define UPDATE_TITLE 0x2
54 #define UPDATE_LINE1 0x4
55 #define UPDATE_LINE2 (UPDATE_LINE1<<1)
56 #define UPDATE_LINE3 (UPDATE_LINE2<<2)
59 #define WM_DLG_UPDATE (WM_APP+1) /* set to the dialog when it should update */
60 #define WM_DLG_DESTROY (WM_APP+2) /* DestroyWindow must be called from the owning thread */
62 typedef struct tagProgressDialog {
63 IProgressDialog IProgressDialog_iface;
64 IOleWindow IOleWindow_iface;
65 LONG refCount;
66 CRITICAL_SECTION cs;
67 HWND hwnd;
68 DWORD dwFlags;
69 DWORD dwUpdate;
70 LPWSTR lines[3];
71 LPWSTR cancelMsg;
72 LPWSTR title;
73 BOOL isCancelled;
74 ULONGLONG ullCompleted;
75 ULONGLONG ullTotal;
76 HWND hwndDisabledParent; /* For modal dialog: the parent that need to be re-enabled when the dialog ends */
77 } ProgressDialog;
79 static inline ProgressDialog *impl_from_IProgressDialog(IProgressDialog *iface)
81 return CONTAINING_RECORD(iface, ProgressDialog, IProgressDialog_iface);
84 static inline ProgressDialog *impl_from_IOleWindow(IOleWindow *iface)
86 return CONTAINING_RECORD(iface, ProgressDialog, IOleWindow_iface);
89 static void set_buffer(LPWSTR *buffer, LPCWSTR string)
91 static const WCHAR empty_string[] = {0};
92 IMalloc *malloc;
93 ULONG cb;
95 if (string == NULL)
96 string = empty_string;
97 CoGetMalloc(MEMCTX_TASK, &malloc);
99 cb = (strlenW(string) + 1)*sizeof(WCHAR);
100 if (*buffer == NULL || cb > IMalloc_GetSize(malloc, *buffer))
101 *buffer = IMalloc_Realloc(malloc, *buffer, cb);
102 memcpy(*buffer, string, cb);
105 struct create_params
107 ProgressDialog *This;
108 HANDLE hEvent;
109 HWND hwndParent;
112 static LPWSTR load_string(HINSTANCE hInstance, UINT uiResourceId)
114 WCHAR string[256];
115 LPWSTR ret;
117 LoadStringW(hInstance, uiResourceId, string, sizeof(string)/sizeof(string[0]));
118 ret = HeapAlloc(GetProcessHeap(), 0, (strlenW(string) + 1) * sizeof(WCHAR));
119 strcpyW(ret, string);
120 return ret;
123 static void set_progress_marquee(ProgressDialog *This)
125 HWND hProgress = GetDlgItem(This->hwnd, IDC_PROGRESS_BAR);
126 SetWindowLongW(hProgress, GWL_STYLE,
127 GetWindowLongW(hProgress, GWL_STYLE)|PBS_MARQUEE);
130 static void update_dialog(ProgressDialog *This, DWORD dwUpdate)
132 WCHAR empty[] = {0};
134 if (dwUpdate & UPDATE_TITLE)
135 SetWindowTextW(This->hwnd, This->title);
137 if (dwUpdate & UPDATE_LINE1)
138 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE, (This->isCancelled ? empty : This->lines[0]));
139 if (dwUpdate & UPDATE_LINE2)
140 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+1, (This->isCancelled ? empty : This->lines[1]));
141 if (dwUpdate & UPDATE_LINE3)
142 SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+2, (This->isCancelled ? This->cancelMsg : This->lines[2]));
144 if (dwUpdate & UPDATE_PROGRESS)
146 ULONGLONG ullTotal = This->ullTotal;
147 ULONGLONG ullCompleted = This->ullCompleted;
149 /* progress bar requires 32-bit coordinates */
150 while (ullTotal >> 32)
152 ullTotal >>= 1;
153 ullCompleted >>= 1;
156 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETRANGE32, 0, (DWORD)ullTotal);
157 SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETPOS, (DWORD)ullCompleted, 0);
161 static void end_dialog(ProgressDialog *This)
163 SendMessageW(This->hwnd, WM_DLG_DESTROY, 0, 0);
164 /* native doesn't re-enable the window? */
165 if (This->hwndDisabledParent)
166 EnableWindow(This->hwndDisabledParent, TRUE);
167 This->hwnd = NULL;
170 static INT_PTR CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
172 ProgressDialog *This = (ProgressDialog *)GetWindowLongPtrW(hwnd, DWLP_USER);
174 switch (msg)
176 case WM_INITDIALOG:
178 struct create_params *params = (struct create_params *)lParam;
180 /* Note: until we set the hEvent, the object is protected by
181 * the critical section held by StartProgress */
182 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)params->This);
183 This = params->This;
184 This->hwnd = hwnd;
186 if (This->dwFlags & PROGDLG_NOPROGRESSBAR)
187 ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS_BAR), SW_HIDE);
188 if (This->dwFlags & PROGDLG_NOCANCEL)
189 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
190 if (This->dwFlags & PROGDLG_MARQUEEPROGRESS)
191 set_progress_marquee(This);
192 if (This->dwFlags & PROGDLG_NOMINIMIZE)
193 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & (~WS_MINIMIZEBOX));
195 update_dialog(This, 0xffffffff);
196 This->dwUpdate = 0;
197 This->isCancelled = FALSE;
198 SetEvent(params->hEvent);
199 return TRUE;
202 case WM_DLG_UPDATE:
203 EnterCriticalSection(&This->cs);
204 update_dialog(This, This->dwUpdate);
205 This->dwUpdate = 0;
206 LeaveCriticalSection(&This->cs);
207 return TRUE;
209 case WM_DLG_DESTROY:
210 DestroyWindow(hwnd);
211 PostThreadMessageW(GetCurrentThreadId(), WM_NULL, 0, 0); /* wake up the GetMessage */
212 return TRUE;
214 case WM_CLOSE:
215 case WM_COMMAND:
216 if (msg == WM_CLOSE || wParam == IDCANCEL)
218 EnterCriticalSection(&This->cs);
219 This->isCancelled = TRUE;
221 if (!This->cancelMsg)
222 This->cancelMsg = load_string(BROWSEUI_hinstance, IDS_CANCELLING);
224 set_progress_marquee(This);
225 EnableWindow(GetDlgItem(This->hwnd, IDCANCEL), FALSE);
226 update_dialog(This, UPDATE_LINE1|UPDATE_LINE2|UPDATE_LINE3);
227 LeaveCriticalSection(&This->cs);
229 return TRUE;
231 return FALSE;
234 static DWORD WINAPI dialog_thread(LPVOID lpParameter)
236 /* Note: until we set the hEvent in WM_INITDIALOG, the ProgressDialog object
237 * is protected by the critical section held by StartProgress */
238 struct create_params *params = lpParameter;
239 ProgressDialog *This = params->This;
240 HWND hwnd;
241 MSG msg;
243 hwnd = CreateDialogParamW(BROWSEUI_hinstance, MAKEINTRESOURCEW(IDD_PROGRESS_DLG),
244 params->hwndParent, dialog_proc, (LPARAM)params);
246 while (GetMessageW(&msg, NULL, 0, 0) > 0)
248 if (!IsWindow(hwnd))
249 break;
250 if(!IsDialogMessageW(hwnd, &msg))
252 TranslateMessage(&msg);
253 DispatchMessageW(&msg);
257 IProgressDialog_Release(&This->IProgressDialog_iface);
258 return 0;
261 static void ProgressDialog_Destructor(ProgressDialog *This)
263 TRACE("destroying %p\n", This);
264 if (This->hwnd)
265 end_dialog(This);
266 heap_free(This->lines[0]);
267 heap_free(This->lines[1]);
268 heap_free(This->lines[2]);
269 heap_free(This->cancelMsg);
270 heap_free(This->title);
271 This->cs.DebugInfo->Spare[0] = 0;
272 DeleteCriticalSection(&This->cs);
273 heap_free(This);
274 InterlockedDecrement(&BROWSEUI_refCount);
277 static HRESULT WINAPI ProgressDialog_QueryInterface(IProgressDialog *iface, REFIID iid, LPVOID *ppvOut)
279 ProgressDialog *This = impl_from_IProgressDialog(iface);
281 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(iid), ppvOut);
282 if (!ppvOut)
283 return E_POINTER;
285 *ppvOut = NULL;
286 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IProgressDialog))
288 *ppvOut = iface;
290 else if (IsEqualIID(iid, &IID_IOleWindow))
292 *ppvOut = &This->IOleWindow_iface;
295 if (*ppvOut)
297 IProgressDialog_AddRef(iface);
298 return S_OK;
301 WARN("unsupported interface: %s\n", debugstr_guid(iid));
302 return E_NOINTERFACE;
305 static ULONG WINAPI ProgressDialog_AddRef(IProgressDialog *iface)
307 ProgressDialog *This = impl_from_IProgressDialog(iface);
308 return InterlockedIncrement(&This->refCount);
311 static ULONG WINAPI ProgressDialog_Release(IProgressDialog *iface)
313 ProgressDialog *This = impl_from_IProgressDialog(iface);
314 ULONG ret;
316 ret = InterlockedDecrement(&This->refCount);
317 if (ret == 0)
318 ProgressDialog_Destructor(This);
319 return ret;
322 static HRESULT WINAPI ProgressDialog_StartProgressDialog(IProgressDialog *iface, HWND hwndParent, IUnknown *punkEnableModeless, DWORD dwFlags, LPCVOID reserved)
324 static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_ANIMATE_CLASS };
325 ProgressDialog *This = impl_from_IProgressDialog(iface);
326 struct create_params params;
327 HANDLE hThread;
329 TRACE("(%p, %p, %x, %p)\n", iface, punkEnableModeless, dwFlags, reserved);
330 if (punkEnableModeless || reserved)
331 FIXME("Reserved parameters not null (%p, %p)\n", punkEnableModeless, reserved);
332 if (dwFlags & PROGDLG_AUTOTIME)
333 FIXME("Flags PROGDLG_AUTOTIME not supported\n");
334 if (dwFlags & PROGDLG_NOTIME)
335 FIXME("Flags PROGDLG_NOTIME not supported\n");
337 InitCommonControlsEx( &init );
339 EnterCriticalSection(&This->cs);
341 if (This->hwnd)
343 LeaveCriticalSection(&This->cs);
344 return S_OK; /* as on XP */
346 This->dwFlags = dwFlags;
348 params.This = This;
349 params.hwndParent = hwndParent;
350 params.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
352 /* thread holds one reference to ensure clean shutdown */
353 IProgressDialog_AddRef(&This->IProgressDialog_iface);
355 hThread = CreateThread(NULL, 0, dialog_thread, &params, 0, NULL);
356 WaitForSingleObject(params.hEvent, INFINITE);
357 CloseHandle(params.hEvent);
358 CloseHandle(hThread);
360 This->hwndDisabledParent = NULL;
361 if (hwndParent && (dwFlags & PROGDLG_MODAL))
363 HWND hwndDisable = GetAncestor(hwndParent, GA_ROOT);
364 if (EnableWindow(hwndDisable, FALSE))
365 This->hwndDisabledParent = hwndDisable;
368 LeaveCriticalSection(&This->cs);
370 return S_OK;
373 static HRESULT WINAPI ProgressDialog_StopProgressDialog(IProgressDialog *iface)
375 ProgressDialog *This = impl_from_IProgressDialog(iface);
377 EnterCriticalSection(&This->cs);
378 if (This->hwnd)
379 end_dialog(This);
380 LeaveCriticalSection(&This->cs);
382 return S_OK;
385 static HRESULT WINAPI ProgressDialog_SetTitle(IProgressDialog *iface, LPCWSTR pwzTitle)
387 ProgressDialog *This = impl_from_IProgressDialog(iface);
388 HWND hwnd;
390 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzTitle));
392 EnterCriticalSection(&This->cs);
393 set_buffer(&This->title, pwzTitle);
394 This->dwUpdate |= UPDATE_TITLE;
395 hwnd = This->hwnd;
396 LeaveCriticalSection(&This->cs);
398 if (hwnd)
399 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
401 return S_OK;
404 static HRESULT WINAPI ProgressDialog_SetAnimation(IProgressDialog *iface, HINSTANCE hInstance, UINT uiResourceId)
406 FIXME("(%p, %p, %d) - stub\n", iface, hInstance, uiResourceId);
407 return S_OK;
410 static BOOL WINAPI ProgressDialog_HasUserCancelled(IProgressDialog *iface)
412 ProgressDialog *This = impl_from_IProgressDialog(iface);
413 return This->isCancelled;
416 static HRESULT WINAPI ProgressDialog_SetProgress64(IProgressDialog *iface, ULONGLONG ullCompleted, ULONGLONG ullTotal)
418 ProgressDialog *This = impl_from_IProgressDialog(iface);
419 HWND hwnd;
421 TRACE("(%p, 0x%s, 0x%s)\n", This, wine_dbgstr_longlong(ullCompleted), wine_dbgstr_longlong(ullTotal));
423 EnterCriticalSection(&This->cs);
424 This->ullTotal = ullTotal;
425 This->ullCompleted = ullCompleted;
426 This->dwUpdate |= UPDATE_PROGRESS;
427 hwnd = This->hwnd;
428 LeaveCriticalSection(&This->cs);
430 if (hwnd)
431 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
433 return S_OK; /* Windows sometimes returns S_FALSE */
436 static HRESULT WINAPI ProgressDialog_SetProgress(IProgressDialog *iface, DWORD dwCompleted, DWORD dwTotal)
438 return IProgressDialog_SetProgress64(iface, dwCompleted, dwTotal);
441 static HRESULT WINAPI ProgressDialog_SetLine(IProgressDialog *iface, DWORD dwLineNum, LPCWSTR pwzLine, BOOL bPath, LPCVOID reserved)
443 ProgressDialog *This = impl_from_IProgressDialog(iface);
444 HWND hwnd;
446 TRACE("(%p, %d, %s, %d)\n", This, dwLineNum, wine_dbgstr_w(pwzLine), bPath);
448 if (reserved)
449 FIXME("reserved pointer not null (%p)\n", reserved);
451 dwLineNum--;
452 if (dwLineNum >= 3) /* Windows seems to do something like that */
453 dwLineNum = 0;
455 EnterCriticalSection(&This->cs);
456 set_buffer(&This->lines[dwLineNum], pwzLine);
457 This->dwUpdate |= UPDATE_LINE1 << dwLineNum;
458 hwnd = (This->isCancelled ? NULL : This->hwnd); /* no sense to send the message if window cancelled */
459 LeaveCriticalSection(&This->cs);
461 if (hwnd)
462 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
464 return S_OK;
467 static HRESULT WINAPI ProgressDialog_SetCancelMsg(IProgressDialog *iface, LPCWSTR pwzMsg, LPCVOID reserved)
469 ProgressDialog *This = impl_from_IProgressDialog(iface);
470 HWND hwnd;
472 TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzMsg));
474 if (reserved)
475 FIXME("reserved pointer not null (%p)\n", reserved);
477 EnterCriticalSection(&This->cs);
478 set_buffer(&This->cancelMsg, pwzMsg);
479 This->dwUpdate |= UPDATE_LINE1 << CANCEL_MSG_LINE;
480 hwnd = (This->isCancelled ? This->hwnd : NULL); /* no sense to send the message if window not cancelled */
481 LeaveCriticalSection(&This->cs);
483 if (hwnd)
484 SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
486 return S_OK;
489 static HRESULT WINAPI ProgressDialog_Timer(IProgressDialog *iface, DWORD dwTimerAction, LPCVOID reserved)
491 ProgressDialog *This = impl_from_IProgressDialog(iface);
493 FIXME("(%p, %d, %p) - stub\n", This, dwTimerAction, reserved);
495 if (reserved)
496 FIXME("Reserved field not NULL but %p\n", reserved);
498 return S_OK;
501 static const IProgressDialogVtbl ProgressDialogVtbl =
503 ProgressDialog_QueryInterface,
504 ProgressDialog_AddRef,
505 ProgressDialog_Release,
507 ProgressDialog_StartProgressDialog,
508 ProgressDialog_StopProgressDialog,
509 ProgressDialog_SetTitle,
510 ProgressDialog_SetAnimation,
511 ProgressDialog_HasUserCancelled,
512 ProgressDialog_SetProgress,
513 ProgressDialog_SetProgress64,
514 ProgressDialog_SetLine,
515 ProgressDialog_SetCancelMsg,
516 ProgressDialog_Timer
519 static HRESULT WINAPI OleWindow_QueryInterface(IOleWindow *iface, REFIID iid, LPVOID *ppvOut)
521 ProgressDialog *This = impl_from_IOleWindow(iface);
522 return ProgressDialog_QueryInterface(&This->IProgressDialog_iface, iid, ppvOut);
525 static ULONG WINAPI OleWindow_AddRef(IOleWindow *iface)
527 ProgressDialog *This = impl_from_IOleWindow(iface);
528 return ProgressDialog_AddRef(&This->IProgressDialog_iface);
531 static ULONG WINAPI OleWindow_Release(IOleWindow *iface)
533 ProgressDialog *This = impl_from_IOleWindow(iface);
534 return ProgressDialog_Release(&This->IProgressDialog_iface);
537 static HRESULT WINAPI OleWindow_GetWindow(IOleWindow* iface, HWND* phwnd)
539 ProgressDialog *This = impl_from_IOleWindow(iface);
541 TRACE("(%p, %p)\n", This, phwnd);
542 EnterCriticalSection(&This->cs);
543 *phwnd = This->hwnd;
544 LeaveCriticalSection(&This->cs);
545 return S_OK;
548 static HRESULT WINAPI OleWindow_ContextSensitiveHelp(IOleWindow* iface, BOOL fEnterMode)
550 ProgressDialog *This = impl_from_IOleWindow(iface);
552 FIXME("(%p, %d): stub\n", This, fEnterMode);
553 return E_NOTIMPL;
556 static const IOleWindowVtbl OleWindowVtbl =
558 OleWindow_QueryInterface,
559 OleWindow_AddRef,
560 OleWindow_Release,
561 OleWindow_GetWindow,
562 OleWindow_ContextSensitiveHelp
566 HRESULT ProgressDialog_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
568 ProgressDialog *This;
569 if (pUnkOuter)
570 return CLASS_E_NOAGGREGATION;
572 This = heap_alloc_zero(sizeof(ProgressDialog));
573 if (This == NULL)
574 return E_OUTOFMEMORY;
576 This->IProgressDialog_iface.lpVtbl = &ProgressDialogVtbl;
577 This->IOleWindow_iface.lpVtbl = &OleWindowVtbl;
578 This->refCount = 1;
579 InitializeCriticalSection(&This->cs);
580 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ProgressDialog.cs");
582 TRACE("returning %p\n", This);
583 *ppOut = (IUnknown *)&This->IProgressDialog_iface;
584 InterlockedIncrement(&BROWSEUI_refCount);
585 return S_OK;