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"
34 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
39 /*-----------------------------------------------------------------------*/
41 static UINT FindReplaceMessage
;
42 static UINT HelpMessage
;
44 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
45 /* CRITICAL_SECTION COMDLG32_CritSect; */
48 * MS uses a critical section at a few locations. However, I fail to
49 * see the reason for this. Their comdlg32.dll has a few race conditions
50 * but _not_ at those places that are protected with the mutex (there are
51 * globals that seem to hold info for the wndproc).
53 * FindText[AW]/ReplaceText[AW]
54 * The find/replace calls are passed a structure that is _not_ used
55 * internally. There is a local copy that holds the running info to
56 * be able to combine xxxA and xxxW calls. The passed pointer is
57 * returned upon sendmessage. Apps won't break this way when they rely
58 * on the original pointer. This will work as long as the sizes of
59 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
60 * the app to see the wine-specific extra flags to distinguish between
61 * A/W and Find/Replace.
65 /***********************************************************************
66 * COMDLG32_FR_GetFlags [internal]
67 * Returns the button state that needs to be reported to the caller.
69 * Current state of check and radio buttons
71 static DWORD
COMDLG32_FR_GetFlags(HWND hDlgWnd
)
74 if(IsDlgButtonChecked(hDlgWnd
, rad2
) == BST_CHECKED
)
76 if(IsDlgButtonChecked(hDlgWnd
, chx1
) == BST_CHECKED
)
77 flags
|= FR_WHOLEWORD
;
78 if(IsDlgButtonChecked(hDlgWnd
, chx2
) == BST_CHECKED
)
79 flags
|= FR_MATCHCASE
;
83 /***********************************************************************
84 * COMDLG32_FR_HandleWMCommand [internal]
85 * Handle WM_COMMAND messages...
87 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd
, COMDLG32_FR_Data
*pData
, int Id
, int NotifyCode
)
91 pData
->user_fr
.fra
->Flags
&= ~FR_MASK
; /* Clear return flags */
92 if(pData
->fr
.Flags
& FR_WINE_REPLACE
) /* Replace always goes down... */
93 pData
->user_fr
.fra
->Flags
|= FR_DOWN
;
95 if(NotifyCode
== BN_CLICKED
)
99 case IDOK
: /* Find Next */
100 if(GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
102 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_FINDNEXT
;
103 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
105 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
106 pData
->user_fr
.frw
->lpstrFindWhat
,
111 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
113 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
118 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | FR_DIALOGTERM
;
119 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
120 DestroyWindow(hDlgWnd
);
123 case psh2
: /* Replace All */
124 flag
= FR_REPLACEALL
;
127 case psh1
: /* Replace */
130 if((pData
->fr
.Flags
& FR_WINE_REPLACE
)
131 && GetDlgItemTextA(hDlgWnd
, edt1
, pData
->fr
.lpstrFindWhat
, pData
->fr
.wFindWhatLen
) > 0)
133 pData
->fr
.lpstrReplaceWith
[0] = 0; /* In case the next GetDlgItemText Fails */
134 GetDlgItemTextA(hDlgWnd
, edt2
, pData
->fr
.lpstrReplaceWith
, pData
->fr
.wReplaceWithLen
);
135 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
) | flag
;
136 if(pData
->fr
.Flags
& FR_WINE_UNICODE
)
138 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrFindWhat
, -1,
139 pData
->user_fr
.frw
->lpstrFindWhat
,
141 MultiByteToWideChar( CP_ACP
, 0, pData
->fr
.lpstrReplaceWith
, -1,
142 pData
->user_fr
.frw
->lpstrReplaceWith
,
147 strcpy(pData
->user_fr
.fra
->lpstrFindWhat
, pData
->fr
.lpstrFindWhat
);
148 strcpy(pData
->user_fr
.fra
->lpstrReplaceWith
, pData
->fr
.lpstrReplaceWith
);
150 SendMessageA(pData
->fr
.hwndOwner
, FindReplaceMessage
, 0, (LPARAM
)pData
->user_fr
.fra
);
155 pData
->user_fr
.fra
->Flags
|= COMDLG32_FR_GetFlags(hDlgWnd
);
156 SendMessageA(pData
->fr
.hwndOwner
, HelpMessage
, (WPARAM
)hDlgWnd
, (LPARAM
)pData
->user_fr
.fra
);
160 else if(NotifyCode
== EN_CHANGE
&& Id
== edt1
)
162 BOOL enable
= SendDlgItemMessageA(hDlgWnd
, edt1
, WM_GETTEXTLENGTH
, 0, 0) > 0;
163 EnableWindow(GetDlgItem(hDlgWnd
, IDOK
), enable
);
164 if(pData
->fr
.Flags
& FR_WINE_REPLACE
)
166 EnableWindow(GetDlgItem(hDlgWnd
, psh1
), enable
);
167 EnableWindow(GetDlgItem(hDlgWnd
, psh2
), enable
);
172 /***********************************************************************
173 * COMDLG32_FindReplaceDlgProc [internal]
174 * [Find/Replace]Text32[A/W] window procedure.
176 static INT_PTR CALLBACK
COMDLG32_FindReplaceDlgProc(HWND hDlgWnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
178 COMDLG32_FR_Data
*pdata
= GetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
179 INT_PTR retval
= TRUE
;
181 if(iMsg
== WM_INITDIALOG
)
183 pdata
= (COMDLG32_FR_Data
*)lParam
;
184 if(!SetPropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
, (HANDLE
)pdata
))
186 ERR("Could not Set prop; invent a gracefull exit?...\n");
187 DestroyWindow(hDlgWnd
);
190 SendDlgItemMessageA(hDlgWnd
, edt1
, EM_SETLIMITTEXT
, (WPARAM
)pdata
->fr
.wFindWhatLen
, 0);
191 SendDlgItemMessageA(hDlgWnd
, edt1
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrFindWhat
);
192 if(pdata
->fr
.Flags
& FR_WINE_REPLACE
)
194 SendDlgItemMessageA(hDlgWnd
, edt2
, EM_SETLIMITTEXT
, (WPARAM
)pdata
->fr
.wReplaceWithLen
, 0);
195 SendDlgItemMessageA(hDlgWnd
, edt2
, WM_SETTEXT
, 0, (LPARAM
)pdata
->fr
.lpstrReplaceWith
);
198 if(!(pdata
->fr
.Flags
& FR_SHOWHELP
))
199 ShowWindow(GetDlgItem(hDlgWnd
, pshHelp
), SW_HIDE
);
200 if(pdata
->fr
.Flags
& FR_HIDEUPDOWN
)
202 ShowWindow(GetDlgItem(hDlgWnd
, rad1
), SW_HIDE
);
203 ShowWindow(GetDlgItem(hDlgWnd
, rad2
), SW_HIDE
);
204 ShowWindow(GetDlgItem(hDlgWnd
, grp1
), SW_HIDE
);
206 else if(pdata
->fr
.Flags
& FR_NOUPDOWN
)
208 EnableWindow(GetDlgItem(hDlgWnd
, rad1
), FALSE
);
209 EnableWindow(GetDlgItem(hDlgWnd
, rad2
), FALSE
);
210 EnableWindow(GetDlgItem(hDlgWnd
, grp1
), FALSE
);
214 SendDlgItemMessageA(hDlgWnd
, rad1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? 0 : BST_CHECKED
, 0);
215 SendDlgItemMessageA(hDlgWnd
, rad2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_DOWN
? BST_CHECKED
: 0, 0);
218 if(pdata
->fr
.Flags
& FR_HIDEMATCHCASE
)
219 ShowWindow(GetDlgItem(hDlgWnd
, chx2
), SW_HIDE
);
220 else if(pdata
->fr
.Flags
& FR_NOMATCHCASE
)
221 EnableWindow(GetDlgItem(hDlgWnd
, chx2
), FALSE
);
223 SendDlgItemMessageA(hDlgWnd
, chx2
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_MATCHCASE
? BST_CHECKED
: 0, 0);
225 if(pdata
->fr
.Flags
& FR_HIDEWHOLEWORD
)
226 ShowWindow(GetDlgItem(hDlgWnd
, chx1
), SW_HIDE
);
227 else if(pdata
->fr
.Flags
& FR_NOWHOLEWORD
)
228 EnableWindow(GetDlgItem(hDlgWnd
, chx1
), FALSE
);
230 SendDlgItemMessageA(hDlgWnd
, chx1
, BM_SETCHECK
, pdata
->fr
.Flags
& FR_WHOLEWORD
? BST_CHECKED
: 0, 0);
232 /* We did the init here, now call the hook if requested */
234 /* We do not do ShowWindow if hook exists and is FALSE */
235 /* per MSDN Article Q96135 */
236 if((pdata
->fr
.Flags
& FR_ENABLEHOOK
)
237 && ! pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, (LPARAM
) &pdata
->fr
))
239 ShowWindow(hDlgWnd
, SW_SHOWNORMAL
);
240 UpdateWindow(hDlgWnd
);
244 if(pdata
&& (pdata
->fr
.Flags
& FR_ENABLEHOOK
))
246 retval
= pdata
->fr
.lpfnHook(hDlgWnd
, iMsg
, wParam
, lParam
);
257 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, LOWORD(wParam
), HIWORD(wParam
));
261 COMDLG32_FR_HandleWMCommand(hDlgWnd
, pdata
, IDCANCEL
, BN_CLICKED
);
266 FIXME("Got WM_HELP. Who is gonna supply it?\n");
271 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
273 /* FIXME: Handle F1 help */
276 retval
= FALSE
; /* We did not handle the message */
280 /* WM_DESTROY is a special case.
281 * We need to ensure that the allocated memory is freed just before
282 * the dialog is killed. We also need to remove the added prop.
284 if(iMsg
== WM_DESTROY
)
286 RemovePropA(hDlgWnd
, (LPSTR
)COMDLG32_Atom
);
287 HeapFree(GetProcessHeap(), 0, pdata
);
293 /***********************************************************************
294 * COMDLG32_FR_CheckPartial [internal]
295 * Check various fault conditions in the supplied parameters that
296 * cause an extended error to be reported.
301 static BOOL
COMDLG32_FR_CheckPartial(
302 const FINDREPLACEA
*pfr
, /* [in] Find structure */
303 BOOL Replace
/* [in] True if called as replace */
307 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES
);
311 if(pfr
->lStructSize
!= sizeof(FINDREPLACEA
))
313 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
317 if(!IsWindow(pfr
->hwndOwner
))
319 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE
);
323 if((pfr
->wFindWhatLen
< 1 || !pfr
->lpstrFindWhat
)
324 ||(Replace
&& (pfr
->wReplaceWithLen
< 1 || !pfr
->lpstrReplaceWith
)))
326 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO
);
330 if((FindReplaceMessage
= RegisterWindowMessageA(FINDMSGSTRINGA
)) == 0)
332 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
335 if((HelpMessage
= RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0)
337 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
341 if((pfr
->Flags
& FR_ENABLEHOOK
) && !pfr
->lpfnHook
)
343 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK
);
347 if((pfr
->Flags
& (FR_ENABLETEMPLATE
| FR_ENABLETEMPLATEHANDLE
)) && !pfr
->hInstance
)
349 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE
);
353 if((pfr
->Flags
& FR_ENABLETEMPLATE
) && !pfr
->lpTemplateName
)
355 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE
);
362 /***********************************************************************
363 * COMDLG32_FR_DoFindReplace [internal]
364 * Actual load and creation of the Find/Replace dialog.
366 * Window handle to created dialog:Success
369 static HWND
COMDLG32_FR_DoFindReplace(
370 COMDLG32_FR_Data
*pdata
/* [in] Internal data structure */
377 TRACE("hInst=%p, Flags=%08x\n", pdata
->fr
.hInstance
, pdata
->fr
.Flags
);
379 if(!(pdata
->fr
.Flags
& FR_ENABLETEMPLATEHANDLE
))
381 HMODULE hmod
= COMDLG32_hInstance
;
383 if(pdata
->fr
.Flags
& FR_ENABLETEMPLATE
)
385 hmod
= pdata
->fr
.hInstance
;
386 if(pdata
->fr
.Flags
& FR_WINE_UNICODE
)
388 htemplate
= FindResourceW(hmod
, (LPCWSTR
)pdata
->fr
.lpTemplateName
, (LPWSTR
)RT_DIALOG
);
392 htemplate
= FindResourceA(hmod
, pdata
->fr
.lpTemplateName
, (LPCSTR
)RT_DIALOG
);
397 int rcid
= pdata
->fr
.Flags
& FR_WINE_REPLACE
? REPLACEDLGORD
399 htemplate
= FindResourceA(hmod
, MAKEINTRESOURCEA(rcid
), (LPCSTR
)RT_DIALOG
);
403 error
= CDERR_FINDRESFAILURE
;
407 loadrc
= LoadResource(hmod
, htemplate
);
411 loadrc
= pdata
->fr
.hInstance
;
416 error
= CDERR_LOADRESFAILURE
;
420 if((rcs
= LockResource(loadrc
)) == NULL
)
422 error
= CDERR_LOCKRESFAILURE
;
426 hdlgwnd
= CreateDialogIndirectParamA(COMDLG32_hInstance
,
429 COMDLG32_FindReplaceDlgProc
,
433 error
= CDERR_DIALOGFAILURE
;
435 COMDLG32_SetCommDlgExtendedError(error
);
436 HeapFree(GetProcessHeap(), 0, pdata
);
441 /***********************************************************************
442 * FindTextA [COMDLG32.@]
446 HWND WINAPI
FindTextA(
447 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
449 COMDLG32_FR_Data
*pdata
;
451 TRACE("LPFINDREPLACE=%p\n", pfr
);
453 if(!COMDLG32_FR_CheckPartial(pfr
, FALSE
))
456 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
457 return 0; /* Error has been set */
459 pdata
->user_fr
.fra
= pfr
;
461 return COMDLG32_FR_DoFindReplace(pdata
);
464 /***********************************************************************
465 * ReplaceTextA [COMDLG32.@]
469 HWND WINAPI
ReplaceTextA(
470 LPFINDREPLACEA pfr
/* [in] Find/replace structure*/
472 COMDLG32_FR_Data
*pdata
;
474 TRACE("LPFINDREPLACE=%p\n", pfr
);
476 if(!COMDLG32_FR_CheckPartial(pfr
, TRUE
))
479 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
))) == NULL
)
480 return 0; /* Error has been set */
482 pdata
->user_fr
.fra
= pfr
;
484 pdata
->fr
.Flags
|= FR_WINE_REPLACE
;
485 return COMDLG32_FR_DoFindReplace(pdata
);
488 /***********************************************************************
489 * FindTextW [COMDLG32.@]
491 * Create a modeless find-text dialog box.
494 * Window handle to created dialog: Success
497 HWND WINAPI
FindTextW(
498 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
500 COMDLG32_FR_Data
*pdata
;
503 TRACE("LPFINDREPLACE=%p\n", pfr
);
505 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
508 len
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
509 NULL
, 0, NULL
, NULL
);
510 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len
)) == NULL
)
511 return 0; /* Error has been set */
513 pdata
->user_fr
.frw
= pfr
;
514 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
515 pdata
->fr
.Flags
|= FR_WINE_UNICODE
;
516 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
517 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
518 pdata
->fr
.lpstrFindWhat
, len
, NULL
, NULL
);
519 return COMDLG32_FR_DoFindReplace(pdata
);
522 /***********************************************************************
523 * ReplaceTextW [COMDLG32.@]
525 * Create a modeless replace-text dialog box.
528 * Window handle to created dialog: Success
531 HWND WINAPI
ReplaceTextW(
532 LPFINDREPLACEW pfr
/* [in] Find/replace structure*/
534 COMDLG32_FR_Data
*pdata
;
537 TRACE("LPFINDREPLACE=%p\n", pfr
);
539 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA
)pfr
, FALSE
))
542 len1
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
543 NULL
, 0, NULL
, NULL
);
544 len2
= WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
545 NULL
, 0, NULL
, NULL
);
546 if((pdata
= COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data
) + len1
+ len2
)) == NULL
)
547 return 0; /* Error has been set */
549 pdata
->user_fr
.frw
= pfr
;
550 pdata
->fr
= *(LPFINDREPLACEA
)pfr
; /* FINDREPLACEx have same size */
551 pdata
->fr
.Flags
|= FR_WINE_REPLACE
| FR_WINE_UNICODE
;
552 pdata
->fr
.lpstrFindWhat
= (LPSTR
)(pdata
+ 1); /* Set string pointer */
553 pdata
->fr
.lpstrReplaceWith
= pdata
->fr
.lpstrFindWhat
+ len1
;
555 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrFindWhat
, pfr
->wFindWhatLen
,
556 pdata
->fr
.lpstrFindWhat
, len1
, NULL
, NULL
);
557 WideCharToMultiByte( CP_ACP
, 0, pfr
->lpstrReplaceWith
, pfr
->wReplaceWithLen
,
558 pdata
->fr
.lpstrReplaceWith
, len2
, NULL
, NULL
);
559 return COMDLG32_FR_DoFindReplace(pdata
);