msacm: Fix acmMetrics(ACM_METRIC_DRIVER_PRIORITY) return on error.
[wine/multimedia.git] / dlls / shell32 / dialogs.c
blob8357d56fdd8bd4d229060590228217f782d3baee
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 LPCSTR lpstrDirectory ;
47 LPCSTR lpstrTitle ;
48 LPCSTR lpstrDescription ;
49 UINT uFlags ;
50 } RUNFILEDLGPARAMS ;
52 typedef BOOL (*LPFNOFN) (OPENFILENAMEA *) ;
54 WINE_DEFAULT_DEBUG_CHANNEL(shell);
55 INT_PTR CALLBACK RunDlgProc (HWND, UINT, WPARAM, LPARAM) ;
56 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,%08lx,%p):stub.\n",
70 hwndOwner, lpstrFile, nMaxFile,lpdwIconIndex);
71 return 0xffffffff;
74 /*************************************************************************
75 * RunFileDlg [SHELL32.61]
77 * NOTES
78 * Original name: RunFileDlg (exported by ordinal)
80 void WINAPI RunFileDlg(
81 HWND hwndOwner,
82 HICON hIcon,
83 LPCSTR lpstrDirectory,
84 LPCSTR lpstrTitle,
85 LPCSTR lpstrDescription,
86 UINT uFlags)
89 RUNFILEDLGPARAMS rfdp;
90 HRSRC hRes;
91 LPVOID template;
92 TRACE("\n");
94 rfdp.hwndOwner = hwndOwner;
95 rfdp.hIcon = hIcon;
96 rfdp.lpstrDirectory = lpstrDirectory;
97 rfdp.lpstrTitle = lpstrTitle;
98 rfdp.lpstrDescription = lpstrDescription;
99 rfdp.uFlags = uFlags;
101 if(!(hRes = FindResourceA(shell32_hInstance, "SHELL_RUN_DLG", (LPSTR)RT_DIALOG)))
103 MessageBoxA (hwndOwner, "Couldn't find dialog.", "Nix", MB_OK) ;
104 return;
106 if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
108 MessageBoxA (hwndOwner, "Couldn't load dialog.", "Nix", MB_OK) ;
109 return;
112 DialogBoxIndirectParamA((HINSTANCE)GetWindowLongPtrW( hwndOwner,
113 GWLP_HINSTANCE ),
114 template, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
118 /* Dialog procedure for RunFileDlg */
119 INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
121 int ic ;
122 char *psz, szMsg[256] ;
123 static RUNFILEDLGPARAMS *prfdp = NULL ;
125 switch (message)
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 ;
137 case WM_COMMAND :
138 switch (LOWORD (wParam))
140 case IDOK :
142 HWND htxt = NULL ;
143 if ((ic = GetWindowTextLengthA (htxt = GetDlgItem (hwnd, 12298))))
145 psz = HeapAlloc( GetProcessHeap(), 0, (ic + 2) );
146 GetWindowTextA (htxt, psz, ic + 1) ;
148 if (ShellExecuteA(NULL, "open", psz, NULL, NULL, SW_SHOWNORMAL) < (HINSTANCE)33)
150 char *pszSysMsg = NULL ;
151 FormatMessageA (
152 FORMAT_MESSAGE_ALLOCATE_BUFFER |
153 FORMAT_MESSAGE_FROM_SYSTEM |
154 FORMAT_MESSAGE_IGNORE_INSERTS,
155 NULL, GetLastError (),
156 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
157 (LPSTR)&pszSysMsg, 0, NULL
159 sprintf (szMsg, "Error: %s", pszSysMsg) ;
160 LocalFree ((HLOCAL)pszSysMsg) ;
161 MessageBoxA (hwnd, szMsg, "Nix", MB_OK | MB_ICONEXCLAMATION) ;
163 HeapFree(GetProcessHeap(), 0, psz);
164 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
165 return TRUE ;
167 FillList (htxt, psz) ;
168 HeapFree(GetProcessHeap(), 0, psz);
169 EndDialog (hwnd, 0) ;
173 case IDCANCEL :
174 EndDialog (hwnd, 0) ;
175 return TRUE ;
177 case 12288 :
179 HMODULE hComdlg = NULL ;
180 LPFNOFN ofnProc = NULL ;
181 static char szFName[1024] = "", szFileTitle[256] = "", szInitDir[768] = "" ;
182 static OPENFILENAMEA ofn =
184 sizeof (OPENFILENAMEA),
185 NULL,
186 NULL,
187 "Executable Files\0*.exe\0All Files\0*.*\0\0\0\0",
188 NULL,
191 szFName,
192 1023,
193 szFileTitle,
194 255,
195 (LPCSTR)szInitDir,
196 "Browse",
197 OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,
200 NULL,
202 (LPOFNHOOKPROC)NULL,
203 NULL
206 ofn.hwndOwner = hwnd ;
208 if (NULL == (hComdlg = LoadLibraryExA ("comdlg32", NULL, 0)))
210 MessageBoxA (hwnd, "Unable to display dialog box (LoadLibraryEx) !", "Nix", MB_OK | MB_ICONEXCLAMATION) ;
211 return TRUE ;
214 if ((LPFNOFN)NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameA")))
216 MessageBoxA (hwnd, "Unable to display dialog box (GetProcAddress) !", "Nix", MB_OK | MB_ICONEXCLAMATION) ;
217 return TRUE ;
220 ofnProc (&ofn) ;
222 SetFocus (GetDlgItem (hwnd, IDOK)) ;
223 SetWindowTextA (GetDlgItem (hwnd, 12298), szFName) ;
224 SendMessageA (GetDlgItem (hwnd, 12298), CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
225 SetFocus (GetDlgItem (hwnd, IDOK)) ;
227 FreeLibrary (hComdlg) ;
229 return TRUE ;
232 return TRUE ;
234 return FALSE ;
237 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
238 void FillList (HWND hCb, char *pszLatest)
240 HKEY hkey ;
241 /* char szDbgMsg[256] = "" ; */
242 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
243 DWORD icList = 0, icCmd = 0 ;
244 UINT Nix ;
246 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
248 if (ERROR_SUCCESS != RegCreateKeyExA (
249 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
250 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
251 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
253 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
255 if (icList > 0)
257 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
258 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
259 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK) ;
261 else
263 icList = 1 ;
264 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
265 pszList[0] = 0 ;
268 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
270 if (pszList[Nix] > cMax)
271 cMax = pszList[Nix] ;
273 szIndex[0] = pszList[Nix] ;
275 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
276 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
277 if( pszCmd )
278 pszCmd = HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
279 else
280 pszCmd = HeapAlloc(GetProcessHeap(), 0, icCmd) ;
281 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
282 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
284 if (NULL != pszLatest)
286 if (!lstrcmpiA(pszCmd, pszLatest))
289 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
290 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
292 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
293 SetWindowTextA (hCb, pszCmd) ;
294 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
296 cMatch = pszList[Nix] ;
297 memmove (&pszList[1], pszList, Nix) ;
298 pszList[0] = cMatch ;
299 continue ;
303 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
306 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
307 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
309 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
310 if (!Nix)
312 SetWindowTextA (hCb, pszCmd) ;
313 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
317 else
320 sprintf (szDbgMsg, "Doing loop thing.\n") ;
321 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
323 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
324 SetWindowTextA (hCb, pszLatest) ;
325 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
327 cMatch = pszList[Nix] ;
328 memmove (&pszList[1], pszList, Nix) ;
329 pszList[0] = cMatch ;
330 szIndex[0] = cMatch ;
331 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
335 if (!cMatch && NULL != pszLatest)
338 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
339 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
341 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
342 SetWindowTextA (hCb, pszLatest) ;
343 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
345 cMatch = ++cMax ;
346 if( pszList )
347 pszList = HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
348 else
349 pszList = HeapAlloc(GetProcessHeap(), 0, ++icList) ;
350 memmove (&pszList[1], pszList, icList - 1) ;
351 pszList[0] = cMatch ;
352 szIndex[0] = cMatch ;
353 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
356 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
358 HeapFree( GetProcessHeap(), 0, pszCmd) ;
359 HeapFree( GetProcessHeap(), 0, pszList) ;
363 /*************************************************************************
364 * ConfirmDialog [internal]
366 * Put up a confirm box, return TRUE if the user confirmed
368 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
370 WCHAR Prompt[256];
371 WCHAR Title[256];
373 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
374 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
375 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
379 /*************************************************************************
380 * RestartDialogEx [SHELL32.730]
383 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
385 TRACE("(%p)\n", hWndOwner);
387 /*FIXME: use uReason */
389 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
391 HANDLE hToken;
392 TOKEN_PRIVILEGES npr;
394 /* enable shutdown privilege for current process */
395 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
397 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
398 npr.PrivilegeCount = 1;
399 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
400 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
401 CloseHandle(hToken);
403 ExitWindowsEx(EWX_REBOOT, 0);
406 return 0;
410 /*************************************************************************
411 * RestartDialog [SHELL32.59]
414 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
416 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
420 /*************************************************************************
421 * ExitWindowsDialog [SHELL32.60]
423 * NOTES
424 * exported by ordinal
426 void WINAPI ExitWindowsDialog (HWND hWndOwner)
428 TRACE("(%p)\n", hWndOwner);
430 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
432 HANDLE hToken;
433 TOKEN_PRIVILEGES npr;
435 /* enable shutdown privilege for current process */
436 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
438 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
439 npr.PrivilegeCount = 1;
440 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
441 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
442 CloseHandle(hToken);
444 ExitWindowsEx(EWX_SHUTDOWN, 0);