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

Last change on this file since 1548 was 1548, checked in by sandervl, 26 years ago

Port of Wine common dialogs (not all)

File size: 17.8 KB
Line 
1/* $Id: finddlg32.cpp,v 1.1 1999-11-02 19:09:42 sandervl Exp $ */
2/*
3 * Common Dialog Boxes interface (32 bit) (990815)
4 * Find/Replace
5 *
6 * Copyright 1998,1999 Bertho A. Stultiens
7 */
8
9#include <os2win.h>
10#include "winuser.h"
11#include "commdlg.h"
12#include "cderr.h"
13#include "dlgs.h"
14#include "debugtools.h"
15#include <heapstring.h>
16
17DEFAULT_DEBUG_CHANNEL(commdlg)
18
19#include "cdlg.h"
20
21
22/*-----------------------------------------------------------------------*/
23
24static UINT FindReplaceMessage;
25static 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 */
54static 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 */
70static 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;
110Replace:
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 */
153static 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#ifdef __WIN32OS2__
211 //SvL: If hooked and hook returns TRUE, then show dialog
212 // -> if not hooked, just display it!
213 if(!(pdata->fr.Flags & FR_ENABLEHOOK) ||
214 pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, pdata->fr.lCustData))
215#else
216 if((pdata->fr.Flags & FR_ENABLEHOOK)
217 && pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, pdata->fr.lCustData))
218#endif
219 {
220 ShowWindow(hDlgWnd, SW_SHOWNORMAL);
221 UpdateWindow(hDlgWnd);
222 }
223 /* else the caller is responsible for showing */
224 return TRUE;
225 }
226
227 if(pdata && (pdata->fr.Flags & FR_ENABLEHOOK))
228 {
229 retval = pdata->fr.lpfnHook(hDlgWnd, iMsg, wParam, lParam);
230 }
231 else
232 retval = FALSE;
233
234 if(pdata && !retval)
235 {
236 retval = TRUE;
237 switch(iMsg)
238 {
239 case WM_COMMAND:
240 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, LOWORD(wParam), HIWORD(wParam));
241 break;
242
243 case WM_CLOSE:
244 COMDLG32_FR_HandleWMCommand(hDlgWnd, pdata, IDCANCEL, BN_CLICKED);
245 break;
246
247 case WM_HELP:
248 /* Heeeeelp! */
249 FIXME("Got WM_HELP. Who is gonna supply it?\n");
250 break;
251
252 case WM_CONTEXTMENU:
253 /* Heeeeelp! */
254 FIXME("Got WM_CONTEXTMENU. Who is gonna supply it?\n");
255 break;
256 /* FIXME: Handle F1 help */
257
258 default:
259 retval = FALSE; /* We did not handle the message */
260 }
261 }
262
263 /* WM_DESTROY is a special case.
264 * We need to ensure that the allocated memory is freed just before
265 * the dialog is killed. We also need to remove the added prop.
266 */
267 if(iMsg == WM_DESTROY)
268 {
269 RemovePropA(hDlgWnd, (LPSTR)COMDLG32_Atom);
270 HeapFree(GetProcessHeap(), 0, pdata);
271 }
272
273 return retval;
274}
275
276/***********************************************************************
277 * COMDLG32_FR_CheckPartial [internal]
278 * Check various fault conditions in the supplied parameters that
279 * cause an extended error to be reported.
280 * RETURNS
281 * TRUE: Succes
282 * FALSE: Failure
283 */
284static BOOL COMDLG32_FR_CheckPartial(
285 LPFINDREPLACEA pfr, /* [in] Find structure */
286 BOOL Replace /* [in] True if called as replace */
287) {
288 if(!pfr)
289 {
290 COMDLG32_SetCommDlgExtendedError(CDERR_GENERALCODES);
291 return FALSE;
292 }
293
294 if(pfr->lStructSize != sizeof(FINDREPLACEA))
295 {
296 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
297 return FALSE;
298 }
299
300 if(!IsWindow(pfr->hwndOwner))
301 {
302 COMDLG32_SetCommDlgExtendedError(CDERR_DIALOGFAILURE);
303 return FALSE;
304 }
305
306#ifdef __WIN32OS2__
307 //SvL: Notepad set this to 32
308 if((!pfr->lpstrFindWhat)
309#else
310 if((pfr->wFindWhatLen < 80 || !pfr->lpstrFindWhat)
311#endif
312 ||(Replace && (pfr->wReplaceWithLen < 80 || !pfr->lpstrReplaceWith)))
313 {
314 COMDLG32_SetCommDlgExtendedError(FRERR_BUFFERLENGTHZERO);
315 return FALSE;
316 }
317
318 if((FindReplaceMessage = RegisterWindowMessageA(FINDMSGSTRING)) == 0)
319 {
320 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
321 return FALSE;
322 }
323 if((HelpMessage = RegisterWindowMessageA(HELPMSGSTRING)) == 0)
324 {
325 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
326 return FALSE;
327 }
328
329 if((pfr->Flags & FR_ENABLEHOOK) && !pfr->lpfnHook)
330 {
331 COMDLG32_SetCommDlgExtendedError(CDERR_NOHOOK);
332 return FALSE;
333 }
334
335 if((pfr->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE)) && !pfr->hInstance)
336 {
337 COMDLG32_SetCommDlgExtendedError(CDERR_NOHINSTANCE);
338 return FALSE;
339 }
340
341 if((pfr->Flags & FR_ENABLETEMPLATE) && !pfr->lpTemplateName)
342 {
343 COMDLG32_SetCommDlgExtendedError(CDERR_NOTEMPLATE);
344 return FALSE;
345 }
346
347 return TRUE;
348}
349
350/***********************************************************************
351 * COMDLG32_FR_DoFindReplace [internal]
352 * Actual load and creation of the Find/Replace dialog.
353 * RETURNS
354 * Window handle to created dialog:Succes
355 * NULL:Failure
356 */
357static HWND COMDLG32_FR_DoFindReplace(
358 COMDLG32_FR_Data *pdata /* [in] Internal data structure */
359) {
360 HWND hdlgwnd = 0;
361 HGLOBAL loadrc;
362 DWORD error;
363 LPDLGTEMPLATEW rcs;
364
365 TRACE("hInst=%08x, Flags=%08lx\n", pdata->fr.hInstance, pdata->fr.Flags);
366
367 if(!(pdata->fr.Flags & FR_ENABLETEMPLATEHANDLE))
368 {
369 HMODULE hmod = COMDLG32_hInstance;
370 HRSRC htemplate;
371 if(pdata->fr.Flags & FR_ENABLETEMPLATE)
372 {
373 hmod = (HMODULE)pdata->fr.hInstance;
374 if(pdata->fr.Flags & FR_WINE_UNICODE)
375 {
376 htemplate = FindResourceW(hmod, (LPWSTR)pdata->fr.lpTemplateName, (LPWSTR)RT_DIALOGA);
377 }
378 else
379 {
380 htemplate = FindResourceA(hmod, pdata->fr.lpTemplateName, (LPCSTR)RT_DIALOGA);
381 }
382 }
383 else
384 {
385 int rcid = pdata->fr.Flags & FR_WINE_REPLACE ? REPLACEDLGORD
386 : FINDDLGORD;
387 htemplate = FindResourceA(hmod, MAKEINTRESOURCEA(rcid), (LPCSTR)RT_DIALOGA);
388 }
389 if(!htemplate)
390 {
391 error = CDERR_FINDRESFAILURE;
392 goto cleanup;
393 }
394
395 loadrc = LoadResource(hmod, htemplate);
396 }
397 else
398 {
399 loadrc = (HGLOBAL)pdata->fr.hInstance;
400 }
401
402 if(!loadrc)
403 {
404 error = CDERR_LOADRESFAILURE;
405 goto cleanup;
406 }
407
408 if((rcs = (LPDLGTEMPLATEW)LockResource(loadrc)) == NULL)
409 {
410 error = CDERR_LOCKRESFAILURE;
411 goto cleanup;
412 }
413
414 hdlgwnd = CreateDialogIndirectParamA(COMDLG32_hInstance,
415 rcs,
416 pdata->fr.hwndOwner,
417 (DLGPROC)COMDLG32_FindReplaceDlgProc,
418 (LPARAM)pdata);
419 if(!hdlgwnd)
420 {
421 error = CDERR_DIALOGFAILURE;
422cleanup:
423 COMDLG32_SetCommDlgExtendedError(error);
424 HeapFree(GetProcessHeap(), 0, pdata);
425 }
426 return hdlgwnd;
427}
428
429/***********************************************************************
430 * FindText32A [COMDLG32.6]
431 * RETURNS
432 * Window handle to created dialog: Succes
433 * NULL: Failure
434 */
435HWND WINAPI FindTextA(
436 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
437) {
438 COMDLG32_FR_Data *pdata;
439
440 dprintf(("FindTextA: LPFINDREPLACE=%p\n", pfr));
441
442 if(!COMDLG32_FR_CheckPartial(pfr, FALSE))
443 return 0;
444
445 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
446 return 0; /* Error has been set */
447
448 pdata->user_fr.fra = pfr;
449 pdata->fr = *pfr;
450 return COMDLG32_FR_DoFindReplace(pdata);
451}
452
453/***********************************************************************
454 * ReplaceText32A [COMDLG32.19]
455 * RETURNS
456 * Window handle to created dialog: Succes
457 * NULL: Failure
458 */
459HWND WINAPI ReplaceTextA(
460 LPFINDREPLACEA pfr /* [in] Find/replace structure*/
461) {
462 COMDLG32_FR_Data *pdata;
463
464 dprintf(("ReplaceTextA: LPFINDREPLACE=%p\n", pfr));
465
466 if(!COMDLG32_FR_CheckPartial(pfr, TRUE))
467 return 0;
468
469 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data))) == NULL)
470 return 0; /* Error has been set */
471
472 pdata->user_fr.fra = pfr;
473 pdata->fr = *pfr;
474 pdata->fr.Flags |= FR_WINE_REPLACE;
475 return COMDLG32_FR_DoFindReplace(pdata);
476}
477
478/***********************************************************************
479 * FinfText32W [COMDLG32.7]
480 * RETURNS
481 * Window handle to created dialog: Succes
482 * NULL: Failure
483 */
484HWND WINAPI FindTextW(
485 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
486) {
487 COMDLG32_FR_Data *pdata;
488
489 dprintf(("FindTextW: LPFINDREPLACE=%p\n", pfr));
490
491 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
492 return 0;
493
494 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
495 + pfr->wFindWhatLen)) == NULL)
496 return 0; /* Error has been set */
497
498 pdata->user_fr.frw = pfr;
499 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
500 pdata->fr.Flags |= FR_WINE_UNICODE;
501 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointer */
502 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
503 return COMDLG32_FR_DoFindReplace(pdata);
504}
505
506/***********************************************************************
507 * ReplaceTextW [COMDLG32.20]
508 * RETURNS
509 * Window handle to created dialog: Succes
510 * NULL: Failure
511 */
512HWND WINAPI ReplaceTextW(
513 LPFINDREPLACEW pfr /* [in] Find/replace structure*/
514) {
515 COMDLG32_FR_Data *pdata;
516
517 dprintf(("ReplaceTextW: LPFINDREPLACE=%p\n", pfr));
518
519 if(!COMDLG32_FR_CheckPartial((LPFINDREPLACEA)pfr, FALSE))
520 return 0;
521
522 if((pdata = (COMDLG32_FR_Data *)COMDLG32_AllocMem(sizeof(COMDLG32_FR_Data)
523 + pfr->wFindWhatLen
524 + pfr->wReplaceWithLen)) == NULL)
525
526 return 0; /* Error has been set */
527
528 pdata->user_fr.frw = pfr;
529 pdata->fr = *(LPFINDREPLACEA)pfr; /* FINDREPLACEx have same size */
530 pdata->fr.Flags |= FR_WINE_REPLACE | FR_WINE_UNICODE;
531 pdata->fr.lpstrFindWhat = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1); /* Set string pointers */
532 pdata->fr.lpstrReplaceWith = (LPSTR)(((LPFINDREPLACEA)(pdata+1))+1) + pfr->wFindWhatLen;
533 lstrcpynWtoA(pdata->fr.lpstrFindWhat, pfr->lpstrFindWhat, pfr->wFindWhatLen);
534 lstrcpynWtoA(pdata->fr.lpstrReplaceWith, pfr->lpstrReplaceWith, pfr->wReplaceWithLen);
535 return COMDLG32_FR_DoFindReplace(pdata);
536}
537
538
Note: See TracBrowser for help on using the repository browser.