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