2d939ee11bb98f003479f217961749c4fe6b5c65
[reactos.git] / dll / win32 / shell32 / dialogs / dialogs.cpp
1 /*
2 * common shell dialogs
3 *
4 * Copyright 2000 Juergen Schmied
5 * Copyright 2018 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "precomp.h"
23
24 typedef struct
25 {
26 HWND hwndOwner;
27 HICON hIcon;
28 LPCWSTR lpstrDirectory;
29 LPCWSTR lpstrTitle;
30 LPCWSTR lpstrDescription;
31 UINT uFlags;
32 } RUNFILEDLGPARAMS;
33
34 typedef BOOL (WINAPI * LPFNOFN) (OPENFILENAMEW *);
35
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37 static INT_PTR CALLBACK RunDlgProc(HWND, UINT, WPARAM, LPARAM);
38 static void FillList(HWND, LPWSTR, UINT, BOOL);
39
40
41 /*************************************************************************
42 * PickIconDlg [SHELL32.62]
43 *
44 */
45
46 typedef struct
47 {
48 HMODULE hLibrary;
49 HWND hDlgCtrl;
50 WCHAR szPath[MAX_PATH];
51 WCHAR szExpandedPath[MAX_PATH];
52 INT Index;
53 INT nIcons;
54 HICON *phIcons;
55 } PICK_ICON_CONTEXT, *PPICK_ICON_CONTEXT;
56
57 BOOL CALLBACK EnumPickIconResourceProc(HMODULE hModule,
58 LPCWSTR lpszType,
59 LPWSTR lpszName,
60 LONG_PTR lParam)
61 {
62 PPICK_ICON_CONTEXT pIconContext = PPICK_ICON_CONTEXT(lParam);
63 HWND hDlgCtrl = pIconContext->hDlgCtrl;
64
65 if (IS_INTRESOURCE(lpszName))
66 lParam = LOWORD(lpszName);
67 else
68 lParam = -1;
69
70 SendMessageW(hDlgCtrl, LB_ADDSTRING, 0, lParam);
71
72 return TRUE;
73 }
74
75 static void
76 DestroyIconList(HWND hDlgCtrl, PPICK_ICON_CONTEXT pIconContext)
77 {
78 int count;
79 int index;
80
81 count = SendMessageW(hDlgCtrl, LB_GETCOUNT, 0, 0);
82 if (count == LB_ERR)
83 return;
84
85 for(index = 0; index < count; index++)
86 {
87 DestroyIcon(pIconContext->phIcons[index]);
88 pIconContext->phIcons[index] = NULL;
89 }
90 }
91
92 static BOOL
93 DoLoadIcons(HWND hwndDlg, PICK_ICON_CONTEXT *pIconContext, LPCWSTR pszFile)
94 {
95 // Destroy previous icons
96 DestroyIconList(pIconContext->hDlgCtrl, pIconContext);
97 SendMessageW(pIconContext->hDlgCtrl, LB_RESETCONTENT, 0, 0);
98 delete[] pIconContext->phIcons;
99
100 // Store the paths
101 StringCchCopyW(pIconContext->szPath, _countof(pIconContext->szPath), pszFile);
102 ExpandEnvironmentStringsW(pszFile, pIconContext->szExpandedPath, _countof(pIconContext->szExpandedPath));
103
104 // Load the module if possible
105 HMODULE hLibrary = LoadLibraryExW(pIconContext->szExpandedPath, NULL, LOAD_LIBRARY_AS_DATAFILE);
106 if (pIconContext->hLibrary)
107 FreeLibrary(pIconContext->hLibrary);
108 pIconContext->hLibrary = hLibrary;
109
110 if (pIconContext->hLibrary)
111 {
112 // Load the icons from the module
113 pIconContext->nIcons = ExtractIconExW(pIconContext->szExpandedPath, -1, NULL, NULL, 0);
114 pIconContext->phIcons = new HICON[pIconContext->nIcons];
115
116 if (ExtractIconExW(pIconContext->szExpandedPath, 0, pIconContext->phIcons, NULL, pIconContext->nIcons))
117 {
118 EnumResourceNamesW(pIconContext->hLibrary, RT_GROUP_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
119 }
120 else
121 {
122 pIconContext->nIcons = 0;
123 }
124 }
125 else
126 {
127 // .ico file
128 pIconContext->nIcons = 1;
129 pIconContext->phIcons = new HICON[1];
130
131 if (ExtractIconExW(pIconContext->szExpandedPath, 0, pIconContext->phIcons, NULL, pIconContext->nIcons))
132 {
133 SendMessageW(pIconContext->hDlgCtrl, LB_ADDSTRING, 0, 0);
134 }
135 else
136 {
137 pIconContext->nIcons = 0;
138 }
139 }
140
141 // Set the text
142 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szPath);
143
144 if (pIconContext->nIcons == 0)
145 {
146 delete[] pIconContext->phIcons;
147 pIconContext->phIcons = NULL;
148 }
149
150 return pIconContext->nIcons > 0;
151 }
152
153 static const LPCWSTR s_pszDefaultPath = L"%SystemRoot%\\system32\\shell32.dll";
154
155 static void NoIconsInFile(HWND hwndDlg, PICK_ICON_CONTEXT *pIconContext)
156 {
157 // Show an error message
158 CStringW strText, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE));
159 strText.Format(IDS_NO_ICONS, pIconContext->szPath);
160 MessageBoxW(hwndDlg, strText, strTitle, MB_ICONWARNING);
161
162 // Load the default icons
163 DoLoadIcons(hwndDlg, pIconContext, s_pszDefaultPath);
164 }
165
166 // Icon size
167 #define CX_ICON GetSystemMetrics(SM_CXICON)
168 #define CY_ICON GetSystemMetrics(SM_CYICON)
169
170 // Item size
171 #define CX_ITEM (CX_ICON + 4)
172 #define CY_ITEM (CY_ICON + 12)
173
174 INT_PTR CALLBACK PickIconProc(
175 HWND hwndDlg,
176 UINT uMsg,
177 WPARAM wParam,
178 LPARAM lParam)
179 {
180 LPMEASUREITEMSTRUCT lpmis;
181 LPDRAWITEMSTRUCT lpdis;
182 HICON hIcon;
183 INT index, count;
184 WCHAR szText[MAX_PATH], szFilter[100];
185 CStringW strTitle;
186 OPENFILENAMEW ofn;
187
188 PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER);
189
190 switch(uMsg)
191 {
192 case WM_INITDIALOG:
193 {
194 pIconContext = (PPICK_ICON_CONTEXT)lParam;
195 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pIconContext);
196 pIconContext->hDlgCtrl = GetDlgItem(hwndDlg, IDC_PICKICON_LIST);
197
198 SendMessageW(pIconContext->hDlgCtrl, LB_SETCOLUMNWIDTH, CX_ITEM, 0);
199
200 // Load the icons
201 if (!DoLoadIcons(hwndDlg, pIconContext, pIconContext->szPath))
202 NoIconsInFile(hwndDlg, pIconContext);
203
204 // Set the selection
205 count = SendMessageW(pIconContext->hDlgCtrl, LB_GETCOUNT, 0, 0);
206 if (count != LB_ERR)
207 {
208 if (pIconContext->Index < 0)
209 {
210 // A negative value will be interpreted as a negated resource ID.
211 LPARAM lParam = -pIconContext->Index;
212 pIconContext->Index = (INT)SendMessageW(pIconContext->hDlgCtrl, LB_FINDSTRINGEXACT, -1, lParam);
213 }
214
215 if (pIconContext->Index < 0 || count <= pIconContext->Index)
216 pIconContext->Index = 0;
217
218 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, pIconContext->Index, 0);
219 SendMessageW(pIconContext->hDlgCtrl, LB_SETTOPINDEX, pIconContext->Index, 0);
220 }
221 return TRUE;
222 }
223
224 case WM_COMMAND:
225 switch(LOWORD(wParam))
226 {
227 case IDOK:
228 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
229 pIconContext->Index = index;
230 GetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szPath, _countof(pIconContext->szPath));
231 ExpandEnvironmentStringsW(pIconContext->szPath, pIconContext->szExpandedPath, _countof(pIconContext->szExpandedPath));
232 DestroyIconList(pIconContext->hDlgCtrl, pIconContext);
233 delete[] pIconContext->phIcons;
234 EndDialog(hwndDlg, 1);
235 break;
236
237 case IDCANCEL:
238 DestroyIconList(pIconContext->hDlgCtrl, pIconContext);
239 delete[] pIconContext->phIcons;
240 EndDialog(hwndDlg, 0);
241 break;
242
243 case IDC_PICKICON_LIST:
244 switch (HIWORD(wParam))
245 {
246 case LBN_SELCHANGE:
247 InvalidateRect((HWND)lParam, NULL, TRUE);
248 break;
249
250 case LBN_DBLCLK:
251 SendMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), 0);
252 break;
253 }
254 break;
255
256 case IDC_BUTTON_PATH:
257 {
258 // Choose the module path
259 szText[0] = 0;
260 szFilter[0] = 0;
261 ZeroMemory(&ofn, sizeof(ofn));
262 ofn.lStructSize = sizeof(ofn);
263 ofn.hwndOwner = hwndDlg;
264 ofn.lpstrFile = szText;
265 ofn.nMaxFile = _countof(szText);
266 strTitle.LoadString(IDS_PICK_ICON_TITLE);
267 ofn.lpstrTitle = strTitle;
268 LoadStringW(shell32_hInstance, IDS_PICK_ICON_FILTER, szFilter, _countof(szFilter));
269 ofn.lpstrFilter = szFilter;
270 if (!GetOpenFileNameW(&ofn))
271 break;
272
273 // Load the icons
274 if (!DoLoadIcons(hwndDlg, pIconContext, szText))
275 NoIconsInFile(hwndDlg, pIconContext);
276
277 // Set the selection
278 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
279 break;
280 }
281
282 default:
283 break;
284 }
285 break;
286
287 case WM_MEASUREITEM:
288 lpmis = (LPMEASUREITEMSTRUCT)lParam;
289 lpmis->itemHeight = CY_ITEM;
290 return TRUE;
291
292 case WM_DRAWITEM:
293 {
294 lpdis = (LPDRAWITEMSTRUCT)lParam;
295 if (lpdis->itemID == (UINT)-1)
296 {
297 break;
298 }
299 switch (lpdis->itemAction)
300 {
301 case ODA_SELECT:
302 case ODA_DRAWENTIRE:
303 {
304 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
305 hIcon = pIconContext->phIcons[lpdis->itemID];
306
307 if (lpdis->itemID == (UINT)index)
308 FillRect(lpdis->hDC, &lpdis->rcItem, (HBRUSH)(COLOR_HIGHLIGHT + 1));
309 else
310 FillRect(lpdis->hDC, &lpdis->rcItem, (HBRUSH)(COLOR_WINDOW + 1));
311
312 // Centering
313 INT x = lpdis->rcItem.left + (CX_ITEM - CX_ICON) / 2;
314 INT y = lpdis->rcItem.top + (CY_ITEM - CY_ICON) / 2;
315
316 DrawIconEx(lpdis->hDC, x, y, hIcon, 0, 0, 0, NULL, DI_NORMAL);
317 break;
318 }
319 }
320 return TRUE;
321 }
322 }
323
324 return FALSE;
325 }
326
327 BOOL WINAPI PickIconDlg(
328 HWND hWndOwner,
329 LPWSTR lpstrFile,
330 UINT nMaxFile,
331 INT* lpdwIconIndex)
332 {
333 int res;
334
335 // Initialize the dialog
336 PICK_ICON_CONTEXT IconContext = { NULL };
337 IconContext.Index = *lpdwIconIndex;
338 StringCchCopyW(IconContext.szPath, _countof(IconContext.szPath), lpstrFile);
339 ExpandEnvironmentStringsW(lpstrFile, IconContext.szExpandedPath, _countof(IconContext.szExpandedPath));
340
341 if (!IconContext.szExpandedPath[0] ||
342 GetFileAttributesW(IconContext.szExpandedPath) == INVALID_FILE_ATTRIBUTES)
343 {
344 if (IconContext.szExpandedPath[0])
345 {
346 // No such file
347 CStringW strText, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE));
348 strText.Format(IDS_FILE_NOT_FOUND, lpstrFile);
349 MessageBoxW(hWndOwner, strText, strTitle, MB_ICONWARNING);
350 }
351
352 // Set the default value
353 StringCchCopyW(IconContext.szPath, _countof(IconContext.szPath), s_pszDefaultPath);
354 ExpandEnvironmentStringsW(s_pszDefaultPath, IconContext.szPath, _countof(IconContext.szPath));
355 }
356
357 // Show the dialog
358 res = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_PICK_ICON), hWndOwner, PickIconProc, (LPARAM)&IconContext);
359 if (res)
360 {
361 // Store the selected icon
362 StringCchCopyW(lpstrFile, nMaxFile, IconContext.szExpandedPath);
363 *lpdwIconIndex = IconContext.Index;
364 }
365
366 if (IconContext.hLibrary)
367 FreeLibrary(IconContext.hLibrary);
368 return res;
369 }
370
371 /*************************************************************************
372 * RunFileDlg [internal]
373 *
374 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
375 */
376 void WINAPI RunFileDlg(
377 HWND hWndOwner,
378 HICON hIcon,
379 LPCWSTR lpstrDirectory,
380 LPCWSTR lpstrTitle,
381 LPCWSTR lpstrDescription,
382 UINT uFlags)
383 {
384 TRACE("\n");
385
386 RUNFILEDLGPARAMS rfdp;
387 rfdp.hwndOwner = hWndOwner;
388 rfdp.hIcon = hIcon;
389 rfdp.lpstrDirectory = lpstrDirectory;
390 rfdp.lpstrTitle = lpstrTitle;
391 rfdp.lpstrDescription = lpstrDescription;
392 rfdp.uFlags = uFlags;
393
394 DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_RUN), hWndOwner, RunDlgProc, (LPARAM)&rfdp);
395 }
396
397
398 /* find the directory that contains the file being run */
399 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
400 {
401 const WCHAR *src;
402 WCHAR *dest, *result, *result_end=NULL;
403 static const WCHAR dotexeW[] = L".exe";
404
405 result = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(strlenW(cmdline)+5));
406
407 if (NULL == result)
408 {
409 TRACE("HeapAlloc couldn't allocate %d bytes\n", sizeof(WCHAR)*(strlenW(cmdline)+5));
410 return NULL;
411 }
412
413 src = cmdline;
414 dest = result;
415
416 if (*src == '"')
417 {
418 src++;
419 while (*src && *src != '"')
420 {
421 if (*src == '\\')
422 result_end = dest;
423 *dest++ = *src++;
424 }
425 }
426 else {
427 while (*src)
428 {
429 if (isspaceW(*src))
430 {
431 *dest = 0;
432 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
433 break;
434 strcatW(dest, dotexeW);
435 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
436 break;
437 }
438 else if (*src == '\\')
439 result_end = dest;
440 *dest++ = *src++;
441 }
442 }
443
444 if (result_end)
445 {
446 *result_end = 0;
447 return result;
448 }
449 else
450 {
451 HeapFree(GetProcessHeap(), 0, result);
452 return NULL;
453 }
454 }
455
456 static void EnableOkButtonFromEditContents(HWND hwnd)
457 {
458 BOOL Enable = FALSE;
459 INT Length, n;
460 HWND Edit = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
461 Length = GetWindowTextLengthW(Edit);
462 if (Length > 0)
463 {
464 PWCHAR psz = (PWCHAR)HeapAlloc(GetProcessHeap(), 0, (Length + 1) * sizeof(WCHAR));
465 if (psz)
466 {
467 GetWindowTextW(Edit, psz, Length + 1);
468 for (n = 0; n < Length && !Enable; ++n)
469 Enable = psz[n] != ' ';
470 HeapFree(GetProcessHeap(), 0, psz);
471 }
472 else
473 Enable = TRUE;
474 }
475 EnableWindow(GetDlgItem(hwnd, IDOK), Enable);
476 }
477
478 /* Dialog procedure for RunFileDlg */
479 static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
480 {
481 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
482
483 switch (message)
484 {
485 case WM_INITDIALOG:
486 prfdp = (RUNFILEDLGPARAMS *)lParam;
487 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
488
489 if (prfdp->lpstrTitle)
490 SetWindowTextW(hwnd, prfdp->lpstrTitle);
491 if (prfdp->lpstrDescription)
492 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
493 if (prfdp->uFlags & RFF_NOBROWSE)
494 {
495 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
496 ShowWindow(browse, SW_HIDE);
497 EnableWindow(browse, FALSE);
498 }
499 if (prfdp->uFlags & RFF_NOLABEL)
500 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
501 if (prfdp->uFlags & RFF_NOSEPARATEMEM)
502 {
503 FIXME("RFF_NOSEPARATEMEM not supported\n");
504 }
505
506 /* Use the default Shell Run icon if no one is specified */
507 if (prfdp->hIcon == NULL)
508 prfdp->hIcon = LoadIconW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_RUN));
509 /*
510 * NOTE: Starting Windows Vista, the "Run File" dialog gets a
511 * title icon that remains the same as the default one, even if
512 * the user specifies a custom icon.
513 * Since we currently imitate Windows 2003, therefore do not show
514 * any title icon.
515 */
516 // SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
517 // SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
518 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
519
520 FillList(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), NULL, 0, (prfdp->uFlags & RFF_NODEFAULT) == 0);
521 EnableOkButtonFromEditContents(hwnd);
522 SetFocus(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH));
523 return TRUE;
524
525 case WM_COMMAND:
526 switch (LOWORD(wParam))
527 {
528 case IDOK:
529 {
530 LRESULT lRet;
531 HWND htxt = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
532 INT ic;
533 WCHAR *psz, *parent = NULL;
534 NMRUNFILEDLGW nmrfd;
535
536 ic = GetWindowTextLengthW(htxt);
537 if (ic == 0)
538 {
539 EndDialog(hwnd, IDCANCEL);
540 return TRUE;
541 }
542
543 /*
544 * Allocate a new MRU entry, we need to add two characters
545 * for the terminating "\\1" part, then the NULL character.
546 */
547 psz = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (ic + 2 + 1)*sizeof(WCHAR));
548 if (!psz)
549 {
550 EndDialog(hwnd, IDCANCEL);
551 return TRUE;
552 }
553
554 GetWindowTextW(htxt, psz, ic + 1);
555 StrTrimW(psz, L" \t");
556
557 /*
558 * The precedence is the following: first the user-given
559 * current directory is used; if there is none, a current
560 * directory is computed if the RFF_CALCDIRECTORY is set,
561 * otherwise no current directory is defined.
562 */
563 LPCWSTR pszStartDir;
564 if (prfdp->lpstrDirectory)
565 pszStartDir = prfdp->lpstrDirectory;
566 else if (prfdp->uFlags & RFF_CALCDIRECTORY)
567 pszStartDir = parent = RunDlg_GetParentDir(psz);
568 else
569 pszStartDir = NULL;
570
571 /* Hide the dialog for now on, we will show it up in case of retry */
572 ShowWindow(hwnd, SW_HIDE);
573
574 /*
575 * As shown by manual tests on Windows, modifying the contents
576 * of the notification structure will not modify what the
577 * Run-Dialog will use for the nShow parameter. However the
578 * lpFile and lpDirectory pointers are set to the buffers used
579 * by the Run-Dialog, as a consequence they can be modified by
580 * the notification receiver, as long as it respects the lengths
581 * of the buffers (to avoid buffer overflows).
582 */
583 nmrfd.hdr.code = RFN_VALIDATE;
584 nmrfd.hdr.hwndFrom = hwnd;
585 nmrfd.hdr.idFrom = 0;
586 nmrfd.lpFile = psz;
587 nmrfd.lpDirectory = pszStartDir;
588 nmrfd.nShow = SW_SHOWNORMAL;
589
590 lRet = SendMessageW(prfdp->hwndOwner, WM_NOTIFY, 0, (LPARAM)&nmrfd.hdr);
591
592 switch (lRet)
593 {
594 case RF_CANCEL:
595 EndDialog(hwnd, IDCANCEL);
596 break;
597
598 case RF_OK:
599 if (SUCCEEDED(ShellExecCmdLine(hwnd, psz, pszStartDir, SW_SHOWNORMAL, NULL,
600 SECL_ALLOW_NONEXE)))
601 {
602 /* Call again GetWindowText in case the contents of the edit box has changed? */
603 GetWindowTextW(htxt, psz, ic + 1);
604 FillList(htxt, psz, ic + 2 + 1, FALSE);
605 EndDialog(hwnd, IDOK);
606 break;
607 }
608
609 /* Fall-back */
610 case RF_RETRY:
611 default:
612 SendMessageW(htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
613 /* Show back the dialog */
614 ShowWindow(hwnd, SW_SHOW);
615 break;
616 }
617
618 HeapFree(GetProcessHeap(), 0, parent);
619 HeapFree(GetProcessHeap(), 0, psz);
620 return TRUE;
621 }
622
623 case IDCANCEL:
624 EndDialog(hwnd, IDCANCEL);
625 return TRUE;
626
627 case IDC_RUNDLG_BROWSE:
628 {
629 HMODULE hComdlg = NULL;
630 LPFNOFN ofnProc = NULL;
631 WCHAR szFName[1024] = {0};
632 WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
633 OPENFILENAMEW ofn;
634
635 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, _countof(filter));
636 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, _countof(szCaption));
637
638 ZeroMemory(&ofn, sizeof(ofn));
639 ofn.lStructSize = sizeof(ofn);
640 ofn.hwndOwner = hwnd;
641 ofn.lpstrFilter = filter;
642 ofn.lpstrFile = szFName;
643 ofn.nMaxFile = _countof(szFName) - 1;
644 ofn.lpstrTitle = szCaption;
645 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
646 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
647
648 if (NULL == (hComdlg = LoadLibraryExW(L"comdlg32", NULL, 0)) ||
649 NULL == (ofnProc = (LPFNOFN)GetProcAddress(hComdlg, "GetOpenFileNameW")))
650 {
651 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
652 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
653 return TRUE;
654 }
655
656 if (ofnProc(&ofn))
657 {
658 SetFocus(GetDlgItem(hwnd, IDOK));
659 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), szFName);
660 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
661 EnableOkButtonFromEditContents(hwnd);
662 SetFocus(GetDlgItem(hwnd, IDOK));
663 }
664
665 FreeLibrary(hComdlg);
666
667 return TRUE;
668 }
669 case IDC_RUNDLG_EDITPATH:
670 {
671 if (HIWORD(wParam) == CBN_EDITCHANGE)
672 {
673 EnableOkButtonFromEditContents(hwnd);
674 }
675 return TRUE;
676 }
677 }
678 return TRUE;
679 }
680 return FALSE;
681 }
682
683 /*
684 * This function grabs the MRU list from the registry and fills the combo-list
685 * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL.
686 */
687 // FIXME: Part of this code should be part of some MRUList API,
688 // that is scattered amongst shell32, comctl32 (?!) and comdlg32.
689 static void FillList(HWND hCb, LPWSTR pszLatest, UINT cchStr, BOOL fShowDefault)
690 {
691 HKEY hkey;
692 WCHAR *pszList = NULL, *pszCmd = NULL, *pszTmp = NULL, cMatch = 0, cMax = 0x60;
693 WCHAR szIndex[2] = L"-";
694 UINT cchLatest;
695 DWORD dwType, icList = 0, icCmd = 0;
696 LRESULT lRet;
697 UINT Nix;
698
699 /*
700 * Retrieve the string length of pszLatest and check whether its buffer size
701 * (cchStr in number of characters) is large enough to add the terminating "\\1"
702 * (and the NULL character).
703 */
704 if (pszLatest)
705 {
706 cchLatest = wcslen(pszLatest);
707 if (cchStr < cchLatest + 2 + 1)
708 {
709 TRACE("pszLatest buffer is not large enough (%d) to hold the MRU terminator.\n", cchStr);
710 return;
711 }
712 }
713 else
714 {
715 cchStr = 0;
716 }
717
718 SendMessageW(hCb, CB_RESETCONTENT, 0, 0);
719
720 lRet = RegCreateKeyExW(HKEY_CURRENT_USER,
721 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
722 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL);
723 if (lRet != ERROR_SUCCESS)
724 {
725 TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError());
726 return;
727 }
728
729 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, &dwType, NULL, &icList);
730 if (lRet == ERROR_SUCCESS && dwType == REG_SZ && icList > sizeof(WCHAR))
731 {
732 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
733 if (!pszList)
734 {
735 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
736 goto Continue;
737 }
738 pszList[0] = L'\0';
739
740 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, NULL, (LPBYTE)pszList, &icList);
741 if (lRet != ERROR_SUCCESS)
742 {
743 TRACE("Unable to grab MRUList, error %d\n", GetLastError());
744 pszList[0] = L'\0';
745 }
746 }
747 else
748 {
749 Continue:
750 icList = sizeof(WCHAR);
751 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
752 if (!pszList)
753 {
754 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
755 RegCloseKey(hkey);
756 return;
757 }
758 pszList[0] = L'\0';
759 }
760
761 /* Convert the number of bytes from MRUList into number of characters (== number of indices) */
762 icList /= sizeof(WCHAR);
763
764 for (Nix = 0; Nix < icList - 1; Nix++)
765 {
766 if (pszList[Nix] > cMax)
767 cMax = pszList[Nix];
768
769 szIndex[0] = pszList[Nix];
770
771 lRet = RegQueryValueExW(hkey, szIndex, NULL, &dwType, NULL, &icCmd);
772 if (lRet != ERROR_SUCCESS || dwType != REG_SZ)
773 {
774 TRACE("Unable to grab size of index, error %d\n", GetLastError());
775 continue;
776 }
777
778 if (pszCmd)
779 {
780 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd);
781 if (!pszTmp)
782 {
783 TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd);
784 continue;
785 }
786 pszCmd = pszTmp;
787 }
788 else
789 {
790 pszCmd = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icCmd);
791 if (!pszCmd)
792 {
793 TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd);
794 continue;
795 }
796 }
797
798 lRet = RegQueryValueExW(hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd);
799 if (lRet != ERROR_SUCCESS)
800 {
801 TRACE("Unable to grab index, error %d\n", GetLastError());
802 continue;
803 }
804
805 /*
806 * Generally the command string will end up with "\\1".
807 * Find the last backslash in the string and NULL-terminate.
808 * Windows does not seem to check for what comes next, so that
809 * a command of the form:
810 * c:\\my_dir\\myfile.exe
811 * will be cut just after "my_dir", whereas a command of the form:
812 * c:\\my_dir\\myfile.exe\\1
813 * will be cut just after "myfile.exe".
814 */
815 pszTmp = wcsrchr(pszCmd, L'\\');
816 if (pszTmp)
817 *pszTmp = L'\0';
818
819 /*
820 * In the following we try to add pszLatest to the MRU list.
821 * We suppose that our caller has already correctly allocated
822 * the string with enough space for us to append a "\\1".
823 *
824 * FIXME: TODO! (At the moment we don't append it!)
825 */
826
827 if (pszLatest)
828 {
829 if (wcsicmp(pszCmd, pszLatest) == 0)
830 {
831 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd);
832 SetWindowTextW(hCb, pszCmd);
833 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
834
835 cMatch = pszList[Nix];
836 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
837 pszList[0] = cMatch;
838 continue;
839 }
840 }
841
842 if (icList - 1 != 26 || icList - 2 != Nix || cMatch || pszLatest == NULL)
843 {
844 SendMessageW(hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd);
845 if (!Nix && fShowDefault)
846 {
847 SetWindowTextW(hCb, pszCmd);
848 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
849 }
850 }
851 else
852 {
853 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
854 SetWindowTextW(hCb, pszLatest);
855 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
856
857 cMatch = pszList[Nix];
858 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
859 pszList[0] = cMatch;
860 szIndex[0] = cMatch;
861
862 wcscpy(&pszLatest[cchLatest], L"\\1");
863 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR));
864 pszLatest[cchLatest] = L'\0';
865 }
866 }
867
868 if (!cMatch && pszLatest != NULL)
869 {
870 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
871 SetWindowTextW(hCb, pszLatest);
872 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
873
874 cMatch = ++cMax;
875
876 if (pszList)
877 {
878 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszList, (++icList) * sizeof(WCHAR));
879 if (!pszTmp)
880 {
881 TRACE("HeapReAlloc failed to reallocate enough bytes\n");
882 goto Cleanup;
883 }
884 pszList = pszTmp;
885 }
886 else
887 {
888 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (++icList) * sizeof(WCHAR));
889 if (!pszList)
890 {
891 TRACE("HeapAlloc failed to allocate enough bytes\n");
892 goto Cleanup;
893 }
894 }
895
896 memmove(&pszList[1], pszList, (icList - 1) * sizeof(WCHAR));
897 pszList[0] = cMatch;
898 szIndex[0] = cMatch;
899
900 wcscpy(&pszLatest[cchLatest], L"\\1");
901 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR));
902 pszLatest[cchLatest] = L'\0';
903 }
904
905 Cleanup:
906 RegSetValueExW(hkey, L"MRUList", 0, REG_SZ, (LPBYTE)pszList, (wcslen(pszList) + 1) * sizeof(WCHAR));
907
908 HeapFree(GetProcessHeap(), 0, pszCmd);
909 HeapFree(GetProcessHeap(), 0, pszList);
910
911 RegCloseKey(hkey);
912 }
913
914
915 /*************************************************************************
916 * ConfirmDialog [internal]
917 *
918 * Put up a confirm box, return TRUE if the user confirmed
919 */
920 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
921 {
922 WCHAR Prompt[256];
923 WCHAR Title[256];
924
925 LoadStringW(shell32_hInstance, PromptId, Prompt, _countof(Prompt));
926 LoadStringW(shell32_hInstance, TitleId, Title, _countof(Title));
927 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO | MB_ICONQUESTION) == IDYES;
928 }
929
930 typedef HRESULT (WINAPI *tShellDimScreen)(IUnknown** Unknown, HWND* hWindow);
931
932 BOOL
933 CallShellDimScreen(IUnknown** pUnknown, HWND* hWindow)
934 {
935 static tShellDimScreen ShellDimScreen;
936 static BOOL Initialized = FALSE;
937 if (!Initialized)
938 {
939 HMODULE mod = LoadLibraryW(L"msgina.dll");
940 ShellDimScreen = (tShellDimScreen)GetProcAddress(mod, (LPCSTR)16);
941 Initialized = TRUE;
942 }
943
944 HRESULT hr = E_FAIL;
945 if (ShellDimScreen)
946 hr = ShellDimScreen(pUnknown, hWindow);
947 return SUCCEEDED(hr);
948 }
949
950
951 /* Used to get the shutdown privilege */
952 static BOOL
953 EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
954 {
955 BOOL Success;
956 HANDLE hToken;
957 TOKEN_PRIVILEGES tp;
958
959 Success = OpenProcessToken(GetCurrentProcess(),
960 TOKEN_ADJUST_PRIVILEGES,
961 &hToken);
962 if (!Success) return Success;
963
964 Success = LookupPrivilegeValueW(NULL,
965 lpszPrivilegeName,
966 &tp.Privileges[0].Luid);
967 if (!Success) goto Quit;
968
969 tp.PrivilegeCount = 1;
970 tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
971
972 Success = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
973
974 Quit:
975 CloseHandle(hToken);
976 return Success;
977 }
978
979 /*************************************************************************
980 * RestartDialogEx [SHELL32.730]
981 */
982
983 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
984 {
985 TRACE("(%p)\n", hWndOwner);
986
987 CComPtr<IUnknown> fadeHandler;
988 HWND parent;
989
990 if (!CallShellDimScreen(&fadeHandler, &parent))
991 parent = hWndOwner;
992
993 /* FIXME: use lpwstrReason */
994 if (ConfirmDialog(parent, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
995 {
996 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
997 ExitWindowsEx(EWX_REBOOT, uReason);
998 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
999 }
1000
1001 return 0;
1002 }
1003
1004 /*************************************************************************
1005 * LogOffDialogProc
1006 *
1007 * NOTES: Used to make the Log Off dialog work
1008 */
1009 INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1010 {
1011 switch (uMsg)
1012 {
1013 case WM_INITDIALOG:
1014 return TRUE;
1015
1016 case WM_CLOSE:
1017 EndDialog(hwnd, IDCANCEL);
1018 break;
1019
1020 #if 0
1021 case WM_ACTIVATE:
1022 {
1023 if (LOWORD(wParam) == WA_INACTIVE)
1024 EndDialog(hwnd, 0);
1025 return FALSE;
1026 }
1027 #endif
1028
1029 case WM_COMMAND:
1030 switch (LOWORD(wParam))
1031 {
1032 case IDOK:
1033 ExitWindowsEx(EWX_LOGOFF, 0);
1034 break;
1035
1036 case IDCANCEL:
1037 EndDialog(hwnd, IDCANCEL);
1038 break;
1039 }
1040 break;
1041
1042 default:
1043 break;
1044 }
1045 return FALSE;
1046 }
1047
1048 /*************************************************************************
1049 * LogoffWindowsDialog [SHELL32.54]
1050 */
1051
1052 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
1053 {
1054 CComPtr<IUnknown> fadeHandler;
1055 HWND parent;
1056
1057 if (!CallShellDimScreen(&fadeHandler, &parent))
1058 parent = hWndOwner;
1059
1060 DialogBoxW(shell32_hInstance, MAKEINTRESOURCEW(IDD_LOG_OFF), parent, LogOffDialogProc);
1061 return 0;
1062 }
1063
1064 /*************************************************************************
1065 * RestartDialog [SHELL32.59]
1066 */
1067
1068 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
1069 {
1070 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
1071 }
1072
1073 /*************************************************************************
1074 * ExitWindowsDialog_backup
1075 *
1076 * NOTES
1077 * Used as a backup solution to shutdown the OS in case msgina.dll
1078 * somehow cannot be found.
1079 */
1080 VOID ExitWindowsDialog_backup(HWND hWndOwner)
1081 {
1082 TRACE("(%p)\n", hWndOwner);
1083
1084 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
1085 {
1086 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1087 ExitWindowsEx(EWX_SHUTDOWN, 0);
1088 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1089 }
1090 }
1091
1092 /*************************************************************************
1093 * ExitWindowsDialog [SHELL32.60]
1094 *
1095 * NOTES
1096 * exported by ordinal
1097 */
1098 /*
1099 * TODO:
1100 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
1101 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
1102 */
1103 void WINAPI ExitWindowsDialog(HWND hWndOwner)
1104 {
1105 typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff);
1106 HINSTANCE msginaDll = LoadLibraryW(L"msgina.dll");
1107
1108 TRACE("(%p)\n", hWndOwner);
1109
1110 CComPtr<IUnknown> fadeHandler;
1111 HWND parent;
1112 if (!CallShellDimScreen(&fadeHandler, &parent))
1113 parent = hWndOwner;
1114
1115 /* If the DLL cannot be found for any reason, then it simply uses a
1116 dialog box to ask if the user wants to shut down the computer. */
1117 if (!msginaDll)
1118 {
1119 TRACE("Unable to load msgina.dll.\n");
1120 ExitWindowsDialog_backup(parent);
1121 return;
1122 }
1123
1124 ShellShFunc pShellShutdownDialog = (ShellShFunc)GetProcAddress(msginaDll, "ShellShutdownDialog");
1125
1126 if (pShellShutdownDialog)
1127 {
1128 /* Actually call the function */
1129 DWORD returnValue = pShellShutdownDialog(parent, NULL, FALSE);
1130
1131 switch (returnValue)
1132 {
1133 case 0x01: /* Log off user */
1134 {
1135 ExitWindowsEx(EWX_LOGOFF, 0);
1136 break;
1137 }
1138 case 0x02: /* Shut down */
1139 {
1140 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1141 ExitWindowsEx(EWX_SHUTDOWN, 0);
1142 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1143 break;
1144 }
1145 case 0x03: /* Install Updates/Shutdown (?) */
1146 {
1147 break;
1148 }
1149 case 0x04: /* Reboot */
1150 {
1151 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1152 ExitWindowsEx(EWX_REBOOT, 0);
1153 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1154 break;
1155 }
1156 case 0x10: /* Sleep */
1157 {
1158 if (IsPwrSuspendAllowed())
1159 {
1160 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1161 SetSuspendState(FALSE, FALSE, FALSE);
1162 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1163 }
1164 break;
1165 }
1166 case 0x40: /* Hibernate */
1167 {
1168 if (IsPwrHibernateAllowed())
1169 {
1170 EnablePrivilege(L"SeShutdownPrivilege", TRUE);
1171 SetSuspendState(TRUE, FALSE, TRUE);
1172 EnablePrivilege(L"SeShutdownPrivilege", FALSE);
1173 }
1174 break;
1175 }
1176 /* If the option is any other value */
1177 default:
1178 break;
1179 }
1180 }
1181 else
1182 {
1183 /* If the function cannot be found, then revert to using the backup solution */
1184 TRACE("Unable to find the 'ShellShutdownDialog' function");
1185 ExitWindowsDialog_backup(parent);
1186 }
1187
1188 FreeLibrary(msginaDll);
1189 }