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