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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
38 /*-----------------------------------------------------------------------*/
40 static UINT FindReplaceMessage;
41 static UINT HelpMessage;
43 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
44 /* CRITICAL_SECTION COMDLG32_CritSect; */
47 * MS uses a critical section at a few locations. However, I fail to
48 * see the reason for this. Their comdlg32.dll has a few race conditions
49 * but _not_ at those places that are protected with the mutex (there are
50 * globals that seem to hold info for the wndproc).
52 * FindText[AW]/ReplaceText[AW]
53 * The find/replace calls are passed a structure that is _not_ used
54 * internally. There is a local copy that holds the running info to
55 * be able to combine xxxA and xxxW calls. The passed pointer is
56 * returned upon sendmessage. Apps wont break this way when they rely
57 * on the original pointer. This will work as long as the sizes of
58 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
59 * the app to see the wine-specific extra flags to distinguish between
60 * A/W and Find/Replace.
64 /***********************************************************************
65 * COMDLG32_FR_GetFlags [internal]
66 * Returns the button state that needs to be reported to the caller.
68 * Current state of check and radio buttons
70 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
73 if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
75 if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
76 flags |= FR_WHOLEWORD;
77 if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
78 flags |= FR_MATCHCASE;
82 /***********************************************************************
83 * COMDLG32_FR_HandleWMCommand [internal]
84 * Handle WM_COMMAND messages...
86 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd, COMDLG32_FR_Data *pData, int Id, int NotifyCode)
90 pData->user_fr.fra->Flags &= ~FR_MASK; /* Clear return flags */
91 if(pData->fr.Flags & FR_WINE_REPLACE) /* Replace always goes down... */
92 pData->user_fr.fra->Flags |= FR_DOWN;
94 if(NotifyCode == BN_CLICKED)
98 case IDOK: /* Find Next */
99 if(GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
101 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_FINDNEXT;
102 if(pData->fr.Flags & FR_WINE_UNICODE)
104 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
105 pData->user_fr.frw->lpstrFindWhat,
110 strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
112 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
117 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_DIALOGTERM;
118 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
119 DestroyWindow(hDlgWnd);
122 case psh2: /* Replace All */
123 flag = FR_REPLACEALL;
126 case psh1: /* Replace */
129 if((pData->fr.Flags & FR_WINE_REPLACE)
130 && GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
132 pData->fr.lpstrReplaceWith[0] = 0; /* In case the next GetDlgItemText Fails */
133 GetDlgItemTextA(hDlgWnd, edt2, pData->fr.lpstrReplaceWith, pData->fr.wReplaceWithLen);
134 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | flag;
135 if(pData->fr.Flags & FR_WINE_UNICODE)
137 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
138 pData->user_fr.frw->lpstrFindWhat,
140 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrReplaceWith, -1,
141 pData->user_fr.frw->lpstrReplaceWith,
146 strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
147 strcpy(pData->user_fr.fra->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
149 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
154 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
155 SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
159 else if(NotifyCode == EN_CHANGE && Id == edt1)
161 BOOL enable = SendDlgItemMessageA(hDlgWnd, edt1, WM_GETTEXTLENGTH, 0, 0) > 0;
162 EnableWindow(GetDlgItem(hDlgWnd, IDOK), enable);
163 if(pData->fr.Flags & FR_WINE_REPLACE)
165 EnableWindow(GetDlgItem(hDlgWnd, psh1), enable);
166 EnableWindow(GetDlgItem(hDlgWnd, psh2), enable);
171 /***********************************************************************
172 * COMDLG32_FindReplaceDlgProc [internal]
173 * [Find/Replace]Text32[A/W] window procedure.
175 static INT_PTR CALLBACK COMDLG32_FindReplaceDlgProc(HWND hDlgWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
177 COMDLG32_FR_Data *pdata = (COMDLG32_FR_Data *)GetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
178 INT_PTR retval = TRUE;
180 if(iMsg == WM_INITDIALOG)
182 pdata = (COMDLG32_FR_Data *)lParam;
183 if(!SetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom, (HANDLE)pdata))
185 ERR("Could not Set prop; invent a gracefull exit?...\n");
186 DestroyWindow(hDlgWnd);
189 SendDlgItemMessageA(hDlgWnd, edt1, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wFindWhatLen, 0);
190 SendDlgItemMessageA(hDlgWnd, edt1, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrFindWhat);
191 if(pdata->fr.Flags & FR_WINE_REPLACE)
193 SendDlgItemMessageA(hDlgWnd, edt2, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wReplaceWithLen, 0);
194 SendDlgItemMessageA(hDlgWnd, edt2, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrReplaceWith);
197 if(!(pdata->fr.Flags & FR_SHOWHELP))
198 ShowWindow(GetDlgItem(hDlgWnd, pshHelp), SW_HIDE);
199 if(pdata->fr.Flags & FR_HIDEUPDOWN)
201 ShowWindow(GetDlgItem(hDlgWnd, rad1), SW_HIDE);
202 ShowWindow(GetDlgItem(hDlgWnd, rad2), SW_HIDE);
203 ShowWindow(GetDlgItem(hDlgWnd, grp1), SW_HIDE);
205 else if(pdata->fr.Flags & FR_NOUPDOWN)
207 EnableWindow(GetDlgItem(hDlgWnd, rad1), FALSE);
208 EnableWindow(GetDlgItem(hDlgWnd, rad2), FALSE);
209 EnableWindow(GetDlgItem(hDlgWnd, grp1), FALSE);
213 SendDlgItemMessageA(hDlgWnd, rad1, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? 0 : BST_CHECKED, 0);
214 SendDlgItemMessageA(hDlgWnd, rad2, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? BST_CHECKED : 0, 0);
217 if(pdata->fr.Flags & FR_HIDEMATCHCASE)
218 ShowWindow(GetDlgItem(hDlgWnd, chx2), SW_HIDE);
219 else if(pdata->fr.Flags & FR_NOMATCHCASE)
220 EnableWindow(GetDlgItem(hDlgWnd, chx2), FALSE);
222 SendDlgItemMessageA(hDlgWnd, chx2, BM_SETCHECK, pdata->fr.Flags & FR_MATCHCASE ? BST_CHECKED : 0, 0);
224 if(pdata->fr.Flags & FR_HIDEWHOLEWORD)
225 ShowWindow(GetDlgItem(hDlgWnd, chx1), SW_HIDE);
226 else if(pdata->fr.Flags & FR_NOWHOLEWORD)
227 EnableWindow(GetDlgItem(hDlgWnd, chx1), FALSE);
229 SendDlgItemMessageA(hDlgWnd, chx1, BM_SETCHECK, pdata->fr.Flags & FR_WHOLEWORD ? BST_CHECKED : 0, 0);
231 /* We did the init here, now call the hook if requested */
233 /* We do not do ShowWindow if hook exists and is FALSE */
234 /* per MSDN Article Q96135 */
235 if((pdata->fr.Flags & FR_ENABLEHOOK)
236 && ! pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, (LPARAM) &pdata->fr))
238 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
239 UpdateWindow(hDlgWnd);
243 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
245 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
256 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
260 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
265 FIXME("Got WM_HELP. Who is gonna supply it?\n");
270 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
272 /* FIXME: Handle F1 help */
275 retval = FALSE; /* We did not handle the message */
279 /* WM_DESTROY is a special case.
280 * We need to ensure that the allocated memory is freed just before
281 * the dialog is killed. We also need to remove the added prop.
283 if(iMsg == WM_DESTROY)
285 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
286 HeapFree(GetProcessHeap(), 0, pdata);
292 /***********************************************************************
293 * COMDLG32_FR_CheckPartial [internal]
294 * Check various fault conditions in the supplied parameters that
295 * cause an extended error to be reported.
300 static BOOL COMDLG32_FR_CheckPartial(
301 LPFINDREPLACEA pfr, /* [in] Find structure */
302 BOOL Replace /* [in] True if called as replace */
306 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
310 if(pfr->lStructSize != sizeof(FINDREPLACEA))
312 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
316 if(!IsWindow(pfr->hwndOwner))
318 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
322 if((pfr->wFindWhatLen < 1 || !pfr->lpstrFindWhat)
323 ||(Replace && (pfr->wReplaceWithLen < 1 || !pfr->lpstrReplaceWith)))
325 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
329 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRINGA)) == 0)
331 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
334 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRINGA)) == 0)
336 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
340 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
342 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
346 if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
348 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
352 if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
354 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
361 /***********************************************************************
362 * COMDLG32_FR_DoFindReplace [internal]
363 * Actual load and creation of the Find/Replace dialog.
365 * Window handle to created dialog:Success
368 static HWND COMDLG32_FR_DoFindReplace(
369 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
376 TRACE("hInst=%08x, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
378 if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
380 HMODULE hmod = COMDLG32_hInstance;
382 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
384 hmod = (HMODULE)pdata->fr.hInstance;
385 if(pdata->fr.Flags & FR_WINE_UNICODE)
387 htemplate = FindResourceW(hmod, (LPWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOGA);
391 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
396 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
398 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
402 error = CDERR_FINDRESFAILURE;
406 loadrc = LoadResource(hmod, htemplate);
410 loadrc = (HGLOBAL)pdata->fr.hInstance;
415 error = CDERR_LOADRESFAILURE;
419 if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
421 error = CDERR_LOCKRESFAILURE;
425 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
428 COMDLG32_FindReplaceDlgProc,
432 error = CDERR_DIALOGFAILURE;
434 COMDLG32_SetCommDlgExtendedError(error);
435 HeapFree(GetProcessHeap(), 0, pdata);
440 /***********************************************************************
441 * FindTextA [COMDLG32.@]
443 * Window handle to created dialog: Success
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_FR_Data *)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.@]
467 * Window handle to created dialog: Success
470 HWND WINAPI ReplaceTextA(
471 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
473 COMDLG32_FR_Data *pdata;
475 TRACE("LPFINDREPLACE=%p\n", pfr);
477 if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
480 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
481 return 0; /* Error has been set */
483 pdata->user_fr.fra = pfr;
485 pdata->fr.Flags |= FR_WINE_REPLACE;
486 return COMDLG32_FR_DoFindReplace(pdata);
489 /***********************************************************************
490 * FindTextW [COMDLG32.@]
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_FR_Data *)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 * 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, FALSE))
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_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
542 + len1 + len2)) == NULL)
543 return 0; /* Error has been set */
545 pdata->user_fr.frw = pfr;
546 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
547 pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
548 pdata->fr.lpstrFindWhat = (LPSTR)(pdata + 1); /* Set string pointer */
549 pdata->fr.lpstrReplaceWith = pdata->fr.lpstrFindWhat + len1;
551 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
552 pdata->fr.lpstrFindWhat, len1, NULL, NULL );
553 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrReplaceWith, pfr->wReplaceWithLen,
554 pdata->fr.lpstrReplaceWith, len2, NULL, NULL );
555 return COMDLG32_FR_DoFindReplace(pdata);