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 * 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.
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 */
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
))
216 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
217 UpdateWindow(hDlgWnd
);
221 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
223 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
234 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
238 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
243 FIXME("Got WM_HELP. Who is gonna supply it?\n");
248 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
250 /* FIXME: Handle F1 help */
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
);
270 /***********************************************************************
271 * COMDLG32_FR_CheckPartial [internal]
272 * Check various fault conditions in the supplied parameters that
273 * cause an extended error to be reported.
278 static BOOL
COMDLG32_FR_CheckPartial(
279 LPFINDREPLACEA pfr
, /* [in] Find structure */
280 BOOL Replace
/* [in] True if called as replace */
284 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES
);
288 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
290 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
294 if(!IsWindow(pfr
->hwndOwner
))
296 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
300 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
301 ||(Replace
&& (pfr
->wReplaceWithLen
< 1 || !pfr
->lpstrReplaceWith
)))
303 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
307 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRING
)) == 0)
309 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
312 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRING
)) == 0)
314 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
318 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
320 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
324 if((pfr
->Flags
& (FR_ENABLETEMPLATE
| FR_ENABLETEMPLATEHANDLE
)) && !pfr
->hInstance
)
326 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
330 if((pfr
->Flags
& FR_ENABLETEMPLATE
) && !pfr
->lpTemplateName
)
332 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE
);
339 /***********************************************************************
340 * COMDLG32_FR_DoFindReplace [internal]
341 * Actual load and creation of the Find/Replace dialog.
343 * Window handle to created dialog:Succes
346 static HWND
COMDLG32_FR_DoFindReplace(
347 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
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
;
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
);
369 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOGA
);
374 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
376 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOGA
);
380 error
= CDERR_FINDRESFAILURE
;
384 loadrc
= LoadResource(hmod
, htemplate
);
388 loadrc
= (HGLOBAL
)pdata
->fr
.hInstance
;
393 error
= CDERR_LOADRESFAILURE
;
397 if((rcs
= (LPDLGTEMPLATEW
)LockResource(loadrc
)) == NULL
)
399 error
= CDERR_LOCKRESFAILURE
;
403 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
406 (DLGPROC
)COMDLG32_FindReplaceDlgProc
,
410 error
= CDERR_DIALOGFAILURE
;
412 COMDLG32_SetCommDlgExtendedError(error
);
413 HeapFree(GetProcessHeap(), 0, pdata
);
418 /***********************************************************************
419 * FindTextA [COMDLG32.6]
421 * Window handle to created dialog: Succes
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
))
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
;
439 return COMDLG32_FR_DoFindReplace(pdata
);
442 /***********************************************************************
443 * ReplaceTextA [COMDLG32.19]
445 * Window handle to created dialog: Succes
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
))
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
;
463 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
464 return COMDLG32_FR_DoFindReplace(pdata
);
467 /***********************************************************************
468 * FindTextW [COMDLG32.7]
470 * Window handle to created dialog: Succes
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
))
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]
498 * Window handle to created dialog: Succes
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
))
511 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
)
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
);