[SHELL32]
[reactos.git] / reactos / dll / win32 / shell32 / dialogs.c
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 == -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 == 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 template;
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 !(template = 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 template, hwndOwner, RunDlgProc, (LPARAM)&rfdp);
299
300 }
301
302 /* find the directory that contains the file being run */
303 static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
304 {
305 const WCHAR *src;
306 WCHAR *dest, *result, *result_end=NULL;
307 static const WCHAR dotexeW[] = {'.','e','x','e',0};
308
309 result = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(strlenW(cmdline)+5));
310
311 src = cmdline;
312 dest = result;
313
314 if (*src == '"')
315 {
316 src++;
317 while (*src && *src != '"')
318 {
319 if (*src == '\\')
320 result_end = dest;
321 *dest++ = *src++;
322 }
323 }
324 else {
325 while (*src)
326 {
327 if (isspaceW(*src))
328 {
329 *dest = 0;
330 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
331 break;
332 strcatW(dest, dotexeW);
333 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(result))
334 break;
335 }
336 else if (*src == '\\')
337 result_end = dest;
338 *dest++ = *src++;
339 }
340 }
341
342 if (result_end)
343 {
344 *result_end = 0;
345 return result;
346 }
347 else
348 {
349 HeapFree(GetProcessHeap(), 0, result);
350 return NULL;
351 }
352 }
353
354 /* Dialog procedure for RunFileDlg */
355 static INT_PTR CALLBACK RunDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
356 {
357 RUNFILEDLGPARAMS *prfdp = (RUNFILEDLGPARAMS *)GetWindowLongPtrW(hwnd, DWLP_USER);
358
359 switch (message)
360 {
361 case WM_INITDIALOG :
362 prfdp = (RUNFILEDLGPARAMS *)lParam ;
363 SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
364
365 if (prfdp->lpstrTitle)
366 SetWindowTextW(hwnd, prfdp->lpstrTitle);
367 if (prfdp->lpstrDescription)
368 SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_DESCRIPTION), prfdp->lpstrDescription);
369 if (prfdp->uFlags & RFF_NOBROWSE)
370 {
371 HWND browse = GetDlgItem(hwnd, IDC_RUNDLG_BROWSE);
372 ShowWindow(browse, SW_HIDE);
373 EnableWindow(browse, FALSE);
374 }
375 if (prfdp->uFlags & RFF_NOLABEL)
376 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
377 if (prfdp->uFlags & RFF_CALCDIRECTORY)
378 FIXME("RFF_CALCDIRECTORY not supported\n");
379
380 if (prfdp->hIcon == NULL)
381 prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
382 SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
383 SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
384 SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
385
386 FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0) ;
387 SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH)) ;
388 return TRUE ;
389
390 case WM_COMMAND :
391 switch (LOWORD (wParam))
392 {
393 case IDOK :
394 {
395 int ic ;
396 HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
397 if ((ic = GetWindowTextLengthW (htxt)))
398 {
399 WCHAR *psz, *parent=NULL ;
400 SHELLEXECUTEINFOW sei ;
401
402 ZeroMemory (&sei, sizeof(sei)) ;
403 sei.cbSize = sizeof(sei) ;
404 psz = HeapAlloc( GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR) );
405 GetWindowTextW (htxt, psz, ic + 1) ;
406
407 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
408 * WM_NOTIFY before execution */
409
410 sei.hwnd = hwnd;
411 sei.nShow = SW_SHOWNORMAL;
412 sei.lpFile = psz;
413
414 if (prfdp->lpstrDirectory)
415 sei.lpDirectory = prfdp->lpstrDirectory;
416 else
417 sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
418
419 if (!ShellExecuteExW( &sei ))
420 {
421 HeapFree(GetProcessHeap(), 0, psz);
422 HeapFree(GetProcessHeap(), 0, parent);
423 SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
424 return TRUE ;
425 }
426
427 /* FillList is still ANSI */
428 GetWindowTextA (htxt, (LPSTR)psz, ic + 1) ;
429 FillList (htxt, (LPSTR)psz, FALSE) ;
430
431 HeapFree(GetProcessHeap(), 0, psz);
432 HeapFree(GetProcessHeap(), 0, parent);
433 EndDialog (hwnd, 0);
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[] = {'c','o','m','d','l','g','3','2',0};
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 = HeapAlloc( GetProcessHeap(), 0, icList) ;
511 if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
512 MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK) ;
513 }
514 else
515 {
516 icList = 1 ;
517 pszList = HeapAlloc( GetProcessHeap(), 0, icList) ;
518 pszList[0] = 0 ;
519 }
520
521 for (Nix = 0 ; Nix < icList - 1 ; Nix++)
522 {
523 if (pszList[Nix] > cMax)
524 cMax = pszList[Nix] ;
525
526 szIndex[0] = pszList[Nix] ;
527
528 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
529 MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
530 if( pszCmd )
531 pszCmd = HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
532 else
533 pszCmd = HeapAlloc(GetProcessHeap(), 0, icCmd) ;
534 if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
535 MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
536
537 if (NULL != pszLatest)
538 {
539 if (!lstrcmpiA(pszCmd, pszLatest))
540 {
541 /*
542 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
543 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
544 */
545 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
546 SetWindowTextA (hCb, pszCmd) ;
547 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
548
549 cMatch = pszList[Nix] ;
550 memmove (&pszList[1], pszList, Nix) ;
551 pszList[0] = cMatch ;
552 continue ;
553 }
554 }
555
556 if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
557 {
558 /*
559 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
560 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
561 */
562 SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
563 if (!Nix && fShowDefault)
564 {
565 SetWindowTextA (hCb, pszCmd) ;
566 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
567 }
568
569 }
570 else
571 {
572 /*
573 sprintf (szDbgMsg, "Doing loop thing.\n") ;
574 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
575 */
576 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
577 SetWindowTextA (hCb, pszLatest) ;
578 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
579
580 cMatch = pszList[Nix] ;
581 memmove (&pszList[1], pszList, Nix) ;
582 pszList[0] = cMatch ;
583 szIndex[0] = cMatch ;
584 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
585 }
586 }
587
588 if (!cMatch && NULL != pszLatest)
589 {
590 /*
591 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
592 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
593 */
594 SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
595 SetWindowTextA (hCb, pszLatest) ;
596 SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
597
598 cMatch = ++cMax ;
599 if( pszList )
600 pszList = HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
601 else
602 pszList = HeapAlloc(GetProcessHeap(), 0, ++icList) ;
603 memmove (&pszList[1], pszList, icList - 1) ;
604 pszList[0] = cMatch ;
605 szIndex[0] = cMatch ;
606 RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
607 }
608
609 RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
610
611 HeapFree( GetProcessHeap(), 0, pszCmd) ;
612 HeapFree( GetProcessHeap(), 0, pszList) ;
613 }
614
615
616 /*************************************************************************
617 * ConfirmDialog [internal]
618 *
619 * Put up a confirm box, return TRUE if the user confirmed
620 */
621 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
622 {
623 WCHAR Prompt[256];
624 WCHAR Title[256];
625
626 LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
627 LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
628 return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
629 }
630
631
632 /*************************************************************************
633 * RestartDialogEx [SHELL32.730]
634 */
635
636 int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, DWORD uReason)
637 {
638 TRACE("(%p)\n", hWndOwner);
639
640 /* FIXME: use lpwstrReason */
641 if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
642 {
643 HANDLE hToken;
644 TOKEN_PRIVILEGES npr;
645
646 /* enable the shutdown privilege for the current process */
647 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
648 {
649 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
650 npr.PrivilegeCount = 1;
651 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
652 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
653 CloseHandle(hToken);
654 }
655 ExitWindowsEx(EWX_REBOOT, uReason);
656 }
657
658 return 0;
659 }
660
661
662 /*************************************************************************
663 * LogoffWindowsDialog [SHELL32.54]
664 */
665
666 int WINAPI LogoffWindowsDialog(HWND hWndOwner)
667 {
668 if (ConfirmDialog(hWndOwner, IDS_LOGOFF_PROMPT, IDS_LOGOFF_TITLE))
669 {
670 ExitWindowsEx(EWX_LOGOFF, 0);
671 }
672 return 0;
673 }
674
675
676 /*************************************************************************
677 * RestartDialog [SHELL32.59]
678 */
679
680 int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
681 {
682 return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
683 }
684
685
686 /*************************************************************************
687 * ExitWindowsDialog [SHELL32.60]
688 *
689 * NOTES
690 * exported by ordinal
691 */
692 void WINAPI ExitWindowsDialog (HWND hWndOwner)
693 {
694 TRACE("(%p)\n", hWndOwner);
695
696 if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
697 {
698 HANDLE hToken;
699 TOKEN_PRIVILEGES npr;
700
701 /* enable shutdown privilege for current process */
702 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
703 {
704 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
705 npr.PrivilegeCount = 1;
706 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
707 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
708 CloseHandle(hToken);
709 }
710 ExitWindowsEx(EWX_SHUTDOWN, 0);
711 }
712 }