e927478ac23cd071444835b814c2f3c403d49b6c
[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 extern LPCTSTR get_root_key_name(HKEY hRootKey)
30 {
31 if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
32 if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
33 if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
34 if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
35 if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
36 if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
37 return _T("UKNOWN HKEY, PLEASE REPORT");
38 }
39
40 extern void ResizeWnd(int cx, int cy)
41 {
42 HDWP hdwp = BeginDeferWindowPos(3);
43 RECT rt, rs, rb;
44 const int tHeight = 18;
45 SetRect(&rt, 0, 0, cx, cy);
46 cy = 0;
47 if (hStatusBar != NULL)
48 {
49 GetWindowRect(hStatusBar, &rs);
50 cy = rs.bottom - rs.top;
51 }
52 GetWindowRect(g_pChildWnd->hAddressBtnWnd, &rb);
53 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
54 DeferWindowPos(hdwp, g_pChildWnd->hAddressBarWnd, 0, rt.left, rt.top, rt.right-rt.left - tHeight-2, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
55 DeferWindowPos(hdwp, g_pChildWnd->hAddressBtnWnd, 0, rt.right - tHeight, rt.top, tHeight, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
56 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);
57 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);
58 EndDeferWindowPos(hdwp);
59 }
60
61 /*******************************************************************************
62 * Local module support methods
63 */
64
65 static void draw_splitbar(HWND hWnd, int x)
66 {
67 RECT rt;
68 HGDIOBJ OldObj;
69 HDC hdc = GetDC(hWnd);
70
71 if(!SizingPattern)
72 {
73 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
74 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
75 }
76 if(!SizingBrush)
77 {
78 SizingBrush = CreatePatternBrush(SizingPattern);
79 }
80 GetClientRect(hWnd, &rt);
81 rt.left = x - SPLIT_WIDTH/2;
82 rt.right = x + SPLIT_WIDTH/2+1;
83 OldObj = SelectObject(hdc, SizingBrush);
84 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
85 SelectObject(hdc, OldObj);
86 ReleaseDC(hWnd, hdc);
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, COUNT_OF(s_szNode));
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, COUNT_OF(buffer));
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 }
533 break;
534 case NM_SETFOCUS:
535 g_pChildWnd->nFocusPanel = 0;
536 break;
537 case TVN_BEGINLABELEDIT:
538 {
539 LPNMTVDISPINFO ptvdi;
540 /* cancel label edit for rootkeys */
541 ptvdi = (LPNMTVDISPINFO) lParam;
542 if (!TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
543 !TreeView_GetParent(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem)))
544 return TRUE;
545 break;
546 }
547 case TVN_ENDLABELEDIT:
548 {
549 LPCTSTR keyPath;
550 HKEY hRootKey;
551 HKEY hKey = NULL;
552 LPNMTVDISPINFO ptvdi;
553 LONG lResult = TRUE;
554 TCHAR szBuffer[MAX_PATH];
555
556 ptvdi = (LPNMTVDISPINFO) lParam;
557 if (ptvdi->item.pszText)
558 {
559 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
560 _sntprintf(szBuffer, COUNT_OF(szBuffer), _T("%s\\%s"), keyPath, ptvdi->item.pszText);
561 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
562 if (RegOpenKeyEx(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
563 {
564 lResult = FALSE;
565 RegCloseKey(hKey);
566 (void)TreeView_EditLabel(g_pChildWnd->hTreeWnd, ptvdi->item.hItem);
567 }
568 else
569 {
570 if (RenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
571 lResult = FALSE;
572 }
573 return lResult;
574 }
575 }
576 default:
577 return 0;
578 }
579 }
580 else
581 {
582 if ((int)wParam == LIST_WINDOW && g_pChildWnd != NULL)
583 {
584 switch (((LPNMHDR)lParam)->code)
585 {
586 case NM_SETFOCUS:
587 g_pChildWnd->nFocusPanel = 1;
588 break;
589 default:
590 if(!ListWndNotifyProc(g_pChildWnd->hListWnd, wParam, lParam, &Result))
591 {
592 goto def;
593 }
594 return Result;
595 break;
596 }
597 }
598 }
599 break;
600
601 case WM_CONTEXTMENU:
602 {
603 POINT pt;
604 if((HWND)wParam == g_pChildWnd->hListWnd)
605 {
606 int i, cnt;
607 BOOL IsDefault;
608 pt.x = (short) LOWORD(lParam);
609 pt.y = (short) HIWORD(lParam);
610 cnt = ListView_GetSelectedCount(g_pChildWnd->hListWnd);
611 i = ListView_GetNextItem(g_pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
612 if (pt.x == -1 && pt.y == -1)
613 {
614 RECT rc;
615 if (i != -1)
616 {
617 rc.left = LVIR_BOUNDS;
618 SendMessage(g_pChildWnd->hListWnd, LVM_GETITEMRECT, i, (LPARAM) &rc);
619 pt.x = rc.left + 8;
620 pt.y = rc.top + 8;
621 }
622 else
623 pt.x = pt.y = 0;
624 ClientToScreen(g_pChildWnd->hListWnd, &pt);
625 }
626 if(i == -1)
627 {
628 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
629 }
630 else
631 {
632 HMENU mnu = GetSubMenu(hPopupMenus, PM_MODIFYVALUE);
633 SetMenuDefaultItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND);
634 IsDefault = IsDefaultValue(g_pChildWnd->hListWnd, i);
635 if(cnt == 1)
636 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | (IsDefault ? MF_DISABLED | MF_GRAYED : MF_ENABLED));
637 else
638 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
639 EnableMenuItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
640 EnableMenuItem(mnu, ID_EDIT_MODIFY_BIN, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
641
642 TrackPopupMenu(mnu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
643 }
644 }
645 else if ((HWND)wParam == g_pChildWnd->hTreeWnd)
646 {
647 TVHITTESTINFO hti;
648 HMENU hContextMenu;
649 TVITEM item;
650 MENUITEMINFO mii;
651 TCHAR resource[256];
652 TCHAR buffer[256];
653 LPTSTR s;
654 LPCTSTR keyPath;
655 HKEY hRootKey;
656 int iLastPos;
657 WORD wID;
658
659 pt.x = (short) LOWORD(lParam);
660 pt.y = (short) HIWORD(lParam);
661
662 if (pt.x == -1 && pt.y == -1)
663 {
664 RECT rc;
665 hti.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
666 if (hti.hItem != NULL)
667 {
668 TreeView_GetItemRect(g_pChildWnd->hTreeWnd, hti.hItem, &rc, TRUE);
669 pt.x = rc.left + 8;
670 pt.y = rc.top + 8;
671 ClientToScreen(g_pChildWnd->hTreeWnd, &pt);
672 hti.flags = TVHT_ONITEM;
673 }
674 else
675 hti.flags = 0;
676 }
677 else
678 {
679 hti.pt.x = pt.x;
680 hti.pt.y = pt.y;
681 ScreenToClient(g_pChildWnd->hTreeWnd, &hti.pt);
682 (void)TreeView_HitTest(g_pChildWnd->hTreeWnd, &hti);
683 }
684
685 if (hti.flags & TVHT_ONITEM)
686 {
687 hContextMenu = GetSubMenu(hPopupMenus, PM_TREECONTEXT);
688 (void)TreeView_SelectItem(g_pChildWnd->hTreeWnd, hti.hItem);
689
690 memset(&item, 0, sizeof(item));
691 item.mask = TVIF_STATE | TVIF_CHILDREN;
692 item.hItem = hti.hItem;
693 (void)TreeView_GetItem(g_pChildWnd->hTreeWnd, &item);
694
695 /* Set the Expand/Collapse menu item appropriately */
696 LoadString(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, COUNT_OF(buffer));
697 memset(&mii, 0, sizeof(mii));
698 mii.cbSize = sizeof(mii);
699 mii.fMask = MIIM_STRING | MIIM_STATE | MIIM_ID;
700 mii.fState = (item.cChildren > 0) ? MFS_DEFAULT : MFS_GRAYED;
701 mii.wID = (item.state & TVIS_EXPANDED) ? ID_TREE_COLLAPSEBRANCH : ID_TREE_EXPANDBRANCH;
702 mii.dwTypeData = (LPTSTR) buffer;
703 SetMenuItemInfo(hContextMenu, 0, TRUE, &mii);
704
705 /* Remove any existing suggestions */
706 memset(&mii, 0, sizeof(mii));
707 mii.cbSize = sizeof(mii);
708 mii.fMask = MIIM_ID;
709 GetMenuItemInfo(hContextMenu, GetMenuItemCount(hContextMenu) - 1, TRUE, &mii);
710 if ((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX))
711 {
712 do
713 {
714 iLastPos = GetMenuItemCount(hContextMenu) - 1;
715 GetMenuItemInfo(hContextMenu, iLastPos, TRUE, &mii);
716 RemoveMenu(hContextMenu, iLastPos, MF_BYPOSITION);
717 }
718 while((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX));
719 }
720
721 /* Come up with suggestions */
722 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, NULL, &hRootKey);
723 SuggestKeys(hRootKey, keyPath, Suggestions, COUNT_OF(Suggestions));
724 if (Suggestions[0])
725 {
726 AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
727
728 LoadString(hInst, IDS_GOTO_SUGGESTED_KEY, resource, COUNT_OF(resource));
729
730 s = Suggestions;
731 wID = ID_TREE_SUGGESTION_MIN;
732 while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
733 {
734 _sntprintf(buffer, COUNT_OF(buffer), resource, s);
735
736 memset(&mii, 0, sizeof(mii));
737 mii.cbSize = sizeof(mii);
738 mii.fMask = MIIM_STRING | MIIM_ID;
739 mii.wID = wID++;
740 mii.dwTypeData = buffer;
741 InsertMenuItem(hContextMenu, GetMenuItemCount(hContextMenu), TRUE, &mii);
742
743 s += _tcslen(s) + 1;
744 }
745 }
746 TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, g_pChildWnd->hWnd, NULL);
747 }
748 }
749 break;
750 }
751
752 case WM_SIZE:
753 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL)
754 {
755 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
756 }
757 /* fall through */
758 default:
759 def:
760 return DefWindowProc(hWnd, message, wParam, lParam);
761 }
762 return 0;
763 }