finddlg32.c 19.2 KB
Newer Older
1 2 3 4 5
/*
 *  Common Dialog Boxes interface (32 bit)
 *  Find/Replace
 *
 * Copyright 1998,1999 Bertho A. Stultiens
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 21
 */

22
#include <string.h>
23
#include "winbase.h"
24
#include "windef.h"
25
#include "winnls.h"
26
#include "wingdi.h"
27 28 29 30
#include "winuser.h"
#include "commdlg.h"
#include "cderr.h"
#include "dlgs.h"
31
#include "wine/debug.h"
32

33
WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

#include "cdlg.h"


/*-----------------------------------------------------------------------*/

static UINT		FindReplaceMessage;
static UINT		HelpMessage;

#define FR_MASK	(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
/* CRITICAL_SECTION COMDLG32_CritSect; */

/* Notes:
 * MS uses a critical section at a few locations. However, I fail to
 * see the reason for this. Their comdlg32.dll has a few race conditions
 * but _not_ at those places that are protected with the mutex (there are
 * globals that seem to hold info for the wndproc).
 *
52
 * FindText[AW]/ReplaceText[AW]
53 54
 * The find/replace calls are passed a structure that is _not_ used
 * 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 57
 * returned upon sendmessage. Apps wont break this way when they rely
 * on the original pointer. This will work as long as the sizes of
58
 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
59 60 61 62 63 64 65 66 67 68 69
 * the app to see the wine-specific extra flags to distinguish between
 * A/W and Find/Replace.
 */


/***********************************************************************
 *	COMDLG32_FR_GetFlags			[internal]
 * Returns the button state that needs to be reported to the caller.
 *	RETURNS
 *		Current state of check and radio buttons
 */
Patrik Stridvall's avatar
Patrik Stridvall committed
70
static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
	DWORD flags = 0;
	if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
        	flags |= FR_DOWN;
	if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
        	flags |= FR_WHOLEWORD;
	if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
        	flags |= FR_MATCHCASE;
        return flags;
}

/***********************************************************************
 *	COMDLG32_FR_HandleWMCommand		[internal]
 * Handle WM_COMMAND messages...
 */
Patrik Stridvall's avatar
Patrik Stridvall committed
86
static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd, COMDLG32_FR_Data *pData, int Id, int NotifyCode)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
	DWORD flag;

	pData->user_fr.fra->Flags &= ~FR_MASK;	/* Clear return flags */
	if(pData->fr.Flags & FR_WINE_REPLACE)	/* Replace always goes down... */
		pData->user_fr.fra->Flags |= FR_DOWN;

	if(NotifyCode == BN_CLICKED)
        {
	       	switch(Id)
		{
		case IDOK: /* Find Next */
			if(GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
                        {
				pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_FINDNEXT;
                                if(pData->fr.Flags & FR_WINE_UNICODE)
                                {
104 105 106
                                    MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
                                                         pData->user_fr.frw->lpstrFindWhat,
                                                         0x7fffffff );
107 108 109
                                }
                                else
                                {
110
                                	strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                                }
				SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
                        }
			break;

		case IDCANCEL:
			pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_DIALOGTERM;
			SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
        	        DestroyWindow(hDlgWnd);
			break;

                case psh2: /* Replace All */
                	flag = FR_REPLACEALL;
                        goto Replace;

                case psh1: /* Replace */
                	flag = FR_REPLACE;
Replace:
			if((pData->fr.Flags & FR_WINE_REPLACE)
                        && GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
                        {
				pData->fr.lpstrReplaceWith[0] = 0; /* In case the next GetDlgItemText Fails */
				GetDlgItemTextA(hDlgWnd, edt2, pData->fr.lpstrReplaceWith, pData->fr.wReplaceWithLen);
				pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | flag;
                                if(pData->fr.Flags & FR_WINE_UNICODE)
                                {
137 138 139 140 141 142
                                    MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
                                                         pData->user_fr.frw->lpstrFindWhat,
                                                         0x7fffffff );
                                    MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrReplaceWith, -1,
                                                         pData->user_fr.frw->lpstrReplaceWith,
                                                         0x7fffffff );
143 144 145
                                }
                                else
                                {
146 147
                                	strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
                                	strcpy(pData->user_fr.fra->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                                }
				SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
                        }
			break;

		case pshHelp:
			pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
			SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
			break;
                }
        }
        else if(NotifyCode == EN_CHANGE && Id == edt1)
	{
		BOOL enable = SendDlgItemMessageA(hDlgWnd, edt1, WM_GETTEXTLENGTH, 0, 0) > 0;
		EnableWindow(GetDlgItem(hDlgWnd, IDOK), enable);
                if(pData->fr.Flags & FR_WINE_REPLACE)
                {
			EnableWindow(GetDlgItem(hDlgWnd, psh1), enable);
			EnableWindow(GetDlgItem(hDlgWnd, psh2), enable);
                }
	}
}

/***********************************************************************
 *	COMDLG32_FindReplaceDlgProc		[internal]
 * [Find/Replace]Text32[A/W] window procedure.
 */
175
static INT_PTR CALLBACK COMDLG32_FindReplaceDlgProc(HWND hDlgWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
176 177
{
	COMDLG32_FR_Data *pdata = (COMDLG32_FR_Data *)GetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
178
	INT_PTR retval = TRUE;
179 180 181 182 183 184

	if(iMsg == WM_INITDIALOG)
        {
                pdata = (COMDLG32_FR_Data *)lParam;
        	if(!SetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom, (HANDLE)pdata))
                {
185
			ERR("Could not Set prop; invent a gracefull exit?...\n");
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                	DestroyWindow(hDlgWnd);
                        return FALSE;
                }
        	SendDlgItemMessageA(hDlgWnd, edt1, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wFindWhatLen, 0);
	        SendDlgItemMessageA(hDlgWnd, edt1, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrFindWhat);
                if(pdata->fr.Flags & FR_WINE_REPLACE)
                {
	        	SendDlgItemMessageA(hDlgWnd, edt2, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wReplaceWithLen, 0);
		        SendDlgItemMessageA(hDlgWnd, edt2, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrReplaceWith);
                }

        	if(!(pdata->fr.Flags & FR_SHOWHELP))
			ShowWindow(GetDlgItem(hDlgWnd, pshHelp), SW_HIDE);
	        if(pdata->fr.Flags & FR_HIDEUPDOWN)
        	{
			ShowWindow(GetDlgItem(hDlgWnd, rad1), SW_HIDE);
			ShowWindow(GetDlgItem(hDlgWnd, rad2), SW_HIDE);
			ShowWindow(GetDlgItem(hDlgWnd, grp1), SW_HIDE);
	        }
        	else if(pdata->fr.Flags & FR_NOUPDOWN)
	        {
			EnableWindow(GetDlgItem(hDlgWnd, rad1), FALSE);
			EnableWindow(GetDlgItem(hDlgWnd, rad2), FALSE);
			EnableWindow(GetDlgItem(hDlgWnd, grp1), FALSE);
        	}
                else
                {
			SendDlgItemMessageA(hDlgWnd, rad1, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? 0 : BST_CHECKED, 0);
			SendDlgItemMessageA(hDlgWnd, rad2, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? BST_CHECKED : 0, 0);
                }

	        if(pdata->fr.Flags & FR_HIDEMATCHCASE)
			ShowWindow(GetDlgItem(hDlgWnd, chx2), SW_HIDE);
        	else if(pdata->fr.Flags & FR_NOMATCHCASE)
			EnableWindow(GetDlgItem(hDlgWnd, chx2), FALSE);
                else
			SendDlgItemMessageA(hDlgWnd, chx2, BM_SETCHECK, pdata->fr.Flags & FR_MATCHCASE ? BST_CHECKED : 0, 0);

	        if(pdata->fr.Flags & FR_HIDEWHOLEWORD)
			ShowWindow(GetDlgItem(hDlgWnd, chx1), SW_HIDE);
        	else if(pdata->fr.Flags & FR_NOWHOLEWORD)
			EnableWindow(GetDlgItem(hDlgWnd, chx1), FALSE);
                else
			SendDlgItemMessageA(hDlgWnd, chx1, BM_SETCHECK, pdata->fr.Flags & FR_WHOLEWORD ? BST_CHECKED : 0, 0);

		/* We did the init here, now call the hook if requested */
232 233 234 235

		/* We do not do ShowWindow if hook exists and is FALSE  */
		/*   per MSDN Article Q96135                            */
              	if((pdata->fr.Flags & FR_ENABLEHOOK)
236
	             && ! pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, (LPARAM) &pdata->fr))
237 238 239
		        return TRUE;
		ShowWindow(hDlgWnd, SW_SHOWNORMAL);
		UpdateWindow(hDlgWnd);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                return TRUE;
        }

	if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
	{
		retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
	}
	else
		retval = FALSE;

       	if(pdata && !retval)
        {
        	retval = TRUE;
		switch(iMsg)
        	{
	        case WM_COMMAND:
			COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
                	break;

	        case WM_CLOSE:
			COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
        		break;

	        case WM_HELP:
        		/* Heeeeelp! */
265
        		FIXME("Got WM_HELP. Who is gonna supply it?\n");
266 267 268 269
	                break;

        	case WM_CONTEXTMENU:
        		/* Heeeeelp! */
270
        		FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        	        break;
		/* FIXME: Handle F1 help */

	        default:
		        retval = FALSE;	/* We did not handle the message */
	        }
        }

        /* WM_DESTROY is a special case.
         * We need to ensure that the allocated memory is freed just before
         * the dialog is killed. We also need to remove the added prop.
         */
        if(iMsg == WM_DESTROY)
        {
        	RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
        	HeapFree(GetProcessHeap(), 0, pdata);
        }

        return retval;
}

