[SHELL32] Remove 2 redundant initializations
[reactos.git] / base / applications / regedit / childwnd.c
index b1fb9fd..452313d 100644 (file)
@@ -2,6 +2,7 @@
  * Regedit child window
  *
  * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
+ * Copyright (C) 2024 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -24,7 +25,7 @@ ChildWnd* g_pChildWnd;
 static int last_split;
 HBITMAP SizingPattern = 0;
 HBRUSH  SizingBrush = 0;
-static WCHAR Suggestions[256];
+WCHAR Suggestions[256];
 
 extern LPCWSTR get_root_key_name(HKEY hRootKey)
 {
@@ -38,11 +39,20 @@ extern LPCWSTR get_root_key_name(HKEY hRootKey)
     return L"UNKNOWN HKEY, PLEASE REPORT";
 }
 
+static INT ClampSplitBarX(HWND hWnd, INT x)
+{
+    RECT rc;
+    GetClientRect(hWnd, &rc);
+    return min(max(x, SPLIT_MIN), rc.right - SPLIT_MIN);
+}
+
 extern void ResizeWnd(int cx, int cy)
 {
     HDWP hdwp = BeginDeferWindowPos(4);
     RECT rt, rs, rb;
-    const int tHeight = 22;
+    const int nButtonWidth = 44;
+    const int nButtonHeight = 22;
+    int cyEdge = GetSystemMetrics(SM_CYEDGE);
     const UINT uFlags = SWP_NOZORDER | SWP_NOACTIVATE;
     SetRect(&rt, 0, 0, cx, cy);
     cy = 0;
@@ -52,26 +62,33 @@ extern void ResizeWnd(int cx, int cy)
         cy = rs.bottom - rs.top;
     }
     GetWindowRect(g_pChildWnd->hAddressBtnWnd, &rb);
+
+    g_pChildWnd->nSplitPos = ClampSplitBarX(g_pChildWnd->hWnd, g_pChildWnd->nSplitPos);
+
     cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH / 2;
     if (hdwp)
         hdwp = DeferWindowPos(hdwp, g_pChildWnd->hAddressBarWnd, NULL,
                               rt.left, rt.top,
-                              rt.right - rt.left - 2*tHeight, tHeight,
+                              rt.right - rt.left - nButtonWidth, nButtonHeight,
                               uFlags);
     if (hdwp)
         hdwp = DeferWindowPos(hdwp, g_pChildWnd->hAddressBtnWnd, NULL,
-                              rt.right - 2*tHeight, rt.top,
-                              2*tHeight, tHeight,
+                              rt.right - nButtonWidth, rt.top,
+                              nButtonWidth, nButtonHeight,
                               uFlags);
     if (hdwp)
         hdwp = DeferWindowPos(hdwp, g_pChildWnd->hTreeWnd, NULL,
-                              rt.left, rt.top + tHeight + 2,
-                              g_pChildWnd->nSplitPos - SPLIT_WIDTH/2 - rt.left, rt.bottom - rt.top - cy,
+                              rt.left,
+                              rt.top + nButtonHeight + cyEdge,
+                              g_pChildWnd->nSplitPos - SPLIT_WIDTH/2 - rt.left,
+                              rt.bottom - rt.top - cy - 2 * cyEdge,
                               uFlags);
     if (hdwp)
         hdwp = DeferWindowPos(hdwp, g_pChildWnd->hListWnd, NULL,
-                              rt.left + cx, rt.top + tHeight + 2,
-                              rt.right - cx, rt.bottom - rt.top - cy,
+                              rt.left + cx,
+                              rt.top + nButtonHeight + cyEdge,
+                              rt.right - cx,
+                              rt.bottom - rt.top - cy - 2 * cyEdge,
                               uFlags);
     if (hdwp)
         EndDeferWindowPos(hdwp);
@@ -123,94 +140,6 @@ static void finish_splitbar(HWND hWnd, int x)
     ReleaseCapture();
 }
 
