Added *.spec.c, *.spec.glue.s, *.glue.c to .cvsignore that need them.
[wine] / dlls / commdlg / finddlg32.c
1 /*
2  *  Common Dialog Boxes interface (32 bit)
3  *  Find/Replace
4  *
5  * Copyright 1998,1999 Bertho A. Stultiens
6  */
7
8 #include "winbase.h"
9 #include "winuser.h"
10 #include "commdlg.h"
11 #include "cderr.h"
12 #include "dlgs.h"
13 #include "debugtools.h"
14
15 DEFAULT_DEBUG_CHANNEL(commdlg)
16
17 #include "cdlg.h"
18
19
20 /*-----------------------------------------------------------------------*/
21
22 static UINT             FindReplaceMessage;
23 static UINT             HelpMessage;
24
25 #define FR_MASK (FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD | FR_REPLACEALL | FR_REPLACE | FR_FINDNEXT | FR_DIALOGTERM)
26 /* CRITICAL_SECTION COMDLG32_CritSect; */
27
28 /* Notes:
29  * MS uses a critical section at a few locations. However, I fail to
30  * see the reason for this. Their comdlg32.dll has a few race conditions
31  * but _not_ at those places that are protected with the mutex (there are
32  * globals that seem to hold info for the wndproc).
33  *
34  * FindText32x/ReplaceText32x
35  * The find/replace calls are passed a structure that is _not_ used
36  * internally. There is a local copy that holds the running info to
37  * be able to combine xxx32A and xxx32W calls. The passed pointer is
38  * returned upon sendmessage. Apps wont break this way when they rely
39  * on the original pointer. This will work as long as the sizes of
40  * FINDREPLACE32A == FINDREPLACE32W. The local copy will also prevent
41  * the app to see the wine-specific extra flags to distinguish between
42  * A/W and Find/Replace.
43  */
44
45
46 /***********************************************************************
47  *      COMDLG32_FR_GetFlags                    [internal]
48  * Returns the button state that needs to be reported to the caller.
49  *      RETURNS
50  *              Current state of check and radio buttons
51  */
52 static DWORD COMDLG32_FR_GetFlags(HWND hDlgWnd)
53 {
54         DWORD flags = 0;
55         if(IsDlgButtonChecked(hDlgWnd, rad2) == BST_CHECKED)
56                 flags |= FR_DOWN;
57         if(IsDlgButtonChecked(hDlgWnd, chx1) == BST_CHECKED)
58                 flags |= FR_WHOLEWORD;
59         if(IsDlgButtonChecked(hDlgWnd, chx2) == BST_CHECKED)
60                 flags |= FR_MATCHCASE;
61         return flags;
62 }
63
64 /***********************************************************************
65  *      COMDLG32_FR_HandleWMCommand             [internal]
66  * Handle WM_COMMAND messages...
67  */
68 static void COMDLG32_FR_HandleWMCommand(HWND hDlgWnd, COMDLG32_FR_Data *pData, int Id, int NotifyCode)
69 {
70         DWORD flag;
71
72         pData->user_fr.fra->Flags &= ~FR_MASK;  /* Clear return flags */
73         if(pData->fr.Flags & FR_WINE_REPLACE)   /* Replace always goes down... */
74                 pData->user_fr.fra->Flags |= FR_DOWN;
75
76         if(NotifyCode == BN_CLICKED)
77         {
78                 switch(Id)
79                 {
80                 case IDOK: /* Find Next */
81                         if(GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
82                         {
83                                 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_FINDNEXT;
84                                 if(pData->fr.Flags & FR_WINE_UNICODE)
85                                 {
86                                         lstrcpyAtoW(pData->user_fr.frw->lpstrFindWhat, pData->fr.lpstrFindWhat);
87                                 }
88                                 else
89                                 {
90                                         lstrcpyA(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
91                                 }
92                                 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
93                         }
94                         break;
95
96                 case IDCANCEL:
97                         pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | FR_DIALOGTERM;
98                         SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
99                         DestroyWindow(hDlgWnd);
100                         break;
101
102                 case psh2: /* Replace All */
103                         flag = FR_REPLACEALL;
104                         goto Replace;
105
106                 case psh1: /* Replace */
107                         flag = FR_REPLACE;
108 Replace:
109                         if((pData->fr.Flags & FR_WINE_REPLACE)
110                         && GetDlgItemTextA(hDlgWnd, edt1, pData->fr.lpstrFindWhat, pData->fr.wFindWhatLen) > 0)
111                         {
112                                 pData->fr.lpstrReplaceWith[0] = 0; /* In case the next GetDlgItemText Fails */
113                                 GetDlgItemTextA(hDlgWnd, edt2, pData->fr.lpstrReplaceWith, pData->fr.wReplaceWithLen);
114                                 pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd) | flag;
115                                 if(pData->fr.Flags & FR_WINE_UNICODE)
116                                 {
117                                         lstrcpyAtoW(pData->user_fr.frw->lpstrFindWhat, pData->fr.lpstrFindWhat);
118                                         lstrcpyAtoW(pData->user_fr.frw->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
119                                 }
120                                 else
121                                 {
122                                         lstrcpyA(pData->user_fr.fra->lpstrFindWhat, pData->fr.lpstrFindWhat);
123                                         lstrcpyA(pData->user_fr.fra->lpstrReplaceWith, pData->fr.lpstrReplaceWith);
124                                 }
125                                 SendMessageA(pData->fr.hwndOwner, FindReplaceMessage, 0, (LPARAM)pData->user_fr.fra);
126                         }
127                         break;
128
129                 case pshHelp:
130                         pData->user_fr.fra->Flags |= COMDLG32_FR_GetFlags(hDlgWnd);
131                         SendMessageA(pData->fr.hwndOwner, HelpMessage, (WPARAM)hDlgWnd, (LPARAM)pData->user_fr.fra);
132                         break;
133                 }
134         }
135         else if(NotifyCode == EN_CHANGE && Id == edt1)
136         {
137                 BOOL enable = SendDlgItemMessageA(hDlgWnd, edt1, WM_GETTEXTLENGTH, 0, 0) > 0;
138                 EnableWindow(GetDlgItem(hDlgWnd, IDOK), enable);
139                 if(pData->fr.Flags & FR_WINE_REPLACE)
140                 {
141                         EnableWindow(GetDlgItem(hDlgWnd, psh1), enable);
142                         EnableWindow(GetDlgItem(hDlgWnd, psh2), enable);
143                 }
144         }
145 }
146
147 /***********************************************************************
148  *      COMDLG32_FindReplaceDlgProc             [internal]
149  * [Find/Replace]Text32[A/W] window procedure.
150  */
151 static BOOL CALLBACK COMDLG32_FindReplaceDlgProc(HWND hDlgWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
152 {
153         COMDLG32_FR_Data *pdata = (COMDLG32_FR_Data *)GetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
154         BOOL retval = TRUE;;
155
156         if(iMsg == WM_INITDIALOG)
157         {
158                 pdata = (COMDLG32_FR_Data *)lParam;
159                 if(!SetPropA(hDlgWnd, (LPSTR)COMDLG32_Atom, (HANDLE)pdata))
160                 {
161                         ERR("Could not Set prop; invent a gracefull exit?...\n");
162                         DestroyWindow(hDlgWnd);
163                         return FALSE;
164                 }
165                 SendDlgItemMessageA(hDlgWnd, edt1, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wFindWhatLen, 0);
166                 SendDlgItemMessageA(hDlgWnd, edt1, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrFindWhat);
167                 if(pdata->fr.Flags & FR_WINE_REPLACE)
168                 {
169                         SendDlgItemMessageA(hDlgWnd, edt2, EM_SETLIMITTEXT, (WPARAM)pdata->fr.wReplaceWithLen, 0);
170                         SendDlgItemMessageA(hDlgWnd, edt2, WM_SETTEXT, 0, (LPARAM)pdata->fr.lpstrReplaceWith);
171                 }
172
173                 if(!(pdata->fr.Flags & FR_SHOWHELP))
174                         ShowWindow(GetDlgItem(hDlgWnd, pshHelp), SW_HIDE);
175                 if(pdata->fr.Flags & FR_HIDEUPDOWN)
176                 {
177                         ShowWindow(GetDlgItem(hDlgWnd, rad1), SW_HIDE);
178                         ShowWindow(GetDlgItem(hDlgWnd, rad2), SW_HIDE);
179                         ShowWindow(GetDlgItem(hDlgWnd, grp1), SW_HIDE);
180                 }
181                 else if(pdata->fr.Flags & FR_NOUPDOWN)
182                 {
183                         EnableWindow(GetDlgItem(hDlgWnd, rad1), FALSE);
184                         EnableWindow(GetDlgItem(hDlgWnd, rad2), FALSE);
185                         EnableWindow(GetDlgItem(hDlgWnd, grp1), FALSE);
186                 }
187                 else
188                 {
189                         SendDlgItemMessageA(hDlgWnd, rad1, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? 0 : BST_CHECKED, 0);
190                         SendDlgItemMessageA(hDlgWnd, rad2, BM_SETCHECK, pdata->fr.Flags & FR_DOWN ? BST_CHECKED : 0, 0);
191                 }
192
193                 if(pdata->fr.Flags & FR_HIDEMATCHCASE)
194                         ShowWindow(GetDlgItem(hDlgWnd, chx2), SW_HIDE);
195                 else if(pdata->fr.Flags & FR_NOMATCHCASE)
196                         EnableWindow(GetDlgItem(hDlgWnd, chx2), FALSE);
197                 else
198                         SendDlgItemMessageA(hDlgWnd, chx2, BM_SETCHECK, pdata->fr.Flags & FR_MATCHCASE ? BST_CHECKED : 0, 0);
199
200                 if(pdata->fr.Flags & FR_HIDEWHOLEWORD)
201                         ShowWindow(GetDlgItem(hDlgWnd, chx1), SW_HIDE);
202                 else if(pdata->fr.Flags & FR_NOWHOLEWORD)
203                         EnableWindow(GetDlgItem(hDlgWnd, chx1), FALSE);
204                 else
205                         SendDlgItemMessageA(hDlgWnd, chx1, BM_SETCHECK, pdata->fr.Flags & FR_WHOLEWORD ? BST_CHECKED : 0, 0);
206
207                 /* We did the init here, now call the hook if requested */
208                 if((pdata->fr.Flags & FR_ENABLEHOOK)
209                      && pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, pdata->fr.lCustData))
210                 {
211                         ShowWindow(hDlgWnd, SW_SHOWNORMAL);
212                         UpdateWindow(hDlgWnd);
213                 }
214                 /* else the caller is responsible for showing */
215                 return TRUE;
216         }
217
218         if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
219         {
220                 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
221         }
222         else
223                 retval = FALSE;
224
225         if(pdata && !retval)
226         {
227                 retval = TRUE;
228                 switch(iMsg)
229                 {
230                 case WM_COMMAND:
231                         COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
232                         break;
233
234                 case WM_CLOSE:
235                         COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
236                         break;
237
238                 case WM_HELP:
239                         /* Heeeeelp! */
240                         FIXME("Got WM_HELP. Who is gonna supply it?\n");
241                         break;
242
243                 case WM_CONTEXTMENU:
244                         /* Heeeeelp! */
245                         FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
246                         break;
247                 /* FIXME: Handle F1 help */
248
249                 default:
250                         retval = FALSE; /* We did not handle the message */
251                 }
252         }
253
254         /* WM_DESTROY is a special case.
255          * We need to ensure that the allocated memory is freed just before
256          * the dialog is killed. We also need to remove the added prop.
257          */
258         if(iMsg == WM_DESTROY)
259         {
260                 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
261                 HeapFree(GetProcessHeap(), 0, pdata);
262         }
263
264         return retval;
265 }
266
267 /***********************************************************************
268  *      COMDLG32_FR_CheckPartial                [internal]
269  * Check various fault conditions in the supplied parameters that
270  * cause an extended error to be reported.
271  *      RETURNS
272  *              TRUE: Succes
273  *              FALSE: Failure
274  */
275 static BOOL COMDLG32_FR_CheckPartial(
276         LPFINDREPLACEA pfr,     /* [in] Find structure */
277         BOOL Replace            /* [in] True if called as replace */
278 ) {
279         if(!pfr)
280         {
281                 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
282                 return FALSE;
283         }
284
285         if(pfr->lStructSize != sizeof(FINDREPLACEA))
286         {
287                 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
288                 return FALSE;
289         }
290
291         if(!IsWindow(pfr->hwndOwner))
292         {
293                 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
294                 return FALSE;
295         }
296
297         if((pfr->wFindWhatLen < 80 || !pfr->lpstrFindWhat)
298         ||(Replace && (pfr->wReplaceWithLen < 80 || !pfr->lpstrReplaceWith)))
299         {
300                 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
301                 return FALSE;
302         }
303
304         if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRING)) == 0)
305         {
306                 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
307                 return FALSE;
308         }
309         if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRING)) == 0)
310         {
311                 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
312                 return FALSE;
313         }
314
315         if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
316         {
317                 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
318                 return FALSE;
319         }
320
321         if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
322         {
323                 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
324                 return FALSE;
325         }
326
327         if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
328         {
329                 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
330                 return FALSE;
331         }
332
333         return TRUE;
334 }
335
336 /***********************************************************************
337  *      COMDLG32_FR_DoFindReplace               [internal]
338  * Actual load and creation of the Find/Replace dialog.
339  *      RETURNS
340  *              Window handle to created dialog:Succes
341  *              NULL:Failure
342  */
343 static HWND COMDLG32_FR_DoFindReplace(
344         COMDLG32_FR_Data *pdata /* [in] Internal data structure */
345 ) {
346         HWND hdlgwnd = 0;
347         HGLOBAL loadrc;
348         DWORD error;
349         LPDLGTEMPLATEW rcs;
350
351         TRACE("hInst=%08x, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
352
353         if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
354         {
355                 HMODULE hmod = COMDLG32_hInstance;
356                 HRSRC htemplate;
357                 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
358                 {
359                         hmod = (HMODULE)pdata->fr.hInstance;
360                         if(pdata->fr.Flags & FR_WINE_UNICODE)
361                         {
362                                 htemplate = FindResourceW(hmod, (LPWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOGA);
363                         }
364                         else
365                         {
366                                 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
367                         }
368                 }
369                 else
370                 {
371                         int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
372                                                                      : FINDDLGORD;
373                         htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
374                 }
375                 if(!htemplate)
376                 {
377                         error = CDERR_FINDRESFAILURE;
378                         goto cleanup;
379                 }
380
381                 loadrc = LoadResource(hmod, htemplate);
382         }
383         else
384         {
385                 loadrc = (HGLOBAL)pdata->fr.hInstance;
386         }
387
388         if(!loadrc)
389         {
390                 error = CDERR_LOADRESFAILURE;
391                 goto cleanup;
392         }
393
394         if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
395         {
396                 error = CDERR_LOCKRESFAILURE;
397                 goto cleanup;
398         }
399
400         hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
401                                              rcs,
402                                              pdata->fr.hwndOwner,
403                                              (DLGPROC)COMDLG32_FindReplaceDlgProc,
404                                              (LPARAM)pdata);
405         if(!hdlgwnd)
406         {
407                 error = CDERR_DIALOGFAILURE;
408 cleanup:
409                 COMDLG32_SetCommDlgExtendedError(error);
410                 HeapFree(GetProcessHeap(), 0, pdata);
411         }
412         return hdlgwnd;
413 }
414
415 /***********************************************************************
416  *      FindText32A                             [COMDLG32.6]
417  *      RETURNS
418  *              Window handle to created dialog: Succes
419  *              NULL: Failure
420  */
421 HWND WINAPI FindTextA(
422         LPFINDREPLACEA pfr      /* [in] Find/replace structure*/
423 ) {
424         COMDLG32_FR_Data *pdata;
425
426         TRACE("LPFINDREPLACE=%p\n", pfr);
427
428         if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
429                 return 0;
430
431         if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
432                 return 0; /* Error has been set */
433
434         pdata->user_fr.fra = pfr;
435         pdata->fr = *pfr;
436         return COMDLG32_FR_DoFindReplace(pdata);
437 }
438
439 /***********************************************************************
440  *      ReplaceText32A                          [COMDLG32.19]
441  *      RETURNS
442  *              Window handle to created dialog: Succes
443  *              NULL: Failure
444  */
445 HWND WINAPI ReplaceTextA(
446         LPFINDREPLACEA pfr      /* [in] Find/replace structure*/
447 ) {
448         COMDLG32_FR_Data *pdata;
449
450         TRACE("LPFINDREPLACE=%p\n", pfr);
451
452         if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
453                 return 0;
454
455         if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
456                 return 0; /* Error has been set */
457
458         pdata->user_fr.fra = pfr;
459         pdata->fr = *pfr;
460         pdata->fr.Flags |= FR_WINE_REPLACE;
461         return COMDLG32_FR_DoFindReplace(pdata);
462 }
463
464 /***********************************************************************
465  *      FinfText32W                             [COMDLG32.7]
466  *      RETURNS
467  *              Window handle to created dialog: Succes
468  *              NULL: Failure
469  */
470 HWND WINAPI FindTextW(
471         LPFINDREPLACEW pfr      /* [in] Find/replace structure*/
472 ) {
473         COMDLG32_FR_Data *pdata;
474
475         TRACE("LPFINDREPLACE=%p\n", pfr);
476
477         if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
478                 return 0;
479
480         if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
481                                                            + pfr->wFindWhatLen)) == NULL)
482                 return 0; /* Error has been set */
483
484         pdata->user_fr.frw = pfr;
485         pdata->fr = *(LPFINDREPLACEA)pfr;       /* FINDREPLACEx have same size */
486         pdata->fr.Flags |= FR_WINE_UNICODE;
487         pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1);       /* Set string pointer */
488         lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
489         return COMDLG32_FR_DoFindReplace(pdata);
490 }
491
492 /***********************************************************************
493  *      ReplaceTextW                            [COMDLG32.20]
494  *      RETURNS
495  *              Window handle to created dialog: Succes
496  *              NULL: Failure
497  */
498 HWND WINAPI ReplaceTextW(
499         LPFINDREPLACEW pfr      /* [in] Find/replace structure*/
500 ) {
501         COMDLG32_FR_Data *pdata;
502
503         TRACE("LPFINDREPLACE=%p\n", pfr);
504
505         if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
506                 return 0;
507
508         if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
509                                                            + pfr->wFindWhatLen
510                                                            + pfr->wReplaceWithLen)) == NULL)
511
512                 return 0; /* Error has been set */
513
514         pdata->user_fr.frw = pfr;
515         pdata->fr = *(LPFINDREPLACEA)pfr;       /* FINDREPLACEx have same size */
516         pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
517         pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1);       /* Set string pointers */
518         pdata->fr.lpstrReplaceWith = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1) + pfr->wFindWhatLen;
519         lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
520         lstrcpynWtoA(pdata->fr.lpstrReplaceWith, pfr->lpstrReplaceWith, pfr->wReplaceWithLen);
521         return COMDLG32_FR_DoFindReplace(pdata);
522 }
523
524