wow32: Use spec file imports.
[wine.git] / dlls / shell32 / dialogs.c
bloba2ab99499b5f55c5c6c3c3c3c032825467d4d3f3
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 <string.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "winerror.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "commdlg.h"
31 #include "wine/debug.h"
33 #include "shellapi.h"
34 #include "shlobj.h"
35 #include "shell32_main.h"
36 #include "shresdef.h"
38 /* RunFileDlg flags */
39 #define RFF_NOBROWSE 0x01
40 #define RFF_NODEFAULT 0x02
41 #define RFF_CALCDIRECTORY 0x04
42 #define RFF_NOLABEL 0x08
43 #define RFF_NOSEPARATEMEM 0x20 /* NT only */
45 /* RunFileFlg notification structure */
46 typedef struct
48 NMHDR hdr;
49 const char *lpFile;
50 const char *lpDirectory;
51 int nShow;
52 } NM_RUNFILEDLG;
54 /* RunFileDlg notification return values */
55 #define RF_OK 0x00
56 #define RF_CANCEL 0x01
57 #define RF_RETRY 0x02
59 typedef struct
61 HWND hwndOwner ;
62 HICON hIcon ;
63 LPCWSTR lpstrDirectory ;
64 LPCWSTR lpstrTitle ;
65 LPCWSTR lpstrDescription ;
66 UINT uFlags ;
67 } RUNFILEDLGPARAMS ;
69 typedef BOOL (WINAPI * LPFNOFN) (OPENFILENAMEW *) ;
71 WINE_DEFAULT_DEBUG_CHANNEL(shell);
72 static INT_PTR CALLBACK RunDlgProc (HWND, UINT, WPARAM, LPARAM) ;
73 static void FillList (HWND, char *, BOOL) ;
76 /*************************************************************************
77 * PickIconDlg [SHELL32.62]
80 INT WINAPI PickIconDlg(HWND hwndOwner, WCHAR *path, UINT path_len, INT *index)
82 FIXME("(%p,%s,%u,%p):stub.\n", hwndOwner, debugstr_w(path), path_len, index);
83 return 0xffffffff;
86 HRESULT WINAPI SHOpenWithDialog(HWND parent, const OPENASINFO *info)
88 FIXME("stub\n");
89 return E_NOTIMPL;
92 /*************************************************************************
93 * RunFileDlgW [internal]
95 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
97 * SEE ALSO
98 * RunFileDlgAW
100 static void RunFileDlgW(
101 HWND hwndOwner,
102 HICON hIcon,
103 LPCWSTR lpstrDirectory,
104 LPCWSTR lpstrTitle,
105 LPCWSTR lpstrDescription,
106 UINT uFlags)
108 RUNFILEDLGPARAMS rfdp;
109 HRSRC hRes;
110 LPVOID template;
111 TRACE("\n");
113 rfdp.hwndOwner = hwndOwner;
114 rfdp.hIcon = hIcon;
115 rfdp.lpstrDirectory = lpstrDirectory;
116 rfdp.lpstrTitle = lpstrTitle;
117 rfdp.lpstrDescription = lpstrDescription;
118 rfdp.uFlags = uFlags;
120 if (!(hRes = FindResourceW(shell32_hInstance, L"SHELL_RUN_DLG", (LPWSTR)RT_DIALOG)) ||
121 !(template = LoadResource(shell32_hInstance, hRes)))
123 ERR("Couldn't load SHELL_RUN_DLG resource\n");
124 ShellMessageBoxW(shell32_hInstance, hwndOwner, MAKEINTRESOURCEW(IDS_RUNDLG_ERROR), NULL, MB_OK | MB_ICONERROR);
125 return;
128 DialogBoxIndirectParamW(shell32_hInstance,
129 template, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
133 /* find the directory that contains the file being run */
134 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
136 const WCHAR *src;
137 WCHAR *dest, *result, *result_end=NULL;
139 result = heap_alloc(sizeof(WCHAR)*(lstrlenW(cmdline)+5));
141 src = cmdline;
142 dest = result;
144 if (*src == '"')
146 src++;
147 while (*src && *src != '"')
149 if (*src == '\\')
150 result_end = dest;
151 *dest++ = *src++;
154 else {
155 while (*src)
157 if (iswspace(*src))
159 *dest = 0;
160 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
161 break;
162 lstrcatW(dest, L".exe");
163 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
164 break;
166 else if (*src == '\\')
167 result_end = dest;
168 *dest++ = *src++;
172 if (result_end)
174 *result_end = 0;
175 return result;
177 else
179 heap_free(result);
180 return NULL;
184 /* Dialog procedure for RunFileDlg */
185 static INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
187 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
189 switch (message)
191 case WM_INITDIALOG :
192 prfdp = (RUNFILEDLGPARAMS *)lParam ;
193 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
195 if (prfdp->lpstrTitle)
196 SetWindowTextW(hwnd, prfdp->lpstrTitle);
197 if (prfdp->lpstrDescription)
198 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
199 if (prfdp->uFlags & RFF_NOBROWSE)
201 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
202 ShowWindow(browse, SW_HIDE);
203 EnableWindow(browse, FALSE);
205 if (prfdp->uFlags & RFF_NOLABEL)
206 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
207 if (prfdp->uFlags & RFF_CALCDIRECTORY)
208 FIXME("RFF_CALCDIRECTORY not supported\n");
210 if (prfdp->hIcon == NULL)
211 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
212 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
213 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
214 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
216 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0) ;
217 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH)) ;
218 return TRUE ;
220 case WM_COMMAND :
221 switch (LOWORD (wParam))
223 case IDOK :
225 int ic ;
226 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
227 if ((ic = GetWindowTextLengthW (htxt)))
229 WCHAR *psz, *parent=NULL ;
230 SHELLEXECUTEINFOW sei ;
232 ZeroMemory (&sei, sizeof(sei)) ;
233 sei.cbSize = sizeof(sei) ;
234 psz = heap_alloc( (ic + 1)*sizeof(WCHAR) );
235 GetWindowTextW (htxt, psz, ic + 1) ;
237 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
238 * WM_NOTIFY before execution */
240 sei.hwnd = hwnd;
241 sei.nShow = SW_SHOWNORMAL;
242 sei.lpFile = psz;
244 if (prfdp->lpstrDirectory)
245 sei.lpDirectory = prfdp->lpstrDirectory;
246 else
247 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
249 if (!ShellExecuteExW( &sei ))
251 heap_free(psz);
252 heap_free(parent);
253 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
254 return TRUE ;
257 /* FillList is still ANSI */
258 GetWindowTextA (htxt, (LPSTR)psz, ic + 1) ;
259 FillList (htxt, (LPSTR)psz, FALSE) ;
261 heap_free(psz);
262 heap_free(parent);
263 EndDialog (hwnd, 0);
266 return TRUE;
268 case IDCANCEL :
269 EndDialog (hwnd, 0) ;
270 return TRUE ;
272 case IDC_RUNDLG_BROWSE :
274 WCHAR szFName[1024] = {0};
275 WCHAR filter_exe[256], filter_all[256], filter[MAX_PATH], szCaption[MAX_PATH];
276 OPENFILENAMEW ofn;
278 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER_EXE, filter_exe, 256);
279 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER_ALL, filter_all, 256);
280 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
281 swprintf( filter, MAX_PATH, L"%s%c*.exe%c%s%c*.*%c", filter_exe, 0, 0, filter_all, 0, 0 );
283 ZeroMemory(&ofn, sizeof(ofn));
284 ofn.lStructSize = sizeof(OPENFILENAMEW);
285 ofn.hwndOwner = hwnd;
286 ofn.lpstrFilter = filter;
287 ofn.lpstrFile = szFName;
288 ofn.nMaxFile = 1023;
289 ofn.lpstrTitle = szCaption;
290 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
291 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
293 if (GetOpenFileNameW(&ofn))
295 SetFocus (GetDlgItem (hwnd, IDOK)) ;
296 SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName) ;
297 SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
298 SetFocus (GetDlgItem (hwnd, IDOK)) ;
301 return TRUE ;
304 return TRUE ;
306 return FALSE ;
309 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
310 /* fShowDefault ignored if pszLatest != NULL */
311 static void FillList (HWND hCb, char *pszLatest, BOOL fShowDefault)
313 HKEY hkey ;
314 /* char szDbgMsg[256] = "" ; */
315 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
316 DWORD icList = 0, icCmd = 0 ;
317 UINT Nix ;
319 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
321 if (ERROR_SUCCESS != RegCreateKeyExA (
322 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
323 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
324 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
326 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
328 if (icList > 0)
330 pszList = heap_alloc(icList) ;
331 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
332 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK) ;
334 else
336 icList = 1 ;
337 pszList = heap_alloc(icList) ;
338 pszList[0] = 0 ;
341 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
343 if (pszList[Nix] > cMax)
344 cMax = pszList[Nix] ;
346 szIndex[0] = pszList[Nix] ;
348 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
349 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
350 if( pszCmd )
351 pszCmd = heap_realloc(pszCmd, icCmd) ;
352 else
353 pszCmd = heap_alloc(icCmd) ;
354 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
355 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
357 if (NULL != pszLatest)
359 if (!lstrcmpiA(pszCmd, pszLatest))
362 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
363 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
365 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
366 SetWindowTextA (hCb, pszCmd) ;
367 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
369 cMatch = pszList[Nix] ;
370 memmove (&pszList[1], pszList, Nix) ;
371 pszList[0] = cMatch ;
372 continue ;
376 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
379 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
380 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
382 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
383 if (!Nix && fShowDefault)
385 SetWindowTextA (hCb, pszCmd) ;
386 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
390 else
393 sprintf (szDbgMsg, "Doing loop thing.\n") ;
394 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
396 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
397 SetWindowTextA (hCb, pszLatest) ;
398 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
400 cMatch = pszList[Nix] ;
401 memmove (&pszList[1], pszList, Nix) ;
402 pszList[0] = cMatch ;
403 szIndex[0] = cMatch ;
404 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
408 if (!cMatch && NULL != pszLatest)
411 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
412 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
414 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
415 SetWindowTextA (hCb, pszLatest) ;
416 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
418 cMatch = ++cMax ;
419 if( pszList )
420 pszList = heap_realloc(pszList, ++icList) ;
421 else
422 pszList = heap_alloc(++icList) ;
423 memmove (&pszList[1], pszList, icList - 1) ;
424 pszList[0] = cMatch ;
425 szIndex[0] = cMatch ;
426 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
429 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
431 heap_free(pszCmd) ;
432 heap_free(pszList) ;
435 /*************************************************************************
436 * RunFileDlgA [internal]
438 * The ANSI function that is available as ordinal 61 on Windows 9x/Me
440 * SEE ALSO
441 * RunFileDlgAW
443 static void RunFileDlgA(
444 HWND hwndOwner,
445 HICON hIcon,
446 LPCSTR lpstrDirectory,
447 LPCSTR lpstrTitle,
448 LPCSTR lpstrDescription,
449 UINT uFlags)
451 WCHAR title[MAX_PATH]; /* longer string wouldn't be visible in the dialog anyway */
452 WCHAR description[MAX_PATH];
453 WCHAR directory[MAX_PATH];
455 MultiByteToWideChar(CP_ACP, 0, lpstrTitle, -1, title, MAX_PATH);
456 title[MAX_PATH - 1] = 0;
457 MultiByteToWideChar(CP_ACP, 0, lpstrDescription, -1, description, MAX_PATH);
458 description[MAX_PATH - 1] = 0;
459 if (!MultiByteToWideChar(CP_ACP, 0, lpstrDirectory, -1, directory, MAX_PATH))
460 directory[0] = 0;
462 RunFileDlgW(hwndOwner, hIcon,
463 lpstrDirectory ? directory : NULL,
464 lpstrTitle ? title : NULL,
465 lpstrDescription ? description : NULL,
466 uFlags);
469 /*************************************************************************
470 * RunFileDlgAW [SHELL32.61]
472 * An undocumented way to open the Run File dialog. A documented way is to use
473 * CLSID_Shell, IID_IShellDispatch (as of Wine 1.0, not implemented under Wine)
475 * Exported by ordinal. ANSI on Windows 9x and Unicode on Windows NT/2000/XP/etc
478 void WINAPI RunFileDlgAW(
479 HWND hwndOwner,
480 HICON hIcon,
481 LPCVOID lpstrDirectory,
482 LPCVOID lpstrTitle,
483 LPCVOID lpstrDescription,
484 UINT uFlags)
486 if (SHELL_OsIsUnicode())
487 RunFileDlgW(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
488 else
489 RunFileDlgA(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
493 /*************************************************************************
494 * ConfirmDialog [internal]
496 * Put up a confirm box, return TRUE if the user confirmed
498 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
500 WCHAR Prompt[256];
501 WCHAR Title[256];
503 LoadStringW(shell32_hInstance, PromptId, Prompt, ARRAY_SIZE(Prompt));
504 LoadStringW(shell32_hInstance, TitleId, Title, ARRAY_SIZE(Title));
505 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
509 /*************************************************************************
510 * RestartDialogEx [SHELL32.730]
513 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
515 TRACE("(%p)\n", hWndOwner);
517 /* FIXME: use lpwstrReason */
518 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
520 HANDLE hToken;
521 TOKEN_PRIVILEGES npr;
523 /* enable the shutdown privilege for the current process */
524 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
526 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
527 npr.PrivilegeCount = 1;
528 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
529 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
530 CloseHandle(hToken);
532 ExitWindowsEx(EWX_REBOOT, uReason);
535 return 0;
539 /*************************************************************************
540 * RestartDialog [SHELL32.59]
543 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
545 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
549 /*************************************************************************
550 * ExitWindowsDialog [SHELL32.60]
552 * NOTES
553 * exported by ordinal
555 void WINAPI ExitWindowsDialog (HWND hWndOwner)
557 TRACE("(%p)\n", hWndOwner);
559 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
561 HANDLE hToken;
562 TOKEN_PRIVILEGES npr;
564 /* enable shutdown privilege for current process */
565 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
567 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
568 npr.PrivilegeCount = 1;
569 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
570 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
571 CloseHandle(hToken);
573 ExitWindowsEx(EWX_SHUTDOWN, 0);