-/*******************************************************************************
- *
- *  FUNCTION: ChildWnd_CmdWndProc(HWND, unsigned, WORD, LONG)
- *
- *  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
- *
- */
-
-static BOOL ChildWnd_CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
-{
-    HTREEITEM hSelection;
-    HKEY hRootKey;
-    LPCWSTR keyPath, s;
-    WORD wID = LOWORD(wParam);
-
-    UNREFERENCED_PARAMETER(message);
-
-    switch (wID)
-    {
-        /* Parse the menu selections: */
-    case ID_REGISTRY_EXIT:
-        DestroyWindow(hWnd);
-        break;
-    case ID_VIEW_REFRESH:
-        /* TODO */
-        break;
-    case ID_TREE_EXPANDBRANCH:
-        TreeView_Expand(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd), TVE_EXPAND);
-        break;
-    case ID_TREE_COLLAPSEBRANCH:
-        TreeView_Expand(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd), TVE_COLLAPSE);
-        break;
-    case ID_TREE_RENAME:
-        SetFocus(g_pChildWnd->hTreeWnd);
-        TreeView_EditLabel(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd));
-        break;
-    case ID_TREE_DELETE:
-        hSelection = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
-        keyPath = GetItemPath(g_pChildWnd->hTreeWnd, hSelection, &hRootKey);
-
-        if (keyPath == 0 || *keyPath == 0)
-        {
-            MessageBeep(MB_ICONHAND);
-        }
-        else if (DeleteKey(hWnd, hRootKey, keyPath))
-            DeleteNode(g_pChildWnd->hTreeWnd, 0);
-        break;
-    case ID_TREE_EXPORT:
-        ExportRegistryFile(g_pChildWnd->hTreeWnd);
-        break;
-    case ID_EDIT_FIND:
-        FindDialog(hWnd);
-        break;
-    case ID_EDIT_COPYKEYNAME:
-        hSelection = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
-        keyPath = GetItemPath(g_pChildWnd->hTreeWnd, hSelection, &hRootKey);
-        CopyKeyName(hWnd, hRootKey, keyPath);
-        break;
-    case ID_EDIT_NEW_KEY:
-        CreateNewKey(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd));
-        break;
-    case ID_EDIT_NEW_STRINGVALUE:
-    case ID_EDIT_NEW_BINARYVALUE:
-    case ID_EDIT_NEW_DWORDVALUE:
-        SendMessageW(hFrameWnd, WM_COMMAND, wParam, lParam);
-        break;
-    case ID_SWITCH_PANELS:
-        g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
-        SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
-        break;
-    default:
-        if ((wID >= ID_TREE_SUGGESTION_MIN) && (wID <= ID_TREE_SUGGESTION_MAX))
-        {
-            s = Suggestions;
-            while(wID > ID_TREE_SUGGESTION_MIN)
-            {
-                if (*s)
-                    s += wcslen(s) + 1;
-                wID--;
-            }
-            SelectNode(g_pChildWnd->hTreeWnd, s);
-            break;
-        }
-        return FALSE;
-    }
-    return TRUE;
-}
-
 /*******************************************************************************
  *
  *  Key suggestion
@@ -239,7 +168,7 @@ static void SuggestKeys(HKEY hRootKey, LPCWSTR pszKeyPath, LPWSTR pszSuggestions
 
             /* Check default key */
             if (QueryStringValue(hRootKey, pszKeyPath, NULL,
-                                 szBuffer, COUNT_OF(szBuffer)) == ERROR_SUCCESS)
+                                 szBuffer, ARRAY_SIZE(szBuffer)) == ERROR_SUCCESS)
             {
                 /* Sanity check this key; it cannot be empty, nor can it be a
                  * loop back */
@@ -259,7 +188,7 @@ static void SuggestKeys(HKEY hRootKey, LPCWSTR pszKeyPath, LPWSTR pszSuggestions
                         RegCloseKey(hOtherKey);
 
                         bFound = TRUE;
-                        wcscpy(szLastFound, szBuffer);
+                        StringCbCopyW(szLastFound, sizeof(szLastFound), szBuffer);
                         pszKeyPath = szLastFound;
                     }
                 }
