- Get autochk, calc, cmd, devmgr, expand, format, gettype, hostname, lsass, msconfig...
[reactos.git] / reactos / subsys / system / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 switch(uMsg)
80 {
81 case WM_CLOSE:
82 s_bAbort = TRUE;
83 break;
84
85 case WM_COMMAND:
86 switch(HIWORD(wParam))
87 {
88 case BN_CLICKED:
89 switch(LOWORD(wParam))
90 {
91 case IDCANCEL:
92 s_bAbort = TRUE;
93 break;
94 }
95 break;
96 }
97 break;
98 }
99 return 0;
100 }
101
102 static BOOL RegSearchProc(LPVOID lpParam)
103 {
104 MSG msg;
105
106 if (s_hwndAbortDialog && PeekMessage(&msg, s_hwndAbortDialog, 0, 0, PM_REMOVE))
107 {
108 TranslateMessage(&msg);
109 DispatchMessage(&msg);
110 }
111 return s_bAbort;
112 }
113
114 BOOL FindNext(HWND hWnd)
115 {
116 HKEY hKeyRoot;
117 LPCTSTR pszFindWhat;
118 LPCTSTR pszKeyPath;
119 DWORD dwFlags;
120 LONG lResult;
121 TCHAR szSubKey[512];
122 TCHAR szError[512];
123 TCHAR szTitle[64];
124 TCHAR szFullKey[512];
125
126 pszFindWhat = s_szFindWhat;
127 dwFlags = GetFindFlags() & ~(RSF_LOOKATVALUES | RSF_LOOKATDATA);
128
129 pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
130 lstrcpyn(szSubKey, pszKeyPath, sizeof(szSubKey) / sizeof(szSubKey[0]));
131
132 /* Create abort find dialog */
133 s_hwndAbortDialog = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_FINDING), hWnd, AbortFindDialogProc);
134 if (s_hwndAbortDialog)
135 ShowWindow(s_hwndAbortDialog, SW_SHOW);
136 s_bAbort = FALSE;
137
138 lResult = RegSearch(hKeyRoot, szSubKey, sizeof(szSubKey) / sizeof(szSubKey[0]),
139 pszFindWhat, 0, dwFlags, RegSearchProc, NULL);
140
141 if (s_hwndAbortDialog)
142 {
143 DestroyWindow(s_hwndAbortDialog);
144 s_hwndAbortDialog = NULL;
145 }
146
147 /* Did the user click "Cancel"? If so, exit without displaying an error message */
148 if (lResult == ERROR_OPERATION_ABORTED)
149 return FALSE;
150
151 if (lResult != ERROR_SUCCESS)
152 {
153 LoadString(NULL, IDS_APP_TITLE, szTitle, sizeof(szTitle) / sizeof(szTitle[0]));
154
155 if ((lResult != ERROR_NO_MORE_ITEMS) || !LoadString(NULL, IDS_FINISHEDFIND, szError, sizeof(szError) / sizeof(szError[0])))
156 {
157 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, lResult, 0,
158 szError, sizeof(szError) / sizeof(szError[0]), NULL);
159 }
160 MessageBox(hWnd, szError, szTitle, MB_OK);
161 return FALSE;
162 }
163
164 RegKeyGetName(szFullKey, sizeof(szFullKey) / sizeof(szFullKey[0]), hKeyRoot, szSubKey);
165 SelectNode(g_pChildWnd->hTreeWnd, szFullKey);
166 return TRUE;
167 }
168
169 static INT_PTR CALLBACK FindDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
170 {
171 INT_PTR iResult = 0;
172 HWND hControl;
173 LONG lStyle;
174 DWORD dwFlags;
175 static TCHAR s_szSavedFindValue[256];
176
177 switch(uMsg)
178 {
179 case WM_INITDIALOG:
180 dwFlags = GetFindFlags();
181
182 /* Looking at values is not yet implemented */
183 hControl = GetDlgItem(hDlg, IDC_LOOKAT_KEYS);
184 if (hControl)
185 SendMessage(hControl, BM_SETCHECK, (dwFlags & RSF_LOOKATKEYS) ? TRUE : FALSE, 0);
186
187 /* Looking at values is not yet implemented */
188 hControl = GetDlgItem(hDlg, IDC_LOOKAT_VALUES);
189 if (hControl)
190 {
191 lStyle = GetWindowLong(hControl, GWL_STYLE);
192 SetWindowLong(hControl, GWL_STYLE, lStyle | WS_DISABLED);
193 }
194
195 /* Looking at data is not yet implemented */
196 hControl = GetDlgItem(hDlg, IDC_LOOKAT_DATA);
197 if (hControl)
198 {
199 lStyle = GetWindowLong(hControl, GWL_STYLE);
200 SetWindowLong(hControl, GWL_STYLE, lStyle | WS_DISABLED);
201 }
202
203 /* Match whole string */
204 hControl = GetDlgItem(hDlg, IDC_MATCHSTRING);
205 if (hControl)
206 SendMessage(hControl, BM_SETCHECK, (dwFlags & RSF_WHOLESTRING) ? TRUE : FALSE, 0);
207
208 /* Case sensitivity */
209 hControl = GetDlgItem(hDlg, IDC_MATCHCASE);
210 if (hControl)
211 SendMessage(hControl, BM_SETCHECK, (dwFlags & RSF_MATCHCASE) ? TRUE : FALSE, 0);
212
213 hControl = GetDlgItem(hDlg, IDC_FINDWHAT);
214 if (hControl)
215 {
216 SetWindowText(hControl, s_szSavedFindValue);
217 SetFocus(hControl);
218 SendMessage(hControl, EM_SETSEL, 0, -1);
219 }
220 break;
221
222 case WM_CLOSE:
223 EndDialog(hDlg, 0);
224 break;
225
226 case WM_COMMAND:
227 switch(HIWORD(wParam))
228 {
229 case BN_CLICKED:
230 switch(LOWORD(wParam))
231 {
232 case IDOK:
233 dwFlags = 0;
234
235 hControl = GetDlgItem(hDlg, IDC_LOOKAT_KEYS);
236 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
237 dwFlags |= RSF_LOOKATKEYS;
238
239 hControl = GetDlgItem(hDlg, IDC_LOOKAT_VALUES);
240 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
241 dwFlags |= RSF_LOOKATVALUES;
242
243 hControl = GetDlgItem(hDlg, IDC_LOOKAT_DATA);
244 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
245 dwFlags |= RSF_LOOKATDATA;
246
247 hControl = GetDlgItem(hDlg, IDC_MATCHSTRING);
248 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
249 dwFlags |= RSF_WHOLESTRING;
250
251 hControl = GetDlgItem(hDlg, IDC_MATCHCASE);
252 if (hControl && (SendMessage(hControl, BM_GETCHECK, 0, 0) == BST_CHECKED))
253 dwFlags |= RSF_MATCHCASE;
254
255 SetFindFlags(dwFlags);
256
257 hControl = GetDlgItem(hDlg, IDC_FINDWHAT);
258 if (hControl)
259 GetWindowText(hControl, s_szFindWhat, sizeof(s_szFindWhat) / sizeof(s_szFindWhat[0]));
260 EndDialog(hDlg, 1);
261 break;
262
263 case IDCANCEL:
264 EndDialog(hDlg, 0);
265 break;
266 }
267 break;
268
269 case EN_CHANGE:
270 switch(LOWORD(wParam))
271 {
272 case IDC_FINDWHAT:
273 GetWindowText((HWND) lParam, s_szSavedFindValue, sizeof(s_szSavedFindValue) / sizeof(s_szSavedFindValue[0]));
274 hControl = GetDlgItem(hDlg, IDOK);
275 if (hControl)
276 {
277 lStyle = GetWindowLong(hControl, GWL_STYLE);
278 if (s_szSavedFindValue[0])
279 lStyle &= ~WS_DISABLED;
280 else
281 lStyle |= WS_DISABLED;
282 SetWindowLong(hControl, GWL_STYLE, lStyle);
283 RedrawWindow(hControl, NULL, NULL, RDW_INVALIDATE);
284 }
285 break;
286 }
287 }
288 break;
289 }
290 return iResult;
291 }
292
293 void FindDialog(HWND hWnd)
294 {
295 if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_FIND),
296 hWnd, FindDialogProc, 0) != 0)
297 {
298 FindNext(hWnd);
299 }
300 }
301