⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 childfrm.cpp

📁 c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++6.0
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
// FileFury
// Copyright (c) 2000 Tenebril Incorporated
// All rights reserved.
//
// This source code is governed by the Tenebril open source
// license (http://www.tenebril.com/developers/opensource/license.html)
//
// For more information on this and other open source applications,
// visit the Tenebril OpenSource page:
//       http://www.tenebril.com/developers/opensource
//
//////////////////////////////////////////////////////////////////////

// ChildFrm.cpp : implementation of the CChildFrame class
//

#include "stdafx.h"
#include "Oscar.h"

#include "ChildFrm.h"
#include "MainFrm.h"
#include "DirTreeView.h"
#include "DirListView.h"

#include "DirectoryListFrame.h"

#include "AwareNetDivers.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChildFrame

IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
	//{{AFX_MSG_MAP(CChildFrame)
	ON_WM_MDIACTIVATE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChildFrame construction/destruction

CChildFrame::CChildFrame()
{
}

CChildFrame::~CChildFrame()
{
}

BOOL CChildFrame::OnCreateClient( LPCREATESTRUCT lpcs,
	CCreateContext* pContext)
{
	// Get the target of the view.
	COscarApp *pApp = (COscarApp *)AfxGetApp( );
	ASSERT(pApp != NULL);

	CWaitCursor waitCursor;

	int nSysType = pApp->m_nNewSysType;
	m_cszTarget = pApp->m_cszNewTargetMachine;

	// Convert the target into an IP address, if it isn't already.
	if(m_cszTarget.Find(_T('@')) > -1)
	{
		CAwareNet AwareNet;    // Get a handle.
		CString cszIP;

		if(!pApp->m_bANetInit)
		{
			// No AwareNet.  Die.
			return FALSE;
		}

		int nResult = AwareNet_FindIP(&AwareNet, m_cszTarget, cszIP, TRUE);
		if(nResult < 1)
		{
			// Allow user to add non-existent friend.

			if(nResult == -2)      // Not a friend or acquaintance.
			{
				// Get the parent for showing dialogs.
				CMDIFrameWnd *pFrame = GetMDIFrame();
				ASSERT(pFrame);

				// See if the person is registered under AwareNet.
				int nNewFrnd = AwareNet.LookupID(m_cszTarget);
				BOOL bIsInAwareNet = (nNewFrnd >= 0);

				if(bIsInAwareNet)
				{
					// Ask the user if he wants to add the friend.
					CString cszUserAsk;
					cszUserAsk.Format(_T("%s is not in your friends list.\n"
						"Would you like to add him or her?"), m_cszTarget);

					if(pFrame->MessageBox(cszUserAsk, "Add a new friend", MB_YESNO |
						MB_ICONQUESTION) == IDYES)
					{
						// Try to add the friend.
						if(!AwareNet.AddID(nNewFrnd))
						{
							// Did not work.
							return FALSE;
						}

						// Add worked.  Get the friend's IP.
						if(AwareNet_FindIP(&AwareNet, m_cszTarget, cszIP, TRUE) > 0)
						{
							m_cszTarget = cszIP;				
						}
					}
				}
				else
				{
					// Ask the user if he wants to invite the friend.
					CString cszUserAsk;
					cszUserAsk.Format(_T("%s does not have FileFury.\n"
						"Would you like to invite him or her to get it?"), 
						m_cszTarget);

					if(pFrame->MessageBox(cszUserAsk, "Invite a new friend", 
						MB_YESNO | MB_ICONQUESTION) == IDYES)
					{
						// Send the invitation.
						AwareNet.SendInvitation(m_cszTarget, _T("FileFury"),
							_T("http://www.filefury.com"), AWARENET_OSCAR_TOS);

						return FALSE;
					}
				}
			}
		}
		else
			m_cszTarget = cszIP;         // Conversion worked.
	}

	if(m_cszTarget.CompareNoCase(_T("0.0.0.0")) == 0)
		MessageBox(_T("Your friend is offline."), _T("Open Computer"), 
			MB_ICONEXCLAMATION | MB_OK);

	// Setup the splitter.
	if(!m_wndSplitter.CreateStatic( this, 1, 2 ))
	{
		TRACE0("Failed to create splitter\n");
		return FALSE;      // fail to create
	}

	// Get the archive.
	CSettingsArchive *pArchive = pApp->GetArchive();
	ASSERT(pArchive);

	// Setup the panes.
	m_wndSplitter.SetupPanes(nSysType, m_cszTarget, pContext);

	// Set the view and sort types.
	SetViewType(pArchive->m_arch1.nDisplayType);
	SetArrangeType(pArchive->m_arch1.nSortType);

	return TRUE;
}

BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	return CMDIChildWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CChildFrame diagnostics

#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
	CMDIChildWnd::AssertValid();
}

void CChildFrame::Dump(CDumpContext& dc) const
{
	CMDIChildWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChildFrame message handlers

void CChildFrame::SetViewType(int iType)
{
	CDirListView *pListView;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	if(!pListView)
		return;

	pListView->SetViewType(iType);
	return;
}

void CChildFrame::SetArrangeType(int iType)
{
	CDirListView *pListView;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	if(!pListView)
		return;

	pListView->SetArrangeType(iType);
	return;
}

int CChildFrame::SelectedID()
{
	CDirListView *pListView;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	if(!pListView)
		return -1;

	return pListView->SelectedID();
}

void CChildFrame::OnMDIActivate( BOOL bActivate, CWnd* pActivateWnd, 
								 CWnd* pDeactivateWnd )
{
	if(bActivate)
	{
		CMDIFrameWnd *pFrame = GetMDIFrame();
		CMainFrame *pMainFrame = (CMainFrame *)pFrame;

		ASSERT(pMainFrame);
		if(pMainFrame->ChildShouldRefresh())
			m_wndSplitter.Refresh(FALSE);
	}

	CMDIChildWnd::OnMDIActivate(bActivate, pActivateWnd, pDeactivateWnd);
}

BOOL CChildFrame::IsDirectory(int iListItem)
{
	CDirListView *pListView;
	CString cszPath;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	ASSERT(pListView);
	if(!pListView)
		return FALSE;

	cszPath = pListView->GetFilePath(iListItem);
	return m_wndSplitter.m_pFileSystem->IsDirectory(cszPath);
}

void CChildFrame::ExploreItem(int iItem)
{
	CDirListView *pListView;
	CString cszPath;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	ASSERT(pListView);
	if(!pListView)
		return;

	cszPath = pListView->GetFilePath(iItem);
	pListView->ExecuteFile(cszPath);
}


BOOL CChildFrame::ShareItem(int iID, BOOL bShare)
{
	CDirListView *pListView;
	CString cszPath;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	ASSERT(pListView);
	if(!pListView)
		return FALSE;

	// Get the security filter.
	COscarApp *pApp = (COscarApp *)AfxGetApp();
	ASSERT(pApp);
	CSecurityFilter *pSecFilter = pApp->GetArchive()->m_pSecurityFilter;

	// Hold the security filter (not really necessary; the app isn't going
	// anywhere).
	pSecFilter->Reference();

	// Update the security filter.
	cszPath = pListView->GetFilePath(iID);
	if(bShare)
	{
		pSecFilter->AddDirectory(cszPath);
		if(pApp->m_pDirectoryList)
			pApp->m_pDirectoryList->AddDirectory(cszPath);
	}
	else
	{
		pSecFilter->RemoveDirectory(cszPath);
		if(pApp->m_pDirectoryList)
			pApp->m_pDirectoryList->RemoveDirectory(cszPath);
	}

	// Let go the security filter.
	pSecFilter->Dereference();

	// The icon in the tree needs to be updated before the refresh.
	CDirTreeView *pTreeView = (CDirTreeView *)m_wndSplitter.GetPane(0, 0);
	ASSERT(pTreeView);
	if(!pTreeView)
		return TRUE;

	// The icon to be changed must be a child of the currently-selected item.
	HTREEITEM hItem = pTreeView->SearchSiblingItem(pTreeView->SelectedItem(),
		cszPath);
	if(hItem)
	{
		if(bShare)
			pTreeView->SetIcon(hItem, pApp->m_nShareFolderIcon[SFI_CLOSED],
				pApp->m_nShareFolderIcon[SFI_OPEN]);
		else
			pTreeView->SetIcon(hItem, pApp->m_nNormalFolderIcon[SFI_CLOSED],
				pApp->m_nNormalFolderIcon[SFI_OPEN]);
	}
	
	return TRUE;
}

BOOL CChildFrame::ShareItem(HTREEITEM hItem, BOOL bShare)
{
	CDirTreeView *pTreeView;
	CString cszPath;

	pTreeView = (CDirTreeView *)m_wndSplitter.GetPane(0, 0);
	ASSERT(pTreeView);
	if(!pTreeView)
		return FALSE;

	// Get the security filter.
	COscarApp *pApp = (COscarApp *)AfxGetApp();
	ASSERT(pApp);
	CSecurityFilter *pSecFilter = pApp->GetArchive()->m_pSecurityFilter;

	// Hold the security filter (not really necessary; the app isn't going
	// anywhere).
	pSecFilter->Reference();

	// Update the security filter.
	cszPath = pTreeView->GetFullPath(hItem);
	
	if(bShare)
	{
		pSecFilter->AddDirectory(cszPath);
		if(pApp->m_pDirectoryList)
			pApp->m_pDirectoryList->AddDirectory(cszPath);
	}
	else
	{
		pSecFilter->RemoveDirectory(cszPath);
		if(pApp->m_pDirectoryList)
			pApp->m_pDirectoryList->RemoveDirectory(cszPath);
	}

	// Let go the security filter.
	pSecFilter->Dereference();

	// Change the icon appropriately.
	if(bShare)
	{
		pTreeView->SetIcon(hItem, pApp->m_nShareFolderIcon[SFI_CLOSED],
			pApp->m_nShareFolderIcon[SFI_OPEN]);
	}
	else
	{
		pTreeView->SetIcon(hItem, pApp->m_nNormalFolderIcon[SFI_CLOSED],
			pApp->m_nNormalFolderIcon[SFI_OPEN]);
	}

	return TRUE;
}

HTREEITEM CChildFrame::SelectedDirItem()
{
	CDirTreeView *pTreeView;

	pTreeView = (CDirTreeView *)m_wndSplitter.GetPane(0, 0);
	if(!pTreeView)
		return (HTREEITEM)NULL;

	return pTreeView->SelectedItem();
}

void CChildFrame::Refresh()
{
	m_wndSplitter.Refresh();
}

CString CChildFrame::GetFullPath(HTREEITEM hItem)
{
	// Get the tree.
	CDirTreeView *pTreeView = (CDirTreeView *)m_wndSplitter.GetPane(0, 0);
	ASSERT(pTreeView);
	if(!pTreeView)
		return _T("");

	// Return the path.
	return pTreeView->GetFullPath(hItem);
}

CString CChildFrame::GetFullPath(int iItem)
{
	// Get the list.
	CDirListView *pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	ASSERT(pListView);
	if(!pListView)
		return _T("");

	// Return the path.
	return pListView->GetFilePath(iItem);
}

BOOL CChildFrame::SetDirectory(LPCTSTR czDir)
{
	m_wndSplitter.SetDirectory(czDir);
	return TRUE;
}

CFileSystem *CChildFrame::GetFileSystem()
{
	return m_wndSplitter.m_pFileSystem;
}

CString CChildFrame::GetMachineName()
{
	// Check to see if we're local.
	CFileSystem *pFileSystem = GetFileSystem();
	ASSERT(pFileSystem);
	if(pFileSystem->GetFSType() == 0)
		return _T("127.0.0.1");

	// We're remote; the m_cszTarget member will be valid.
	return m_cszTarget;
}

BOOL CChildFrame::DeleteSelectedFile()
{
	int iSel = SelectedID();

	if(iSel < 0) return FALSE;

	CString cszFileName = GetFullPath(iSel);
	
	ASSERT(m_wndSplitter.m_pFileSystem);
	BOOL bIsDirectory = m_wndSplitter.m_pFileSystem->IsDirectory(cszFileName);
	BOOL bResult = m_wndSplitter.m_pFileSystem->DeleteFile(cszFileName);
	
	if(bResult)
	{
		// Update the UI.

		m_wndSplitter.Refresh();   // TODO: Make this faster.
		if(bIsDirectory)
		{
			// Update the tree view.
			CDirTreeView *pTreeView = (CDirTreeView *)m_wndSplitter.GetPane(0, 0);
			ASSERT(pTreeView);
			if(!pTreeView)
				return bResult;

			pTreeView->RemoveDirectory(cszFileName);
		}
	}

	return bResult;
}

BOOL CChildFrame::CreateNewFolder(LPCTSTR czDirName)
{
	CString cszDirName;
	CString cszCurDirectory;

	// Get the current directory.
	cszCurDirectory = m_wndSplitter.m_strDirectory;
	if(cszCurDirectory.GetLength() < 1) return FALSE;
	if(cszCurDirectory.GetAt(cszCurDirectory.GetLength() - 1) != _T('\\'))
		cszCurDirectory += _T('\\');

	// Get the filesystem.
	CFileSystem *pFileSystem = m_wndSplitter.m_pFileSystem;
	ASSERT(pFileSystem);

	if(!czDirName)
	{
		// Create a generic name.

		CString cszGenericName = _T("New Folder");
		int nNumNew = 1;
		CString cszTestName = cszCurDirectory + cszGenericName;

		while(pFileSystem->FindFile(cszTestName))
		{
			nNumNew += 1;
			cszGenericName.Format(_T("New Folder (%i)"), nNumNew);
			cszTestName = cszCurDirectory + cszGenericName;
		}

		cszDirName = cszGenericName;
	}
	else
		cszDirName = czDirName;

	// Now create the directory.
	CString cszFullName = cszCurDirectory + cszDirName;
	if(!pFileSystem->CreateDirectory(cszFullName))
		return FALSE;

	// Update the list control.
	CDirListView *pDirList = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	ASSERT(pDirList);

	pDirList->AddItem(cszFullName);

	// Update the tree control.
	CDirTreeView *pDirTree = (CDirTreeView *)m_wndSplitter.GetPane(0, 0);
	ASSERT(pDirTree);
	HTREEITEM hSelItem = pDirTree->SelectedItem();
	ASSERT(hSelItem);
	pDirTree->AddItem(hSelItem, cszFullName);
	// Force a redraw to add a plus button if necessary.
	pDirTree->InvalidateRect(NULL);

	// Select and open an edit.
	pDirList->SelectEdit(cszFullName);

	return TRUE;
}

void CChildFrame::OnDestroy() 
{
	// Get the splitter position before we close.
	int nWidth, nMinWidth;
	m_wndSplitter.GetColumnInfo( 0, nWidth, nMinWidth );

	// Also get the view and sort types.
	int nSort = GetArrangeType();
	int nDisplay = GetViewType();

	CMDIChildWnd::OnDestroy();
	
	// Store the splitter position.
	COscarApp *pApp = (COscarApp *)AfxGetApp();
	ASSERT(pApp);
	CSettingsArchive *pArchive = pApp->GetArchive();
	ASSERT(pArchive);
	if(nWidth > 0)
		pArchive->m_arch1.nExplorerWidth = nWidth;

	// Store the view and sort types.
	pArchive->m_arch1.nDisplayType = nDisplay;
	pArchive->m_arch1.nSortType    = nSort;
}

CString CChildFrame::GetDirectory()
{
	return m_wndSplitter.m_strDirectory;
}

int CChildFrame::GetViewType()
{
	CDirListView *pListView;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	if(!pListView)
		return -1;

	return pListView->GetViewType();
}

int CChildFrame::GetArrangeType()
{
	CDirListView *pListView;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	if(!pListView)
		return -1;

	return pListView->GetArrangeType();
}

void CChildFrame::BrushRefresh()
{
	CDirListView *pListView;

	pListView = (CDirListView *)m_wndSplitter.GetPane(0, 1);
	if(!pListView)
		return;

	pListView->BrushRefresh();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -