Synchronize with trunk r58457.
[reactos.git] / dll / win32 / shlwapi / msgbox.c
1 /*
2 * SHLWAPI message box functions
3 *
4 * Copyright 2004 Jon Griffiths
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 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <config.h>
26 //#include "wine/port.h"
27
28 #include <stdarg.h>
29 //#include <string.h>
30
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33 #include <windef.h>
34 #include <winbase.h>
35 //#include "winuser.h"
36 #include <winreg.h>
37 #include <shlwapi.h>
38 #include <wine/unicode.h>
39 #include <wine/debug.h>
40
41 #include "resource.h"
42
43
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45
46 extern HINSTANCE shlwapi_hInstance; /* in shlwapi_main.c */
47
48 static const WCHAR szDontShowKey[] = { 'S','o','f','t','w','a','r','e','\\',
49 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
50 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
51 'E','x','p','l','o','r','e','r','\\','D','o','n','t','S','h','o','w',
52 'M','e','T','h','i','s','D','i','a','l','o','g','A','g','a','i','n','\0'
53 };
54
55 INT_PTR WINAPI SHMessageBoxCheckExW(HWND,HINSTANCE,LPCWSTR,DLGPROC,LPARAM,INT_PTR,LPCWSTR);
56 INT_PTR WINAPI SHMessageBoxCheckW(HWND,LPCWSTR,LPCWSTR,DWORD,INT_PTR,LPCWSTR);
57
58 /* Data held by each general message boxes */
59 typedef struct tagDLGDATAEX
60 {
61 DLGPROC dlgProc; /* User supplied DlgProc */
62 LPARAM lParam; /* User supplied LPARAM for dlgProc */
63 LPCWSTR lpszId; /* Name of reg key holding whether to skip */
64 } DLGDATAEX;
65
66 /* Dialogue procedure for general message boxes */
67 static INT_PTR CALLBACK SHDlgProcEx(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
68 {
69 DLGDATAEX *d = (DLGDATAEX *)GetWindowLongPtrW(hDlg, DWLP_USER);
70
71 TRACE("(%p,%u,%ld,%ld) data %p\n", hDlg, uMsg, wParam, lParam, d);
72
73 switch (uMsg)
74 {
75 case WM_INITDIALOG:
76 {
77 /* FIXME: Not sure where native stores its lParam */
78 SetWindowLongPtrW(hDlg, DWLP_USER, lParam);
79 d = (DLGDATAEX *)lParam;
80 TRACE("WM_INITDIALOG: %p, %s,%p,%p\n", hDlg, debugstr_w(d->lpszId),
81 d->dlgProc, (void*)d->lParam);
82 if (d->dlgProc)
83 return d->dlgProc(hDlg, uMsg, wParam, d->lParam);
84 return TRUE;
85 }
86
87 case WM_COMMAND:
88 switch (LOWORD(wParam))
89 {
90 case IDYES:
91 wParam = MAKELONG(IDOK, HIWORD(wParam));
92 /* Fall through ... */
93 case IDNO:
94 if (LOWORD(wParam) == IDNO)
95 wParam = MAKELONG(IDCANCEL, HIWORD(wParam));
96 /* Fall through ... */
97 case IDOK:
98 case IDCANCEL:
99
100 TRACE("WM_COMMAND: id=%s data=%p\n",
101 LOWORD(wParam) == IDOK ? "IDOK" : "IDCANCEL", d);
102
103 if (SendMessageW(GetDlgItem(hDlg, IDC_ERR_DONT_SHOW), BM_GETCHECK, 0L, 0L))
104 {
105 DWORD dwZero = 0;
106
107 /* The user clicked 'don't show again', so set the key */
108 SHRegSetUSValueW(szDontShowKey, d->lpszId, REG_DWORD, &dwZero,
109 sizeof(dwZero), SHREGSET_DEFAULT);
110 }
111 if (!d->dlgProc || !d->dlgProc(hDlg, uMsg, wParam, lParam))
112 EndDialog(hDlg, wParam);
113 return TRUE;
114 }
115 break;
116
117 default:
118 break;
119 }
120
121 if (d && d->dlgProc)
122 return d->dlgProc(hDlg, uMsg, wParam, lParam);
123 return FALSE;
124 }
125
126 /*************************************************************************
127 * @ [SHLWAPI.291]
128 *
129 * Pop up a 'Don't show this message again' dialogue box.
130 *
131 * PARAMS
132 * hWnd [I] Window to be the dialogues' parent
133 * hInst [I] Instance of the module holding the dialogue resource
134 * lpszName [I] Resource Id of the dialogue resource
135 * dlgProc [I] Dialog procedure, or NULL for default handling
136 * lParam [I] LPARAM to pass to dlgProc
137 * iRet [I] Value to return if dialogue is not shown
138 * lpszId [I] Name of registry subkey which determines whether to show the dialog
139 *
140 * RETURNS
141 * Success: The value returned from the dialogue procedure.
142 * Failure: iRet, if the dialogue resource could not be loaded or the dialogue
143 * should not be shown.
144 *
145 * NOTES
146 * Both lpszName and lpszId must be less than MAX_PATH in length.
147 */
148 INT_PTR WINAPI SHMessageBoxCheckExA(HWND hWnd, HINSTANCE hInst, LPCSTR lpszName,
149 DLGPROC dlgProc, LPARAM lParam, INT_PTR iRet,
150 LPCSTR lpszId)
151 {
152 WCHAR szNameBuff[MAX_PATH], szIdBuff[MAX_PATH];
153 LPCWSTR szName = szNameBuff;
154
155 if (IS_INTRESOURCE(lpszName))
156 szName = (LPCWSTR)lpszName; /* Resource Id or NULL */
157 else
158 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, szNameBuff, MAX_PATH);
159
160 MultiByteToWideChar(CP_ACP, 0, lpszId, -1, szIdBuff, MAX_PATH);
161
162 return SHMessageBoxCheckExW(hWnd, hInst, szName, dlgProc, lParam, iRet, szIdBuff);
163 }
164
165 /*************************************************************************
166 * @ [SHLWAPI.292]
167 *
168 * Unicode version of SHMessageBoxCheckExW.
169 */
170 INT_PTR WINAPI SHMessageBoxCheckExW(HWND hWnd, HINSTANCE hInst, LPCWSTR lpszName,
171 DLGPROC dlgProc, LPARAM lParam, INT_PTR iRet, LPCWSTR lpszId)
172 {
173 DLGDATAEX d;
174
175 if (!SHRegGetBoolUSValueW(szDontShowKey, lpszId, FALSE, TRUE))
176 return iRet;
177
178 d.dlgProc = dlgProc;
179 d.lParam = lParam;
180 d.lpszId = lpszId;
181 return DialogBoxParamW(hInst, lpszName, hWnd, SHDlgProcEx, (LPARAM)&d);
182 }
183
184 /* Data held by each shlwapi message box */
185 typedef struct tagDLGDATA
186 {
187 LPCWSTR lpszTitle; /* User supplied message title */
188 LPCWSTR lpszText; /* User supplied message text */
189 DWORD dwType; /* Message box type */
190 } DLGDATA;
191
192 /* Dialogue procedure for shlwapi message boxes */
193 static INT_PTR CALLBACK SHDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
194 {
195 TRACE("(%p,%u,%ld,%ld)\n", hDlg, uMsg, wParam, lParam);
196
197 switch (uMsg)
198 {
199 case WM_INITDIALOG:
200 {
201 DLGDATA *d = (DLGDATA *)lParam;
202 TRACE("WM_INITDIALOG: %p, %s,%s,%d\n", hDlg, debugstr_w(d->lpszTitle),
203 debugstr_w(d->lpszText), d->dwType);
204
205 SetWindowTextW(hDlg, d->lpszTitle);
206 SetWindowTextW(GetDlgItem(hDlg, IDS_ERR_USER_MSG), d->lpszText);
207
208 /* Set buttons according to dwType */
209 switch (d->dwType)
210 {
211 case 0:
212 ShowWindow(GetDlgItem(hDlg, IDCANCEL), SW_HIDE);
213 /* FIXME: Move OK button to position of the Cancel button (cosmetic) */
214 case 1:
215 ShowWindow(GetDlgItem(hDlg, IDYES), SW_HIDE);
216 ShowWindow(GetDlgItem(hDlg, IDNO), SW_HIDE);
217 break;
218 default:
219 ShowWindow(GetDlgItem(hDlg, IDOK), SW_HIDE);
220 ShowWindow(GetDlgItem(hDlg, IDCANCEL), SW_HIDE);
221 break;
222 }
223 return TRUE;
224 }
225 default:
226 break;
227 }
228 return FALSE;
229 }
230
231 /*************************************************************************
232 * @ [SHLWAPI.185]
233 *
234 * Pop up a 'Don't show this message again' dialogue box.
235 *
236 * PARAMS
237 * hWnd [I] Window to be the dialogues' parent
238 * lpszText [I] Text of the message to show
239 * lpszTitle [I] Title of the dialogue box
240 * dwType [I] Type of dialogue buttons (See below)
241 * iRet [I] Value to return if dialogue is not shown
242 * lpszId [I] Name of registry subkey which determines whether to show the dialog
243 *
244 * RETURNS
245 * Success: The value returned from the dialogue procedure (e.g. IDOK).
246 * Failure: iRet, if the default dialogue resource could not be loaded or the
247 * dialogue should not be shown.
248 *
249 * NOTES
250 * - Both lpszTitle and lpszId must be less than MAX_PATH in length.
251 * - Possible values for dwType are:
252 *| Value Buttons
253 *| ----- -------
254 *| 0 OK
255 *| 1 OK/Cancel
256 *| 2 Yes/No
257 */
258 INT_PTR WINAPI SHMessageBoxCheckA(HWND hWnd, LPCSTR lpszText, LPCSTR lpszTitle,
259 DWORD dwType, INT_PTR iRet, LPCSTR lpszId)
260 {
261 WCHAR szTitleBuff[MAX_PATH], szIdBuff[MAX_PATH];
262 WCHAR *szTextBuff = NULL;
263 int iLen;
264 INT_PTR iRetVal;
265
266 if (lpszTitle)
267 MultiByteToWideChar(CP_ACP, 0, lpszTitle, -1, szTitleBuff, MAX_PATH);
268
269 if (lpszText)
270 {
271 iLen = MultiByteToWideChar(CP_ACP, 0, lpszText, -1, NULL, 0);
272 szTextBuff = HeapAlloc(GetProcessHeap(), 0, iLen * sizeof(WCHAR));
273 MultiByteToWideChar(CP_ACP, 0, lpszText, -1, szTextBuff, iLen);
274 }
275
276 MultiByteToWideChar(CP_ACP, 0, lpszId, -1, szIdBuff, MAX_PATH);
277
278 iRetVal = SHMessageBoxCheckW(hWnd, szTextBuff, lpszTitle ? szTitleBuff : NULL,
279 dwType, iRet, szIdBuff);
280 HeapFree(GetProcessHeap(), 0, szTextBuff);
281 return iRetVal;
282 }
283
284 /*************************************************************************
285 * @ [SHLWAPI.191]
286 *
287 * Unicode version of SHMessageBoxCheckA.
288 */
289 INT_PTR WINAPI SHMessageBoxCheckW(HWND hWnd, LPCWSTR lpszText, LPCWSTR lpszTitle,
290 DWORD dwType, INT_PTR iRet, LPCWSTR lpszId)
291 {
292 DLGDATA d;
293
294 d.lpszTitle = lpszTitle;
295 d.lpszText = lpszText;
296 d.dwType = dwType;
297
298 return SHMessageBoxCheckExW(hWnd, shlwapi_hInstance, (LPCWSTR)IDD_ERR_DIALOG,
299 SHDlgProc, (LPARAM)&d, iRet, lpszId);
300 }