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