- misc cleanups ported from Wine project by Christoph Brill (egore@irc)
[reactos.git] / reactos / dll / win32 / shell32 / dialogs.c
1 /*
2 * common shell dialogs
3 *
4 * Copyright 2000 Juergen Schmied
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 #include "wine/port.h"
23
24 #include <string.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "commdlg.h"
34 #include "wine/debug.h"
35
36 #include "shellapi.h"
37 #include "shlobj.h"
38 #include "shell32_main.h"
39 #include "shresdef.h"
40 #include "undocshell.h"
41
42 typedef struct
43 {
44 HWND hwndOwner ;
45 HICON hIcon ;
46 LPCSTR lpstrDirectory ;
47 LPCSTR lpstrTitle ;
48 LPCSTR lpstrDescription ;
49 UINT uFlags ;
50 } RUNFILEDLGPARAMS ;
51
52 typedef BOOL (*LPFNOFN) (OPENFILENAMEA *) ;
53
54 WINE_DEFAULT_DEBUG_CHANNEL(shell);
55 static INT_PTR CALLBACK RunDlgProc (HWND, UINT, WPARAM, LPARAM) ;
56 static void FillList (HWND, char *) ;
57
58
59 /*************************************************************************
60 * PickIconDlg [SHELL32.62]
61 *
62 */
63 BOOL WINAPI PickIconDlg(
64 HWND hwndOwner,
65 LPWSTR lpstrFile,
66 UINT nMaxFile,
67 INT* lpdwIconIndex)
68 {
69 FIXME("(%p,%s,%08x,%p):stub.\n",
70 hwndOwner, lpstrFile, nMaxFile,lpdwIconIndex);
71 return 0xffffffff;
72 }
73
74 /*************************************************************************
75 * RunFileDlg [SHELL32.61]
76 *
77 * NOTES
78 * Original name: RunFileDlg (exported by ordinal)
79 */
80 void WINAPI RunFileDlg(
81 HWND hwndOwner,
82 HICON hIcon,
83 LPCSTR lpstrDirectory,
84 LPCSTR lpstrTitle,
85 LPCSTR lpstrDescription,
86 UINT uFlags)
87 {
88
89 RUNFILEDLGPARAMS rfdp;
90 HRSRC hRes;
91 LPVOID template;
92 TRACE("\n");
93
94 rfdp.hwndOwner = hwndOwner;
95 rfdp.hIcon = hIcon;
96 rfdp.lpstrDirectory = lpstrDirectory;
97 rfdp.lpstrTitle = lpstrTitle;
98 rfdp.lpstrDescription = lpstrDescription;
99 rfdp.uFlags = uFlags;
100
101 if(!(hRes = FindResourceA(shell32_hInstance, "SHELL_RUN_DLG", (LPSTR)RT_DIALOG)))
102 {
103 MessageBoxA (hwndOwner, "Couldn't find dialog.", "Nix", MB_OK) ;
104 return;
105 }
106 if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
107 {
108 MessageBoxA (hwndOwner, "Couldn't load dialog.", "Nix", MB_OK) ;
109 return;
110 }
111
112 DialogBoxIndirectParamA((HINSTANCE)GetWindowLongPtrW( hwndOwner,
113 GWLP_HINSTANCE ),
114 template, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
115
116 }
117
118 /* Dialog procedure for RunFileDlg */
119 static INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
120 {
121 int ic ;
122 char *psz, *pdir, szMsg[256];
123 static RUNFILEDLGPARAMS *prfdp = NULL ;
124
125 switch (message)
126 {
127 case WM_INITDIALOG :
128 prfdp = (RUNFILEDLGPARAMS *)lParam ;
129 SetWindowTextA (hwnd, prfdp->lpstrTitle) ;
130 SetClassLongPtrW (hwnd, GCLP_HICON, (LPARAM)prfdp->hIcon) ;
131 SendMessageW (GetDlgItem (hwnd, 12297), STM_SETICON,
132 (WPARAM)LoadIconW (NULL, (LPCWSTR)IDI_WINLOGO), 0);
133 FillList (GetDlgItem (hwnd, 12298), NULL) ;
134 SetFocus (GetDlgItem (hwnd, 12298)) ;
135 return TRUE ;
136
137 case WM_COMMAND :
138 switch (LOWORD (wParam))
139 {
140 case IDOK :
141 {
142 HWND htxt = NULL ;
143 if ((ic = GetWindowTextLengthA (htxt = GetDlgItem (hwnd, 12298))))
144 {
145 psz = HeapAlloc( GetProcessHeap(), 0, (ic + 2) );
146 GetWindowTextA (htxt, psz, ic + 1) ;
147 pdir = HeapAlloc( GetProcessHeap(), 0, (ic + 2) );
148 if (pdir)
149 {
150 char * ptr;
151 strcpy(pdir, psz);
152 ptr = strrchr(pdir, '\\');
153 if(ptr)
154 ptr[0] = '\0';
155 }
156 if (ShellExecuteA(NULL, "open", psz, NULL, pdir, SW_SHOWNORMAL) < (HINSTANCE)33)
157 {
158 char *pszSysMsg = NULL ;
159 FormatMessageA (
160 FORMAT_MESSAGE_ALLOCATE_BUFFER |
161 FORMAT_MESSAGE_FROM_SYSTEM |
162 FORMAT_MESSAGE_IGNORE_INSERTS,
163 NULL, GetLastError (),
164 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
165 (LPSTR)&pszSysMsg, 0, NULL
166 ) ;
167 sprintf (szMsg, "Error: %s", pszSysMsg) ;
168 LocalFree ((HLOCAL)pszSysMsg) ;
169 MessageBoxA (hwnd, szMsg, "Nix", MB_OK | MB_ICONEXCLAMATION) ;
170
171 HeapFree(GetProcessHeap(), 0, psz);
172 HeapFree(GetProcessHeap(), 0, pdir);
173 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
174 return TRUE ;
175 }
176 FillList (htxt, psz) ;
177 HeapFree(GetProcessHeap(), 0, psz);
178 HeapFree(GetProcessHeap(), 0, pdir);
179 EndDialog (hwnd, 0) ;
180 }
181 }
182
183 case IDCANCEL :
184 EndDialog (hwnd, 0) ;
185 return TRUE ;
186
187 case 12288 :
188 {
189 HMODULE hComdlg = NULL ;
190 LPFNOFN ofnProc = NULL ;
191 static char szFName[1024] = "", szFileTitle[256] = "", szInitDir[768] = "" ;
192 static OPENFILENAMEA ofn =
193 {
194 sizeof (OPENFILENAMEA),
195 NULL,
196 NULL,
197 "Executable Files\0*.exe\0All Files\0*.*\0\0\0\0",
198 NULL,
199 0,
200 0,
201 szFName,
202 1023,
203 szFileTitle,
204 255,
205 (LPCSTR)szInitDir,
206 "Browse",
207 OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,
208 0,
209 0,
210 NULL,
211 0,
212 (LPOFNHOOKPROC)NULL,
213 NULL
214 } ;
215
216 ofn.hwndOwner = hwnd ;
217
218 if (NULL == (hComdlg = LoadLibraryExA ("comdlg32", NULL, 0)))
219 {
220 MessageBoxA (hwnd, "Unable to display dialog box (LoadLibraryEx) !", "Nix", MB_OK | MB_ICONEXCLAMATION) ;
221 return TRUE ;
222 }
223
224 if ((LPFNOFN)NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameA")))
225 {
226 MessageBoxA (hwnd, "Unable to display dialog box (GetProcAddress) !", "Nix", MB_OK | MB_ICONEXCLAMATION) ;
227 return TRUE ;
228 }
229
230 ofnProc (&ofn) ;
231
232 SetFocus (GetDlgItem (hwnd, IDOK)) ;
233 SetWindowTextA (GetDlgItem (hwnd, 12298), szFName) ;
234 SendMessageA (GetDlgItem (hwnd, 12298), CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
235 SetFocus (GetDlgItem (hwnd, IDOK)) ;
236
237 FreeLibrary (hComdlg) ;
238
239 return TRUE ;
240 }
241 }
242 return TRUE ;
243 }
244 return FALSE ;
245 }
246
247 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
248 static void FillList (HWND hCb, char *pszLatest)
249 {
250 HKEY hkey ;
251 /* char szDbgMsg[256] = "" ; */
252 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
253 DWORD icList = 0, icCmd = 0 ;
254 UINT Nix ;
255
256 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
257
258 if (ERROR_SUCCESS != RegCreateKeyExA (
259 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
260 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
261 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
262
263 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
264
265 if (icList > 0)
266 {
267 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
268 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
269 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK) ;
270 }
271 else
272 {
273 icList = 1 ;
274 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
275 pszList[0] = 0 ;
276 }
277
278 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
279 {
280 if (pszList[Nix] > cMax)
281 cMax = pszList[Nix] ;
282
283 szIndex[0] = pszList[Nix] ;
284
285 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
286 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
287 if( pszCmd )
288 pszCmd = HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
289 else
290 pszCmd = HeapAlloc(GetProcessHeap(), 0, icCmd) ;
291 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
292 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
293
294 if (NULL != pszLatest)
295 {
296 if (!lstrcmpiA(pszCmd, pszLatest))
297 {
298 /*
299 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
300 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
301 */
302 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
303 SetWindowTextA (hCb, pszCmd) ;
304 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
305
306 cMatch = pszList[Nix] ;
307 memmove (&pszList[1], pszList, Nix) ;
308 pszList[0] = cMatch ;
309 continue ;
310 }
311 }
312
313 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
314 {
315 /*
316 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
317 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
318 */
319 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
320 if (!Nix)
321 {
322 SetWindowTextA (hCb, pszCmd) ;
323 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
324 }
325
326 }
327 else
328 {
329 /*
330 sprintf (szDbgMsg, "Doing loop thing.\n") ;
331 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
332 */
333 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
334 SetWindowTextA (hCb, pszLatest) ;
335 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
336
337 cMatch = pszList[Nix] ;
338 memmove (&pszList[1], pszList, Nix) ;
339 pszList[0] = cMatch ;
340 szIndex[0] = cMatch ;
341 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
342 }
343 }
344
345 if (!cMatch && NULL != pszLatest)
346 {
347 /*
348 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
349 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
350 */
351 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
352 SetWindowTextA (hCb, pszLatest) ;
353 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
354
355 cMatch = ++cMax ;
356 if( pszList )
357 pszList = HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
358 else
359 pszList = HeapAlloc(GetProcessHeap(), 0, ++icList) ;
360 memmove (&pszList[1], pszList, icList - 1) ;
361 pszList[0] = cMatch ;
362 szIndex[0] = cMatch ;
363 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
364 }
365
366 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
367
368 HeapFree( GetProcessHeap(), 0, pszCmd) ;
369 HeapFree( GetProcessHeap(), 0, pszList) ;
370 }
371
372
373 /*************************************************************************
374 * ConfirmDialog [internal]
375 *
376 * Put up a confirm box, return TRUE if the user confirmed
377 */
378 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
379 {
380 WCHAR Prompt[256];
381 WCHAR Title[256];
382
383 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
384 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
385 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
386 }
387
388
389 /*************************************************************************
390 * RestartDialogEx [SHELL32.730]
391 */
392
393 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
394 {
395 TRACE("(%p)\n", hWndOwner);
396
397 /* FIXME: use lpwstrReason */
398 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
399 {
400 HANDLE hToken;
401 TOKEN_PRIVILEGES npr;
402
403 /* enable the shutdown privilege for the current process */
404 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
405 {
406 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
407 npr.PrivilegeCount = 1;
408 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
409 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
410 CloseHandle(hToken);
411 }
412 ExitWindowsEx(EWX_REBOOT, uReason);
413 }
414
415 return 0;
416 }
417
418 /*************************************************************************
419 * LogoffWindowsDialog [SHELL32.54]
420 */
421
422 int WINAPI LogoffWindowsDialog(DWORD uFlags)
423 {
424 UNIMPLEMENTED;
425 ExitWindowsEx(EWX_LOGOFF, 0);
426 return 0;
427 }
428
429 /*************************************************************************
430 * RestartDialog [SHELL32.59]
431 */
432
433 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
434 {
435 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
436 }
437
438
439 /*************************************************************************
440 * ExitWindowsDialog [SHELL32.60]
441 *
442 * NOTES
443 * exported by ordinal
444 */
445 void WINAPI ExitWindowsDialog (HWND hWndOwner)
446 {
447 TRACE("(%p)\n", hWndOwner);
448
449 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
450 {
451 HANDLE hToken;
452 TOKEN_PRIVILEGES npr;
453
454 /* enable shutdown privilege for current process */
455 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
456 {
457 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
458 npr.PrivilegeCount = 1;
459 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
460 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
461 CloseHandle(hToken);
462 }
463 ExitWindowsEx(EWX_SHUTDOWN, 0);
464 }
465 }