- Get autochk, calc, cmd, devmgr, expand, format, gettype, hostname, lsass, msconfig...
[reactos.git] / reactos / subsys / system / regedit / treeview.c
index 726d06d..536434a 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#define WIN32_LEAN_AND_MEAN     /* Exclude rarely-used stuff from Windows headers */
-
-#define NONAMELESSUNION
-#define NONAMELESSSTRUCT
-#include <windows.h>
-#include <commctrl.h>
-#include <stdlib.h>
-#include <tchar.h>
-#include <process.h>
-#include <stdio.h>
-#include <string.h>
-#include <tchar.h>
-
-#include "main.h"
+#include <regedit.h>
 
 /* Global variables and constants  */
 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images.  */
@@ -101,10 +88,19 @@ LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
     if (maxLen == -1) return NULL;
     if (!hItem) hItem = TreeView_GetSelection(hwndTV);
     if (!hItem) return NULL;
-    if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
+    if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) {
+               return NULL;
+       }
     return pathBuffer;
 }
 
+BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
+{
+    if (!hItem) hItem = TreeView_GetSelection(hwndTV);
+    if (!hItem) return FALSE;
+    return TreeView_DeleteItem(hwndTV, hItem);
+}
+
 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
 {
@@ -124,13 +120,13 @@ static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HK
     tvi.iSelectedImage = Image_Open;
     tvi.cChildren = dwChildren;
     tvi.lParam = (LPARAM)hKey;
-    tvins.u.item = tvi;
+    tvins.item = tvi;
     tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_FIRST);
     tvins.hParent = hParent;
     return TreeView_InsertItem(hwndTV, &tvins);
 }
 
-static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
+BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
 {
     HKEY hRoot, hKey, hSubKey;
     HTREEITEM childItem;
@@ -177,9 +173,9 @@ static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
         goto done;
     }
     tvItem.cchTextMax = dwMaxSubKeyLen;
-    if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
+    /*if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
         goto done;
-    }
+    }*/
 
     /* Get all of the tree node siblings in one contiguous block of memory */
     {
@@ -310,28 +306,46 @@ HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
     HTREEITEM hNewItem = 0;
     TVITEMEX item;
 
-    if (!hItem) hItem = TreeView_GetSelection(hwndTV);
-    if (!hItem) return FALSE;
-    if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
-       hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
-       SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM) hItem);
-    } else {
-       item.mask = TVIF_CHILDREN | TVIF_HANDLE;
-       item.hItem = hItem;
-       if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
-       item.cChildren = 1;
-       if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
+    /* Default to the current selection */
+    if (!hItem)
+    {
+        hItem = TreeView_GetSelection(hwndTV);
+        if (!hItem)
+            return FALSE;
+    }
+
+    memset(&item, 0, sizeof(item));
+    item.hItem = hItem;
+    item.mask = TVIF_CHILDREN | TVIF_HANDLE | TVIF_STATE;
+    if (!TreeView_GetItem(hwndTV, &item))
+        return FALSE;
+
+    if ((item.state & TVIS_EXPANDEDONCE) && (item.cChildren > 0))
+    {
+        hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
+        SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM) hItem);
     }
+       else
+    {
+        item.mask = TVIF_CHILDREN | TVIF_HANDLE;
+        item.hItem = hItem;
+        item.cChildren = 1;
+        if (!TreeView_SetItem(hwndTV, &item))
+            return FALSE;
+    }
+
     TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
-    if (!hNewItem) {
-       for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
-           item.mask = TVIF_HANDLE | TVIF_TEXT;
-           item.hItem = hNewItem;
-           item.pszText = buf;
-           item.cchTextMax = COUNT_OF(buf);
-           if (!TreeView_GetItem(hwndTV, &item)) continue;
-           if (lstrcmp(name, item.pszText) == 0) break;
-       }       
+    if (!hNewItem)
+    {
+        for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem))
+        {
+            item.mask = TVIF_HANDLE | TVIF_TEXT;
+            item.hItem = hNewItem;
+            item.pszText = buf;
+            item.cchTextMax = COUNT_OF(buf);
+            if (!TreeView_GetItem(hwndTV, &item)) continue;
+            if (lstrcmp(name, item.pszText) == 0) break;
+        }      
     }
     if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
 
