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 SHELLEXECUTEINFOW sei; 531 NMRUNFILEDLGW nmrfd; 532 533 ic = GetWindowTextLengthW(htxt); 534 if (ic == 0) 535 { 536 EndDialog(hwnd, IDCANCEL); 537 return TRUE; 538 } 539 540 ZeroMemory(&sei, sizeof(sei)); 541 sei.cbSize = sizeof(sei); 542 543 /* 544 * Allocate a new MRU entry, we need to add two characters 545 * for the terminating "\\1" part, then the NULL character. 546 */ 547 psz = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (ic + 2 + 1)*sizeof(WCHAR)); 548 if (!psz) 549 { 550 EndDialog(hwnd, IDCANCEL); 551 return TRUE; 552 } 553 554 GetWindowTextW(htxt, psz, ic + 1); 555 556 sei.hwnd = hwnd; 557 sei.nShow = SW_SHOWNORMAL; 558 sei.lpFile = psz; 559 560 /* 561 * The precedence is the following: first the user-given 562 * current directory is used; if there is none, a current 563 * directory is computed if the RFF_CALCDIRECTORY is set, 564 * otherwise no current directory is defined. 565 */ 566 if (prfdp->lpstrDirectory) 567 sei.lpDirectory = prfdp->lpstrDirectory; 568 else if (prfdp->uFlags & RFF_CALCDIRECTORY) 569 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile); 570 else 571 sei.lpDirectory = NULL; 572 573 /* Hide the dialog for now on, we will show it up in case of retry */ 574 ShowWindow(hwnd, SW_HIDE); 575 576 /* 577 * As shown by manual tests on Windows, modifying the contents 578 * of the notification structure will not modify what the 579 * Run-Dialog will use for the nShow parameter. However the 580 * lpFile and lpDirectory pointers are set to the buffers used 581 * by the Run-Dialog, as a consequence they can be modified by 582 * the notification receiver, as long as it respects the lengths 583 * of the buffers (to avoid buffer overflows). 584 */ 585 nmrfd.hdr.code = RFN_VALIDATE; 586 nmrfd.hdr.hwndFrom = hwnd; 587 nmrfd.hdr.idFrom = 0; 588 nmrfd.lpFile = sei.lpFile; 589 nmrfd.lpDirectory = sei.lpDirectory; 590 nmrfd.nShow = sei.nShow; 591 592 lRet = SendMessageW(prfdp->hwndOwner, WM_NOTIFY, 0, (LPARAM)&nmrfd.hdr); 593 594 switch (lRet) 595 { 596 case RF_CANCEL: 597 EndDialog(hwnd, IDCANCEL); 598 break; 599 600 case RF_OK: 601 if (ShellExecuteExW(&sei)) 602 { 603 /* Call again GetWindowText in case the contents of the edit box has changed? */ 604 GetWindowTextW(htxt, psz, ic + 1); 605 FillList(htxt, psz, ic + 2 + 1, FALSE); 606 EndDialog(hwnd, IDOK); 607 break; 608 } 609 610 /* Fall-back */ 611 case RF_RETRY: 612 default: 613 SendMessageW(htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)); 614 /* Show back the dialog */ 615 ShowWindow(hwnd, SW_SHOW); 616 break; 617 } 618 619 HeapFree(GetProcessHeap(), 0, parent); 620 HeapFree(GetProcessHeap(), 0, psz); 621 return TRUE; 622 } 623 624 case IDCANCEL: 625 EndDialog(hwnd, IDCANCEL); 626 return TRUE; 627 628 case IDC_RUNDLG_BROWSE: 629 { 630 HMODULE hComdlg = NULL; 631 LPFNOFN ofnProc = NULL; 632 WCHAR szFName[1024] = {0}; 633 WCHAR filter[MAX_PATH], szCaption[MAX_PATH]; 634 OPENFILENAMEW ofn; 635 636 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH); 637 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH); 638 639 ZeroMemory(&ofn, sizeof(ofn)); 640 ofn.lStructSize = sizeof(ofn); 641 ofn.hwndOwner = hwnd; 642 ofn.lpstrFilter = filter; 643 ofn.lpstrFile = szFName; 644 ofn.nMaxFile = _countof(szFName) - 1; 645 ofn.lpstrTitle = szCaption; 646 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST; 647 ofn.lpstrInitialDir = prfdp->lpstrDirectory; 648 649 if (NULL == (hComdlg = LoadLibraryExW(L"comdlg32", NULL, 0)) || 650 NULL == (ofnProc = (LPFNOFN)GetProcAddress(hComdlg, "GetOpenFileNameW"))) 651 { 652 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc); 653 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR); 654 return TRUE; 655 } 656 657 if (ofnProc(&ofn)) 658 { 659 SetFocus(GetDlgItem(hwnd, IDOK)); 660 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), szFName); 661 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM(0, -1)); 662 EnableOkButtonFromEditContents(hwnd); 663 SetFocus(GetDlgItem(hwnd, IDOK)); 664 } 665 666 FreeLibrary(hComdlg); 667 668 return TRUE; 669 } 670 case IDC_RUNDLG_EDITPATH: 671 { 672 if (HIWORD(wParam) == CBN_EDITCHANGE) 673 { 674 EnableOkButtonFromEditContents(hwnd); 675 } 676 return TRUE; 677 } 678 } 679 return TRUE; 680 } 681 return FALSE; 682 } 683 684 /* 685 * This function grabs the MRU list from the registry and fills the combo-list 686 * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL. 687 */ 688 // FIXME: Part of this code should be part of some MRUList API, 689 // that is scattered amongst shell32, comctl32 (?!) and comdlg32. 690 static void FillList(HWND hCb, LPWSTR pszLatest, UINT cchStr, BOOL fShowDefault) 691 { 692 HKEY hkey; 693 WCHAR *pszList = NULL, *pszCmd = NULL, *pszTmp = NULL, cMatch = 0, cMax = 0x60; 694 WCHAR szIndex[2] = L"-"; 695 UINT cchLatest; 696 DWORD dwType, icList = 0, icCmd = 0; 697 LRESULT lRet; 698 UINT Nix; 699 700 /* 701 * Retrieve the string length of pszLatest and check whether its buffer size 702 * (cchStr in number of characters) is large enough to add the terminating "\\1" 703 * (and the NULL character). 704 */ 705 if (pszLatest) 706 { 707 cchLatest = wcslen(pszLatest); 708 if (cchStr < cchLatest + 2 + 1) 709 { 710 TRACE("pszLatest buffer is not large enough (%d) to hold the MRU terminator.\n", cchStr); 711 return; 712 } 713 } 714 else 715 { 716 cchStr = 0; 717 } 718 719 SendMessageW(hCb, CB_RESETCONTENT, 0, 0); 720 721 lRet = RegCreateKeyExW(HKEY_CURRENT_USER, 722 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU", 723 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL); 724 if (lRet != ERROR_SUCCESS) 725 { 726 TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError()); 727 return; 728 } 729 730 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, &dwType, NULL, &icList); 731 if (lRet == ERROR_SUCCESS && dwType == REG_SZ && icList > sizeof(WCHAR)) 732 { 733 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList); 734 if (!pszList) 735 { 736 TRACE("HeapAlloc failed to allocate %d bytes\n", icList); 737 goto Continue; 738 } 739 pszList[0] = L'\0'; 740 741 lRet = RegQueryValueExW(hkey, L"MRUList", NULL, NULL, (LPBYTE)pszList, &icList); 742 if (lRet != ERROR_SUCCESS) 743 { 744 TRACE("Unable to grab MRUList, error %d\n", GetLastError()); 745 pszList[0] = L'\0'; 746 } 747 } 748 else 749 { 750 Continue: 751 icList = sizeof(WCHAR); 752 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList); 753 if (!pszList) 754 { 755 TRACE("HeapAlloc failed to allocate %d bytes\n", icList); 756 RegCloseKey(hkey); 757 return; 758 } 759 pszList[0] = L'\0'; 760 } 761 762 /* Convert the number of bytes from MRUList into number of characters (== number of indices) */ 763 icList /= sizeof(WCHAR); 764 765 for (Nix = 0; Nix < icList - 1; Nix++) 766 { 767 if (pszList[Nix] > cMax) 768 cMax = pszList[Nix]; 769 770 szIndex[0] = pszList[Nix]; 771 772 lRet = RegQueryValueExW(hkey, szIndex, NULL, &dwType, NULL, &icCmd); 773 if (lRet != ERROR_SUCCESS || dwType != REG_SZ) 774 { 775 TRACE("Unable to grab size of index, error %d\n", GetLastError()); 776 continue; 777 } 778 779 if (pszCmd) 780 { 781 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd); 782 if (!pszTmp) 783 { 784 TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd); 785 continue; 786 } 787 pszCmd = pszTmp; 788 } 789 else 790 { 791 pszCmd = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icCmd); 792 if (!pszCmd) 793 { 794 TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd); 795 continue; 796 } 797 } 798 799 lRet = RegQueryValueExW(hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd); 800 if (lRet != ERROR_SUCCESS) 801 { 802 TRACE("Unable to grab index, error %d\n", GetLastError()); 803 continue; 804 } 805 806 /* 807 * Generally the command string will end up with "\\1". 808 * Find the last backslash in the string and NULL-terminate. 809 * Windows does not seem to check for what comes next, so that 810 * a command of the form: 811 * c:\\my_dir\\myfile.exe 812 * will be cut just after "my_dir", whereas a command of the form: 813 * c:\\my_dir\\myfile.exe\\1 814 * will be cut just after "myfile.exe". 815 */ 816 pszTmp = wcsrchr(pszCmd, L'\\'); 817 if (pszTmp) 818 *pszTmp = L'\0'; 819 820 /* 821 * In the following we try to add pszLatest to the MRU list. 822 * We suppose that our caller has already correctly allocated 823 * the string with enough space for us to append a "\\1". 824 * 825 * FIXME: TODO! (At the moment we don't append it!) 826 */ 827 828 if (pszLatest) 829 { 830 if (wcsicmp(pszCmd, pszLatest) == 0) 831 { 832 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd); 833 SetWindowTextW(hCb, pszCmd); 834 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1)); 835 836 cMatch = pszList[Nix]; 837 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR)); 838 pszList[0] = cMatch; 839 continue; 840 } 841 } 842 843 if (icList - 1 != 26 || icList - 2 != Nix || cMatch || pszLatest == NULL) 844 { 845 SendMessageW(hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd); 846 if (!Nix && fShowDefault) 847 { 848 SetWindowTextW(hCb, pszCmd); 849 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1)); 850 } 851 } 852 else 853 { 854 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest); 855 SetWindowTextW(hCb, pszLatest); 856 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1)); 857 858 cMatch = pszList[Nix]; 859 memmove(&pszList[1], pszList, Nix * sizeof(WCHAR)); 860 pszList[0] = cMatch; 861 szIndex[0] = cMatch; 862 863 wcscpy(&pszLatest[cchLatest], L"\\1"); 864 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR)); 865 pszLatest[cchLatest] = L'\0'; 866 } 867 } 868 869 if (!cMatch && pszLatest != NULL) 870 { 871 SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest); 872 SetWindowTextW(hCb, pszLatest); 873 SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)); 874 875 cMatch = ++cMax; 876 877 if (pszList) 878 { 879 pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszList, (++icList) * sizeof(WCHAR)); 880 if (!pszTmp) 881 { 882 TRACE("HeapReAlloc failed to reallocate enough bytes\n"); 883 goto Cleanup; 884 } 885 pszList = pszTmp; 886 } 887 else 888 { 889 pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (++icList) * sizeof(WCHAR)); 890 if (!pszList) 891 { 892 TRACE("HeapAlloc failed to allocate enough bytes\n"); 893 goto Cleanup; 894 } 895 } 896 897 memmove(&pszList[1], pszList, (icList - 1) * sizeof(WCHAR)); 898 pszList[0] = cMatch; 899 szIndex[0] = cMatch; 900 901 wcscpy(&pszLatest[cchLatest], L"\\1"); 902 RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR)); 903 pszLatest[cchLatest] = L'\0'; 904 } 905 906 Cleanup: 907 RegSetValueExW(hkey, L"MRUList", 0, REG_SZ, (LPBYTE)pszList, (wcslen(pszList) + 1) * sizeof(WCHAR)); 908 909 HeapFree(GetProcessHeap(), 0, pszCmd); 910 HeapFree(GetProcessHeap(), 0, pszList); 911 912 RegCloseKey(hkey); 913 } 914 915 916 /************************************************************************* 917 * ConfirmDialog [internal] 918 * 919 * Put up a confirm box, return TRUE if the user confirmed 920 */ 921 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId) 922 { 923 WCHAR Prompt[256]; 924 WCHAR Title[256]; 925 926 LoadStringW(shell32_hInstance, PromptId, Prompt, _countof(Prompt)); 927 LoadStringW(shell32_hInstance, TitleId, Title, _countof(Title)); 928 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO | MB_ICONQUESTION) == IDYES; 929 } 930 931 typedef HRESULT (WINAPI *tShellDimScreen)(IUnknown** Unknown, HWND* hWindow); 932 933 BOOL 934 CallShellDimScreen(IUnknown** pUnknown, HWND* hWindow) 935 { 936 static tShellDimScreen ShellDimScreen; 937 static BOOL Initialized = FALSE; 938 if (!Initialized) 939 { 940 HMODULE mod = LoadLibraryW(L"msgina.dll"); 941 ShellDimScreen = (tShellDimScreen)GetProcAddress(mod, (LPCSTR)16); 942 Initialized = TRUE; 943 } 944 945 HRESULT hr = E_FAIL; 946 if (ShellDimScreen) 947 hr = ShellDimScreen(pUnknown, hWindow); 948 return SUCCEEDED(hr); 949 } 950 951 952 /* Used to get the shutdown privilege */ 953 static BOOL 954 EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege) 955 { 956 BOOL Success; 957 HANDLE hToken; 958 TOKEN_PRIVILEGES tp; 959 960 Success = OpenProcessToken(GetCurrentProcess(), 961 TOKEN_ADJUST_PRIVILEGES, 962 &hToken); 963 if (!Success) return Success; 964 965 Success = LookupPrivilegeValueW(NULL, 966 lpszPrivilegeName, 967 &tp.Privileges[0].Luid); 968 if (!Success) goto Quit; 969 970 tp.PrivilegeCount = 1; 971 tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0); 972 973 Success = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL); 974 975 Quit: 976 CloseHandle(hToken); 977 return Success; 978 } 979 980 /************************************************************************* 981 * RestartDialogEx [SHELL32.730] 982 */ 983 984 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason) 985 { 986 TRACE("(%p)\n", hWndOwner); 987 988 CComPtr<IUnknown> fadeHandler; 989 HWND parent; 990 991 if (!CallShellDimScreen(&fadeHandler, &parent)) 992 parent = hWndOwner; 993 994 /* FIXME: use lpwstrReason */ 995 if (ConfirmDialog(parent, IDS_RESTART_PROMPT, IDS_RESTART_TITLE)) 996 { 997 EnablePrivilege(L"SeShutdownPrivilege", TRUE); 998 ExitWindowsEx(EWX_REBOOT, uReason); 999 EnablePrivilege(L"SeShutdownPrivilege", FALSE); 1000 } 1001 1002 return 0; 1003 } 1004 1005 /************************************************************************* 1006 * LogOffDialogProc 1007 * 1008 * NOTES: Used to make the Log Off dialog work 1009 */ 1010 INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 1011 { 1012 switch (uMsg) 1013 { 1014 case WM_INITDIALOG: 1015 return TRUE; 1016 1017 case WM_CLOSE: 1018 EndDialog(hwnd, IDCANCEL); 1019 break; 1020 1021 #if 0 1022 case WM_ACTIVATE: 1023 { 1024 if (LOWORD(wParam) == WA_INACTIVE) 1025 EndDialog(hwnd, 0); 1026 return FALSE; 1027 } 1028 #endif 1029 1030 case WM_COMMAND: 1031 switch (LOWORD(wParam)) 1032 { 1033 case IDOK: 1034 ExitWindowsEx(EWX_LOGOFF, 0); 1035 break; 1036 1037 case IDCANCEL: 1038 EndDialog(hwnd, IDCANCEL); 1039 break; 1040 } 1041 break; 1042 1043 default: 1044 break; 1045 } 1046 return FALSE; 1047 } 1048 1049 /************************************************************************* 1050 * LogoffWindowsDialog [SHELL32.54] 1051 */ 1052 1053 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner) 1054 { 1055 CComPtr<IUnknown> fadeHandler; 1056 HWND parent; 1057 1058 if (!CallShellDimScreen(&fadeHandler, &parent)) 1059 parent = hWndOwner; 1060 1061 DialogBoxW(shell32_hInstance, MAKEINTRESOURCEW(IDD_LOG_OFF), parent, LogOffDialogProc); 1062 return 0; 1063 } 1064 1065 /************************************************************************* 1066 * RestartDialog [SHELL32.59] 1067 */ 1068 1069 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags) 1070 { 1071 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0); 1072 } 1073 1074 /************************************************************************* 1075 * ExitWindowsDialog_backup 1076 * 1077 * NOTES 1078 * Used as a backup solution to shutdown the OS in case msgina.dll 1079 * somehow cannot be found. 1080 */ 1081 VOID ExitWindowsDialog_backup(HWND hWndOwner) 1082 { 1083 TRACE("(%p)\n", hWndOwner); 1084 1085 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE)) 1086 { 1087 EnablePrivilege(L"SeShutdownPrivilege", TRUE); 1088 ExitWindowsEx(EWX_SHUTDOWN, 0); 1089 EnablePrivilege(L"SeShutdownPrivilege", FALSE); 1090 } 1091 } 1092 1093 /************************************************************************* 1094 * ExitWindowsDialog [SHELL32.60] 1095 * 1096 * NOTES 1097 * exported by ordinal 1098 */ 1099 /* 1100 * TODO: 1101 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the 1102 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType. 1103 */ 1104 void WINAPI ExitWindowsDialog(HWND hWndOwner) 1105 { 1106 typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff); 1107 HINSTANCE msginaDll = LoadLibraryW(L"msgina.dll"); 1108 1109 TRACE("(%p)\n", hWndOwner); 1110 1111 CComPtr<IUnknown> fadeHandler; 1112 HWND parent; 1113 if (!CallShellDimScreen(&fadeHandler, &parent)) 1114 parent = hWndOwner; 1115 1116 /* If the DLL cannot be found for any reason, then it simply uses a 1117 dialog box to ask if the user wants to shut down the computer. */ 1118 if (!msginaDll) 1119 { 1120 TRACE("Unable to load msgina.dll.\n"); 1121 ExitWindowsDialog_backup(parent); 1122 return; 1123 } 1124 1125 ShellShFunc pShellShutdownDialog = (ShellShFunc)GetProcAddress(msginaDll, "ShellShutdownDialog"); 1126 1127 if (pShellShutdownDialog) 1128 { 1129 /* Actually call the function */ 1130 DWORD returnValue = pShellShutdownDialog(parent, NULL, FALSE); 1131 1132 switch (returnValue) 1133 { 1134 case 0x01: /* Log off user */ 1135 { 1136 ExitWindowsEx(EWX_LOGOFF, 0); 1137 break; 1138 } 1139 case 0x02: /* Shut down */ 1140 { 1141 EnablePrivilege(L"SeShutdownPrivilege", TRUE); 1142 ExitWindowsEx(EWX_SHUTDOWN, 0); 1143 EnablePrivilege(L"SeShutdownPrivilege", FALSE); 1144 break; 1145 } 1146 case 0x03: /* Install Updates/Shutdown (?) */ 1147 { 1148 break; 1149 } 1150 case 0x04: /* Reboot */ 1151 { 1152 EnablePrivilege(L"SeShutdownPrivilege", TRUE); 1153 ExitWindowsEx(EWX_REBOOT, 0); 1154 EnablePrivilege(L"SeShutdownPrivilege", FALSE); 1155 break; 1156 } 1157 case 0x10: /* Sleep */ 1158 { 1159 if (IsPwrSuspendAllowed()) 1160 { 1161 EnablePrivilege(L"SeShutdownPrivilege", TRUE); 1162 SetSuspendState(FALSE, FALSE, FALSE); 1163 EnablePrivilege(L"SeShutdownPrivilege", FALSE); 1164 } 1165 break; 1166 } 1167 case 0x40: /* Hibernate */ 1168 { 1169 if (IsPwrHibernateAllowed()) 1170 { 1171 EnablePrivilege(L"SeShutdownPrivilege", TRUE); 1172 SetSuspendState(TRUE, FALSE, TRUE); 1173 EnablePrivilege(L"SeShutdownPrivilege", FALSE); 1174 } 1175 break; 1176 } 1177 /* If the option is any other value */ 1178 default: 1179 break; 1180 } 1181 } 1182 else 1183 { 1184 /* If the function cannot be found, then revert to using the backup solution */ 1185 TRACE("Unable to find the 'ShellShutdownDialog' function"); 1186 ExitWindowsDialog_backup(parent); 1187 } 1188 1189 FreeLibrary(msginaDll); 1190 } 1191