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