3d66374ce3bc8e766b6e0808c5377668417439d3
[reactos.git] / 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, UINT, 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 StringCbCopyW(szName, sizeof(szName), 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 (ExpandEnvironmentStringsW(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 (ExpandEnvironmentStringsW(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_NOSEPARATEMEM)
380 {
381 FIXME("RFF_NOSEPARATEMEM not supported\n");
382 }
383
384 /* Use the default Shell Run icon if no one is specified */
385 if (prfdp->hIcon == NULL)
386 prfdp->hIcon = LoadIconW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_RUN));
387 /*
388 * NOTE: Starting Windows Vista, the "Run File" dialog gets a
389 * title icon that remains the same as the default one, even if
390 * the user specifies a custom icon.
391 * Since we currently imitate Windows 2003, therefore do not show
392 * any title icon.
393 */
394 // SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
395 // SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
396 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
397
398 FillList(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), NULL, 0, (prfdp->uFlags & RFF_NODEFAULT) == 0);
399 EnableOkButtonFromEditContents(hwnd);
400 SetFocus(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH));
401 return TRUE;
402
403 case WM_COMMAND:
404 switch (LOWORD(wParam))
405 {
406 case IDOK:
407 {
408 LRESULT lRet;
409 HWND htxt = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
410 INT ic;
411 WCHAR *psz, *parent = NULL;
412 SHELLEXECUTEINFOW sei;
413 NMRUNFILEDLGW nmrfd;
414
415 ic = GetWindowTextLengthW(htxt);
416 if (ic == 0)
417 {
418 EndDialog(hwnd, IDCANCEL);
419 return TRUE;
420 }
421
422 ZeroMemory(&sei, sizeof(sei));
423 sei.cbSize = sizeof(sei);
424
425 /*
426 * Allocate a new MRU entry, we need to add two characters
427 * for the terminating "\\1" part, then the NULL character.
428 */
429 psz = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (ic + 2 + 1)*sizeof(WCHAR));
430 if (!psz)
431 {
432 EndDialog(hwnd, IDCANCEL);
433 return TRUE;
434 }
435
436 GetWindowTextW(htxt, psz, ic + 1);
437
438 sei.hwnd = hwnd;
439 sei.nShow = SW_SHOWNORMAL;
440 sei.lpFile = psz;
441
442 /*
443 * The precedence is the following: first the user-given
444 * current directory is used; if there is none, a current
445 * directory is computed if the RFF_CALCDIRECTORY is set,
446 * otherwise no current directory is defined.
447 */
448 if (prfdp->lpstrDirectory)
449 sei.lpDirectory = prfdp->lpstrDirectory;
450 else if (prfdp->uFlags & RFF_CALCDIRECTORY)
451 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
452 else
453 sei.lpDirectory = NULL;
454
455 /* Hide the dialog for now on, we will show it up in case of retry */
456 ShowWindow(hwnd, SW_HIDE);
457
458 /*
459 * As shown by manual tests on Windows, modifying the contents
460 * of the notification structure will not modify what the
461 * Run-Dialog will use for the nShow parameter. However the
462 * lpFile and lpDirectory pointers are set to the buffers used
463 * by the Run-Dialog, as a consequence they can be modified by
464 * the notification receiver, as long as it respects the lengths
465 * of the buffers (to avoid buffer overflows).
466 */
467 nmrfd.hdr.code = RFN_VALIDATE;
468 nmrfd.hdr.hwndFrom = hwnd;
469 nmrfd.hdr.idFrom = 0;
470 nmrfd.lpFile = sei.lpFile;
471 nmrfd.lpDirectory = sei.lpDirectory;
472 nmrfd.nShow = sei.nShow;
473
474 lRet = SendMessageW(prfdp->hwndOwner, WM_NOTIFY, 0, (LPARAM)&nmrfd.hdr);
475
476 switch (lRet)
477 {
478 case RF_CANCEL:
479 EndDialog(hwnd, IDCANCEL);
480 break;
481
482 case RF_OK:
483 if (ShellExecuteExW(&sei))
484 {
485 /* Call again GetWindowText in case the contents of the edit box has changed? */
486 GetWindowTextW(htxt, psz, ic + 1);
487 FillList(htxt, psz, ic + 2 + 1, FALSE);
488 EndDialog(hwnd, IDOK);
489 break;
490 }
491
492 /* Fall-back */
493 case RF_RETRY:
494 default:
495 SendMessageW(htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
496 /* Show back the dialog */
497 ShowWindow(hwnd, SW_SHOW);
498 break;
499 }
500
501 HeapFree(GetProcessHeap(), 0, parent);
502 HeapFree(GetProcessHeap(), 0, psz);
503 return TRUE;
504 }
505
506 case IDCANCEL:
507 EndDialog(hwnd, IDCANCEL);
508 return TRUE;
509
510 case IDC_RUNDLG_BROWSE:
511 {
512 HMODULE hComdlg = NULL;
513 LPFNOFN ofnProc = NULL;
514 WCHAR szFName[1024] = {0};
515 WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
516 OPENFILENAMEW ofn;
517
518 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH);
519 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
520
521 ZeroMemory(&ofn, sizeof(ofn));
522 ofn.lStructSize = sizeof(ofn);
523 ofn.hwndOwner = hwnd;
524 ofn.lpstrFilter = filter;
525 ofn.lpstrFile = szFName;
526 ofn.nMaxFile = _countof(szFName) - 1;
527 ofn.lpstrTitle = szCaption;
528 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
529 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
530
531 if (NULL == (hComdlg = LoadLibraryExW(L"comdlg32", NULL, 0)) ||
532 NULL == (ofnProc = (LPFNOFN)GetProcAddress(hComdlg, "GetOpenFileNameW")))
533 {
534 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
535 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
536 return TRUE;
537 }
538
539 if (ofnProc(&ofn))
540 {
541 SetFocus(GetDlgItem(hwnd, IDOK));
542 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), szFName);
543 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
544 EnableOkButtonFromEditContents(hwnd);
545 SetFocus(GetDlgItem(hwnd, IDOK));
546 }
547
548 FreeLibrary(hComdlg);
549
550 return TRUE;
551 }
552 case IDC_RUNDLG_EDITPATH:
553 {
554 if (HIWORD(wParam) == CBN_EDITCHANGE)
555 {
556 EnableOkButtonFromEditContents(hwnd);
557 }
558 return TRUE;
559 }
560 }
561 return TRUE;
562 }
563 return FALSE;
564 }
565
566 /*
567 * This function grabs the MRU list from the registry and fills the combo-list
568 * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL.
569 */
570 // FIXME: Part of this code should be part of some MRUList API,
571 // that is scattered amongst shell32, comctl32 (?!) and comdlg32.
572 static void FillList(HWND hCb, LPWSTR pszLatest, UINT cchStr, BOOL fShowDefault)
573 {
574 HKEY hkey;
575 WCHAR *pszList = NULL, *pszCmd = NULL, *pszTmp = NULL, cMatch = 0, cMax = 0x60;
576 WCHAR szIndex[2] = L"-";
577 UINT cchLatest;
578 DWORD dwType, icList = 0, icCmd = 0;
579 LRESULT lRet;
580 UINT Nix;
581
582 /*
583 * Retrieve the string length of pszLatest and check whether its buffer size
584 * (cchStr in number of characters) is large enough to add the terminating "\\1"
585 * (and the NULL character).
586 */
587 if (pszLatest)
588 {
589 cchLatest = wcslen(pszLatest);
590 if (cchStr < cchLatest + 2 + 1)
591 {
592 TRACE("pszLatest buffer is not large enough (%d) to hold the MRU terminator.\n", cchStr);
593 return;
594 }
595 }
596 else
597 {
598 cchStr = 0;
599 }
600
601 SendMessageW(hCb, CB_RESETCONTENT, 0, 0);
602
603 lRet = RegCreateKeyExW(HKEY_CURRENT_USER,
604 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
605 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL);
606 if (lRet != ERROR_SUCCESS)
607 {
608 TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError());
609 return;
610 }
611
612 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, &dwType, NULL, &icList);
613 if (lRet == ERROR_SUCCESS && dwType == REG_SZ && icList > sizeof(WCHAR))
614 {
615 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
616 if (!pszList)
617 {
618 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
619 goto Continue;
620 }
621 pszList[0] = L'\0';
622
623 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, NULL, (LPBYTE)pszList, &icList);
624 if (lRet != ERROR_SUCCESS)
625 {
626 TRACE("Unable to grab MRUList, error %d\n", GetLastError());
627 pszList[0] = L'\0';
628 }
629 }
630 else
631 {
632 Continue:
633 icList = sizeof(WCHAR);
634 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
635 if (!pszList)
636 {
637 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
638 RegCloseKey(hkey);
639 return;
640 }
641 pszList[0] = L'\0';
642 }
643
644 /* Convert the number of bytes from MRUList into number of characters (== number of indices) */
645 icList /= sizeof(WCHAR);
646
647 for (Nix = 0; Nix < icList - 1; Nix++)
648 {
649 if (pszList[Nix] > cMax)
650 cMax = pszList[Nix];
651
652 szIndex[0] = pszList[Nix];
653
654 lRet = RegQueryValueExW(hkey, szIndex, NULL, &dwType, NULL, &icCmd);
655 if (lRet != ERROR_SUCCESS || dwType != REG_SZ)
656 {
657 TRACE("Unable to grab size of index, error %d\n", GetLastError());
658 continue;
659 }
660
661 if (pszCmd)
662 {
663 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd);
664 if (!pszTmp)
665 {
666 TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd);
667 continue;
668 }
669 pszCmd = pszTmp;
670 }
671 else
672 {
673 pszCmd = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icCmd);
674 if (!pszCmd)
675 {
676 TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd);
677 continue;
678 }
679 }
680
681 lRet = RegQueryValueExW(hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd);
682 if (lRet != ERROR_SUCCESS)
683 {
684 TRACE("Unable to grab index, error %d\n", GetLastError());
685 continue;
686 }
687
688 /*
689 * Generally the command string will end up with "\\1".
690 * Find the last backslash in the string and NULL-terminate.
691 * Windows does not seem to check for what comes next, so that
692 * a command of the form:
693 * c:\\my_dir\\myfile.exe
694 * will be cut just after "my_dir", whereas a command of the form:
695 * c:\\my_dir\\myfile.exe\\1
696 * will be cut just after "myfile.exe".
697 */
698 pszTmp = wcsrchr(pszCmd, L'\\');
699 if (pszTmp)
700 *pszTmp = L'\0';
701
702 /*
703 * In the following we try to add pszLatest to the MRU list.
704 * We suppose that our caller has already correctly allocated
705 * the string with enough space for us to append a "\\1".
706 *
707 * FIXME: TODO! (At the moment we don't append it!)
708 */
709
710 if (pszLatest)
711 {
712 if (wcsicmp(pszCmd, pszLatest) == 0)
713 {
714 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd);
715 SetWindowTextW(hCb, pszCmd);
716 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
717
718 cMatch = pszList[Nix];
719 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
720 pszList[0] = cMatch;
721 continue;
722 }
723 }
724
725 if (icList - 1 != 26 || icList - 2 != Nix || cMatch || pszLatest == NULL)
726 {
727 SendMessageW(hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd);
728 if (!Nix && fShowDefault)
729 {
730 SetWindowTextW(hCb, pszCmd);
731 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
732 }
733 }
734 else
735 {
736 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
737 SetWindowTextW(hCb, pszLatest);
738 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
739
740 cMatch = pszList[Nix];
741 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
742 pszList[0] = cMatch;
743 szIndex[0] = cMatch;
744
745 wcscpy(&pszLatest[cchLatest], L"\\1");
746 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR));
747 pszLatest[cchLatest] = L'\0';
748 }
749 }
750
751 if (!cMatch && pszLatest != NULL)
752 {
753 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
754 SetWindowTextW(hCb, pszLatest);
755 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
756
757 cMatch = ++cMax;
758
759 if (pszList)
760 {
761 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszList, (++icList) * sizeof(WCHAR));
762 if (!pszTmp)
763 {
764 TRACE("HeapReAlloc failed to reallocate enough bytes\n");
765 goto Cleanup;
766 }
767 pszList = pszTmp;
768 }
769 else
770 {
771 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (++icList) * sizeof(WCHAR));
772 if (!pszList)
773 {
774 TRACE("HeapAlloc failed to allocate enough bytes\n");
775 goto Cleanup;
776 }
777 }
778
779 memmove(&pszList[1], pszList, (icList - 1) * sizeof(WCHAR));
780 pszList[0] = cMatch;
781 szIndex[0] = cMatch;
782
783 wcscpy(&pszLatest[cchLatest], L"\\1");
784 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR));
785 pszLatest[cchLatest] = L'\0';
786 }
787
788 Cleanup:
789 RegSetValueExW(hkey, L"MRUList", 0, REG_SZ, (LPBYTE)pszList, (wcslen(pszList) + 1) * sizeof(WCHAR));
790
791 HeapFree(GetProcessHeap(), 0, pszCmd);
792 HeapFree(GetProcessHeap(), 0, pszList);
793
794 RegCloseKey(hkey);
795 }
796
797
798 /*************************************************************************
799 * ConfirmDialog [internal]
800 *
801 * Put up a confirm box, return TRUE if the user confirmed
802 */
803 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
804 {
805 WCHAR Prompt[256];
806 WCHAR Title[256];
807
808 LoadStringW(shell32_hInstance, PromptId, Prompt, _countof(Prompt));
809 LoadStringW(shell32_hInstance, TitleId, Title, _countof(Title));
810 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO | MB_ICONQUESTION) == IDYES;
811 }
812
813 typedef HRESULT (WINAPI *tShellDimScreen)(IUnknown** Unknown, HWND* hWindow);
814
815 BOOL
816 CallShellDimScreen(IUnknown** pUnknown, HWND* hWindow)
817 {
818 static tShellDimScreen ShellDimScreen;
819 static BOOL Initialized = FALSE;
820 if (!Initialized)
821 {
822 HMODULE mod = LoadLibraryW(L"msgina.dll");
823 ShellDimScreen = (tShellDimScreen)GetProcAddress(mod, (LPCSTR)16);
824 Initialized = TRUE;
825 }
826
827 HRESULT hr = E_FAIL;
828 if (ShellDimScreen)
829 hr = ShellDimScreen(pUnknown, hWindow);
830 return SUCCEEDED(hr);
831 }
832
833
834 /* Used to get the shutdown privilege */
835 static BOOL
836 EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
837 {
838 BOOL Success;
839 HANDLE hToken;
840 TOKEN_PRIVILEGES tp;
841
842 Success = OpenProcessToken(GetCurrentProcess(),
843 TOKEN_ADJUST_PRIVILEGES,
844 &hToken);
845 if (!Success) return Success;
846
847 Success = LookupPrivilegeValueW(NULL,
848 lpszPrivilegeName,
849 &tp.Privileges[0].Luid);
850 if (!Success) goto Quit;
851
852 tp.PrivilegeCount = 1;
853 tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
854
855 Success = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
856
857 Quit:
858 CloseHandle(hToken);
859 return Success;
860 }
861
862 /*************************************************************************
863 * RestartDialogEx [SHELL32.730]
864 */
865
866 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
867 {
868 TRACE("(%p)\n", hWndOwner);
869
870 CComPtr<IUnknown> fadeHandler;
871 HWND parent;
872
873 if (!CallShellDimScreen(&fadeHandler, &parent))
874 parent = hWndOwner;
875
876 /* FIXME: use lpwstrReason */
877 if (ConfirmDialog(parent, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
878 {
879 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
880 ExitWindowsEx(EWX_REBOOT, uReason);
881 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
882 }
883
884 return 0;
885 }
886
887 /*************************************************************************
888 * LogOffDialogProc
889 *
890 * NOTES: Used to make the Log Off dialog work
891 */
892 INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
893 {
894 switch (uMsg)
895 {
896 case WM_INITDIALOG:
897 return TRUE;
898
899 case WM_CLOSE:
900 EndDialog(hwnd, IDCANCEL);
901 break;
902
903 #if 0
904 case WM_ACTIVATE:
905 {
906 if (LOWORD(wParam) == WA_INACTIVE)
907 EndDialog(hwnd, 0);
908 return FALSE;
909 }
910 #endif
911
912 case WM_COMMAND:
913 switch (LOWORD(wParam))
914 {
915 case IDOK:
916 ExitWindowsEx(EWX_LOGOFF, 0);
917 break;
918
919 case IDCANCEL:
920 EndDialog(hwnd, IDCANCEL);
921 break;
922 }
923 break;
924
925 default:
926 break;
927 }
928 return FALSE;
929 }
930
931 /*************************************************************************
932 * LogoffWindowsDialog [SHELL32.54]
933 */
934
935 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
936 {
937 CComPtr<IUnknown> fadeHandler;
938 HWND parent;
939
940 if (!CallShellDimScreen(&fadeHandler, &parent))
941 parent = hWndOwner;
942
943 DialogBoxW(shell32_hInstance, MAKEINTRESOURCEW(IDD_LOG_OFF), parent, LogOffDialogProc);
944 return 0;
945 }
946
947 /*************************************************************************
948 * RestartDialog [SHELL32.59]
949 */
950
951 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
952 {
953 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
954 }
955
956 /*************************************************************************
957 * ExitWindowsDialog_backup
958 *
959 * NOTES
960 * Used as a backup solution to shutdown the OS in case msgina.dll
961 * somehow cannot be found.
962 */
963 VOID ExitWindowsDialog_backup(HWND hWndOwner)
964 {
965 TRACE("(%p)\n", hWndOwner);
966
967 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
968 {
969 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
970 ExitWindowsEx(EWX_SHUTDOWN, 0);
971 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
972 }
973 }
974
975 /*************************************************************************
976 * ExitWindowsDialog [SHELL32.60]
977 *
978 * NOTES
979 * exported by ordinal
980 */
981 /*
982 * TODO:
983 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
984 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
985 */
986 void WINAPI ExitWindowsDialog(HWND hWndOwner)
987 {
988 typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff);
989 HINSTANCE msginaDll = LoadLibraryW(L"msgina.dll");
990
991 TRACE("(%p)\n", hWndOwner);
992
993 CComPtr<IUnknown> fadeHandler;
994 HWND parent;
995 if (!CallShellDimScreen(&fadeHandler, &parent))
996 parent = hWndOwner;
997
998 /* If the DLL cannot be found for any reason, then it simply uses a
999 dialog box to ask if the user wants to shut down the computer. */
1000 if (!msginaDll)
1001 {
1002 TRACE("Unable to load msgina.dll.\n");
1003 ExitWindowsDialog_backup(parent);
1004 return;
1005 }
1006
1007 ShellShFunc pShellShutdownDialog = (ShellShFunc)GetProcAddress(msginaDll, "ShellShutdownDialog");
1008
1009 if (pShellShutdownDialog)
1010 {
1011 /* Actually call the function */
1012 DWORD returnValue = pShellShutdownDialog(parent, NULL, FALSE);
1013
1014 switch (returnValue)
1015 {
1016 case 0x01: /* Log off user */
1017 {
1018 ExitWindowsEx(EWX_LOGOFF, 0);
1019 break;
1020 }
1021 case 0x02: /* Shut down */
1022 {
1023 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1024 ExitWindowsEx(EWX_SHUTDOWN, 0);
1025 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1026 break;
1027 }
1028 case 0x03: /* Install Updates/Shutdown (?) */
1029 {
1030 break;
1031 }
1032 case 0x04: /* Reboot */
1033 {
1034 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1035 ExitWindowsEx(EWX_REBOOT, 0);
1036 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1037 break;
1038 }
1039 case 0x10: /* Sleep */
1040 {
1041 if (IsPwrSuspendAllowed())
1042 {
1043 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1044 SetSuspendState(FALSE, FALSE, FALSE);
1045 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1046 }
1047 break;
1048 }
1049 case 0x40: /* Hibernate */
1050 {
1051 if (IsPwrHibernateAllowed())
1052 {
1053 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1054 SetSuspendState(TRUE, FALSE, TRUE);
1055 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1056 }
1057 break;
1058 }
1059 /* If the option is any other value */
1060 default:
1061 break;
1062 }
1063 }
1064 else
1065 {
1066 /* If the function cannot be found, then revert to using the backup solution */
1067 TRACE("Unable to find the 'ShellShutdownDialog' function");
1068 ExitWindowsDialog_backup(parent);
1069 }
1070
1071 FreeLibrary(msginaDll);
1072 }