shell32: RunFileDlg: make the error messages easier to understand for the user.
[wine/hacks.git] / dlls / shell32 / dialogs.c
blob367a40a7e988d43fdd1969ed8735bc183b633c75
1 /*
2 * common shell dialogs
4 * Copyright 2000 Juergen Schmied
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"
22 #include "wine/port.h"
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"
36 #include "shellapi.h"
37 #include "shlobj.h"
38 #include "shell32_main.h"
39 #include "shresdef.h"
40 #include "undocshell.h"
42 typedef struct
44 HWND hwndOwner ;
45 HICON hIcon ;
46 LPCWSTR lpstrDirectory ;
47 LPCWSTR lpstrTitle ;
48 LPCWSTR lpstrDescription ;
49 UINT uFlags ;
50 } RUNFILEDLGPARAMS ;
52 typedef BOOL (*WINAPI LPFNOFN) (OPENFILENAMEW *) ;
54 WINE_DEFAULT_DEBUG_CHANNEL(shell);
55 static INT_PTR CALLBACK RunDlgProc (HWND, UINT, WPARAM, LPARAM) ;
56 static void FillList (HWND, char *) ;
59 /*************************************************************************
60 * PickIconDlg [SHELL32.62]
63 BOOL WINAPI PickIconDlg(
64 HWND hwndOwner,
65 LPSTR lpstrFile,
66 DWORD nMaxFile,
67 LPDWORD lpdwIconIndex)
69 FIXME("(%p,%s,%08x,%p):stub.\n",
70 hwndOwner, lpstrFile, nMaxFile,lpdwIconIndex);
71 return 0xffffffff;
74 /*************************************************************************
75 * RunFileDlgW [internal]
77 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
79 * SEE ALSO
80 * RunFileDlgAW
82 void WINAPI RunFileDlgW(
83 HWND hwndOwner,
84 HICON hIcon,
85 LPCWSTR lpstrDirectory,
86 LPCWSTR lpstrTitle,
87 LPCWSTR lpstrDescription,
88 UINT uFlags)
90 static const WCHAR resnameW[] = {'S','H','E','L','L','_','R','U','N','_','D','L','G',0};
91 RUNFILEDLGPARAMS rfdp;
92 HRSRC hRes;
93 LPVOID template;
94 TRACE("\n");
96 rfdp.hwndOwner = hwndOwner;
97 rfdp.hIcon = hIcon;
98 rfdp.lpstrDirectory = lpstrDirectory;
99 rfdp.lpstrTitle = lpstrTitle;
100 rfdp.lpstrDescription = lpstrDescription;
101 rfdp.uFlags = uFlags;
103 if (!(hRes = FindResourceW(shell32_hInstance, resnameW, (LPWSTR)RT_DIALOG)) ||
104 !(template = LoadResource(shell32_hInstance, hRes)))
106 ERR("Couldn't load SHELL_RUN_DLG resource\n");
107 MessageBoxA(hwndOwner, "Unable to display Run File dialog box (internal error)", NULL, MB_OK | MB_ICONERROR);
108 return;
111 DialogBoxIndirectParamW(shell32_hInstance,
112 template, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
116 /* Dialog procedure for RunFileDlg */
117 static INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
119 static RUNFILEDLGPARAMS *prfdp = NULL ;
121 switch (message)
123 case WM_INITDIALOG :
124 prfdp = (RUNFILEDLGPARAMS *)lParam ;
125 SetWindowTextW (hwnd, prfdp->lpstrTitle) ;
126 SetClassLongPtrW (hwnd, GCLP_HICON, (LPARAM)prfdp->hIcon) ;
127 SendMessageW (GetDlgItem (hwnd, 12297), STM_SETICON,
128 (WPARAM)LoadIconW (NULL, (LPCWSTR)IDI_WINLOGO), 0);
129 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL) ;
130 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH)) ;
131 return TRUE ;
133 case WM_COMMAND :
134 switch (LOWORD (wParam))
136 case IDOK :
138 int ic ;
139 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
140 if ((ic = GetWindowTextLengthW (htxt)))
142 WCHAR *psz ;
143 psz = HeapAlloc( GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR) );
144 GetWindowTextW (htxt, psz, ic + 1) ;
146 if (ShellExecuteW(NULL, NULL, psz, NULL, NULL, SW_SHOWNORMAL) < (HINSTANCE)33)
148 char *pszSysMsg = NULL ;
149 char szMsg[256];
150 FormatMessageA (
151 FORMAT_MESSAGE_ALLOCATE_BUFFER |
152 FORMAT_MESSAGE_FROM_SYSTEM |
153 FORMAT_MESSAGE_IGNORE_INSERTS,
154 NULL, GetLastError (),
155 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
156 (LPSTR)&pszSysMsg, 0, NULL
158 sprintf (szMsg, "Error: %s", pszSysMsg) ;
159 LocalFree ((HLOCAL)pszSysMsg) ;
160 MessageBoxA (hwnd, szMsg, "Nix", MB_OK | MB_ICONEXCLAMATION) ;
162 HeapFree(GetProcessHeap(), 0, psz);
163 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
164 return TRUE ;
167 /* FillList is still ANSI */
168 GetWindowTextA (htxt, (LPSTR)psz, ic + 1) ;
169 FillList (htxt, (LPSTR)psz) ;
171 HeapFree(GetProcessHeap(), 0, psz);
172 EndDialog (hwnd, 0) ;
176 case IDCANCEL :
177 EndDialog (hwnd, 0) ;
178 return TRUE ;
180 case IDC_RUNDLG_BROWSE :
182 HMODULE hComdlg = NULL ;
183 LPFNOFN ofnProc = NULL ;
184 static const WCHAR comdlg32W[] = {'c','o','m','d','l','g','3','2',0};
185 WCHAR szFName[1024] = {0};
186 WCHAR szFilter[MAX_PATH], szCaption[MAX_PATH];
187 static const char ansiFilter[] = "Executable Files\0*.exe\0All Files\0*.*\0\0\0\0";
188 OPENFILENAMEW ofn;
190 MultiByteToWideChar(CP_UTF8, 0, ansiFilter, sizeof(ansiFilter), szFilter, MAX_PATH);
191 MultiByteToWideChar(CP_UTF8, 0, "Browse", -1, szCaption, MAX_PATH);
192 ZeroMemory(&ofn, sizeof(ofn));
193 ofn.lStructSize = sizeof(OPENFILENAMEW);
194 ofn.hwndOwner = hwnd;
195 ofn.lpstrFilter = szFilter;
196 ofn.lpstrFile = szFName;
197 ofn.nMaxFile = 1023;
198 ofn.lpstrTitle = szCaption;
199 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
201 if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
202 NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
204 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
205 MessageBoxA(hwnd, "Unable to display Browse dialog box (internal error)", NULL, MB_OK | MB_ICONERROR);
206 return TRUE ;
209 if (ofnProc(&ofn))
212 SetFocus (GetDlgItem (hwnd, IDOK)) ;
213 SetWindowTextW (GetDlgItem (hwnd, 12298), szFName) ;
214 SendMessageW (GetDlgItem (hwnd, 12298), CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
215 SetFocus (GetDlgItem (hwnd, IDOK)) ;
218 FreeLibrary (hComdlg) ;
220 return TRUE ;
223 return TRUE ;
225 return FALSE ;
228 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
229 static void FillList (HWND hCb, char *pszLatest)
231 HKEY hkey ;
232 /* char szDbgMsg[256] = "" ; */
233 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
234 DWORD icList = 0, icCmd = 0 ;
235 UINT Nix ;
237 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
239 if (ERROR_SUCCESS != RegCreateKeyExA (
240 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
241 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
242 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
244 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
246 if (icList > 0)
248 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
249 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
250 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK) ;
252 else
254 icList = 1 ;
255 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
256 pszList[0] = 0 ;
259 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
261 if (pszList[Nix] > cMax)
262 cMax = pszList[Nix] ;
264 szIndex[0] = pszList[Nix] ;
266 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
267 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
268 if( pszCmd )
269 pszCmd = HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
270 else
271 pszCmd = HeapAlloc(GetProcessHeap(), 0, icCmd) ;
272 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
273 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
275 if (NULL != pszLatest)
277 if (!lstrcmpiA(pszCmd, pszLatest))
280 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
281 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
283 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
284 SetWindowTextA (hCb, pszCmd) ;
285 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
287 cMatch = pszList[Nix] ;
288 memmove (&pszList[1], pszList, Nix) ;
289 pszList[0] = cMatch ;
290 continue ;
294 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
297 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
298 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
300 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
301 if (!Nix)
303 SetWindowTextA (hCb, pszCmd) ;
304 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
308 else
311 sprintf (szDbgMsg, "Doing loop thing.\n") ;
312 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
314 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
315 SetWindowTextA (hCb, pszLatest) ;
316 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
318 cMatch = pszList[Nix] ;
319 memmove (&pszList[1], pszList, Nix) ;
320 pszList[0] = cMatch ;
321 szIndex[0] = cMatch ;
322 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
326 if (!cMatch && NULL != pszLatest)
329 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
330 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
332 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
333 SetWindowTextA (hCb, pszLatest) ;
334 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
336 cMatch = ++cMax ;
337 if( pszList )
338 pszList = HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
339 else
340 pszList = HeapAlloc(GetProcessHeap(), 0, ++icList) ;
341 memmove (&pszList[1], pszList, icList - 1) ;
342 pszList[0] = cMatch ;
343 szIndex[0] = cMatch ;
344 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
347 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
349 HeapFree( GetProcessHeap(), 0, pszCmd) ;
350 HeapFree( GetProcessHeap(), 0, pszList) ;
353 /*************************************************************************
354 * RunFileDlgA [internal]
356 * The ANSI function that is available as ordinal 61 on Windows 9x/Me
358 * SEE ALSO
359 * RunFileDlgAW
361 void WINAPI RunFileDlgA(
362 HWND hwndOwner,
363 HICON hIcon,
364 LPCSTR lpstrDirectory,
365 LPCSTR lpstrTitle,
366 LPCSTR lpstrDescription,
367 UINT uFlags)
369 WCHAR title[MAX_PATH]; /* longer string wouldn't be visible in the dialog anyway */
370 WCHAR description[MAX_PATH];
371 WCHAR directory[MAX_PATH];
373 MultiByteToWideChar(CP_ACP, 0, lpstrTitle, -1, title, MAX_PATH);
374 title[MAX_PATH - 1] = 0;
375 MultiByteToWideChar(CP_ACP, 0, lpstrDescription, -1, description, MAX_PATH);
376 description[MAX_PATH - 1] = 0;
377 if (!MultiByteToWideChar(CP_ACP, 0, lpstrDirectory, -1, directory, MAX_PATH))
378 directory[0] = 0;
380 RunFileDlgW(hwndOwner, hIcon, directory, title, description, uFlags);
383 /*************************************************************************
384 * RunFileDlgAW [SHELL32.61]
386 * An undocumented way to open the Run File dialog. A documented way is to use
387 * CLSID_Shell, IID_IShellDispatch (as of Wine 1.0, not implemented under Wine)
389 * Exported by ordinal. ANSI on Windows 9x and Unicode on Windows NT/2000/XP/etc
392 void WINAPI RunFileDlgAW(
393 HWND hwndOwner,
394 HICON hIcon,
395 LPCVOID lpstrDirectory,
396 LPCVOID lpstrTitle,
397 LPCVOID lpstrDescription,
398 UINT uFlags)
400 if (SHELL_OsIsUnicode())
401 RunFileDlgW(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
402 else
403 RunFileDlgA(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
407 /*************************************************************************
408 * ConfirmDialog [internal]
410 * Put up a confirm box, return TRUE if the user confirmed
412 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
414 WCHAR Prompt[256];
415 WCHAR Title[256];
417 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
418 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
419 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
423 /*************************************************************************
424 * RestartDialogEx [SHELL32.730]
427 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
429 TRACE("(%p)\n", hWndOwner);
431 /* FIXME: use lpwstrReason */
432 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
434 HANDLE hToken;
435 TOKEN_PRIVILEGES npr;
437 /* enable the shutdown privilege for the current process */
438 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
440 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
441 npr.PrivilegeCount = 1;
442 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
443 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
444 CloseHandle(hToken);
446 ExitWindowsEx(EWX_REBOOT, uReason);
449 return 0;
453 /*************************************************************************
454 * RestartDialog [SHELL32.59]
457 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
459 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
463 /*************************************************************************
464 * ExitWindowsDialog [SHELL32.60]
466 * NOTES
467 * exported by ordinal
469 void WINAPI ExitWindowsDialog (HWND hWndOwner)
471 TRACE("(%p)\n", hWndOwner);
473 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
475 HANDLE hToken;
476 TOKEN_PRIVILEGES npr;
478 /* enable shutdown privilege for current process */
479 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
481 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
482 npr.PrivilegeCount = 1;
483 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
484 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
485 CloseHandle(hToken);
487 ExitWindowsEx(EWX_SHUTDOWN, 0);