dmime: Implement band track IDirectMusicTrack_Play.
[wine.git] / dlls / comdlg32 / finddlg.c
blobe627dbce0c7f812fbefd36a42a64be49592cbc82
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "commdlg.h"
30 #include "cderr.h"
31 #include "dlgs.h"
32 #include "wine/debug.h"
33 #include "wine/heap.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
37 #include "cdlg.h"
40 /*-----------------------------------------------------------------------*/
42 static UINT FindReplaceMessage;
43 static UINT HelpMessage;
45 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
46 /* CRITICAL_SECTION COMDLG32_CritSect; */
48 /* Notes:
49 * MS uses a critical section at a few locations. However, I fail to
50 * see the reason for this. Their comdlg32.dll has a few race conditions
51 * but _not_ at those places that are protected with the mutex (there are
52 * globals that seem to hold info for the wndproc).
54 * FindText[AW]/ReplaceText[AW]
55 * The find/replace calls are passed a structure that is _not_ used
56 * internally. There is a local copy that holds the running info to
57 * be able to combine xxxA and xxxW calls. The passed pointer is
58 * returned upon sendmessage. Apps won't break this way when they rely
59 * on the original pointer. This will work as long as the sizes of
60 * FINDREPLACEA == FINDREPLACEW. The local copy will also prevent
61 * the app to see the wine-specific extra flags to distinguish between
62 * A/W and Find/Replace.
66 /***********************************************************************
67 * COMDLG32_FR_GetFlags [internal]
68 * Returns the button state that needs to be reported to the caller.
69 * RETURNS
70 * Current state of check and radio buttons
72 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
74 DWORD flags = 0;
75 if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
76 flags |= FR_DOWN;
77 if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
78 flags |= FR_WHOLEWORD;
79 if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
80 flags |= FR_MATCHCASE;
81 return flags;
84 /***********************************************************************
85 * COMDLG32_FR_HandleWMCommand [internal]
86 * Handle WM_COMMAND messages...
88 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd, COMDLG32_FR_Data *pData, int Id, int NotifyCode)
90 DWORD flag;
92 pData->user_fr.fra->Flags &= ~FR_MASK; /* Clear return flags */
93 if(pData->fr.Flags & FR_WINE_REPLACE) /* Replace always goes down... */
94 pData->user_fr.fra->Flags |= FR_DOWN;
96 if(NotifyCode == BN_CLICKED)
98 switch(Id)
100 case IDOK: /* Find Next */
101 if(GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
103 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_FINDNEXT;
104 if(pData->fr.Flags & FR_WINE_UNICODE)
106 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
107 pData->user_fr.frw->lpstrFindWhat,
108 0x7fffffff );
110 else
112 strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
114 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
116 break;
118 case IDCANCEL:
119 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_DIALOGTERM;
120 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
121 DestroyWindow(hDlgWnd);
122 break;
124 case psh2: /* Replace All */
125 flag = FR_REPLACEALL;
126 goto Replace;
128 case psh1: /* Replace */
129 flag = FR_REPLACE;
130 Replace:
131 if((pData->fr.Flags & FR_WINE_REPLACE)
132 && GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
134 pData->fr.lpstrReplaceWith[0] = 0; /* In case the next GetDlgItemText Fails */
135 GetDlgItemTextA(hDlgWnd, edt2, pData->fr.lpstrReplaceWith, pData->fr.wReplaceWithLen);
136 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | flag;
137 if(pData->fr.Flags & FR_WINE_UNICODE)
139 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrFindWhat, -1,
140 pData->user_fr.frw->lpstrFindWhat,
141 0x7fffffff );
142 MultiByteToWideChar( CP_ACP, 0, pData->fr.lpstrReplaceWith, -1,
143 pData->user_fr.frw->lpstrReplaceWith,
144 0x7fffffff );
146 else
148 strcpy(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
149 strcpy(pData->user_fr.fra->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
151 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
153 break;
155 case pshHelp:
156 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
157 SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
158 break;
161 else if(NotifyCode == EN_CHANGE && Id == edt1)
163 BOOL enable = SendDlgItemMessageA(hDlgWnd, edt1, WM_GETTEXTLENGTH, 0, 0) > 0;
164 EnableWindow(GetDlgItem(hDlgWnd, IDOK), enable);
165 if(pData->fr.Flags & FR_WINE_REPLACE)
167 EnableWindow(GetDlgItem(hDlgWnd, psh1), enable);
168 EnableWindow(GetDlgItem(hDlgWnd, psh2), enable);
173 /***********************************************************************
174 * COMDLG32_FindReplaceDlgProc [internal]
175 * [Find/Replace]Text32[A/W] window procedure.
177 static INT_PTR CALLBACK COMDLG32_FindReplaceDlgProc(HWND hDlgWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
179 COMDLG32_FR_Data *pdata = GetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
180 INT_PTR retval = TRUE;
182 if(iMsg == WM_INITDIALOG)
184 pdata = (COMDLG32_FR_Data *)lParam;
185 if(!SetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom, (HANDLE)pdata))
187 ERR("Could not Set prop; invent a graceful exit?...\n");
188 DestroyWindow(hDlgWnd);
189 return FALSE;
191 SendDlgItemMessageA(hDlgWnd, edt1, EM_SETLIMITTEXT, pdata->fr.wFindWhatLen, 0);
192 SendDlgItemMessageA(hDlgWnd, edt1, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrFindWhat);
193 if(pdata->fr.Flags & FR_WINE_REPLACE)
195 SendDlgItemMessageA(hDlgWnd, edt2, EM_SETLIMITTEXT, pdata->fr.wReplaceWithLen, 0);
196 SendDlgItemMessageA(hDlgWnd, edt2, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrReplaceWith);
199 if(!(pdata->fr.Flags & FR_SHOWHELP))
200 ShowWindow(GetDlgItem(hDlgWnd, pshHelp), SW_HIDE);
201 if(pdata->fr.Flags & FR_HIDEUPDOWN)
203 ShowWindow(GetDlgItem(hDlgWnd, rad1), SW_HIDE);
204 ShowWindow(GetDlgItem(hDlgWnd, rad2), SW_HIDE);
205 ShowWindow(GetDlgItem(hDlgWnd, grp1), SW_HIDE);
207 else if(pdata->fr.Flags & FR_NOUPDOWN)
209 EnableWindow(GetDlgItem(hDlgWnd, rad1), FALSE);
210 EnableWindow(GetDlgItem(hDlgWnd, rad2), FALSE);
211 EnableWindow(GetDlgItem(hDlgWnd, grp1), FALSE);
213 else
215 SendDlgItemMessageA(hDlgWnd, rad1, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? 0 : BST_CHECKED, 0);
216 SendDlgItemMessageA(hDlgWnd, rad2, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? BST_CHECKED : 0, 0);
219 if(pdata->fr.Flags & FR_HIDEMATCHCASE)
220 ShowWindow(GetDlgItem(hDlgWnd, chx2), SW_HIDE);
221 else if(pdata->fr.Flags & FR_NOMATCHCASE)
222 EnableWindow(GetDlgItem(hDlgWnd, chx2), FALSE);
223 else
224 SendDlgItemMessageA(hDlgWnd, chx2, BM_SETCHECK, pdata->fr.Flags & FR_MATCHCASE ? BST_CHECKED : 0, 0);
226 if(pdata->fr.Flags & FR_HIDEWHOLEWORD)
227 ShowWindow(GetDlgItem(hDlgWnd, chx1), SW_HIDE);
228 else if(pdata->fr.Flags & FR_NOWHOLEWORD)
229 EnableWindow(GetDlgItem(hDlgWnd, chx1), FALSE);
230 else
231 SendDlgItemMessageA(hDlgWnd, chx1, BM_SETCHECK, pdata->fr.Flags & FR_WHOLEWORD ? BST_CHECKED : 0, 0);
233 /* We did the init here, now call the hook if requested */
235 /* We do not do ShowWindow if hook exists and is FALSE */
236 /* per MSDN Article Q96135 */
237 if((pdata->fr.Flags & FR_ENABLEHOOK)
238 && ! pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, (LPARAM) &pdata->fr))
239 return TRUE;
240 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
241 UpdateWindow(hDlgWnd);
242 return TRUE;
245 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
247 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
249 else
250 retval = FALSE;
252 if(pdata && !retval)
254 retval = TRUE;
255 switch(iMsg)
257 case WM_COMMAND:
258 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
259 break;
261 case WM_CLOSE:
262 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
263 break;
265 case WM_HELP:
266 /* Heeeeelp! */
267 FIXME("Got WM_HELP. Who is gonna supply it?\n");
268 break;
270 case WM_CONTEXTMENU:
271 /* Heeeeelp! */
272 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
273 break;
274 /* FIXME: Handle F1 help */
276 default:
277 retval = FALSE; /* We did not handle the message */
281 /* WM_DESTROY is a special case.
282 * We need to ensure that the allocated memory is freed just before
283 * the dialog is killed. We also need to remove the added prop.
285 if(iMsg == WM_DESTROY)
287 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
288 heap_free(pdata);
291 return retval;
294 /***********************************************************************
295 * COMDLG32_FR_CheckPartial [internal]
296 * Check various fault conditions in the supplied parameters that
297 * cause an extended error to be reported.
298 * RETURNS
299 * TRUE: Success
300 * FALSE: Failure
302 static BOOL COMDLG32_FR_CheckPartial(
303 const FINDREPLACEA *pfr, /* [in] Find structure */
304 BOOL Replace /* [in] True if called as replace */
306 if(!pfr)
308 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
309 return FALSE;
312 if(pfr->lStructSize != sizeof(FINDREPLACEA))
314 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
315 return FALSE;
318 if(!IsWindow(pfr->hwndOwner))
320 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
321 return FALSE;
324 if((pfr->wFindWhatLen < 1 || !pfr->lpstrFindWhat)
325 ||(Replace && !pfr->lpstrReplaceWith))
327 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
328 return FALSE;
331 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRINGA)) == 0)
333 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
334 return FALSE;
336 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRINGA)) == 0)
338 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
339 return FALSE;
342 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
344 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
345 return FALSE;
348 if((pfr->Flags & FR_ENABLETEMPLATEHANDLE) && !pfr->hInstance)
350 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
351 return FALSE;
354 return TRUE;
357 /***********************************************************************
358 * COMDLG32_FR_DoFindReplace [internal]
359 * Actual load and creation of the Find/Replace dialog.
360 * RETURNS
361 * Window handle to created dialog:Success
362 * NULL:Failure
364 static HWND COMDLG32_FR_DoFindReplace(
365 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
367 HWND hdlgwnd = 0;
368 HGLOBAL loadrc;
369 DWORD error;
370 LPDLGTEMPLATEW rcs;
372 TRACE("hInst=%p, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
374 if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
376 HMODULE hmod = COMDLG32_hInstance;
377 HRSRC htemplate;
378 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
380 hmod = pdata->fr.hInstance;
381 if(pdata->fr.Flags & FR_WINE_UNICODE)
383 htemplate = FindResourceW(hmod, (LPCWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOG);
385 else
387 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOG);
390 else
392 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
393 : FINDDLGORD;
394 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOG);
396 if(!htemplate)
398 error = CDERR_FINDRESFAILURE;
399 goto cleanup;
402 loadrc = LoadResource(hmod, htemplate);
404 else
406 loadrc = pdata->fr.hInstance;
409 if(!loadrc)
411 error = CDERR_LOADRESFAILURE;
412 goto cleanup;
415 if((rcs = LockResource(loadrc)) == NULL)
417 error = CDERR_LOCKRESFAILURE;
418 goto cleanup;
421 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
422 rcs,
423 pdata->fr.hwndOwner,
424 COMDLG32_FindReplaceDlgProc,
425 (LPARAM)pdata);
426 if(!hdlgwnd)
428 error = CDERR_DIALOGFAILURE;
429 cleanup:
430 COMDLG32_SetCommDlgExtendedError(error);
431 heap_free(pdata);
433 return hdlgwnd;
436 /***********************************************************************
437 * FindTextA [COMDLG32.@]
439 * See FindTextW.
441 HWND WINAPI FindTextA(
442 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
444 COMDLG32_FR_Data *pdata;
446 TRACE("LPFINDREPLACE=%p\n", pfr);
448 if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
449 return 0;
451 if((pdata = COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
452 return 0; /* Error has been set */
454 pdata->user_fr.fra = pfr;
455 pdata->fr = *pfr;
456 return COMDLG32_FR_DoFindReplace(pdata);
459 /***********************************************************************
460 * ReplaceTextA [COMDLG32.@]
462 * See ReplaceTextW.
464 HWND WINAPI ReplaceTextA(
465 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
467 COMDLG32_FR_Data *pdata;
469 TRACE("LPFINDREPLACE=%p\n", pfr);
471 if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
472 return 0;
474 if((pdata = COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
475 return 0; /* Error has been set */
477 pdata->user_fr.fra = pfr;
478 pdata->fr = *pfr;
479 pdata->fr.Flags |= FR_WINE_REPLACE;
480 return COMDLG32_FR_DoFindReplace(pdata);
483 /***********************************************************************
484 * FindTextW [COMDLG32.@]
486 * Create a modeless find-text dialog box.
488 * RETURNS
489 * Window handle to created dialog: Success
490 * NULL: Failure
492 HWND WINAPI FindTextW(
493 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
495 COMDLG32_FR_Data *pdata;
496 DWORD len;
498 TRACE("LPFINDREPLACE=%p\n", pfr);
500 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
501 return 0;
503 len = WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
504 NULL, 0, NULL, NULL );
505 if((pdata = COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data) + len)) == NULL)
506 return 0; /* Error has been set */
508 pdata->user_fr.frw = pfr;
509 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
510 pdata->fr.Flags |= FR_WINE_UNICODE;
511 pdata->fr.lpstrFindWhat = (LPSTR)(pdata + 1); /* Set string pointer */
512 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
513 pdata->fr.lpstrFindWhat, len, NULL, NULL );
514 return COMDLG32_FR_DoFindReplace(pdata);
517 /***********************************************************************
518 * ReplaceTextW [COMDLG32.@]
520 * Create a modeless replace-text dialog box.
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, TRUE))
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_AllocMem(sizeof(COMDLG32_FR_Data) + len1 + len2)) == NULL)
542 return 0; /* Error has been set */
544 pdata->user_fr.frw = pfr;
545 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
546 pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
547 pdata->fr.lpstrFindWhat = (LPSTR)(pdata + 1); /* Set string pointer */
548 pdata->fr.lpstrReplaceWith = pdata->fr.lpstrFindWhat + len1;
550 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrFindWhat, pfr->wFindWhatLen,
551 pdata->fr.lpstrFindWhat, len1, NULL, NULL );
552 WideCharToMultiByte( CP_ACP, 0, pfr->lpstrReplaceWith, pfr->wReplaceWithLen,
553 pdata->fr.lpstrReplaceWith, len2, NULL, NULL );
554 return COMDLG32_FR_DoFindReplace(pdata);