d718007adb5eb4ffbdf0f1fb8c6651e6efd4d862
[reactos.git] / base / applications / regedit / find.c
1 /*
2 * Regedit find dialog
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19
20 #include <regedit.h>
21
22 static TCHAR s_szFindWhat[256];
23 static const TCHAR s_szFindFlags[] = _T("FindFlags");
24 static const TCHAR s_szFindFlagsR[] = _T("FindFlagsReactOS");
25 static HWND s_hwndAbortDialog;
26 static BOOL s_bAbort;
27
28
29
30 static DWORD GetFindFlags(void)
31 {
32 HKEY hKey;
33 DWORD dwFlags = RSF_LOOKATKEYS;
34 DWORD dwType, dwValue, cbData;
35
36 if (RegOpenKey(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
37 {
38 /* Retrieve flags from registry key */
39 cbData = sizeof(dwValue);
40 if (RegQueryValueEx(hKey, s_szFindFlags, NULL, &dwType, (LPBYTE) &dwValue, &cbData) == ERROR_SUCCESS)
41 {
42 if (dwType == REG_DWORD)
43 dwFlags = (dwFlags & ~0x0000FFFF) | ((dwValue & 0x0000FFFF) << 0);
44 }
45
46 /* Retrieve ReactOS Regedit specific flags from registry key */
47 cbData = sizeof(dwValue);
48 if (RegQueryValueEx(hKey, s_szFindFlagsR, NULL, &dwType, (LPBYTE) &dwValue, &cbData) == ERROR_SUCCESS)
49 {
50 if (dwType == REG_DWORD)
51 dwFlags = (dwFlags & ~0xFFFF0000) | ((dwValue & 0x0000FFFF) << 16);
52 }
53
54 RegCloseKey(hKey);
55 }
56 return dwFlags;
57 }
58
59 static void SetFindFlags(DWORD dwFlags)
60 {
61 HKEY hKey;
62 DWORD dwDisposition;
63 DWORD dwData;
64
65 if (RegCreateKeyEx(HKEY_CURRENT_USER, g_szGeneralRegKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition) == ERROR_SUCCESS)
66 {
67 dwData = (dwFlags >> 0) & 0x0000FFFF;
68 RegSetValueEx(hKey, s_szFindFlags, 0, REG_DWORD, (const BYTE *) &dwData, sizeof(dwData));
69
70 dwData = (dwFlags >> 16) & 0x0000FFFF;
71 RegSetValueEx(hKey, s_szFindFlagsR, 0, REG_DWORD, (const BYTE *) &dwData, sizeof(dwData));
72
73 RegCloseKey(hKey);
74 }
75 }
76
77 static INT_PTR CALLBACK AbortFindDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
78 {
79 UNREFERENCED_PARAMETER(lParam);
80 UNREFERENCED_PARAMETER(hDlg);
81
82 switch(uMsg)
83 {
84 case WM_CLOSE:
85 s_bAbort = TRUE;
86 break;
87
88 case WM_COMMAND:
89 switch(HIWORD(wParam))
90 {
91 case BN_CLICKED:
92 switch(LOWORD(wParam))
93 {
94 case IDCANCEL:
95 s_bAbort = TRUE;
96 break;
97 }
98 break;
99 }
100 break;
101 }
102 return 0;
103 }
104
105 static BOOL RegSearchProc(LPVOID lpParam)
106 {
107 MSG msg;
108 UNREFERENCED_PARAMETER(lpParam);
109
110 if (s_hwndAbortDialog && PeekMessage(&msg, s_hwndAbortDialog, 0, 0, PM_REMOVE))
111 {
112 TranslateMessage(&msg);
113 DispatchMessage(&msg);
114 }
115 return s_bAbort;
116 }
117
118 BOOL FindNext(HWND hWnd)
119 {
120 HKEY hKeyRoot;
121 LPCTSTR pszFindWhat;
122 LPCTSTR pszKeyPath;
123 DWORD dwFlags;
124 LONG lResult;
125 TCHAR szSubKey[512];
126 TCHAR szError[512];
127 TCHAR szTitle[64];
128 TCHAR szFullKey[512];
129
130 pszFindWhat = s_szFindWhat;
131 dwFlags = GetFindFlags() & ~(RSF_LOOKATVALUES | RSF_LOOKATDATA);
132
133 pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
134 lstrcpyn(szSubKey, pszKeyPath, sizeof(szSubKey) / sizeof(szSubKey[0]));
135
136 /* Create abort find dialog */
137 s_hwndAbortDialog = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_FINDING), hWnd, AbortFindDialogProc);
138 if (s_hwndAbortDialog)
139 ShowWindow(s_hwndAbortDialog, SW_SHOW);
140 s_bAbort = FALSE;
141
142 lResult = RegSearch(hKeyRoot, szSubKey, sizeof(szSubKey) / sizeof(szSubKey[0]),
143 pszFindWhat, 0, dwFlags, RegSearchProc, NULL);
144
145 if (s_hwndAbortDialog)
146 {
147 DestroyWindow(s_hwndAbortDialog);
148 s_hwndAbortDialog = NULL;
149 }
150
151 /* Did the user click "Cancel"? If so, exit without displaying an error message */
152 if (lResult == ERROR_OPERATION_ABORTED)
153 return FALSE;
154
155 if (lResult != ERROR_SUCCESS)
156 {
157 LoadString(NULL, IDS_APP_TITLE, szTitle, sizeof(szTitle) / sizeof(szTitle[0]));
158
159 if ((lResult != ERROR_NO_MORE_ITEMS) || !LoadString(NULL, IDS_FINISHEDFIND, szError, sizeof(szError) / sizeof(szError[0])))
160 {
161 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, lResult, 0,
162 szError, sizeof(szError) / sizeof(szError[0]), NULL);
163 }
164 MessageBox(hWnd, szError, szTitle, MB_OK);
165 return FALSE;
166 }
167
168 RegKeyGetName(szFullKey, sizeof(szFullKey) / sizeof(szFullKey[0]), hKeyRoot, szSubKey);
169 SelectNode(g_pChildWnd->hTreeWnd, szFullKey);
170 return TRUE;
171 }
172
173 static INT_PTR CALLBACK FindDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
174 {
175 INT_PTR iResult = 0;
176 HWND hControl;
177 LONG lStyle;
178 DWORD dwFlags;
179 static TCHAR s_szSavedFindValue[256];
180
181 switch(uMsg)
182 {
183 case WM_INITDIALOG:
184 dwFlags = GetFindFlags();
185
186 /* Looking at values is not yet implemented */
187 hControl = GetDlgItem(hDlg, IDC_LOOKAT_KEYS);
188 if (hControl)
189 SendMessage(hControl, BM_SETCHECK, (dwFlags & RSF_LOOKATKEYS) ? TRUE : FALSE, 0);
190
191 /* Looking at values is not yet implemented */
192 hControl = GetDlgItem(hDlg, IDC_LOOKAT_VALUES);
193 if (hControl)
194 {
195 lStyle = GetWindowLongPtr(hControl, GWL_STYLE);
196 SetWindowLongPtr(hControl, GWL_STYLE, lStyle | WS_DISABLED);
197 }
198
199 /* Looking at data is not yet implemented */
200 hControl = GetDlgItem(hDlg, IDC_LOOKAT_DATA);
201 if (hControl)
202 {
203 lStyle = GetWindowLongPtr(hControl, GWL_STYLE);
204 SetWindowLongPtr(hControl, GWL_STYLE, lStyle | WS_DISABLED);
205 }
206
207 /* Match whole string */
208 hControl = GetDlgItem(hDlg, IDC_MATCHSTRING);
209 if (hControl)
210 SendMessage(hControl, BM_SETCHECK, (dwFlags & RSF_WHOLESTRING) ? TRUE : FALSE, 0);
211
212 /* Case sensitivity */
213 hControl = GetDlgItem(hDlg, IDC_MATCHCASE);
214 if (hControl)
215 SendMessage(hControl, BM_SETCHECK, (dwFlags & RSF_MATCHCASE) ? TRUE : FALSE, 0);
216
217 hControl = GetDlgItem(hDlg, IDC_FINDWHAT);
218 if (hControl)
219 {
220 SetWindowText(hControl, s_szSavedFindValue);
221 SetFocus(hControl);
222 SendMessage(hControl, EM_SETSEL, 0, -1);
223 }
224 break;
225
226 case WM_CLOSE:
227 EndDialog(hDlg, 0);
228 break;
229
230 case WM_COMMAND:
231 switch(HIWORD(wParam))
232 {
233 case BN_CLICKED:
234 switch(LOWORD(wParam))
235 {
236 case IDOK:
237 dwFlags = 0;
238
239 hControl = GetDlgItem(hDlg, IDC_LOOKAT_KEYS);
240 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
241 dwFlags |= RSF_LOOKATKEYS;
242
243 hControl = GetDlgItem(hDlg, IDC_LOOKAT_VALUES);
244 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
245 dwFlags |= RSF_LOOKATVALUES;
246
247 hControl = GetDlgItem(hDlg, IDC_LOOKAT_DATA);
248 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
249 dwFlags |= RSF_LOOKATDATA;
250
251 hControl = GetDlgItem(hDlg, IDC_MATCHSTRING);
252 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
253 dwFlags |= RSF_WHOLESTRING;
254
255 hControl = GetDlgItem(hDlg, IDC_MATCHCASE);
256 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
257 dwFlags |= RSF_MATCHCASE;
258
259 SetFindFlags(dwFlags);
260
261 hControl = GetDlgItem(hDlg, IDC_FINDWHAT);
262 if (hControl)
263 GetWindowText(hControl, s_szFindWhat, sizeof(s_szFindWhat) / sizeof(s_szFindWhat[0]));
264 EndDialog(hDlg, 1);
265 break;
266
267 case IDCANCEL:
268 EndDialog(hDlg, 0);
269 break;
270 }
271 break;
272
273 case EN_CHANGE:
274 switch(LOWORD(wParam))
275 {
276 case IDC_FINDWHAT:
277 GetWindowText((HWND) lParam, s_szSavedFindValue, sizeof(s_szSavedFindValue) / sizeof(s_szSavedFindValue[0]));
278 hControl = GetDlgItem(hDlg, IDOK);
279 if (hControl)
280 {
281 lStyle = GetWindowLongPtr(hControl, GWL_STYLE);
282 if (s_szSavedFindValue[0])
283 lStyle &= ~WS_DISABLED;
284 else
285 lStyle |= WS_DISABLED;
286 SetWindowLongPtr(hControl, GWL_STYLE, lStyle);
287 RedrawWindow(hControl, NULL, NULL, RDW_INVALIDATE);
288 }
289 break;
290 }
291 }
292 break;
293 }
294 return iResult;
295 }
296
297 void FindDialog(HWND hWnd)
298 {
299 if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_FIND),
300 hWnd, FindDialogProc, 0) != 0)
301 {
302 FindNext(hWnd);
303 }
304 }
305