[SHELL32]
[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", (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
350 /* Dialog procedure for RunFileDlg */
351 static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
352 {
353 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
354
355 switch (message)
356 {
357 case WM_INITDIALOG:
358 prfdp = (RUNFILEDLGPARAMS *)lParam ;
359 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
360
361 if (prfdp->lpstrTitle)
362 SetWindowTextW(hwnd, prfdp->lpstrTitle);
363 if (prfdp->lpstrDescription)
364 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
365 if (prfdp->uFlags & RFF_NOBROWSE)
366 {
367 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
368 ShowWindow(browse, SW_HIDE);
369 EnableWindow(browse, FALSE);
370 }
371 if (prfdp->uFlags & RFF_NOLABEL)
372 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
373 if (prfdp->uFlags & RFF_CALCDIRECTORY)
374 FIXME("RFF_CALCDIRECTORY not supported\n");
375
376 if (prfdp->hIcon == NULL)
377 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
378 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
379 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
380 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
381
382 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0);
383 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH));
384 return TRUE;
385
386 case WM_COMMAND:
387 switch (LOWORD (wParam))
388 {
389 case IDOK:
390 {
391 int ic;
392 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
393 if ((ic = GetWindowTextLengthW (htxt)))
394 {
395 WCHAR *psz, *parent = NULL;
396 SHELLEXECUTEINFOW sei;
397
398 ZeroMemory (&sei, sizeof(sei));
399 sei.cbSize = sizeof(sei);
400 psz = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR));
401
402 if (psz)
403 {
404 GetWindowTextW(htxt, psz, ic + 1);
405
406 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
407 * WM_NOTIFY before execution */
408
409 sei.hwnd = hwnd;
410 sei.nShow = SW_SHOWNORMAL;
411 sei.lpFile = psz;
412
413 if (prfdp->lpstrDirectory)
414 sei.lpDirectory = prfdp->lpstrDirectory;
415 else
416 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
417
418 if (!ShellExecuteExW(&sei))
419 {
420 HeapFree(GetProcessHeap(), 0, psz);
421 HeapFree(GetProcessHeap(), 0, parent);
422 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
423 return TRUE;
424 }
425
426 /* FillList is still ANSI */
427 GetWindowTextA (htxt, (LPSTR)psz, ic + 1);
428 FillList (htxt, (LPSTR)psz, FALSE);
429
430 HeapFree(GetProcessHeap(), 0, psz);
431 HeapFree(GetProcessHeap(), 0, parent);
432 EndDialog (hwnd, 0);
433 }
434 }
435 }
436
437 case IDCANCEL:
438 EndDialog (hwnd, 0);
439 return TRUE;
440
441 case IDC_RUNDLG_BROWSE:
442 {
443 HMODULE hComdlg = NULL;
444 LPFNOFN ofnProc = NULL;
445 static const WCHAR comdlg32W[] = L"comdlg32";
446 WCHAR szFName[1024] = {0};
447 WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
448 OPENFILENAMEW ofn;
449
450 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH);
451 LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
452
453 ZeroMemory(&ofn, sizeof(ofn));
454 ofn.lStructSize = sizeof(OPENFILENAMEW);
455 ofn.hwndOwner = hwnd;
456 ofn.lpstrFilter = filter;
457 ofn.lpstrFile = szFName;
458 ofn.nMaxFile = 1023;
459 ofn.lpstrTitle = szCaption;
460 ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
461 ofn.lpstrInitialDir = prfdp->lpstrDirectory;
462
463 if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
464 NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
465 {
466 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
467 ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
468 return TRUE;
469 }
470
471 if (ofnProc(&ofn))
472 {
473 SetFocus (GetDlgItem (hwnd, IDOK));
474 SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName);
475 SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
476 SetFocus (GetDlgItem (hwnd, IDOK));
477 }
478
479 FreeLibrary (hComdlg);
480
481 return TRUE;
482 }
483 }
484 return TRUE;
485 }
486 return FALSE;
487 }
488
489 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
490 /* fShowDefault ignored if pszLatest != NULL */
491 static void FillList(HWND hCb, char *pszLatest, BOOL fShowDefault)
492 {
493 HKEY hkey ;
494 /* char szDbgMsg[256] = "" ; */
495 char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
496 DWORD icList = 0, icCmd = 0 ;
497 UINT Nix ;
498
499 SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
500
501 if (ERROR_SUCCESS != RegCreateKeyExA (
502 HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
503 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
504 MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
505
506 RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
507
508 if (icList > 0)
509 {
510 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
511
512 if (pszList)
513 {
514 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
515 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK);
516 }
517 else
518 {
519 TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
520 }
521 }
522 else
523 {
524 icList = 1 ;
525 pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
526 pszList[0] = 0 ;
527 }
528
529 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
530 {
531 if (pszList[Nix] > cMax)
532 cMax = pszList[Nix] ;
533
534 szIndex[0] = pszList[Nix] ;
535
536 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
537 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
538 if( pszCmd )
539 pszCmd = (char *)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
540 else
541 pszCmd = (char *)HeapAlloc(GetProcessHeap(), 0, icCmd) ;
542 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
543 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
544
545 if (NULL != pszLatest)
546 {
547 if (!lstrcmpiA(pszCmd, pszLatest))
548 {
549 /*
550 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
551 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
552 */
553 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
554 SetWindowTextA (hCb, pszCmd) ;
555 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
556
557 cMatch = pszList[Nix] ;
558 memmove (&pszList[1], pszList, Nix) ;
559 pszList[0] = cMatch ;
560 continue ;
561 }
562 }
563
564 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
565 {
566 /*
567 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
568 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
569 */
570 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
571 if (!Nix && fShowDefault)
572 {
573 SetWindowTextA (hCb, pszCmd) ;
574 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
575 }
576 }
577 else
578 {
579 /*
580 sprintf (szDbgMsg, "Doing loop thing.\n") ;
581 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
582 */
583 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
584 SetWindowTextA (hCb, pszLatest) ;
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 szIndex[0] = cMatch ;
591 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
592 }
593 }
594
595 if (!cMatch && NULL != pszLatest)
596 {
597 /*
598 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
599 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
600 */
601 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
602 SetWindowTextA (hCb, pszLatest) ;
603 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
604
605 cMatch = ++cMax ;
606 if (pszList)
607 pszList = (char *)HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
608 else
609 pszList = (char *)HeapAlloc(GetProcessHeap(), 0, ++icList) ;
610
611 if (pszList)
612 {
613 memmove (&pszList[1], pszList, icList - 1) ;
614 pszList[0] = cMatch ;
615 szIndex[0] = cMatch ;
616 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
617 }
618 else
619 {
620 TRACE("HeapAlloc or HeapReAlloc failed to allocate enough bytes\n");
621 }
622 }
623
624 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
625
626 HeapFree( GetProcessHeap(), 0, pszCmd) ;
627 HeapFree( GetProcessHeap(), 0, pszList) ;
628 }
629
630
631 /*************************************************************************
632 * ConfirmDialog [internal]
633 *
634 * Put up a confirm box, return TRUE if the user confirmed
635 */
636 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
637 {
638 WCHAR Prompt[256];
639 WCHAR Title[256];
640
641 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
642 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
643 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
644 }
645
646
647 /*************************************************************************
648 * RestartDialogEx [SHELL32.730]
649 */
650
651 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
652 {
653 TRACE("(%p)\n", hWndOwner);
654
655 /* FIXME: use lpwstrReason */
656 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
657 {
658 HANDLE hToken;
659 TOKEN_PRIVILEGES npr;
660
661 /* enable the shutdown privilege for the current process */
662 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
663 {
664 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
665 npr.PrivilegeCount = 1;
666 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
667 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
668 CloseHandle(hToken);
669 }
670 ExitWindowsEx(EWX_REBOOT, uReason);
671 }
672
673 return 0;
674 }
675
676
677 /*************************************************************************
678 * LogoffWindowsDialog [SHELL32.54]
679 */
680
681 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
682 {
683 if (ConfirmDialog(hWndOwner, IDS_LOGOFF_PROMPT, IDS_LOGOFF_TITLE))
684 ExitWindowsEx(EWX_LOGOFF, 0);
685
686 return 0;
687 }
688
689
690 /*************************************************************************
691 * RestartDialog [SHELL32.59]
692 */
693
694 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
695 {
696 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
697 }
698
699 /*************************************************************************
700 * Used to get the shutdown privilege
701 */
702 VOID ExitWindows_GetShutdownPrivilege(VOID)
703 {
704 HANDLE hToken;
705 TOKEN_PRIVILEGES npr;
706
707 /* enable shut down privilege for current process */
708 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
709 {
710 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
711
712 npr.PrivilegeCount = 1;
713 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
714 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
715
716 CloseHandle(hToken);
717 }
718 }
719
720 /*************************************************************************
721 * ExitWindowsDialog_backup
722 *
723 * NOTES
724 * used as a backup solution to shutdown the OS in case msgina.dll somehow
725 * cannot be found.
726 */
727 VOID ExitWindowsDialog_backup(HWND hWndOwner)
728 {
729 TRACE("(%p)\n", hWndOwner);
730
731 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
732 {
733 ExitWindows_GetShutdownPrivilege();
734 ExitWindowsEx(EWX_SHUTDOWN, 0);
735 }
736 }
737
738 /*************************************************************************
739 * ExitWindowsDialog [SHELL32.60]
740 *
741 * NOTES
742 * exported by ordinal
743 */
744 /*
745 * TODO:
746 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
747 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
748 */
749 void WINAPI ExitWindowsDialog(HWND hWndOwner)
750 {
751 typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff);
752 HINSTANCE msginaDll = LoadLibraryA("msgina.dll");
753
754 TRACE("(%p)\n", hWndOwner);
755
756 /* If the DLL cannot be found for any reason, then it simply uses a
757 dialog box to ask if the user wants to shut down the computer. */
758 if(!msginaDll)
759 {
760 TRACE("Unable to load msgina.dll.\n");
761 ExitWindowsDialog_backup(hWndOwner);
762 return;
763 }
764
765 ShellShFunc pShellShutdownDialog = (ShellShFunc) GetProcAddress(msginaDll, "ShellShutdownDialog");
766
767 if(pShellShutdownDialog)
768 {
769 /* Actually call the function */
770 DWORD returnValue = pShellShutdownDialog(hWndOwner, NULL, FALSE);
771
772 switch(returnValue)
773 {
774 case 0x01: /* Log off user */
775 {
776 ExitWindowsEx(EWX_LOGOFF, 0);
777 break;
778 }
779 case 0x02: /* Shut down */
780 {
781 ExitWindows_GetShutdownPrivilege();
782 ExitWindowsEx(EWX_SHUTDOWN, 0);
783 break;
784 }
785 case 0x03: /* Install Updates/Shutdown (?) */
786 {
787 break;
788 }
789 case 0x04: /* Reboot */
790 {
791 ExitWindows_GetShutdownPrivilege();
792 ExitWindowsEx(EWX_REBOOT, 0);
793 break;
794 }
795 case 0x10: /* Sleep */
796 {
797 if(IsPwrSuspendAllowed())
798 {
799 ExitWindows_GetShutdownPrivilege();
800 SetSuspendState(FALSE, FALSE, FALSE);
801 }
802 break;
803 }
804 case 0x40: /* Hibernate */
805 {
806 if(IsPwrHibernateAllowed())
807 {
808 ExitWindows_GetShutdownPrivilege();
809 SetSuspendState(TRUE, FALSE, TRUE);
810 }
811 break;
812 }
813 /* If the option is any other value */
814 default:
815 break;
816 }
817 }
818 else
819 {
820 /* If the function cannot be found, then revert to using the backup solution */
821 TRACE("Unable to find the 'ShellShutdownDialog' function");
822 FreeLibrary(msginaDll);
823 ExitWindowsDialog_backup(hWndOwner);
824 }
825 }