@@ -271,7 +200,7 @@ static void SuggestKeys(HKEY hRootKey, LPCWSTR pszKeyPath, LPWSTR pszSuggestions
         if (RegOpenKeyW(hRootKey, pszKeyPath, &hSubKey) == ERROR_SUCCESS)
         {
             if (QueryStringValue(hSubKey, L"CLSID", NULL, szBuffer,
-                                 COUNT_OF(szBuffer)) == ERROR_SUCCESS)
+                                 ARRAY_SIZE(szBuffer)) == ERROR_SUCCESS)
             {
                 lstrcpynW(pszSuggestions, L"HKCR\\CLSID\\", (int)iSuggestionsLength);
                 i = wcslen(pszSuggestions);
@@ -300,7 +229,7 @@ LRESULT CALLBACK AddressBarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPar
     case WM_KEYUP:
         if (wParam == VK_RETURN)
         {
-            GetWindowTextW(hwnd, s_szNode, COUNT_OF(s_szNode));
+            GetWindowTextW(hwnd, s_szNode, ARRAY_SIZE(s_szNode));
             SelectNode(g_pChildWnd->hTreeWnd, s_szNode);
         }
         break;
@@ -310,11 +239,12 @@ LRESULT CALLBACK AddressBarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPar
     return CallWindowProcW(oldwndproc, hwnd, uMsg, wParam, lParam);
 }
 
-static VOID
-UpdateAddress(HTREEITEM hItem, HKEY hRootKey, LPCWSTR pszPath)
+VOID
+UpdateAddress(HTREEITEM hItem, HKEY hRootKey, LPCWSTR pszPath, BOOL bSelectNone)
 {
     LPCWSTR keyPath, rootName;
     LPWSTR fullPath;
+    DWORD cbFullPath;
 
     /* Wipe the listview, the status bar and the address bar if the root key was selected */
     if (TreeView_GetParent(g_pChildWnd->hTreeWnd, hItem) == NULL)
@@ -332,35 +262,39 @@ UpdateAddress(HTREEITEM hItem, HKEY hRootKey, LPCWSTR pszPath)
 
     if (keyPath)
     {
-        RefreshListView(g_pChildWnd->hListWnd, hRootKey, keyPath);
+        RefreshListView(g_pChildWnd->hListWnd, hRootKey, keyPath, bSelectNone);
         rootName = get_root_key_name(hRootKey);
-        fullPath = HeapAlloc(GetProcessHeap(), 0, (wcslen(rootName) + 1 + wcslen(keyPath) + 1) * sizeof(WCHAR));
+        cbFullPath = (wcslen(rootName) + 1 + wcslen(keyPath) + 1) * sizeof(WCHAR);
+        fullPath = malloc(cbFullPath);
         if (fullPath)
         {
             /* set (correct) the address bar text */
-            if (keyPath[0] != L'\0')
-                swprintf(fullPath, L"%s\\%s", rootName, keyPath);
+            if (keyPath[0] != UNICODE_NULL)
+                StringCbPrintfW(fullPath, cbFullPath, L"%s%s%s", rootName,
+                                ((keyPath[0] == L'\\') ? L"" : L"\\"), keyPath);
             else
-                fullPath = wcscpy(fullPath, rootName);
+                StringCbCopyW(fullPath, cbFullPath, rootName);
+
             SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)fullPath);
             SendMessageW(g_pChildWnd->hAddressBarWnd, WM_SETTEXT, 0, (LPARAM)fullPath);
-            HeapFree(GetProcessHeap(), 0, fullPath);
+            free(fullPath);
+
             /* disable hive manipulation items temporarily (enable only if necessary) */
-            EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_GRAYED);
-            EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_GRAYED);
+            EnableMenuItem(hMenuFrame, ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_GRAYED);
+            EnableMenuItem(hMenuFrame, ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_GRAYED);
             /* compare the strings to see if we should enable/disable the "Load Hive" menus accordingly */
-            if (!(_wcsicmp(rootName, L"HKEY_LOCAL_MACHINE") &&
-                  _wcsicmp(rootName, L"HKEY_USERS")))
+            if (_wcsicmp(rootName, L"HKEY_LOCAL_MACHINE") == 0 ||
+                _wcsicmp(rootName, L"HKEY_USERS") == 0)
             {
                 /*
                  * enable the unload menu item if at the root, otherwise
                  * enable the load menu item if there is no slash in
                  * keyPath (ie. immediate child selected)
                  */
-                if(keyPath[0] == L'\0')
-                    EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_ENABLED);
-                else if(!wcschr(keyPath, L'\\'))
-                    EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_ENABLED);
+                if (keyPath[0] == UNICODE_NULL)
+                    EnableMenuItem(hMenuFrame, ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_ENABLED);
+                else if (!wcschr(keyPath, L'\\'))
+                    EnableMenuItem(hMenuFrame, ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_ENABLED);
             }
         }
     }
