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

📄 ctrldownloads.cpp

📁 p2p软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:

BOOL CDownloadsCtrl::GetAt(int nSelect, CDownload** ppDownload, CDownloadSource** ppSource)
{
	int nScroll = GetScrollPos( SB_VERT );
	int nIndex = 0;
	
	if ( ppDownload != NULL ) *ppDownload = NULL;
	if ( ppSource != NULL ) *ppSource = NULL;
	
	for ( POSITION posDownload = Downloads.GetIterator() ; posDownload ; )
	{
		CDownload* pDownload = Downloads.GetNext( posDownload );
		
		if ( m_nGroupCookie != 0 && m_nGroupCookie != pDownload->m_nGroupCookie ) continue;
		if ( IsFiltered( pDownload ) ) continue;
		
		if ( nIndex++ == nSelect )
		{
			if ( ppDownload != NULL ) *ppDownload = pDownload;
			return TRUE;
		}
		
		if ( ! pDownload->m_bExpanded ) continue;
		
		for ( CDownloadSource* pSource = pDownload->GetFirstSource() ; pSource ; pSource = pSource->m_pNext )
		{
			if ( Settings.Downloads.ShowSources || ( pSource->m_pTransfer != NULL && pSource->m_pTransfer->m_nState > dtsConnecting ) )
			{
				if ( nIndex++ == nSelect )
				{
					if ( ppSource != NULL ) *ppSource = pSource;
					return TRUE;
				}
			}
		}
	}
	
	return FALSE;
}

BOOL CDownloadsCtrl::GetRect(CDownload* pSelect, RECT* prcItem)
{
	CRect rcClient, rcItem;
	
	GetClientRect( &rcClient );
	rcClient.top += HEADER_HEIGHT;
	
	rcItem.CopyRect( &rcClient );
	rcItem.left -= GetScrollPos( SB_HORZ );
	rcItem.bottom = rcItem.top + ITEM_HEIGHT;
	
	int nScroll = GetScrollPos( SB_VERT );
	rcItem.OffsetRect( 0, ITEM_HEIGHT * -nScroll );
	
	for ( POSITION posDownload = Downloads.GetIterator() ; posDownload ; )
	{
		CDownload* pDownload = Downloads.GetNext( posDownload );
		
		if ( m_nGroupCookie != 0 && m_nGroupCookie != pDownload->m_nGroupCookie ) continue;
		if ( IsFiltered( pDownload ) ) continue;
		
		if ( pDownload == pSelect )
		{
			*prcItem = rcItem;
			return TRUE;
		}
		
		rcItem.OffsetRect( 0, ITEM_HEIGHT );
		
		if ( ! pDownload->m_bExpanded ) continue;
		
		if ( Settings.Downloads.ShowSources )
		{
			int nSources = pDownload->GetSourceCount();
			rcItem.OffsetRect( 0, ITEM_HEIGHT * nSources );
			continue;
		}
		
		for ( CDownloadSource* pSource = pDownload->GetFirstSource() ; pSource ; pSource = pSource->m_pNext )
		{
			if ( pSource->m_pTransfer != NULL && pSource->m_pTransfer->m_nState > dtsConnecting )
			{
				rcItem.OffsetRect( 0, ITEM_HEIGHT );
			}
		}
	}
	
	return FALSE;
}

void CDownloadsCtrl::MoveSelected(int nDelta)
{
	CSingleLock pLock( &Transfers.m_pSection, TRUE );
	CList<CDownload*> pList;
	POSITION pos;
	
	for ( pos = Downloads.GetIterator() ; pos ; )
	{
		CDownload* pDownload = Downloads.GetNext( pos );
		if ( pDownload->m_bSelected ) pList.AddTail( pDownload );
	}
	
	pos = nDelta > 0 ? pList.GetTailPosition() : pList.GetHeadPosition();
	
	while ( pos )
	{
		CDownload* pDownload = nDelta > 0 ? pList.GetPrev( pos ) : pList.GetNext( pos );
		Downloads.Move( pDownload, nDelta );
	}
	
	Update();
}

BOOL CDownloadsCtrl::DropShowTarget(CPtrList* pSel, const CPoint& ptScreen)
{
	CPoint ptLocal( ptScreen );
	CRect rcClient;
	
	ScreenToClient( &ptLocal );
	GetClientRect( &rcClient );
	
	BOOL bLocal = rcClient.PtInRect( ptLocal );
	CDownload* pHit = NULL;
	
	if ( bLocal ) HitTest( ptLocal, &pHit, NULL, NULL, NULL );
	
	if ( pHit != m_pDragDrop )
	{
		CImageList::DragShowNolock( FALSE );
		m_pDragDrop = pHit;
		RedrawWindow();
		CImageList::DragShowNolock( TRUE );
	}
	
	return bLocal;
}

