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
24 #define COM_NO_WINDOWS_H
29 //#include "winbase.h"
36 #include <wine/debug.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
43 /*-----------------------------------------------------------------------*/
45 static UINT FindReplaceMessage
;
46 static UINT HelpMessage
;
48 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
49 /* CRITICAL_SECTION COMDLG32_CritSect; */
52 * MS uses a critical section at a few locations. However, I fail to
53 * see the reason for this. Their comdlg32.dll has a few race conditions
54 * but _not_ at those places that are protected with the mutex (there are
55 * globals that seem to hold info for the wndproc).
57 * FindText[AW]/ReplaceText[AW]
58 * The find/replace calls are passed a structure that is _not_ used
59 * internally. There is a local copy that holds the running info to
60 * be able to combine xxxA and xxxW calls. The passed pointer is
61 * returned upon sendmessage. Apps won't break this way when they rely
62 * on the original pointer. This will work as long as the sizes of
63 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
64 * the app to see the wine-specific extra flags to distinguish between
65 * A/W and Find/Replace.
69 /***********************************************************************
70 * COMDLG32_FR_GetFlags [internal]
71 * Returns the button state that needs to be reported to the caller.
73 * Current state of check and radio buttons
75 static DWORD
COMDLG32_FR_GetFlags(HWND hDlgWnd
)
78 if(IsDlgButtonChecked(hDlgWnd
, rad2
) == BST_CHECKED
)
80 if(IsDlgButtonChecked(hDlgWnd
, chx1
) == BST_CHECKED
)
81 flags
|= FR_WHOLEWORD
;
82 if(IsDlgButtonChecked(hDlgWnd
, chx2
) == BST_CHECKED
)
83 flags
|= FR_MATCHCASE
;
87 /***********************************************************************
88 * COMDLG32_FR_HandleWMCommand [internal]
89 * Handle WM_COMMAND messages...
91 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd
, COMDLG32_FR_Data
*pData
, int Id
, int NotifyCode
)
95 pData
->user_fr
.fra
->Flags
&= ~FR_MASK
; /* Clear return flags */
96 if(pData
->fr
.Flags
& FR_WINE_REPLACE
) /* Replace always goes down... */
97 pData
->user_fr
.fra
->Flags
|= FR_DOWN
;
99 if(NotifyCode
== BN_CLICKED
)
103 case IDOK
: /* Find Next */
104 if(GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
106 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_FINDNEXT
;
107 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
109 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
110 pData
->user_fr
.frw
->lpstrFindWhat
,
115 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
117 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
122 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_DIALOGTERM
;
123 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
124 DestroyWindow(hDlgWnd
);
127 case psh2
: /* Replace All */
128 flag
= FR_REPLACEALL
;
131 case psh1
: /* Replace */
134 if((pData
->fr
.Flags
& FR_WINE_REPLACE
)
135 && GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
137 pData
->fr
.lpstrReplaceWith
[0] = 0; /* In case the next GetDlgItemText Fails */
138 GetDlgItemTextA(hDlgWnd
, edt2
, pData
->fr
.lpstrReplaceWith
, pData
->fr
.wReplaceWithLen
);
139 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | flag
;
140 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
142 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
143 pData
->user_fr
.frw
->lpstrFindWhat
,
145 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrReplaceWith
, -1,
146 pData
->user_fr
.frw
->lpstrReplaceWith
,
151 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
152 strcpy(pData
->user_fr
.fra
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
154 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
159 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
);
160 SendMessageA(pData
->fr
.hwndOwner
, HelpMessage
, (WPARAM
)hDlgWnd
, (LPARAM
)pData
->user_fr
.fra
);
164 else if(NotifyCode
== EN_CHANGE
&& Id
== edt1
)
166 BOOL enable
= SendDlgItemMessageA(hDlgWnd
, edt1
, WM_GETTEXTLENGTH
, 0, 0) > 0;
167 EnableWindow(GetDlgItem(hDlgWnd
, IDOK
), enable
);
168 if(pData
->fr
.Flags
& FR_WINE_REPLACE
)
170 EnableWindow(GetDlgItem(hDlgWnd
, psh1
), enable
);
171 EnableWindow(GetDlgItem(hDlgWnd
, psh2
), enable
);
176 /***********************************************************************
177 * COMDLG32_FindReplaceDlgProc [internal]
178 * [Find/Replace]Text32[A/W] window procedure.
180 static INT_PTR CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
182 COMDLG32_FR_Data
*pdata
= GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
183 INT_PTR retval
= TRUE
;
185 if(iMsg
== WM_INITDIALOG
)
187 pdata
= (COMDLG32_FR_Data
*)lParam
;
188 if(!SetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
, (HANDLE
)pdata
))
190 ERR("Could not Set prop; invent a graceful exit?...\n");
191 DestroyWindow(hDlgWnd
);
194 SendDlgItemMessageA(hDlgWnd
, edt1
, EM_SETLIMITTEXT
, pdata
->fr
.wFindWhatLen
, 0);
195 SendDlgItemMessageA(hDlgWnd
, edt1
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrFindWhat
);
196 if(pdata
->fr
.Flags
& FR_WINE_REPLACE
)
198 SendDlgItemMessageA(hDlgWnd
, edt2
, EM_SETLIMITTEXT
, pdata
->fr
.wReplaceWithLen
, 0);
199 SendDlgItemMessageA(hDlgWnd
, edt2
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrReplaceWith
);
202 if(!(pdata
->fr
.Flags
& FR_SHOWHELP
))
203 ShowWindow(GetDlgItem(hDlgWnd
, pshHelp
), SW_HIDE
);
204 if(pdata
->fr
.Flags
& FR_HIDEUPDOWN
)
206 ShowWindow(GetDlgItem(hDlgWnd
, rad1
), SW_HIDE
);
207 ShowWindow(GetDlgItem(hDlgWnd
, rad2
), SW_HIDE
);
208 ShowWindow(GetDlgItem(hDlgWnd
, grp1
), SW_HIDE
);
210 else if(pdata
->fr
.Flags
& FR_NOUPDOWN
)
212 EnableWindow(GetDlgItem(hDlgWnd
, rad1
), FALSE
);
213 EnableWindow(GetDlgItem(hDlgWnd
, rad2
), FALSE
);
214 EnableWindow(GetDlgItem(hDlgWnd
, grp1
), FALSE
);
218 SendDlgItemMessageA(hDlgWnd
, rad1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? 0 : BST_CHECKED
, 0);
219 SendDlgItemMessageA(hDlgWnd
, rad2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? BST_CHECKED
: 0, 0);
222 if(pdata
->fr
.Flags
& FR_HIDEMATCHCASE
)
223 ShowWindow(GetDlgItem(hDlgWnd
, chx2
), SW_HIDE
);
224 else if(pdata
->fr
.Flags
& FR_NOMATCHCASE
)
225 EnableWindow(GetDlgItem(hDlgWnd
, chx2
), FALSE
);
227 SendDlgItemMessageA(hDlgWnd
, chx2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_MATCHCASE
? BST_CHECKED
: 0, 0);
229 if(pdata
->fr
.Flags
& FR_HIDEWHOLEWORD
)
230 ShowWindow(GetDlgItem(hDlgWnd
, chx1
), SW_HIDE
);
231 else if(pdata
->fr
.Flags
& FR_NOWHOLEWORD
)
232 EnableWindow(GetDlgItem(hDlgWnd
, chx1
), FALSE
);
234 SendDlgItemMessageA(hDlgWnd
, chx1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_WHOLEWORD
? BST_CHECKED
: 0, 0);
236 /* We did the init here, now call the hook if requested */
238 /* We do not do ShowWindow if hook exists and is FALSE */
239 /* per MSDN Article Q96135 */
240 if((pdata
->fr
.Flags
& FR_ENABLEHOOK
)
241 && ! pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, (LPARAM
) &pdata
->fr
))
243 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
244 UpdateWindow(hDlgWnd
);
248 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
250 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
261 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
265 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
270 FIXME("Got WM_HELP. Who is gonna supply it?\n");
275 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
277 /* FIXME: Handle F1 help */
280 retval
= FALSE
; /* We did not handle the message */
284 /* WM_DESTROY is a special case.
285 * We need to ensure that the allocated memory is freed just before
286 * the dialog is killed. We also need to remove the added prop.
288 if(iMsg
== WM_DESTROY
)
290 RemovePropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
291 HeapFree(GetProcessHeap(), 0, pdata
);
297 /***********************************************************************
298 * COMDLG32_FR_CheckPartial [internal]
299 * Check various fault conditions in the supplied parameters that
300 * cause an extended error to be reported.
305 static BOOL
COMDLG32_FR_CheckPartial(
306 const FINDREPLACEA
*pfr
, /* [in] Find structure */
307 BOOL Replace
/* [in] True if called as replace */
311 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION
);
315 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
317 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
321 if(!IsWindow(pfr
->hwndOwner
))
323 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
327 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
328 ||(Replace
&& !pfr
->lpstrReplaceWith
))
330 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
334 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRINGA
)) == 0)
336 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
339 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0)
341 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
345 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
347 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
351 if((pfr
->Flags
& FR_ENABLETEMPLATEHANDLE
) && !pfr
->hInstance
)
353 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
360 /***********************************************************************
361 * COMDLG32_FR_DoFindReplace [internal]
362 * Actual load and creation of the Find/Replace dialog.
364 * Window handle to created dialog:Success
367 static HWND
COMDLG32_FR_DoFindReplace(
368 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
375 TRACE("hInst=%p, Flags=%08x\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
377 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
379 HMODULE hmod
= COMDLG32_hInstance
;
381 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
383 hmod
= pdata
->fr
.hInstance
;
384 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
386 htemplate
= FindResourceW(hmod
, (LPCWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOG
);
390 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOG
);
395 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
397 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOG
);
401 error
= CDERR_FINDRESFAILURE
;
405 loadrc
= LoadResource(hmod
, htemplate
);
409 loadrc
= pdata
->fr
.hInstance
;
414 error
= CDERR_LOADRESFAILURE
;
418 if((rcs
= LockResource(loadrc
)) == NULL
)
420 error
= CDERR_LOCKRESFAILURE
;
424 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
427 COMDLG32_FindReplaceDlgProc
,
431 error
= CDERR_DIALOGFAILURE
;
433 COMDLG32_SetCommDlgExtendedError(error
);
434 HeapFree(GetProcessHeap(), 0, pdata
);
439 /***********************************************************************
440 * FindTextA [COMDLG32.@]
444 HWND WINAPI
FindTextA(
445 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
447 COMDLG32_FR_Data
*pdata
;
449 TRACE("LPFINDREPLACE=%p\n", pfr
);
451 if(!COMDLG32_FR_CheckPartial(pfr
, FALSE
))
454 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
455 return 0; /* Error has been set */
457 pdata
->user_fr
.fra
= pfr
;
459 return COMDLG32_FR_DoFindReplace(pdata
);
462 /***********************************************************************
463 * ReplaceTextA [COMDLG32.@]
467 HWND WINAPI
ReplaceTextA(
468 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
470 COMDLG32_FR_Data
*pdata
;
472 TRACE("LPFINDREPLACE=%p\n", pfr
);
474 if(!COMDLG32_FR_CheckPartial(pfr
, TRUE
))
477 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
478 return 0; /* Error has been set */
480 pdata
->user_fr
.fra
= pfr
;
482 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
483 return COMDLG32_FR_DoFindReplace(pdata
);
486 /***********************************************************************
487 * FindTextW [COMDLG32.@]
489 * Create a modeless find-text dialog box.
492 * Window handle to created dialog: Success
495 HWND WINAPI
FindTextW(
496 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
498 COMDLG32_FR_Data
*pdata
;
501 TRACE("LPFINDREPLACE=%p\n", pfr
);
503 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
506 len
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
507 NULL
, 0, NULL
, NULL
);
508 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len
)) == NULL
)
509 return 0; /* Error has been set */
511 pdata
->user_fr
.frw
= pfr
;
512 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
513 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
514 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
515 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
516 pdata
->fr
.lpstrFindWhat
, len
, NULL
, NULL
);
517 return COMDLG32_FR_DoFindReplace(pdata
);
520 /***********************************************************************
521 * ReplaceTextW [COMDLG32.@]
523 * Create a modeless replace-text dialog box.
526 * Window handle to created dialog: Success
529 HWND WINAPI
ReplaceTextW(
530 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
532 COMDLG32_FR_Data
*pdata
;
535 TRACE("LPFINDREPLACE=%p\n", pfr
);
537 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, TRUE
))
540 len1
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
541 NULL
, 0, NULL
, NULL
);
542 len2
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
543 NULL
, 0, NULL
, NULL
);
544 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len1
+ len2
)) == NULL
)
545 return 0; /* Error has been set */
547 pdata
->user_fr
.frw
= pfr
;
548 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
549 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
550 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
551 pdata
->fr
.lpstrReplaceWith
= pdata
->fr
.lpstrFindWhat
+ len1
;
553 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
554 pdata
->fr
.lpstrFindWhat
, len1
, NULL
, NULL
);
555 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
556 pdata
->fr
.lpstrReplaceWith
, len2
, NULL
, NULL
);
557 return COMDLG32_FR_DoFindReplace(pdata
);