Disable some misleading service tests because a test cannot determine wheter or not...
[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(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 else if (!_CmdWndProc(hWnd, message, wParam, lParam))
371 {
372 goto def;
373 }
374 break;
375 case WM_PAINT:
376 OnPaint(hWnd);
377 return 0;
378 case WM_SETCURSOR:
379 if (LOWORD(lParam) == HTCLIENT)
380 {
381 POINT pt;
382 GetCursorPos(&pt);
383 ScreenToClient(hWnd, &pt);
384 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1)
385 {
386 SetCursor(LoadCursor(0, IDC_SIZEWE));
387 return TRUE;
388 }
389 }
390 goto def;
391 case WM_DESTROY:
392 DestroyTreeView();
393 DestroyListView(g_pChildWnd->hListWnd);
394 DestroyMainMenu();
395 HeapFree(GetProcessHeap(), 0, g_pChildWnd);
396 g_pChildWnd = NULL;
397 PostQuitMessage(0);
398 break;
399 case WM_LBUTTONDOWN:
400 {
401 RECT rt;
402 int x = (short)LOWORD(lParam);
403 GetClientRect(hWnd, &rt);
404 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1)
405 {
406 last_split = g_pChildWnd->nSplitPos;
407 draw_splitbar(hWnd, last_split);
408 SetCapture(hWnd);
409 }
410 break;
411 }
412
413 case WM_LBUTTONUP:
414 case WM_RBUTTONDOWN:
415 if (GetCapture() == hWnd)
416 {
417 finish_splitbar(hWnd, LOWORD(lParam));
418 }
419 break;
420
421 case WM_CAPTURECHANGED:
422 if (GetCapture()==hWnd && last_split>=0)
423 draw_splitbar(hWnd, last_split);
424 break;
425
426 case WM_KEYDOWN:
427 if (wParam == VK_ESCAPE)
428 if (GetCapture() == hWnd)
429 {
430 RECT rt;
431 draw_splitbar(hWnd, last_split);
432 GetClientRect(hWnd, &rt);
433 ResizeWnd(rt.right, rt.bottom);
434 last_split = -1;
435 ReleaseCapture();
436 SetCursor(LoadCursor(0, IDC_ARROW));
437 }
438 break;
439
440 case WM_MOUSEMOVE:
441 if (GetCapture() == hWnd)
442 {
443 HDC hdc;
444 RECT rt;
445 HGDIOBJ OldObj;
446 int x = LOWORD(lParam);
447 if(!SizingPattern)
448 {
449 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
450 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
451 }
452 if(!SizingBrush)
453 {
454 SizingBrush = CreatePatternBrush(SizingPattern);
455 }
456
457 GetClientRect(hWnd, &rt);
458 x = (SHORT) min(max(x, SPLIT_MIN), rt.right - SPLIT_MIN);
459 if(last_split != x)
460 {
461 rt.left = last_split-SPLIT_WIDTH/2;
462 rt.right = last_split+SPLIT_WIDTH/2+1;
463 hdc = GetDC(hWnd);
464 OldObj = SelectObject(hdc, SizingBrush);
465 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
466 last_split = x;
467 rt.left = x-SPLIT_WIDTH/2;
468 rt.right = x+SPLIT_WIDTH/2+1;
469 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
470 SelectObject(hdc, OldObj);
471 ReleaseDC(hWnd, hdc);
472 }
473 }
474 break;
475
476 case WM_SETFOCUS:
477 if (g_pChildWnd != NULL)
478 {
479 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
480 }
481 break;
482
483 case WM_TIMER:
484 break;
485
486 case WM_NOTIFY:
487 if ((int)wParam == TREE_WINDOW && g_pChildWnd != NULL)
488 {
489 switch (((LPNMHDR)lParam)->code)
490 {
491 case TVN_ITEMEXPANDING:
492 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
493 case TVN_SELCHANGED:
494 {
495 LPCTSTR keyPath, rootName;
496 LPTSTR fullPath;
497 HKEY hRootKey;
498
499 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, ((NMTREEVIEW*)lParam)->itemNew.hItem, &hRootKey);
500 if (keyPath)
501 {
502 RefreshListView(g_pChildWnd->hListWnd, hRootKey, keyPath);
503 rootName = get_root_key_name(hRootKey);
504 fullPath = HeapAlloc(GetProcessHeap(), 0, (_tcslen(rootName) + 1 + _tcslen(keyPath) + 1) * sizeof(TCHAR));
505 if (fullPath)
506 {
507 /* set (correct) the address bar text */
508 if(keyPath[0] != '\0')
509 _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
510 else
511 fullPath = _tcscpy(fullPath, rootName);
512 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
513 SendMessage(g_pChildWnd->hAddressBarWnd, WM_SETTEXT, 0, (LPARAM)fullPath);
514 HeapFree(GetProcessHeap(), 0, fullPath);
515 /* disable hive manipulation items temporarily (enable only if necessary) */
516 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_GRAYED);
517 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_GRAYED);
518 /* compare the strings to see if we should enable/disable the "Load Hive" menus accordingly */
519 if (!(_tcsicmp(rootName, TEXT("HKEY_LOCAL_MACHINE")) &&
520 _tcsicmp(rootName, TEXT("HKEY_USERS"))))
521 {
522 // enable the unload menu item if at the root
523 // otherwise enable the load menu item if there is no slash in keyPath (ie. immediate child selected)
524 if(keyPath[0] == '\0')
525 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_ENABLED);
526 else if(!_tcschr(keyPath, _T('\\')))
527 EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_ENABLED);
528 }
529
530 {
531 HKEY hKey;
532 TCHAR szBuffer[MAX_PATH];
533 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("My Computer\\%s\\%s"), rootName, keyPath);
534
535 if (RegCreateKey(HKEY_CURRENT_USER,
536 g_szGeneralRegKey,
537 &hKey) == ERROR_SUCCESS)
538 {
539 RegSetValueEx(hKey, _T("LastKey"), 0, REG_SZ, (LPBYTE) szBuffer, (DWORD) _tcslen(szBuffer) * sizeof(szBuffer[0]));
540 RegCloseKey(hKey);
541 }
542 }
543 }
544 }
545 }
546 break;
547 case NM_SETFOCUS:
548 g_pChildWnd->nFocusPanel = 0;
549 break;
550 case TVN_BEGINLABELEDIT:
551 {
552 LPNMTVDISPINFO ptvdi;
553 /* cancel label edit for rootkeys */
554 ptvdi = (LPNMTVDISPINFO) lParam;
555 if (!TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
556 !TreeView_GetParent(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem)))
557 return TRUE;
558 break;
559 }
560 case TVN_ENDLABELEDIT:
561 {
562 LPCTSTR keyPath;
563 HKEY hRootKey;
564 HKEY hKey = NULL;
565 LPNMTVDISPINFO ptvdi;
566 LONG lResult = TRUE;
567 TCHAR szBuffer[MAX_PATH];
568
569 ptvdi = (LPNMTVDISPINFO) lParam;
570 if (ptvdi->item.pszText)
571 {
572 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
573 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("%s\\%s"), keyPath, ptvdi->item.pszText);
574 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
575 if (RegOpenKeyEx(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
576 {
577 lResult = FALSE;
578 RegCloseKey(hKey);
579 (void)TreeView_EditLabel(g_pChildWnd->hTreeWnd, ptvdi->item.hItem);
580 }
581 else
582 {
583 if (RenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
584 lResult = FALSE;
585 }
586 return lResult;
587 }
588 }
589 default:
590 return 0;
591 }
592 }
593 else
594 {
595 if ((int)wParam == LIST_WINDOW && g_pChildWnd != NULL)
596 {
597 switch (((LPNMHDR)lParam)->code)
598 {
599 case NM_SETFOCUS:
600 g_pChildWnd->nFocusPanel = 1;
601 break;
602 default:
603 if(!ListWndNotifyProc(g_pChildWnd->hListWnd, wParam, lParam, &Result))
604 {
605 goto def;
606 }
607 return Result;
608 break;
609 }
610 }
611 }
612 break;
613
614 case WM_CONTEXTMENU:
615 {
616 POINT pt;
617 if((HWND)wParam == g_pChildWnd->hListWnd)
618 {
619 int i, cnt;
620 BOOL IsDefault;
621 pt.x = (short) LOWORD(lParam);
622 pt.y = (short) HIWORD(lParam);
623 cnt = ListView_GetSelectedCount(g_pChildWnd->hListWnd);
624 i = ListView_GetNextItem(g_pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
625 if (pt.x == -1 && pt.y == -1)
626 {
627 RECT rc;
628 if (i != -1)
629 {
630 rc.left = LVIR_BOUNDS;
631 SendMessage(g_pChildWnd->hListWnd, LVM_GETITEMRECT, i, (LPARAM) &rc);
632 pt.x = rc.left + 8;
633 pt.y = rc.top + 8;
634 }
635 else
636 pt.x = pt.y = 0;
637 ClientToScreen(g_pChildWnd->hListWnd, &pt);
638 }
639 if(i == -1)
640 {
641 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
642 }
643 else
644 {
645 HMENU mnu = GetSubMenu(hPopupMenus, PM_MODIFYVALUE);
646 SetMenuDefaultItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND);
647 IsDefault = IsDefaultValue(g_pChildWnd->hListWnd, i);
648 if(cnt == 1)
649 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | (IsDefault ? MF_DISABLED | MF_GRAYED : MF_ENABLED));
650 else
651 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
652 EnableMenuItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
653 EnableMenuItem(mnu, ID_EDIT_MODIFY_BIN, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
654
655 TrackPopupMenu(mnu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
656 }
657 }
658 else if ((HWND)wParam == g_pChildWnd->hTreeWnd)
659 {
660 TVHITTESTINFO hti;
661 HMENU hContextMenu;
662 TVITEM item;
663 MENUITEMINFO mii;
664 TCHAR resource[256];
665 TCHAR buffer[256];
666 LPTSTR s;
667 LPCTSTR keyPath;
668 HKEY hRootKey;
669 int iLastPos;
670 WORD wID;
671
672 pt.x = (short) LOWORD(lParam);
673 pt.y = (short) HIWORD(lParam);
674
675 if (pt.x == -1 && pt.y == -1)
676 {
677 RECT rc;
678 hti.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
679 if (hti.hItem != NULL)
680 {
681 TreeView_GetItemRect(g_pChildWnd->hTreeWnd, hti.hItem, &rc, TRUE);
682 pt.x = rc.left + 8;
683 pt.y = rc.top + 8;
684 ClientToScreen(g_pChildWnd->hTreeWnd, &pt);
685 hti.flags = TVHT_ONITEM;
686 }
687 else
688 hti.flags = 0;
689 }
690 else
691 {
692 hti.pt.x = pt.x;
693 hti.pt.y = pt.y;
694 ScreenToClient(g_pChildWnd->hTreeWnd, &hti.pt);
695 (void)TreeView_HitTest(g_pChildWnd->hTreeWnd, &hti);
696 }
697
698 if (hti.flags & TVHT_ONITEM)
699 {
700 hContextMenu = GetSubMenu(hPopupMenus, PM_TREECONTEXT);
701 (void)TreeView_SelectItem(g_pChildWnd->hTreeWnd, hti.hItem);
702
703 memset(&item, 0, sizeof(item));
704 item.mask = TVIF_STATE | TVIF_CHILDREN;
705 item.hItem = hti.hItem;
706 (void)TreeView_GetItem(g_pChildWnd->hTreeWnd, &item);
707
708 /* Set the Expand/Collapse menu item appropriately */
709 LoadString(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, sizeof(buffer) / sizeof(buffer[0]));
710 memset(&mii, 0, sizeof(mii));
711 mii.cbSize = sizeof(mii);
712 mii.fMask = MIIM_STRING | MIIM_STATE | MIIM_ID;
713 mii.fState = (item.cChildren > 0) ? MFS_DEFAULT : MFS_GRAYED;
714 mii.wID = (item.state & TVIS_EXPANDED) ? ID_TREE_COLLAPSEBRANCH : ID_TREE_EXPANDBRANCH;
715 mii.dwTypeData = (LPTSTR) buffer;
716 SetMenuItemInfo(hContextMenu, 0, TRUE, &mii);
717
718 /* Remove any existing suggestions */
719 memset(&mii, 0, sizeof(mii));
720 mii.cbSize = sizeof(mii);
721 mii.fMask = MIIM_ID;
722 GetMenuItemInfo(hContextMenu, GetMenuItemCount(hContextMenu) - 1, TRUE, &mii);
723 if ((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX))
724 {
725 do
726 {
727 iLastPos = GetMenuItemCount(hContextMenu) - 1;
728 GetMenuItemInfo(hContextMenu, iLastPos, TRUE, &mii);
729 RemoveMenu(hContextMenu, iLastPos, MF_BYPOSITION);
730 }
731 while((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX));
732 }
733
734 /* Come up with suggestions */
735 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, NULL, &hRootKey);
736 SuggestKeys(hRootKey, keyPath, Suggestions, sizeof(Suggestions) / sizeof(Suggestions[0]));
737 if (Suggestions[0])
738 {
739 AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
740
741 LoadString(hInst, IDS_GOTO_SUGGESTED_KEY, resource, sizeof(resource) / sizeof(resource[0]));
742
743 s = Suggestions;
744 wID = ID_TREE_SUGGESTION_MIN;
745 while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
746 {
747 _sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), resource, s);
748
749 memset(&mii, 0, sizeof(mii));
750 mii.cbSize = sizeof(mii);
751 mii.fMask = MIIM_STRING | MIIM_ID;
752 mii.wID = wID++;
753 mii.dwTypeData = buffer;
754 InsertMenuItem(hContextMenu, GetMenuItemCount(hContextMenu), TRUE, &mii);
755
756 s += _tcslen(s) + 1;
757 }
758 }
759 TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, g_pChildWnd->hWnd, NULL);
760 }
761 }
762 break;
763 }
764
765 case WM_SIZE:
766 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL)
767 {
768 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
769 }
770 /* fall through */
771 default:
772 def:
773 return DefWindowProc(hWnd, message, wParam, lParam);
774 }
775 return 0;
776 }