2 * Common Dialog Boxes interface (32 bit)
5 * Copyright 1998,1999 Bertho A. Stultiens
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(commdlg
);
24 /*-----------------------------------------------------------------------*/
26 static UINT FindReplaceMessage
;
27 static UINT HelpMessage
;
29 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
30 /* CRITICAL_SECTION COMDLG32_CritSect; */
33 * MS uses a critical section at a few locations. However, I fail to
34 * see the reason for this. Their comdlg32.dll has a few race conditions
35 * but _not_ at those places that are protected with the mutex (there are
36 * globals that seem to hold info for the wndproc).
38 * FindText[AW]/ReplaceText[AW]
39 * The find/replace calls are passed a structure that is _not_ used
40 * internally. There is a local copy that holds the running info to
41 * be able to combine xxxA and xxxW calls. The passed pointer is
42 * returned upon sendmessage. Apps wont break this way when they rely
43 * on the original pointer. This will work as long as the sizes of
44 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
45 * the app to see the wine-specific extra flags to distinguish between
46 * A/W and Find/Replace.
50 /***********************************************************************
51 * COMDLG32_FR_GetFlags [internal]
52 * Returns the button state that needs to be reported to the caller.
54 * Current state of check and radio buttons
56 static DWORD
COMDLG32_FR_GetFlags(HWND hDlgWnd
)
59 if(IsDlgButtonChecked(hDlgWnd
, rad2
) == BST_CHECKED
)
61 if(IsDlgButtonChecked(hDlgWnd
, chx1
) == BST_CHECKED
)
62 flags
|= FR_WHOLEWORD
;
63 if(IsDlgButtonChecked(hDlgWnd
, chx2
) == BST_CHECKED
)
64 flags
|= FR_MATCHCASE
;
68 /***********************************************************************
69 * COMDLG32_FR_HandleWMCommand [internal]
70 * Handle WM_COMMAND messages...
72 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd
, COMDLG32_FR_Data
*pData
, int Id
, int NotifyCode
)
76 pData
->user_fr
.fra
->Flags
&= ~FR_MASK
; /* Clear return flags */
77 if(pData
->fr
.Flags
& FR_WINE_REPLACE
) /* Replace always goes down... */
78 pData
->user_fr
.fra
->Flags
|= FR_DOWN
;
80 if(NotifyCode
== BN_CLICKED
)
84 case IDOK
: /* Find Next */
85 if(GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
87 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_FINDNEXT
;
88 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
90 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
91 pData
->user_fr
.frw
->lpstrFindWhat
,
96 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
98 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
103 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_DIALOGTERM
;
104 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
105 DestroyWindow(hDlgWnd
);
108 case psh2
: /* Replace All */
109 flag
= FR_REPLACEALL
;
112 case psh1
: /* Replace */
115 if((pData
->fr
.Flags
& FR_WINE_REPLACE
)
116 && GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
118 pData
->fr
.lpstrReplaceWith
[0] = 0; /* In case the next GetDlgItemText Fails */
119 GetDlgItemTextA(hDlgWnd
, edt2
, pData
->fr
.lpstrReplaceWith
, pData
->fr
.wReplaceWithLen
);
120 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | flag
;
121 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
123 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
124 pData
->user_fr
.frw
->lpstrFindWhat
,
126 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrReplaceWith
, -1,
127 pData
->user_fr
.frw
->lpstrReplaceWith
,
132 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
133 strcpy(pData
->user_fr
.fra
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
135 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
140 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
);
141 SendMessageA(pData
->fr
.hwndOwner
, HelpMessage
, (WPARAM
)hDlgWnd
, (LPARAM
)pData
->user_fr
.fra
);
145 else if(NotifyCode
== EN_CHANGE
&& Id
== edt1
)
147 BOOL enable
= SendDlgItemMessageA(hDlgWnd
, edt1
, WM_GETTEXTLENGTH
, 0, 0) > 0;
148 EnableWindow(GetDlgItem(hDlgWnd
, IDOK
), enable
);
149 if(pData
->fr
.Flags
& FR_WINE_REPLACE
)
151 EnableWindow(GetDlgItem(hDlgWnd
, psh1
), enable
);
152 EnableWindow(GetDlgItem(hDlgWnd
, psh2
), enable
);
157 /***********************************************************************
158 * COMDLG32_FindReplaceDlgProc [internal]
159 * [Find/Replace]Text32[A/W] window procedure.
161 static BOOL CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
163 COMDLG32_FR_Data
*pdata
= (COMDLG32_FR_Data
*)GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
166 if(iMsg
== WM_INITDIALOG
)
168 pdata
= (COMDLG32_FR_Data
*)lParam
;
169 if(!SetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
, (HANDLE
)pdata
))
171 ERR("Could not Set prop; invent a gracefull exit?...\n");
172 DestroyWindow(hDlgWnd
);
175 SendDlgItemMessageA(hDlgWnd
, edt1
, EM_SETLIMITTEXT
, (WPARAM
)pdata
->fr
.wFindWhatLen
, 0);
176 SendDlgItemMessageA(hDlgWnd
, edt1
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrFindWhat
);
177 if(pdata
->fr
.Flags
& FR_WINE_REPLACE
)
179 SendDlgItemMessageA(hDlgWnd
, edt2
, EM_SETLIMITTEXT
, (WPARAM
)pdata
->fr
.wReplaceWithLen
, 0);
180 SendDlgItemMessageA(hDlgWnd
, edt2
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrReplaceWith
);
183 if(!(pdata
->fr
.Flags
& FR_SHOWHELP
))
184 ShowWindow(GetDlgItem(hDlgWnd
, pshHelp
), SW_HIDE
);
185 if(pdata
->fr
.Flags
& FR_HIDEUPDOWN
)
187 ShowWindow(GetDlgItem(hDlgWnd
, rad1
), SW_HIDE
);
188 ShowWindow(GetDlgItem(hDlgWnd
, rad2
), SW_HIDE
);
189 ShowWindow(GetDlgItem(hDlgWnd
, grp1
), SW_HIDE
);
191 else if(pdata
->fr
.Flags
& FR_NOUPDOWN
)
193 EnableWindow(GetDlgItem(hDlgWnd
, rad1
), FALSE
);
194 EnableWindow(GetDlgItem(hDlgWnd
, rad2
), FALSE
);
195 EnableWindow(GetDlgItem(hDlgWnd
, grp1
), FALSE
);
199 SendDlgItemMessageA(hDlgWnd
, rad1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? 0 : BST_CHECKED
, 0);
200 SendDlgItemMessageA(hDlgWnd
, rad2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? BST_CHECKED
: 0, 0);
203 if(pdata
->fr
.Flags
& FR_HIDEMATCHCASE
)
204 ShowWindow(GetDlgItem(hDlgWnd
, chx2
), SW_HIDE
);
205 else if(pdata
->fr
.Flags
& FR_NOMATCHCASE
)
206 EnableWindow(GetDlgItem(hDlgWnd
, chx2
), FALSE
);
208 SendDlgItemMessageA(hDlgWnd
, chx2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_MATCHCASE
? BST_CHECKED
: 0, 0);
210 if(pdata
->fr
.Flags
& FR_HIDEWHOLEWORD
)
211 ShowWindow(GetDlgItem(hDlgWnd
, chx1
), SW_HIDE
);
212 else if(pdata
->fr
.Flags
& FR_NOWHOLEWORD
)
213 EnableWindow(GetDlgItem(hDlgWnd
, chx1
), FALSE
);
215 SendDlgItemMessageA(hDlgWnd
, chx1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_WHOLEWORD
? BST_CHECKED
: 0, 0);
217 /* We did the init here, now call the hook if requested */
219 /* We do not do ShowWindow if hook exists and is FALSE */
220 /* per MSDN Article Q96135 */
221 if((pdata
->fr
.Flags
& FR_ENABLEHOOK
)
222 && ! pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, (LPARAM
) &pdata
->fr
))
224 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
225 UpdateWindow(hDlgWnd
);
229 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
231 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
242 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
246 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
251 FIXME("Got WM_HELP. Who is gonna supply it?\n");
256 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
258 /* FIXME: Handle F1 help */
261 retval
= FALSE
; /* We did not handle the message */
265 /* WM_DESTROY is a special case.
266 * We need to ensure that the allocated memory is freed just before
267 * the dialog is killed. We also need to remove the added prop.
269 if(iMsg
== WM_DESTROY
)
271 RemovePropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
272 HeapFree(GetProcessHeap(), 0, pdata
);
278 /***********************************************************************
279 * COMDLG32_FR_CheckPartial [internal]
280 * Check various fault conditions in the supplied parameters that
281 * cause an extended error to be reported.
286 static BOOL
COMDLG32_FR_CheckPartial(
287 LPFINDREPLACEA pfr
, /* [in] Find structure */
288 BOOL Replace
/* [in] True if called as replace */
292 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES
);
296 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
298 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
302 if(!IsWindow(pfr
->hwndOwner
))
304 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
308 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
309 ||(Replace
&& (pfr
->wReplaceWithLen
< 1 || !pfr
->lpstrReplaceWith
)))
311 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
315 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRINGA
)) == 0)
317 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
320 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0)
322 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
326 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
328 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
332 if((pfr
->Flags
& (FR_ENABLETEMPLATE
| FR_ENABLETEMPLATEHANDLE
)) && !pfr
->hInstance
)
334 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
338 if((pfr
->Flags
& FR_ENABLETEMPLATE
) && !pfr
->lpTemplateName
)
340 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE
);
347 /***********************************************************************
348 * COMDLG32_FR_DoFindReplace [internal]
349 * Actual load and creation of the Find/Replace dialog.
351 * Window handle to created dialog:Success
354 static HWND
COMDLG32_FR_DoFindReplace(
355 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
362 TRACE("hInst=%08x, Flags=%08lx\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
364 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
366 HMODULE hmod
= COMDLG32_hInstance
;
368 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
370 hmod
= (HMODULE
)pdata
->fr
.hInstance
;
371 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
373 htemplate
= FindResourceW(hmod
, (LPWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOGA
);
377 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOGA
);
382 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
384 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOGA
);
388 error
= CDERR_FINDRESFAILURE
;
392 loadrc
= LoadResource(hmod
, htemplate
);
396 loadrc
= (HGLOBAL
)pdata
->fr
.hInstance
;
401 error
= CDERR_LOADRESFAILURE
;
405 if((rcs
= (LPDLGTEMPLATEW
)LockResource(loadrc
)) == NULL
)
407 error
= CDERR_LOCKRESFAILURE
;
411 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
414 (DLGPROC
)COMDLG32_FindReplaceDlgProc
,
418 error
= CDERR_DIALOGFAILURE
;
420 COMDLG32_SetCommDlgExtendedError(error
);
421 HeapFree(GetProcessHeap(), 0, pdata
);
426 /***********************************************************************
427 * FindTextA [COMDLG32.@]
429 * Window handle to created dialog: Success
432 HWND WINAPI
FindTextA(
433 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
435 COMDLG32_FR_Data
*pdata
;
437 TRACE("LPFINDREPLACE=%p\n", pfr
);
439 if(!COMDLG32_FR_CheckPartial(pfr
, FALSE
))
442 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
443 return 0; /* Error has been set */
445 pdata
->user_fr
.fra
= pfr
;
447 return COMDLG32_FR_DoFindReplace(pdata
);
450 /***********************************************************************
451 * ReplaceTextA [COMDLG32.@]
453 * Window handle to created dialog: Success
456 HWND WINAPI
ReplaceTextA(
457 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
459 COMDLG32_FR_Data
*pdata
;
461 TRACE("LPFINDREPLACE=%p\n", pfr
);
463 if(!COMDLG32_FR_CheckPartial(pfr
, TRUE
))
466 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
467 return 0; /* Error has been set */
469 pdata
->user_fr
.fra
= pfr
;
471 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
472 return COMDLG32_FR_DoFindReplace(pdata
);
475 /***********************************************************************
476 * FindTextW [COMDLG32.@]
478 * Window handle to created dialog: Success
481 HWND WINAPI
FindTextW(
482 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
484 COMDLG32_FR_Data
*pdata
;
487 TRACE("LPFINDREPLACE=%p\n", pfr
);
489 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
492 len
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
493 NULL
, 0, NULL
, NULL
);
494 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len
)) == NULL
)
495 return 0; /* Error has been set */
497 pdata
->user_fr
.frw
= pfr
;
498 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
499 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
500 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
501 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
502 pdata
->fr
.lpstrFindWhat
, len
, NULL
, NULL
);
503 return COMDLG32_FR_DoFindReplace(pdata
);
506 /***********************************************************************
507 * ReplaceTextW [COMDLG32.@]
509 * Window handle to created dialog: Success
512 HWND WINAPI
ReplaceTextW(
513 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
515 COMDLG32_FR_Data
*pdata
;
518 TRACE("LPFINDREPLACE=%p\n", pfr
);
520 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
523 len1
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
524 NULL
, 0, NULL
, NULL
);
525 len2
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
526 NULL
, 0, NULL
, NULL
);
527 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
)
528 + len1
+ len2
)) == NULL
)
529 return 0; /* Error has been set */
531 pdata
->user_fr
.frw
= pfr
;
532 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
533 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
534 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
535 pdata
->fr
.lpstrReplaceWith
= pdata
->fr
.lpstrFindWhat
+ len1
;
537 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
538 pdata
->fr
.lpstrFindWhat
, len1
, NULL
, NULL
);
539 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
540 pdata
->fr
.lpstrReplaceWith
, len2
, NULL
, NULL
);
541 return COMDLG32_FR_DoFindReplace(pdata
);