source: trunk/src/comdlg32/finddlg32.c@ 5280

Last change on this file since 5280 was 4560, checked in by sandervl, 25 years ago

FindReplace fix for hook call during WM_INITDIALOG

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