[CMAKE]
[reactos.git] / base / applications / regedit / childwnd.c
index 232a846..56ab005 100644 (file)
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 #include <regedit.h>
@@ -67,19 +67,21 @@ static void draw_splitbar(HWND hWnd, int x)
 
 static void ResizeWnd(ChildWnd* pChildWnd, int cx, int cy)
 {
-    HDWP hdwp = BeginDeferWindowPos(2);
-    RECT rt, rs;
-
+    HDWP hdwp = BeginDeferWindowPos(3);
+    RECT rt, rs, rb;
+    const int tHeight = 18;
     SetRect(&rt, 0, 0, cx, cy);
     cy = 0;
     if (hStatusBar != NULL) {
         GetWindowRect(hStatusBar, &rs);
-        cy = rs.bottom - rs.top + 8;
+        cy = rs.bottom - rs.top;
     }
+    GetWindowRect(pChildWnd->hAddressBtnWnd, &rb);
     cx = pChildWnd->nSplitPos + SPLIT_WIDTH/2;
-       DeferWindowPos(hdwp, pChildWnd->hAddressBarWnd, 0, rt.left, rt.top, rt.right-rt.left, 23, SWP_NOZORDER|SWP_NOACTIVATE);
-    DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top + 25, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
-    DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx  , rt.top + 25, rt.right-cx, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
+    DeferWindowPos(hdwp, pChildWnd->hAddressBarWnd, 0, rt.left, rt.top, rt.right-rt.left - tHeight-2, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
+    DeferWindowPos(hdwp, pChildWnd->hAddressBtnWnd, 0, rt.right - tHeight, rt.top, tHeight, tHeight, SWP_NOZORDER|SWP_NOACTIVATE);
+    DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top + tHeight+2, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
+    DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx, rt.top + tHeight+2, rt.right-cx, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
     EndDeferWindowPos(hdwp);
 }
 
@@ -87,10 +89,9 @@ static void OnPaint(HWND hWnd)
 {
     PAINTSTRUCT ps;
     RECT rt;
-    HDC hdc;
 
     GetClientRect(hWnd, &rt);
-    hdc = BeginPaint(hWnd, &ps);
+    BeginPaint(hWnd, &ps);
     FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
     EndPaint(hWnd, &ps);
 }
@@ -228,8 +229,8 @@ static void SuggestKeys(HKEY hRootKey, LPCTSTR pszKeyPath, LPTSTR pszSuggestions
                        bFound = FALSE;
 
                        /* Check default key */
-                       if (RegQueryStringValue(hRootKey, pszKeyPath, NULL,
-                               szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
+                       if (QueryStringValue(hRootKey, pszKeyPath, NULL,
+                               szBuffer, COUNT_OF(szBuffer)) == ERROR_SUCCESS)
                        {
                                /* Sanity check this key; it cannot be empty, nor can it be a
                                 * loop back */
@@ -260,8 +261,8 @@ static void SuggestKeys(HKEY hRootKey, LPCTSTR pszKeyPath, LPTSTR pszSuggestions
                /* Check CLSID key */
                if (RegOpenKey(hRootKey, pszKeyPath, &hSubKey) == ERROR_SUCCESS)
                {
-                       if (RegQueryStringValue(hSubKey, TEXT("CLSID"), NULL,
-                               szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
+                       if (QueryStringValue(hSubKey, TEXT("CLSID"), NULL, szBuffer,
+                                            COUNT_OF(szBuffer)) == ERROR_SUCCESS)
                        {
                                lstrcpyn(pszSuggestions, TEXT("HKCR\\CLSID\\"), (int) iSuggestionsLength);
                                i = _tcslen(pszSuggestions);
@@ -300,17 +301,6 @@ LRESULT CALLBACK AddressBarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPar
        return CallWindowProc(oldwndproc, hwnd, uMsg, wParam, lParam);
 }
 
-/* fix coords to top-left when SHIFT-F10 is pressed */
-void FixPointIfContext(POINTS *pt, HWND hWnd)
-{
-    if (pt->x == -1 && pt->y == -1) {
-        POINT p = { 0, 0 };
-        ClientToScreen(hWnd, &p);
-        pt->x = (WORD)(p.x);
-        pt->y = (WORD)(p.y);
-    }
-}
-
 /*******************************************************************************
  *
  *  FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
@@ -336,29 +326,35 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
         /* load "My Computer" string */
         LoadString(hInst, IDS_MY_COMPUTER, buffer, sizeof(buffer)/sizeof(TCHAR));
 
-           g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ChildWnd));
+        g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ChildWnd));
 
         if (!pChildWnd) return 0;
         _tcsncpy(pChildWnd->szPath, buffer, MAX_PATH);
         pChildWnd->nSplitPos = 250;
         pChildWnd->hWnd = hWnd;
-               pChildWnd->hAddressBarWnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP,
+        pChildWnd->hAddressBarWnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP,
                                                                                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                                                                                hWnd, (HMENU)0, hInst, 0);
+        pChildWnd->hAddressBtnWnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("Button"), _T("ยป"), WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP | BS_DEFPUSHBUTTON,
+                                        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+                                        hWnd, (HMENU)0, hInst, 0);
                pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, (HMENU) TREE_WINDOW);
         pChildWnd->hListWnd = CreateListView(hWnd, (HMENU) LIST_WINDOW/*, pChildWnd->szPath*/);
         SetFocus(pChildWnd->hTreeWnd);
 
-        /* set the address bar font */
-        if (pChildWnd->hAddressBarWnd)
+        /* set the address bar and button font */
+        if ((pChildWnd->hAddressBarWnd) && (pChildWnd->hAddressBtnWnd))
         {
             hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
             SendMessage(pChildWnd->hAddressBarWnd,
                         WM_SETFONT,
                         (WPARAM)hFont,
                         0);
+            SendMessage(pChildWnd->hAddressBtnWnd,
+                        WM_SETFONT,
+                        (WPARAM)hFont,
+                        0);
         }
-
                /* Subclass the AddressBar */
                oldproc = (WNDPROC)(LONG_PTR)GetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_WNDPROC);
         SetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_USERDATA, (DWORD_PTR)oldproc);