/***********************************************************************
 *	COMDLG32_FR_CheckPartial		[internal]
 * Check various fault conditions in the supplied parameters that
 * cause an extended error to be reported.
 *	RETURNS
297
 *		TRUE: Success
298 299
 *		FALSE: Failure
 */
Patrik Stridvall's avatar
Patrik Stridvall committed
300
static BOOL COMDLG32_FR_CheckPartial(
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	LPFINDREPLACEA pfr,	/* [in] Find structure */
        BOOL Replace		/* [in] True if called as replace */
) {
	if(!pfr)
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
                return FALSE;
	}

        if(pfr->lStructSize != sizeof(FINDREPLACEA))
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
        	return FALSE;
        }

        if(!IsWindow(pfr->hwndOwner))
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
        	return FALSE;
        }

322 323
	if((pfr->wFindWhatLen < 1 || !pfr->lpstrFindWhat)
        ||(Replace && (pfr->wReplaceWithLen < 1 || !pfr->lpstrReplaceWith)))
324 325 326 327 328
        {
        	COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
                return FALSE;
        }

329
	if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRINGA)) == 0)
330 331 332 333
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
                return FALSE;
        }
334
	if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRINGA)) == 0)
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
                return FALSE;
        }

        if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
                return FALSE;
        }

        if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
                return FALSE;
        }

        if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
                return FALSE;
        }

	return TRUE;
}

/***********************************************************************
 *	COMDLG32_FR_DoFindReplace		[internal]
 * Actual load and creation of the Find/Replace dialog.
 *	RETURNS
365
 *		Window handle to created dialog:Success
366 367
 *		NULL:Failure
 */
Patrik Stridvall's avatar
Patrik Stridvall committed
368
static HWND COMDLG32_FR_DoFindReplace(
369 370 371 372 373 374 375
	COMDLG32_FR_Data *pdata	/* [in] Internal data structure */
) {
	HWND hdlgwnd = 0;
        HGLOBAL loadrc;
        DWORD error;
        LPDLGTEMPLATEW rcs;

376
	TRACE("hInst=%p, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

        if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
        {
	        HMODULE hmod = COMDLG32_hInstance;
		HRSRC htemplate;
        	if(pdata->fr.Flags & FR_ENABLETEMPLATE)
	        {
        		hmod = (HMODULE)pdata->fr.hInstance;
                        if(pdata->fr.Flags & FR_WINE_UNICODE)
                        {
				htemplate = FindResourceW(hmod, (LPWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOGA);
                        }
                        else
                        {
				htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
                        }
        	}
		else
        	{
			int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
								     : FINDDLGORD;
			htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
		}
		if(!htemplate)
       	        {
                	error = CDERR_FINDRESFAILURE;
                       	goto cleanup;
               	}

	        loadrc = LoadResource(hmod, htemplate);
        }
        else
        {
        	loadrc = (HGLOBAL)pdata->fr.hInstance;
        }

        if(!loadrc)
        {
		error = CDERR_LOADRESFAILURE;
		goto cleanup;
	}

        if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
        {
		error = CDERR_LOCKRESFAILURE;
		goto cleanup;
        }

        hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
        				     rcs,
                                             pdata->fr.hwndOwner,
428
                                             COMDLG32_FindReplaceDlgProc,
429 430 431 432 433 434 435 436 437 438 439 440
                                             (LPARAM)pdata);
	if(!hdlgwnd)
        {
        	error = CDERR_DIALOGFAILURE;
cleanup:
        	COMDLG32_SetCommDlgExtendedError(error);
                HeapFree(GetProcessHeap(), 0, pdata);
        }
        return hdlgwnd;
}

/***********************************************************************
441
 *	FindTextA 				[COMDLG32.@]
442
 *	RETURNS
443
 *		Window handle to created dialog: Success
444 445 446 447 448 449 450
 *		NULL: Failure
 */
HWND WINAPI FindTextA(
	LPFINDREPLACEA pfr	/* [in] Find/replace structure*/
) {
	COMDLG32_FR_Data *pdata;

451
        TRACE("LPFINDREPLACE=%p\n", pfr);
452 453 454 455 456 457 458 459 460 461 462 463 464

	if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
        	return 0;

	if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
        	return 0; /* Error has been set */

        pdata->user_fr.fra = pfr;
        pdata->fr = *pfr;
	return COMDLG32_FR_DoFindReplace(pdata);
}

/***********************************************************************
465
 *	ReplaceTextA 				[COMDLG32.@]
466
 *	RETURNS
467
 *		Window handle to created dialog: Success
468 469 470 471 472 473 474
 *		NULL: Failure
 */
HWND WINAPI ReplaceTextA(
	LPFINDREPLACEA pfr	/* [in] Find/replace structure*/
) {
	COMDLG32_FR_Data *pdata;

475
        TRACE("LPFINDREPLACE=%p\n", pfr);
476 477 478 479 480 481 482 483 484 485 486 487 488 489

	if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
        	return 0;

	if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
        	return 0; /* Error has been set */

        pdata->user_fr.fra = pfr;
        pdata->fr = *pfr;
	pdata->fr.Flags |= FR_WINE_REPLACE;
	return COMDLG32_FR_DoFindReplace(pdata);
}

/***********************************************************************
490
 *	FindTextW 				[COMDLG32.@]
491
 *	RETURNS
492
 *		Window handle to created dialog: Success
493 494 495 496 497 498
 *		NULL: Failure
 */
HWND WINAPI FindTextW(
	LPFINDREPLACEW pfr	/* [in] Find/replace structure*/
) {
	COMDLG32_FR_Data *pdata;
499
        DWORD len;
500

501
        TRACE("LPFINDREPLACE=%p\n", pfr);
502 503 504 505

	if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
        	return 0;

506 507 508 509
        len = WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
                                   NULL, 0, NULL, NULL );
        if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data) + len)) == NULL)
            return 0; /* Error has been set */
510 511 512 513

        pdata->user_fr.frw = pfr;
        pdata->fr = *(LPFINDREPLACEA)pfr;	/* FINDREPLACEx have same size */
	pdata->fr.Flags |= FR_WINE_UNICODE;
514
        pdata->fr.lpstrFindWhat = (LPSTR)(pdata + 1);  /* Set string pointer */
515 516
        WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
                             pdata->fr.lpstrFindWhat, len, NULL, NULL );
517 518 519 520
        return COMDLG32_FR_DoFindReplace(pdata);
}

/***********************************************************************
521
 *	ReplaceTextW 				[COMDLG32.@]
522
 *	RETURNS
523
 *		Window handle to created dialog: Success
524 525 526 527 528 529
 *		NULL: Failure
 */
HWND WINAPI ReplaceTextW(
	LPFINDREPLACEW pfr	/* [in] Find/replace structure*/
) {
	COMDLG32_FR_Data *pdata;
530
        DWORD len1, len2;
531

532
        TRACE("LPFINDREPLACE=%p\n", pfr);
533 534 535 536

	if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
        	return 0;

537 538 539 540
        len1 = WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
                                    NULL, 0, NULL, NULL );
        len2 = WideCharToMultiByte( CP_ACP, 0, pfr->lpstrReplaceWith, pfr->wReplaceWithLen,
                                    NULL, 0, NULL, NULL );
541
	if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
542 543
                                                          + len1 + len2)) == NULL)
            return 0; /* Error has been set */
544 545 546 547

        pdata->user_fr.frw = pfr;
        pdata->fr = *(LPFINDREPLACEA)pfr;	/* FINDREPLACEx have same size */
	pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
548 549
        pdata->fr.lpstrFindWhat = (LPSTR)(pdata + 1);  /* Set string pointer */
        pdata->fr.lpstrReplaceWith = pdata->fr.lpstrFindWhat + len1;
550 551 552 553 554

        WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
                             pdata->fr.lpstrFindWhat, len1, NULL, NULL );
        WideCharToMultiByte( CP_ACP, 0, pfr->lpstrReplaceWith, pfr->wReplaceWithLen,
                             pdata->fr.lpstrReplaceWith, len2, NULL, NULL );
555 556
        return COMDLG32_FR_DoFindReplace(pdata);
}