[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 WCHAR Suggestions[256];
28
29 extern LPCWSTR get_root_key_name(HKEY hRootKey)
30 {
31 if (hRootKey == HKEY_CLASSES_ROOT) return L"HKEY_CLASSES_ROOT";
32 if (hRootKey == HKEY_CURRENT_USER) return L"HKEY_CURRENT_USER";
33 if (hRootKey == HKEY_LOCAL_MACHINE) return L"HKEY_LOCAL_MACHINE";
34 if (hRootKey == HKEY_USERS) return L"HKEY_USERS";
35 if (hRootKey == HKEY_CURRENT_CONFIG) return L"HKEY_CURRENT_CONFIG";
36 if (hRootKey == HKEY_DYN_DATA) return L"HKEY_DYN_DATA";
37
38 return L"UNKNOWN HKEY, PLEASE REPORT";
39 }
40
41 extern void ResizeWnd(int cx, int cy)
42 {
43 HDWP hdwp = BeginDeferWindowPos(3);
44 RECT rt, rs, rb;
45 const int tHeight = 18;
46 SetRect(&rt, 0, 0, cx, cy);
47 cy = 0;
48 if (hStatusBar != NULL)
49 {
50 GetWindowRect(hStatusBar, &rs);
51 cy = rs.bottom - rs.top;
52 }
53 GetWindowRect(g_pChildWnd->hAddressBtnWnd, &rb);
54 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
55 DeferWindowPos(hdwp, g_pChildWnd->hAddressBarWnd, 0, rt.left, rt.top, rt.right-rt.left - tHeight-2, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
56 DeferWindowPos(hdwp, g_pChildWnd->hAddressBtnWnd, 0, rt.right - tHeight, rt.top, tHeight, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
57 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);
58 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);
59 EndDeferWindowPos(hdwp);
60 }
61
62 /*******************************************************************************
63 * Local module support methods
64 */
65
66 static void draw_splitbar(HWND hWnd, int x)
67 {
68 RECT rt;
69 HGDIOBJ OldObj;
70 HDC hdc = GetDC(hWnd);
71
72 if(!SizingPattern)
73 {
74 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
75 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
76 }
77 if(!SizingBrush)
78 {
79 SizingBrush = CreatePatternBrush(SizingPattern);
80 }
81 GetClientRect(hWnd, &rt);
82 rt.left = x - SPLIT_WIDTH/2;
83 rt.right = x + SPLIT_WIDTH/2+1;
84 OldObj = SelectObject(hdc, SizingBrush);
85 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
86 SelectObject(hdc, OldObj);
87 ReleaseDC(hWnd, hdc);
88 }
89
90 static void OnPaint(HWND hWnd)
91 {
92 PAINTSTRUCT ps;
93 RECT rt;
94
95 GetClientRect(hWnd, &rt);
96 BeginPaint(hWnd, &ps);
97 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
98 EndPaint(hWnd, &ps);
99 }
100
101 /*******************************************************************************
102 * finish_splitbar [internal]
103 *
104 * make the splitbar invisible and resize the windows
105 * (helper for ChildWndProc)
106 */
107 static void finish_splitbar(HWND hWnd, int x)
108 {
109 RECT rt;
110
111 draw_splitbar(hWnd, last_split);
112 last_split = -1;
113 GetClientRect(hWnd, &rt);
114 g_pChildWnd->nSplitPos = x;
115 ResizeWnd(rt.right, rt.bottom);
116 ReleaseCapture();
117 }
118
119 /*******************************************************************************
120 *
121 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
122 *
123 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
124 *
125 */
126
127 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
128 {
129 HTREEITEM hSelection;
130 HKEY hRootKey;
131 LPCWSTR 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(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd), TVE_EXPAND);
147 break;
148 case ID_TREE_COLLAPSEBRANCH:
149 (void)TreeView_Expand(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd), TVE_COLLAPSE);
150 break;
151 case ID_TREE_RENAME:
152 SetFocus(g_pChildWnd->hTreeWnd);
153 (void)TreeView_EditLabel(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd));
154 break;
155 case ID_TREE_DELETE:
156 hSelection = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
157 keyPath = GetItemPath(g_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(g_pChildWnd->hTreeWnd);
168 break;
169 case ID_EDIT_FIND:
170 FindDialog(hWnd);
171 break;
172 case ID_EDIT_COPYKEYNAME:
173 hSelection = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
174 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, hSelection, &hRootKey);
175 CopyKeyName(hWnd, hRootKey, keyPath);
176 break;
177 case ID_EDIT_NEW_KEY:
178 CreateNewKey(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd));
179 break;
180 case ID_EDIT_NEW_STRINGVALUE:
181 case ID_EDIT_NEW_BINARYVALUE:
182 case ID_EDIT_NEW_DWORDVALUE:
183 SendMessageW(hFrameWnd, WM_COMMAND, wParam, lParam);
184 break;
185 case ID_SWITCH_PANELS:
186 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
187 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_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 += wcslen(s) + 1;
197 wID--;
198 }
199 SelectNode(g_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, LPCWSTR pszKeyPath, LPWSTR pszSuggestions,
215 size_t iSuggestionsLength)
216 {
217 WCHAR szBuffer[256];
218 WCHAR 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] && !wcschr(pszKeyPath, L'\\'))
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] != L'\0') && wcsicmp(szBuffer, pszKeyPath))
240 {
241 if (RegOpenKeyW(hRootKey, szBuffer, &hOtherKey) == ERROR_SUCCESS)
242 {
243 wcsncpy(pszSuggestions, L"HKCR\\", (int) iSuggestionsLength);
244 i = wcslen(pszSuggestions);
245 pszSuggestions += i;
246 iSuggestionsLength -= i;
247
248 wcsncpy(pszSuggestions, szBuffer, (int) iSuggestionsLength);
249 i = MIN(wcslen(pszSuggestions) + 1, iSuggestionsLength);
250 pszSuggestions += i;
251 iSuggestionsLength -= i;
252 RegCloseKey(hOtherKey);
253
254 bFound = TRUE;
255 wcscpy(szLastFound, szBuffer);
256 pszKeyPath = szLastFound;
257 }
258 }
259 }
260 }
261 while(bFound && (iSuggestionsLength > 0));
262
263 /* Check CLSID key */
264 if (RegOpenKeyW(hRootKey, pszKeyPath, &hSubKey) == ERROR_SUCCESS)
265 {
266 if (QueryStringValue(hSubKey, L"CLSID", NULL, szBuffer,
267 COUNT_OF(szBuffer)) == ERROR_SUCCESS)
268 {
269 wcsncpy(pszSuggestions, L"HKCR\\CLSID\\", (int)iSuggestionsLength);
270 i = wcslen(pszSuggestions);
271 pszSuggestions += i;
272 iSuggestionsLength -= i;
273
274 wcsncpy(pszSuggestions, szBuffer, (int)iSuggestionsLength);
275 i = MIN(wcslen(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 WCHAR 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 GetWindowTextW(hwnd, s_szNode, COUNT_OF(s_szNode));
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 static VOID
307 UpdateAddress(HTREEITEM hItem, HKEY hRootKey, LPCWSTR pszPath)
308 {
309 LPCWSTR keyPath, rootName;
310 LPWSTR fullPath;
311
312 if (pszPath == NULL)
313 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, hItem, &hRootKey);
314 else
315 keyPath = pszPath;
316
317 if (keyPath)
318 {
319 RefreshListView(g_pChildWnd->hListWnd, hRootKey, keyPath);
320 rootName = get_root_key_name(hRootKey);
321 fullPath = HeapAlloc(GetProcessHeap(), 0, (wcslen(rootName) + 1 + wcslen(keyPath) + 1) * sizeof(WCHAR));
322 if (fullPath)
323 {
324 /* set (correct) the address bar text */
325 if (keyPath[0] != L'\0')
326 swprintf(fullPath, L"%s\\%s", rootName, keyPath);
327 else
328 fullPath = wcscpy(fullPath, rootName);
329 SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)fullPath);
330 SendMessageW(g_pChildWnd->hAddressBarWnd, WM_SETTEXT, 0, (LPARAM)fullPath);
331 HeapFree(GetProcessHeap(), 0, fullPath);
332 /* disable hive manipulation items temporarily (enable only if necessary) */
333 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_GRAYED);
334 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_GRAYED);
335 /* compare the strings to see if we should enable/disable the "Load Hive" menus accordingly */
336 if (!(wcsicmp(rootName, L"HKEY_LOCAL_MACHINE") &&
337 wcsicmp(rootName, L"HKEY_USERS")))
338 {
339 /*
340 * enable the unload menu item if at the root, otherwise
341 * enable the load menu item if there is no slash in
342 * keyPath (ie. immediate child selected)
343 */
344 if(keyPath[0] == L'\0')
345 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_ENABLED);
346 else if(!wcschr(keyPath, L'\\'))
347 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_ENABLED);
348 }
349 }
350 }
351 }
352
353 /*******************************************************************************
354 *
355 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
356 *
357 * PURPOSE: Processes messages for the child windows.
358 *
359 * WM_COMMAND - process the application menu
360 * WM_PAINT - Paint the main window
361 * WM_DESTROY - post a quit message and return
362 *
363 */
364 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
365 {
366 BOOL Result;
367
368 switch (message)
369 {
370 case WM_CREATE:
371 {
372 WNDPROC oldproc;
373 HFONT hFont;
374 WCHAR buffer[MAX_PATH];
375
376 /* Load "My Computer" string */
377 LoadStringW(hInst, IDS_MY_COMPUTER, buffer, COUNT_OF(buffer));
378
379 g_pChildWnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ChildWnd));
380 if (!g_pChildWnd) return 0;
381
382 wcsncpy(g_pChildWnd->szPath, buffer, MAX_PATH);
383 g_pChildWnd->nSplitPos = 250;
384 g_pChildWnd->hWnd = hWnd;
385 g_pChildWnd->hAddressBarWnd = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP,
386 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
387 hWnd, (HMENU)0, hInst, 0);
388 g_pChildWnd->hAddressBtnWnd = CreateWindowExW(WS_EX_CLIENTEDGE, L"Button", L"ยป", WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP | BS_DEFPUSHBUTTON,
389 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
390 hWnd, (HMENU)0, hInst, 0);
391 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, (HMENU) TREE_WINDOW);
392 g_pChildWnd->hListWnd = CreateListView(hWnd, (HMENU) LIST_WINDOW/*, g_pChildWnd->szPath*/);
393 SetFocus(g_pChildWnd->hTreeWnd);
394
395 /* set the address bar and button font */
396 if ((g_pChildWnd->hAddressBarWnd) && (g_pChildWnd->hAddressBtnWnd))
397 {
398 hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
399 SendMessageW(g_pChildWnd->hAddressBarWnd,
400 WM_SETFONT,
401 (WPARAM)hFont,
402 0);
403 SendMessageW(g_pChildWnd->hAddressBtnWnd,
404 WM_SETFONT,
405 (WPARAM)hFont,
406 0);
407 }
408 /* Subclass the AddressBar */
409 oldproc = (WNDPROC)(LONG_PTR)GetWindowLongPtr(g_pChildWnd->hAddressBarWnd, GWL_WNDPROC);
410 SetWindowLongPtr(g_pChildWnd->hAddressBarWnd, GWL_USERDATA, (DWORD_PTR)oldproc);
411 SetWindowLongPtr(g_pChildWnd->hAddressBarWnd, GWL_WNDPROC, (DWORD_PTR)AddressBarProc);
412 break;
413 }
414 case WM_COMMAND:
415 if(HIWORD(wParam) == BN_CLICKED)
416 {
417 PostMessageW(g_pChildWnd->hAddressBarWnd, WM_KEYUP, VK_RETURN, 0);
418 }
419
420 if (!_CmdWndProc(hWnd, message, wParam, lParam))
421 {
422 goto def;
423 }
424 break;
425 case WM_PAINT:
426 OnPaint(hWnd);
427 return 0;
428 case WM_SETCURSOR:
429 if (LOWORD(lParam) == HTCLIENT)
430 {
431 POINT pt;
432 GetCursorPos(&pt);
433 ScreenToClient(hWnd, &pt);
434 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1)
435 {
436 SetCursor(LoadCursorW(0, IDC_SIZEWE));
437 return TRUE;
438 }
439 }
440 goto def;
441 case WM_DESTROY:
442 DestroyTreeView();
443 DestroyListView(g_pChildWnd->hListWnd);
444 DestroyMainMenu();
445 HeapFree(GetProcessHeap(), 0, g_pChildWnd);
446 g_pChildWnd = NULL;
447 PostQuitMessage(0);
448 break;
449 case WM_LBUTTONDOWN:
450 {
451 RECT rt;
452 int x = (short)LOWORD(lParam);
453 GetClientRect(hWnd, &rt);
454 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1)
455 {
456 last_split = g_pChildWnd->nSplitPos;
457 draw_splitbar(hWnd, last_split);
458 SetCapture(hWnd);
459 }
460 break;
461 }
462
463 case WM_LBUTTONUP:
464 case WM_RBUTTONDOWN:
465 if (GetCapture() == hWnd)
466 {
467 finish_splitbar(hWnd, LOWORD(lParam));
468 }
469 break;
470
471 case WM_CAPTURECHANGED:
472 if (GetCapture()==hWnd && last_split>=0)
473 draw_splitbar(hWnd, last_split);
474 break;
475
476 case WM_KEYDOWN:
477 if (wParam == VK_ESCAPE)
478 if (GetCapture() == hWnd)
479 {
480 RECT rt;
481 draw_splitbar(hWnd, last_split);
482 GetClientRect(hWnd, &rt);
483 ResizeWnd(rt.right, rt.bottom);
484 last_split = -1;
485 ReleaseCapture();
486 SetCursor(LoadCursorW(0, IDC_ARROW));
487 }
488 break;
489
490 case WM_MOUSEMOVE:
491 if (GetCapture() == hWnd)
492 {
493 HDC hdc;
494 RECT rt;
495 HGDIOBJ OldObj;
496 int x = LOWORD(lParam);
497 if(!SizingPattern)
498 {
499 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
500 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
501 }
502 if(!SizingBrush)
503 {
504 SizingBrush = CreatePatternBrush(SizingPattern);
505 }
506
507 GetClientRect(hWnd, &rt);
508 x = (SHORT) min(max(x, SPLIT_MIN), rt.right - SPLIT_MIN);
509 if(last_split != x)
510 {
511 rt.left = last_split-SPLIT_WIDTH/2;
512 rt.right = last_split+SPLIT_WIDTH/2+1;
513 hdc = GetDC(hWnd);
514 OldObj = SelectObject(hdc, SizingBrush);
515 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
516 last_split = x;
517 rt.left = x-SPLIT_WIDTH/2;
518 rt.right = x+SPLIT_WIDTH/2+1;
519 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
520 SelectObject(hdc, OldObj);
521 ReleaseDC(hWnd, hdc);
522 }
523 }
524 break;
525
526 case WM_SETFOCUS:
527 if (g_pChildWnd != NULL)
528 {
529 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
530 }
531 break;
532
533 case WM_TIMER:
534 break;
535
536 case WM_NOTIFY:
537 if ((int)wParam == TREE_WINDOW && g_pChildWnd != NULL)
538 {
539 switch (((LPNMHDR)lParam)->code)
540 {
541 case TVN_ITEMEXPANDING:
542 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
543 case TVN_SELCHANGED:
544 UpdateAddress(((NMTREEVIEW*)lParam)->itemNew.hItem, NULL, NULL);
545 break;
546 case NM_SETFOCUS:
547 g_pChildWnd->nFocusPanel = 0;
548 break;
549 case TVN_BEGINLABELEDIT:
550 {
551 LPNMTVDISPINFO ptvdi;
552 /* cancel label edit for rootkeys */
553 ptvdi = (LPNMTVDISPINFO) lParam;
554 if (!TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
555 !TreeView_GetParent(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem)))
556 return TRUE;
557 break;
558 }
559 case TVN_ENDLABELEDIT:
560 {
561 LPCWSTR keyPath;
562 HKEY hRootKey;
563 HKEY hKey = NULL;
564 LPNMTVDISPINFO ptvdi;
565 LONG lResult = TRUE;
566 WCHAR szBuffer[MAX_PATH];
567
568 ptvdi = (LPNMTVDISPINFO) lParam;
569 if (ptvdi->item.pszText)
570 {
571 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
572 _snwprintf(szBuffer, COUNT_OF(szBuffer), L"%s\\%s", keyPath, ptvdi->item.pszText);
573 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
574 if (RegOpenKeyExW(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
575 {
576 lResult = FALSE;
577 RegCloseKey(hKey);
578 (void)TreeView_EditLabel(g_pChildWnd->hTreeWnd, ptvdi->item.hItem);
579 }
580 else
581 {
582 if (RenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
583 lResult = FALSE;
584 else
585 UpdateAddress(ptvdi->item.hItem, hRootKey, szBuffer);
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 SendMessageW(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 WCHAR resource[256];
666 WCHAR buffer[256];
667 LPWSTR s;
668 LPCWSTR 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 LoadStringW(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, COUNT_OF(buffer));
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 = (LPWSTR) 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, COUNT_OF(Suggestions));
738 if (Suggestions[0])
739 {
740 AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
741
742 LoadStringW(hInst, IDS_GOTO_SUGGESTED_KEY, resource, COUNT_OF(resource));
743
744 s = Suggestions;
745 wID = ID_TREE_SUGGESTION_MIN;
746 while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
747 {
748 _snwprintf(buffer, COUNT_OF(buffer), 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 += wcslen(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 DefWindowProcW(hWnd, message, wParam, lParam);
775 }
776 return 0;
777 }