@@ -379,6 +313,7 @@ UpdateAddress(HTREEITEM hItem, HKEY hRootKey, LPCWSTR pszPath)
 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
     BOOL Result;
+    RECT rc;
 
     switch (message)
     {
@@ -387,24 +322,35 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
         WNDPROC oldproc;
         HFONT hFont;
         WCHAR buffer[MAX_PATH];
+        DWORD style;
 
         /* Load "My Computer" string */
-        LoadStringW(hInst, IDS_MY_COMPUTER, buffer, COUNT_OF(buffer));
+        LoadStringW(hInst, IDS_MY_COMPUTER, buffer, ARRAY_SIZE(buffer));
 
         g_pChildWnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ChildWnd));
         if (!g_pChildWnd) return 0;
 
         wcsncpy(g_pChildWnd->szPath, buffer, MAX_PATH);
-        g_pChildWnd->nSplitPos = 250;
+        g_pChildWnd->nSplitPos = 190;
         g_pChildWnd->hWnd = hWnd;
-        g_pChildWnd->hAddressBarWnd = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP,
+
+        style = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
+        g_pChildWnd->hAddressBarWnd = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", NULL, style,
                                                       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                                                       hWnd, (HMENU)0, hInst, 0);
-        g_pChildWnd->hAddressBtnWnd = CreateWindowExW(0, L"Button", L"\x00BB", WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP | BS_TEXT | BS_CENTER | BS_VCENTER | BS_FLAT | BS_DEFPUSHBUTTON,
+
+        style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_ICON | BS_CENTER |
+                BS_VCENTER | BS_FLAT | BS_DEFPUSHBUTTON;
+        g_pChildWnd->hAddressBtnWnd = CreateWindowExW(0, L"Button", L"\x00BB", style,
                                                       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                                                       hWnd, (HMENU)0, hInst, 0);
+        g_pChildWnd->hArrowIcon = (HICON)LoadImageW(hInst, MAKEINTRESOURCEW(IDI_ARROW),
+                                                    IMAGE_ICON, 12, 12, 0);
+        SendMessageW(g_pChildWnd->hAddressBtnWnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM)g_pChildWnd->hArrowIcon);
+
+        GetClientRect(hWnd, &rc);
         g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, (HMENU) TREE_WINDOW);
-        g_pChildWnd->hListWnd = CreateListView(hWnd, (HMENU) LIST_WINDOW/*, g_pChildWnd->szPath*/);
+        g_pChildWnd->hListWnd = CreateListView(hWnd, (HMENU) LIST_WINDOW, rc.right - g_pChildWnd->nSplitPos);
         SetFocus(g_pChildWnd->hTreeWnd);
 
         /* set the address bar and button font */
@@ -431,12 +377,7 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
         {
             PostMessageW(g_pChildWnd->hAddressBarWnd, WM_KEYUP, VK_RETURN, 0);
         }
-
-        if (!ChildWnd_CmdWndProc(hWnd, message, wParam, lParam))
-        {
-            goto def;
-        }
-        break;
+        break; //goto def;
     case WM_SETCURSOR:
         if (LOWORD(lParam) == HTCLIENT)
         {
@@ -450,23 +391,26 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
             }
         }
         goto def;
+
     case WM_DESTROY:
         DestroyListView(g_pChildWnd->hListWnd);
         DestroyTreeView(g_pChildWnd->hTreeWnd);
         DestroyMainMenu();
+        DestroyIcon(g_pChildWnd->hArrowIcon);
         HeapFree(GetProcessHeap(), 0, g_pChildWnd);
         g_pChildWnd = NULL;
         PostQuitMessage(0);
         break;
+
     case WM_LBUTTONDOWN:
     {
-        RECT rt;
-        int x = (short)LOWORD(lParam);
-        GetClientRect(hWnd, &rt);
-        if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1)
+        INT x = (SHORT)LOWORD(lParam);
+        if (x >= g_pChildWnd->nSplitPos - SPLIT_WIDTH / 2 &&
+            x <  g_pChildWnd->nSplitPos + SPLIT_WIDTH / 2 + 1)
         {
-            last_split = g_pChildWnd->nSplitPos;
-            draw_splitbar(hWnd, last_split);
+            x = ClampSplitBarX(hWnd, x);
+            draw_splitbar(hWnd, x);
+            last_split = x;
             SetCapture(hWnd);
         }
         break;
