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
;
238 ProgressDialog
*This
= params
->This
;
242 hwnd
= CreateDialogParamW(BROWSEUI_hinstance
, MAKEINTRESOURCEW(IDD_PROGRESS_DLG
),
243 params
->hwndParent
, dialog_proc
, (LPARAM
)params
);
245 while (GetMessageW(&msg
, NULL
, 0, 0) > 0)
249 if(!IsDialogMessageW(hwnd
, &msg
))
251 TranslateMessage(&msg
);
252 DispatchMessageW(&msg
);
256 IProgressDialog_Release(&This
->IProgressDialog_iface
);
260 static void ProgressDialog_Destructor(ProgressDialog
*This
)
262 TRACE("destroying %p\n", 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
);
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
);
285 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_IProgressDialog
))
289 else if (IsEqualIID(iid
, &IID_IOleWindow
))
291 *ppvOut
= &This
->IOleWindow_iface
;
296 IProgressDialog_AddRef(iface
);
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
);
315 ret
= InterlockedDecrement(&This
->refCount
);
317 ProgressDialog_Destructor(This
);
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
;
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
);
342 LeaveCriticalSection(&This
->cs
);
343 return S_OK
; /* as on XP */
345 This
->dwFlags
= dwFlags
;
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
, ¶ms
, 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
);
372 static HRESULT WINAPI
ProgressDialog_StopProgressDialog(IProgressDialog
*iface
)
374 ProgressDialog
*This
= impl_from_IProgressDialog(iface
);
376 EnterCriticalSection(&This
->cs
);
379 LeaveCriticalSection(&This
->cs
);
384 static HRESULT WINAPI
ProgressDialog_SetTitle(IProgressDialog
*iface
, LPCWSTR pwzTitle
)
386 ProgressDialog
*This
= impl_from_IProgressDialog(iface
);
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
;
395 LeaveCriticalSection(&This
->cs
);
398 SendMessageW(hwnd
, WM_DLG_UPDATE
, 0, 0);
403 static HRESULT WINAPI
ProgressDialog_SetAnimation(IProgressDialog
*iface
, HINSTANCE hInstance
, UINT uiResourceId
)
405 FIXME("(%p, %p, %d) - stub\n", iface
, hInstance
, uiResourceId
);
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
);
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
;
427 LeaveCriticalSection(&This
->cs
);
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
);
445 TRACE("(%p, %d, %s, %d)\n", This
, dwLineNum
, wine_dbgstr_w(pwzLine
), bPath
);
448 FIXME("reserved pointer not null (%p)\n", reserved
);
451 if (dwLineNum
>= 3) /* Windows seems to do something like that */
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
);
461 SendMessageW(hwnd
, WM_DLG_UPDATE
, 0, 0);
466 static HRESULT WINAPI
ProgressDialog_SetCancelMsg(IProgressDialog
*iface
, LPCWSTR pwzMsg
, LPCVOID reserved
)
468 ProgressDialog
*This
= impl_from_IProgressDialog(iface
);
471 TRACE("(%p, %s)\n", This
, wine_dbgstr_w(pwzMsg
));
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
);
483 SendMessageW(hwnd
, WM_DLG_UPDATE
, 0, 0);
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
);
495 FIXME("Reserved field not NULL but %p\n", reserved
);
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
,
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
);
543 LeaveCriticalSection(&This
->cs
);
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
);
555 static const IOleWindowVtbl OleWindowVtbl
=
557 OleWindow_QueryInterface
,
561 OleWindow_ContextSensitiveHelp
565 HRESULT
ProgressDialog_Constructor(IUnknown
*pUnkOuter
, IUnknown
**ppOut
)
567 ProgressDialog
*This
;
569 return CLASS_E_NOAGGREGATION
;
571 This
= heap_alloc_zero(sizeof(ProgressDialog
));
573 return E_OUTOFMEMORY
;
575 This
->IProgressDialog_iface
.lpVtbl
= &ProgressDialogVtbl
;
576 This
->IOleWindow_iface
.lpVtbl
= &OleWindowVtbl
;
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
);