schedsvc: Fix a typo in a parameter name.
[wine.git] / dlls / shell32 / dialogs.c
blobc6d8e95e966f1fccab95b31517150c41f79c0417
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 *, BOOL) ;
59 /*************************************************************************
60 * PickIconDlg [SHELL32.62]
63 INT WINAPI PickIconDlg(HWND hwndOwner, WCHAR *path, UINT path_len, INT *index)
65 FIXME("(%p,%s,%u,%p):stub.\n", hwndOwner, debugstr_w(path), path_len, index);
66 return 0xffffffff;
69 HRESULT WINAPI SHOpenWithDialog(HWND parent, const OPENASINFO *info)
71 FIXME("stub\n");
72 return E_NOTIMPL;
75 /*************************************************************************
76 * RunFileDlgW [internal]
78 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
80 * SEE ALSO
81 * RunFileDlgAW
83 static void RunFileDlgW(
84 HWND hwndOwner,
85 HICON hIcon,
86 LPCWSTR lpstrDirectory,
87 LPCWSTR lpstrTitle,
88 LPCWSTR lpstrDescription,
89 UINT uFlags)
91 static const WCHAR resnameW[] = {'S','H','E','L','L','_','R','U','N','_','D','L','G',0};
92 RUNFILEDLGPARAMS rfdp;
93 HRSRC hRes;
94 LPVOID template;
95 TRACE("\n");
97 rfdp.hwndOwner = hwndOwner;
98 rfdp.hIcon = hIcon;
99 rfdp.lpstrDirectory = lpstrDirectory;
100 rfdp.lpstrTitle = lpstrTitle;
101 rfdp.lpstrDescription = lpstrDescription;
102 rfdp.uFlags = uFlags;
104 if (!(hRes = FindResourceW(shell32_hInstance, resnameW, (LPWSTR)RT_DIALOG)) ||
105 !(template = LoadResource(shell32_hInstance, hRes)))
107 ERR("Couldn't load SHELL_RUN_DLG resource\n");
108 ShellMessageBoxW(shell32_hInstance, hwndOwner, MAKEINTRESOURCEW(IDS_RUNDLG_ERROR), NULL, MB_OK | MB_ICONERROR);
109 return;
112 DialogBoxIndirectParamW(shell32_hInstance,
113 template, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
117 /* find the directory that contains the file being run */
118 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
120 const WCHAR *src;
121 WCHAR *dest, *result, *result_end=NULL;
122 static const WCHAR dotexeW[] = {'.','e','x','e',0};
124 result = heap_alloc(sizeof(WCHAR)*(strlenW(cmdline)+5));
126 src = cmdline;
127 dest = result;
129 if (*src == '"')
131 src++;
132 while (*src && *src != '"')
134 if (*src == '\\')
135 result_end = dest;
136 *dest++ = *src++;
139 else {
140 while (*src)
142 if (isspaceW(*src))
144 *dest = 0;
145 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
146 break;
147 strcatW(dest, dotexeW);
148 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
149 break;
151 else if (*src == '\\')
152 result_end = dest;
153 *dest++ = *src++;
157 if (result_end)
159 *result_end = 0;
160 return result;
162 else
164 heap_free(result);
165 return NULL;
169 /* Dialog procedure for RunFileDlg */
170 static INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
172 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
174 switch (message)
176 case WM_INITDIALOG :
177 prfdp = (RUNFILEDLGPARAMS *)lParam ;
178 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
180 if (prfdp->lpstrTitle)
181 SetWindowTextW(hwnd, prfdp->lpstrTitle);
182 if (prfdp->lpstrDescription)
183 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
184 if (prfdp->uFlags & RFF_NOBROWSE)
186 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
187 ShowWindow(browse, SW_HIDE);
188 EnableWindow(browse, FALSE);
190 if (prfdp->uFlags & RFF_NOLABEL)
191 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
192 if (prfdp->uFlags & RFF_CALCDIRECTORY)
193 FIXME("RFF_CALCDIRECTORY not supported\n");
195 if (prfdp->hIcon == NULL)
196 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
197 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
198 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
199 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
201 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0) ;
202 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH)) ;
203 return TRUE ;
205 case WM_COMMAND :
206 switch (LOWORD (wParam))
208 case IDOK :
210 int ic ;
211 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
212 if ((ic = GetWindowTextLengthW (htxt)))
214 WCHAR *psz, *parent=NULL ;
215 SHELLEXECUTEINFOW sei ;
217 ZeroMemory (&sei, sizeof(sei)) ;
218 sei.cbSize = sizeof(sei) ;
219 psz = heap_alloc( (ic + 1)*sizeof(WCHAR) );
220 GetWindowTextW (htxt, psz, ic + 1) ;
222 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
223 * WM_NOTIFY before execution */
225 sei.hwnd = hwnd;
226 sei.nShow = SW_SHOWNORMAL;
227 sei.lpFile = psz;
229 if (prfdp->lpstrDirectory)
230 sei.lpDirectory = prfdp->lpstrDirectory;
231 else
232 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
234 if (!ShellExecuteExW( &sei ))
236 heap_free(psz);
237 heap_free(parent);
238 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
239 return TRUE ;
242 /* FillList is still ANSI */
243 GetWindowTextA (htxt, (LPSTR)psz, ic + 1) ;
244 FillList (htxt, (LPSTR)psz, FALSE) ;
246 heap_free(psz);
247 heap_free(parent);
248 EndDialog (hwnd, 0);
251 return TRUE;
253 case IDCANCEL :
254 EndDialog (hwnd, 0) ;
255 return TRUE ;
257 case IDC_RUNDLG_BROWSE :
259 static const WCHAR filterW[] = {'%','s','%','c','*','.','e','x','e','%','c','%','s','%','c','*','.','*','%','c',0};
260 HMODULE hComdlg = NULL ;
261 LPFNOFN ofnProc = NULL ;
262 static const WCHAR comdlg32W[] = {'c','o','m','d','l','g','3','2',0};
263 WCHAR szFName[1024] = {0};
264 WCHAR filter_exe[256], filter_all[256], filter[MAX_PATH], szCaption[MAX_PATH];
265 OPENFILENAMEW ofn;
267 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER_EXE, filter_exe, 256);
268 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER_ALL, filter_all, 256);
269 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
270 snprintfW( filter, MAX_PATH, filterW, filter_exe, 0, 0, filter_all, 0, 0 );
272 ZeroMemory(&ofn, sizeof(ofn));
273 ofn.lStructSize = sizeof(OPENFILENAMEW);
274 ofn.hwndOwner = hwnd;
275 ofn.lpstrFilter = filter;
276 ofn.lpstrFile = szFName;
277 ofn.nMaxFile = 1023;
278 ofn.lpstrTitle = szCaption;
279 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
280 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
282 if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
283 NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
285 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
286 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
287 return TRUE ;
290 if (ofnProc(&ofn))
292 SetFocus (GetDlgItem (hwnd, IDOK)) ;
293 SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName) ;
294 SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
295 SetFocus (GetDlgItem (hwnd, IDOK)) ;
298 FreeLibrary (hComdlg) ;
300 return TRUE ;
303 return TRUE ;
305 return FALSE ;
308 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
309 /* fShowDefault ignored if pszLatest != NULL */
310 static void FillList (HWND hCb, char *pszLatest, BOOL fShowDefault)
312 HKEY hkey ;
313 /* char szDbgMsg[256] = "" ; */
314 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
315 DWORD icList = 0, icCmd = 0 ;
316 UINT Nix ;
318 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
320 if (ERROR_SUCCESS != RegCreateKeyExA (
321 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
322 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
323 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
325 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
327 if (icList > 0)
329 pszList = heap_alloc(icList) ;
330 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
331 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK) ;
333 else
335 icList = 1 ;
336 pszList = heap_alloc(icList) ;
337 pszList[0] = 0 ;
340 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
342 if (pszList[Nix] > cMax)
343 cMax = pszList[Nix] ;
345 szIndex[0] = pszList[Nix] ;
347 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
348 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
349 if( pszCmd )
350 pszCmd = heap_realloc(pszCmd, icCmd) ;
351 else
352 pszCmd = heap_alloc(icCmd) ;
353 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
354 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
356 if (NULL != pszLatest)
358 if (!lstrcmpiA(pszCmd, pszLatest))
361 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
362 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
364 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
365 SetWindowTextA (hCb, pszCmd) ;
366 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
368 cMatch = pszList[Nix] ;
369 memmove (&pszList[1], pszList, Nix) ;
370 pszList[0] = cMatch ;
371 continue ;
375 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
378 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
379 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
381 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
382 if (!Nix && fShowDefault)
384 SetWindowTextA (hCb, pszCmd) ;
385 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
389 else
392 sprintf (szDbgMsg, "Doing loop thing.\n") ;
393 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
395 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
396 SetWindowTextA (hCb, pszLatest) ;
397 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
399 cMatch = pszList[Nix] ;
400 memmove (&pszList[1], pszList, Nix) ;
401 pszList[0] = cMatch ;
402 szIndex[0] = cMatch ;
403 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
407 if (!cMatch && NULL != pszLatest)
410 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
411 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
413 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
414 SetWindowTextA (hCb, pszLatest) ;
415 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
417 cMatch = ++cMax ;
418 if( pszList )
419 pszList = heap_realloc(pszList, ++icList) ;
420 else
421 pszList = heap_alloc(++icList) ;
422 memmove (&pszList[1], pszList, icList - 1) ;
423 pszList[0] = cMatch ;
424 szIndex[0] = cMatch ;
425 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
428 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
430 heap_free(pszCmd) ;
431 heap_free(pszList) ;
434 /*************************************************************************
435 * RunFileDlgA [internal]
437 * The ANSI function that is available as ordinal 61 on Windows 9x/Me
439 * SEE ALSO
440 * RunFileDlgAW
442 static void RunFileDlgA(
443 HWND hwndOwner,
444 HICON hIcon,
445 LPCSTR lpstrDirectory,
446 LPCSTR lpstrTitle,
447 LPCSTR lpstrDescription,
448 UINT uFlags)
450 WCHAR title[MAX_PATH]; /* longer string wouldn't be visible in the dialog anyway */
451 WCHAR description[MAX_PATH];
452 WCHAR directory[MAX_PATH];
454 MultiByteToWideChar(CP_ACP, 0, lpstrTitle, -1, title, MAX_PATH);
455 title[MAX_PATH - 1] = 0;
456 MultiByteToWideChar(CP_ACP, 0, lpstrDescription, -1, description, MAX_PATH);
457 description[MAX_PATH - 1] = 0;
458 if (!MultiByteToWideChar(CP_ACP, 0, lpstrDirectory, -1, directory, MAX_PATH))
459 directory[0] = 0;
461 RunFileDlgW(hwndOwner, hIcon,
462 lpstrDirectory ? directory : NULL,
463 lpstrTitle ? title : NULL,
464 lpstrDescription ? description : NULL,
465 uFlags);
468 /*************************************************************************
469 * RunFileDlgAW [SHELL32.61]
471 * An undocumented way to open the Run File dialog. A documented way is to use
472 * CLSID_Shell, IID_IShellDispatch (as of Wine 1.0, not implemented under Wine)
474 * Exported by ordinal. ANSI on Windows 9x and Unicode on Windows NT/2000/XP/etc
477 void WINAPI RunFileDlgAW(
478 HWND hwndOwner,
479 HICON hIcon,
480 LPCVOID lpstrDirectory,
481 LPCVOID lpstrTitle,
482 LPCVOID lpstrDescription,
483 UINT uFlags)
485 if (SHELL_OsIsUnicode())
486 RunFileDlgW(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
487 else
488 RunFileDlgA(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
492 /*************************************************************************
493 * ConfirmDialog [internal]
495 * Put up a confirm box, return TRUE if the user confirmed
497 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
499 WCHAR Prompt[256];
500 WCHAR Title[256];
502 LoadStringW(shell32_hInstance, PromptId, Prompt, ARRAY_SIZE(Prompt));
503 LoadStringW(shell32_hInstance, TitleId, Title, ARRAY_SIZE(Title));
504 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
508 /*************************************************************************
509 * RestartDialogEx [SHELL32.730]
512 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
514 TRACE("(%p)\n", hWndOwner);
516 /* FIXME: use lpwstrReason */
517 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
519 HANDLE hToken;
520 TOKEN_PRIVILEGES npr;
522 /* enable the shutdown privilege for the current process */
523 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
525 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
526 npr.PrivilegeCount = 1;
527 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
528 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
529 CloseHandle(hToken);
531 ExitWindowsEx(EWX_REBOOT, uReason);
534 return 0;
538 /*************************************************************************
539 * RestartDialog [SHELL32.59]
542 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
544 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
548 /*************************************************************************
549 * ExitWindowsDialog [SHELL32.60]
551 * NOTES
552 * exported by ordinal
554 void WINAPI ExitWindowsDialog (HWND hWndOwner)
556 TRACE("(%p)\n", hWndOwner);
558 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
560 HANDLE hToken;
561 TOKEN_PRIVILEGES npr;
563 /* enable shutdown privilege for current process */
564 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
566 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
567 npr.PrivilegeCount = 1;
568 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
569 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
570 CloseHandle(hToken);
572 ExitWindowsEx(EWX_SHUTDOWN, 0);