@@ -362,7 +376,7 @@ static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
     tvi.cChildren = 5;
     /* Save the heading level in the item's application-defined data area.  */
     tvi.lParam = (LPARAM)NULL;
-    tvins.u.item = tvi;
+    tvins.item = tvi;
     tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
     tvins.hParent = TVI_ROOT;
     /* Add the item to the tree view control.  */
@@ -508,6 +522,7 @@ BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
     if (LoadString(hInst, IDS_NEW_KEY, szNewKeyFormat, sizeof(szNewKeyFormat) / sizeof(szNewKeyFormat[0])) <= 0)
         goto done;
 
+    /* Need to create a new key with a unique name */
     do
     {
         _sntprintf(szNewKey, sizeof(szNewKey) / sizeof(szNewKey[0]), szNewKeyFormat, iIndex++);
@@ -520,10 +535,12 @@ BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
     }
     while(!hNewKey);
 
-    hNewItem = AddEntryToTree(hwndTV, hItem, szNewKey, NULL, 0);
+    /* Insert the new key */
+    hNewItem = InsertNode(hwndTV, hItem, szNewKey);
     if (!hNewItem)
         goto done;
-    SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM) hItem);
+
+    /* The new key's name is probably not appropriate yet */
     TreeView_EditLabel(hwndTV, hNewItem);
 
     bSuccess = TRUE;
@@ -542,7 +559,7 @@ done:
  * Returns the handle to the new control if successful, or NULL otherwise.
  * hwndParent - handle to the control's parent window.
  */
-HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
+HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, HMENU id)
 {
     RECT rcClient;
     HWND hwndTV;
@@ -552,7 +569,7 @@ HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
     hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, NULL,
                             WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_EDITLABELS,
                             0, 0, rcClient.right, rcClient.bottom,
-                            hwndParent, (HMENU)id, hInst, NULL);
+                            hwndParent, id, hInst, NULL);
     /* Initialize the image list, and add items to the control.  */
     if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
         DestroyWindow(hwndTV);
@@ -561,6 +578,11 @@ HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
     return hwndTV;
 }
 
+void DestroyTreeView() {
+       if (pathBuffer)
+               HeapFree(GetProcessHeap(), 0, pathBuffer);
+}
+
 BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
 {
        HTREEITEM hRoot, hItem;
@@ -570,6 +592,10 @@ BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
        LPCTSTR s;
        TVITEM tvi;
 
+    /* Total no-good hack */
+    if (!_tcsncmp(keyPath, _T("My Computer\\"), 12))
+        keyPath += 12;
+
        hRoot = TreeView_GetRoot(hwndTV);
        hItem = hRoot;
 
@@ -600,7 +626,7 @@ BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
                {
                        memset(&tvi, 0, sizeof(tvi));
                        tvi.hItem = hChildItem;
-                       tvi.mask = TVIF_TEXT;
+                       tvi.mask = TVIF_TEXT | TVIF_CHILDREN;
                        tvi.pszText = szBuffer;
                        tvi.cchTextMax = sizeof(szBuffer) / sizeof(szBuffer[0]);
 
@@ -613,8 +639,11 @@ BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
                if (!hChildItem)
                        return FALSE;
 
-               if (!TreeView_Expand(hwndTV, hChildItem, TVE_EXPAND))
-                       return FALSE;
+               if (tvi.cChildren > 0)
+               {
+                       if (!TreeView_Expand(hwndTV, hChildItem, TVE_EXPAND))
+                               return FALSE;
+               }
 
                keyPath = s ? s + 1 : _T("");
                hItem = hChildItem;