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 /*-----------------------------------------------------------------------*/
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 won't 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 INT_PTR CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
163 COMDLG32_FR_Data
*pdata
= GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
164 INT_PTR retval
= TRUE
;
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 graceful exit?...\n");
172 DestroyWindow(hDlgWnd
);
175 SendDlgItemMessageA(hDlgWnd
, edt1
, EM_SETLIMITTEXT
, 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
, 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 const FINDREPLACEA
*pfr
, /* [in] Find structure */
288 BOOL Replace
/* [in] True if called as replace */
292 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION
);
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
->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_ENABLETEMPLATEHANDLE
) && !pfr
->hInstance
)
334 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
341 /***********************************************************************
342 * COMDLG32_FR_DoFindReplace [internal]
343 * Actual load and creation of the Find/Replace dialog.
345 * Window handle to created dialog:Success
348 static HWND
COMDLG32_FR_DoFindReplace(
349 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
356 TRACE("hInst=%p, Flags=%08x\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
358 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
360 HMODULE hmod
= COMDLG32_hInstance
;
362 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
364 hmod
= pdata
->fr
.hInstance
;
365 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
367 htemplate
= FindResourceW(hmod
, (LPCWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOG
);
371 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOG
);
376 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
378 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOG
);
382 error
= CDERR_FINDRESFAILURE
;
386 loadrc
= LoadResource(hmod
, htemplate
);
390 loadrc
= pdata
->fr
.hInstance
;
395 error
= CDERR_LOADRESFAILURE
;
399 if((rcs
= LockResource(loadrc
)) == NULL
)
401 error
= CDERR_LOCKRESFAILURE
;
405 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
408 COMDLG32_FindReplaceDlgProc
,
412 error
= CDERR_DIALOGFAILURE
;
414 COMDLG32_SetCommDlgExtendedError(error
);
415 HeapFree(GetProcessHeap(), 0, pdata
);
420 /***********************************************************************
421 * FindTextA [COMDLG32.@]
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_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.@]
448 HWND WINAPI
ReplaceTextA(
449 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
451 COMDLG32_FR_Data
*pdata
;
453 TRACE("LPFINDREPLACE=%p\n", pfr
);
455 if(!COMDLG32_FR_CheckPartial(pfr
, TRUE
))
458 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
459 return 0; /* Error has been set */
461 pdata
->user_fr
.fra
= pfr
;
463 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
464 return COMDLG32_FR_DoFindReplace(pdata
);
467 /***********************************************************************
468 * FindTextW [COMDLG32.@]
470 * Create a modeless find-text dialog box.
473 * Window handle to created dialog: Success
476 HWND WINAPI
FindTextW(
477 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
479 COMDLG32_FR_Data
*pdata
;
482 TRACE("LPFINDREPLACE=%p\n", pfr
);
484 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
487 len
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
488 NULL
, 0, NULL
, NULL
);
489 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len
)) == NULL
)
490 return 0; /* Error has been set */
492 pdata
->user_fr
.frw
= pfr
;
493 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
494 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
495 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
496 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
497 pdata
->fr
.lpstrFindWhat
, len
, NULL
, NULL
);
498 return COMDLG32_FR_DoFindReplace(pdata
);
501 /***********************************************************************
502 * ReplaceTextW [COMDLG32.@]
504 * Create a modeless replace-text dialog box.
507 * Window handle to created dialog: Success
510 HWND WINAPI
ReplaceTextW(
511 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
513 COMDLG32_FR_Data
*pdata
;
516 TRACE("LPFINDREPLACE=%p\n", pfr
);
518 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, TRUE
))
521 len1
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
522 NULL
, 0, NULL
, NULL
);
523 len2
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
524 NULL
, 0, NULL
, NULL
);
525 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len1
+ len2
)) == NULL
)
526 return 0; /* Error has been set */
528 pdata
->user_fr
.frw
= pfr
;
529 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
530 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
531 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
532 pdata
->fr
.lpstrReplaceWith
= pdata
->fr
.lpstrFindWhat
+ len1
;
534 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
535 pdata
->fr
.lpstrFindWhat
, len1
, NULL
, NULL
);
536 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
537 pdata
->fr
.lpstrReplaceWith
, len2
, NULL
, NULL
);
538 return COMDLG32_FR_DoFindReplace(pdata
);