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
32 #include "wine/debug.h"
33 #include "wine/heap.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
40 /*-----------------------------------------------------------------------*/
42 static UINT FindReplaceMessage
;
43 static UINT HelpMessage
;
45 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
46 /* CRITICAL_SECTION COMDLG32_CritSect; */
49 * MS uses a critical section at a few locations. However, I fail to
50 * see the reason for this. Their comdlg32.dll has a few race conditions
51 * but _not_ at those places that are protected with the mutex (there are
52 * globals that seem to hold info for the wndproc).
54 * FindText[AW]/ReplaceText[AW]
55 * The find/replace calls are passed a structure that is _not_ used
56 * internally. There is a local copy that holds the running info to
57 * be able to combine xxxA and xxxW calls. The passed pointer is
58 * returned upon sendmessage. Apps won't break this way when they rely
59 * on the original pointer. This will work as long as the sizes of
60 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
61 * the app to see the wine-specific extra flags to distinguish between
62 * A/W and Find/Replace.
66 /***********************************************************************
67 * COMDLG32_FR_GetFlags [internal]
68 * Returns the button state that needs to be reported to the caller.
70 * Current state of check and radio buttons
72 static DWORD
COMDLG32_FR_GetFlags(HWND hDlgWnd
)
75 if(IsDlgButtonChecked(hDlgWnd
, rad2
) == BST_CHECKED
)
77 if(IsDlgButtonChecked(hDlgWnd
, chx1
) == BST_CHECKED
)
78 flags
|= FR_WHOLEWORD
;
79 if(IsDlgButtonChecked(hDlgWnd
, chx2
) == BST_CHECKED
)
80 flags
|= FR_MATCHCASE
;
84 /***********************************************************************
85 * COMDLG32_FR_HandleWMCommand [internal]
86 * Handle WM_COMMAND messages...
88 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd
, COMDLG32_FR_Data
*pData
, int Id
, int NotifyCode
)
92 pData
->user_fr
.fra
->Flags
&= ~FR_MASK
; /* Clear return flags */
93 if(pData
->fr
.Flags
& FR_WINE_REPLACE
) /* Replace always goes down... */
94 pData
->user_fr
.fra
->Flags
|= FR_DOWN
;
96 if(NotifyCode
== BN_CLICKED
)
100 case IDOK
: /* Find Next */
101 if(GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
103 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_FINDNEXT
;
104 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
106 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
107 pData
->user_fr
.frw
->lpstrFindWhat
,
112 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
114 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
119 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_DIALOGTERM
;
120 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
121 DestroyWindow(hDlgWnd
);
124 case psh2
: /* Replace All */
125 flag
= FR_REPLACEALL
;
128 case psh1
: /* Replace */
131 if((pData
->fr
.Flags
& FR_WINE_REPLACE
)
132 && GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
134 pData
->fr
.lpstrReplaceWith
[0] = 0; /* In case the next GetDlgItemText Fails */
135 GetDlgItemTextA(hDlgWnd
, edt2
, pData
->fr
.lpstrReplaceWith
, pData
->fr
.wReplaceWithLen
);
136 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | flag
;
137 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
139 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
140 pData
->user_fr
.frw
->lpstrFindWhat
,
142 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrReplaceWith
, -1,
143 pData
->user_fr
.frw
->lpstrReplaceWith
,
148 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
149 strcpy(pData
->user_fr
.fra
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
151 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
156 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
);
157 SendMessageA(pData
->fr
.hwndOwner
, HelpMessage
, (WPARAM
)hDlgWnd
, (LPARAM
)pData
->user_fr
.fra
);
161 else if(NotifyCode
== EN_CHANGE
&& Id
== edt1
)
163 BOOL enable
= SendDlgItemMessageA(hDlgWnd
, edt1
, WM_GETTEXTLENGTH
, 0, 0) > 0;
164 EnableWindow(GetDlgItem(hDlgWnd
, IDOK
), enable
);
165 if(pData
->fr
.Flags
& FR_WINE_REPLACE
)
167 EnableWindow(GetDlgItem(hDlgWnd
, psh1
), enable
);
168 EnableWindow(GetDlgItem(hDlgWnd
, psh2
), enable
);
173 /***********************************************************************
174 * COMDLG32_FindReplaceDlgProc [internal]
175 * [Find/Replace]Text32[A/W] window procedure.
177 static INT_PTR CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
179 COMDLG32_FR_Data
*pdata
= GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
180 INT_PTR retval
= TRUE
;
182 if(iMsg
== WM_INITDIALOG
)
184 pdata
= (COMDLG32_FR_Data
*)lParam
;
185 if(!SetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
, (HANDLE
)pdata
))
187 ERR("Could not Set prop; invent a graceful exit?...\n");
188 DestroyWindow(hDlgWnd
);
191 SendDlgItemMessageA(hDlgWnd
, edt1
, EM_SETLIMITTEXT
, pdata
->fr
.wFindWhatLen
, 0);
192 SendDlgItemMessageA(hDlgWnd
, edt1
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrFindWhat
);
193 if(pdata
->fr
.Flags
& FR_WINE_REPLACE
)
195 SendDlgItemMessageA(hDlgWnd
, edt2
, EM_SETLIMITTEXT
, pdata
->fr
.wReplaceWithLen
, 0);
196 SendDlgItemMessageA(hDlgWnd
, edt2
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrReplaceWith
);
199 if(!(pdata
->fr
.Flags
& FR_SHOWHELP
))
200 ShowWindow(GetDlgItem(hDlgWnd
, pshHelp
), SW_HIDE
);
201 if(pdata
->fr
.Flags
& FR_HIDEUPDOWN
)
203 ShowWindow(GetDlgItem(hDlgWnd
, rad1
), SW_HIDE
);
204 ShowWindow(GetDlgItem(hDlgWnd
, rad2
), SW_HIDE
);
205 ShowWindow(GetDlgItem(hDlgWnd
, grp1
), SW_HIDE
);
207 else if(pdata
->fr
.Flags
& FR_NOUPDOWN
)
209 EnableWindow(GetDlgItem(hDlgWnd
, rad1
), FALSE
);
210 EnableWindow(GetDlgItem(hDlgWnd
, rad2
), FALSE
);
211 EnableWindow(GetDlgItem(hDlgWnd
, grp1
), FALSE
);
215 SendDlgItemMessageA(hDlgWnd
, rad1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? 0 : BST_CHECKED
, 0);
216 SendDlgItemMessageA(hDlgWnd
, rad2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? BST_CHECKED
: 0, 0);
219 if(pdata
->fr
.Flags
& FR_HIDEMATCHCASE
)
220 ShowWindow(GetDlgItem(hDlgWnd
, chx2
), SW_HIDE
);
221 else if(pdata
->fr
.Flags
& FR_NOMATCHCASE
)
222 EnableWindow(GetDlgItem(hDlgWnd
, chx2
), FALSE
);
224 SendDlgItemMessageA(hDlgWnd
, chx2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_MATCHCASE
? BST_CHECKED
: 0, 0);
226 if(pdata
->fr
.Flags
& FR_HIDEWHOLEWORD
)
227 ShowWindow(GetDlgItem(hDlgWnd
, chx1
), SW_HIDE
);
228 else if(pdata
->fr
.Flags
& FR_NOWHOLEWORD
)
229 EnableWindow(GetDlgItem(hDlgWnd
, chx1
), FALSE
);
231 SendDlgItemMessageA(hDlgWnd
, chx1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_WHOLEWORD
? BST_CHECKED
: 0, 0);
233 /* We did the init here, now call the hook if requested */
235 /* We do not do ShowWindow if hook exists and is FALSE */
236 /* per MSDN Article Q96135 */
237 if((pdata
->fr
.Flags
& FR_ENABLEHOOK
)
238 && ! pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, (LPARAM
) &pdata
->fr
))
240 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
241 UpdateWindow(hDlgWnd
);
245 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
247 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
258 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
262 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
267 FIXME("Got WM_HELP. Who is gonna supply it?\n");
272 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
274 /* FIXME: Handle F1 help */
277 retval
= FALSE
; /* We did not handle the message */
281 /* WM_DESTROY is a special case.
282 * We need to ensure that the allocated memory is freed just before
283 * the dialog is killed. We also need to remove the added prop.
285 if(iMsg
== WM_DESTROY
)
287 RemovePropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
294 /***********************************************************************
295 * COMDLG32_FR_CheckPartial [internal]
296 * Check various fault conditions in the supplied parameters that
297 * cause an extended error to be reported.
302 static BOOL
COMDLG32_FR_CheckPartial(
303 const FINDREPLACEA
*pfr
, /* [in] Find structure */
304 BOOL Replace
/* [in] True if called as replace */
308 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION
);
312 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
314 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
318 if(!IsWindow(pfr
->hwndOwner
))
320 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
324 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
325 ||(Replace
&& !pfr
->lpstrReplaceWith
))
327 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
331 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRINGA
)) == 0)
333 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
336 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0)
338 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
342 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
344 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
348 if((pfr
->Flags
& FR_ENABLETEMPLATEHANDLE
) && !pfr
->hInstance
)
350 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
357 /***********************************************************************
358 * COMDLG32_FR_DoFindReplace [internal]
359 * Actual load and creation of the Find/Replace dialog.
361 * Window handle to created dialog:Success
364 static HWND
COMDLG32_FR_DoFindReplace(
365 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
372 TRACE("hInst=%p, Flags=%08lx\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
374 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
376 HMODULE hmod
= COMDLG32_hInstance
;
378 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
380 hmod
= pdata
->fr
.hInstance
;
381 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
383 htemplate
= FindResourceW(hmod
, (LPCWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOG
);
387 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOG
);
392 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
394 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOG
);
398 error
= CDERR_FINDRESFAILURE
;
402 loadrc
= LoadResource(hmod
, htemplate
);
406 loadrc
= pdata
->fr
.hInstance
;
411 error
= CDERR_LOADRESFAILURE
;
415 if((rcs
= LockResource(loadrc
)) == NULL
)
417 error
= CDERR_LOCKRESFAILURE
;
421 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
424 COMDLG32_FindReplaceDlgProc
,
428 error
= CDERR_DIALOGFAILURE
;
430 COMDLG32_SetCommDlgExtendedError(error
);
436 /***********************************************************************
437 * FindTextA [COMDLG32.@]
441 HWND WINAPI
FindTextA(
442 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
444 COMDLG32_FR_Data
*pdata
;
446 TRACE("LPFINDREPLACE=%p\n", pfr
);
448 if(!COMDLG32_FR_CheckPartial(pfr
, FALSE
))
451 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
452 return 0; /* Error has been set */
454 pdata
->user_fr
.fra
= pfr
;
456 return COMDLG32_FR_DoFindReplace(pdata
);
459 /***********************************************************************
460 * ReplaceTextA [COMDLG32.@]
464 HWND WINAPI
ReplaceTextA(
465 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
467 COMDLG32_FR_Data
*pdata
;
469 TRACE("LPFINDREPLACE=%p\n", pfr
);
471 if(!COMDLG32_FR_CheckPartial(pfr
, TRUE
))
474 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
475 return 0; /* Error has been set */
477 pdata
->user_fr
.fra
= pfr
;
479 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
480 return COMDLG32_FR_DoFindReplace(pdata
);
483 /***********************************************************************
484 * FindTextW [COMDLG32.@]
486 * Create a modeless find-text dialog box.
489 * Window handle to created dialog: Success
492 HWND WINAPI
FindTextW(
493 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
495 COMDLG32_FR_Data
*pdata
;
498 TRACE("LPFINDREPLACE=%p\n", pfr
);
500 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
503 len
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
504 NULL
, 0, NULL
, NULL
);
505 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len
)) == NULL
)
506 return 0; /* Error has been set */
508 pdata
->user_fr
.frw
= pfr
;
509 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
510 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
511 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
512 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
513 pdata
->fr
.lpstrFindWhat
, len
, NULL
, NULL
);
514 return COMDLG32_FR_DoFindReplace(pdata
);
517 /***********************************************************************
518 * ReplaceTextW [COMDLG32.@]
520 * Create a modeless replace-text dialog box.
523 * Window handle to created dialog: Success
526 HWND WINAPI
ReplaceTextW(
527 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
529 COMDLG32_FR_Data
*pdata
;
532 TRACE("LPFINDREPLACE=%p\n", pfr
);
534 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, TRUE
))
537 len1
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
538 NULL
, 0, NULL
, NULL
);
539 len2
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
540 NULL
, 0, NULL
, NULL
);
541 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len1
+ len2
)) == NULL
)
542 return 0; /* Error has been set */
544 pdata
->user_fr
.frw
= pfr
;
545 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
546 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
547 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
548 pdata
->fr
.lpstrReplaceWith
= pdata
->fr
.lpstrFindWhat
+ len1
;
550 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
551 pdata
->fr
.lpstrFindWhat
, len1
, NULL
, NULL
);
552 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
553 pdata
->fr
.lpstrReplaceWith
, len2
, NULL
, NULL
);
554 return COMDLG32_FR_DoFindReplace(pdata
);