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