[shell32.dll]
[reactos.git] / dll / win32 / shell32 / 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
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, char *, BOOL) ;
39
40
41 /*************************************************************************
42 * PickIconDlg [SHELL32.62]
43 *
44 */
45
46 typedef struct
47 {
48 HMODULE hLibrary;
49 HWND hDlgCtrl;
50 WCHAR szName[MAX_PATH];
51 INT Index;
52 }PICK_ICON_CONTEXT, *PPICK_ICON_CONTEXT;
53
54 BOOL CALLBACK EnumPickIconResourceProc(HMODULE hModule,
55 LPCWSTR lpszType,
56 LPWSTR lpszName,
57 LONG_PTR lParam
58 )
59 {
60 WCHAR szName[100];
61 int index;
62 HICON hIcon;
63 PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)lParam;
64
65 if (IS_INTRESOURCE(lpszName))
66 swprintf(szName, L"%u", lpszName);
67 else
68 wcscpy(szName, (WCHAR*)lpszName);
69
70
71 hIcon = LoadIconW(pIconContext->hLibrary, (LPCWSTR)lpszName);
72 if (hIcon == NULL)
73 return TRUE;
74
75 index = SendMessageW(pIconContext->hDlgCtrl, LB_ADDSTRING, 0, (LPARAM)szName);
76 if (index != LB_ERR)
77 SendMessageW(pIconContext->hDlgCtrl, LB_SETITEMDATA, index, (LPARAM)hIcon);
78
79 return TRUE;
80 }
81
82 void
83 DestroyIconList(HWND hDlgCtrl)
84 {
85 int count;
86 int index;
87
88 count = SendMessage(hDlgCtrl, LB_GETCOUNT, 0, 0);
89 if (count == LB_ERR)
90 return;
91
92 for(index = 0; index < count; index++)
93 {
94 HICON hIcon = (HICON)SendMessageW(hDlgCtrl, LB_GETITEMDATA, index, 0);
95 DestroyIcon(hIcon);
96 }
97 }
98
99 INT_PTR CALLBACK PickIconProc(HWND hwndDlg,
100 UINT uMsg,
101 WPARAM wParam,
102 LPARAM lParam
103 )
104 {
105 LPMEASUREITEMSTRUCT lpmis;
106 LPDRAWITEMSTRUCT lpdis;
107 HICON hIcon;
108 INT index, count;
109 WCHAR szText[MAX_PATH], szTitle[100], szFilter[100];
110 OPENFILENAMEW ofn = {0};
111
112 PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER);
113
114 switch(uMsg)
115 {
116 case WM_INITDIALOG:
117 pIconContext = (PPICK_ICON_CONTEXT)lParam;
118 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG)pIconContext);
119 pIconContext->hDlgCtrl = GetDlgItem(hwndDlg, IDC_PICKICON_LIST);
120 EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
121 if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
122 SendDlgItemMessageW(hwndDlg, IDC_EDIT_PATH, WM_SETTEXT, 0, (LPARAM)szText);
123 else
124 SendDlgItemMessageW(hwndDlg, IDC_EDIT_PATH, WM_SETTEXT, 0, (LPARAM)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 SendDlgItemMessageW(hwndDlg, IDC_EDIT_PATH, WM_GETTEXT, MAX_PATH, (LPARAM)pIconContext->szName);
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 SendDlgItemMessageW(hwndDlg, IDC_EDIT_PATH, WM_SETTEXT, 0, (LPARAM)szText);
183 else
184 SendDlgItemMessageW(hwndDlg, IDC_EDIT_PATH, WM_SETTEXT, 0, (LPARAM)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_DIALOG), 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 static const WCHAR resnameW[] = {'S','H','E','L','L','_','R','U','N','_','D','L','G',0};
277 RUNFILEDLGPARAMS rfdp;
278 HRSRC hRes;
279 LPVOID tmplate;
280 TRACE("\n");
281
282 rfdp.hwndOwner = hwndOwner;
283 rfdp.hIcon = hIcon;
284 rfdp.lpstrDirectory = lpstrDirectory;
285 rfdp.lpstrTitle = lpstrTitle;
286 rfdp.lpstrDescription = lpstrDescription;
287 rfdp.uFlags = uFlags;
288
289 if (!(hRes = FindResourceW(shell32_hInstance, resnameW, (LPWSTR)RT_DIALOG)) ||
290 !(tmplate = LoadResource(shell32_hInstance, hRes)))
291 {
292 ERR("Couldn't load SHELL_RUN_DLG resource\n");
293 ShellMessageBoxW(shell32_hInstance, hwndOwner, MAKEINTRESOURCEW(IDS_RUNDLG_ERROR), NULL, MB_OK | MB_ICONERROR);
294 return;
295 }
296
297 DialogBoxIndirectParamW(shell32_hInstance,
298 (LPCDLGTEMPLATEW)tmplate, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
299
300 }
301
302
303 /* find the directory that contains the file being run */
304 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
305 {
306 const WCHAR *src;
307 WCHAR *dest, *result, *result_end=NULL;
308 static const WCHAR dotexeW[] = {'.','e','x','e',0};
309
310 result = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(strlenW(cmdline)+5));
311
312 if (NULL == result)
313 {
314 TRACE("HeapAlloc couldn't allocate %d bytes\n", sizeof(WCHAR)*(strlenW(cmdline)+5));
315 return NULL;
316 }
317
318 src = cmdline;
319 dest = result;
320
321 if (*src == '"')
322 {
323 src++;
324 while (*src && *src != '"')
325 {
326 if (*src == '\\')
327 result_end = dest;
328 *dest++ = *src++;
329 }
330 }
331 else {
332 while (*src)
333 {
334 if (isspaceW(*src))
335 {
336 *dest = 0;
337 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
338 break;
339 strcatW(dest, dotexeW);
340 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
341 break;
342 }
343 else if (*src == '\\')
344 result_end = dest;
345 *dest++ = *src++;
346 }
347 }
348
349 if (result_end)
350 {
351 *result_end = 0;
352 return result;
353 }
354 else
355 {
356 HeapFree(GetProcessHeap(), 0, result);
357 return NULL;
358 }
359 }
360
361
362 /* Dialog procedure for RunFileDlg */
363 static INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
364 {
365 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
366
367 switch (message)
368 {
369 case WM_INITDIALOG :
370 prfdp = (RUNFILEDLGPARAMS *)lParam ;
371 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
372
373 if (prfdp->lpstrTitle)
374 SetWindowTextW(hwnd, prfdp->lpstrTitle);
375 if (prfdp->lpstrDescription)
376 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
377 if (prfdp->uFlags & RFF_NOBROWSE)
378 {
379 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
380 ShowWindow(browse, SW_HIDE);
381 EnableWindow(browse, FALSE);
382 }
383 if (prfdp->uFlags & RFF_NOLABEL)
384 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
385 if (prfdp->uFlags & RFF_CALCDIRECTORY)
386 FIXME("RFF_CALCDIRECTORY not supported\n");
387
388 if (prfdp->hIcon == NULL)
389 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
390 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
391 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
392 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
393
394 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0) ;
395 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH)) ;
396 return TRUE ;
397
398 case WM_COMMAND :
399 switch (LOWORD (wParam))
400 {
401 case IDOK :
402 {
403 int ic ;
404 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
405 if ((ic = GetWindowTextLengthW (htxt)))
406 {
407 WCHAR *psz, *parent=NULL ;
408 SHELLEXECUTEINFOW sei ;
409
410 ZeroMemory (&sei, sizeof(sei)) ;
411 sei.cbSize = sizeof(sei) ;
412 psz = (WCHAR *)HeapAlloc( GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR) );
413
414 if (psz)
415 {
416 GetWindowTextW (htxt, psz, ic + 1) ;
417
418 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
419 * WM_NOTIFY before execution */
420
421 sei.hwnd = hwnd;
422 sei.nShow = SW_SHOWNORMAL;
423 sei.lpFile = psz;
424
425 if (prfdp->lpstrDirectory)
426 sei.lpDirectory = prfdp->lpstrDirectory;
427 else
428 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
429
430 if (!ShellExecuteExW( &sei ))
431 {
432 HeapFree(GetProcessHeap(), 0, psz);
433 HeapFree(GetProcessHeap(), 0, parent);
434 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
435 return TRUE ;
436 }
437
438 /* FillList is still ANSI */
439 GetWindowTextA (htxt, (LPSTR)psz, ic + 1) ;
440 FillList (htxt, (LPSTR)psz, FALSE) ;
441
442 HeapFree(GetProcessHeap(), 0, psz);
443 HeapFree(GetProcessHeap(), 0, parent);
444 EndDialog (hwnd, 0);
445 }
446 }
447 }
448
449 case IDCANCEL :
450 EndDialog (hwnd, 0) ;
451 return TRUE ;
452
453 case IDC_RUNDLG_BROWSE :
454 {
455 HMODULE hComdlg = NULL ;
456 LPFNOFN ofnProc = NULL ;
457 static const WCHAR comdlg32W[] = {'c','o','m','d','l','g','3','2',0};
458 WCHAR szFName[1024] = {0};
459 WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
460 OPENFILENAMEW ofn;
461
462 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH);
463 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
464
465 ZeroMemory(&ofn, sizeof(ofn));
466 ofn.lStructSize = sizeof(OPENFILENAMEW);
467 ofn.hwndOwner = hwnd;
468 ofn.lpstrFilter = filter;
469 ofn.lpstrFile = szFName;
470 ofn.nMaxFile = 1023;
471 ofn.lpstrTitle = szCaption;
472 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
473 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
474
475 if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
476 NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
477 {
478 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
479 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
480 return TRUE ;
481 }
482
483 if (ofnProc(&ofn))
484 {
485 SetFocus (GetDlgItem (hwnd, IDOK)) ;
486 SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName) ;
487 SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
488 SetFocus (GetDlgItem (hwnd, IDOK)) ;
489 }
490
491 FreeLibrary (hComdlg) ;
492
493 return TRUE ;
494 }
495 }
496 return TRUE ;
497 }
498 return FALSE ;
499 }
500
501 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
502 /* fShowDefault ignored if pszLatest != NULL */
503 static void FillList (HWND hCb, char *pszLatest, BOOL fShowDefault)
504 {
505 HKEY hkey ;
506 /* char szDbgMsg[256] = "" ; */
507 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
508 DWORD icList = 0, icCmd = 0 ;
509 UINT Nix ;
510
511 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
512
513 if (ERROR_SUCCESS != RegCreateKeyExA (
514 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
515 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
516 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
517
518 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
519
520 if (icList > 0)
521 {
522 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
523
524 if (pszList)
525 {
526 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
527 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK);
528 }
529 else
530 {
531 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
532 }
533 }
534 else
535 {
536 icList = 1 ;
537 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
538 pszList[0] = 0 ;
539 }
540
541 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
542 {
543 if (pszList[Nix] > cMax)
544 cMax = pszList[Nix] ;
545
546 szIndex[0] = pszList[Nix] ;
547
548 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
549 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
550 if( pszCmd )
551 pszCmd = (char *)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
552 else
553 pszCmd = (char *)HeapAlloc(GetProcessHeap(), 0, icCmd) ;
554 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
555 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
556
557 if (NULL != pszLatest)
558 {
559 if (!lstrcmpiA(pszCmd, pszLatest))
560 {
561 /*
562 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
563 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
564 */
565 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
566 SetWindowTextA (hCb, pszCmd) ;
567 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
568
569 cMatch = pszList[Nix] ;
570 memmove (&pszList[1], pszList, Nix) ;
571 pszList[0] = cMatch ;
572 continue ;
573 }
574 }
575
576 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
577 {
578 /*
579 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
580 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
581 */
582 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
583 if (!Nix && fShowDefault)
584 {
585 SetWindowTextA (hCb, pszCmd) ;
586 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
587 }
588 }
589 else
590 {
591 /*
592 sprintf (szDbgMsg, "Doing loop thing.\n") ;
593 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
594 */
595 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
596 SetWindowTextA (hCb, pszLatest) ;
597 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
598
599 cMatch = pszList[Nix] ;
600 memmove (&pszList[1], pszList, Nix) ;
601 pszList[0] = cMatch ;
602 szIndex[0] = cMatch ;
603 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
604 }
605 }
606
607 if (!cMatch && NULL != pszLatest)
608 {
609 /*
610 sprintf (szDbgMsg, "Simply inserting (increasing list).\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 = ++cMax ;
618 if (pszList)
619 pszList = (char *)HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
620 else
621 pszList = (char *)HeapAlloc(GetProcessHeap(), 0, ++icList) ;
622
623 if (pszList)
624 {
625 memmove (&pszList[1], pszList, icList - 1) ;
626 pszList[0] = cMatch ;
627 szIndex[0] = cMatch ;
628 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
629 }
630 else
631 {
632 TRACE("HeapAlloc or HeapReAlloc failed to allocate enough bytes\n");
633 }
634 }
635
636 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
637
638 HeapFree( GetProcessHeap(), 0, pszCmd) ;
639 HeapFree( GetProcessHeap(), 0, pszList) ;
640 }
641
642
643 /*************************************************************************
644 * ConfirmDialog [internal]
645 *
646 * Put up a confirm box, return TRUE if the user confirmed
647 */
648 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
649 {
650 WCHAR Prompt[256];
651 WCHAR Title[256];
652
653 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
654 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
655 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
656 }
657
658
659 /*************************************************************************
660 * RestartDialogEx [SHELL32.730]
661 */
662
663 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
664 {
665 TRACE("(%p)\n", hWndOwner);
666
667 /* FIXME: use lpwstrReason */
668 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
669 {
670 HANDLE hToken;
671 TOKEN_PRIVILEGES npr;
672
673 /* enable the shutdown privilege for the current process */
674 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
675 {
676 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
677 npr.PrivilegeCount = 1;
678 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
679 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
680 CloseHandle(hToken);
681 }
682 ExitWindowsEx(EWX_REBOOT, uReason);
683 }
684
685 return 0;
686 }
687
688
689 /*************************************************************************
690 * LogoffWindowsDialog [SHELL32.54]
691 */
692
693 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
694 {
695 if (ConfirmDialog(hWndOwner, IDS_LOGOFF_PROMPT, IDS_LOGOFF_TITLE))
696 {
697 ExitWindowsEx(EWX_LOGOFF, 0);
698 }
699 return 0;}
700
701
702 /*************************************************************************
703 * RestartDialog [SHELL32.59]
704 */
705
706 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
707 {
708 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
709 }
710
711
712 /*************************************************************************
713 * ExitWindowsDialog [SHELL32.60]
714 *
715 * NOTES
716 * exported by ordinal
717 */
718 void WINAPI ExitWindowsDialog (HWND hWndOwner)
719 {
720 TRACE("(%p)\n", hWndOwner);
721
722 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
723 {
724 HANDLE hToken;
725 TOKEN_PRIVILEGES npr;
726
727 /* enable shutdown privilege for current process */
728 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
729 {
730 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
731 npr.PrivilegeCount = 1;
732 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
733 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
734 CloseHandle(hToken);
735 }
736 ExitWindowsEx(EWX_SHUTDOWN, 0);
737 }
738 }