Compiler warnings fix.
[wine] / dlls / commdlg / finddlg.c
1 /*
2  * COMMDLG - Find & Replace Text Dialogs
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1996 Albrecht Kleine
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "winbase.h"
12 #include "wine/winbase16.h"
13 #include "ldt.h"
14 #include "commdlg.h"
15 #include "resource.h"
16 #include "dialog.h"
17 #include "dlgs.h"
18 #include "module.h"
19 #include "debugtools.h"
20 #include "winproc.h"
21 #include "cderr.h"
22
23 DEFAULT_DEBUG_CHANNEL(commdlg)
24
25 #include "cdlg.h"
26
27 /***********************************************************************
28  *           FindText16   (COMMDLG.11)
29  */
30 HWND16 WINAPI FindText16( SEGPTR find )
31 {
32     HANDLE16 hInst;
33     LPCVOID ptr;
34     HANDLE hResInfo, hDlgTmpl;
35     LPFINDREPLACE16 lpFind = (LPFINDREPLACE16)PTR_SEG_TO_LIN(find);
36
37     /*
38      * FIXME : Should respond to FR_ENABLETEMPLATE and FR_ENABLEHOOK here
39      * For now, only the standard dialog works.
40      */
41     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE | 
42         FR_ENABLEHOOK)) FIXME(": unimplemented flag (ignored)\n");     
43     if (!(hResInfo = FindResourceA(COMMDLG_hInstance32, MAKEINTRESOURCEA(FINDDLGORD), RT_DIALOGA)))
44     {
45         COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
46         return FALSE;
47     }
48     if (!(hDlgTmpl = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
49         !(ptr = LockResource( hDlgTmpl )))
50     {
51         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
52         return FALSE;
53     }
54     hInst = GetWindowLongA( lpFind->hwndOwner , GWL_HINSTANCE);
55     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
56                         (DLGPROC16)MODULE_GetWndProcEntry16("FindTextDlgProc"),
57                                   find, WIN_PROC_16 );
58 }
59
60
61 /***********************************************************************
62  *           ReplaceText16   (COMMDLG.12)
63  */
64 HWND16 WINAPI ReplaceText16( SEGPTR find )
65 {
66     HANDLE16 hInst;
67     LPCVOID ptr;
68     HANDLE hResInfo, hDlgTmpl;
69     LPFINDREPLACE16 lpFind = (LPFINDREPLACE16)PTR_SEG_TO_LIN(find);
70
71     /*
72      * FIXME : We should do error checking on the lpFind structure here
73      * and make CommDlgExtendedError() return the error condition.
74      */
75     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE | 
76         FR_ENABLEHOOK)) FIXME(": unimplemented flag (ignored)\n");     
77     if (!(hResInfo = FindResourceA(COMMDLG_hInstance32, MAKEINTRESOURCEA(REPLACEDLGORD), RT_DIALOGA)))
78     {
79         COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
80         return FALSE;
81     }
82     if (!(hDlgTmpl = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
83         !(ptr = LockResource( hDlgTmpl )))
84     {
85         COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
86         return FALSE;
87     }
88     hInst = GetWindowLongA( lpFind->hwndOwner , GWL_HINSTANCE);
89     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
90                      (DLGPROC16)MODULE_GetWndProcEntry16("ReplaceTextDlgProc"),
91                                   find, WIN_PROC_16 );
92 }
93
94
95 /***********************************************************************
96  *                              FINDDLG_WMInitDialog            [internal]
97  */
98 static LRESULT FINDDLG_WMInitDialog(HWND hWnd, LPARAM lParam, LPDWORD lpFlags,
99                                     LPSTR lpstrFindWhat, BOOL fUnicode)
100 {
101     SetWindowLongA(hWnd, DWL_USER, lParam);
102     *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
103     /*
104      * FIXME : If the initial FindWhat string is empty, we should disable the
105      * FindNext (IDOK) button.  Only after typing some text, the button should be
106      * enabled.
107      */
108     if (fUnicode) SetDlgItemTextW(hWnd, edt1, (LPWSTR)lpstrFindWhat);
109         else SetDlgItemTextA(hWnd, edt1, lpstrFindWhat);
110     CheckRadioButton(hWnd, rad1, rad2, (*lpFlags & FR_DOWN) ? rad2 : rad1);
111     if (*lpFlags & (FR_HIDEUPDOWN | FR_NOUPDOWN)) {
112         EnableWindow(GetDlgItem(hWnd, rad1), FALSE);
113         EnableWindow(GetDlgItem(hWnd, rad2), FALSE);
114     }
115     if (*lpFlags & FR_HIDEUPDOWN) {
116         ShowWindow(GetDlgItem(hWnd, rad1), SW_HIDE);
117         ShowWindow(GetDlgItem(hWnd, rad2), SW_HIDE);
118         ShowWindow(GetDlgItem(hWnd, grp1), SW_HIDE);
119     }
120     CheckDlgButton(hWnd, chx1, (*lpFlags & FR_WHOLEWORD) ? 1 : 0);
121     if (*lpFlags & (FR_HIDEWHOLEWORD | FR_NOWHOLEWORD))
122         EnableWindow(GetDlgItem(hWnd, chx1), FALSE);
123     if (*lpFlags & FR_HIDEWHOLEWORD)
124         ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
125     CheckDlgButton(hWnd, chx2, (*lpFlags & FR_MATCHCASE) ? 1 : 0);
126     if (*lpFlags & (FR_HIDEMATCHCASE | FR_NOMATCHCASE))
127         EnableWindow(GetDlgItem(hWnd, chx2), FALSE);
128     if (*lpFlags & FR_HIDEMATCHCASE)
129         ShowWindow(GetDlgItem(hWnd, chx2), SW_HIDE);
130     if (!(*lpFlags & FR_SHOWHELP)) {
131         EnableWindow(GetDlgItem(hWnd, pshHelp), FALSE);
132         ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
133     }
134     ShowWindow(hWnd, SW_SHOWNORMAL);
135     return TRUE;
136 }    
137
138
139 /***********************************************************************
140  *                              FINDDLG_WMCommand               [internal]
141  */
142 static LRESULT FINDDLG_WMCommand(HWND hWnd, WPARAM wParam, 
143                         HWND hwndOwner, LPDWORD lpFlags,
144                         LPSTR lpstrFindWhat, WORD wFindWhatLen, 
145                         BOOL fUnicode)
146 {
147     int uFindReplaceMessage = RegisterWindowMessageA( FINDMSGSTRING );
148     int uHelpMessage = RegisterWindowMessageA( HELPMSGSTRING );
149
150     switch (wParam) {
151         case IDOK:
152             if (fUnicode) 
153               GetDlgItemTextW(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
154               else GetDlgItemTextA(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
155             if (IsDlgButtonChecked(hWnd, rad2))
156                 *lpFlags |= FR_DOWN;
157                 else *lpFlags &= ~FR_DOWN;
158             if (IsDlgButtonChecked(hWnd, chx1))
159                 *lpFlags |= FR_WHOLEWORD; 
160                 else *lpFlags &= ~FR_WHOLEWORD;
161             if (IsDlgButtonChecked(hWnd, chx2))
162                 *lpFlags |= FR_MATCHCASE; 
163                 else *lpFlags &= ~FR_MATCHCASE;
164             *lpFlags &= ~(FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
165             *lpFlags |= FR_FINDNEXT;
166             SendMessageA(hwndOwner, uFindReplaceMessage, 0,
167                           GetWindowLongA(hWnd, DWL_USER) );
168             return TRUE;
169         case IDCANCEL:
170             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL);
171             *lpFlags |= FR_DIALOGTERM;
172             SendMessageA(hwndOwner, uFindReplaceMessage, 0,
173                           GetWindowLongA(hWnd, DWL_USER) );
174             DestroyWindow(hWnd);
175             return TRUE;
176         case pshHelp:
177             /* FIXME : should lpfr structure be passed as an argument ??? */
178             SendMessageA(hwndOwner, uHelpMessage, 0, 0);
179             return TRUE;
180     }
181     return FALSE;
182 }    
183
184
185 /***********************************************************************
186  *           FindTextDlgProc16   (COMMDLG.13)
187  */
188 LRESULT WINAPI FindTextDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
189                                  LPARAM lParam)
190 {
191     LPFINDREPLACE16 lpfr;
192     switch (wMsg) {
193         case WM_INITDIALOG:
194             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(lParam);
195             return FINDDLG_WMInitDialog(hWnd, lParam, &(lpfr->Flags),
196                 PTR_SEG_TO_LIN(lpfr->lpstrFindWhat), FALSE);
197         case WM_COMMAND:
198             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(GetWindowLongA(hWnd, DWL_USER));
199             return FINDDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner,
200                 &lpfr->Flags, PTR_SEG_TO_LIN(lpfr->lpstrFindWhat),
201                 lpfr->wFindWhatLen, FALSE);
202     }
203     return FALSE;
204 }
205
206
207 /***********************************************************************
208  *                              REPLACEDLG_WMInitDialog         [internal]
209  */
210 static LRESULT REPLACEDLG_WMInitDialog(HWND hWnd, LPARAM lParam,
211                     LPDWORD lpFlags, LPSTR lpstrFindWhat, 
212                     LPSTR lpstrReplaceWith, BOOL fUnicode)
213 {
214     SetWindowLongA(hWnd, DWL_USER, lParam);
215     *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
216     /*
217      * FIXME : If the initial FindWhat string is empty, we should disable the FinNext /
218      * Replace / ReplaceAll buttons.  Only after typing some text, the buttons should be
219      * enabled.
220      */
221     if (fUnicode)     
222     {
223         SetDlgItemTextW(hWnd, edt1, (LPWSTR)lpstrFindWhat);
224         SetDlgItemTextW(hWnd, edt2, (LPWSTR)lpstrReplaceWith);
225     } else
226     {
227         SetDlgItemTextA(hWnd, edt1, lpstrFindWhat);
228         SetDlgItemTextA(hWnd, edt2, lpstrReplaceWith);
229     }
230     CheckDlgButton(hWnd, chx1, (*lpFlags & FR_WHOLEWORD) ? 1 : 0);
231     if (*lpFlags & (FR_HIDEWHOLEWORD | FR_NOWHOLEWORD))
232         EnableWindow(GetDlgItem(hWnd, chx1), FALSE);
233     if (*lpFlags & FR_HIDEWHOLEWORD)
234         ShowWindow(GetDlgItem(hWnd, chx1), SW_HIDE);
235     CheckDlgButton(hWnd, chx2, (*lpFlags & FR_MATCHCASE) ? 1 : 0);
236     if (*lpFlags & (FR_HIDEMATCHCASE | FR_NOMATCHCASE))
237         EnableWindow(GetDlgItem(hWnd, chx2), FALSE);
238     if (*lpFlags & FR_HIDEMATCHCASE)
239         ShowWindow(GetDlgItem(hWnd, chx2), SW_HIDE);
240     if (!(*lpFlags & FR_SHOWHELP)) {
241         EnableWindow(GetDlgItem(hWnd, pshHelp), FALSE);
242         ShowWindow(GetDlgItem(hWnd, pshHelp), SW_HIDE);
243     }
244     ShowWindow(hWnd, SW_SHOWNORMAL);
245     return TRUE;
246 }    
247
248
249 /***********************************************************************
250  *                              REPLACEDLG_WMCommand            [internal]
251  */
252 static LRESULT REPLACEDLG_WMCommand(HWND hWnd, WPARAM16 wParam,
253                     HWND hwndOwner, LPDWORD lpFlags,
254                     LPSTR lpstrFindWhat, WORD wFindWhatLen,
255                     LPSTR lpstrReplaceWith, WORD wReplaceWithLen,
256                     BOOL fUnicode)
257 {
258     int uFindReplaceMessage = RegisterWindowMessageA( FINDMSGSTRING );
259     int uHelpMessage = RegisterWindowMessageA( HELPMSGSTRING );
260
261     switch (wParam) {
262         case IDOK:
263             if (fUnicode)
264             {
265                 GetDlgItemTextW(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
266                 GetDlgItemTextW(hWnd, edt2, (LPWSTR)lpstrReplaceWith, wReplaceWithLen/2);
267             }  else
268             {
269                 GetDlgItemTextA(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
270                 GetDlgItemTextA(hWnd, edt2, lpstrReplaceWith, wReplaceWithLen);
271             }
272             if (IsDlgButtonChecked(hWnd, chx1))
273                 *lpFlags |= FR_WHOLEWORD; 
274                 else *lpFlags &= ~FR_WHOLEWORD;
275             if (IsDlgButtonChecked(hWnd, chx2))
276                 *lpFlags |= FR_MATCHCASE; 
277                 else *lpFlags &= ~FR_MATCHCASE;
278             *lpFlags &= ~(FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
279             *lpFlags |= FR_FINDNEXT;
280             SendMessageA(hwndOwner, uFindReplaceMessage, 0,
281                           GetWindowLongA(hWnd, DWL_USER) );
282             return TRUE;
283         case IDCANCEL:
284             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL);
285             *lpFlags |= FR_DIALOGTERM;
286             SendMessageA(hwndOwner, uFindReplaceMessage, 0,
287                           GetWindowLongA(hWnd, DWL_USER) );
288             DestroyWindow(hWnd);
289             return TRUE;
290         case psh1:
291             if (fUnicode)
292             {
293                 GetDlgItemTextW(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
294                 GetDlgItemTextW(hWnd, edt2, (LPWSTR)lpstrReplaceWith, wReplaceWithLen/2);
295             }  else
296             {   
297                 GetDlgItemTextA(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
298                 GetDlgItemTextA(hWnd, edt2, lpstrReplaceWith, wReplaceWithLen);
299             }
300             if (IsDlgButtonChecked(hWnd, chx1))
301                 *lpFlags |= FR_WHOLEWORD; 
302                 else *lpFlags &= ~FR_WHOLEWORD;
303             if (IsDlgButtonChecked(hWnd, chx2))
304                 *lpFlags |= FR_MATCHCASE; 
305                 else *lpFlags &= ~FR_MATCHCASE;
306             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACEALL | FR_DIALOGTERM);
307             *lpFlags |= FR_REPLACE;
308             SendMessageA(hwndOwner, uFindReplaceMessage, 0,
309                           GetWindowLongA(hWnd, DWL_USER) );
310             return TRUE;
311         case psh2:
312             if (fUnicode)
313             {
314                 GetDlgItemTextW(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
315                 GetDlgItemTextW(hWnd, edt2, (LPWSTR)lpstrReplaceWith, wReplaceWithLen/2);
316             }  else
317             {
318                 GetDlgItemTextA(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
319                 GetDlgItemTextA(hWnd, edt2, lpstrReplaceWith, wReplaceWithLen);
320             }
321             if (IsDlgButtonChecked(hWnd, chx1))
322                 *lpFlags |= FR_WHOLEWORD; 
323                 else *lpFlags &= ~FR_WHOLEWORD;
324             if (IsDlgButtonChecked(hWnd, chx2))
325                 *lpFlags |= FR_MATCHCASE; 
326                 else *lpFlags &= ~FR_MATCHCASE;
327             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_DIALOGTERM);
328             *lpFlags |= FR_REPLACEALL;
329             SendMessageA(hwndOwner, uFindReplaceMessage, 0,
330                           GetWindowLongA(hWnd, DWL_USER) );
331             return TRUE;
332         case pshHelp:
333             /* FIXME : should lpfr structure be passed as an argument ??? */
334             SendMessageA(hwndOwner, uHelpMessage, 0, 0);
335             return TRUE;
336     }
337     return FALSE;
338 }    
339
340
341 /***********************************************************************
342  *           ReplaceTextDlgProc16   (COMMDLG.14)
343  */
344 LRESULT WINAPI ReplaceTextDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
345                                     LPARAM lParam)
346 {
347     LPFINDREPLACE16 lpfr;
348     switch (wMsg) {
349         case WM_INITDIALOG:
350             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(lParam);
351             return REPLACEDLG_WMInitDialog(hWnd, lParam, &lpfr->Flags,
352                     PTR_SEG_TO_LIN(lpfr->lpstrFindWhat),
353                     PTR_SEG_TO_LIN(lpfr->lpstrReplaceWith), FALSE);
354         case WM_COMMAND:
355             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(GetWindowLongA(hWnd, DWL_USER));
356             return REPLACEDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner, 
357                     &lpfr->Flags, PTR_SEG_TO_LIN(lpfr->lpstrFindWhat),
358                     lpfr->wFindWhatLen, PTR_SEG_TO_LIN(lpfr->lpstrReplaceWith),
359                     lpfr->wReplaceWithLen, FALSE);
360     }
361     return FALSE;
362 }
363