@@ -476,12 +420,14 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
     case WM_RBUTTONDOWN:
         if (GetCapture() == hWnd)
         {
-            finish_splitbar(hWnd, LOWORD(lParam));
+            INT x = (SHORT)LOWORD(lParam);
+            x = ClampSplitBarX(hWnd, x);
+            finish_splitbar(hWnd, x);
         }
         break;
 
     case WM_CAPTURECHANGED:
-        if (GetCapture()==hWnd && last_split>=0)
+        if (GetCapture() == hWnd && last_split >= 0)
             draw_splitbar(hWnd, last_split);
         break;
 
@@ -502,35 +448,13 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
     case WM_MOUSEMOVE:
         if (GetCapture() == hWnd)
         {
-            HDC hdc;
-            RECT rt;
-            HGDIOBJ OldObj;
-            int x = LOWORD(lParam);
-            if(!SizingPattern)
-            {
-                const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
-                SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
-            }
-            if(!SizingBrush)
+            INT x = (SHORT)LOWORD(lParam);
+            x = ClampSplitBarX(hWnd, x);
+            if (last_split != x)
             {
-                SizingBrush = CreatePatternBrush(SizingPattern);
-            }
-
-            GetClientRect(hWnd, &rt);
-            x = (SHORT) min(max(x, SPLIT_MIN), rt.right - SPLIT_MIN);
-            if(last_split != x)
-            {
-                rt.left = last_split-SPLIT_WIDTH/2;
-                rt.right = last_split+SPLIT_WIDTH/2+1;
-                hdc = GetDC(hWnd);
-                OldObj = SelectObject(hdc, SizingBrush);
-                PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
+                draw_splitbar(hWnd, last_split);
                 last_split = x;
-                rt.left = x-SPLIT_WIDTH/2;
-                rt.right = x+SPLIT_WIDTH/2+1;
-                PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
-                SelectObject(hdc, OldObj);
-                ReleaseDC(hWnd, hdc);
+                draw_splitbar(hWnd, last_split);
             }
         }
         break;
@@ -542,110 +466,32 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
         }
         break;
 
-    case WM_TIMER:
-        break;
-
     case WM_NOTIFY:
-        if ((int)wParam == TREE_WINDOW && g_pChildWnd != NULL)
-        {
-            switch (((LPNMHDR)lParam)->code)
-            {
-            case TVN_ITEMEXPANDING:
-                return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
-            case TVN_SELCHANGED:
-            {
-                NMTREEVIEW* pnmtv = (NMTREEVIEW*)lParam;
-                /* Get the parent of the current item */
-                HTREEITEM hParentItem = TreeView_GetParent(g_pChildWnd->hTreeWnd, pnmtv->itemNew.hItem);
+        if (g_pChildWnd == NULL) break;
 
-                UpdateAddress(pnmtv->itemNew.hItem, NULL, NULL);
-
-                /*
-                 * Disable Delete/Rename menu options for 'My Computer' (first item so doesn't have any parent)
-                 * and HKEY_* keys (their parent is 'My Computer' and the previous remark applies).
-                 */
-                if (!hParentItem || !TreeView_GetParent(g_pChildWnd->hTreeWnd, hParentItem))
-                {
-                    EnableMenuItem(hMenuFrame , ID_EDIT_DELETE, MF_BYCOMMAND | MF_GRAYED);
-                    EnableMenuItem(hMenuFrame , ID_EDIT_RENAME, MF_BYCOMMAND | MF_GRAYED);
-                    EnableMenuItem(hPopupMenus, ID_TREE_DELETE, MF_BYCOMMAND | MF_GRAYED);
-                    EnableMenuItem(hPopupMenus, ID_TREE_RENAME, MF_BYCOMMAND | MF_GRAYED); 
-                }
-                else
-                {
-                    EnableMenuItem(hMenuFrame , ID_EDIT_DELETE, MF_BYCOMMAND | MF_ENABLED);
-                    EnableMenuItem(hMenuFrame , ID_EDIT_RENAME, MF_BYCOMMAND | MF_ENABLED);
-                    EnableMenuItem(hPopupMenus, ID_TREE_DELETE, MF_BYCOMMAND | MF_ENABLED);
-                    EnableMenuItem(hPopupMenus, ID_TREE_RENAME, MF_BYCOMMAND | MF_ENABLED);
-                }
-
-                break;
-            }
-            case NM_SETFOCUS:
-                g_pChildWnd->nFocusPanel = 0;
-                break;
-            case TVN_BEGINLABELEDIT:
-            {
-                LPNMTVDISPINFO ptvdi;
-                /* cancel label edit for rootkeys  */
-                ptvdi = (LPNMTVDISPINFO) lParam;
-                if (!TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
-                    !TreeView_GetParent(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem)))
-                    return TRUE;
-                break;
-            }
-            case TVN_ENDLABELEDIT:
+        if (((LPNMHDR)lParam)->idFrom == TREE_WINDOW)
+        {
+            if (!TreeWndNotifyProc(g_pChildWnd->hListWnd, wParam, lParam, &Result))
             {
-                LPCWSTR keyPath;
-                HKEY hRootKey;
-                HKEY hKey = NULL;
-                LPNMTVDISPINFO ptvdi;
-                LONG lResult = TRUE;
-                WCHAR szBuffer[MAX_PATH];
-
-                ptvdi = (LPNMTVDISPINFO) lParam;
-                if (ptvdi->item.pszText)
-                {
-                    keyPath = GetItemPath(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
-                    _snwprintf(szBuffer, COUNT_OF(szBuffer), L"%s\\%s", keyPath, ptvdi->item.pszText);
-                    keyPath = GetItemPath(g_pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
-                    if (RegOpenKeyExW(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
-                    {
-                        lResult = FALSE;
-                        RegCloseKey(hKey);
-                        TreeView_EditLabel(g_pChildWnd->hTreeWnd, ptvdi->item.hItem);
-                    }
-                    else
-                    {
-                        if (RenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
-                            lResult = FALSE;
-                        else
-                            UpdateAddress(ptvdi->item.hItem, hRootKey, szBuffer);
-                    }
-                    return lResult;
-                }
-            }
-            default:
-                return 0;
+                goto def;
             }
+
+            return Result;
         }
         else
         {
-            if ((int)wParam == LIST_WINDOW && g_pChildWnd != NULL)
+            if (((LPNMHDR)lParam)->idFrom == LIST_WINDOW)
             {
-                switch (((LPNMHDR)lParam)->code)
+                if (!ListWndNotifyProc(g_pChildWnd->hListWnd, wParam, lParam, &Result))
                 {
-                case NM_SETFOCUS:
-                    g_pChildWnd->nFocusPanel = 1;
-                    break;
-                default:
-                    if(!ListWndNotifyProc(g_pChildWnd->hListWnd, wParam, lParam, &Result))
-                    {
-                        goto def;
-                    }
-                    return Result;
-                    break;
+                    goto def;
                 }
+
+                return Result;
+            }
+            else
+            {
+                goto def;
             }
         }
         break;
@@ -707,6 +553,7 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
             HKEY hRootKey;
             int iLastPos;
             WORD wID;
+            BOOL isRoot;
 
             pt.x = (short) LOWORD(lParam);
             pt.y = (short) HIWORD(lParam);
@@ -736,16 +583,18 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
 
             if (hti.flags & TVHT_ONITEM)
             {
-                hContextMenu = GetSubMenu(hPopupMenus, PM_TREECONTEXT);
                 TreeView_SelectItem(g_pChildWnd->hTreeWnd, hti.hItem);
 
+                isRoot = (TreeView_GetParent(g_pChildWnd->hTreeWnd, hti.hItem) == NULL);
+                hContextMenu = GetSubMenu(hPopupMenus, isRoot ?  PM_ROOTITEM : PM_TREECONTEXT);
+
                 memset(&item, 0, sizeof(item));
                 item.mask = TVIF_STATE | TVIF_CHILDREN;
                 item.hItem = hti.hItem;
                 TreeView_GetItem(g_pChildWnd->hTreeWnd, &item);
 
                 /* Set the Expand/Collapse menu item appropriately */
-                LoadStringW(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, COUNT_OF(buffer));
+                LoadStringW(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, ARRAY_SIZE(buffer));
                 memset(&mii, 0, sizeof(mii));
                 mii.cbSize = sizeof(mii);
                 mii.fMask = MIIM_STRING | MIIM_STATE | MIIM_ID;
@@ -754,48 +603,51 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
                 mii.dwTypeData = (LPWSTR) buffer;
                 SetMenuItemInfo(hContextMenu, 0, TRUE, &mii);
 
-                /* Remove any existing suggestions */
-                memset(&mii, 0, sizeof(mii));
-                mii.cbSize = sizeof(mii);
-                mii.fMask = MIIM_ID;
-                GetMenuItemInfo(hContextMenu, GetMenuItemCount(hContextMenu) - 1, TRUE, &mii);
-                if ((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX))
+                if (isRoot == FALSE)
                 {
-                    do
+                    /* Remove any existing suggestions */
+                    memset(&mii, 0, sizeof(mii));
+                    mii.cbSize = sizeof(mii);
+                    mii.fMask = MIIM_ID;
+                    GetMenuItemInfo(hContextMenu, GetMenuItemCount(hContextMenu) - 1, TRUE, &mii);
+                    if ((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX))
                     {
-                        iLastPos = GetMenuItemCount(hContextMenu) - 1;
-                        GetMenuItemInfo(hContextMenu, iLastPos, TRUE, &mii);
-                        RemoveMenu(hContextMenu, iLastPos, MF_BYPOSITION);
+                        do
+                        {
+                            iLastPos = GetMenuItemCount(hContextMenu) - 1;
+                            GetMenuItemInfo(hContextMenu, iLastPos, TRUE, &mii);
+                            RemoveMenu(hContextMenu, iLastPos, MF_BYPOSITION);
+                        }
+                        while((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX));
                     }
-                    while((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX));
-                }
 
-                /* Come up with suggestions */
-                keyPath = GetItemPath(g_pChildWnd->hTreeWnd, NULL, &hRootKey);
-                SuggestKeys(hRootKey, keyPath, Suggestions, COUNT_OF(Suggestions));
-                if (Suggestions[0])
-                {
-                    AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
+                    /* Come up with suggestions */
+                    keyPath = GetItemPath(g_pChildWnd->hTreeWnd, NULL, &hRootKey);
+                    SuggestKeys(hRootKey, keyPath, Suggestions, ARRAY_SIZE(Suggestions));
+                    if (Suggestions[0])
+                    {
+                        AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
 
-                    LoadStringW(hInst, IDS_GOTO_SUGGESTED_KEY, resource, COUNT_OF(resource));
+                        LoadStringW(hInst, IDS_GOTO_SUGGESTED_KEY, resource, ARRAY_SIZE(resource));
 
-                    s = Suggestions;
-                    wID = ID_TREE_SUGGESTION_MIN;
-                    while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
-                    {
-                        _snwprintf(buffer, COUNT_OF(buffer), resource, s);
+                        s = Suggestions;
+                        wID = ID_TREE_SUGGESTION_MIN;
+                        while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
+                        {
+                            _snwprintf(buffer, ARRAY_SIZE(buffer), resource, s);
 
-                        memset(&mii, 0, sizeof(mii));
-                        mii.cbSize = sizeof(mii);
-                        mii.fMask = MIIM_STRING | MIIM_ID;
-                        mii.wID = wID++;
-                        mii.dwTypeData = buffer;
-                        InsertMenuItem(hContextMenu, GetMenuItemCount(hContextMenu), TRUE, &mii);
+                            memset(&mii, 0, sizeof(mii));
+                            mii.cbSize = sizeof(mii);
+                            mii.fMask = MIIM_STRING | MIIM_ID;
+                            mii.wID = wID++;
+                            mii.dwTypeData = buffer;
+                            InsertMenuItem(hContextMenu, GetMenuItemCount(hContextMenu), TRUE, &mii);
 
-                        s += wcslen(s) + 1;
+                            s += wcslen(s) + 1;
+                        }
                     }
                 }
-                TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, g_pChildWnd->hWnd, NULL);
+                TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
             }
         }
         break;
@@ -806,7 +658,8 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
         {
             ResizeWnd(LOWORD(lParam), HIWORD(lParam));
         }
-        /* fall through */
+        break;
+
     default:
 def:
         return DefWindowProcW(hWnd, message, wParam, lParam);