@@ -366,7 +362,10 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
                break;
     }
     case WM_COMMAND:
-        if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
+        if(HIWORD(wParam) == BN_CLICKED){
+            PostMessage(pChildWnd->hAddressBarWnd, WM_KEYUP, VK_RETURN, 0);
+        }
+        else if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
             goto def;
         }
         break;
@@ -489,10 +488,28 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
                        rootName = get_root_key_name(hRootKey);
                        fullPath = HeapAlloc(GetProcessHeap(), 0, (_tcslen(rootName) + 1 + _tcslen(keyPath) + 1) * sizeof(TCHAR));
                        if (fullPath) {
-                           _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
+                           /* set (correct) the address bar text */
+                if(keyPath[0] != '\0')
+                    _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
+                else
+                    fullPath = _tcscpy(fullPath, rootName);
                            SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
                                SendMessage(pChildWnd->hAddressBarWnd, WM_SETTEXT, 0, (LPARAM)fullPath);
-                           HeapFree(GetProcessHeap(), 0, fullPath);
+                HeapFree(GetProcessHeap(), 0, 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);
+                               /* compare the strings to see if we should enable/disable the "Load Hive" menus accordingly */
+                               if (!(_tcsicmp(rootName, TEXT("HKEY_LOCAL_MACHINE")) &&
+                      _tcsicmp(rootName, TEXT("HKEY_USERS"))))
+                {
+                    // 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] == '\0')
+                        EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_LOADHIVE, MF_BYCOMMAND | MF_ENABLED);
+                                   else if(!_tcschr(keyPath, _T('\\')))
+                        EnableMenuItem(GetSubMenu(hMenuFrame,0), ID_REGISTRY_UNLOADHIVE, MF_BYCOMMAND | MF_ENABLED);
+                               }
 
                            {
                                HKEY hKey;
@@ -547,8 +564,8 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
                     }
                     else
                     {
-                      if (RegRenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
-                                                 lResult = FALSE;
+                      if (RenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
+                        lResult = FALSE;
                     }
                     return lResult;
                   }
@@ -578,16 +595,29 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
 
     case WM_CONTEXTMENU:
     {
-      POINTS pt;
+      POINT pt;
       if((HWND)wParam == pChildWnd->hListWnd)
       {
         int i, cnt;
         BOOL IsDefault;
-        pt.x = LOWORD(lParam);
-               pt.y = HIWORD(lParam);
+        pt.x = (short) LOWORD(lParam);
+        pt.y = (short) HIWORD(lParam);
         cnt = ListView_GetSelectedCount(pChildWnd->hListWnd);
         i = ListView_GetNextItem(pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
-        FixPointIfContext(&pt, pChildWnd->hListWnd);
+        if (pt.x == -1 && pt.y == -1)
+        {
+            RECT rc;
+            if (i != -1)
+            {
+                rc.left = LVIR_BOUNDS;
+                SendMessage(pChildWnd->hListWnd, LVM_GETITEMRECT, i, (LPARAM) &rc);
+                pt.x = rc.left + 8;
+                pt.y = rc.top + 8;
+            }
+            else
+                pt.x = pt.y = 0;
+            ClientToScreen(pChildWnd->hListWnd, &pt);
+        }
         if(i == -1)
         {
           TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
@@ -621,14 +651,33 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
         int iLastPos;
         WORD wID;
 
-        pt.x = LOWORD(lParam);
-               pt.y = HIWORD(lParam);
-        hti.pt.x = pt.x;
-        hti.pt.y = pt.y;
-        ScreenToClient(pChildWnd->hTreeWnd, &hti.pt);
-        (void)TreeView_HitTest(pChildWnd->hTreeWnd, &hti);
+        pt.x = (short) LOWORD(lParam);
+        pt.y = (short) HIWORD(lParam);
+
+        if (pt.x == -1 && pt.y == -1)
+        {
+            RECT rc;
+            hti.hItem = TreeView_GetSelection(pChildWnd->hTreeWnd);
+            if (hti.hItem != NULL)
+            {
+                TreeView_GetItemRect(pChildWnd->hTreeWnd, hti.hItem, &rc, TRUE);
+                pt.x = rc.left + 8;
+                pt.y = rc.top + 8;
+                ClientToScreen(pChildWnd->hTreeWnd, &pt);
+                hti.flags = TVHT_ONITEM;
+            }
+            else
+                hti.flags = 0;
+        }
+        else
+        {
+            hti.pt.x = pt.x;
+            hti.pt.y = pt.y;
+            ScreenToClient(pChildWnd->hTreeWnd, &hti.pt);
+            (void)TreeView_HitTest(pChildWnd->hTreeWnd, &hti);
+        }
 
-        if ((hti.flags & TVHT_ONITEM) != 0 || (pt.x == -1 && pt.y == -1))
+        if (hti.flags & TVHT_ONITEM)
         {
           hContextMenu = GetSubMenu(hPopupMenus, PM_TREECONTEXT);
           (void)TreeView_SelectItem(pChildWnd->hTreeWnd, hti.hItem);
@@ -689,7 +738,6 @@ LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
               s += _tcslen(s) + 1;
                        }
                  }
-          FixPointIfContext(&pt, pChildWnd->hTreeWnd);
           TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, pChildWnd->hWnd, NULL);
         }
       }