BOOL CDownloadsCtrl::DropObjects(CPtrList* pSel, const CPoint& ptScreen)
{
	CPoint ptLocal( ptScreen );
	CRect rcClient;
	
	ScreenToClient( &ptLocal );
	GetClientRect( &rcClient );
	
	m_pDragDrop = NULL;
	
	if ( pSel == NULL || ! rcClient.PtInRect( ptLocal ) ) return FALSE;
	
	CSingleLock pLock( &Transfers.m_pSection, TRUE );
	CDownload* pHit = NULL;
	
	HitTest( ptLocal, &pHit, NULL, NULL, NULL );
	
	for ( POSITION pos = pSel->GetHeadPosition() ; pos ; )
	{
		CDownload* pDownload = (CDownload*)pSel->GetNext( pos );
		
		if ( Downloads.Check( pDownload ) && pDownload != pHit )
		{
			Downloads.Reorder( pDownload, pHit );
		}
	}
	
	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
// CDownloadsCtrl presentation message handlers

void CDownloadsCtrl::OnSize(UINT nType, int cx, int cy)
{
	int nWidth = 0, nHeight = 0;
	CRect rcClient;
	HDITEM pColumn;
	
	if ( nType != 1982 ) CWnd::OnSize( nType, cx, cy );
	
	GetClientRect( &rcClient );
	
	ZeroMemory( &pColumn, sizeof(pColumn) );
	pColumn.mask = HDI_WIDTH;
	
	for ( int nColumn = 0 ; m_wndHeader.GetItem( nColumn, &pColumn ) ; nColumn ++ )
		nWidth += pColumn.cxy;
	
	SCROLLINFO pScroll;
	ZeroMemory( &pScroll, sizeof(pScroll) );
	pScroll.cbSize	= sizeof(pScroll);
	pScroll.fMask	= SIF_RANGE|SIF_PAGE;
	pScroll.nMin	= 0;
	pScroll.nMax	= nWidth;
	pScroll.nPage	= rcClient.right;
	SetScrollInfo( SB_HORZ, &pScroll, TRUE );
	
	int nScroll = GetScrollPos( SB_HORZ );
	m_wndHeader.SetWindowPos( NULL, -nScroll, 0, rcClient.right + nScroll, HEADER_HEIGHT, SWP_SHOWWINDOW );
	
	CSingleLock pLock( &Transfers.m_pSection, TRUE );
	
	for ( POSITION posDownload = Downloads.GetIterator() ; posDownload ; )
	{
		CDownload* pDownload = Downloads.GetNext( posDownload );
		
		if ( m_nGroupCookie != 0 && m_nGroupCookie != pDownload->m_nGroupCookie || IsFiltered( pDownload ) )
		{
			pDownload->m_bSelected = FALSE;
			for ( CDownloadSource* pSource = pDownload->GetFirstSource() ; pSource ; pSource = pSource->m_pNext )
				pSource->m_bSelected = FALSE;
			continue;
		}
		
		nHeight ++;
		
		if ( ! pDownload->m_bExpanded )
		{
		}
		else if ( Settings.Downloads.ShowSources )
		{
			nHeight += pDownload->GetSourceCount();
		}
		else
		{
			for ( CDownloadSource* pSource = pDownload->GetFirstSource() ; pSource ; pSource = pSource->m_pNext )
			{
				if ( Settings.Downloads.ShowSources || ( pSource->m_pTransfer != NULL && pSource->m_pTransfer->m_nState > dtsConnecting ) )
				{
					nHeight ++;
				}
			}
		}
	}
	
	pLock.Unlock();
	
	ZeroMemory( &pScroll, sizeof(pScroll) );
	pScroll.cbSize	= sizeof(pScroll);
	pScroll.fMask	= SIF_RANGE|SIF_PAGE;
	pScroll.nMin	= 0;
	pScroll.nMax	= nHeight;
	pScroll.nPage	= ( rcClient.bottom - HEADER_HEIGHT ) / ITEM_HEIGHT + 1;
	SetScrollInfo( SB_VERT, &pScroll, TRUE );
	
	m_nFocus = min( m_nFocus, max( 0, nHeight - 1 ) );
	
	Invalidate();
}

//////////////////////////////////////////////////////////////////////////////
// CDownloadsCtrl painting

void CDownloadsCtrl::OnPaint()
{
	CSingleLock pLock( &Transfers.m_pSection, TRUE );
	CRect rcClient, rcItem;
	CPaintDC dc( this );
	
	GetClientRect( &rcClient );
	rcClient.top += HEADER_HEIGHT;
	
	rcItem.CopyRect( &rcClient );
	rcItem.left -= GetScrollPos( SB_HORZ );
	rcItem.bottom = rcItem.top + ITEM_HEIGHT;
	
	int nScroll = GetScrollPos( SB_VERT );
	int nIndex = 0;
	
	CFont* pfOld	= (CFont*)dc.SelectObject( &theApp.m_gdiFont );
	BOOL bFocus		= ( GetFocus() == this );
	
	for ( POSITION posDownload = Downloads.GetIterator() ; posDownload && rcItem.top < rcClient.bottom ; )
	{
		CDownload* pDownload = Downloads.GetNext( posDownload );
		
		if ( m_nGroupCookie != 0 && m_nGroupCookie != pDownload->m_nGroupCookie ) continue;
		if ( IsFiltered( pDownload ) ) continue;
		
		if ( nScroll > 0 )
		{
			nScroll --;
		}
		else
		{
			PaintDownload( dc, rcItem, pDownload, bFocus && ( m_nFocus == nIndex ), m_pDragDrop == pDownload );
			rcItem.OffsetRect( 0, ITEM_HEIGHT );
		}
		
		nIndex ++;
		
		if ( ! pDownload->m_bExpanded ) continue;
		
		if ( Settings.Downloads.ShowSources )
		{
			int nSources = pDownload->GetSourceCount();
			
			if ( nScroll >= nSources )
			{
				nScroll -= nSources;
				nIndex += nSources;
				continue;
			}
		}
		
		for ( CDownloadSource* pSource = pDownload->GetFirstSource() ; pSource && rcItem.top < rcClient.bottom ; pSource = pSource->m_pNext )
		{
			if ( Settings.Downloads.ShowSources || ( pSource->m_pTransfer != NULL && pSource->m_pTransfer->m_nState > dtsConnecting ) )
			{
				if ( nScroll > 0 )
				{
					nScroll --;
				}
				else
				{
					PaintSource( dc, rcItem, pDownload, pSource, bFocus && ( m_nFocus == nIndex ) );
					rcItem.OffsetRect( 0, ITEM_HEIGHT );
				}
				
				nIndex ++;
			}
		}
	}
	
	dc.SelectObject( pfOld );
	
	rcClient.top = rcItem.top;
	if ( rcClient.top < rcClient.bottom )
		dc.FillSolidRect( &rcClient, CoolInterface.m_crWindow );
}

void CDownloadsCtrl::PaintDownload(CDC& dc, const CRect& rcRow, CDownload* pDownload, BOOL bFocus, BOOL bDrop)
{
	COLORREF crNatural	= m_bCreateDragImage ? RGB( 250, 255, 250 ) : CoolInterface.m_crWindow;
	COLORREF crBack		= pDownload->m_bSelected ? CoolInterface.m_crBackSel : crNatural;
	
	if ( bDrop )
	{
		CRect rcDrop( rcRow.left, rcRow.top, rcRow.right, rcRow.top + 2 );
		dc.Draw3dRect( &rcDrop, 0, 0 );
		dc.ExcludeClipRect( &rcDrop );
	}
	
	dc.SetBkColor( crBack );
	dc.SetBkMode( OPAQUE );
	
	if ( pDownload->m_bVerify == TS_FALSE )
		dc.SetTextColor( RGB( 255, 0, 0 ) );
	else if ( pDownload->m_bSelected )
		dc.SetTextColor( CoolInterface.m_crText );
	else if ( pDownload->m_bVerify == TS_TRUE )
		dc.SetTextColor( RGB( 0, 127, 0 ) );
	else
		dc.SetTextColor( CoolInterface.m_crText );
	
	int nTextLeft = rcRow.right, nTextRight = rcRow.left;
	HDITEM pColumn;
	
	ZeroMemory( &pColumn, sizeof(pColumn) );
	pColumn.mask = HDI_FORMAT | HDI_LPARAM;
	
	int nTransfers	= pDownload->GetTransferCount();
	int nSources	= pDownload->GetSourceCount();
	
	for ( int nColumn = 0 ; m_wndHeader.GetItem( nColumn, &pColumn ) ; nColumn++ )
	{
		CString strText;
		CRect rcCell;
		
		m_wndHeader.GetItemRect( nColumn, &rcCell );
		rcCell.left		+= rcRow.left;
		rcCell.right	+= rcRow.left;
		rcCell.top		= rcRow.top;
		rcCell.bottom	= rcRow.bottom;
		
		switch ( pColumn.lParam )
		{
		case DOWNLOAD_COLUMN_TITLE:
			dc.FillSolidRect( rcCell.left, rcCell.bottom - 1, 32, 1, crNatural );
			if ( IsExpandable( pDownload ) )
			{
				ImageList_DrawEx( ShellIcons.GetHandle( 16 ), pDownload->m_bExpanded ? SHI_MINUS : SHI_PLUS, dc.GetSafeHdc(),
						rcCell.left, rcCell.top, 16, 16, crNatural, CLR_DEFAULT, ILD_NORMAL );
			}
			else
				dc.FillSolidRect( rcCell.left, rcCell.top, 16, 16, crNatural );
			rcCell.left += 16;
			ImageList_DrawEx( ShellIcons.GetHandle( 16 ), ShellIcons.Get( pDownload->m_sRemoteName, 16 ), dc.GetSafeHdc(),
					rcCell.left, rcCell.top, 16, 16, crNatural, CLR_DEFAULT, pDownload->m_bSelected ? ILD_SELECTED : ILD_NORMAL );
			rcCell.left += 16;
			dc.FillSolidRect( rcCell.left, rcCell.top, 1, rcCell.Height(), crNatural );
			rcCell.left += 1;
			
			strText = pDownload->GetDisplayName();
			break;
			
		case DOWNLOAD_COLUMN_SIZE:
			if ( pDownload->m_nSize < SIZE_UNKNOWN )
				strText = Settings.SmartVolume( pDownload->m_nSize, FALSE );
			else
				strText = _T("Unknown");
			break;
			
		case DOWNLOAD_COLUMN_PROGRESS:
			dc.Draw3dRect( &rcCell, crBack, crBack );
			rcCell.DeflateRect( 1, 1 );
			dc.Draw3dRect( &rcCell, crBack, crBack );
			rcCell.DeflateRect( 0, 1 );
			dc.Draw3dRect( &rcCell, RGB( 50, 50, 50 ), RGB( 50, 50, 50 ) );
			rcCell.DeflateRect( 1, 1 );
			CFragmentBar::DrawDownload( &dc, &rcCell, pDownload, crNatural );
			break;
			
		case DOWNLOAD_COLUMN_SPEED:
			if ( ! pDownload->IsMoving() )
			{
				if ( DWORD nSpeed = pDownload->GetAverageSpeed() * 8 )
					strText = Settings.SmartVolume( nSpeed, FALSE, TRUE );
			}
			break;
			
		case DOWNLOAD_COLUMN_STATUS:
			if ( pDownload->IsCompleted() )
				strText = pDownload->IsSeeding() ? _T("Seeding") : _T("Completed");
			else if ( pDownload->IsMoving() )
				strText = _T("Moving");
			else if ( pDownload->IsPaused() )
			{
				if ( pDownload->m_bDiskFull )
					strText = ( pDownload->IsCompleted() ) ? _T("Can't Move") : _T("File Error");
				else
					strText = _T("Paused");
			}
			else if ( pDownload->GetProgress() == 1.0f && pDownload->IsStarted() )
				strText = _T("Verifying");
			else if ( pDownload->IsDownloading() )
			{
				DWORD nTime = pDownload->GetTimeRemaining();
				
				if ( nTime == 0xFFFFFFFF )
					strText = _T("Active");
				else
					strText.Format( _T("%i:%.2i:%.2i"), nTime / 3600, ( nTime % 3600 ) / 60, nTime % 60 );
			}
			else if ( nSources > 0 )
				strText = _T("Pending");
			else if ( pDownload->m_nSize == SIZE_UNKNOWN )
				strText = _T("Searching");
			else if ( pDownload->m_bBTH )
			{
				if ( pDownload->IsTasking() )
					strText = _T("Creating");
				else if ( pDownload->m_bTorrentTrackerError )
					strText = _T("Tracker Down");
				else
					strText = _T("Torrent");
			}
			else
				strText = _T("No Sources");
			break;
			
		case DOWNLOAD_COLUMN_CLIENT:
			if ( pDownload->IsCompleted() )
			{
				if ( pDownload->m_bVerify == TS_TRUE )
					strText = _T("Verified");
				else if ( pDownload->m_bVerify == TS_FALSE )
					strText = _T("Unverified");
			}
			else if ( nSources == 1 )
				strText = _T("(1 source)");
			else if ( nSources > 1 )
				strText.Format( _T("(%i sources)"), nSources );
			else
				strText = _T("(No sources)");
			break;
		}
		
		nTextLeft	= min( nTextLeft, rcCell.left );
		nTextRight	= max( nTextRight, rcCell.right );
		
		if ( pColumn.lParam == DOWNLOAD_COLUMN_PROGRESS ) continue;
		
		if ( rcCell.Width() < 8 ) strText.Empty();
		
		if ( dc.GetTextExtent( strText ).cx > rcCell.Width() - 8 )
		{
			while ( dc.GetTextExtent( strText + _T("

⌨️ 快捷键说明

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