source: trunk/src/comdlg32/finddlg32.cpp@ 3669

Last change on this file since 3669 was 3243, checked in by cbratschi, 25 years ago

merged with Corel WINE 20000324

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