[SHELL32] Set the OK button in the run dialog as disabled by default unless text...
[reactos.git] / reactos / dll / win32 / shell32 / dialogs / dialogs.cpp
1 /*
2 * common shell dialogs
3 *
4 * Copyright 2000 Juergen Schmied
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "precomp.h"
22
23 typedef struct
24 {
25 HWND hwndOwner ;
26 HICON hIcon ;
27 LPCWSTR lpstrDirectory ;
28 LPCWSTR lpstrTitle ;
29 LPCWSTR lpstrDescription ;
30 UINT uFlags ;
31 } RUNFILEDLGPARAMS ;
32
33 typedef BOOL (WINAPI * LPFNOFN) (OPENFILENAMEW *) ;
34
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 static INT_PTR CALLBACK RunDlgProc (HWND, UINT, WPARAM, LPARAM) ;
37 static void FillList (HWND, char *, BOOL) ;
38
39
40 /*************************************************************************
41 * PickIconDlg [SHELL32.62]
42 *
43 */
44
45 typedef struct
46 {
47 HMODULE hLibrary;
48 HWND hDlgCtrl;
49 WCHAR szName[MAX_PATH];
50 INT Index;
51 } PICK_ICON_CONTEXT, *PPICK_ICON_CONTEXT;
52
53 BOOL CALLBACK EnumPickIconResourceProc(HMODULE hModule,
54 LPCWSTR lpszType,
55 LPWSTR lpszName,
56 LONG_PTR lParam
57 )
58 {
59 WCHAR szName[100];
60 int index;
61 HICON hIcon;
62 PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)lParam;
63
64 if (IS_INTRESOURCE(lpszName))
65 swprintf(szName, L"%u", (DWORD)lpszName);
66 else
67 wcscpy(szName, (WCHAR*)lpszName);
68
69
70 hIcon = LoadIconW(pIconContext->hLibrary, (LPCWSTR)lpszName);
71 if (hIcon == NULL)
72 return TRUE;
73
74 index = SendMessageW(pIconContext->hDlgCtrl, LB_ADDSTRING, 0, (LPARAM)szName);
75 if (index != LB_ERR)
76 SendMessageW(pIconContext->hDlgCtrl, LB_SETITEMDATA, index, (LPARAM)hIcon);
77
78 return TRUE;
79 }
80
81 static void
82 DestroyIconList(HWND hDlgCtrl)
83 {
84 int count;
85 int index;
86
87 count = SendMessage(hDlgCtrl, LB_GETCOUNT, 0, 0);
88 if (count == LB_ERR)
89 return;
90
91 for(index = 0; index < count; index++)
92 {
93 HICON hIcon = (HICON)SendMessageW(hDlgCtrl, LB_GETITEMDATA, index, 0);
94 DestroyIcon(hIcon);
95 }
96 }
97
98 INT_PTR CALLBACK PickIconProc(HWND hwndDlg,
99 UINT uMsg,
100 WPARAM wParam,
101 LPARAM lParam
102 )
103 {
104 LPMEASUREITEMSTRUCT lpmis;
105 LPDRAWITEMSTRUCT lpdis;
106 HICON hIcon;
107 INT index, count;
108 WCHAR szText[MAX_PATH], szTitle[100], szFilter[100];
109 OPENFILENAMEW ofn = {0};
110
111 PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER);
112
113 switch(uMsg)
114 {
115 case WM_INITDIALOG:
116 pIconContext = (PPICK_ICON_CONTEXT)lParam;
117 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG)pIconContext);
118 pIconContext->hDlgCtrl = GetDlgItem(hwndDlg, IDC_PICKICON_LIST);
119 SendMessageW(pIconContext->hDlgCtrl, LB_SETCOLUMNWIDTH, 32, 0);
120 EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
121 if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
122 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
123 else
124 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
125
126 count = SendMessage(pIconContext->hDlgCtrl, LB_GETCOUNT, 0, 0);
127 if (count != LB_ERR)
128 {
129 if (count > pIconContext->Index)
130 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, pIconContext->Index, 0);
131 else
132 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
133 }
134 return TRUE;
135 case WM_COMMAND:
136 switch(LOWORD(wParam))
137 {
138 case IDOK:
139 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
140 pIconContext->Index = index;
141 GetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName, MAX_PATH);
142 DestroyIconList(pIconContext->hDlgCtrl);
143 EndDialog(hwndDlg, 1);
144 break;
145 case IDCANCEL:
146 DestroyIconList(pIconContext->hDlgCtrl);
147 EndDialog(hwndDlg, 0);
148 break;
149 case IDC_PICKICON_LIST:
150 if (HIWORD(wParam) == LBN_SELCHANGE)
151 InvalidateRect((HWND)lParam, NULL, TRUE); // FIXME USE UPDATE RECT
152 break;
153 case IDC_BUTTON_PATH:
154 szText[0] = 0;
155 szTitle[0] = 0;
156 szFilter[0] = 0;
157 ofn.lStructSize = sizeof(ofn);
158 ofn.hwndOwner = hwndDlg;
159 ofn.lpstrFile = szText;
160 ofn.nMaxFile = MAX_PATH;
161 LoadStringW(shell32_hInstance, IDS_PICK_ICON_TITLE, szTitle, sizeof(szTitle) / sizeof(WCHAR));
162 ofn.lpstrTitle = szTitle;
163 LoadStringW(shell32_hInstance, IDS_PICK_ICON_FILTER, szFilter, sizeof(szFilter) / sizeof(WCHAR));
164 ofn.lpstrFilter = szFilter;
165 if (GetOpenFileNameW(&ofn))
166 {
167 HMODULE hLibrary;
168
169 if (!wcsicmp(pIconContext->szName, szText))
170 break;
171
172 DestroyIconList(pIconContext->hDlgCtrl);
173
174 hLibrary = LoadLibraryExW(szText, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
175 if (hLibrary == NULL)
176 break;
177 FreeLibrary(pIconContext->hLibrary);
178 pIconContext->hLibrary = hLibrary;
179 wcscpy(pIconContext->szName, szText);
180 EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
181 if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
182 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
183 else
184 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
185
186 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
187 }
188 break;
189 }
190 break;
191 case WM_MEASUREITEM:
192 lpmis = (LPMEASUREITEMSTRUCT) lParam;
193 lpmis->itemHeight = 32;
194 lpmis->itemWidth = 64;
195 return TRUE;
196 case WM_DRAWITEM:
197 lpdis = (LPDRAWITEMSTRUCT) lParam;
198 if (lpdis->itemID == (UINT)-1)
199 {
200 break;
201 }
202 switch (lpdis->itemAction)
203 {
204 case ODA_SELECT:
205 case ODA_DRAWENTIRE:
206 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
207 hIcon =(HICON)SendMessage(lpdis->hwndItem, LB_GETITEMDATA, lpdis->itemID, (LPARAM) 0);
208
209 if (lpdis->itemID == (UINT)index)
210 {
211 HBRUSH hBrush;
212 hBrush = CreateSolidBrush(RGB(0, 0, 255));
213 FillRect(lpdis->hDC, &lpdis->rcItem, hBrush);
214 DeleteObject(hBrush);
215 }
216 else
217 {
218 HBRUSH hBrush;
219 hBrush = CreateSolidBrush(RGB(255, 255, 255));
220 FillRect(lpdis->hDC, &lpdis->rcItem, hBrush);
221 DeleteObject(hBrush);
222 }
223 DrawIconEx(lpdis->hDC, lpdis->rcItem.left,lpdis->rcItem.top, hIcon,
224 0,
225 0,
226 0,
227 NULL,
228 DI_NORMAL);
229 break;
230 }
231 break;
232 }
233
234 return FALSE;
235 }
236
237 BOOL WINAPI PickIconDlg(
238 HWND hwndOwner,
239 LPWSTR lpstrFile,
240 UINT nMaxFile,
241 INT* lpdwIconIndex)
242 {
243 HMODULE hLibrary;
244 int res;
245 PICK_ICON_CONTEXT IconContext;
246
247 hLibrary = LoadLibraryExW(lpstrFile, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
248 IconContext.hLibrary = hLibrary;
249 IconContext.Index = *lpdwIconIndex;
250 wcscpy(IconContext.szName, lpstrFile);
251
252 res = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_PICK_ICON), hwndOwner, PickIconProc, (LPARAM)&IconContext);
253 if (res)
254 {
255 wcscpy(lpstrFile, IconContext.szName);
256 *lpdwIconIndex = IconContext.Index;
257 }
258
259 FreeLibrary(hLibrary);
260 return res;
261 }
262
263 /*************************************************************************
264 * RunFileDlg [internal]
265 *
266 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
267 */
268 void WINAPI RunFileDlg(
269 HWND hwndOwner,
270 HICON hIcon,
271 LPCWSTR lpstrDirectory,
272 LPCWSTR lpstrTitle,
273 LPCWSTR lpstrDescription,
274 UINT uFlags)
275 {
276 TRACE("\n");
277
278 RUNFILEDLGPARAMS rfdp;
279 rfdp.hwndOwner = hwndOwner;
280 rfdp.hIcon = hIcon;
281 rfdp.lpstrDirectory = lpstrDirectory;
282 rfdp.lpstrTitle = lpstrTitle;
283 rfdp.lpstrDescription = lpstrDescription;
284 rfdp.uFlags = uFlags;
285
286 DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_RUN), hwndOwner, RunDlgProc, (LPARAM)&rfdp);
287
288 }
289
290
291 /* find the directory that contains the file being run */
292 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
293 {
294 const WCHAR *src;
295 WCHAR *dest, *result, *result_end=NULL;
296 static const WCHAR dotexeW[] = L".exe";
297
298 result = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(strlenW(cmdline)+5));
299
300 if (NULL == result)
301 {
302 TRACE("HeapAlloc couldn't allocate %d bytes\n", sizeof(WCHAR)*(strlenW(cmdline)+5));
303 return NULL;
304 }
305
306 src = cmdline;
307 dest = result;
308
309 if (*src == '"')
310 {
311 src++;
312 while (*src && *src != '"')
313 {
314 if (*src == '\\')
315 result_end = dest;
316 *dest++ = *src++;
317 }
318 }
319 else {
320 while (*src)
321 {
322 if (isspaceW(*src))
323 {
324 *dest = 0;
325 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
326 break;
327 strcatW(dest, dotexeW);
328 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
329 break;
330 }
331 else if (*src == '\\')
332 result_end = dest;
333 *dest++ = *src++;
334 }
335 }
336
337 if (result_end)
338 {
339 *result_end = 0;
340 return result;
341 }
342 else
343 {
344 HeapFree(GetProcessHeap(), 0, result);
345 return NULL;
346 }
347 }
348
349 static void EnableOkButtonFromEditContents(HWND hwnd)
350 {
351 BOOL Enable = FALSE;
352 INT Length, n;
353 HWND Edit = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
354 Length = GetWindowTextLengthA(Edit);
355 if (Length > 0)
356 {
357 PWCHAR psz = (PWCHAR)HeapAlloc(GetProcessHeap(), 0, (Length + 1) * sizeof(WCHAR));
358 if (psz)
359 {
360 GetWindowTextW(Edit, psz, Length + 1);
361 for (n = 0; n < Length && !Enable; ++n)
362 Enable = psz[n] != ' ';
363 HeapFree(GetProcessHeap(), 0, psz);
364 }
365 else
366 Enable = TRUE;
367 }
368 EnableWindow(GetDlgItem(hwnd, IDOK), Enable);
369 }
370
371 /* Dialog procedure for RunFileDlg */
372 static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
373 {
374 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
375
376 switch (message)
377 {
378 case WM_INITDIALOG:
379 prfdp = (RUNFILEDLGPARAMS *)lParam ;
380 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
381
382 if (prfdp->lpstrTitle)
383 SetWindowTextW(hwnd, prfdp->lpstrTitle);
384 if (prfdp->lpstrDescription)
385 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
386 if (prfdp->uFlags & RFF_NOBROWSE)
387 {
388 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
389 ShowWindow(browse, SW_HIDE);
390 EnableWindow(browse, FALSE);
391 }
392 if (prfdp->uFlags & RFF_NOLABEL)
393 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
394 if (prfdp->uFlags & RFF_CALCDIRECTORY)
395 FIXME("RFF_CALCDIRECTORY not supported\n");
396
397 if (prfdp->hIcon == NULL)
398 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
399 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
400 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
401
402 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0);
403 EnableOkButtonFromEditContents(hwnd);
404 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH));
405 return TRUE;
406
407 case WM_COMMAND:
408 switch (LOWORD (wParam))
409 {
410 case IDOK:
411 {
412 int ic;
413 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
414 if ((ic = GetWindowTextLengthW (htxt)))
415 {
416 WCHAR *psz, *parent = NULL;
417 SHELLEXECUTEINFOW sei;
418
419 ZeroMemory (&sei, sizeof(sei));
420 sei.cbSize = sizeof(sei);
421 psz = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR));
422
423 if (psz)
424 {
425 GetWindowTextW(htxt, psz, ic + 1);
426
427 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
428 * WM_NOTIFY before execution */
429
430 sei.hwnd = hwnd;
431 sei.nShow = SW_SHOWNORMAL;
432 sei.lpFile = psz;
433
434 if (prfdp->lpstrDirectory)
435 sei.lpDirectory = prfdp->lpstrDirectory;
436 else
437 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
438
439 if (!ShellExecuteExW(&sei))
440 {
441 HeapFree(GetProcessHeap(), 0, psz);
442 HeapFree(GetProcessHeap(), 0, parent);
443 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
444 return TRUE;
445 }
446
447 /* FillList is still ANSI */
448 GetWindowTextA (htxt, (LPSTR)psz, ic + 1);
449 FillList (htxt, (LPSTR)psz, FALSE);
450
451 HeapFree(GetProcessHeap(), 0, psz);
452 HeapFree(GetProcessHeap(), 0, parent);
453 EndDialog (hwnd, 0);
454 }
455 }
456 }
457
458 case IDCANCEL:
459 EndDialog (hwnd, 0);
460 return TRUE;
461
462 case IDC_RUNDLG_BROWSE:
463 {
464 HMODULE hComdlg = NULL;
465 LPFNOFN ofnProc = NULL;
466 static const WCHAR comdlg32W[] = L"comdlg32";
467 WCHAR szFName[1024] = {0};
468 WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
469 OPENFILENAMEW ofn;
470
471 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH);
472 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
473
474 ZeroMemory(&ofn, sizeof(ofn));
475 ofn.lStructSize = sizeof(OPENFILENAMEW);
476 ofn.hwndOwner = hwnd;
477 ofn.lpstrFilter = filter;
478 ofn.lpstrFile = szFName;
479 ofn.nMaxFile = 1023;
480 ofn.lpstrTitle = szCaption;
481 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
482 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
483
484 if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
485 NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
486 {
487 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
488 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
489 return TRUE;
490 }
491
492 if (ofnProc(&ofn))
493 {
494 SetFocus (GetDlgItem (hwnd, IDOK));
495 SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName);
496 SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
497 EnableOkButtonFromEditContents(hwnd);
498 SetFocus (GetDlgItem (hwnd, IDOK));
499 }
500
501 FreeLibrary (hComdlg);
502
503 return TRUE;
504 }
505 case IDC_RUNDLG_EDITPATH:
506 {
507 if (HIWORD(wParam) == CBN_EDITCHANGE)
508 {
509 EnableOkButtonFromEditContents(hwnd);
510 }
511 return TRUE;
512 }
513 }
514 return TRUE;
515 }
516 return FALSE;
517 }
518
519 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
520 /* fShowDefault ignored if pszLatest != NULL */
521 static void FillList(HWND hCb, char *pszLatest, BOOL fShowDefault)
522 {
523 HKEY hkey ;
524 /* char szDbgMsg[256] = "" ; */
525 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
526 DWORD icList = 0, icCmd = 0 ;
527 UINT Nix ;
528
529 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
530
531 if (ERROR_SUCCESS != RegCreateKeyExA (
532 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
533 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
534 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
535
536 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
537
538 if (icList > 0)
539 {
540 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
541
542 if (pszList)
543 {
544 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
545 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK);
546 }
547 else
548 {
549 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
550 }
551 }
552 else
553 {
554 icList = 1 ;
555 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
556 pszList[0] = 0 ;
557 }
558
559 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
560 {
561 if (pszList[Nix] > cMax)
562 cMax = pszList[Nix] ;
563
564 szIndex[0] = pszList[Nix] ;
565
566 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
567 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
568 if( pszCmd )
569 pszCmd = (char *)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
570 else
571 pszCmd = (char *)HeapAlloc(GetProcessHeap(), 0, icCmd) ;
572 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
573 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
574
575 if (NULL != pszLatest)
576 {
577 if (!lstrcmpiA(pszCmd, pszLatest))
578 {
579 /*
580 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
581 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
582 */
583 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
584 SetWindowTextA (hCb, pszCmd) ;
585 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
586
587 cMatch = pszList[Nix] ;
588 memmove (&pszList[1], pszList, Nix) ;
589 pszList[0] = cMatch ;
590 continue ;
591 }
592 }
593
594 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
595 {
596 /*
597 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
598 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
599 */
600 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
601 if (!Nix && fShowDefault)
602 {
603 SetWindowTextA (hCb, pszCmd) ;
604 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
605 }
606 }
607 else
608 {
609 /*
610 sprintf (szDbgMsg, "Doing loop thing.\n") ;
611 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
612 */
613 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
614 SetWindowTextA (hCb, pszLatest) ;
615 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
616
617 cMatch = pszList[Nix] ;
618 memmove (&pszList[1], pszList, Nix) ;
619 pszList[0] = cMatch ;
620 szIndex[0] = cMatch ;
621 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
622 }
623 }
624
625 if (!cMatch && NULL != pszLatest)
626 {
627 /*
628 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
629 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
630 */
631 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
632 SetWindowTextA (hCb, pszLatest) ;
633 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
634
635 cMatch = ++cMax ;
636 if (pszList)
637 pszList = (char *)HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
638 else
639 pszList = (char *)HeapAlloc(GetProcessHeap(), 0, ++icList) ;
640
641 if (pszList)
642 {
643 memmove (&pszList[1], pszList, icList - 1) ;
644 pszList[0] = cMatch ;
645 szIndex[0] = cMatch ;
646 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
647 }
648 else
649 {
650 TRACE("HeapAlloc or HeapReAlloc failed to allocate enough bytes\n");
651 }
652 }
653
654 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
655
656 HeapFree( GetProcessHeap(), 0, pszCmd) ;
657 HeapFree( GetProcessHeap(), 0, pszList) ;
658 }
659
660
661 /*************************************************************************
662 * ConfirmDialog [internal]
663 *
664 * Put up a confirm box, return TRUE if the user confirmed
665 */
666 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
667 {
668 WCHAR Prompt[256];
669 WCHAR Title[256];
670
671 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
672 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
673 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
674 }
675
676
677 /*************************************************************************
678 * RestartDialogEx [SHELL32.730]
679 */
680
681 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
682 {
683 TRACE("(%p)\n", hWndOwner);
684
685 /* FIXME: use lpwstrReason */
686 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
687 {
688 HANDLE hToken;
689 TOKEN_PRIVILEGES npr;
690
691 /* enable the shutdown privilege for the current process */
692 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
693 {
694 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
695 npr.PrivilegeCount = 1;
696 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
697 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
698 CloseHandle(hToken);
699 }
700 ExitWindowsEx(EWX_REBOOT, uReason);
701 }
702
703 return 0;
704 }
705
706
707 /*************************************************************************
708 * LogoffWindowsDialog [SHELL32.54]
709 */
710
711 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
712 {
713 if (ConfirmDialog(hWndOwner, IDS_LOGOFF_PROMPT, IDS_LOGOFF_TITLE))
714 ExitWindowsEx(EWX_LOGOFF, 0);
715
716 return 0;
717 }
718
719
720 /*************************************************************************
721 * RestartDialog [SHELL32.59]
722 */
723
724 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
725 {
726 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
727 }
728
729 /*************************************************************************
730 * Used to get the shutdown privilege
731 */
732 VOID ExitWindows_GetShutdownPrivilege(VOID)
733 {
734 HANDLE hToken;
735 TOKEN_PRIVILEGES npr;
736
737 /* enable shut down privilege for current process */
738 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
739 {
740 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
741
742 npr.PrivilegeCount = 1;
743 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
744 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
745
746 CloseHandle(hToken);
747 }
748 }
749
750 /*************************************************************************
751 * ExitWindowsDialog_backup
752 *
753 * NOTES
754 * used as a backup solution to shutdown the OS in case msgina.dll somehow
755 * cannot be found.
756 */
757 VOID ExitWindowsDialog_backup(HWND hWndOwner)
758 {
759 TRACE("(%p)\n", hWndOwner);
760
761 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
762 {
763 ExitWindows_GetShutdownPrivilege();
764 ExitWindowsEx(EWX_SHUTDOWN, 0);
765 }
766 }
767
768 /*************************************************************************
769 * ExitWindowsDialog [SHELL32.60]
770 *
771 * NOTES
772 * exported by ordinal
773 */
774 /*
775 * TODO:
776 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
777 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
778 */
779 void WINAPI ExitWindowsDialog(HWND hWndOwner)
780 {
781 typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff);
782 HINSTANCE msginaDll = LoadLibraryA("msgina.dll");
783
784 TRACE("(%p)\n", hWndOwner);
785
786 /* If the DLL cannot be found for any reason, then it simply uses a
787 dialog box to ask if the user wants to shut down the computer. */
788 if(!msginaDll)
789 {
790 TRACE("Unable to load msgina.dll.\n");
791 ExitWindowsDialog_backup(hWndOwner);
792 return;
793 }
794
795 ShellShFunc pShellShutdownDialog = (ShellShFunc) GetProcAddress(msginaDll, "ShellShutdownDialog");
796
797 if(pShellShutdownDialog)
798 {
799 /* Actually call the function */
800 DWORD returnValue = pShellShutdownDialog(hWndOwner, NULL, FALSE);
801
802 switch(returnValue)
803 {
804 case 0x01: /* Log off user */
805 {
806 ExitWindowsEx(EWX_LOGOFF, 0);
807 break;
808 }
809 case 0x02: /* Shut down */
810 {
811 ExitWindows_GetShutdownPrivilege();
812 ExitWindowsEx(EWX_SHUTDOWN, 0);
813 break;
814 }
815 case 0x03: /* Install Updates/Shutdown (?) */
816 {
817 break;
818 }
819 case 0x04: /* Reboot */
820 {
821 ExitWindows_GetShutdownPrivilege();
822 ExitWindowsEx(EWX_REBOOT, 0);
823 break;
824 }
825 case 0x10: /* Sleep */
826 {
827 if(IsPwrSuspendAllowed())
828 {
829 ExitWindows_GetShutdownPrivilege();
830 SetSuspendState(FALSE, FALSE, FALSE);
831 }
832 break;
833 }
834 case 0x40: /* Hibernate */
835 {
836 if(IsPwrHibernateAllowed())
837 {
838 ExitWindows_GetShutdownPrivilege();
839 SetSuspendState(TRUE, FALSE, TRUE);
840 }
841 break;
842 }
843 /* If the option is any other value */
844 default:
845 break;
846 }
847 }
848 else
849 {
850 /* If the function cannot be found, then revert to using the backup solution */
851 TRACE("Unable to find the 'ShellShutdownDialog' function");
852 FreeLibrary(msginaDll);
853 ExitWindowsDialog_backup(hWndOwner);
854 }
855 }