Synchronize with trunk r58528.
[reactos.git] / dll / win32 / comdlg32 / finddlg.c
1 /*
2 * Common Dialog Boxes interface (32 bit)
3 * Find/Replace
4 *
5 * Copyright 1998,1999 Bertho A. Stultiens
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #define WIN32_NO_STATUS
23 #define _INC_WINDOWS
24 #define COM_NO_WINDOWS_H
25
26 #include <stdarg.h>
27 //#include <string.h>
28 #include <windef.h>
29 //#include "winbase.h"
30 //#include "winnls.h"
31 #include <wingdi.h>
32 #include <winuser.h>
33 #include <commdlg.h>
34 #include <cderr.h>
35 //#include "dlgs.h"
36 #include <wine/debug.h>
37
38 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
39
40 #include "cdlg.h"
41
42
43 /*-----------------------------------------------------------------------*/
44
45 static UINT FindReplaceMessage;
46 static UINT HelpMessage;
47
48 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
49 /* CRITICAL_SECTION COMDLG32_CritSect; */
50
51 /* Notes:
52 * MS uses a critical section at a few locations. However, I fail to
53 * see the reason for this. Their comdlg32.dll has a few race conditions
54 * but _not_ at those places that are protected with the mutex (there are
55 * globals that seem to hold info for the wndproc).
56 *
57 * FindText[AW]/ReplaceText[AW]
58 * The find/replace calls are passed a structure that is _not_ used
59 * internally. There is a local copy that holds the running info to
60 * be able to combine xxxA and xxxW calls. The passed pointer is
61 * returned upon sendmessage. Apps won't break this way when they rely
62 * on the original pointer. This will work as long as the sizes of
63 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
64 * the app to see the wine-specific extra flags to distinguish between
65 * A/W and Find/Replace.
66 */
67
68
69 /***********************************************************************
70 * COMDLG32_FR_GetFlags [internal]
71 * Returns the button state that needs to be reported to the caller.
72 * RETURNS
73 * Current state of check and radio buttons
74 */
75 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
76 {
77 DWORD flags = 0;
78 if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
79 flags |= FR_DOWN;
80 if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
81 flags |= FR_WHOLEWORD;
82 if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
83 flags |= FR_MATCHCASE;
84 return flags;
85 }
86
87 /***********************************************************************
88 * COMDLG32_FR_HandleWMCommand [internal]
89 * Handle WM_COMMAND messages...
90 */
91 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd, COMDLG32_FR_Data *pData, int Id, int NotifyCode)
92 {
93 DWORD flag;
94
95 pData->user_fr.fra->Flags &= ~FR_MASK; /* Clear return flags */
96 if(pData->fr.Flags & FR_WINE_REPLACE) /* Replace always goes down... */
97 pData->user_fr.fra->Flags |= FR_DOWN;
98
99 if(NotifyCode == BN_CLICKED)
100 {
101 switch(Id)
102 {
103 case IDOK: /* Find Next */
104 if(GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
105 {
106 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_FINDNEXT;
107 if(pData->fr.Flags & FR_WINE_UNICODE)
108 {
109 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
110 pData->user_fr.frw->lpstrFindWhat,
111 0x7fffffff );
112 }
113 else
114 {
115 strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
116 }
117 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
118 }
119 break;
120
121 case IDCANCEL:
122 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_DIALOGTERM;
123 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
124 DestroyWindow(hDlgWnd);
125 break;
126
127 case psh2: /* Replace All */
128 flag = FR_REPLACEALL;
129 goto Replace;
130
131 case psh1: /* Replace */
132 flag = FR_REPLACE;
133 Replace:
134 if((pData->fr.Flags & FR_WINE_REPLACE)
135 && GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
136 {
137 pData->fr.lpstrReplaceWith[0] = 0; /* In case the next GetDlgItemText Fails */
138 GetDlgItemTextA(hDlgWnd, edt2, pData->fr.lpstrReplaceWith, pData->fr.wReplaceWithLen);
139 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | flag;
140 if(pData->fr.Flags & FR_WINE_UNICODE)
141 {
142 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
143 pData->user_fr.frw->lpstrFindWhat,
144 0x7fffffff );
145 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrReplaceWith, -1,
146 pData->user_fr.frw->lpstrReplaceWith,
147 0x7fffffff );
148 }
149 else
150 {
151 strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
152 strcpy(pData->user_fr.fra->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
153 }
154 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
155 }
156 break;
157
158 case pshHelp:
159 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
160 SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
161 break;
162 }
163 }
164 else if(NotifyCode == EN_CHANGE && Id == edt1)
165 {
166 BOOL enable = SendDlgItemMessageA(hDlgWnd, edt1, WM_GETTEXTLENGTH, 0, 0) > 0;
167 EnableWindow(GetDlgItem(hDlgWnd, IDOK), enable);
168 if(pData->fr.Flags & FR_WINE_REPLACE)
169 {
170 EnableWindow(GetDlgItem(hDlgWnd, psh1), enable);
171 EnableWindow(GetDlgItem(hDlgWnd, psh2), enable);
172 }
173 }
174 }
175
176 /***********************************************************************
177 * COMDLG32_FindReplaceDlgProc [internal]
178 * [Find/Replace]Text32[A/W] window procedure.
179 */
180 static INT_PTR CALLBACK COMDLG32_FindReplaceDlgProc(HWND hDlgWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
181 {
182 COMDLG32_FR_Data *pdata = GetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
183 INT_PTR retval = TRUE;
184
185 if(iMsg == WM_INITDIALOG)
186 {
187 pdata = (COMDLG32_FR_Data *)lParam;
188 if(!SetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom, (HANDLE)pdata))
189 {
190 ERR("Could not Set prop; invent a graceful exit?...\n");
191 DestroyWindow(hDlgWnd);
192 return FALSE;
193 }
194 SendDlgItemMessageA(hDlgWnd, edt1, EM_SETLIMITTEXT, pdata->fr.wFindWhatLen, 0);
195 SendDlgItemMessageA(hDlgWnd, edt1, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrFindWhat);
196 if(pdata->fr.Flags & FR_WINE_REPLACE)
197 {
198 SendDlgItemMessageA(hDlgWnd, edt2, EM_SETLIMITTEXT, pdata->fr.wReplaceWithLen, 0);
199 SendDlgItemMessageA(hDlgWnd, edt2, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrReplaceWith);
200 }
201
202 if(!(pdata->fr.Flags & FR_SHOWHELP))
203 ShowWindow(GetDlgItem(hDlgWnd, pshHelp), SW_HIDE);
204 if(pdata->fr.Flags & FR_HIDEUPDOWN)
205 {
206 ShowWindow(GetDlgItem(hDlgWnd, rad1), SW_HIDE);
207 ShowWindow(GetDlgItem(hDlgWnd, rad2), SW_HIDE);
208 ShowWindow(GetDlgItem(hDlgWnd, grp1), SW_HIDE);
209 }
210 else if(pdata->fr.Flags & FR_NOUPDOWN)
211 {
212 EnableWindow(GetDlgItem(hDlgWnd, rad1), FALSE);
213 EnableWindow(GetDlgItem(hDlgWnd, rad2), FALSE);
214 EnableWindow(GetDlgItem(hDlgWnd, grp1), FALSE);
215 }
216 else
217 {
218 SendDlgItemMessageA(hDlgWnd, rad1, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? 0 : BST_CHECKED, 0);
219 SendDlgItemMessageA(hDlgWnd, rad2, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? BST_CHECKED : 0, 0);
220 }
221
222 if(pdata->fr.Flags & FR_HIDEMATCHCASE)
223 ShowWindow(GetDlgItem(hDlgWnd, chx2), SW_HIDE);
224 else if(pdata->fr.Flags & FR_NOMATCHCASE)
225 EnableWindow(GetDlgItem(hDlgWnd, chx2), FALSE);
226 else
227 SendDlgItemMessageA(hDlgWnd, chx2, BM_SETCHECK, pdata->fr.Flags & FR_MATCHCASE ? BST_CHECKED : 0, 0);
228
229 if(pdata->fr.Flags & FR_HIDEWHOLEWORD)
230 ShowWindow(GetDlgItem(hDlgWnd, chx1), SW_HIDE);
231 else if(pdata->fr.Flags & FR_NOWHOLEWORD)
232 EnableWindow(GetDlgItem(hDlgWnd, chx1), FALSE);
233 else
234 SendDlgItemMessageA(hDlgWnd, chx1, BM_SETCHECK, pdata->fr.Flags & FR_WHOLEWORD ? BST_CHECKED : 0, 0);
235
236 /* We did the init here, now call the hook if requested */
237
238 /* We do not do ShowWindow if hook exists and is FALSE */
239 /* per MSDN Article Q96135 */
240 if((pdata->fr.Flags & FR_ENABLEHOOK)
241 && ! pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, (LPARAM) &pdata->fr))
242 return TRUE;
243 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
244 UpdateWindow(hDlgWnd);
245 return TRUE;
246 }
247
248 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
249 {
250 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
251 }
252 else
253 retval = FALSE;
254
255 if(pdata && !retval)
256 {
257 retval = TRUE;
258 switch(iMsg)
259 {
260 case WM_COMMAND:
261 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
262 break;
263
264 case WM_CLOSE:
265 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
266 break;
267
268 case WM_HELP:
269 /* Heeeeelp! */
270 FIXME("Got WM_HELP. Who is gonna supply it?\n");
271 break;
272
273 case WM_CONTEXTMENU:
274 /* Heeeeelp! */
275 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
276 break;
277 /* FIXME: Handle F1 help */
278
279 default:
280 retval = FALSE; /* We did not handle the message */
281 }
282 }
283
284 /* WM_DESTROY is a special case.
285 * We need to ensure that the allocated memory is freed just before
286 * the dialog is killed. We also need to remove the added prop.
287 */
288 if(iMsg == WM_DESTROY)
289 {
290 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
291 HeapFree(GetProcessHeap(), 0, pdata);
292 }
293
294 return retval;
295 }
296
297 /***********************************************************************
298 * COMDLG32_FR_CheckPartial [internal]
299 * Check various fault conditions in the supplied parameters that
300 * cause an extended error to be reported.
301 * RETURNS
302 * TRUE: Success
303 * FALSE: Failure
304 */
305 static BOOL COMDLG32_FR_CheckPartial(
306 const FINDREPLACEA *pfr, /* [in] Find structure */
307 BOOL Replace /* [in] True if called as replace */
308 ) {
309 if(!pfr)
310 {
311 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
312 return FALSE;
313 }
314
315 if(pfr->lStructSize != sizeof(FINDREPLACEA))
316 {
317 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
318 return FALSE;
319 }
320
321 if(!IsWindow(pfr->hwndOwner))
322 {
323 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
324 return FALSE;
325 }
326
327 if((pfr->wFindWhatLen < 1 || !pfr->lpstrFindWhat)
328 ||(Replace && !pfr->lpstrReplaceWith))
329 {
330 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
331 return FALSE;
332 }
333
334 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRINGA)) == 0)
335 {
336 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
337 return FALSE;
338 }
339 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRINGA)) == 0)
340 {
341 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
342 return FALSE;
343 }
344
345 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
346 {
347 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
348 return FALSE;
349 }
350
351 if((pfr->Flags & FR_ENABLETEMPLATEHANDLE) && !pfr->hInstance)
352 {
353 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
354 return FALSE;
355 }
356
357 return TRUE;
358 }
359
360 /***********************************************************************
361 * COMDLG32_FR_DoFindReplace [internal]
362 * Actual load and creation of the Find/Replace dialog.
363 * RETURNS
364 * Window handle to created dialog:Success
365 * NULL:Failure
366 */
367 static HWND COMDLG32_FR_DoFindReplace(
368 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
369 ) {
370 HWND hdlgwnd = 0;
371 HGLOBAL loadrc;
372 DWORD error;
373 LPDLGTEMPLATEW rcs;
374
375 TRACE("hInst=%p, Flags=%08x\n", pdata->fr.hInstance, pdata->fr.Flags);
376
377 if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
378 {
379 HMODULE hmod = COMDLG32_hInstance;
380 HRSRC htemplate;
381 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
382 {
383 hmod = pdata->fr.hInstance;
384 if(pdata->fr.Flags & FR_WINE_UNICODE)
385 {
386 htemplate = FindResourceW(hmod, (LPCWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOG);
387 }
388 else
389 {
390 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOG);
391 }
392 }
393 else
394 {
395 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
396 : FINDDLGORD;
397 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOG);
398 }
399 if(!htemplate)
400 {
401 error = CDERR_FINDRESFAILURE;
402 goto cleanup;
403 }
404
405 loadrc = LoadResource(hmod, htemplate);
406 }
407 else
408 {
409 loadrc = pdata->fr.hInstance;
410 }
411
412 if(!loadrc)
413 {
414 error = CDERR_LOADRESFAILURE;
415 goto cleanup;
416 }
417
418 if((rcs = LockResource(loadrc)) == NULL)
419 {
420 error = CDERR_LOCKRESFAILURE;
421 goto cleanup;
422 }
423
424 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
425 rcs,
426 pdata->fr.hwndOwner,
427 COMDLG32_FindReplaceDlgProc,
428 (LPARAM)pdata);
429 if(!hdlgwnd)
430 {
431 error = CDERR_DIALOGFAILURE;
432 cleanup:
433 COMDLG32_SetCommDlgExtendedError(error);
434 HeapFree(GetProcessHeap(), 0, pdata);
435 }
436 return hdlgwnd;
437 }
438
439 /***********************************************************************
440 * FindTextA [COMDLG32.@]
441 *
442 * See FindTextW.
443 */
444 HWND WINAPI FindTextA(
445 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
446 ) {
447 COMDLG32_FR_Data *pdata;
448
449 TRACE("LPFINDREPLACE=%p\n", pfr);
450
451 if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
452 return 0;
453
454 if((pdata = COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
455 return 0; /* Error has been set */
456
457 pdata->user_fr.fra = pfr;
458 pdata->fr = *pfr;
459 return COMDLG32_FR_DoFindReplace(pdata);
460 }
461
462 /***********************************************************************
463 * ReplaceTextA [COMDLG32.@]
464 *
465 * See ReplaceTextW.
466 */
467 HWND WINAPI ReplaceTextA(
468 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
469 ) {
470 COMDLG32_FR_Data *pdata;
471
472 TRACE("LPFINDREPLACE=%p\n", pfr);
473
474 if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
475 return 0;
476
477 if((pdata = COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
478 return 0; /* Error has been set */
479
480 pdata->user_fr.fra = pfr;
481 pdata->fr = *pfr;
482 pdata->fr.Flags |= FR_WINE_REPLACE;
483 return COMDLG32_FR_DoFindReplace(pdata);
484 }
485
486 /***********************************************************************
487 * FindTextW [COMDLG32.@]
488 *
489 * Create a modeless find-text dialog box.
490 *
491 * RETURNS
492 * Window handle to created dialog: Success
493 * NULL: Failure
494 */
495 HWND WINAPI FindTextW(
496 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
497 ) {
498 COMDLG32_FR_Data *pdata;
499 DWORD len;
500
501 TRACE("LPFINDREPLACE=%p\n", pfr);
502
503 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
504 return 0;
505
506 len = WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
507 NULL, 0, NULL, NULL );
508 if((pdata = COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data) + len)) == NULL)
509 return 0; /* Error has been set */
510
511 pdata->user_fr.frw = pfr;
512 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
513 pdata->fr.Flags |= FR_WINE_UNICODE;
514 pdata->fr.lpstrFindWhat = (LPSTR)(pdata + 1); /* Set string pointer */
515 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
516 pdata->fr.lpstrFindWhat, len, NULL, NULL );
517 return COMDLG32_FR_DoFindReplace(pdata);
518 }
519
520 /***********************************************************************
521 * ReplaceTextW [COMDLG32.@]
522 *
523 * Create a modeless replace-text dialog box.
524 *
525 * RETURNS
526 * Window handle to created dialog: Success
527 * NULL: Failure
528 */
529 HWND WINAPI ReplaceTextW(
530 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
531 ) {
532 COMDLG32_FR_Data *pdata;
533 DWORD len1, len2;
534
535 TRACE("LPFINDREPLACE=%p\n", pfr);
536
537 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, TRUE))
538 return 0;
539
540 len1 = WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
541 NULL, 0, NULL, NULL );
542 len2 = WideCharToMultiByte( CP_ACP, 0, pfr->lpstrReplaceWith, pfr->wReplaceWithLen,
543 NULL, 0, NULL, NULL );
544 if((pdata = COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data) + len1 + len2)) == NULL)
545 return 0; /* Error has been set */
546
547 pdata->user_fr.frw = pfr;
548 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
549 pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
550 pdata->fr.lpstrFindWhat = (LPSTR)(pdata + 1); /* Set string pointer */
551 pdata->fr.lpstrReplaceWith = pdata->fr.lpstrFindWhat + len1;
552
553 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
554 pdata->fr.lpstrFindWhat, len1, NULL, NULL );
555 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrReplaceWith, pfr->wReplaceWithLen,
556 pdata->fr.lpstrReplaceWith, len2, NULL, NULL );
557 return COMDLG32_FR_DoFindReplace(pdata);
558 }