2 * Common Dialog Boxes interface (32 bit)
5 * Copyright 1998,1999 Bertho A. Stultiens
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_NO_STATUS
34 #include <wine/debug.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
41 /*-----------------------------------------------------------------------*/
43 static UINT FindReplaceMessage
;
44 static UINT HelpMessage
;
46 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
47 /* CRITICAL_SECTION COMDLG32_CritSect; */
50 * MS uses a critical section at a few locations. However, I fail to
51 * see the reason for this. Their comdlg32.dll has a few race conditions
52 * but _not_ at those places that are protected with the mutex (there are
53 * globals that seem to hold info for the wndproc).
55 * FindText[AW]/ReplaceText[AW]
56 * The find/replace calls are passed a structure that is _not_ used
57 * internally. There is a local copy that holds the running info to
58 * be able to combine xxxA and xxxW calls. The passed pointer is
59 * returned upon sendmessage. Apps won't break this way when they rely
60 * on the original pointer. This will work as long as the sizes of
61 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
62 * the app to see the wine-specific extra flags to distinguish between
63 * A/W and Find/Replace.
67 /***********************************************************************
68 * COMDLG32_FR_GetFlags [internal]
69 * Returns the button state that needs to be reported to the caller.
71 * Current state of check and radio buttons
73 static DWORD
COMDLG32_FR_GetFlags(HWND hDlgWnd
)
76 if(IsDlgButtonChecked(hDlgWnd
, rad2
) == BST_CHECKED
)
78 if(IsDlgButtonChecked(hDlgWnd
, chx1
) == BST_CHECKED
)
79 flags
|= FR_WHOLEWORD
;
80 if(IsDlgButtonChecked(hDlgWnd
, chx2
) == BST_CHECKED
)
81 flags
|= FR_MATCHCASE
;
85 /***********************************************************************
86 * COMDLG32_FR_HandleWMCommand [internal]
87 * Handle WM_COMMAND messages...
89 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd
, COMDLG32_FR_Data
*pData
, int Id
, int NotifyCode
)
93 pData
->user_fr
.fra
->Flags
&= ~FR_MASK
; /* Clear return flags */
94 if(pData
->fr
.Flags
& FR_WINE_REPLACE
) /* Replace always goes down... */
95 pData
->user_fr
.fra
->Flags
|= FR_DOWN
;
97 if(NotifyCode
== BN_CLICKED
)
101 case IDOK
: /* Find Next */
102 if(GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
104 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_FINDNEXT
;
105 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
107 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
108 pData
->user_fr
.frw
->lpstrFindWhat
,
113 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
115 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
120 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_DIALOGTERM
;
121 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
122 DestroyWindow(hDlgWnd
);
125 case psh2
: /* Replace All */
126 flag
= FR_REPLACEALL
;
129 case psh1
: /* Replace */
132 if((pData
->fr
.Flags
& FR_WINE_REPLACE
)
133 && GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
135 pData
->fr
.lpstrReplaceWith
[0] = 0; /* In case the next GetDlgItemText Fails */
136 GetDlgItemTextA(hDlgWnd
, edt2
, pData
->fr
.lpstrReplaceWith
, pData
->fr
.wReplaceWithLen
);
137 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | flag
;
138 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
140 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
141 pData
->user_fr
.frw
->lpstrFindWhat
,
143 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrReplaceWith
, -1,
144 pData
->user_fr
.frw
->lpstrReplaceWith
,
149 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
150 strcpy(pData
->user_fr
.fra
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
152 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
157 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
);
158 SendMessageA(pData
->fr
.hwndOwner
, HelpMessage
, (WPARAM
)hDlgWnd
, (LPARAM
)pData
->user_fr
.fra
);
162 else if(NotifyCode
== EN_CHANGE
&& Id
== edt1
)
164 BOOL enable
= SendDlgItemMessageA(hDlgWnd
, edt1
, WM_GETTEXTLENGTH
, 0, 0) > 0;
165 EnableWindow(GetDlgItem(hDlgWnd
, IDOK
), enable
);
166 if(pData
->fr
.Flags
& FR_WINE_REPLACE
)
168 EnableWindow(GetDlgItem(hDlgWnd
, psh1
), enable
);
169 EnableWindow(GetDlgItem(hDlgWnd
, psh2
), enable
);
174 /***********************************************************************
175 * COMDLG32_FindReplaceDlgProc [internal]
176 * [Find/Replace]Text32[A/W] window procedure.
178 static INT_PTR CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
180 COMDLG32_FR_Data
*pdata
= GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
181 INT_PTR retval
= TRUE
;
183 if(iMsg
== WM_INITDIALOG
)
185 pdata
= (COMDLG32_FR_Data
*)lParam
;
186 if(!SetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
, (HANDLE
)pdata
))
188 ERR("Could not Set prop; invent a graceful exit?...\n");
189 DestroyWindow(hDlgWnd
);
192 SendDlgItemMessageA(hDlgWnd
, edt1
, EM_SETLIMITTEXT
, pdata
->fr
.wFindWhatLen
, 0);
193 SendDlgItemMessageA(hDlgWnd
, edt1
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrFindWhat
);
194 if(pdata
->fr
.Flags
& FR_WINE_REPLACE
)
196 SendDlgItemMessageA(hDlgWnd
, edt2
, EM_SETLIMITTEXT
, pdata
->fr
.wReplaceWithLen
, 0);
197 SendDlgItemMessageA(hDlgWnd
, edt2
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrReplaceWith
);
200 if(!(pdata
->fr
.Flags
& FR_SHOWHELP
))
201 ShowWindow(GetDlgItem(hDlgWnd
, pshHelp
), SW_HIDE
);
202 if(pdata
->fr
.Flags
& FR_HIDEUPDOWN
)
204 ShowWindow(GetDlgItem(hDlgWnd
, rad1
), SW_HIDE
);
205 ShowWindow(GetDlgItem(hDlgWnd
, rad2
), SW_HIDE
);
206 ShowWindow(GetDlgItem(hDlgWnd
, grp1
), SW_HIDE
);
208 else if(pdata
->fr
.Flags
& FR_NOUPDOWN
)
210 EnableWindow(GetDlgItem(hDlgWnd
, rad1
), FALSE
);
211 EnableWindow(GetDlgItem(hDlgWnd
, rad2
), FALSE
);
212 EnableWindow(GetDlgItem(hDlgWnd
, grp1
), FALSE
);
216 SendDlgItemMessageA(hDlgWnd
, rad1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? 0 : BST_CHECKED
, 0);
217 SendDlgItemMessageA(hDlgWnd
, rad2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? BST_CHECKED
: 0, 0);
220 if(pdata
->fr
.Flags
& FR_HIDEMATCHCASE
)
221 ShowWindow(GetDlgItem(hDlgWnd
, chx2
), SW_HIDE
);
222 else if(pdata
->fr
.Flags
& FR_NOMATCHCASE
)
223 EnableWindow(GetDlgItem(hDlgWnd
, chx2
), FALSE
);
225 SendDlgItemMessageA(hDlgWnd
, chx2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_MATCHCASE
? BST_CHECKED
: 0, 0);
227 if(pdata
->fr
.Flags
& FR_HIDEWHOLEWORD
)
228 ShowWindow(GetDlgItem(hDlgWnd
, chx1
), SW_HIDE
);
229 else if(pdata
->fr
.Flags
& FR_NOWHOLEWORD
)
230 EnableWindow(GetDlgItem(hDlgWnd
, chx1
), FALSE
);
232 SendDlgItemMessageA(hDlgWnd
, chx1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_WHOLEWORD
? BST_CHECKED
: 0, 0);
234 /* We did the init here, now call the hook if requested */
236 /* We do not do ShowWindow if hook exists and is FALSE */
237 /* per MSDN Article Q96135 */
238 if((pdata
->fr
.Flags
& FR_ENABLEHOOK
)
239 && ! pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, (LPARAM
) &pdata
->fr
))
241 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
242 UpdateWindow(hDlgWnd
);
246 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
248 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
259 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
263 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
268 FIXME("Got WM_HELP. Who is gonna supply it?\n");
273 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
275 /* FIXME: Handle F1 help */
278 retval
= FALSE
; /* We did not handle the message */
282 /* WM_DESTROY is a special case.
283 * We need to ensure that the allocated memory is freed just before
284 * the dialog is killed. We also need to remove the added prop.
286 if(iMsg
== WM_DESTROY
)
288 RemovePropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
289 HeapFree(GetProcessHeap(), 0, pdata
);
295 /***********************************************************************
296 * COMDLG32_FR_CheckPartial [internal]
297 * Check various fault conditions in the supplied parameters that
298 * cause an extended error to be reported.
303 static BOOL
COMDLG32_FR_CheckPartial(
304 const FINDREPLACEA
*pfr
, /* [in] Find structure */
305 BOOL Replace
/* [in] True if called as replace */
309 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION
);
313 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
315 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
319 if(!IsWindow(pfr
->hwndOwner
))
321 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
325 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
326 ||(Replace
&& !pfr
->lpstrReplaceWith
))
328 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
332 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRINGA
)) == 0)
334 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
337 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0)
339 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
343 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
345 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
349 if((pfr
->Flags
& FR_ENABLETEMPLATEHANDLE
) && !pfr
->hInstance
)
351 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
358 /***********************************************************************
359 * COMDLG32_FR_DoFindReplace [internal]
360 * Actual load and creation of the Find/Replace dialog.
362 * Window handle to created dialog:Success
365 static HWND
COMDLG32_FR_DoFindReplace(
366 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
373 TRACE("hInst=%p, Flags=%08x\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
375 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
377 HMODULE hmod
= COMDLG32_hInstance
;
379 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
381 hmod
= pdata
->fr
.hInstance
;
382 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
384 htemplate
= FindResourceW(hmod
, (LPCWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOG
);
388 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOG
);
393 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
395 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOG
);
399 error
= CDERR_FINDRESFAILURE
;
403 loadrc
= LoadResource(hmod
, htemplate
);
407 loadrc
= pdata
->fr
.hInstance
;
412 error
= CDERR_LOADRESFAILURE
;
416 if((rcs
= LockResource(loadrc
)) == NULL
)
418 error
= CDERR_LOCKRESFAILURE
;
422 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
425 COMDLG32_FindReplaceDlgProc
,
429 error
= CDERR_DIALOGFAILURE
;
431 COMDLG32_SetCommDlgExtendedError(error
);
432 HeapFree(GetProcessHeap(), 0, pdata
);
437 /***********************************************************************
438 * FindTextA [COMDLG32.@]
442 HWND WINAPI
FindTextA(
443 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
445 COMDLG32_FR_Data
*pdata
;
447 TRACE("LPFINDREPLACE=%p\n", pfr
);
449 if(!COMDLG32_FR_CheckPartial(pfr
, FALSE
))
452 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
453 return 0; /* Error has been set */
455 pdata
->user_fr
.fra
= pfr
;
457 return COMDLG32_FR_DoFindReplace(pdata
);
460 /***********************************************************************
461 * ReplaceTextA [COMDLG32.@]
465 HWND WINAPI
ReplaceTextA(
466 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
468 COMDLG32_FR_Data
*pdata
;
470 TRACE("LPFINDREPLACE=%p\n", pfr
);
472 if(!COMDLG32_FR_CheckPartial(pfr
, TRUE
))
475 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
476 return 0; /* Error has been set */
478 pdata
->user_fr
.fra
= pfr
;
480 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
481 return COMDLG32_FR_DoFindReplace(pdata
);
484 /***********************************************************************
485 * FindTextW [COMDLG32.@]
487 * Create a modeless find-text dialog box.
490 * Window handle to created dialog: Success
493 HWND WINAPI
FindTextW(
494 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
496 COMDLG32_FR_Data
*pdata
;
499 TRACE("LPFINDREPLACE=%p\n", pfr
);
501 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
504 len
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
505 NULL
, 0, NULL
, NULL
);
506 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len
)) == NULL
)
507 return 0; /* Error has been set */
509 pdata
->user_fr
.frw
= pfr
;
510 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
511 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
512 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
513 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
514 pdata
->fr
.lpstrFindWhat
, len
, NULL
, NULL
);
515 return COMDLG32_FR_DoFindReplace(pdata
);
518 /***********************************************************************
519 * ReplaceTextW [COMDLG32.@]
521 * Create a modeless replace-text dialog box.
524 * Window handle to created dialog: Success
527 HWND WINAPI
ReplaceTextW(
528 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
530 COMDLG32_FR_Data
*pdata
;
533 TRACE("LPFINDREPLACE=%p\n", pfr
);
535 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, TRUE
))
538 len1
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
539 NULL
, 0, NULL
, NULL
);
540 len2
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
541 NULL
, 0, NULL
, NULL
);
542 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len1
+ len2
)) == NULL
)
543 return 0; /* Error has been set */
545 pdata
->user_fr
.frw
= pfr
;
546 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
547 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
548 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
549 pdata
->fr
.lpstrReplaceWith
= pdata
->fr
.lpstrFindWhat
+ len1
;
551 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
552 pdata
->fr
.lpstrFindWhat
, len1
, NULL
, NULL
);
553 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
554 pdata
->fr
.lpstrReplaceWith
, len2
, NULL
, NULL
);
555 return COMDLG32_FR_DoFindReplace(pdata
);