Added assembly wrapper for calling window procedures.
[wine.git] / dlls / commdlg / finddlg32.c
blob6f4e96e381cc49a4a7c491bba5b7c6a135afbec0
1 /*
2 * Common Dialog Boxes interface (32 bit)
3 * Find/Replace
5 * Copyright 1998,1999 Bertho A. Stultiens
6 */
8 #include "winbase.h"
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "winuser.h"
12 #include "commdlg.h"
13 #include "cderr.h"
14 #include "dlgs.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(commdlg)
19 #include "cdlg.h"
22 /*-----------------------------------------------------------------------*/
24 static UINT FindReplaceMessage;
25 static UINT HelpMessage;
27 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
28 /* CRITICAL_SECTION COMDLG32_CritSect; */
30 /* Notes:
31 * MS uses a critical section at a few locations. However, I fail to
32 * see the reason for this. Their comdlg32.dll has a few race conditions
33 * but _not_ at those places that are protected with the mutex (there are
34 * globals that seem to hold info for the wndproc).
36 * FindText[AW]/ReplaceText[AW]
37 * The find/replace calls are passed a structure that is _not_ used
38 * internally. There is a local copy that holds the running info to
39 * be able to combine xxxA and xxxW calls. The passed pointer is
40 * returned upon sendmessage. Apps wont break this way when they rely
41 * on the original pointer. This will work as long as the sizes of
42 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
43 * the app to see the wine-specific extra flags to distinguish between
44 * A/W and Find/Replace.
48 /***********************************************************************
49 * COMDLG32_FR_GetFlags [internal]
50 * Returns the button state that needs to be reported to the caller.
51 * RETURNS
52 * Current state of check and radio buttons
54 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
56 DWORD flags = 0;
57 if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
58 flags |= FR_DOWN;
59 if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
60 flags |= FR_WHOLEWORD;
61 if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
62 flags |= FR_MATCHCASE;
63 return flags;
66 /***********************************************************************
67 * COMDLG32_FR_HandleWMCommand [internal]
68 * Handle WM_COMMAND messages...
70 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd, COMDLG32_FR_Data *pData, int Id, int NotifyCode)
72 DWORD flag;
74 pData->user_fr.fra->Flags &= ~FR_MASK; /* Clear return flags */
75 if(pData->fr.Flags & FR_WINE_REPLACE) /* Replace always goes down... */
76 pData->user_fr.fra->Flags |= FR_DOWN;
78 if(NotifyCode == BN_CLICKED)
80 switch(Id)
82 case IDOK: /* Find Next */
83 if(GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
85 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_FINDNEXT;
86 if(pData->fr.Flags & FR_WINE_UNICODE)
88 lstrcpyAtoW(pData->user_fr.frw->lpstrFindWhat, pData->fr.lpstrFindWhat);
90 else
92 lstrcpyA(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
94 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
96 break;
98 case IDCANCEL:
99 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_DIALOGTERM;
100 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
101 DestroyWindow(hDlgWnd);
102 break;
104 case psh2: /* Replace All */
105 flag = FR_REPLACEALL;
106 goto Replace;
108 case psh1: /* Replace */
109 flag = FR_REPLACE;
110 Replace:
111 if((pData->fr.Flags & FR_WINE_REPLACE)
112 && GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
114 pData->fr.lpstrReplaceWith[0] = 0; /* In case the next GetDlgItemText Fails */
115 GetDlgItemTextA(hDlgWnd, edt2, pData->fr.lpstrReplaceWith, pData->fr.wReplaceWithLen);
116 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | flag;
117 if(pData->fr.Flags & FR_WINE_UNICODE)
119 lstrcpyAtoW(pData->user_fr.frw->lpstrFindWhat, pData->fr.lpstrFindWhat);
120 lstrcpyAtoW(pData->user_fr.frw->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
122 else
124 lstrcpyA(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
125 lstrcpyA(pData->user_fr.fra->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
127 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
129 break;
131 case pshHelp:
132 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
133 SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
134 break;
137 else if(NotifyCode == EN_CHANGE && Id == edt1)
139 BOOL enable = SendDlgItemMessageA(hDlgWnd, edt1, WM_GETTEXTLENGTH, 0, 0) > 0;
140 EnableWindow(GetDlgItem(hDlgWnd, IDOK), enable);
141 if(pData->fr.Flags & FR_WINE_REPLACE)
143 EnableWindow(GetDlgItem(hDlgWnd, psh1), enable);
144 EnableWindow(GetDlgItem(hDlgWnd, psh2), enable);
149 /***********************************************************************
150 * COMDLG32_FindReplaceDlgProc [internal]
151 * [Find/Replace]Text32[A/W] window procedure.
153 static BOOL CALLBACK COMDLG32_FindReplaceDlgProc(HWND hDlgWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
155 COMDLG32_FR_Data *pdata = (COMDLG32_FR_Data *)GetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
156 BOOL retval = TRUE;;
158 if(iMsg == WM_INITDIALOG)
160 pdata = (COMDLG32_FR_Data *)lParam;
161 if(!SetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom, (HANDLE)pdata))
163 ERR("Could not Set prop; invent a gracefull exit?...\n");
164 DestroyWindow(hDlgWnd);
165 return FALSE;
167 SendDlgItemMessageA(hDlgWnd, edt1, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wFindWhatLen, 0);
168 SendDlgItemMessageA(hDlgWnd, edt1, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrFindWhat);
169 if(pdata->fr.Flags & FR_WINE_REPLACE)
171 SendDlgItemMessageA(hDlgWnd, edt2, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wReplaceWithLen, 0);
172 SendDlgItemMessageA(hDlgWnd, edt2, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrReplaceWith);
175 if(!(pdata->fr.Flags & FR_SHOWHELP))
176 ShowWindow(GetDlgItem(hDlgWnd, pshHelp), SW_HIDE);
177 if(pdata->fr.Flags & FR_HIDEUPDOWN)
179 ShowWindow(GetDlgItem(hDlgWnd, rad1), SW_HIDE);
180 ShowWindow(GetDlgItem(hDlgWnd, rad2), SW_HIDE);
181 ShowWindow(GetDlgItem(hDlgWnd, grp1), SW_HIDE);
183 else if(pdata->fr.Flags & FR_NOUPDOWN)
185 EnableWindow(GetDlgItem(hDlgWnd, rad1), FALSE);
186 EnableWindow(GetDlgItem(hDlgWnd, rad2), FALSE);
187 EnableWindow(GetDlgItem(hDlgWnd, grp1), FALSE);
189 else
191 SendDlgItemMessageA(hDlgWnd, rad1, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? 0 : BST_CHECKED, 0);
192 SendDlgItemMessageA(hDlgWnd, rad2, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? BST_CHECKED : 0, 0);
195 if(pdata->fr.Flags & FR_HIDEMATCHCASE)
196 ShowWindow(GetDlgItem(hDlgWnd, chx2), SW_HIDE);
197 else if(pdata->fr.Flags & FR_NOMATCHCASE)
198 EnableWindow(GetDlgItem(hDlgWnd, chx2), FALSE);
199 else
200 SendDlgItemMessageA(hDlgWnd, chx2, BM_SETCHECK, pdata->fr.Flags & FR_MATCHCASE ? BST_CHECKED : 0, 0);
202 if(pdata->fr.Flags & FR_HIDEWHOLEWORD)
203 ShowWindow(GetDlgItem(hDlgWnd, chx1), SW_HIDE);
204 else if(pdata->fr.Flags & FR_NOWHOLEWORD)
205 EnableWindow(GetDlgItem(hDlgWnd, chx1), FALSE);
206 else
207 SendDlgItemMessageA(hDlgWnd, chx1, BM_SETCHECK, pdata->fr.Flags & FR_WHOLEWORD ? BST_CHECKED : 0, 0);
209 /* We did the init here, now call the hook if requested */
211 /* We do not do ShowWindow if hook exists and is FALSE */
212 /* per MSDN Article Q96135 */
213 if((pdata->fr.Flags & FR_ENABLEHOOK)
214 && ! pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, pdata->fr.lCustData))
215 return TRUE;
216 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
217 UpdateWindow(hDlgWnd);
218 return TRUE;
221 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
223 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
225 else
226 retval = FALSE;
228 if(pdata && !retval)
230 retval = TRUE;
231 switch(iMsg)
233 case WM_COMMAND:
234 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
235 break;
237 case WM_CLOSE:
238 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
239 break;
241 case WM_HELP:
242 /* Heeeeelp! */
243 FIXME("Got WM_HELP. Who is gonna supply it?\n");
244 break;
246 case WM_CONTEXTMENU:
247 /* Heeeeelp! */
248 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
249 break;
250 /* FIXME: Handle F1 help */
252 default:
253 retval = FALSE; /* We did not handle the message */
257 /* WM_DESTROY is a special case.
258 * We need to ensure that the allocated memory is freed just before
259 * the dialog is killed. We also need to remove the added prop.
261 if(iMsg == WM_DESTROY)
263 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
264 HeapFree(GetProcessHeap(), 0, pdata);
267 return retval;
270 /***********************************************************************
271 * COMDLG32_FR_CheckPartial [internal]
272 * Check various fault conditions in the supplied parameters that
273 * cause an extended error to be reported.
274 * RETURNS
275 * TRUE: Succes
276 * FALSE: Failure
278 static BOOL COMDLG32_FR_CheckPartial(
279 LPFINDREPLACEA pfr, /* [in] Find structure */
280 BOOL Replace /* [in] True if called as replace */
282 if(!pfr)
284 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
285 return FALSE;
288 if(pfr->lStructSize != sizeof(FINDREPLACEA))
290 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
291 return FALSE;
294 if(!IsWindow(pfr->hwndOwner))
296 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
297 return FALSE;
300 if((pfr->wFindWhatLen < 1 || !pfr->lpstrFindWhat)
301 ||(Replace && (pfr->wReplaceWithLen < 1 || !pfr->lpstrReplaceWith)))
303 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
304 return FALSE;
307 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRING)) == 0)
309 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
310 return FALSE;
312 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRING)) == 0)
314 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
315 return FALSE;
318 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
320 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
321 return FALSE;
324 if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
326 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
327 return FALSE;
330 if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
332 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
333 return FALSE;
336 return TRUE;
339 /***********************************************************************
340 * COMDLG32_FR_DoFindReplace [internal]
341 * Actual load and creation of the Find/Replace dialog.
342 * RETURNS
343 * Window handle to created dialog:Succes
344 * NULL:Failure
346 static HWND COMDLG32_FR_DoFindReplace(
347 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
349 HWND hdlgwnd = 0;
350 HGLOBAL loadrc;
351 DWORD error;
352 LPDLGTEMPLATEW rcs;
354 TRACE("hInst=%08x, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
356 if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
358 HMODULE hmod = COMDLG32_hInstance;
359 HRSRC htemplate;
360 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
362 hmod = (HMODULE)pdata->fr.hInstance;
363 if(pdata->fr.Flags & FR_WINE_UNICODE)
365 htemplate = FindResourceW(hmod, (LPWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOGA);
367 else
369 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
372 else
374 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
375 : FINDDLGORD;
376 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
378 if(!htemplate)
380 error = CDERR_FINDRESFAILURE;
381 goto cleanup;
384 loadrc = LoadResource(hmod, htemplate);
386 else
388 loadrc = (HGLOBAL)pdata->fr.hInstance;
391 if(!loadrc)
393 error = CDERR_LOADRESFAILURE;
394 goto cleanup;
397 if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
399 error = CDERR_LOCKRESFAILURE;
400 goto cleanup;
403 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
404 rcs,
405 pdata->fr.hwndOwner,
406 (DLGPROC)COMDLG32_FindReplaceDlgProc,
407 (LPARAM)pdata);
408 if(!hdlgwnd)
410 error = CDERR_DIALOGFAILURE;
411 cleanup:
412 COMDLG32_SetCommDlgExtendedError(error);
413 HeapFree(GetProcessHeap(), 0, pdata);
415 return hdlgwnd;
418 /***********************************************************************
419 * FindTextA [COMDLG32.6]
420 * RETURNS
421 * Window handle to created dialog: Succes
422 * NULL: Failure
424 HWND WINAPI FindTextA(
425 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
427 COMDLG32_FR_Data *pdata;
429 TRACE("LPFINDREPLACE=%p\n", pfr);
431 if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
432 return 0;
434 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
435 return 0; /* Error has been set */
437 pdata->user_fr.fra = pfr;
438 pdata->fr = *pfr;
439 return COMDLG32_FR_DoFindReplace(pdata);
442 /***********************************************************************
443 * ReplaceTextA [COMDLG32.19]
444 * RETURNS
445 * Window handle to created dialog: Succes
446 * NULL: Failure
448 HWND WINAPI ReplaceTextA(
449 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
451 COMDLG32_FR_Data *pdata;
453 TRACE("LPFINDREPLACE=%p\n", pfr);
455 if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
456 return 0;
458 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
459 return 0; /* Error has been set */
461 pdata->user_fr.fra = pfr;
462 pdata->fr = *pfr;
463 pdata->fr.Flags |= FR_WINE_REPLACE;
464 return COMDLG32_FR_DoFindReplace(pdata);
467 /***********************************************************************
468 * FindTextW [COMDLG32.7]
469 * RETURNS
470 * Window handle to created dialog: Succes
471 * NULL: Failure
473 HWND WINAPI FindTextW(
474 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
476 COMDLG32_FR_Data *pdata;
478 TRACE("LPFINDREPLACE=%p\n", pfr);
480 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
481 return 0;
483 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
484 + pfr->wFindWhatLen)) == NULL)
485 return 0; /* Error has been set */
487 pdata->user_fr.frw = pfr;
488 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
489 pdata->fr.Flags |= FR_WINE_UNICODE;
490 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointer */
491 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
492 return COMDLG32_FR_DoFindReplace(pdata);
495 /***********************************************************************
496 * ReplaceTextW [COMDLG32.20]
497 * RETURNS
498 * Window handle to created dialog: Succes
499 * NULL: Failure
501 HWND WINAPI ReplaceTextW(
502 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
504 COMDLG32_FR_Data *pdata;
506 TRACE("LPFINDREPLACE=%p\n", pfr);
508 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
509 return 0;
511 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
512 + pfr->wFindWhatLen
513 + pfr->wReplaceWithLen)) == NULL)
515 return 0; /* Error has been set */
517 pdata->user_fr.frw = pfr;
518 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
519 pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
520 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointers */
521 pdata->fr.lpstrReplaceWith = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1) + pfr->wFindWhatLen;
522 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
523 lstrcpynWtoA(pdata->fr.lpstrReplaceWith, pfr->lpstrReplaceWith, pfr->wReplaceWithLen);
524 return COMDLG32_FR_DoFindReplace(pdata);