Fixed some alignment issues (based on a patch by Gregg Mattinson).
[wine.git] / dlls / commdlg / finddlg32.c
blob778804957720a536aae8de52aa4bbaea2196a771
1 /*
2 * Common Dialog Boxes interface (32 bit)
3 * Find/Replace
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
22 #include <string.h>
23 #include "winbase.h"
24 #include "windef.h"
25 #include "winnls.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "commdlg.h"
29 #include "cderr.h"
30 #include "dlgs.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
35 #include "cdlg.h"
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; */
46 /* Notes:
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.
67 * RETURNS
68 * Current state of check and radio buttons
70 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
72 DWORD flags = 0;
73 if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
74 flags |= FR_DOWN;
75 if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
76 flags |= FR_WHOLEWORD;
77 if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
78 flags |= FR_MATCHCASE;
79 return flags;
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)
88 DWORD flag;
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)
96 switch(Id)
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,
106 0x7fffffff );
108 else
110 strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
112 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
114 break;
116 case IDCANCEL:
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);
120 break;
122 case psh2: /* Replace All */
123 flag = FR_REPLACEALL;
124 goto Replace;
126 case psh1: /* Replace */
127 flag = FR_REPLACE;
128 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,
139 0x7fffffff );
140 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrReplaceWith, -1,
141 pData->user_fr.frw->lpstrReplaceWith,
142 0x7fffffff );
144 else
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);
151 break;
153 case pshHelp:
154 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
155 SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
156 break;
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 BOOL 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 BOOL 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);
187 return FALSE;
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);
211 else
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);
221 else
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);
228 else
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))
237 return TRUE;
238 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
239 UpdateWindow(hDlgWnd);
240 return TRUE;
243 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
245 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
247 else
248 retval = FALSE;
250 if(pdata && !retval)
252 retval = TRUE;
253 switch(iMsg)
255 case WM_COMMAND:
256 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
257 break;
259 case WM_CLOSE:
260 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
261 break;
263 case WM_HELP:
264 /* Heeeeelp! */
265 FIXME("Got WM_HELP. Who is gonna supply it?\n");
266 break;
268 case WM_CONTEXTMENU:
269 /* Heeeeelp! */
270 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
271 break;
272 /* FIXME: Handle F1 help */
274 default:
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);
289 return retval;
292 /***********************************************************************
293 * COMDLG32_FR_CheckPartial [internal]
294 * Check various fault conditions in the supplied parameters that
295 * cause an extended error to be reported.
296 * RETURNS
297 * TRUE: Success
298 * FALSE: Failure
300 static BOOL COMDLG32_FR_CheckPartial(
301 LPFINDREPLACEA pfr, /* [in] Find structure */
302 BOOL Replace /* [in] True if called as replace */
304 if(!pfr)
306 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
307 return FALSE;
310 if(pfr->lStructSize != sizeof(FINDREPLACEA))
312 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
313 return FALSE;
316 if(!IsWindow(pfr->hwndOwner))
318 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
319 return FALSE;
322 if((pfr->wFindWhatLen < 1 || !pfr->lpstrFindWhat)
323 ||(Replace && (pfr->wReplaceWithLen < 1 || !pfr->lpstrReplaceWith)))
325 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
326 return FALSE;
329 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRINGA)) == 0)
331 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
332 return FALSE;
334 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRINGA)) == 0)
336 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
337 return FALSE;
340 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
342 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
343 return FALSE;
346 if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
348 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
349 return FALSE;
352 if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
354 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
355 return FALSE;
358 return TRUE;
361 /***********************************************************************
362 * COMDLG32_FR_DoFindReplace [internal]
363 * Actual load and creation of the Find/Replace dialog.
364 * RETURNS
365 * Window handle to created dialog:Success
366 * NULL:Failure
368 static HWND COMDLG32_FR_DoFindReplace(
369 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
371 HWND hdlgwnd = 0;
372 HGLOBAL loadrc;
373 DWORD error;
374 LPDLGTEMPLATEW rcs;
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;
381 HRSRC htemplate;
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);
389 else
391 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
394 else
396 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
397 : FINDDLGORD;
398 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
400 if(!htemplate)
402 error = CDERR_FINDRESFAILURE;
403 goto cleanup;
406 loadrc = LoadResource(hmod, htemplate);
408 else
410 loadrc = (HGLOBAL)pdata->fr.hInstance;
413 if(!loadrc)
415 error = CDERR_LOADRESFAILURE;
416 goto cleanup;
419 if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
421 error = CDERR_LOCKRESFAILURE;
422 goto cleanup;
425 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
426 rcs,
427 pdata->fr.hwndOwner,
428 (DLGPROC)COMDLG32_FindReplaceDlgProc,
429 (LPARAM)pdata);
430 if(!hdlgwnd)
432 error = CDERR_DIALOGFAILURE;
433 cleanup:
434 COMDLG32_SetCommDlgExtendedError(error);
435 HeapFree(GetProcessHeap(), 0, pdata);
437 return hdlgwnd;
440 /***********************************************************************
441 * FindTextA [COMDLG32.@]
442 * RETURNS
443 * Window handle to created dialog: Success
444 * NULL: Failure
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))
454 return 0;
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;
460 pdata->fr = *pfr;
461 return COMDLG32_FR_DoFindReplace(pdata);
464 /***********************************************************************
465 * ReplaceTextA [COMDLG32.@]
466 * RETURNS
467 * Window handle to created dialog: Success
468 * NULL: Failure
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))
478 return 0;
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;
484 pdata->fr = *pfr;
485 pdata->fr.Flags |= FR_WINE_REPLACE;
486 return COMDLG32_FR_DoFindReplace(pdata);
489 /***********************************************************************
490 * FindTextW [COMDLG32.@]
491 * RETURNS
492 * Window handle to created dialog: Success
493 * NULL: Failure
495 HWND WINAPI FindTextW(
496 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
498 COMDLG32_FR_Data *pdata;
499 DWORD len;
501 TRACE("LPFINDREPLACE=%p\n", pfr);
503 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
504 return 0;
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.@]
522 * RETURNS
523 * Window handle to created dialog: Success
524 * NULL: Failure
526 HWND WINAPI ReplaceTextW(
527 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
529 COMDLG32_FR_Data *pdata;
530 DWORD len1, len2;
532 TRACE("LPFINDREPLACE=%p\n", pfr);
534 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
535 return 0;
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);