2 * Common Dialog Boxes interface (32 bit)
5 * Copyright 1998,1999 Bertho A. Stultiens
12 #include "wine/winestring.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(commdlg
)
23 /*-----------------------------------------------------------------------*/
25 static UINT FindReplaceMessage
;
26 static UINT HelpMessage
;
28 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
29 /* CRITICAL_SECTION COMDLG32_CritSect; */
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).
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.
49 /***********************************************************************
50 * COMDLG32_FR_GetFlags [internal]
51 * Returns the button state that needs to be reported to the caller.
53 * Current state of check and radio buttons
55 static DWORD
COMDLG32_FR_GetFlags(HWND hDlgWnd
)
58 if(IsDlgButtonChecked(hDlgWnd
, rad2
) == BST_CHECKED
)
60 if(IsDlgButtonChecked(hDlgWnd
, chx1
) == BST_CHECKED
)
61 flags
|= FR_WHOLEWORD
;
62 if(IsDlgButtonChecked(hDlgWnd
, chx2
) == BST_CHECKED
)
63 flags
|= FR_MATCHCASE
;
67 /***********************************************************************
68 * COMDLG32_FR_HandleWMCommand [internal]
69 * Handle WM_COMMAND messages...
71 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd
, COMDLG32_FR_Data
*pData
, int Id
, int NotifyCode
)
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
;
79 if(NotifyCode
== BN_CLICKED
)
83 case IDOK
: /* Find Next */
84 if(GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
86 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_FINDNEXT
;
87 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
89 lstrcpyAtoW(pData
->user_fr
.frw
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
93 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
95 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
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
);
105 case psh2
: /* Replace All */
106 flag
= FR_REPLACEALL
;
109 case psh1
: /* Replace */
112 if((pData
->fr
.Flags
& FR_WINE_REPLACE
)
113 && GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
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
)
120 lstrcpyAtoW(pData
->user_fr
.frw
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
121 lstrcpyAtoW(pData
->user_fr
.frw
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
125 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
126 strcpy(pData
->user_fr
.fra
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
128 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
133 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
);
134 SendMessageA(pData
->fr
.hwndOwner
, HelpMessage
, (WPARAM
)hDlgWnd
, (LPARAM
)pData
->user_fr
.fra
);
138 else if(NotifyCode
== EN_CHANGE
&& Id
== edt1
)
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
)
144 EnableWindow(GetDlgItem(hDlgWnd
, psh1
), enable
);
145 EnableWindow(GetDlgItem(hDlgWnd
, psh2
), enable
);
150 /***********************************************************************
151 * COMDLG32_FindReplaceDlgProc [internal]
152 * [Find/Replace]Text32[A/W] window procedure.
154 static BOOL CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
156 COMDLG32_FR_Data
*pdata
= (COMDLG32_FR_Data
*)GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
159 if(iMsg
== WM_INITDIALOG
)
161 pdata
= (COMDLG32_FR_Data
*)lParam
;
162 if(!SetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
, (HANDLE
)pdata
))
164 ERR("Could not Set prop; invent a gracefull exit?...\n");
165 DestroyWindow(hDlgWnd
);
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
)
172 SendDlgItemMessageA(hDlgWnd
, edt2
, EM_SETLIMITTEXT
, (WPARAM
)pdata
->fr
.wReplaceWithLen
, 0);
173 SendDlgItemMessageA(hDlgWnd
, edt2
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrReplaceWith
);
176 if(!(pdata
->fr
.Flags
& FR_SHOWHELP
))
177 ShowWindow(GetDlgItem(hDlgWnd
, pshHelp
), SW_HIDE
);
178 if(pdata
->fr
.Flags
& FR_HIDEUPDOWN
)
180 ShowWindow(GetDlgItem(hDlgWnd
, rad1
), SW_HIDE
);
181 ShowWindow(GetDlgItem(hDlgWnd
, rad2
), SW_HIDE
);
182 ShowWindow(GetDlgItem(hDlgWnd
, grp1
), SW_HIDE
);
184 else if(pdata
->fr
.Flags
& FR_NOUPDOWN
)
186 EnableWindow(GetDlgItem(hDlgWnd
, rad1
), FALSE
);
187 EnableWindow(GetDlgItem(hDlgWnd
, rad2
), FALSE
);
188 EnableWindow(GetDlgItem(hDlgWnd
, grp1
), FALSE
);
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);
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
);
201 SendDlgItemMessageA(hDlgWnd
, chx2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_MATCHCASE
? BST_CHECKED
: 0, 0);
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
);
208 SendDlgItemMessageA(hDlgWnd
, chx1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_WHOLEWORD
? BST_CHECKED
: 0, 0);
210 /* We did the init here, now call the hook if requested */
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 && ! pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, pdata
->fr
.lCustData
))
217 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
218 UpdateWindow(hDlgWnd
);
222 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
224 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
235 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
239 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
244 FIXME("Got WM_HELP. Who is gonna supply it?\n");
249 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
251 /* FIXME: Handle F1 help */
254 retval
= FALSE
; /* We did not handle the message */
258 /* WM_DESTROY is a special case.
259 * We need to ensure that the allocated memory is freed just before
260 * the dialog is killed. We also need to remove the added prop.
262 if(iMsg
== WM_DESTROY
)
264 RemovePropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
265 HeapFree(GetProcessHeap(), 0, pdata
);
271 /***********************************************************************
272 * COMDLG32_FR_CheckPartial [internal]
273 * Check various fault conditions in the supplied parameters that
274 * cause an extended error to be reported.
279 static BOOL
COMDLG32_FR_CheckPartial(
280 LPFINDREPLACEA pfr
, /* [in] Find structure */
281 BOOL Replace
/* [in] True if called as replace */
285 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES
);
289 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
291 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
295 if(!IsWindow(pfr
->hwndOwner
))
297 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
301 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
302 ||(Replace
&& (pfr
->wReplaceWithLen
< 1 || !pfr
->lpstrReplaceWith
)))
304 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
308 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRING
)) == 0)
310 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
313 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRING
)) == 0)
315 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
319 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
321 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
325 if((pfr
->Flags
& (FR_ENABLETEMPLATE
| FR_ENABLETEMPLATEHANDLE
)) && !pfr
->hInstance
)
327 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
331 if((pfr
->Flags
& FR_ENABLETEMPLATE
) && !pfr
->lpTemplateName
)
333 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE
);
340 /***********************************************************************
341 * COMDLG32_FR_DoFindReplace [internal]
342 * Actual load and creation of the Find/Replace dialog.
344 * Window handle to created dialog:Succes
347 static HWND
COMDLG32_FR_DoFindReplace(
348 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
355 TRACE("hInst=%08x, Flags=%08lx\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
357 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
359 HMODULE hmod
= COMDLG32_hInstance
;
361 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
363 hmod
= (HMODULE
)pdata
->fr
.hInstance
;
364 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
366 htemplate
= FindResourceW(hmod
, (LPWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOGA
);
370 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOGA
);
375 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
377 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOGA
);
381 error
= CDERR_FINDRESFAILURE
;
385 loadrc
= LoadResource(hmod
, htemplate
);
389 loadrc
= (HGLOBAL
)pdata
->fr
.hInstance
;
394 error
= CDERR_LOADRESFAILURE
;
398 if((rcs
= (LPDLGTEMPLATEW
)LockResource(loadrc
)) == NULL
)
400 error
= CDERR_LOCKRESFAILURE
;
404 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
407 (DLGPROC
)COMDLG32_FindReplaceDlgProc
,
411 error
= CDERR_DIALOGFAILURE
;
413 COMDLG32_SetCommDlgExtendedError(error
);
414 HeapFree(GetProcessHeap(), 0, pdata
);
419 /***********************************************************************
420 * FindTextA [COMDLG32.6]
422 * Window handle to created dialog: Succes
425 HWND WINAPI
FindTextA(
426 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
428 COMDLG32_FR_Data
*pdata
;
430 TRACE("LPFINDREPLACE=%p\n", pfr
);
432 if(!COMDLG32_FR_CheckPartial(pfr
, FALSE
))
435 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
436 return 0; /* Error has been set */
438 pdata
->user_fr
.fra
= pfr
;
440 return COMDLG32_FR_DoFindReplace(pdata
);
443 /***********************************************************************
444 * ReplaceTextA [COMDLG32.19]
446 * Window handle to created dialog: Succes
449 HWND WINAPI
ReplaceTextA(
450 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
452 COMDLG32_FR_Data
*pdata
;
454 TRACE("LPFINDREPLACE=%p\n", pfr
);
456 if(!COMDLG32_FR_CheckPartial(pfr
, TRUE
))
459 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
460 return 0; /* Error has been set */
462 pdata
->user_fr
.fra
= pfr
;
464 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
465 return COMDLG32_FR_DoFindReplace(pdata
);
468 /***********************************************************************
469 * FindTextW [COMDLG32.7]
471 * Window handle to created dialog: Succes
474 HWND WINAPI
FindTextW(
475 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
477 COMDLG32_FR_Data
*pdata
;
479 TRACE("LPFINDREPLACE=%p\n", pfr
);
481 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
484 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
)
485 + pfr
->wFindWhatLen
)) == NULL
)
486 return 0; /* Error has been set */
488 pdata
->user_fr
.frw
= pfr
;
489 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
490 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
491 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(((LPFINDREPLACEA
)(pdata
+1))+1); /* Set string pointer */
492 lstrcpynWtoA(pdata
->fr
.lpstrFindWhat
, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
);
493 return COMDLG32_FR_DoFindReplace(pdata
);
496 /***********************************************************************
497 * ReplaceTextW [COMDLG32.20]
499 * Window handle to created dialog: Succes
502 HWND WINAPI
ReplaceTextW(
503 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
505 COMDLG32_FR_Data
*pdata
;
507 TRACE("LPFINDREPLACE=%p\n", pfr
);
509 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
512 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
)
514 + pfr
->wReplaceWithLen
)) == NULL
)
516 return 0; /* Error has been set */
518 pdata
->user_fr
.frw
= pfr
;
519 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
520 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
521 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(((LPFINDREPLACEA
)(pdata
+1))+1); /* Set string pointers */
522 pdata
->fr
.lpstrReplaceWith
= (LPSTR
)(((LPFINDREPLACEA
)(pdata
+1))+1) + pfr
->wFindWhatLen
;
523 lstrcpynWtoA(pdata
->fr
.lpstrFindWhat
, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
);
524 lstrcpynWtoA(pdata
->fr
.lpstrReplaceWith
, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
);
525 return COMDLG32_FR_DoFindReplace(pdata
);