[SHELL32][UNDOCSHELL]
[reactos.git] / reactos / dll / win32 / shell32 / dialogs / dialogs.cpp
1 /*
2 * common shell dialogs
3 *
4 * Copyright 2000 Juergen Schmied
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "precomp.h"
22
23 typedef struct
24 {
25 HWND hwndOwner;
26 HICON hIcon;
27 LPCWSTR lpstrDirectory;
28 LPCWSTR lpstrTitle;
29 LPCWSTR lpstrDescription;
30 UINT uFlags;
31 } RUNFILEDLGPARAMS;
32
33 typedef BOOL (WINAPI * LPFNOFN) (OPENFILENAMEW *);
34
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 static INT_PTR CALLBACK RunDlgProc(HWND, UINT, WPARAM, LPARAM);
37 static void FillList(HWND, LPWSTR, BOOL);
38
39
40 /*************************************************************************
41 * PickIconDlg [SHELL32.62]
42 *
43 */
44
45 typedef struct
46 {
47 HMODULE hLibrary;
48 HWND hDlgCtrl;
49 WCHAR szName[MAX_PATH];
50 INT Index;
51 } PICK_ICON_CONTEXT, *PPICK_ICON_CONTEXT;
52
53 BOOL CALLBACK EnumPickIconResourceProc(HMODULE hModule,
54 LPCWSTR lpszType,
55 LPWSTR lpszName,
56 LONG_PTR lParam
57 )
58 {
59 WCHAR szName[100];
60 int index;
61 HICON hIcon;
62 HWND hDlgCtrl = (HWND)lParam;
63
64 if (IS_INTRESOURCE(lpszName))
65 swprintf(szName, L"%u", (DWORD)lpszName);
66 else
67 wcscpy(szName, (WCHAR*)lpszName);
68
69 hIcon = LoadIconW(hModule, lpszName);
70 if (hIcon == NULL)
71 return TRUE;
72
73 index = SendMessageW(hDlgCtrl, LB_ADDSTRING, 0, (LPARAM)szName);
74 if (index != LB_ERR)
75 SendMessageW(hDlgCtrl, LB_SETITEMDATA, index, (LPARAM)hIcon);
76
77 return TRUE;
78 }
79
80 static void
81 DestroyIconList(HWND hDlgCtrl)
82 {
83 int count;
84 int index;
85
86 count = SendMessageW(hDlgCtrl, LB_GETCOUNT, 0, 0);
87 if (count == LB_ERR)
88 return;
89
90 for(index = 0; index < count; index++)
91 {
92 HICON hIcon = (HICON)SendMessageW(hDlgCtrl, LB_GETITEMDATA, index, 0);
93 DestroyIcon(hIcon);
94 }
95 }
96
97 INT_PTR CALLBACK PickIconProc(HWND hwndDlg,
98 UINT uMsg,
99 WPARAM wParam,
100 LPARAM lParam
101 )
102 {
103 LPMEASUREITEMSTRUCT lpmis;
104 LPDRAWITEMSTRUCT lpdis;
105 HICON hIcon;
106 INT index, count;
107 WCHAR szText[MAX_PATH], szTitle[100], szFilter[100];
108 OPENFILENAMEW ofn = {0};
109
110 PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER);
111
112 switch(uMsg)
113 {
114 case WM_INITDIALOG:
115 pIconContext = (PPICK_ICON_CONTEXT)lParam;
116 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG)pIconContext);
117 pIconContext->hDlgCtrl = GetDlgItem(hwndDlg, IDC_PICKICON_LIST);
118 SendMessageW(pIconContext->hDlgCtrl, LB_SETCOLUMNWIDTH, 32, 0);
119 EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext->hDlgCtrl);
120 if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
121 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
122 else
123 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
124
125 count = SendMessageW(pIconContext->hDlgCtrl, LB_GETCOUNT, 0, 0);
126 if (count != LB_ERR)
127 {
128 if (count > pIconContext->Index)
129 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, pIconContext->Index, 0);
130 else
131 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
132 }
133 return TRUE;
134 case WM_COMMAND:
135 switch(LOWORD(wParam))
136 {
137 case IDOK:
138 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
139 pIconContext->Index = index;
140 GetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName, MAX_PATH);
141 DestroyIconList(pIconContext->hDlgCtrl);
142 EndDialog(hwndDlg, 1);
143 break;
144 case IDCANCEL:
145 DestroyIconList(pIconContext->hDlgCtrl);
146 EndDialog(hwndDlg, 0);
147 break;
148 case IDC_PICKICON_LIST:
149 if (HIWORD(wParam) == LBN_SELCHANGE)
150 InvalidateRect((HWND)lParam, NULL, TRUE); // FIXME USE UPDATE RECT
151 break;
152 case IDC_BUTTON_PATH:
153 szText[0] = 0;
154 szTitle[0] = 0;
155 szFilter[0] = 0;
156 ofn.lStructSize = sizeof(ofn);
157 ofn.hwndOwner = hwndDlg;
158 ofn.lpstrFile = szText;
159 ofn.nMaxFile = MAX_PATH;
160 LoadStringW(shell32_hInstance, IDS_PICK_ICON_TITLE, szTitle, sizeof(szTitle) / sizeof(WCHAR));
161 ofn.lpstrTitle = szTitle;
162 LoadStringW(shell32_hInstance, IDS_PICK_ICON_FILTER, szFilter, sizeof(szFilter) / sizeof(WCHAR));
163 ofn.lpstrFilter = szFilter;
164 if (GetOpenFileNameW(&ofn))
165 {
166 HMODULE hLibrary;
167
168 if (!wcsicmp(pIconContext->szName, szText))
169 break;
170
171 DestroyIconList(pIconContext->hDlgCtrl);
172
173 hLibrary = LoadLibraryExW(szText, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
174 if (hLibrary == NULL)
175 break;
176 FreeLibrary(pIconContext->hLibrary);
177 pIconContext->hLibrary = hLibrary;
178 wcscpy(pIconContext->szName, szText);
179 EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext->hDlgCtrl);
180 if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
181 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
182 else
183 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
184
185 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
186 }
187 break;
188 }
189 break;
190 case WM_MEASUREITEM:
191 lpmis = (LPMEASUREITEMSTRUCT) lParam;
192 lpmis->itemHeight = 32;
193 lpmis->itemWidth = 64;
194 return TRUE;
195 case WM_DRAWITEM:
196 lpdis = (LPDRAWITEMSTRUCT) lParam;
197 if (lpdis->itemID == (UINT)-1)
198 {
199 break;
200 }
201 switch (lpdis->itemAction)
202 {
203 case ODA_SELECT:
204 case ODA_DRAWENTIRE:
205 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
206 hIcon = (HICON)SendMessageW(lpdis->hwndItem, LB_GETITEMDATA, lpdis->itemID, 0);
207
208 if (lpdis->itemID == (UINT)index)
209 FillRect(lpdis->hDC, &lpdis->rcItem, (HBRUSH)(COLOR_HIGHLIGHT + 1));
210 else
211 FillRect(lpdis->hDC, &lpdis->rcItem, (HBRUSH)(COLOR_WINDOW + 1));
212
213 DrawIconEx(lpdis->hDC, lpdis->rcItem.left,lpdis->rcItem.top, hIcon,
214 0, 0, 0, NULL, DI_NORMAL);
215 break;
216 }
217 break;
218 }
219
220 return FALSE;
221 }
222
223 BOOL WINAPI PickIconDlg(
224 HWND hWndOwner,
225 LPWSTR lpstrFile,
226 UINT nMaxFile,
227 INT* lpdwIconIndex)
228 {
229 HMODULE hLibrary;
230 int res;
231 PICK_ICON_CONTEXT IconContext;
232
233 hLibrary = LoadLibraryExW(lpstrFile, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
234 IconContext.hLibrary = hLibrary;
235 IconContext.Index = *lpdwIconIndex;
236 StringCchCopyNW(IconContext.szName, _countof(IconContext.szName), lpstrFile, nMaxFile);
237
238 res = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_PICK_ICON), hWndOwner, PickIconProc, (LPARAM)&IconContext);
239 if (res)
240 {
241 StringCchCopyNW(lpstrFile, nMaxFile, IconContext.szName, _countof(IconContext.szName));
242 *lpdwIconIndex = IconContext.Index;
243 }
244
245 FreeLibrary(hLibrary);
246 return res;
247 }
248
249 /*************************************************************************
250 * RunFileDlg [internal]
251 *
252 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
253 */
254 void WINAPI RunFileDlg(
255 HWND hWndOwner,
256 HICON hIcon,
257 LPCWSTR lpstrDirectory,
258 LPCWSTR lpstrTitle,
259 LPCWSTR lpstrDescription,
260 UINT uFlags)
261 {
262 TRACE("\n");
263
264 RUNFILEDLGPARAMS rfdp;
265 rfdp.hwndOwner = hWndOwner;
266 rfdp.hIcon = hIcon;
267 rfdp.lpstrDirectory = lpstrDirectory;
268 rfdp.lpstrTitle = lpstrTitle;
269 rfdp.lpstrDescription = lpstrDescription;
270 rfdp.uFlags = uFlags;
271
272 DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_RUN), hWndOwner, RunDlgProc, (LPARAM)&rfdp);
273 }
274
275
276 /* find the directory that contains the file being run */
277 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
278 {
279 const WCHAR *src;
280 WCHAR *dest, *result, *result_end=NULL;
281 static const WCHAR dotexeW[] = L".exe";
282
283 result = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(strlenW(cmdline)+5));
284
285 if (NULL == result)
286 {
287 TRACE("HeapAlloc couldn't allocate %d bytes\n", sizeof(WCHAR)*(strlenW(cmdline)+5));
288 return NULL;
289 }
290
291 src = cmdline;
292 dest = result;
293
294 if (*src == '"')
295 {
296 src++;
297 while (*src && *src != '"')
298 {
299 if (*src == '\\')
300 result_end = dest;
301 *dest++ = *src++;
302 }
303 }
304 else {
305 while (*src)
306 {
307 if (isspaceW(*src))
308 {
309 *dest = 0;
310 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
311 break;
312 strcatW(dest, dotexeW);
313 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
314 break;
315 }
316 else if (*src == '\\')
317 result_end = dest;
318 *dest++ = *src++;
319 }
320 }
321
322 if (result_end)
323 {
324 *result_end = 0;
325 return result;
326 }
327 else
328 {
329 HeapFree(GetProcessHeap(), 0, result);
330 return NULL;
331 }
332 }
333
334 static void EnableOkButtonFromEditContents(HWND hwnd)
335 {
336 BOOL Enable = FALSE;
337 INT Length, n;
338 HWND Edit = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
339 Length = GetWindowTextLengthW(Edit);
340 if (Length > 0)
341 {
342 PWCHAR psz = (PWCHAR)HeapAlloc(GetProcessHeap(), 0, (Length + 1) * sizeof(WCHAR));
343 if (psz)
344 {
345 GetWindowTextW(Edit, psz, Length + 1);
346 for (n = 0; n < Length && !Enable; ++n)
347 Enable = psz[n] != ' ';
348 HeapFree(GetProcessHeap(), 0, psz);
349 }
350 else
351 Enable = TRUE;
352 }
353 EnableWindow(GetDlgItem(hwnd, IDOK), Enable);
354 }
355
356 /* Dialog procedure for RunFileDlg */
357 static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
358 {
359 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
360
361 switch (message)
362 {
363 case WM_INITDIALOG:
364 prfdp = (RUNFILEDLGPARAMS *)lParam;
365 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
366
367 if (prfdp->lpstrTitle)
368 SetWindowTextW(hwnd, prfdp->lpstrTitle);
369 if (prfdp->lpstrDescription)
370 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
371 if (prfdp->uFlags & RFF_NOBROWSE)
372 {
373 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
374 ShowWindow(browse, SW_HIDE);
375 EnableWindow(browse, FALSE);
376 }
377 if (prfdp->uFlags & RFF_NOLABEL)
378 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
379 if (prfdp->uFlags & RFF_CALCDIRECTORY)
380 FIXME("RFF_CALCDIRECTORY not supported\n");
381
382 /* Use the default Shell Run icon if no one is specified */
383 if (prfdp->hIcon == NULL)
384 prfdp->hIcon = LoadIconW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_RUN));
385 /*
386 * NOTE: Starting Windows Vista, the "Run File" dialog gets a
387 * title icon that remains the same as the default one, even if
388 * the user specifies a custom icon.
389 * Since we currently imitate Windows 2003, therefore do not show
390 * any title icon.
391 */
392 // SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
393 // SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
394 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
395
396 FillList(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0);
397 EnableOkButtonFromEditContents(hwnd);
398 SetFocus(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH));
399 return TRUE;
400
401 case WM_COMMAND:
402 switch (LOWORD(wParam))
403 {
404 case IDOK:
405 {
406 int ic;
407 HWND htxt = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
408 if ((ic = GetWindowTextLengthW(htxt)))
409 {
410 WCHAR *psz, *parent = NULL;
411 SHELLEXECUTEINFOW sei;
412
413 ZeroMemory(&sei, sizeof(sei));
414 sei.cbSize = sizeof(sei);
415 psz = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR));
416
417 if (psz)
418 {
419 GetWindowTextW(htxt, psz, ic + 1);
420
421 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
422 * WM_NOTIFY before execution */
423
424 sei.hwnd = hwnd;
425 sei.nShow = SW_SHOWNORMAL;
426 sei.lpFile = psz;
427
428 if (prfdp->lpstrDirectory)
429 sei.lpDirectory = prfdp->lpstrDirectory;
430 else
431 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
432
433 if (!ShellExecuteExW(&sei))
434 {
435 HeapFree(GetProcessHeap(), 0, psz);
436 HeapFree(GetProcessHeap(), 0, parent);
437 SendMessageW(htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
438 return TRUE;
439 }
440
441 GetWindowTextW(htxt, psz, ic + 1);
442 FillList(htxt, psz, FALSE);
443
444 HeapFree(GetProcessHeap(), 0, psz);
445 HeapFree(GetProcessHeap(), 0, parent);
446 EndDialog(hwnd, 0);
447 }
448 }
449 }
450
451 case IDCANCEL:
452 EndDialog(hwnd, 0);
453 return TRUE;
454
455 case IDC_RUNDLG_BROWSE:
456 {
457 HMODULE hComdlg = NULL;
458 LPFNOFN ofnProc = NULL;
459 WCHAR szFName[1024] = {0};
460 WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
461 OPENFILENAMEW ofn;
462
463 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH);
464 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
465
466 ZeroMemory(&ofn, sizeof(ofn));
467 ofn.lStructSize = sizeof(ofn);
468 ofn.hwndOwner = hwnd;
469 ofn.lpstrFilter = filter;
470 ofn.lpstrFile = szFName;
471 ofn.nMaxFile = _countof(szFName) - 1;
472 ofn.lpstrTitle = szCaption;
473 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
474 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
475
476 if (NULL == (hComdlg = LoadLibraryExW(L"comdlg32", NULL, 0)) ||
477 NULL == (ofnProc = (LPFNOFN)GetProcAddress(hComdlg, "GetOpenFileNameW")))
478 {
479 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
480 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
481 return TRUE;
482 }
483
484 if (ofnProc(&ofn))
485 {
486 SetFocus(GetDlgItem(hwnd, IDOK));
487 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), szFName);
488 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
489 EnableOkButtonFromEditContents(hwnd);
490 SetFocus(GetDlgItem(hwnd, IDOK));
491 }
492
493 FreeLibrary(hComdlg);
494
495 return TRUE;
496 }
497 case IDC_RUNDLG_EDITPATH:
498 {
499 if (HIWORD(wParam) == CBN_EDITCHANGE)
500 {
501 EnableOkButtonFromEditContents(hwnd);
502 }
503 return TRUE;
504 }
505 }
506 return TRUE;
507 }
508 return FALSE;
509 }
510
511 /*
512 * This function grabs the MRU list from the registry and fills the combo-list
513 * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL.
514 */
515 static void FillList(HWND hCb, LPWSTR pszLatest, BOOL fShowDefault)
516 {
517 HKEY hkey;
518 WCHAR *pszList = NULL, *pszCmd = NULL, *pszTmp = NULL, cMatch = 0, cMax = 0x60;
519 WCHAR szIndex[2] = L"-";
520 DWORD dwType, icList = 0, icCmd = 0;
521 LRESULT lRet;
522 UINT Nix;
523
524 SendMessageW(hCb, CB_RESETCONTENT, 0, 0);
525
526 lRet = RegCreateKeyExW(HKEY_CURRENT_USER,
527 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
528 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL);
529 if (lRet != ERROR_SUCCESS)
530 {
531 TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError());
532 return;
533 }
534
535 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, &dwType, NULL, &icList);
536 if (lRet == ERROR_SUCCESS && dwType == REG_SZ && icList > sizeof(WCHAR))
537 {
538 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
539 if (!pszList)
540 {
541 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
542 goto Continue;
543 }
544 pszList[0] = L'\0';
545
546 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, NULL, (LPBYTE)pszList, &icList);
547 if (lRet != ERROR_SUCCESS)
548 {
549 TRACE("Unable to grab MRUList, error %d\n", GetLastError());
550 pszList[0] = L'\0';
551 }
552 }
553 else
554 {
555 Continue:
556 icList = sizeof(WCHAR);
557 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
558 if (!pszList)
559 {
560 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
561 RegCloseKey(hkey);
562 return;
563 }
564 pszList[0] = L'\0';
565 }
566
567 /* Convert the number of bytes from MRUList into number of characters (== number of indices) */
568 icList /= sizeof(WCHAR);
569
570 for (Nix = 0; Nix < icList - 1; Nix++)
571 {
572 if (pszList[Nix] > cMax)
573 cMax = pszList[Nix];
574
575 szIndex[0] = pszList[Nix];
576
577 lRet = RegQueryValueExW(hkey, szIndex, NULL, &dwType, NULL, &icCmd);
578 if (lRet != ERROR_SUCCESS || dwType != REG_SZ)
579 {
580 TRACE("Unable to grab size of index, error %d\n", GetLastError());
581 continue;
582 }
583
584 if (pszCmd)
585 {
586 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd);
587 if (!pszTmp)
588 {
589 TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd);
590 continue;
591 }
592 pszCmd = pszTmp;
593 }
594 else
595 {
596 pszCmd = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icCmd);
597 if (!pszCmd)
598 {
599 TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd);
600 continue;
601 }
602 }
603
604 lRet = RegQueryValueExW(hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd);
605 if (lRet != ERROR_SUCCESS)
606 {
607 TRACE("Unable to grab index, error %d\n", GetLastError());
608 continue;
609 }
610
611 /*
612 * Generally the command string will end up with "\\1".
613 * Find the last backslash in the string and NULL-terminate.
614 * Windows does not seem to check for what comes next, so that
615 * a command of the form:
616 * c:\\my_dir\\myfile.exe
617 * will be cut just after "my_dir", whereas a command of the form:
618 * c:\\my_dir\\myfile.exe\\1
619 * will be cut just after "myfile.exe".
620 */
621 pszTmp = wcsrchr(pszCmd, L'\\');
622 if (pszTmp)
623 *pszTmp = L'\0';
624
625 /*
626 * In the following we try to add pszLatest to the MRU list.
627 * We suppose that our caller has already correctly allocated
628 * the string with enough space for us to append a "\\1".
629 *
630 * FIXME: TODO! (At the moment we don't append it!)
631 */
632
633 if (pszLatest)
634 {
635 if (wcsicmp(pszCmd, pszLatest) == 0)
636 {
637 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd);
638 SetWindowTextW(hCb, pszCmd);
639 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
640
641 cMatch = pszList[Nix];
642 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
643 pszList[0] = cMatch;
644 continue;
645 }
646 }
647
648 if (icList - 1 != 26 || icList - 2 != Nix || cMatch || pszLatest == NULL)
649 {
650 SendMessageW(hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd);
651 if (!Nix && fShowDefault)
652 {
653 SetWindowTextW(hCb, pszCmd);
654 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
655 }
656 }
657 else
658 {
659 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
660 SetWindowTextW(hCb, pszLatest);
661 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
662
663 cMatch = pszList[Nix];
664 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
665 pszList[0] = cMatch;
666 szIndex[0] = cMatch;
667 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (wcslen(pszLatest) + 1) * sizeof(WCHAR));
668 }
669 }
670
671 if (!cMatch && pszLatest != NULL)
672 {
673 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
674 SetWindowTextW(hCb, pszLatest);
675 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
676
677 cMatch = ++cMax;
678
679 if (pszList)
680 {
681 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszList, (++icList) * sizeof(WCHAR));
682 if (!pszTmp)
683 {
684 TRACE("HeapReAlloc failed to reallocate enough bytes\n");
685 goto Cleanup;
686 }
687 pszList = pszTmp;
688 }
689 else
690 {
691 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (++icList) * sizeof(WCHAR));
692 if (!pszList)
693 {
694 TRACE("HeapAlloc failed to allocate enough bytes\n");
695 goto Cleanup;
696 }
697 }
698
699 memmove(&pszList[1], pszList, (icList - 1) * sizeof(WCHAR));
700 pszList[0] = cMatch;
701 szIndex[0] = cMatch;
702 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (wcslen(pszLatest) + 1) * sizeof(WCHAR));
703 }
704
705 Cleanup:
706 RegSetValueExW(hkey, L"MRUList", 0, REG_SZ, (LPBYTE)pszList, (wcslen(pszList) + 1) * sizeof(WCHAR));
707
708 HeapFree(GetProcessHeap(), 0, pszCmd);
709 HeapFree(GetProcessHeap(), 0, pszList);
710
711 RegCloseKey(hkey);
712 }
713
714
715 /*************************************************************************
716 * ConfirmDialog [internal]
717 *
718 * Put up a confirm box, return TRUE if the user confirmed
719 */
720 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
721 {
722 WCHAR Prompt[256];
723 WCHAR Title[256];
724
725 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
726 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
727 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
728 }
729
730
731 /*************************************************************************
732 * RestartDialogEx [SHELL32.730]
733 */
734
735 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
736 {
737 TRACE("(%p)\n", hWndOwner);
738
739 /* FIXME: use lpwstrReason */
740 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
741 {
742 HANDLE hToken;
743 TOKEN_PRIVILEGES npr;
744
745 /* enable the shutdown privilege for the current process */
746 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
747 {
748 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
749 npr.PrivilegeCount = 1;
750 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
751 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
752 CloseHandle(hToken);
753 }
754 ExitWindowsEx(EWX_REBOOT, uReason);
755 }
756
757 return 0;
758 }
759
760 /*************************************************************************
761 * LogOffDialogProc
762 *
763 * NOTES: Used to make the Log Off dialog work
764 *
765 */
766 INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
767 {
768 switch(Message)
769 {
770 case WM_INITDIALOG:
771 {
772 return TRUE;
773 }
774 case WM_CLOSE:
775 EndDialog(hwnd, IDCANCEL);
776 break;
777 case WM_COMMAND:
778 switch(LOWORD(wParam))
779 {
780 case IDOK:
781 ExitWindowsEx(EWX_LOGOFF, 0);
782 break;
783 case IDCANCEL:
784 EndDialog(hwnd, IDCANCEL);
785 break;
786 }
787 break;
788 default:
789 break;
790 }
791 return FALSE;
792 }
793
794
795 /*************************************************************************
796 * LogoffWindowsDialog [SHELL32.54]
797 */
798
799 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
800 {
801 DialogBox(shell32_hInstance, MAKEINTRESOURCE(IDD_LOG_OFF), hWndOwner, LogOffDialogProc);
802 return 0;
803 }
804
805
806 /*************************************************************************
807 * RestartDialog [SHELL32.59]
808 */
809
810 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
811 {
812 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
813 }
814
815 /*************************************************************************
816 * Used to get the shutdown privilege
817 */
818 VOID ExitWindows_GetShutdownPrivilege(VOID)
819 {
820 HANDLE hToken;
821 TOKEN_PRIVILEGES npr;
822
823 /* enable shut down privilege for current process */
824 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
825 {
826 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
827
828 npr.PrivilegeCount = 1;
829 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
830 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
831
832 CloseHandle(hToken);
833 }
834 }
835
836 /*************************************************************************
837 * ExitWindowsDialog_backup
838 *
839 * NOTES
840 * used as a backup solution to shutdown the OS in case msgina.dll somehow
841 * cannot be found.
842 */
843 VOID ExitWindowsDialog_backup(HWND hWndOwner)
844 {
845 TRACE("(%p)\n", hWndOwner);
846
847 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
848 {
849 ExitWindows_GetShutdownPrivilege();
850 ExitWindowsEx(EWX_SHUTDOWN, 0);
851 }
852 }
853
854 /*************************************************************************
855 * ExitWindowsDialog [SHELL32.60]
856 *
857 * NOTES
858 * exported by ordinal
859 */
860 /*
861 * TODO:
862 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
863 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
864 */
865 void WINAPI ExitWindowsDialog(HWND hWndOwner)
866 {
867 typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff);
868 HINSTANCE msginaDll = LoadLibraryA("msgina.dll");
869
870 TRACE("(%p)\n", hWndOwner);
871
872 /* If the DLL cannot be found for any reason, then it simply uses a
873 dialog box to ask if the user wants to shut down the computer. */
874 if(!msginaDll)
875 {
876 TRACE("Unable to load msgina.dll.\n");
877 ExitWindowsDialog_backup(hWndOwner);
878 return;
879 }
880
881 ShellShFunc pShellShutdownDialog = (ShellShFunc) GetProcAddress(msginaDll, "ShellShutdownDialog");
882
883 if(pShellShutdownDialog)
884 {
885 /* Actually call the function */
886 DWORD returnValue = pShellShutdownDialog(hWndOwner, NULL, FALSE);
887
888 switch(returnValue)
889 {
890 case 0x01: /* Log off user */
891 {
892 ExitWindowsEx(EWX_LOGOFF, 0);
893 break;
894 }
895 case 0x02: /* Shut down */
896 {
897 ExitWindows_GetShutdownPrivilege();
898 ExitWindowsEx(EWX_SHUTDOWN, 0);
899 break;
900 }
901 case 0x03: /* Install Updates/Shutdown (?) */
902 {
903 break;
904 }
905 case 0x04: /* Reboot */
906 {
907 ExitWindows_GetShutdownPrivilege();
908 ExitWindowsEx(EWX_REBOOT, 0);
909 break;
910 }
911 case 0x10: /* Sleep */
912 {
913 if(IsPwrSuspendAllowed())
914 {
915 ExitWindows_GetShutdownPrivilege();
916 SetSuspendState(FALSE, FALSE, FALSE);
917 }
918 break;
919 }
920 case 0x40: /* Hibernate */
921 {
922 if(IsPwrHibernateAllowed())
923 {
924 ExitWindows_GetShutdownPrivilege();
925 SetSuspendState(TRUE, FALSE, TRUE);
926 }
927 break;
928 }
929 /* If the option is any other value */
930 default:
931 break;
932 }
933 }
934 else
935 {
936 /* If the function cannot be found, then revert to using the backup solution */
937 TRACE("Unable to find the 'ShellShutdownDialog' function");
938 FreeLibrary(msginaDll);
939 ExitWindowsDialog_backup(hWndOwner);
940 }
941 }