winhttp: Reverse the order of arguments passed to Invoke.
[wine] / dlls / browseui / progressdlg.c
1 /*
2  *      Progress dialog
3  *
4  *      Copyright 2007  Mikolaj Zalewski
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26
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"
35
36 #include "shlguid.h"
37 #include "shlobj.h"
38
39 #include "wine/unicode.h"
40
41 #include "browseui.h"
42 #include "resids.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(browseui);
45
46 #define CANCEL_MSG_LINE 2
47
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)
56
57
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 */
60
61 typedef struct tagProgressDialog {
62     IProgressDialog IProgressDialog_iface;
63     LONG refCount;
64     CRITICAL_SECTION cs;
65     HWND hwnd;
66     DWORD dwFlags;
67     DWORD dwUpdate;
68     LPWSTR lines[3];
69     LPWSTR cancelMsg;
70     LPWSTR title;
71     BOOL isCancelled;
72     ULONGLONG ullCompleted;
73     ULONGLONG ullTotal;
74     HWND hwndDisabledParent;    /* For modal dialog: the parent that need to be re-enabled when the dialog ends */
75 } ProgressDialog;
76
77 static inline ProgressDialog *impl_from_IProgressDialog(IProgressDialog *iface)
78 {
79     return CONTAINING_RECORD(iface, ProgressDialog, IProgressDialog_iface);
80 }
81
82 static void set_buffer(LPWSTR *buffer, LPCWSTR string)
83 {
84     static const WCHAR empty_string[] = {0};
85     IMalloc *malloc;
86     ULONG cb;
87
88     if (string == NULL)
89         string = empty_string;
90     CoGetMalloc(1, &malloc);
91
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);
96 }
97
98 struct create_params
99 {
100     ProgressDialog *This;
101     HANDLE hEvent;
102     HWND hwndParent;
103 };
104
105 static LPWSTR load_string(HINSTANCE hInstance, UINT uiResourceId)
106 {
107     WCHAR string[256];
108     LPWSTR ret;
109
110     LoadStringW(hInstance, uiResourceId, string, sizeof(string)/sizeof(string[0]));
111     ret = HeapAlloc(GetProcessHeap(), 0, (strlenW(string) + 1) * sizeof(WCHAR));
112     strcpyW(ret, string);
113     return ret;
114 }
115
116 static void set_progress_marquee(ProgressDialog *This)
117 {
118     HWND hProgress = GetDlgItem(This->hwnd, IDC_PROGRESS_BAR);
119     SetWindowLongW(hProgress, GWL_STYLE,
120         GetWindowLongW(hProgress, GWL_STYLE)|PBS_MARQUEE);
121 }
122
123 static void update_dialog(ProgressDialog *This, DWORD dwUpdate)
124 {
125     WCHAR empty[] = {0};
126
127     if (dwUpdate & UPDATE_TITLE)
128         SetWindowTextW(This->hwnd, This->title);
129
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]));
136
137     if (dwUpdate & UPDATE_PROGRESS)
138     {
139         ULONGLONG ullTotal = This->ullTotal;
140         ULONGLONG ullCompleted = This->ullCompleted;
141
142         /* progress bar requires 32-bit coordinates */
143         while (ullTotal >> 32)
144         {
145             ullTotal >>= 1;
146             ullCompleted >>= 1;
147         }
148
149         SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETRANGE32, 0, (DWORD)ullTotal);
150         SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETPOS, (DWORD)ullCompleted, 0);
151     }
152 }
153
154 static void end_dialog(ProgressDialog *This)
155 {
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);
160     This->hwnd = NULL;
161 }
162
163 static INT_PTR CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
164 {
165     ProgressDialog *This = (ProgressDialog *)GetWindowLongPtrW(hwnd, DWLP_USER);
166
167     switch (msg)
168     {
169         case WM_INITDIALOG:
170         {
171             struct create_params *params = (struct create_params *)lParam;
172
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);
176             This = params->This;
177             This->hwnd = hwnd;
178
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));
187
188             update_dialog(This, 0xffffffff);
189             This->dwUpdate = 0;
190             This->isCancelled = FALSE;
191             SetEvent(params->hEvent);
192             return TRUE;
193         }
194
195         case WM_DLG_UPDATE:
196             EnterCriticalSection(&This->cs);
197             update_dialog(This, This->dwUpdate);
198             This->dwUpdate = 0;
199             LeaveCriticalSection(&This->cs);
200             return TRUE;
201
202         case WM_DLG_DESTROY:
203             DestroyWindow(hwnd);
204             PostThreadMessageW(GetCurrentThreadId(), WM_NULL, 0, 0); /* wake up the GetMessage */
205             return TRUE;
206
207         case WM_CLOSE:
208         case WM_COMMAND:
209             if (msg == WM_CLOSE || wParam == IDCANCEL)
210             {
211                 EnterCriticalSection(&This->cs);
212                 This->isCancelled = TRUE;
213
214                 if (!This->cancelMsg)
215                     This->cancelMsg = load_string(BROWSEUI_hinstance, IDS_CANCELLING);
216
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);
221             }
222             return TRUE;
223     }
224     return FALSE;
225 }
226
227 static DWORD WINAPI dialog_thread(LPVOID lpParameter)
228 {
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;
232     HWND hwnd;
233     MSG msg;
234
235     hwnd = CreateDialogParamW(BROWSEUI_hinstance, MAKEINTRESOURCEW(IDD_PROGRESS_DLG),
236         params->hwndParent, dialog_proc, (LPARAM)params);
237
238     while (GetMessageW(&msg, NULL, 0, 0) > 0)
239     {
240         if (!IsWindow(hwnd))
241             break;
242         if(!IsDialogMessageW(hwnd, &msg))
243         {
244             TranslateMessage(&msg);
245             DispatchMessageW(&msg);
246         }
247     }
248
249     return 0;
250 }
251
252 static void ProgressDialog_Destructor(ProgressDialog *This)
253 {
254     TRACE("destroying %p\n", This);
255     if (This->hwnd)
256         end_dialog(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);
262     heap_free(This);
263     This->cs.DebugInfo->Spare[0] = 0;
264     DeleteCriticalSection(&This->cs);
265     BROWSEUI_refCount--;
266 }
267
268 static HRESULT WINAPI ProgressDialog_QueryInterface(IProgressDialog *iface, REFIID iid, LPVOID *ppvOut)
269 {
270     ProgressDialog *This = impl_from_IProgressDialog(iface);
271     *ppvOut = NULL;
272
273     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IProgressDialog))
274     {
275         *ppvOut = This;
276     }
277
278     if (*ppvOut)
279     {
280         IUnknown_AddRef(iface);
281         return S_OK;
282     }
283
284     WARN("unsupported interface: %s\n", debugstr_guid(iid));
285     return E_NOINTERFACE;
286 }
287
288 static ULONG WINAPI ProgressDialog_AddRef(IProgressDialog *iface)
289 {
290     ProgressDialog *This = impl_from_IProgressDialog(iface);
291     return InterlockedIncrement(&This->refCount);
292 }
293
294 static ULONG WINAPI ProgressDialog_Release(IProgressDialog *iface)
295 {
296     ProgressDialog *This = impl_from_IProgressDialog(iface);
297     ULONG ret;
298
299     ret = InterlockedDecrement(&This->refCount);
300     if (ret == 0)
301         ProgressDialog_Destructor(This);
302     return ret;
303 }
304
305 static HRESULT WINAPI ProgressDialog_StartProgressDialog(IProgressDialog *iface, HWND hwndParent, IUnknown *punkEnableModeless, DWORD dwFlags, LPCVOID reserved)
306 {
307     ProgressDialog *This = impl_from_IProgressDialog(iface);
308     struct create_params params;
309     HANDLE hThread;
310
311     TRACE("(%p, %p, %x, %p)\n", iface, punkEnableModeless, dwFlags, reserved);
312     if (punkEnableModeless || reserved)
313         FIXME("Reserved parameters not null (%p, %p)\n", punkEnableModeless, reserved);
314     if (dwFlags & PROGDLG_AUTOTIME)
315         FIXME("Flags PROGDLG_AUTOTIME not supported\n");
316     if (dwFlags & PROGDLG_NOTIME)
317         FIXME("Flags PROGDLG_NOTIME not supported\n");
318
319     EnterCriticalSection(&This->cs);
320
321     if (This->hwnd)
322     {
323         LeaveCriticalSection(&This->cs);
324         return S_OK;  /* as on XP */
325     }
326     This->dwFlags = dwFlags;
327     params.This = This;
328     params.hwndParent = hwndParent;
329     params.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
330
331     hThread = CreateThread(NULL, 0, dialog_thread, &params, 0, NULL);
332     WaitForSingleObject(params.hEvent, INFINITE);
333     CloseHandle(params.hEvent);
334     CloseHandle(hThread);
335
336     This->hwndDisabledParent = NULL;
337     if (hwndParent && (dwFlags & PROGDLG_MODAL))
338     {
339         HWND hwndDisable = GetAncestor(hwndParent, GA_ROOT);
340         if (EnableWindow(hwndDisable, FALSE))
341             This->hwndDisabledParent = hwndDisable;
342     }
343
344     LeaveCriticalSection(&This->cs);
345
346     return S_OK;
347 }
348
349 static HRESULT WINAPI ProgressDialog_StopProgressDialog(IProgressDialog *iface)
350 {
351     ProgressDialog *This = impl_from_IProgressDialog(iface);
352
353     EnterCriticalSection(&This->cs);
354     if (This->hwnd)
355         end_dialog(This);
356     LeaveCriticalSection(&This->cs);
357
358     return S_OK;
359 }
360
361 static HRESULT WINAPI ProgressDialog_SetTitle(IProgressDialog *iface, LPCWSTR pwzTitle)
362 {
363     ProgressDialog *This = impl_from_IProgressDialog(iface);
364     HWND hwnd;
365
366     TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzTitle));
367
368     EnterCriticalSection(&This->cs);
369     set_buffer(&This->title, pwzTitle);
370     This->dwUpdate |= UPDATE_TITLE;
371     hwnd = This->hwnd;
372     LeaveCriticalSection(&This->cs);
373
374     if (hwnd)
375         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
376
377     return S_OK;
378 }
379
380 static HRESULT WINAPI ProgressDialog_SetAnimation(IProgressDialog *iface, HINSTANCE hInstance, UINT uiResourceId)
381 {
382     FIXME("(%p, %p, %d) - stub\n", iface, hInstance, uiResourceId);
383     return S_OK;
384 }
385
386 static BOOL WINAPI ProgressDialog_HasUserCancelled(IProgressDialog *iface)
387 {
388     ProgressDialog *This = impl_from_IProgressDialog(iface);
389     return This->isCancelled;
390 }
391
392 static HRESULT WINAPI ProgressDialog_SetProgress64(IProgressDialog *iface, ULONGLONG ullCompleted, ULONGLONG ullTotal)
393 {
394     ProgressDialog *This = impl_from_IProgressDialog(iface);
395     HWND hwnd;
396
397     TRACE("(%p, 0x%s, 0x%s)\n", This, wine_dbgstr_longlong(ullCompleted), wine_dbgstr_longlong(ullTotal));
398
399     EnterCriticalSection(&This->cs);
400     This->ullTotal = ullTotal;
401     This->ullCompleted = ullCompleted;
402     This->dwUpdate |= UPDATE_PROGRESS;
403     hwnd = This->hwnd;
404     LeaveCriticalSection(&This->cs);
405
406     if (hwnd)
407         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
408
409     return S_OK;  /* Windows sometimes returns S_FALSE */
410 }
411
412 static HRESULT WINAPI ProgressDialog_SetProgress(IProgressDialog *iface, DWORD dwCompleted, DWORD dwTotal)
413 {
414     return IProgressDialog_SetProgress64(iface, dwCompleted, dwTotal);
415 }
416
417 static HRESULT WINAPI ProgressDialog_SetLine(IProgressDialog *iface, DWORD dwLineNum, LPCWSTR pwzLine, BOOL bPath, LPCVOID reserved)
418 {
419     ProgressDialog *This = impl_from_IProgressDialog(iface);
420     HWND hwnd;
421
422     TRACE("(%p, %d, %s, %d)\n", This, dwLineNum, wine_dbgstr_w(pwzLine), bPath);
423
424     if (reserved)
425         FIXME("reserved pointer not null (%p)\n", reserved);
426
427     dwLineNum--;
428     if (dwLineNum >= 3)  /* Windows seems to do something like that */
429         dwLineNum = 0;
430
431     EnterCriticalSection(&This->cs);
432     set_buffer(&This->lines[dwLineNum], pwzLine);
433     This->dwUpdate |= UPDATE_LINE1 << dwLineNum;
434     hwnd = (This->isCancelled ? NULL : This->hwnd); /* no sense to send the message if window cancelled */
435     LeaveCriticalSection(&This->cs);
436
437     if (hwnd)
438         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
439
440     return S_OK;
441 }
442
443 static HRESULT WINAPI ProgressDialog_SetCancelMsg(IProgressDialog *iface, LPCWSTR pwzMsg, LPCVOID reserved)
444 {
445     ProgressDialog *This = impl_from_IProgressDialog(iface);
446     HWND hwnd;
447
448     TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzMsg));
449
450     if (reserved)
451         FIXME("reserved pointer not null (%p)\n", reserved);
452
453     EnterCriticalSection(&This->cs);
454     set_buffer(&This->cancelMsg, pwzMsg);
455     This->dwUpdate |= UPDATE_LINE1 << CANCEL_MSG_LINE;
456     hwnd = (This->isCancelled ? This->hwnd : NULL); /* no sense to send the message if window not cancelled */
457     LeaveCriticalSection(&This->cs);
458
459     if (hwnd)
460         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
461
462     return S_OK;
463 }
464
465 static HRESULT WINAPI ProgressDialog_Timer(IProgressDialog *iface, DWORD dwTimerAction, LPCVOID reserved)
466 {
467     ProgressDialog *This = impl_from_IProgressDialog(iface);
468
469     FIXME("(%p, %d, %p) - stub\n", This, dwTimerAction, reserved);
470
471     if (reserved)
472         FIXME("Reserved field not NULL but %p\n", reserved);
473
474     return S_OK;
475 }
476
477 static const IProgressDialogVtbl ProgressDialogVtbl =
478 {
479     ProgressDialog_QueryInterface,
480     ProgressDialog_AddRef,
481     ProgressDialog_Release,
482
483     ProgressDialog_StartProgressDialog,
484     ProgressDialog_StopProgressDialog,
485     ProgressDialog_SetTitle,
486     ProgressDialog_SetAnimation,
487     ProgressDialog_HasUserCancelled,
488     ProgressDialog_SetProgress,
489     ProgressDialog_SetProgress64,
490     ProgressDialog_SetLine,
491     ProgressDialog_SetCancelMsg,
492     ProgressDialog_Timer
493 };
494
495 HRESULT ProgressDialog_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
496 {
497     ProgressDialog *This;
498     if (pUnkOuter)
499         return CLASS_E_NOAGGREGATION;
500
501     This = heap_alloc_zero(sizeof(ProgressDialog));
502     if (This == NULL)
503         return E_OUTOFMEMORY;
504
505     This->IProgressDialog_iface.lpVtbl = &ProgressDialogVtbl;
506     This->refCount = 1;
507     InitializeCriticalSection(&This->cs);
508     This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ProgressDialog.cs");
509
510     TRACE("returning %p\n", This);
511     *ppOut = (IUnknown *)This;
512     BROWSEUI_refCount++;
513     return S_OK;
514 }