/*---------------------------------------------------------------------------------------------------------------------------------------------------
Program Listing for:  FTPTreeCtrl2.cpp
Project:  treeview
Namespace:  cpp
----------------------------------------------------------------------------------------------------------------------------------------------------*/

// FTPTreeCtrl2.cpp : Implementation of CFTPTreeCtrl2

#include "stdafx.h"
#include "FTPTree2.h"
#include "FTPTreeCtrl2.h"
#include <comdef.h>
#include <string>
#pragma comment(lib, "comctl32.lib")

using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CFTPTreeCtrl2


STDMETHODIMP CFTPTreeCtrl2::AddFile(BSTR file_path, BSTR file_path_short, long lParent, long lIsFolder, long *lReturn)
{

         HTREEITEM htParent;
         if (lParent == 0L)
                  htParent = (HTREEITEM)NULL;
         else
                  htParent = (HTREEITEM)lParent;

         int imageIndex = (lIsFolder == 0L) ? 0 : 1;
         //file_path_short
         HTREEITEM hTRoot2 = AddOneItem ( htParent, (LPSTR)(_bstr_t)file_path_short, (LPSTR)(_bstr_t)file_path, (HTREEITEM)TVI_ROOT,
                                    imageIndex, m_ctlSysTreeView32.m_hWnd);

         if (hTRoot2 == NULL) 
                  *lReturn = 0L;
         else
                  *lReturn = (long)hTRoot2;

         // Save the heading level in the item's application-defined 
    // data area. 
    // tvi.lParam = (LPARAM) nLevel; 

         return S_OK;
}

HTREEITEM CFTPTreeCtrl2::AddOneItem(HTREEITEM hParent, LPSTR szText, LPSTR szFullPath, HTREEITEM hInsAfter, int iImage, HWND hwndTree)
{

         HTREEITEM hItem;
         TV_ITEM tvI;
         TV_INSERTSTRUCT tvIns;

         int n, k;
         for (n=0;n < 600;n++) {
                  if (szFullPath[n] == (char)0)
                           break;
         }

         //typedef basic_string<char> string;
         char *test_string;
         test_string = new char[n + 2];

         for (k=0; k  < n; k++) {
                  test_string[k] = szFullPath[k];
         }
         test_string[k] = (char)0;

         // The pszText, iImage, and iSelectedImage members are filled out.
         tvI.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
         tvI.pszText = szText;
         tvI.cchTextMax = lstrlen ( szText);
         tvI.iImage = iImage;
         tvI.iSelectedImage = iImage;
    tvI.lParam = (DWORD)test_string; // (DWORD)test_string; 

         tvIns.item = tvI;
         tvIns.hInsertAfter = hInsAfter;
         tvIns.hParent = hParent;
         //MessageBox(test_string);

         // Insert the item into the tree.
         hItem = (HTREEITEM) SendMessage (hwndTree, TVM_INSERTITEM, 0, 
                  (LPARAM)(LPTV_INSERTSTRUCT)&tvIns);

         //m_stringVector.push_back(test_string);

         return (hItem);

}

STDMETHODIMP CFTPTreeCtrl2::DeleteItems()
{
         // TODO: Add your implementation code here
         TreeView_DeleteAllItems(m_ctlSysTreeView32.m_hWnd);
         return S_OK;
}

STDMETHODIMP CFTPTreeCtrl2::GetFirstTreeItem(long *treeitem)
{
         // TODO: Add your implementation code here
         *treeitem = (long)TreeView_GetRoot( m_ctlSysTreeView32.m_hWnd );                  
         return S_OK;
}

STDMETHODIMP CFTPTreeCtrl2::GetNextSibling(long tree1, long *tree2)
{
         // TODO: Add your implementation code here
         *tree2 = (long)TreeView_GetNextSibling(m_ctlSysTreeView32.m_hWnd, (HTREEITEM)tree1); 
         if (*tree2 == NULL)
                  *tree2 = 0;
         return S_OK;
}

STDMETHODIMP CFTPTreeCtrl2::GetChild(long treeview, long *treeret)
{
         // TODO: Add your implementation code here
         *treeret = (long)TreeView_GetChild(m_ctlSysTreeView32.m_hWnd, (HTREEITEM)treeview); 
         if (*treeret == NULL)
                  *treeret = 0;

         return S_OK;
}

STDMETHODIMP CFTPTreeCtrl2::GetTreeItemLabel(long treeitem, BSTR *label)
{

         TVITEM tvi;
         tvi.mask = TVIF_TEXT ;
         char *cptr = new char[401];
         tvi.pszText = cptr;
         tvi.cchTextMax = 400;

         tvi.hItem = (HTREEITEM) treeitem;

         if (TreeView_GetItem( m_ctlSysTreeView32.m_hWnd, &tvi) ) {
                  *label = (_bstr_t)(tvi.pszText);
         } else {
                  *label = (_bstr_t)m_EmptyString;
         }

         delete cptr;
         return S_OK;

}

STDMETHODIMP CFTPTreeCtrl2::GetTreeItemLPString(long treeitem, BSTR *filePath)
{

         TVITEM tvi;
         tvi.mask = TVIF_PARAM ;

         tvi.hItem = (HTREEITEM) treeitem;

         if (TreeView_GetItem( m_ctlSysTreeView32.m_hWnd, &tvi) ) {

                  char *test_string;
                  test_string = (char *)(tvi.lParam);
                  *filePath = (_bstr_t)test_string;

         } else {
                  *filePath = (_bstr_t)m_EmptyString;
         }

         return S_OK;

}