[CMAKE]
[reactos.git] / base / applications / regedit / childwnd.c
1 /*
2 * Regedit child window
3 *
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <regedit.h>
22
23 ChildWnd* g_pChildWnd;
24 static int last_split;
25 HBITMAP SizingPattern = 0;
26 HBRUSH SizingBrush = 0;
27 static TCHAR Suggestions[256];
28
29 /*******************************************************************************
30 * Local module support methods
31 */
32
33 static LPCTSTR get_root_key_name(HKEY hRootKey)
34 {
35 if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
36 if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
37 if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
38 if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
39 if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
40 if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
41 return _T("UKNOWN HKEY, PLEASE REPORT");
42 }
43
44 static void draw_splitbar(HWND hWnd, int x)
45 {
46 RECT rt;
47 HGDIOBJ OldObj;
48 HDC hdc = GetDC(hWnd);
49
50 if(!SizingPattern)
51 {
52 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
53 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
54 }
55 if(!SizingBrush)
56 {
57 SizingBrush = CreatePatternBrush(SizingPattern);
58 }
59 GetClientRect(hWnd, &rt);
60 rt.left = x - SPLIT_WIDTH/2;
61 rt.right = x + SPLIT_WIDTH/2+1;
62 OldObj = SelectObject(hdc, SizingBrush);
63 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
64 SelectObject(hdc, OldObj);
65 ReleaseDC(hWnd, hdc);
66 }
67
68 static void ResizeWnd(ChildWnd* pChildWnd, int cx, int cy)
69 {
70 HDWP hdwp = BeginDeferWindowPos(3);
71 RECT rt, rs, rb;
72 const int tHeight = 18;
73 SetRect(&rt, 0, 0, cx, cy);
74 cy = 0;
75 if (hStatusBar != NULL) {
76 GetWindowRect(hStatusBar, &rs);
77 cy = rs.bottom - rs.top;
78 }
79 GetWindowRect(pChildWnd->hAddressBtnWnd, &rb);
80 cx = pChildWnd->nSplitPos + SPLIT_WIDTH/2;
81 DeferWindowPos(hdwp, pChildWnd->hAddressBarWnd, 0, rt.left, rt.top, rt.right-rt.left - tHeight-2, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
82 DeferWindowPos(hdwp, pChildWnd->hAddressBtnWnd, 0, rt.right - tHeight, rt.top, tHeight, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
83 DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top + tHeight+2, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
84 DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx, rt.top + tHeight+2, rt.right-cx, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
85 EndDeferWindowPos(hdwp);
86 }
87
88 static void OnPaint(HWND hWnd)
89 {
90 PAINTSTRUCT ps;
91 RECT rt;
92
93 GetClientRect(hWnd, &rt);
94 BeginPaint(hWnd, &ps);
95 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
96 EndPaint(hWnd, &ps);
97 }
98
99 /*******************************************************************************
100 * finish_splitbar [internal]
101 *
102 * make the splitbar invisible and resize the windows
103 * (helper for ChildWndProc)
104 */
105 static void finish_splitbar(HWND hWnd, int x)
106 {
107 RECT rt;
108
109 draw_splitbar(hWnd, last_split);
110 last_split = -1;
111 GetClientRect(hWnd, &rt);
112 g_pChildWnd->nSplitPos = x;
113 ResizeWnd(g_pChildWnd, rt.right, rt.bottom);
114 ReleaseCapture();
115 }
116
117 /*******************************************************************************
118 *
119 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
120 *
121 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
122 *
123 */
124
125 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
126 {
127 ChildWnd* pChildWnd = g_pChildWnd;
128 HTREEITEM hSelection;
129 HKEY hRootKey;
130 LPCTSTR keyPath, s;
131 WORD wID = LOWORD(wParam);
132
133 UNREFERENCED_PARAMETER(message);
134
135 switch (wID) {
136 /* Parse the menu selections: */
137 case ID_REGISTRY_EXIT:
138 DestroyWindow(hWnd);
139 break;
140 case ID_VIEW_REFRESH:
141 /* TODO */
142 break;
143 case ID_TREE_EXPANDBRANCH:
144 (void)TreeView_Expand(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd), TVE_EXPAND);
145 break;
146 case ID_TREE_COLLAPSEBRANCH:
147 (void)TreeView_Expand(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd), TVE_COLLAPSE);
148 break;
149 case ID_TREE_RENAME:
150 SetFocus(pChildWnd->hTreeWnd);
151 (void)TreeView_EditLabel(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd));
152 break;
153 case ID_TREE_DELETE:
154 hSelection = TreeView_GetSelection(pChildWnd->hTreeWnd);
155 keyPath = GetItemPath(pChildWnd->hTreeWnd, hSelection, &hRootKey);
156
157 if (keyPath == 0 || *keyPath == 0)
158 {
159 MessageBeep(MB_ICONHAND);
160 } else
161 if (DeleteKey(hWnd, hRootKey, keyPath))
162 DeleteNode(g_pChildWnd->hTreeWnd, 0);
163 break;
164 case ID_TREE_EXPORT:
165 ExportRegistryFile(pChildWnd->hTreeWnd);
166 break;
167 case ID_EDIT_FIND:
168 FindDialog(hWnd);
169 break;
170 case ID_EDIT_COPYKEYNAME:
171 hSelection = TreeView_GetSelection(pChildWnd->hTreeWnd);
172 keyPath = GetItemPath(pChildWnd->hTreeWnd, hSelection, &hRootKey);
173 CopyKeyName(hWnd, hRootKey, keyPath);
174 break;
175 case ID_EDIT_NEW_KEY:
176 CreateNewKey(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd));
177 break;
178 case ID_EDIT_NEW_STRINGVALUE:
179 case ID_EDIT_NEW_BINARYVALUE:
180 case ID_EDIT_NEW_DWORDVALUE:
181 SendMessage(hFrameWnd, WM_COMMAND, wParam, lParam);
182 break;
183 case ID_SWITCH_PANELS:
184 pChildWnd->nFocusPanel = !pChildWnd->nFocusPanel;
185 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
186 break;
187 default:
188 if ((wID >= ID_TREE_SUGGESTION_MIN) && (wID <= ID_TREE_SUGGESTION_MAX))
189 {
190 s = Suggestions;
191 while(wID > ID_TREE_SUGGESTION_MIN)
192 {
193 if (*s)
194 s += _tcslen(s) + 1;
195 wID--;
196 }
197 SelectNode(pChildWnd->hTreeWnd, s);
198 break;
199 }
200 return FALSE;
201 }
202 return TRUE;
203 }
204
205 /*******************************************************************************
206 *
207 * Key suggestion
208 */
209
210 #define MIN(a,b) ((a < b) ? (a) : (b))
211
212 static void SuggestKeys(HKEY hRootKey, LPCTSTR pszKeyPath, LPTSTR pszSuggestions,
213 size_t iSuggestionsLength)
214 {
215 TCHAR szBuffer[256];
216 TCHAR szLastFound[256];
217 size_t i;
218 HKEY hOtherKey, hSubKey;
219 BOOL bFound;
220
221 memset(pszSuggestions, 0, iSuggestionsLength * sizeof(*pszSuggestions));
222 iSuggestionsLength--;
223
224 /* Are we a root key in HKEY_CLASSES_ROOT? */
225 if ((hRootKey == HKEY_CLASSES_ROOT) && pszKeyPath[0] && !_tcschr(pszKeyPath, TEXT('\\')))
226 {
227 do
228 {
229 bFound = FALSE;
230
231 /* Check default key */
232 if (QueryStringValue(hRootKey, pszKeyPath, NULL,
233 szBuffer, COUNT_OF(szBuffer)) == ERROR_SUCCESS)
234 {
235 /* Sanity check this key; it cannot be empty, nor can it be a
236 * loop back */
237 if ((szBuffer[0] != '\0') && _tcsicmp(szBuffer, pszKeyPath))
238 {
239 if (RegOpenKey(hRootKey, szBuffer, &hOtherKey) == ERROR_SUCCESS)
240 {
241 lstrcpyn(pszSuggestions, TEXT("HKCR\\"), (int) iSuggestionsLength);
242 i = _tcslen(pszSuggestions);
243 pszSuggestions += i;
244 iSuggestionsLength -= i;
245
246 lstrcpyn(pszSuggestions, szBuffer, (int) iSuggestionsLength);
247 i = MIN(_tcslen(pszSuggestions) + 1, iSuggestionsLength);
248 pszSuggestions += i;
249 iSuggestionsLength -= i;
250 RegCloseKey(hOtherKey);
251
252 bFound = TRUE;
253 _tcscpy(szLastFound, szBuffer);
254 pszKeyPath = szLastFound;
255 }
256 }
257 }
258 }
259 while(bFound && (iSuggestionsLength > 0));
260
261 /* Check CLSID key */
262 if (RegOpenKey(hRootKey, pszKeyPath, &hSubKey) == ERROR_SUCCESS)
263 {
264 if (QueryStringValue(hSubKey, TEXT("CLSID"), NULL, szBuffer,
265 COUNT_OF(szBuffer)) == ERROR_SUCCESS)
266 {
267 lstrcpyn(pszSuggestions, TEXT("HKCR\\CLSID\\"), (int) iSuggestionsLength);
268 i = _tcslen(pszSuggestions);
269 pszSuggestions += i;
270 iSuggestionsLength -= i;
271
272 lstrcpyn(pszSuggestions, szBuffer, (int) iSuggestionsLength);
273 i = MIN(_tcslen(pszSuggestions) + 1, iSuggestionsLength);
274 pszSuggestions += i;
275 iSuggestionsLength -= i;
276 }
277 RegCloseKey(hSubKey);
278 }
279 }
280 }
281
282
283 LRESULT CALLBACK AddressBarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
284 {
285 WNDPROC oldwndproc;
286 static TCHAR s_szNode[256];
287 oldwndproc = (WNDPROC)(LONG_PTR)GetWindowLongPtr(hwnd, GWL_USERDATA);
288
289 switch (uMsg)
290 {
291 case WM_KEYUP:
292 if (wParam == VK_RETURN)
293 {
294 GetWindowText(hwnd, s_szNode, sizeof(s_szNode) / sizeof(s_szNode[0]));
295 SelectNode(g_pChildWnd->hTreeWnd, s_szNode);
296 }
297 break;
298 default:
299 break;
300 }
301 return CallWindowProc(oldwndproc, hwnd, uMsg, wParam, lParam);
302 }
303
304 /*******************************************************************************
305 *
306 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
307 *
308 * PURPOSE: Processes messages for the child windows.
309 *
310 * WM_COMMAND - process the application menu
311 * WM_PAINT - Paint the main window
312 * WM_DESTROY - post a quit message and return
313 *
314 */
315 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
316 {
317 BOOL Result;
318 ChildWnd* pChildWnd = g_pChildWnd;
319
320 switch (message) {
321 case WM_CREATE:
322 {
323 WNDPROC oldproc;
324 HFONT hFont;
325 TCHAR buffer[MAX_PATH];
326 /* load "My Computer" string */
327 LoadString(hInst, IDS_MY_COMPUTER, buffer, sizeof(buffer)/sizeof(TCHAR));
328
329 g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ChildWnd));
330
331 if (!pChildWnd) return 0;
332 _tcsncpy(pChildWnd->szPath, buffer, MAX_PATH);
333 pChildWnd->nSplitPos = 250;
334 pChildWnd->hWnd = hWnd;
335 pChildWnd->hAddressBarWnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP,
336 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
337 hWnd, (HMENU)0, hInst, 0);
338 pChildWnd->hAddressBtnWnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("Button"), _T("ยป"), WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP | BS_DEFPUSHBUTTON,
339 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
340 hWnd, (HMENU)0, hInst, 0);
341 pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, (HMENU) TREE_WINDOW);
342 pChildWnd->hListWnd = CreateListView(hWnd, (HMENU) LIST_WINDOW/*, pChildWnd->szPath*/);
343 SetFocus(pChildWnd->hTreeWnd);
344
345 /* set the address bar and button font */
346 if ((pChildWnd->hAddressBarWnd) && (pChildWnd->hAddressBtnWnd))
347 {
348 hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
349 SendMessage(pChildWnd->hAddressBarWnd,
350 WM_SETFONT,
351 (WPARAM)hFont,
352 0);
353 SendMessage(pChildWnd->hAddressBtnWnd,
354 WM_SETFONT,
355 (WPARAM)hFont,
356 0);
357 }
358 /* Subclass the AddressBar */
359 oldproc = (WNDPROC)(LONG_PTR)GetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_WNDPROC);
360 SetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_USERDATA, (DWORD_PTR)oldproc);
361 SetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_WNDPROC, (DWORD_PTR)AddressBarProc);
362 break;
363 }
364 case WM_COMMAND:
365 if(HIWORD(wParam) == BN_CLICKED){
366 PostMessage(pChildWnd->hAddressBarWnd, WM_KEYUP, VK_RETURN, 0);
367 }
368 else if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
369 goto def;
370 }
371 break;
372 case WM_PAINT:
373 OnPaint(hWnd);
374 return 0;
375 case WM_SETCURSOR:
376 if (LOWORD(lParam) == HTCLIENT) {
377 POINT pt;
378 GetCursorPos(&pt);
379 ScreenToClient(hWnd, &pt);
380 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
381 SetCursor(LoadCursor(0, IDC_SIZEWE));
382 return TRUE;
383 }
384 }
385 goto def;
386 case WM_DESTROY:
387 DestroyTreeView();
388 DestroyListView(pChildWnd->hListWnd);
389 DestroyMainMenu();
390 HeapFree(GetProcessHeap(), 0, pChildWnd);
391 pChildWnd = NULL;
392 PostQuitMessage(0);
393 break;
394 case WM_LBUTTONDOWN: {
395 RECT rt;
396 int x = (short)LOWORD(lParam);
397 GetClientRect(hWnd, &rt);
398 if (x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
399 last_split = pChildWnd->nSplitPos;
400 draw_splitbar(hWnd, last_split);
401 SetCapture(hWnd);
402 }
403 break;
404 }
405
406 case WM_LBUTTONUP:
407 case WM_RBUTTONDOWN:
408 if (GetCapture() == hWnd) {
409 finish_splitbar(hWnd, LOWORD(lParam));
410 }
411 break;
412
413 case WM_CAPTURECHANGED:
414 if (GetCapture()==hWnd && last_split>=0)
415 draw_splitbar(hWnd, last_split);
416 break;
417
418 case WM_KEYDOWN:
419 if (wParam == VK_ESCAPE)
420 if (GetCapture() == hWnd) {
421 RECT rt;
422 draw_splitbar(hWnd, last_split);
423 GetClientRect(hWnd, &rt);
424 ResizeWnd(pChildWnd, rt.right, rt.bottom);
425 last_split = -1;
426 ReleaseCapture();
427 SetCursor(LoadCursor(0, IDC_ARROW));
428 }
429 break;
430
431 case WM_MOUSEMOVE:
432 if (GetCapture() == hWnd) {
433 HDC hdc;
434 RECT rt;
435 HGDIOBJ OldObj;
436 int x = LOWORD(lParam);
437 if(!SizingPattern)
438 {
439 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
440 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
441 }
442 if(!SizingBrush)
443 {
444 SizingBrush = CreatePatternBrush(SizingPattern);
445 }
446
447 GetClientRect(hWnd, &rt);
448 x = (SHORT) min(max(x, SPLIT_MIN), rt.right - SPLIT_MIN);
449 if(last_split != x)
450 {
451 rt.left = last_split-SPLIT_WIDTH/2;
452 rt.right = last_split+SPLIT_WIDTH/2+1;
453 hdc = GetDC(hWnd);
454 OldObj = SelectObject(hdc, SizingBrush);
455 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
456 last_split = x;
457 rt.left = x-SPLIT_WIDTH/2;
458 rt.right = x+SPLIT_WIDTH/2+1;
459 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
460 SelectObject(hdc, OldObj);
461 ReleaseDC(hWnd, hdc);
462 }
463 }
464 break;
465
466 case WM_SETFOCUS:
467 if (pChildWnd != NULL) {
468 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
469 }
470 break;
471
472 case WM_TIMER:
473 break;
474
475 case WM_NOTIFY:
476 if ((int)wParam == TREE_WINDOW) {
477 switch (((LPNMHDR)lParam)->code) {
478 case TVN_ITEMEXPANDING:
479 return !OnTreeExpanding(pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
480 case TVN_SELCHANGED: {
481 LPCTSTR keyPath, rootName;
482 LPTSTR fullPath;
483 HKEY hRootKey;
484
485 keyPath = GetItemPath(pChildWnd->hTreeWnd, ((NMTREEVIEW*)lParam)->itemNew.hItem, &hRootKey);
486 if (keyPath) {
487 RefreshListView(pChildWnd->hListWnd, hRootKey, keyPath);
488 rootName = get_root_key_name(hRootKey);
489 fullPath = HeapAlloc(GetProcessHeap(), 0, (_tcslen(rootName) + 1 + _tcslen(keyPath) + 1) * sizeof(TCHAR));
490 if (fullPath) {
491 /* set (correct) the address bar text */
492 if(keyPath[0] != '\0')
493 _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
494 else
495 fullPath = _tcscpy(fullPath, rootName);
496 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
497 SendMessage(pChildWnd->hAddressBarWnd, WM_SETTEXT, 0, (LPARAM)fullPath);
498 HeapFree(GetProcessHeap(), 0, fullPath);
499 /* disable hive manipulation items temporarily (enable only if necessary) */
500 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_GRAYED);
501 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_GRAYED);
502 /* compare the strings to see if we should enable/disable the "Load Hive" menus accordingly */
503 if (!(_tcsicmp(rootName, TEXT("HKEY_LOCAL_MACHINE")) &&
504 _tcsicmp(rootName, TEXT("HKEY_USERS"))))
505 {
506 // enable the unload menu item if at the root
507 // otherwise enable the load menu item if there is no slash in keyPath (ie. immediate child selected)
508 if(keyPath[0] == '\0')
509 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_ENABLED);
510 else if(!_tcschr(keyPath, _T('\\')))
511 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_ENABLED);
512 }
513
514 {
515 HKEY hKey;
516 TCHAR szBuffer[MAX_PATH];
517 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("My Computer\\%s\\%s"), rootName, keyPath);
518
519 if (RegCreateKey(HKEY_CURRENT_USER,
520 g_szGeneralRegKey,
521 &hKey) == ERROR_SUCCESS)
522 {
523 RegSetValueEx(hKey, _T("LastKey"), 0, REG_SZ, (LPBYTE) szBuffer, (DWORD) _tcslen(szBuffer) * sizeof(szBuffer[0]));
524 RegCloseKey(hKey);
525 }
526 }
527 }
528 }
529 }
530 break;
531 case NM_SETFOCUS:
532 pChildWnd->nFocusPanel = 0;
533 break;
534 case TVN_BEGINLABELEDIT:
535 {
536 LPNMTVDISPINFO ptvdi;
537 /* cancel label edit for rootkeys */
538 ptvdi = (LPNMTVDISPINFO) lParam;
539 if (!TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
540 !TreeView_GetParent(pChildWnd->hTreeWnd, TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem)))
541 return TRUE;
542 break;
543 }
544 case TVN_ENDLABELEDIT:
545 {
546 LPCTSTR keyPath;
547 HKEY hRootKey;
548 HKEY hKey = NULL;
549 LPNMTVDISPINFO ptvdi;
550 LONG lResult = TRUE;
551 TCHAR szBuffer[MAX_PATH];
552
553 ptvdi = (LPNMTVDISPINFO) lParam;
554 if (ptvdi->item.pszText)
555 {
556 keyPath = GetItemPath(pChildWnd->hTreeWnd, TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
557 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("%s\\%s"), keyPath, ptvdi->item.pszText);
558 keyPath = GetItemPath(pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
559 if (RegOpenKeyEx(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
560 {
561 lResult = FALSE;
562 RegCloseKey(hKey);
563 (void)TreeView_EditLabel(pChildWnd->hTreeWnd, ptvdi->item.hItem);
564 }
565 else
566 {
567 if (RenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
568 lResult = FALSE;
569 }
570 return lResult;
571 }
572 }
573 default:
574 return 0;
575 }
576 } else
577 {
578 if ((int)wParam == LIST_WINDOW)
579 {
580 switch (((LPNMHDR)lParam)->code) {
581 case NM_SETFOCUS:
582 pChildWnd->nFocusPanel = 1;
583 break;
584 default:
585 if(!ListWndNotifyProc(pChildWnd->hListWnd, wParam, lParam, &Result))
586 {
587 goto def;
588 }
589 return Result;
590 break;
591 }
592 }
593 }
594 break;
595
596 case WM_CONTEXTMENU:
597 {
598 POINT pt;
599 if((HWND)wParam == pChildWnd->hListWnd)
600 {
601 int i, cnt;
602 BOOL IsDefault;
603 pt.x = (short) LOWORD(lParam);
604 pt.y = (short) HIWORD(lParam);
605 cnt = ListView_GetSelectedCount(pChildWnd->hListWnd);
606 i = ListView_GetNextItem(pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
607 if (pt.x == -1 && pt.y == -1)
608 {
609 RECT rc;
610 if (i != -1)
611 {
612 rc.left = LVIR_BOUNDS;
613 SendMessage(pChildWnd->hListWnd, LVM_GETITEMRECT, i, (LPARAM) &rc);
614 pt.x = rc.left + 8;
615 pt.y = rc.top + 8;
616 }
617 else
618 pt.x = pt.y = 0;
619 ClientToScreen(pChildWnd->hListWnd, &pt);
620 }
621 if(i == -1)
622 {
623 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
624 }
625 else
626 {
627 HMENU mnu = GetSubMenu(hPopupMenus, PM_MODIFYVALUE);
628 SetMenuDefaultItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND);
629 IsDefault = IsDefaultValue(pChildWnd->hListWnd, i);
630 if(cnt == 1)
631 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | (IsDefault ? MF_DISABLED | MF_GRAYED : MF_ENABLED));
632 else
633 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
634 EnableMenuItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
635 EnableMenuItem(mnu, ID_EDIT_MODIFY_BIN, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
636
637 TrackPopupMenu(mnu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
638 }
639 }
640 else if ((HWND)wParam == pChildWnd->hTreeWnd)
641 {
642 TVHITTESTINFO hti;
643 HMENU hContextMenu;
644 TVITEM item;
645 MENUITEMINFO mii;
646 TCHAR resource[256];
647 TCHAR buffer[256];
648 LPTSTR s;
649 LPCTSTR keyPath;
650 HKEY hRootKey;
651 int iLastPos;
652 WORD wID;
653
654 pt.x = (short) LOWORD(lParam);
655 pt.y = (short) HIWORD(lParam);
656
657 if (pt.x == -1 && pt.y == -1)
658 {
659 RECT rc;
660 hti.hItem = TreeView_GetSelection(pChildWnd->hTreeWnd);
661 if (hti.hItem != NULL)
662 {
663 TreeView_GetItemRect(pChildWnd->hTreeWnd, hti.hItem, &rc, TRUE);
664 pt.x = rc.left + 8;
665 pt.y = rc.top + 8;
666 ClientToScreen(pChildWnd->hTreeWnd, &pt);
667 hti.flags = TVHT_ONITEM;
668 }
669 else
670 hti.flags = 0;
671 }
672 else
673 {
674 hti.pt.x = pt.x;
675 hti.pt.y = pt.y;
676 ScreenToClient(pChildWnd->hTreeWnd, &hti.pt);
677 (void)TreeView_HitTest(pChildWnd->hTreeWnd, &hti);
678 }
679
680 if (hti.flags & TVHT_ONITEM)
681 {
682 hContextMenu = GetSubMenu(hPopupMenus, PM_TREECONTEXT);
683 (void)TreeView_SelectItem(pChildWnd->hTreeWnd, hti.hItem);
684
685 memset(&item, 0, sizeof(item));
686 item.mask = TVIF_STATE | TVIF_CHILDREN;
687 item.hItem = hti.hItem;
688 (void)TreeView_GetItem(pChildWnd->hTreeWnd, &item);
689
690 /* Set the Expand/Collapse menu item appropriately */
691 LoadString(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, sizeof(buffer) / sizeof(buffer[0]));
692 memset(&mii, 0, sizeof(mii));
693 mii.cbSize = sizeof(mii);
694 mii.fMask = MIIM_STRING | MIIM_STATE | MIIM_ID;
695 mii.fState = (item.cChildren > 0) ? MFS_DEFAULT : MFS_GRAYED;
696 mii.wID = (item.state & TVIS_EXPANDED) ? ID_TREE_COLLAPSEBRANCH : ID_TREE_EXPANDBRANCH;
697 mii.dwTypeData = (LPTSTR) buffer;
698 SetMenuItemInfo(hContextMenu, 0, TRUE, &mii);
699
700 /* Remove any existing suggestions */
701 memset(&mii, 0, sizeof(mii));
702 mii.cbSize = sizeof(mii);
703 mii.fMask = MIIM_ID;
704 GetMenuItemInfo(hContextMenu, GetMenuItemCount(hContextMenu) - 1, TRUE, &mii);
705 if ((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX))
706 {
707 do
708 {
709 iLastPos = GetMenuItemCount(hContextMenu) - 1;
710 GetMenuItemInfo(hContextMenu, iLastPos, TRUE, &mii);
711 RemoveMenu(hContextMenu, iLastPos, MF_BYPOSITION);
712 }
713 while((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX));
714 }
715
716 /* Come up with suggestions */
717 keyPath = GetItemPath(pChildWnd->hTreeWnd, NULL, &hRootKey);
718 SuggestKeys(hRootKey, keyPath, Suggestions, sizeof(Suggestions) / sizeof(Suggestions[0]));
719 if (Suggestions[0])
720 {
721 AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
722
723 LoadString(hInst, IDS_GOTO_SUGGESTED_KEY, resource, sizeof(resource) / sizeof(resource[0]));
724
725 s = Suggestions;
726 wID = ID_TREE_SUGGESTION_MIN;
727 while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
728 {
729 _sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), resource, s);
730
731 memset(&mii, 0, sizeof(mii));
732 mii.cbSize = sizeof(mii);
733 mii.fMask = MIIM_STRING | MIIM_ID;
734 mii.wID = wID++;
735 mii.dwTypeData = buffer;
736 InsertMenuItem(hContextMenu, GetMenuItemCount(hContextMenu), TRUE, &mii);
737
738 s += _tcslen(s) + 1;
739 }
740 }
741 TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, pChildWnd->hWnd, NULL);
742 }
743 }
744 break;
745 }
746
747 case WM_SIZE:
748 if (wParam != SIZE_MINIMIZED && pChildWnd != NULL) {
749 ResizeWnd(pChildWnd, LOWORD(lParam), HIWORD(lParam));
750 }
751 /* fall through */
752 default: def:
753 return DefWindowProc(hWnd, message, wParam, lParam);
754 }
755 return 0;
756 }