d47832ee1248a0eeebb2239f44e3bdcbb3bded82
[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 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", 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 EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
120 if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
121 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
122 else
123 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
124
125 count = SendMessage(pIconContext->hDlgCtrl, LB_GETCOUNT, 0, 0);
126 if (count != LB_ERR)
127 {
128 if (count > pIconContext->Index)
129 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, pIconContext->Index, 0);
130 else
131 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
132 }
133 return TRUE;
134 case WM_COMMAND:
135 switch(LOWORD(wParam))
136 {
137 case IDOK:
138 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
139 pIconContext->Index = index;
140 GetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName, MAX_PATH);
141 DestroyIconList(pIconContext->hDlgCtrl);
142 EndDialog(hwndDlg, 1);
143 break;
144 case IDCANCEL:
145 DestroyIconList(pIconContext->hDlgCtrl);
146 EndDialog(hwndDlg, 0);
147 break;
148 case IDC_PICKICON_LIST:
149 if (HIWORD(wParam) == LBN_SELCHANGE)
150 InvalidateRect((HWND)lParam, NULL, TRUE); // FIXME USE UPDATE RECT
151 break;
152 case IDC_BUTTON_PATH:
153 szText[0] = 0;
154 szTitle[0] = 0;
155 szFilter[0] = 0;
156 ofn.lStructSize = sizeof(ofn);
157 ofn.hwndOwner = hwndDlg;
158 ofn.lpstrFile = szText;
159 ofn.nMaxFile = MAX_PATH;
160 LoadStringW(shell32_hInstance, IDS_PICK_ICON_TITLE, szTitle, sizeof(szTitle) / sizeof(WCHAR));
161 ofn.lpstrTitle = szTitle;
162 LoadStringW(shell32_hInstance, IDS_PICK_ICON_FILTER, szFilter, sizeof(szFilter) / sizeof(WCHAR));
163 ofn.lpstrFilter = szFilter;
164 if (GetOpenFileNameW(&ofn))
165 {
166 HMODULE hLibrary;
167
168 if (!wcsicmp(pIconContext->szName, szText))
169 break;
170
171 DestroyIconList(pIconContext->hDlgCtrl);
172
173 hLibrary = LoadLibraryExW(szText, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
174 if (hLibrary == NULL)
175 break;
176 FreeLibrary(pIconContext->hLibrary);
177 pIconContext->hLibrary = hLibrary;
178 wcscpy(pIconContext->szName, szText);
179 EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
180 if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
181 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
182 else
183 SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
184
185 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
186 }
187 break;
188 }
189 break;
190 case WM_MEASUREITEM:
191 lpmis = (LPMEASUREITEMSTRUCT) lParam;
192 lpmis->itemHeight = 32;
193 lpmis->itemWidth = 64;
194 return TRUE;
195 case WM_DRAWITEM:
196 lpdis = (LPDRAWITEMSTRUCT) lParam;
197 if (lpdis->itemID == (UINT)-1)
198 {
199 break;
200 }
201 switch (lpdis->itemAction)
202 {
203 case ODA_SELECT:
204 case ODA_DRAWENTIRE:
205 index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
206 hIcon =(HICON)SendMessage(lpdis->hwndItem, LB_GETITEMDATA, lpdis->itemID, (LPARAM) 0);
207
208 if (lpdis->itemID == (UINT)index)
209 {
210 HBRUSH hBrush;
211 hBrush = CreateSolidBrush(RGB(0, 0, 255));
212 FillRect(lpdis->hDC, &lpdis->rcItem, hBrush);
213 DeleteObject(hBrush);
214 }
215 else
216 {
217 HBRUSH hBrush;
218 hBrush = CreateSolidBrush(RGB(255, 255, 255));
219 FillRect(lpdis->hDC, &lpdis->rcItem, hBrush);
220 DeleteObject(hBrush);
221 }
222 DrawIconEx(lpdis->hDC, lpdis->rcItem.left,lpdis->rcItem.top, hIcon,
223 0,
224 0,
225 0,
226 NULL,
227 DI_NORMAL);
228 break;
229 }
230 break;
231 }
232
233 return FALSE;
234 }
235
236 BOOL WINAPI PickIconDlg(
237 HWND hwndOwner,
238 LPWSTR lpstrFile,
239 UINT nMaxFile,
240 INT* lpdwIconIndex)
241 {
242 HMODULE hLibrary;
243 int res;
244 PICK_ICON_CONTEXT IconContext;
245
246 hLibrary = LoadLibraryExW(lpstrFile, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
247 IconContext.hLibrary = hLibrary;
248 IconContext.Index = *lpdwIconIndex;
249 wcscpy(IconContext.szName, lpstrFile);
250
251 res = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_PICK_ICON), hwndOwner, PickIconProc, (LPARAM)&IconContext);
252 if (res)
253 {
254 wcscpy(lpstrFile, IconContext.szName);
255 *lpdwIconIndex = IconContext.Index;
256 }
257
258 FreeLibrary(hLibrary);
259 return res;
260 }
261
262 /*************************************************************************
263 * RunFileDlg [internal]
264 *
265 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
266 */
267 void WINAPI RunFileDlg(
268 HWND hwndOwner,
269 HICON hIcon,
270 LPCWSTR lpstrDirectory,
271 LPCWSTR lpstrTitle,
272 LPCWSTR lpstrDescription,
273 UINT uFlags)
274 {
275 TRACE("\n");
276
277 RUNFILEDLGPARAMS rfdp;
278 rfdp.hwndOwner = hwndOwner;
279 rfdp.hIcon = hIcon;
280 rfdp.lpstrDirectory = lpstrDirectory;
281 rfdp.lpstrTitle = lpstrTitle;
282 rfdp.lpstrDescription = lpstrDescription;
283 rfdp.uFlags = uFlags;
284
285 DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_RUN), hwndOwner, RunDlgProc, (LPARAM)&rfdp);
286
287 }
288
289
290 /* find the directory that contains the file being run */
291 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
292 {
293 const WCHAR *src;
294 WCHAR *dest, *result, *result_end=NULL;
295 static const WCHAR dotexeW[] = L".exe";
296
297 result = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(strlenW(cmdline)+5));
298
299 if (NULL == result)
300 {
301 TRACE("HeapAlloc couldn't allocate %d bytes\n", sizeof(WCHAR)*(strlenW(cmdline)+5));
302 return NULL;
303 }
304
305 src = cmdline;
306 dest = result;
307
308 if (*src == '"')
309 {
310 src++;
311 while (*src && *src != '"')
312 {
313 if (*src == '\\')
314 result_end = dest;
315 *dest++ = *src++;
316 }
317 }
318 else {
319 while (*src)
320 {
321 if (isspaceW(*src))
322 {
323 *dest = 0;
324 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
325 break;
326 strcatW(dest, dotexeW);
327 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
328 break;
329 }
330 else if (*src == '\\')
331 result_end = dest;
332 *dest++ = *src++;
333 }
334 }
335
336 if (result_end)
337 {
338 *result_end = 0;
339 return result;
340 }
341 else
342 {
343 HeapFree(GetProcessHeap(), 0, result);
344 return NULL;
345 }
346 }
347
348
349 /* Dialog procedure for RunFileDlg */
350 static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
351 {
352 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
353
354 switch (message)
355 {
356 case WM_INITDIALOG:
357 prfdp = (RUNFILEDLGPARAMS *)lParam ;
358 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
359
360 if (prfdp->lpstrTitle)
361 SetWindowTextW(hwnd, prfdp->lpstrTitle);
362 if (prfdp->lpstrDescription)
363 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
364 if (prfdp->uFlags & RFF_NOBROWSE)
365 {
366 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
367 ShowWindow(browse, SW_HIDE);
368 EnableWindow(browse, FALSE);
369 }
370 if (prfdp->uFlags & RFF_NOLABEL)
371 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
372 if (prfdp->uFlags & RFF_CALCDIRECTORY)
373 FIXME("RFF_CALCDIRECTORY not supported\n");
374
375 if (prfdp->hIcon == NULL)
376 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
377 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
378 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
379 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
380
381 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0);
382 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH));
383 return TRUE;
384
385 case WM_COMMAND:
386 switch (LOWORD (wParam))
387 {
388 case IDOK:
389 {
390 int ic;
391 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
392 if ((ic = GetWindowTextLengthW (htxt)))
393 {
394 WCHAR *psz, *parent = NULL;
395 SHELLEXECUTEINFOW sei;
396
397 ZeroMemory (&sei, sizeof(sei));
398 sei.cbSize = sizeof(sei);
399 psz = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR));
400
401 if (psz)
402 {
403 GetWindowTextW(htxt, psz, ic + 1);
404
405 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
406 * WM_NOTIFY before execution */
407
408 sei.hwnd = hwnd;
409 sei.nShow = SW_SHOWNORMAL;
410 sei.lpFile = psz;
411
412 if (prfdp->lpstrDirectory)
413 sei.lpDirectory = prfdp->lpstrDirectory;
414 else
415 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
416
417 if (!ShellExecuteExW(&sei))
418 {
419 HeapFree(GetProcessHeap(), 0, psz);
420 HeapFree(GetProcessHeap(), 0, parent);
421 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
422 return TRUE;
423 }
424
425 /* FillList is still ANSI */
426 GetWindowTextA (htxt, (LPSTR)psz, ic + 1);
427 FillList (htxt, (LPSTR)psz, FALSE);
428
429 HeapFree(GetProcessHeap(), 0, psz);
430 HeapFree(GetProcessHeap(), 0, parent);
431 EndDialog (hwnd, 0);
432 }
433 }
434 }
435
436 case IDCANCEL:
437 EndDialog (hwnd, 0);
438 return TRUE;
439
440 case IDC_RUNDLG_BROWSE:
441 {
442 HMODULE hComdlg = NULL;
443 LPFNOFN ofnProc = NULL;
444 static const WCHAR comdlg32W[] = L"comdlg32";
445 WCHAR szFName[1024] = {0};
446 WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
447 OPENFILENAMEW ofn;
448
449 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH);
450 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
451
452 ZeroMemory(&ofn, sizeof(ofn));
453 ofn.lStructSize = sizeof(OPENFILENAMEW);
454 ofn.hwndOwner = hwnd;
455 ofn.lpstrFilter = filter;
456 ofn.lpstrFile = szFName;
457 ofn.nMaxFile = 1023;
458 ofn.lpstrTitle = szCaption;
459 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
460 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
461
462 if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
463 NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
464 {
465 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
466 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
467 return TRUE;
468 }
469
470 if (ofnProc(&ofn))
471 {
472 SetFocus (GetDlgItem (hwnd, IDOK));
473 SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName);
474 SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
475 SetFocus (GetDlgItem (hwnd, IDOK));
476 }
477
478 FreeLibrary (hComdlg);
479
480 return TRUE;
481 }
482 }
483 return TRUE;
484 }
485 return FALSE;
486 }
487
488 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
489 /* fShowDefault ignored if pszLatest != NULL */
490 static void FillList(HWND hCb, char *pszLatest, BOOL fShowDefault)
491 {
492 HKEY hkey ;
493 /* char szDbgMsg[256] = "" ; */
494 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
495 DWORD icList = 0, icCmd = 0 ;
496 UINT Nix ;
497
498 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
499
500 if (ERROR_SUCCESS != RegCreateKeyExA (
501 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
502 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
503 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
504
505 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
506
507 if (icList > 0)
508 {
509 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
510
511 if (pszList)
512 {
513 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
514 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK);
515 }
516 else
517 {
518 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
519 }
520 }
521 else
522 {
523 icList = 1 ;
524 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
525 pszList[0] = 0 ;
526 }
527
528 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
529 {
530 if (pszList[Nix] > cMax)
531 cMax = pszList[Nix] ;
532
533 szIndex[0] = pszList[Nix] ;
534
535 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
536 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
537 if( pszCmd )
538 pszCmd = (char *)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
539 else
540 pszCmd = (char *)HeapAlloc(GetProcessHeap(), 0, icCmd) ;
541 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
542 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
543
544 if (NULL != pszLatest)
545 {
546 if (!lstrcmpiA(pszCmd, pszLatest))
547 {
548 /*
549 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
550 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
551 */
552 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
553 SetWindowTextA (hCb, pszCmd) ;
554 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
555
556 cMatch = pszList[Nix] ;
557 memmove (&pszList[1], pszList, Nix) ;
558 pszList[0] = cMatch ;
559 continue ;
560 }
561 }
562
563 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
564 {
565 /*
566 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
567 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
568 */
569 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
570 if (!Nix && fShowDefault)
571 {
572 SetWindowTextA (hCb, pszCmd) ;
573 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
574 }
575 }
576 else
577 {
578 /*
579 sprintf (szDbgMsg, "Doing loop thing.\n") ;
580 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
581 */
582 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
583 SetWindowTextA (hCb, pszLatest) ;
584 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
585
586 cMatch = pszList[Nix] ;
587 memmove (&pszList[1], pszList, Nix) ;
588 pszList[0] = cMatch ;
589 szIndex[0] = cMatch ;
590 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
591 }
592 }
593
594 if (!cMatch && NULL != pszLatest)
595 {
596 /*
597 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
598 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
599 */
600 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
601 SetWindowTextA (hCb, pszLatest) ;
602 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
603
604 cMatch = ++cMax ;
605 if (pszList)
606 pszList = (char *)HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
607 else
608 pszList = (char *)HeapAlloc(GetProcessHeap(), 0, ++icList) ;
609
610 if (pszList)
611 {
612 memmove (&pszList[1], pszList, icList - 1) ;
613 pszList[0] = cMatch ;
614 szIndex[0] = cMatch ;
615 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
616 }
617 else
618 {
619 TRACE("HeapAlloc or HeapReAlloc failed to allocate enough bytes\n");
620 }
621 }
622
623 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
624
625 HeapFree( GetProcessHeap(), 0, pszCmd) ;
626 HeapFree( GetProcessHeap(), 0, pszList) ;
627 }
628
629
630 /*************************************************************************
631 * ConfirmDialog [internal]
632 *
633 * Put up a confirm box, return TRUE if the user confirmed
634 */
635 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
636 {
637 WCHAR Prompt[256];
638 WCHAR Title[256];
639
640 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
641 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
642 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
643 }
644
645
646 /*************************************************************************
647 * RestartDialogEx [SHELL32.730]
648 */
649
650 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
651 {
652 TRACE("(%p)\n", hWndOwner);
653
654 /* FIXME: use lpwstrReason */
655 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
656 {
657 HANDLE hToken;
658 TOKEN_PRIVILEGES npr;
659
660 /* enable the shutdown privilege for the current process */
661 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
662 {
663 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
664 npr.PrivilegeCount = 1;
665 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
666 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
667 CloseHandle(hToken);
668 }
669 ExitWindowsEx(EWX_REBOOT, uReason);
670 }
671
672 return 0;
673 }
674
675
676 /*************************************************************************
677 * LogoffWindowsDialog [SHELL32.54]
678 */
679
680 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
681 {
682 if (ConfirmDialog(hWndOwner, IDS_LOGOFF_PROMPT, IDS_LOGOFF_TITLE))
683 ExitWindowsEx(EWX_LOGOFF, 0);
684
685 return 0;
686 }
687
688
689 /*************************************************************************
690 * RestartDialog [SHELL32.59]
691 */
692
693 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
694 {
695 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
696 }
697
698
699 /*************************************************************************
700 * ExitWindowsDialog [SHELL32.60]
701 *
702 * NOTES
703 * exported by ordinal
704 */
705 void WINAPI ExitWindowsDialog(HWND hWndOwner)
706 {
707 TRACE("(%p)\n", hWndOwner);
708
709 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
710 {
711 HANDLE hToken;
712 TOKEN_PRIVILEGES npr;
713
714 /* enable shutdown privilege for current process */
715 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
716 {
717 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
718 npr.PrivilegeCount = 1;
719 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
720 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
721 CloseHandle(hToken);
722 }
723 ExitWindowsEx(EWX_SHUTDOWN, 0);
724 }
725 }