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