2 * Common Dialog Boxes interface (32 bit)
5 * Copyright 1998,1999 Bertho A. Stultiens
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(commdlg)
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; */
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 * FindText32x/ReplaceText32x
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 xxx32A and xxx32W 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 * FINDREPLACE32A == FINDREPLACE32W. 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.
52 * Current state of check and radio buttons
54 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
57 if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
59 if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
60 flags |= FR_WHOLEWORD;
61 if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
62 flags |= FR_MATCHCASE;
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)
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)
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);
92 lstrcpyA(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
94 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
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);
104 case psh2: /* Replace All */
105 flag = FR_REPLACEALL;
108 case psh1: /* 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);
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);
132 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
133 SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
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);
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);
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);
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);
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);
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 */
210 if((pdata->fr.Flags & FR_ENABLEHOOK)
211 && pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, pdata->fr.lCustData))
213 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
214 UpdateWindow(hDlgWnd);
216 /* else the caller is responsible for showing */
220 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
222 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
233 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
237 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
242 FIXME("Got WM_HELP. Who is gonna supply it?\n");
247 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
249 /* FIXME: Handle F1 help */
252 retval = FALSE; /* We did not handle the message */
256 /* WM_DESTROY is a special case.
257 * We need to ensure that the allocated memory is freed just before
258 * the dialog is killed. We also need to remove the added prop.
260 if(iMsg == WM_DESTROY)
262 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
263 HeapFree(GetProcessHeap(), 0, pdata);
269 /***********************************************************************
270 * COMDLG32_FR_CheckPartial [internal]
271 * Check various fault conditions in the supplied parameters that
272 * cause an extended error to be reported.
277 static BOOL COMDLG32_FR_CheckPartial(
278 LPFINDREPLACEA pfr, /* [in] Find structure */
279 BOOL Replace /* [in] True if called as replace */
283 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
287 if(pfr->lStructSize != sizeof(FINDREPLACEA))
289 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
293 if(!IsWindow(pfr->hwndOwner))
295 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
299 if((pfr->wFindWhatLen < 80 || !pfr->lpstrFindWhat)
300 ||(Replace && (pfr->wReplaceWithLen < 80 || !pfr->lpstrReplaceWith)))
302 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
306 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRING)) == 0)
308 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
311 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRING)) == 0)
313 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
317 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
319 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
323 if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
325 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
329 if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
331 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
338 /***********************************************************************
339 * COMDLG32_FR_DoFindReplace [internal]
340 * Actual load and creation of the Find/Replace dialog.
342 * Window handle to created dialog:Succes
345 static HWND COMDLG32_FR_DoFindReplace(
346 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
353 TRACE("hInst=%08x, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
355 if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
357 HMODULE hmod = COMDLG32_hInstance;
359 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
361 hmod = (HMODULE)pdata->fr.hInstance;
362 if(pdata->fr.Flags & FR_WINE_UNICODE)
364 htemplate = FindResourceW(hmod, (LPWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOGA);
368 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
373 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
375 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
379 error = CDERR_FINDRESFAILURE;
383 loadrc = LoadResource(hmod, htemplate);
387 loadrc = (HGLOBAL)pdata->fr.hInstance;
392 error = CDERR_LOADRESFAILURE;
396 if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
398 error = CDERR_LOCKRESFAILURE;
402 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
405 (DLGPROC)COMDLG32_FindReplaceDlgProc,
409 error = CDERR_DIALOGFAILURE;
411 COMDLG32_SetCommDlgExtendedError(error);
412 HeapFree(GetProcessHeap(), 0, pdata);
417 /***********************************************************************
418 * FindText32A [COMDLG32.6]
420 * Window handle to created dialog: Succes
423 HWND WINAPI FindTextA(
424 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
426 COMDLG32_FR_Data *pdata;
428 TRACE("LPFINDREPLACE=%p\n", pfr);
430 if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
433 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
434 return 0; /* Error has been set */
436 pdata->user_fr.fra = pfr;
438 return COMDLG32_FR_DoFindReplace(pdata);
441 /***********************************************************************
442 * ReplaceText32A [COMDLG32.19]
444 * Window handle to created dialog: Succes
447 HWND WINAPI ReplaceTextA(
448 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
450 COMDLG32_FR_Data *pdata;
452 TRACE("LPFINDREPLACE=%p\n", pfr);
454 if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
457 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
458 return 0; /* Error has been set */
460 pdata->user_fr.fra = pfr;
462 pdata->fr.Flags |= FR_WINE_REPLACE;
463 return COMDLG32_FR_DoFindReplace(pdata);
466 /***********************************************************************
467 * FinfText32W [COMDLG32.7]
469 * Window handle to created dialog: Succes
472 HWND WINAPI FindTextW(
473 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
475 COMDLG32_FR_Data *pdata;
477 TRACE("LPFINDREPLACE=%p\n", pfr);
479 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
482 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
483 + pfr->wFindWhatLen)) == NULL)
484 return 0; /* Error has been set */
486 pdata->user_fr.frw = pfr;
487 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
488 pdata->fr.Flags |= FR_WINE_UNICODE;
489 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointer */
490 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
491 return COMDLG32_FR_DoFindReplace(pdata);
494 /***********************************************************************
495 * ReplaceTextW [COMDLG32.20]
497 * Window handle to created dialog: Succes
500 HWND WINAPI ReplaceTextW(
501 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
503 COMDLG32_FR_Data *pdata;
505 TRACE("LPFINDREPLACE=%p\n", pfr);
507 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
510 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
512 + pfr->wReplaceWithLen)) == NULL)
514 return 0; /* Error has been set */
516 pdata->user_fr.frw = pfr;
517 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
518 pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
519 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointers */
520 pdata->fr.lpstrReplaceWith = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1) + pfr->wFindWhatLen;
521 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
522 lstrcpynWtoA(pdata->fr.lpstrReplaceWith, pfr->lpstrReplaceWith, pfr->wReplaceWithLen);
523 return COMDLG32_FR_DoFindReplace(pdata);