HTREEITEM CFileAndFolder::InsertFileItem(const CString& sFile, const CString& sPath, HTREEITEM hParent) { //Retreive the icon indexes for the specified file/folder int nIconIndex = GetIconIndex(sPath); int nSelIconIndex = GetSelIconIndex(sPath); if (nIconIndex == -1 || nSelIconIndex == -1) { TRACE(_T("Failed in call to SHGetFileInfo for %s, GetLastError:%d\n"), sPath, ::GetLastError()); return NULL; } //Add the actual item CString sTemp(sFile); TV_INSERTSTRUCT tvis; ZeroMemory(&tvis, sizeof(TV_INSERTSTRUCT)); tvis.hParent = hParent; tvis.hInsertAfter = TVI_LAST; tvis.item.mask = TVIF_CHILDREN | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT; tvis.item.pszText = sTemp.GetBuffer(sTemp.GetLength()); tvis.item.cchTextMax = sTemp.GetLength(); tvis.item.iImage = nIconIndex; tvis.item.iSelectedImage = nSelIconIndex; tvis.item.cChildren = HasGotSubEntries(sPath); HTREEITEM hItem = m_Tree.InsertItem(&tvis); sTemp.ReleaseBuffer(); return hItem; }
void CTreeFileCtrl::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult) { TV_DISPINFO* pDispInfo = (TV_DISPINFO*)pNMHDR; if (pDispInfo->item.pszText) { SHFILEOPSTRUCT shfo; ZeroMemory(&shfo, sizeof(SHFILEOPSTRUCT)); shfo.hwnd = AfxGetMainWnd()->GetSafeHwnd(); shfo.wFunc = FO_RENAME; shfo.fFlags = FOF_ALLOWUNDO; CString sFrom = ItemToPath(pDispInfo->item.hItem); int nFromLength = sFrom.GetLength(); TCHAR* pszFrom = new TCHAR[nFromLength + 2]; _tcscpy(pszFrom, sFrom); pszFrom[nFromLength+1] = _T('\0'); shfo.pFrom = pszFrom; HTREEITEM hParent = GetParentItem(pDispInfo->item.hItem); CString sParent = ItemToPath(hParent); CString sTo; if (IsDrive(sParent)) sTo = sParent + pDispInfo->item.pszText; else sTo = sParent + _T("\\") + pDispInfo->item.pszText; int nToLength = _tcslen(sTo); TCHAR* pszTo = new TCHAR[nToLength + 2]; _tcscpy(pszTo, sTo); pszTo[nToLength+1] = _T('\0'); shfo.pTo = pszTo; //Let the shell perform the actual rename if (SHFileOperation(&shfo) == 0 && shfo.fAnyOperationsAborted == FALSE) { *pResult = TRUE; //Update its text SetItemText(pDispInfo->item.hItem, pDispInfo->item.pszText); //Also update the icons for it CString sPath = ItemToPath(pDispInfo->item.hItem); SetItemImage(pDispInfo->item.hItem, GetIconIndex(sPath), GetSelIconIndex(sPath)); } //Don't forget to free up the memory we allocated delete [] pszFrom; delete [] pszTo; } *pResult = 0; }
void FolderTree::DisplayPath(const tstring &sPath, HTREEITEM hParent, bool bUseSetRedraw /* = true */) { //CWaitCursor c; //Speed up the job by turning off redraw if (bUseSetRedraw) SetRedraw(false); //Remove all the items currently under hParent HTREEITEM hChild = GetChildItem(hParent); while (hChild) { DeleteItem(hChild); hChild = GetChildItem(hParent); } //Should we display the root folder if (m_bShowRootedFolder && (hParent == TVI_ROOT)) { FolderTreeItemInfo* pItem = new FolderTreeItemInfo; pItem->m_sFQPath = m_sRootFolder; pItem->m_sRelativePath = m_sRootFolder; m_hRootedFolder = InsertFileItem(TVI_ROOT, pItem, false, GetIconIndex(m_sRootFolder), GetSelIconIndex(m_sRootFolder), true); Expand(m_hRootedFolder, TVE_EXPAND); return; } //find all the directories underneath sPath int nDirectories = 0; tstring sFile; if (sPath[sPath.size()-1] != _T('\\')) sFile = sPath + _T("\\"); else sFile = sPath; WIN32_FIND_DATA fData; HANDLE hFind; hFind = FindFirstFile((sFile + _T("*")).c_str(), &fData); if(hFind != INVALID_HANDLE_VALUE) { do { tstring filename = fData.cFileName; if((fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (filename != _T(".")) && (filename != _T(".."))) { ++nDirectories; tstring path = sFile + filename; TCHAR szPath[_MAX_PATH]; TCHAR szFname[_MAX_FNAME]; TCHAR szExt[_MAX_EXT]; _tsplitpath(path.c_str(), NULL, NULL, szFname, szExt); _tmakepath(szPath, NULL, NULL, szFname, szExt); FolderTreeItemInfo* pItem = new FolderTreeItemInfo; pItem->m_sFQPath = path; pItem->m_sRelativePath = szPath; InsertFileItem(hParent, pItem, m_bShowSharedUsingDifferentIcon && IsShared(path), GetIconIndex(path), GetSelIconIndex(path), true); } } while (FindNextFile(hFind, &fData)); } FindClose(hFind); //Now sort the items we just added TVSORTCB tvsortcb; tvsortcb.hParent = hParent; tvsortcb.lpfnCompare = CompareByFilenameNoCase; tvsortcb.lParam = 0; SortChildrenCB(&tvsortcb); //We want to add them before sorting checkRemovedDirs(sFile, hParent); //If no items were added then remove the "+" indicator from hParent if(nDirectories == 0) SetHasPlusButton(hParent, FALSE); //Turn back on the redraw flag if(bUseSetRedraw) SetRedraw(true); }
void FolderTree::DisplayDrives(HTREEITEM hParent, bool bUseSetRedraw) { //CWaitCursor c; //Speed up the job by turning off redraw if (bUseSetRedraw) SetRedraw(false); //Enumerate the drive letters and add them to the tree control DWORD dwDrives = GetLogicalDrives(); DWORD dwMask = 1; for (int i=0; i<32; i++) { if (dwDrives & dwMask) { tstring sDrive; sDrive = (wchar_t)('A' + i); sDrive += _T(":\\"); //check if this drive is one of the types to hide if (CanDisplayDrive(sDrive)) { FolderTreeItemInfo* pItem = new FolderTreeItemInfo; pItem->m_sFQPath = sDrive; pItem->m_sRelativePath = sDrive; //Insert the item into the view InsertFileItem(hParent, pItem, m_bShowSharedUsingDifferentIcon && IsShared(sDrive), GetIconIndex(sDrive), GetSelIconIndex(sDrive), true); } } dwMask <<= 1; } if (bUseSetRedraw) SetRedraw(true); }
void FolderTree::Refresh() { //Just in case this will take some time //CWaitCursor wait; SetRedraw(FALSE); //Get the item which is currently selected HTREEITEM hSelItem = GetSelectedItem(); tstring sItem; bool bExpanded = false; if (hSelItem) { sItem = ItemToPath(hSelItem); bExpanded = IsExpanded(hSelItem); } theSharedEnumerator.Refresh(); //Remove all nodes that currently exist Clear(); //Display the folder items in the tree if (m_sRootFolder.empty()) { //Should we insert a "My Computer" node if (m_bShowMyComputer) { FolderTreeItemInfo* pItem = new FolderTreeItemInfo; pItem->m_bNetworkNode = false; int nIcon = 0; int nSelIcon = 0; //Get the localized name and correct icons for "My Computer" LPITEMIDLIST lpMCPidl; if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &lpMCPidl))) { SHFILEINFO sfi; if (SHGetFileInfo((LPCTSTR)lpMCPidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL | SHGFI_DISPLAYNAME)) pItem->m_sRelativePath = sfi.szDisplayName; nIcon = GetIconIndex(lpMCPidl); nSelIcon = GetSelIconIndex(lpMCPidl); //Free up the pidl now that we are finished with it //ASSERT(m_pMalloc); m_pMalloc->Free(lpMCPidl); m_pMalloc->Release(); } //Add it to the tree control m_hMyComputerRoot = InsertFileItem(TVI_ROOT, pItem, false, nIcon, nSelIcon, false); SetHasSharedChildren(m_hMyComputerRoot); } //Display all the drives if (!m_bShowMyComputer) DisplayDrives(TVI_ROOT, false); //Display homegroup /*if (m_bDisplayNetwork) { FolderTreeItemInfo* pItem = new FolderTreeItemInfo; //pItem-> = true; int nIcon = 0; int nSelIcon = 0; //Get the localized name and correct icons for "Network Neighborhood" LPITEMIDLIST lpNNPidl; //SHGetKnownFolderIDList(FOLDERID_HomeGroup, 0, NULL, &lpNNPidl); if (SUCCEEDED(SHGetKnownFolderIDList(FOLDERID_HomeGroup, 0, NULL, &lpNNPidl))) { SHFILEINFO sfi; if (SHGetFileInfo((LPCTSTR)lpNNPidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL | SHGFI_DISPLAYNAME)) pItem->m_sRelativePath = sfi.szDisplayName; nIcon = GetIconIndex(lpNNPidl); nSelIcon = GetSelIconIndex(lpNNPidl); //Free up the pidl now that we are finished with it //ASSERT(m_pMalloc); m_pMalloc->Free(lpNNPidl); m_pMalloc->Release(); } //Add it to the tree control m_hHomegroupRoot = InsertFileItem(TVI_ROOT, pItem, false, nIcon, nSelIcon, false, shared); SetHasSharedChildren(m_hHomegroupRoot, shared); //checkRemovedDirs(Util::emptyStringT, TVI_ROOT, shared); }*/ //Also add network neighborhood if requested to do so if (m_bDisplayNetwork) { FolderTreeItemInfo* pItem = new FolderTreeItemInfo; pItem->m_bNetworkNode = true; int nIcon = 0; int nSelIcon = 0; //Get the localized name and correct icons for "Network Neighborhood" LPITEMIDLIST lpNNPidl; if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_NETWORK, &lpNNPidl))) { SHFILEINFO sfi; if (SHGetFileInfo((LPCTSTR)lpNNPidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL | SHGFI_DISPLAYNAME)) pItem->m_sRelativePath = sfi.szDisplayName; nIcon = GetIconIndex(lpNNPidl); nSelIcon = GetSelIconIndex(lpNNPidl); //Free up the pidl now that we are finished with it //ASSERT(m_pMalloc); m_pMalloc->Free(lpNNPidl); m_pMalloc->Release(); } //Add it to the tree control m_hNetworkRoot = InsertFileItem(TVI_ROOT, pItem, false, nIcon, nSelIcon, false); SetHasSharedChildren(m_hNetworkRoot); //checkRemovedDirs(Util::emptyStringT, TVI_ROOT, shared); } } else { DisplayPath(m_sRootFolder, TVI_ROOT, false); } //Reselect the initially selected item if (hSelItem) SetSelectedPath(sItem, bExpanded); //Turn back on the redraw flag SetRedraw(true); }
bool FolderTree::EnumNetwork(HTREEITEM hParent) { //What will be the return value from this function bool bGotChildren = false; //Check if the item already has a network resource and use it. FolderTreeItemInfo* pItem = (FolderTreeItemInfo*) GetItemData(hParent); NETRESOURCE* pNetResource = pItem->m_pNetResource; //Setup for the network enumeration HANDLE hEnum; DWORD dwResult = WNetOpenEnum(pNetResource ? RESOURCE_GLOBALNET : RESOURCE_CONTEXT, m_dwNetworkItemTypes, 0, pNetResource ? pNetResource : NULL, &hEnum); //Was the read sucessful if (dwResult != NO_ERROR) { //TRACE(_T("Cannot enumerate network drives, Error:%d\n"), dwResult); return FALSE; } //Do the network enumeration DWORD cbBuffer = 16384; bool bNeedMoreMemory = true; bool bSuccess = false; LPNETRESOURCE lpnrDrv = NULL; DWORD cEntries = 0; while (bNeedMoreMemory && !bSuccess) { //Allocate the memory and enumerate lpnrDrv = (LPNETRESOURCE) new BYTE[cbBuffer]; cEntries = 0xFFFFFFFF; dwResult = WNetEnumResource(hEnum, &cEntries, lpnrDrv, &cbBuffer); if (dwResult == ERROR_MORE_DATA) { //Free up the heap memory we have used delete [] lpnrDrv; cbBuffer *= 2; } else if (dwResult == NO_ERROR) bSuccess = true; else bNeedMoreMemory = false; } //Enumeration successful? if (bSuccess) { //Scan through the results for (DWORD i=0; i<cEntries; i++) { tstring sNameRemote; if(lpnrDrv[i].lpRemoteName != NULL) sNameRemote = lpnrDrv[i].lpRemoteName; else sNameRemote = lpnrDrv[i].lpComment; //Remove leading back slashes if (sNameRemote.size() > 0 && sNameRemote[0] == _T('\\')) sNameRemote = sNameRemote.substr(1); if (sNameRemote.size() > 0 && sNameRemote[0] == _T('\\')) sNameRemote = sNameRemote.substr(1); //Setup the item data for the new item pItem = new FolderTreeItemInfo; pItem->m_pNetResource = new NETRESOURCE; memzero(pItem->m_pNetResource, sizeof(NETRESOURCE)); *pItem->m_pNetResource = lpnrDrv[i]; if (lpnrDrv[i].lpLocalName) pItem->m_pNetResource->lpLocalName = _tcsdup(lpnrDrv[i].lpLocalName); if (lpnrDrv[i].lpRemoteName) pItem->m_pNetResource->lpRemoteName = _tcsdup(lpnrDrv[i].lpRemoteName); if (lpnrDrv[i].lpComment) pItem->m_pNetResource->lpComment = _tcsdup(lpnrDrv[i].lpComment); if (lpnrDrv[i].lpProvider) pItem->m_pNetResource->lpProvider = _tcsdup(lpnrDrv[i].lpProvider); if (lpnrDrv[i].lpRemoteName) pItem->m_sFQPath = lpnrDrv[i].lpRemoteName; else pItem->m_sFQPath = sNameRemote; pItem->m_sRelativePath = sNameRemote; pItem->m_bNetworkNode = true; //Display a share and the appropiate icon if (lpnrDrv[i].dwDisplayType == RESOURCEDISPLAYTYPE_SHARE) { //Display only the share name int nPos = pItem->m_sRelativePath.find(_T('\\')); if (nPos >= 0) pItem->m_sRelativePath = pItem->m_sRelativePath.substr(nPos+1); //Now add the item into the control InsertFileItem(hParent, pItem, m_bShowSharedUsingDifferentIcon, GetIconIndex(pItem->m_sFQPath), GetSelIconIndex(pItem->m_sFQPath), false); } else if (lpnrDrv[i].dwDisplayType == RESOURCEDISPLAYTYPE_SERVER) { //Now add the item into the control tstring sServer = _T("\\\\"); sServer += pItem->m_sRelativePath; InsertFileItem(hParent, pItem, false, GetIconIndex(sServer), GetSelIconIndex(sServer), false); } else { //Now add the item into the control //Just use the generic Network Neighborhood icons for everything else LPITEMIDLIST lpNNPidl; int nIcon = 0xFFFF; int nSelIcon = nIcon; if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_NETWORK, &lpNNPidl))) { nIcon = GetIconIndex(lpNNPidl); nSelIcon = GetSelIconIndex(lpNNPidl); //Free up the pidl now that we are finished with it //ASSERT(m_pMalloc); m_pMalloc->Free(lpNNPidl); m_pMalloc->Release(); } InsertFileItem(hParent, pItem, false, nIcon, nSelIcon, false); } bGotChildren = true; } } /* else TRACE(_T("Cannot complete network drive enumeration, Error:%d\n"), dwResult); */ //Clean up the enumeration handle WNetCloseEnum(hEnum); //Free up the heap memory we have used delete [] lpnrDrv; //Return whether or not we added any items return bGotChildren; }