winex11: add owned windows to taskbar if owner is not mapped
[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     const IProgressDialogVtbl *vtbl;
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 void set_buffer(LPWSTR *buffer, LPCWSTR string)
78 {
79     static const WCHAR empty_string[] = {0};
80     IMalloc *malloc;
81     int cb;
82
83     if (string == NULL)
84         string = empty_string;
85     CoGetMalloc(1, &malloc);
86
87     cb = (strlenW(string) + 1)*sizeof(WCHAR);
88     if (*buffer == NULL || cb > IMalloc_GetSize(malloc, *buffer))
89         *buffer = IMalloc_Realloc(malloc, *buffer, cb);
90     memcpy(*buffer, string, cb);
91 }
92
93 struct create_params
94 {
95     ProgressDialog *This;
96     HANDLE hEvent;
97     HWND hwndParent;
98 };
99
100 static LPWSTR load_string(HINSTANCE hInstance, UINT uiResourceId)
101 {
102     WCHAR string[256];
103     LPWSTR ret;
104
105     LoadStringW(hInstance, uiResourceId, string, sizeof(string)/sizeof(string[0]));
106     ret = HeapAlloc(GetProcessHeap(), 0, (strlenW(string) + 1) * sizeof(WCHAR));
107     strcpyW(ret, string);
108     return ret;
109 }
110
111 static void set_progress_marquee(ProgressDialog *This)
112 {
113     HWND hProgress = GetDlgItem(This->hwnd, IDC_PROGRESS_BAR);
114     SetWindowLongW(hProgress, GWL_STYLE,
115         GetWindowLongW(hProgress, GWL_STYLE)|PBS_MARQUEE);
116 }
117
118 void update_dialog(ProgressDialog *This, DWORD dwUpdate)
119 {
120     WCHAR empty[] = {0};
121
122     if (dwUpdate & UPDATE_TITLE)
123         SetWindowTextW(This->hwnd, This->title);
124
125     if (dwUpdate & UPDATE_LINE1)
126         SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE, (This->isCancelled ? empty : This->lines[0]));
127     if (dwUpdate & UPDATE_LINE2)
128         SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+1, (This->isCancelled ? empty : This->lines[1]));
129     if (dwUpdate & UPDATE_LINE3)
130         SetDlgItemTextW(This->hwnd, IDC_TEXT_LINE+2, (This->isCancelled ? This->cancelMsg : This->lines[2]));
131
132     if (dwUpdate & UPDATE_PROGRESS)
133     {
134         ULONGLONG ullTotal = This->ullTotal;
135         ULONGLONG ullCompleted = This->ullCompleted;
136
137         /* progress bar requires 32-bit coordinates */
138         while (ullTotal >> 32)
139         {
140             ullTotal >>= 1;
141             ullCompleted >>= 1;
142         }
143
144         SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETRANGE32, 0, (DWORD)ullTotal);
145         SendDlgItemMessageW(This->hwnd, IDC_PROGRESS_BAR, PBM_SETPOS, (DWORD)ullCompleted, 0);
146     }
147 }
148
149 static void end_dialog(ProgressDialog *This)
150 {
151     SendMessageW(This->hwnd, WM_DLG_DESTROY, 0, 0);
152     /* native doesn't reenable the window? */
153     if (This->hwndDisabledParent)
154         EnableWindow(This->hwndDisabledParent, TRUE);
155     This->hwnd = NULL;
156 }
157
158 static INT_PTR CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
159 {
160     ProgressDialog *This = (ProgressDialog *)GetWindowLongPtrW(hwnd, DWLP_USER);
161
162     switch (msg)
163     {
164         case WM_INITDIALOG:
165         {
166             struct create_params *params = (struct create_params *)lParam;
167
168             /* Note: until we set the hEvent, the object is protected by
169              * the critical section held by StartProgress */
170             SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)params->This);
171             This = params->This;
172             This->hwnd = hwnd;
173
174             if (This->dwFlags & PROGDLG_NOPROGRESSBAR)
175                 ShowWindow(GetDlgItem(hwnd, IDC_PROGRESS_BAR), SW_HIDE);
176             if (This->dwFlags & PROGDLG_NOCANCEL)
177                 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
178             if (This->dwFlags & PROGDLG_MARQUEEPROGRESS)
179                 set_progress_marquee(This);
180             if (This->dwFlags & PROGDLG_NOMINIMIZE)
181                 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & (~WS_MINIMIZEBOX));
182
183             update_dialog(This, 0xffffffff);
184             This->dwUpdate = 0;
185             This->isCancelled = FALSE;
186             SetEvent(params->hEvent);
187             return TRUE;
188         }
189
190         case WM_DLG_UPDATE:
191             EnterCriticalSection(&This->cs);
192             update_dialog(This, This->dwUpdate);
193             This->dwUpdate = 0;
194             LeaveCriticalSection(&This->cs);
195             return TRUE;
196
197         case WM_DLG_DESTROY:
198             DestroyWindow(hwnd);
199             PostThreadMessageW(GetCurrentThreadId(), WM_NULL, 0, 0); /* wake up the GetMessage */
200             return TRUE;
201
202         case WM_CLOSE:
203         case WM_COMMAND:
204             if (msg == WM_CLOSE || wParam == IDCANCEL)
205             {
206                 EnterCriticalSection(&This->cs);
207                 This->isCancelled = TRUE;
208
209                 if (!This->cancelMsg)
210                     This->cancelMsg = load_string(BROWSEUI_hinstance, IDS_CANCELLING);
211
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);
216             }
217             return TRUE;
218     }
219     return FALSE;
220 }
221
222 static DWORD WINAPI dialog_thread(LPVOID lpParameter)
223 {
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;
227     HWND hwnd;
228     MSG msg;
229
230     hwnd = CreateDialogParamW(BROWSEUI_hinstance, MAKEINTRESOURCEW(IDD_PROGRESS_DLG),
231         params->hwndParent, dialog_proc, (LPARAM)params);
232
233     while (GetMessageW(&msg, NULL, 0, 0) > 0)
234     {
235         if (!IsWindow(hwnd))
236             break;
237         if(!IsDialogMessageW(hwnd, &msg))
238         {
239             TranslateMessage(&msg);
240             DispatchMessageW(&msg);
241         }
242     }
243
244     return 0;
245 }
246
247 static void ProgressDialog_Destructor(ProgressDialog *This)
248 {
249     TRACE("destroying %p\n", This);
250     if (This->hwnd)
251         end_dialog(This);
252     heap_free(This->lines[0]);
253     heap_free(This->lines[1]);
254     heap_free(This->lines[2]);
255     heap_free(This->cancelMsg);
256     heap_free(This->title);
257     heap_free(This);
258     BROWSEUI_refCount--;
259 }
260
261 static HRESULT WINAPI ProgressDialog_QueryInterface(IProgressDialog *iface, REFIID iid, LPVOID *ppvOut)
262 {
263     ProgressDialog *This = (ProgressDialog *)iface;
264     *ppvOut = NULL;
265
266     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IProgressDialog))
267     {
268         *ppvOut = This;
269     }
270
271     if (*ppvOut)
272     {
273         IUnknown_AddRef(iface);
274         return S_OK;
275     }
276
277     WARN("unsupported interface: %s\n", debugstr_guid(iid));
278     return E_NOINTERFACE;
279 }
280
281 static ULONG WINAPI ProgressDialog_AddRef(IProgressDialog *iface)
282 {
283     ProgressDialog *This = (ProgressDialog *)iface;
284     return InterlockedIncrement(&This->refCount);
285 }
286
287 static ULONG WINAPI ProgressDialog_Release(IProgressDialog *iface)
288 {
289     ProgressDialog *This = (ProgressDialog *)iface;
290     ULONG ret;
291
292     ret = InterlockedDecrement(&This->refCount);
293     if (ret == 0)
294         ProgressDialog_Destructor(This);
295     return ret;
296 }
297
298 static HRESULT WINAPI ProgressDialog_StartProgressDialog(IProgressDialog *iface, HWND hwndParent, IUnknown *punkEnableModeless, DWORD dwFlags, LPCVOID reserved)
299 {
300     ProgressDialog *This = (ProgressDialog *)iface;
301     struct create_params params;
302     HANDLE hThread;
303
304     TRACE("(%p, %p, %x, %p)\n", iface, punkEnableModeless, dwFlags, reserved);
305     if (punkEnableModeless || reserved)
306         FIXME("Reserved parameters not null (%p, %p)\n", punkEnableModeless, reserved);
307     if (dwFlags & PROGDLG_AUTOTIME)
308         FIXME("Flags PROGDLG_AUTOTIME not supported\n");
309     if (dwFlags & PROGDLG_NOTIME)
310         FIXME("Flags PROGDLG_NOTIME not supported\n");
311
312     EnterCriticalSection(&This->cs);
313
314     if (This->hwnd)
315     {
316         LeaveCriticalSection(&This->cs);
317         return S_OK;  /* as on XP */
318     }
319     This->dwFlags = dwFlags;
320     params.This = This;
321     params.hwndParent = hwndParent;
322     params.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
323
324     hThread = CreateThread(NULL, 0, dialog_thread, &params, 0, NULL);
325     WaitForSingleObject(params.hEvent, INFINITE);
326
327     CloseHandle(hThread);
328     This->hwndDisabledParent = NULL;
329     if (hwndParent && (dwFlags & PROGDLG_MODAL))
330     {
331         HWND hwndDisable = GetAncestor(hwndParent, GA_ROOT);
332         if (EnableWindow(hwndDisable, FALSE))
333             This->hwndDisabledParent = hwndDisable;
334     }
335
336     LeaveCriticalSection(&This->cs);
337
338     return S_OK;
339 }
340
341 static HRESULT WINAPI ProgressDialog_StopProgressDialog(IProgressDialog *iface)
342 {
343     ProgressDialog *This = (ProgressDialog *)iface;
344
345     EnterCriticalSection(&This->cs);
346     if (This->hwnd)
347         end_dialog(This);
348     LeaveCriticalSection(&This->cs);
349
350     return S_OK;
351 }
352
353 static HRESULT WINAPI ProgressDialog_SetTitle(IProgressDialog *iface, LPCWSTR pwzTitle)
354 {
355     ProgressDialog *This = (ProgressDialog *)iface;
356     HWND hwnd;
357
358     TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzTitle));
359
360     EnterCriticalSection(&This->cs);
361     set_buffer(&This->title, pwzTitle);
362     This->dwUpdate |= UPDATE_TITLE;
363     hwnd = This->hwnd;
364     LeaveCriticalSection(&This->cs);
365
366     if (hwnd)
367         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
368
369     return S_OK;
370 }
371
372 static HRESULT WINAPI ProgressDialog_SetAnimation(IProgressDialog *iface, HINSTANCE hInstance, UINT uiResourceId)
373 {
374     FIXME("(%p, %p, %d) - stub\n", iface, hInstance, uiResourceId);
375     return S_OK;
376 }
377
378 static BOOL WINAPI ProgressDialog_HasUserCancelled(IProgressDialog *iface)
379 {
380     ProgressDialog *This = (ProgressDialog *)iface;
381     return This->isCancelled;
382 }
383
384 static HRESULT WINAPI ProgressDialog_SetProgress64(IProgressDialog *iface, ULONGLONG ullCompleted, ULONGLONG ullTotal)
385 {
386     ProgressDialog *This = (ProgressDialog *)iface;
387     HWND hwnd;
388
389     TRACE("(%p, 0x%s, 0x%s)\n", This, wine_dbgstr_longlong(ullCompleted), wine_dbgstr_longlong(ullTotal));
390
391     EnterCriticalSection(&This->cs);
392     This->ullTotal = ullTotal;
393     This->ullCompleted = ullCompleted;
394     This->dwUpdate |= UPDATE_PROGRESS;
395     hwnd = This->hwnd;
396     LeaveCriticalSection(&This->cs);
397
398     if (hwnd)
399         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
400
401     return S_OK;  /* Windows sometimes returns S_FALSE */
402 }
403
404 static HRESULT WINAPI ProgressDialog_SetProgress(IProgressDialog *iface, DWORD dwCompleted, DWORD dwTotal)
405 {
406     return IProgressDialog_SetProgress64(iface, dwCompleted, dwTotal);
407 }
408
409 static HRESULT WINAPI ProgressDialog_SetLine(IProgressDialog *iface, DWORD dwLineNum, LPCWSTR pwzLine, BOOL bPath, LPCVOID reserved)
410 {
411     ProgressDialog *This = (ProgressDialog *)iface;
412     HWND hwnd;
413
414     TRACE("(%p, %d, %s, %d)\n", This, dwLineNum, wine_dbgstr_w(pwzLine), bPath);
415
416     if (reserved)
417         FIXME("reserved pointer not null (%p)\n", reserved);
418
419     dwLineNum--;
420     if (dwLineNum >= 3)  /* Windows seems to do something like that */
421         dwLineNum = 0;
422
423     EnterCriticalSection(&This->cs);
424     set_buffer(&This->lines[dwLineNum], pwzLine);
425     This->dwUpdate |= UPDATE_LINE1 << dwLineNum;
426     hwnd = (This->isCancelled ? NULL : This->hwnd); /* no sense to send the message if window cancelled */
427     LeaveCriticalSection(&This->cs);
428
429     if (hwnd)
430         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
431
432     return S_OK;
433 }
434
435 static HRESULT WINAPI ProgressDialog_SetCancelMsg(IProgressDialog *iface, LPCWSTR pwzMsg, LPCVOID reserved)
436 {
437     ProgressDialog *This = (ProgressDialog *)iface;
438     HWND hwnd;
439
440     TRACE("(%p, %s)\n", This, wine_dbgstr_w(pwzMsg));
441
442     if (reserved)
443         FIXME("reserved pointer not null (%p)\n", reserved);
444
445     EnterCriticalSection(&This->cs);
446     set_buffer(&This->cancelMsg, pwzMsg);
447     This->dwUpdate |= UPDATE_LINE1 << CANCEL_MSG_LINE;
448     hwnd = (This->isCancelled ? This->hwnd : NULL); /* no sense to send the message if window not cancelled */
449     LeaveCriticalSection(&This->cs);
450
451     if (hwnd)
452         SendMessageW(hwnd, WM_DLG_UPDATE, 0, 0);
453
454     return S_OK;
455 }
456
457 static HRESULT WINAPI ProgressDialog_Timer(IProgressDialog *iface, DWORD dwTimerAction, LPCVOID reserved)
458 {
459     ProgressDialog *This = (ProgressDialog *)iface;
460
461     FIXME("(%p, %d, %p) - stub\n", This, dwTimerAction, reserved);
462
463     if (reserved)
464         FIXME("Reserved field not NULL but %p\n", reserved);
465
466     return S_OK;
467 }
468
469 static const IProgressDialogVtbl ProgressDialogVtbl =
470 {
471     ProgressDialog_QueryInterface,
472     ProgressDialog_AddRef,
473     ProgressDialog_Release,
474
475     ProgressDialog_StartProgressDialog,
476     ProgressDialog_StopProgressDialog,
477     ProgressDialog_SetTitle,
478     ProgressDialog_SetAnimation,
479     ProgressDialog_HasUserCancelled,
480     ProgressDialog_SetProgress,
481     ProgressDialog_SetProgress64,
482     ProgressDialog_SetLine,
483     ProgressDialog_SetCancelMsg,
484     ProgressDialog_Timer
485 };
486
487 HRESULT ProgressDialog_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
488 {
489     ProgressDialog *This;
490     if (pUnkOuter)
491         return CLASS_E_NOAGGREGATION;
492
493     This = heap_alloc_zero(sizeof(ProgressDialog));
494     if (This == NULL)
495         return E_OUTOFMEMORY;
496
497     This->vtbl = &ProgressDialogVtbl;
498     This->refCount = 1;
499     InitializeCriticalSection(&This->cs);
500
501     TRACE("returning %p\n", This);
502     *ppOut = (IUnknown *)This;
503     BROWSEUI_refCount++;
504     return S_OK;
505 }