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