2 * Common Dialog Boxes interface (32 bit)
5 * Copyright 1998,1999 Bertho A. Stultiens
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(commdlg)
20 /*-----------------------------------------------------------------------*/
22 static UINT FindReplaceMessage;
23 static UINT HelpMessage;
25 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
26 /* CRITICAL_SECTION COMDLG32_CritSect; */
29 * MS uses a critical section at a few locations. However, I fail to
30 * see the reason for this. Their comdlg32.dll has a few race conditions
31 * but _not_ at those places that are protected with the mutex (there are
32 * globals that seem to hold info for the wndproc).
34 * FindText32x/ReplaceText32x
35 * The find/replace calls are passed a structure that is _not_ used
36 * internally. There is a local copy that holds the running info to
37 * be able to combine xxx32A and xxx32W calls. The passed pointer is
38 * returned upon sendmessage. Apps wont break this way when they rely
39 * on the original pointer. This will work as long as the sizes of
40 * FINDREPLACE32A == FINDREPLACE32W. The local copy will also prevent
41 * the app to see the wine-specific extra flags to distinguish between
42 * A/W and Find/Replace.
46 /***********************************************************************
47 * COMDLG32_FR_GetFlags [internal]
48 * Returns the button state that needs to be reported to the caller.
50 * Current state of check and radio buttons
52 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
55 if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
57 if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
58 flags |= FR_WHOLEWORD;
59 if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
60 flags |= FR_MATCHCASE;
64 /***********************************************************************
65 * COMDLG32_FR_HandleWMCommand [internal]
66 * Handle WM_COMMAND messages...
68 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd, COMDLG32_FR_Data *pData, int Id, int NotifyCode)
72 pData->user_fr.fra->Flags &= ~FR_MASK; /* Clear return flags */
73 if(pData->fr.Flags & FR_WINE_REPLACE) /* Replace always goes down... */
74 pData->user_fr.fra->Flags |= FR_DOWN;
76 if(NotifyCode == BN_CLICKED)
80 case IDOK: /* Find Next */
81 if(GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
83 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_FINDNEXT;
84 if(pData->fr.Flags & FR_WINE_UNICODE)
86 lstrcpyAtoW(pData->user_fr.frw->lpstrFindWhat, pData->fr.lpstrFindWhat);
90 lstrcpyA(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
92 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
97 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_DIALOGTERM;
98 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
99 DestroyWindow(hDlgWnd);
102 case psh2: /* Replace All */
103 flag = FR_REPLACEALL;
106 case psh1: /* Replace */
109 if((pData->fr.Flags & FR_WINE_REPLACE)
110 && GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
112 pData->fr.lpstrReplaceWith[0] = 0; /* In case the next GetDlgItemText Fails */
113 GetDlgItemTextA(hDlgWnd, edt2, pData->fr.lpstrReplaceWith, pData->fr.wReplaceWithLen);
114 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | flag;
115 if(pData->fr.Flags & FR_WINE_UNICODE)
117 lstrcpyAtoW(pData->user_fr.frw->lpstrFindWhat, pData->fr.lpstrFindWhat);
118 lstrcpyAtoW(pData->user_fr.frw->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
122 lstrcpyA(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
123 lstrcpyA(pData->user_fr.fra->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
125 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
130 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
131 SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
135 else if(NotifyCode == EN_CHANGE && Id == edt1)
137 BOOL enable = SendDlgItemMessageA(hDlgWnd, edt1, WM_GETTEXTLENGTH, 0, 0) > 0;
138 EnableWindow(GetDlgItem(hDlgWnd, IDOK), enable);
139 if(pData->fr.Flags & FR_WINE_REPLACE)
141 EnableWindow(GetDlgItem(hDlgWnd, psh1), enable);
142 EnableWindow(GetDlgItem(hDlgWnd, psh2), enable);
147 /***********************************************************************
148 * COMDLG32_FindReplaceDlgProc [internal]
149 * [Find/Replace]Text32[A/W] window procedure.
151 static BOOL CALLBACK COMDLG32_FindReplaceDlgProc(HWND hDlgWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
153 COMDLG32_FR_Data *pdata = (COMDLG32_FR_Data *)GetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
156 if(iMsg == WM_INITDIALOG)
158 pdata = (COMDLG32_FR_Data *)lParam;
159 if(!SetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom, (HANDLE)pdata))
161 ERR("Could not Set prop; invent a gracefull exit?...\n");
162 DestroyWindow(hDlgWnd);
165 SendDlgItemMessageA(hDlgWnd, edt1, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wFindWhatLen, 0);
166 SendDlgItemMessageA(hDlgWnd, edt1, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrFindWhat);
167 if(pdata->fr.Flags & FR_WINE_REPLACE)
169 SendDlgItemMessageA(hDlgWnd, edt2, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wReplaceWithLen, 0);
170 SendDlgItemMessageA(hDlgWnd, edt2, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrReplaceWith);
173 if(!(pdata->fr.Flags & FR_SHOWHELP))
174 ShowWindow(GetDlgItem(hDlgWnd, pshHelp), SW_HIDE);
175 if(pdata->fr.Flags & FR_HIDEUPDOWN)
177 ShowWindow(GetDlgItem(hDlgWnd, rad1), SW_HIDE);
178 ShowWindow(GetDlgItem(hDlgWnd, rad2), SW_HIDE);
179 ShowWindow(GetDlgItem(hDlgWnd, grp1), SW_HIDE);
181 else if(pdata->fr.Flags & FR_NOUPDOWN)
183 EnableWindow(GetDlgItem(hDlgWnd, rad1), FALSE);
184 EnableWindow(GetDlgItem(hDlgWnd, rad2), FALSE);
185 EnableWindow(GetDlgItem(hDlgWnd, grp1), FALSE);
189 SendDlgItemMessageA(hDlgWnd, rad1, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? 0 : BST_CHECKED, 0);
190 SendDlgItemMessageA(hDlgWnd, rad2, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? BST_CHECKED : 0, 0);
193 if(pdata->fr.Flags & FR_HIDEMATCHCASE)
194 ShowWindow(GetDlgItem(hDlgWnd, chx2), SW_HIDE);
195 else if(pdata->fr.Flags & FR_NOMATCHCASE)
196 EnableWindow(GetDlgItem(hDlgWnd, chx2), FALSE);
198 SendDlgItemMessageA(hDlgWnd, chx2, BM_SETCHECK, pdata->fr.Flags & FR_MATCHCASE ? BST_CHECKED : 0, 0);
200 if(pdata->fr.Flags & FR_HIDEWHOLEWORD)
201 ShowWindow(GetDlgItem(hDlgWnd, chx1), SW_HIDE);
202 else if(pdata->fr.Flags & FR_NOWHOLEWORD)
203 EnableWindow(GetDlgItem(hDlgWnd, chx1), FALSE);
205 SendDlgItemMessageA(hDlgWnd, chx1, BM_SETCHECK, pdata->fr.Flags & FR_WHOLEWORD ? BST_CHECKED : 0, 0);
207 /* We did the init here, now call the hook if requested */
208 if((pdata->fr.Flags & FR_ENABLEHOOK)
209 && pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, pdata->fr.lCustData))
211 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
212 UpdateWindow(hDlgWnd);
214 /* else the caller is responsible for showing */
218 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
220 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
231 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
235 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
240 FIXME("Got WM_HELP. Who is gonna supply it?\n");
245 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
247 /* FIXME: Handle F1 help */
250 retval = FALSE; /* We did not handle the message */
254 /* WM_DESTROY is a special case.
255 * We need to ensure that the allocated memory is freed just before
256 * the dialog is killed. We also need to remove the added prop.
258 if(iMsg == WM_DESTROY)
260 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
261 HeapFree(GetProcessHeap(), 0, pdata);
267 /***********************************************************************
268 * COMDLG32_FR_CheckPartial [internal]
269 * Check various fault conditions in the supplied parameters that
270 * cause an extended error to be reported.
275 static BOOL COMDLG32_FR_CheckPartial(
276 LPFINDREPLACEA pfr, /* [in] Find structure */
277 BOOL Replace /* [in] True if called as replace */
281 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
285 if(pfr->lStructSize != sizeof(FINDREPLACEA))
287 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
291 if(!IsWindow(pfr->hwndOwner))
293 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
297 if((pfr->wFindWhatLen < 80 || !pfr->lpstrFindWhat)
298 ||(Replace && (pfr->wReplaceWithLen < 80 || !pfr->lpstrReplaceWith)))
300 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
304 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRING)) == 0)
306 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
309 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRING)) == 0)
311 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
315 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
317 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
321 if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
323 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
327 if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
329 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
336 /***********************************************************************
337 * COMDLG32_FR_DoFindReplace [internal]
338 * Actual load and creation of the Find/Replace dialog.
340 * Window handle to created dialog:Succes
343 static HWND COMDLG32_FR_DoFindReplace(
344 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
351 TRACE("hInst=%08x, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
353 if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
355 HMODULE hmod = COMDLG32_hInstance;
357 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
359 hmod = (HMODULE)pdata->fr.hInstance;
360 if(pdata->fr.Flags & FR_WINE_UNICODE)
362 htemplate = FindResourceW(hmod, (LPWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOGA);
366 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
371 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
373 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
377 error = CDERR_FINDRESFAILURE;
381 loadrc = LoadResource(hmod, htemplate);
385 loadrc = (HGLOBAL)pdata->fr.hInstance;
390 error = CDERR_LOADRESFAILURE;
394 if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
396 error = CDERR_LOCKRESFAILURE;
400 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
403 (DLGPROC)COMDLG32_FindReplaceDlgProc,
407 error = CDERR_DIALOGFAILURE;
409 COMDLG32_SetCommDlgExtendedError(error);
410 HeapFree(GetProcessHeap(), 0, pdata);
415 /***********************************************************************
416 * FindText32A [COMDLG32.6]
418 * Window handle to created dialog: Succes
421 HWND WINAPI FindTextA(
422 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
424 COMDLG32_FR_Data *pdata;
426 TRACE("LPFINDREPLACE=%p\n", pfr);
428 if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
431 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
432 return 0; /* Error has been set */
434 pdata->user_fr.fra = pfr;
436 return COMDLG32_FR_DoFindReplace(pdata);
439 /***********************************************************************
440 * ReplaceText32A [COMDLG32.19]
442 * Window handle to created dialog: Succes
445 HWND WINAPI ReplaceTextA(
446 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
448 COMDLG32_FR_Data *pdata;
450 TRACE("LPFINDREPLACE=%p\n", pfr);
452 if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
455 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
456 return 0; /* Error has been set */
458 pdata->user_fr.fra = pfr;
460 pdata->fr.Flags |= FR_WINE_REPLACE;
461 return COMDLG32_FR_DoFindReplace(pdata);
464 /***********************************************************************
465 * FinfText32W [COMDLG32.7]
467 * Window handle to created dialog: Succes
470 HWND WINAPI FindTextW(
471 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
473 COMDLG32_FR_Data *pdata;
475 TRACE("LPFINDREPLACE=%p\n", pfr);
477 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
480 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
481 + pfr->wFindWhatLen)) == NULL)
482 return 0; /* Error has been set */
484 pdata->user_fr.frw = pfr;
485 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
486 pdata->fr.Flags |= FR_WINE_UNICODE;
487 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointer */
488 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
489 return COMDLG32_FR_DoFindReplace(pdata);
492 /***********************************************************************
493 * ReplaceTextW [COMDLG32.20]
495 * Window handle to created dialog: Succes
498 HWND WINAPI ReplaceTextW(
499 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
501 COMDLG32_FR_Data *pdata;
503 TRACE("LPFINDREPLACE=%p\n", pfr);
505 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
508 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
510 + pfr->wReplaceWithLen)) == NULL)
512 return 0; /* Error has been set */
514 pdata->user_fr.frw = pfr;
515 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
516 pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
517 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointers */
518 pdata->fr.lpstrReplaceWith = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1) + pfr->wFindWhatLen;
519 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
520 lstrcpynWtoA(pdata->fr.lpstrReplaceWith, pfr->lpstrReplaceWith, pfr->wReplaceWithLen);
521 return COMDLG32_FR_DoFindReplace(pdata);