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