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
24 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
26 /*-----------------------------------------------------------------------*/
28 static UINT FindReplaceMessage
;
29 static UINT HelpMessage
;
31 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
32 /* CRITICAL_SECTION COMDLG32_CritSect; */
35 * MS uses a critical section at a few locations. However, I fail to
36 * see the reason for this. Their comdlg32.dll has a few race conditions
37 * but _not_ at those places that are protected with the mutex (there are
38 * globals that seem to hold info for the wndproc).
40 * FindText[AW]/ReplaceText[AW]
41 * The find/replace calls are passed a structure that is _not_ used
42 * internally. There is a local copy that holds the running info to
43 * be able to combine xxxA and xxxW calls. The passed pointer is
44 * returned upon sendmessage. Apps won't break this way when they rely
45 * on the original pointer. This will work as long as the sizes of
46 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
47 * the app to see the wine-specific extra flags to distinguish between
48 * A/W and Find/Replace.
52 /***********************************************************************
53 * COMDLG32_FR_GetFlags [internal]
54 * Returns the button state that needs to be reported to the caller.
56 * Current state of check and radio buttons
58 static DWORD
COMDLG32_FR_GetFlags(HWND hDlgWnd
)
61 if(IsDlgButtonChecked(hDlgWnd
, rad2
) == BST_CHECKED
)
63 if(IsDlgButtonChecked(hDlgWnd
, chx1
) == BST_CHECKED
)
64 flags
|= FR_WHOLEWORD
;
65 if(IsDlgButtonChecked(hDlgWnd
, chx2
) == BST_CHECKED
)
66 flags
|= FR_MATCHCASE
;
70 /***********************************************************************
71 * COMDLG32_FR_HandleWMCommand [internal]
72 * Handle WM_COMMAND messages...
74 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd
, COMDLG32_FR_Data
*pData
, int Id
, int NotifyCode
)
78 pData
->user_fr
.fra
->Flags
&= ~FR_MASK
; /* Clear return flags */
79 if(pData
->fr
.Flags
& FR_WINE_REPLACE
) /* Replace always goes down... */
80 pData
->user_fr
.fra
->Flags
|= FR_DOWN
;
82 if(NotifyCode
== BN_CLICKED
)
86 case IDOK
: /* Find Next */
87 if(GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
89 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_FINDNEXT
;
90 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
92 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
93 pData
->user_fr
.frw
->lpstrFindWhat
,
98 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
100 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
105 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_DIALOGTERM
;
106 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
107 DestroyWindow(hDlgWnd
);
110 case psh2
: /* Replace All */
111 flag
= FR_REPLACEALL
;
114 case psh1
: /* Replace */
117 if((pData
->fr
.Flags
& FR_WINE_REPLACE
)
118 && GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
120 pData
->fr
.lpstrReplaceWith
[0] = 0; /* In case the next GetDlgItemText Fails */
121 GetDlgItemTextA(hDlgWnd
, edt2
, pData
->fr
.lpstrReplaceWith
, pData
->fr
.wReplaceWithLen
);
122 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | flag
;
123 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
125 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
126 pData
->user_fr
.frw
->lpstrFindWhat
,
128 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrReplaceWith
, -1,
129 pData
->user_fr
.frw
->lpstrReplaceWith
,
134 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
135 strcpy(pData
->user_fr
.fra
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
137 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
142 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
);
143 SendMessageA(pData
->fr
.hwndOwner
, HelpMessage
, (WPARAM
)hDlgWnd
, (LPARAM
)pData
->user_fr
.fra
);
147 else if(NotifyCode
== EN_CHANGE
&& Id
== edt1
)
149 BOOL enable
= SendDlgItemMessageA(hDlgWnd
, edt1
, WM_GETTEXTLENGTH
, 0, 0) > 0;
150 EnableWindow(GetDlgItem(hDlgWnd
, IDOK
), enable
);
151 if(pData
->fr
.Flags
& FR_WINE_REPLACE
)
153 EnableWindow(GetDlgItem(hDlgWnd
, psh1
), enable
);
154 EnableWindow(GetDlgItem(hDlgWnd
, psh2
), enable
);
159 /***********************************************************************
160 * COMDLG32_FindReplaceDlgProc [internal]
161 * [Find/Replace]Text32[A/W] window procedure.
163 static INT_PTR CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
165 COMDLG32_FR_Data
*pdata
= (COMDLG32_FR_Data
*)GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
166 INT_PTR retval
= TRUE
;
168 if(iMsg
== WM_INITDIALOG
)
170 pdata
= (COMDLG32_FR_Data
*)lParam
;
171 if(!SetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
, (HANDLE
)pdata
))
173 ERR("Could not Set prop; invent a gracefull exit?...\n");
174 DestroyWindow(hDlgWnd
);
177 SendDlgItemMessageA(hDlgWnd
, edt1
, EM_SETLIMITTEXT
, (WPARAM
)pdata
->fr
.wFindWhatLen
, 0);
178 SendDlgItemMessageA(hDlgWnd
, edt1
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrFindWhat
);
179 if(pdata
->fr
.Flags
& FR_WINE_REPLACE
)
181 SendDlgItemMessageA(hDlgWnd
, edt2
, EM_SETLIMITTEXT
, (WPARAM
)pdata
->fr
.wReplaceWithLen
, 0);
182 SendDlgItemMessageA(hDlgWnd
, edt2
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrReplaceWith
);
185 if(!(pdata
->fr
.Flags
& FR_SHOWHELP
))
186 ShowWindow(GetDlgItem(hDlgWnd
, pshHelp
), SW_HIDE
);
187 if(pdata
->fr
.Flags
& FR_HIDEUPDOWN
)
189 ShowWindow(GetDlgItem(hDlgWnd
, rad1
), SW_HIDE
);
190 ShowWindow(GetDlgItem(hDlgWnd
, rad2
), SW_HIDE
);
191 ShowWindow(GetDlgItem(hDlgWnd
, grp1
), SW_HIDE
);
193 else if(pdata
->fr
.Flags
& FR_NOUPDOWN
)
195 EnableWindow(GetDlgItem(hDlgWnd
, rad1
), FALSE
);
196 EnableWindow(GetDlgItem(hDlgWnd
, rad2
), FALSE
);
197 EnableWindow(GetDlgItem(hDlgWnd
, grp1
), FALSE
);
201 SendDlgItemMessageA(hDlgWnd
, rad1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? 0 : BST_CHECKED
, 0);
202 SendDlgItemMessageA(hDlgWnd
, rad2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? BST_CHECKED
: 0, 0);
205 if(pdata
->fr
.Flags
& FR_HIDEMATCHCASE
)
206 ShowWindow(GetDlgItem(hDlgWnd
, chx2
), SW_HIDE
);
207 else if(pdata
->fr
.Flags
& FR_NOMATCHCASE
)
208 EnableWindow(GetDlgItem(hDlgWnd
, chx2
), FALSE
);
210 SendDlgItemMessageA(hDlgWnd
, chx2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_MATCHCASE
? BST_CHECKED
: 0, 0);
212 if(pdata
->fr
.Flags
& FR_HIDEWHOLEWORD
)
213 ShowWindow(GetDlgItem(hDlgWnd
, chx1
), SW_HIDE
);
214 else if(pdata
->fr
.Flags
& FR_NOWHOLEWORD
)
215 EnableWindow(GetDlgItem(hDlgWnd
, chx1
), FALSE
);
217 SendDlgItemMessageA(hDlgWnd
, chx1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_WHOLEWORD
? BST_CHECKED
: 0, 0);
219 /* We did the init here, now call the hook if requested */
221 /* We do not do ShowWindow if hook exists and is FALSE */
222 /* per MSDN Article Q96135 */
223 if((pdata
->fr
.Flags
& FR_ENABLEHOOK
)
224 && ! pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, (LPARAM
) &pdata
->fr
))
226 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
227 UpdateWindow(hDlgWnd
);
231 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
233 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
244 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
248 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
253 FIXME("Got WM_HELP. Who is gonna supply it?\n");
258 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
260 /* FIXME: Handle F1 help */
263 retval
= FALSE
; /* We did not handle the message */
267 /* WM_DESTROY is a special case.
268 * We need to ensure that the allocated memory is freed just before
269 * the dialog is killed. We also need to remove the added prop.
271 if(iMsg
== WM_DESTROY
)
273 RemovePropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
274 HeapFree(GetProcessHeap(), 0, pdata
);
280 /***********************************************************************
281 * COMDLG32_FR_CheckPartial [internal]
282 * Check various fault conditions in the supplied parameters that
283 * cause an extended error to be reported.
288 static BOOL
COMDLG32_FR_CheckPartial(
289 LPFINDREPLACEA pfr
, /* [in] Find structure */
290 BOOL Replace
/* [in] True if called as replace */
294 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES
);
298 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
300 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
304 if(!IsWindow(pfr
->hwndOwner
))
306 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
310 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
311 ||(Replace
&& (pfr
->wReplaceWithLen
< 1 || !pfr
->lpstrReplaceWith
)))
313 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
317 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRINGA
)) == 0)
319 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
322 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0)
324 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
328 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
330 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
334 if((pfr
->Flags
& (FR_ENABLETEMPLATE
| FR_ENABLETEMPLATEHANDLE
)) && !pfr
->hInstance
)
336 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
340 if((pfr
->Flags
& FR_ENABLETEMPLATE
) && !pfr
->lpTemplateName
)
342 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE
);
349 /***********************************************************************
350 * COMDLG32_FR_DoFindReplace [internal]
351 * Actual load and creation of the Find/Replace dialog.
353 * Window handle to created dialog:Success
356 static HWND
COMDLG32_FR_DoFindReplace(
357 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
364 TRACE("hInst=%p, Flags=%08lx\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
366 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
368 HMODULE hmod
= COMDLG32_hInstance
;
370 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
372 hmod
= (HMODULE
)pdata
->fr
.hInstance
;
373 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
375 htemplate
= FindResourceW(hmod
, (LPCWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOG
);
379 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOG
);
384 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
386 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOG
);
390 error
= CDERR_FINDRESFAILURE
;
394 loadrc
= LoadResource(hmod
, htemplate
);
398 loadrc
= (HGLOBAL
)pdata
->fr
.hInstance
;
403 error
= CDERR_LOADRESFAILURE
;
407 if((rcs
= (LPDLGTEMPLATEW
)LockResource(loadrc
)) == NULL
)
409 error
= CDERR_LOCKRESFAILURE
;
413 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
416 COMDLG32_FindReplaceDlgProc
,
420 error
= CDERR_DIALOGFAILURE
;
422 COMDLG32_SetCommDlgExtendedError(error
);
423 HeapFree(GetProcessHeap(), 0, pdata
);
428 /***********************************************************************
429 * FindTextA [COMDLG32.@]
433 HWND WINAPI
FindTextA(
434 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
436 COMDLG32_FR_Data
*pdata
;
438 TRACE("LPFINDREPLACE=%p\n", pfr
);
440 if(!COMDLG32_FR_CheckPartial(pfr
, FALSE
))
443 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
444 return 0; /* Error has been set */
446 pdata
->user_fr
.fra
= pfr
;
448 return COMDLG32_FR_DoFindReplace(pdata
);
451 /***********************************************************************
452 * ReplaceTextA [COMDLG32.@]
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 * Create a modeless find-text dialog box.
481 * Window handle to created dialog: Success
484 HWND WINAPI
FindTextW(
485 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
487 COMDLG32_FR_Data
*pdata
;
490 TRACE("LPFINDREPLACE=%p\n", pfr
);
492 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
495 len
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
496 NULL
, 0, NULL
, NULL
);
497 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len
)) == NULL
)
498 return 0; /* Error has been set */
500 pdata
->user_fr
.frw
= pfr
;
501 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
502 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
503 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
504 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
505 pdata
->fr
.lpstrFindWhat
, len
, NULL
, NULL
);
506 return COMDLG32_FR_DoFindReplace(pdata
);
509 /***********************************************************************
510 * ReplaceTextW [COMDLG32.@]
512 * Create a modeless replace-text dialog box.
515 * Window handle to created dialog: Success
518 HWND WINAPI
ReplaceTextW(
519 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
521 COMDLG32_FR_Data
*pdata
;
524 TRACE("LPFINDREPLACE=%p\n", pfr
);
526 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
529 len1
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
530 NULL
, 0, NULL
, NULL
);
531 len2
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
532 NULL
, 0, NULL
, NULL
);
533 if((pdata
= (COMDLG32_FR_Data
*)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
)
534 + len1
+ len2
)) == NULL
)
535 return 0; /* Error has been set */
537 pdata
->user_fr
.frw
= pfr
;
538 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
539 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
540 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
541 pdata
->fr
.lpstrReplaceWith
= pdata
->fr
.lpstrFindWhat
+ len1
;
543 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
544 pdata
->fr
.lpstrFindWhat
, len1
, NULL
, NULL
);
545 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
546 pdata
->fr
.lpstrReplaceWith
, len2
, NULL
, NULL
);
547 return COMDLG32_FR_DoFindReplace(pdata
);