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