server: Store the mapped file descriptor in the memory view.
[wine.git] / dlls / shell32 / dialogs.c
blob5d7e0797f6e570cea1449c311a68f975572d6732
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(
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 HRESULT WINAPI SHOpenWithDialog(HWND parent, const OPENASINFO *info)
76 FIXME("stub\n");
77 return E_NOTIMPL;
80 /*************************************************************************
81 * RunFileDlgW [internal]
83 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
85 * SEE ALSO
86 * RunFileDlgAW
88 static void RunFileDlgW(
89 HWND hwndOwner,
90 HICON hIcon,
91 LPCWSTR lpstrDirectory,
92 LPCWSTR lpstrTitle,
93 LPCWSTR lpstrDescription,
94 UINT uFlags)
96 static const WCHAR resnameW[] = {'S','H','E','L','L','_','R','U','N','_','D','L','G',0};
97 RUNFILEDLGPARAMS rfdp;
98 HRSRC hRes;
99 LPVOID template;
100 TRACE("\n");
102 rfdp.hwndOwner = hwndOwner;
103 rfdp.hIcon = hIcon;
104 rfdp.lpstrDirectory = lpstrDirectory;
105 rfdp.lpstrTitle = lpstrTitle;
106 rfdp.lpstrDescription = lpstrDescription;
107 rfdp.uFlags = uFlags;
109 if (!(hRes = FindResourceW(shell32_hInstance, resnameW, (LPWSTR)RT_DIALOG)) ||
110 !(template = LoadResource(shell32_hInstance, hRes)))
112 ERR("Couldn't load SHELL_RUN_DLG resource\n");
113 ShellMessageBoxW(shell32_hInstance, hwndOwner, MAKEINTRESOURCEW(IDS_RUNDLG_ERROR), NULL, MB_OK | MB_ICONERROR);
114 return;
117 DialogBoxIndirectParamW(shell32_hInstance,
118 template, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
122 /* find the directory that contains the file being run */
123 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
125 const WCHAR *src;
126 WCHAR *dest, *result, *result_end=NULL;
127 static const WCHAR dotexeW[] = {'.','e','x','e',0};
129 result = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(strlenW(cmdline)+5));
131 src = cmdline;
132 dest = result;
134 if (*src == '"')
136 src++;
137 while (*src && *src != '"')
139 if (*src == '\\')
140 result_end = dest;
141 *dest++ = *src++;
144 else {
145 while (*src)
147 if (isspaceW(*src))
149 *dest = 0;
150 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
151 break;
152 strcatW(dest, dotexeW);
153 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
154 break;
156 else if (*src == '\\')
157 result_end = dest;
158 *dest++ = *src++;
162 if (result_end)
164 *result_end = 0;
165 return result;
167 else
169 HeapFree(GetProcessHeap(), 0, result);
170 return NULL;
174 /* Dialog procedure for RunFileDlg */
175 static INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
177 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
179 switch (message)
181 case WM_INITDIALOG :
182 prfdp = (RUNFILEDLGPARAMS *)lParam ;
183 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
185 if (prfdp->lpstrTitle)
186 SetWindowTextW(hwnd, prfdp->lpstrTitle);
187 if (prfdp->lpstrDescription)
188 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
189 if (prfdp->uFlags & RFF_NOBROWSE)
191 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
192 ShowWindow(browse, SW_HIDE);
193 EnableWindow(browse, FALSE);
195 if (prfdp->uFlags & RFF_NOLABEL)
196 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
197 if (prfdp->uFlags & RFF_CALCDIRECTORY)
198 FIXME("RFF_CALCDIRECTORY not supported\n");
200 if (prfdp->hIcon == NULL)
201 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
202 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
203 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
204 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
206 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0) ;
207 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH)) ;
208 return TRUE ;
210 case WM_COMMAND :
211 switch (LOWORD (wParam))
213 case IDOK :
215 int ic ;
216 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
217 if ((ic = GetWindowTextLengthW (htxt)))
219 WCHAR *psz, *parent=NULL ;
220 SHELLEXECUTEINFOW sei ;
222 ZeroMemory (&sei, sizeof(sei)) ;
223 sei.cbSize = sizeof(sei) ;
224 psz = HeapAlloc( GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR) );
225 GetWindowTextW (htxt, psz, ic + 1) ;
227 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
228 * WM_NOTIFY before execution */
230 sei.hwnd = hwnd;
231 sei.nShow = SW_SHOWNORMAL;
232 sei.lpFile = psz;
234 if (prfdp->lpstrDirectory)
235 sei.lpDirectory = prfdp->lpstrDirectory;
236 else
237 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
239 if (!ShellExecuteExW( &sei ))
241 HeapFree(GetProcessHeap(), 0, psz);
242 HeapFree(GetProcessHeap(), 0, parent);
243 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
244 return TRUE ;
247 /* FillList is still ANSI */
248 GetWindowTextA (htxt, (LPSTR)psz, ic + 1) ;
249 FillList (htxt, (LPSTR)psz, FALSE) ;
251 HeapFree(GetProcessHeap(), 0, psz);
252 HeapFree(GetProcessHeap(), 0, parent);
253 EndDialog (hwnd, 0);
256 return TRUE;
258 case IDCANCEL :
259 EndDialog (hwnd, 0) ;
260 return TRUE ;
262 case IDC_RUNDLG_BROWSE :
264 static const WCHAR filterW[] = {'%','s','%','c','*','.','e','x','e','%','c','%','s','%','c','*','.','*','%','c',0};
265 HMODULE hComdlg = NULL ;
266 LPFNOFN ofnProc = NULL ;
267 static const WCHAR comdlg32W[] = {'c','o','m','d','l','g','3','2',0};
268 WCHAR szFName[1024] = {0};
269 WCHAR filter_exe[256], filter_all[256], filter[MAX_PATH], szCaption[MAX_PATH];
270 OPENFILENAMEW ofn;
272 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER_EXE, filter_exe, 256);
273 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER_ALL, filter_all, 256);
274 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
275 snprintfW( filter, MAX_PATH, filterW, filter_exe, 0, 0, filter_all, 0, 0 );
277 ZeroMemory(&ofn, sizeof(ofn));
278 ofn.lStructSize = sizeof(OPENFILENAMEW);
279 ofn.hwndOwner = hwnd;
280 ofn.lpstrFilter = filter;
281 ofn.lpstrFile = szFName;
282 ofn.nMaxFile = 1023;
283 ofn.lpstrTitle = szCaption;
284 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
285 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
287 if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
288 NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
290 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
291 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
292 return TRUE ;
295 if (ofnProc(&ofn))
297 SetFocus (GetDlgItem (hwnd, IDOK)) ;
298 SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName) ;
299 SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
300 SetFocus (GetDlgItem (hwnd, IDOK)) ;
303 FreeLibrary (hComdlg) ;
305 return TRUE ;
308 return TRUE ;
310 return FALSE ;
313 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
314 /* fShowDefault ignored if pszLatest != NULL */
315 static void FillList (HWND hCb, char *pszLatest, BOOL fShowDefault)
317 HKEY hkey ;
318 /* char szDbgMsg[256] = "" ; */
319 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
320 DWORD icList = 0, icCmd = 0 ;
321 UINT Nix ;
323 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
325 if (ERROR_SUCCESS != RegCreateKeyExA (
326 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
327 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
328 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
330 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
332 if (icList > 0)
334 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
335 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
336 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK) ;
338 else
340 icList = 1 ;
341 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
342 pszList[0] = 0 ;
345 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
347 if (pszList[Nix] > cMax)
348 cMax = pszList[Nix] ;
350 szIndex[0] = pszList[Nix] ;
352 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
353 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
354 if( pszCmd )
355 pszCmd = HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
356 else
357 pszCmd = HeapAlloc(GetProcessHeap(), 0, icCmd) ;
358 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
359 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
361 if (NULL != pszLatest)
363 if (!lstrcmpiA(pszCmd, pszLatest))
366 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
367 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
369 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
370 SetWindowTextA (hCb, pszCmd) ;
371 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
373 cMatch = pszList[Nix] ;
374 memmove (&pszList[1], pszList, Nix) ;
375 pszList[0] = cMatch ;
376 continue ;
380 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
383 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
384 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
386 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
387 if (!Nix && fShowDefault)
389 SetWindowTextA (hCb, pszCmd) ;
390 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
394 else
397 sprintf (szDbgMsg, "Doing loop thing.\n") ;
398 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
400 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
401 SetWindowTextA (hCb, pszLatest) ;
402 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
404 cMatch = pszList[Nix] ;
405 memmove (&pszList[1], pszList, Nix) ;
406 pszList[0] = cMatch ;
407 szIndex[0] = cMatch ;
408 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
412 if (!cMatch && NULL != pszLatest)
415 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
416 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
418 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
419 SetWindowTextA (hCb, pszLatest) ;
420 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
422 cMatch = ++cMax ;
423 if( pszList )
424 pszList = HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
425 else
426 pszList = HeapAlloc(GetProcessHeap(), 0, ++icList) ;
427 memmove (&pszList[1], pszList, icList - 1) ;
428 pszList[0] = cMatch ;
429 szIndex[0] = cMatch ;
430 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
433 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
435 HeapFree( GetProcessHeap(), 0, pszCmd) ;
436 HeapFree( GetProcessHeap(), 0, pszList) ;
439 /*************************************************************************
440 * RunFileDlgA [internal]
442 * The ANSI function that is available as ordinal 61 on Windows 9x/Me
444 * SEE ALSO
445 * RunFileDlgAW
447 static void RunFileDlgA(
448 HWND hwndOwner,
449 HICON hIcon,
450 LPCSTR lpstrDirectory,
451 LPCSTR lpstrTitle,
452 LPCSTR lpstrDescription,
453 UINT uFlags)
455 WCHAR title[MAX_PATH]; /* longer string wouldn't be visible in the dialog anyway */
456 WCHAR description[MAX_PATH];
457 WCHAR directory[MAX_PATH];
459 MultiByteToWideChar(CP_ACP, 0, lpstrTitle, -1, title, MAX_PATH);
460 title[MAX_PATH - 1] = 0;
461 MultiByteToWideChar(CP_ACP, 0, lpstrDescription, -1, description, MAX_PATH);
462 description[MAX_PATH - 1] = 0;
463 if (!MultiByteToWideChar(CP_ACP, 0, lpstrDirectory, -1, directory, MAX_PATH))
464 directory[0] = 0;
466 RunFileDlgW(hwndOwner, hIcon,
467 lpstrDirectory ? directory : NULL,
468 lpstrTitle ? title : NULL,
469 lpstrDescription ? description : NULL,
470 uFlags);
473 /*************************************************************************
474 * RunFileDlgAW [SHELL32.61]
476 * An undocumented way to open the Run File dialog. A documented way is to use
477 * CLSID_Shell, IID_IShellDispatch (as of Wine 1.0, not implemented under Wine)
479 * Exported by ordinal. ANSI on Windows 9x and Unicode on Windows NT/2000/XP/etc
482 void WINAPI RunFileDlgAW(
483 HWND hwndOwner,
484 HICON hIcon,
485 LPCVOID lpstrDirectory,
486 LPCVOID lpstrTitle,
487 LPCVOID lpstrDescription,
488 UINT uFlags)
490 if (SHELL_OsIsUnicode())
491 RunFileDlgW(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
492 else
493 RunFileDlgA(hwndOwner, hIcon, lpstrDirectory, lpstrTitle, lpstrDescription, uFlags);
497 /*************************************************************************
498 * ConfirmDialog [internal]
500 * Put up a confirm box, return TRUE if the user confirmed
502 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
504 WCHAR Prompt[256];
505 WCHAR Title[256];
507 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
508 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
509 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
513 /*************************************************************************
514 * RestartDialogEx [SHELL32.730]
517 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
519 TRACE("(%p)\n", hWndOwner);
521 /* FIXME: use lpwstrReason */
522 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
524 HANDLE hToken;
525 TOKEN_PRIVILEGES npr;
527 /* enable the shutdown privilege for the current process */
528 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
530 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
531 npr.PrivilegeCount = 1;
532 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
533 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
534 CloseHandle(hToken);
536 ExitWindowsEx(EWX_REBOOT, uReason);
539 return 0;
543 /*************************************************************************
544 * RestartDialog [SHELL32.59]
547 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
549 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
553 /*************************************************************************
554 * ExitWindowsDialog [SHELL32.60]
556 * NOTES
557 * exported by ordinal
559 void WINAPI ExitWindowsDialog (HWND hWndOwner)
561 TRACE("(%p)\n", hWndOwner);
563 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
565 HANDLE hToken;
566 TOKEN_PRIVILEGES npr;
568 /* enable shutdown privilege for current process */
569 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
571 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
572 npr.PrivilegeCount = 1;
573 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
574 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
575 CloseHandle(hToken);
577 ExitWindowsEx(EWX_SHUTDOWN, 0);