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