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

📄 ctrllibrarydetailview.cpp

📁 著名的下载软件核心Shareaza
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

void CLibraryDetailView::OnGetDispInfoA(NMLVDISPINFO* pNotify, LRESULT* pResult)
{
	*pResult = 0;
	
	LDVITEM* pItem = &m_pList[ pNotify->item.iItem ];
	
	if ( pNotify->item.mask & LVIF_STATE )
	{
		pNotify->item.state &= ~LVIS_SELECTED;
		if ( pItem->nState & LDVI_SELECTED ) pNotify->item.state |= LVIS_SELECTED;
	}
	
	if ( pItem->nCookie != m_nListCookie )
	{
		if ( ! Library.Lock( 100 ) ) return;
		CacheItem( pNotify->item.iItem );
		Library.Unlock();
		if ( pItem->nCookie != m_nListCookie ) return;
	}
	
	if ( pNotify->item.mask & LVIF_TEXT )
	{
		if ( pNotify->item.iSubItem < pItem->pText->GetSize() )
		{
#ifdef _UNICODE
			WideCharToMultiByte( CP_ACP, 0, pItem->pText->GetAt( pNotify->item.iSubItem ), -1, (LPSTR)pNotify->item.pszText, pNotify->item.cchTextMax, NULL, NULL );
#else
			strncpy( (LPSTR)pNotify->item.pszText, pItem->pText->GetAt( pNotify->item.iSubItem ), pNotify->item.cchTextMax );
#endif
		}
	}
	
	if ( pNotify->item.mask & LVIF_IMAGE )
	{
		pNotify->item.iImage = pItem->nIcon;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CLibraryDetailView sorting

CLibraryDetailView* CLibraryDetailView::m_pThis;

void CLibraryDetailView::SortItems(int nColumn)
{
	GET_LIST();
	
	CLiveList::Sort( pList, nColumn );
	nColumn = ( m_nStyle == LVS_REPORT ) ? GetWindowLong( GetSafeHwnd(), GWL_USERDATA ) : -1;

	if ( nColumn != 0 ) 
	{
		m_nSortColumn	= abs( nColumn ) - 1;
		m_bSortFlip		= nColumn < 0;
		m_pThis			= this;
		qsort( m_pList, m_nList, sizeof(m_pList[0]), ListCompare );
	}

	pList->SetItemCount( m_nList );
}

int CLibraryDetailView::ListCompare(LPCVOID pA, LPCVOID pB)
{
	LDVITEM* ppA	= (LDVITEM*)pA;
	LDVITEM* ppB	= (LDVITEM*)pB;
	int nTest		= 0;

	CLibraryFile* pfA = Library.LookupFile( ppA->nIndex );
	CLibraryFile* pfB = Library.LookupFile( ppB->nIndex );

	if ( ! pfA || ! pfB ) return 0;

	switch ( m_pThis->m_nSortColumn )
	{
	case 0:
		nTest = _tcsicoll( pfA->m_sName, pfB->m_sName );
		break;
	case 1:
		{
			LPCTSTR pszA = _tcsrchr( pfA->m_sName, '.' );
			LPCTSTR pszB = _tcsrchr( pfB->m_sName, '.' );
			if ( ! pszA || ! pszB ) return 0;
			nTest = _tcsicoll( pszA, pszB );
			break;
		}
	case 2:
		if ( pfA->GetSize() == pfB->GetSize() )
			nTest = 0;
		else if ( pfA->GetSize() < pfB->GetSize() )
			nTest = -1;
		else
			nTest = 1;
		break;
	case 3:
		if ( pfA->m_pFolder == NULL || pfB->m_pFolder == NULL ) return 0;
		nTest = _tcsicoll( pfA->m_pFolder->m_sPath, pfB->m_pFolder->m_sPath );
		break;
	case 4:
		if ( pfA->m_nHitsTotal == pfB->m_nHitsTotal )
			nTest = 0;
		else if ( pfA->m_nHitsTotal < pfB->m_nHitsTotal )
			nTest = -1;
		else
			nTest = 1;
		break;
	case 5:
		if ( pfA->m_nUploadsTotal == pfB->m_nUploadsTotal )
			nTest = 0;
		else if ( pfA->m_nUploadsTotal < pfB->m_nUploadsTotal )
			nTest = -1;
		else
			nTest = 1;
		break;
	case 6:
		nTest = CompareFileTime( &pfA->m_pTime, &pfB->m_pTime );
		break;
	default:
		{
			int nColumn = m_pThis->m_nSortColumn - DETAIL_COLUMNS;
			if ( nColumn >= m_pThis->m_pColumns.GetCount() ) return 0;
			POSITION pos = m_pThis->m_pColumns.FindIndex( nColumn );
			if ( pos == NULL ) return 0;
			CSchemaMember* pMember = (CSchemaMember*)m_pThis->m_pColumns.GetAt( pos );

			CString strA, strB;
			if ( pfA->m_pMetadata ) strA = pMember->GetValueFrom( pfA->m_pMetadata, NULL, TRUE );
			if ( pfB->m_pMetadata ) strB = pMember->GetValueFrom( pfB->m_pMetadata, NULL, TRUE );

			if ( *(LPCTSTR)strA && *(LPCTSTR)strB &&
				( ((LPCTSTR)strA)[ _tcslen( strA ) - 1 ] == 'k' || ((LPCTSTR)strA)[ _tcslen( strA ) - 1 ] == '~' )
				&&
				( ((LPCTSTR)strB)[ _tcslen( strB ) - 1 ] == 'k' || ((LPCTSTR)strB)[ _tcslen( strB ) - 1 ] == '~' ) )
			{
				nTest = CLiveList::SortProc( strA, strB, TRUE );
			}
			else
			{
				nTest = _tcsicoll( strA, strB );
			}
		}
	}

	if ( ! m_pThis->m_bSortFlip ) nTest = -nTest;

	return nTest;
}

/////////////////////////////////////////////////////////////////////////////
// CLibraryDetailView selection operations

void CLibraryDetailView::CacheSelection()
{
	GET_LIST();
	SelClear( FALSE );
	
	for ( int nItem = -1 ; ( nItem = pList->GetNextItem( nItem, LVNI_SELECTED ) ) >= 0 ; )
	{
		SelAdd( pList->GetItemData( nItem ), FALSE );
	}
}

DWORD CLibraryDetailView::HitTestIndex(const CPoint& point) const
{
	GET_LIST();
	int nItem = pList->HitTest( point );
	if ( nItem < 0 ) return 0;
	return m_pList[ nItem ].nIndex;
}

void CLibraryDetailView::OnItemChanged(NM_LISTVIEW* pNotify, LRESULT* pResult)
{
	*pResult = 0;

	if ( pNotify->iItem >= 0 )
	{
		if ( ( pNotify->uOldState & LVIS_SELECTED ) != ( pNotify->uNewState & LVIS_SELECTED ) )
		{
			if ( pNotify->uNewState & LVIS_SELECTED )
			{
				SelAdd( m_pList[ pNotify->iItem ].nIndex );
				m_pList[ pNotify->iItem ].nState |= LDVI_SELECTED;
			}
			else
			{
				SelRemove( m_pList[ pNotify->iItem ].nIndex );
				m_pList[ pNotify->iItem ].nState &= ~LDVI_SELECTED;
			}
		}
	}
	else
	{
		SelClear();

		LDVITEM* pItem = m_pList;
		for ( DWORD nCount = m_nList ; nCount ; nCount--, pItem++ ) pItem->nState &= ~LDVI_SELECTED;
	}
}

void CLibraryDetailView::OnItemRangeChanged(NMLVODSTATECHANGE* pNotify, LRESULT* pResult)
{
	*pResult = 0;

	for ( int nItem = pNotify->iFrom ; nItem <= pNotify->iTo ; nItem++ )
	{
		if ( pNotify->uNewState & LVIS_SELECTED )
		{
			SelAdd( m_pList[ nItem ].nIndex );
			m_pList[ nItem ].nState |= LDVI_SELECTED;
		}
		else
		{
			SelRemove( m_pList[ nItem ].nIndex );
			m_pList[ nItem ].nState &= ~LDVI_SELECTED;
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CLibraryDetailView list coupling

void CLibraryDetailView::OnColumnClick(NM_LISTVIEW* pNotify, LRESULT* pResult)
{
	*pResult = 0;
	SortItems( pNotify->iSubItem );
}

void CLibraryDetailView::OnBeginLabelEdit(LV_DISPINFO* pNotify, LRESULT* pResult)
{
	m_bEditing = TRUE;
	*pResult = 0;
}

void CLibraryDetailView::OnEndLabelEditW(LV_DISPINFO* pNotify, LRESULT* pResult)
{
	*pResult = 0;
	
	if ( pNotify->item.pszText && *pNotify->item.pszText )
	{
		if ( CLibraryFile* pFile = Library.LookupFile( m_pList[ pNotify->item.iItem ].nIndex, TRUE ) )
		{
			m_pList[ pNotify->item.iItem ].nState &= ~LDVI_SELECTED;
			CString strName = (LPCWSTR)pNotify->item.pszText;
			LPCTSTR pszType = _tcsrchr( pFile->m_sName, '.' );
			if ( pszType ) strName += pszType;
			*pResult = pFile->Rename( strName );
			Library.Unlock( TRUE );
			
			if ( *pResult == FALSE )
			{
				CString strFormat, strMessage, strError = theApp.GetErrorString();
				LoadString( strFormat, IDS_LIBRARY_RENAME_FAIL );
				strMessage.Format( strFormat, (LPCTSTR)pFile->m_sName, (LPCTSTR)strName );
				strMessage += _T("\r\n\r\n") + strError;
				AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
			}
		}
	}
	
	m_bEditing = FALSE;
}

void CLibraryDetailView::OnEndLabelEditA(LV_DISPINFO* pNotify, LRESULT* pResult)
{
	*pResult = 0;
	
	if ( pNotify->item.pszText && *pNotify->item.pszText )
	{
		if ( CLibraryFile* pFile = Library.LookupFile( m_pList[ pNotify->item.iItem ].nIndex, TRUE ) )
		{
			m_pList[ pNotify->item.iItem ].nState &= ~LDVI_SELECTED;
			CString strName = (LPCSTR)pNotify->item.pszText;
			LPCTSTR pszType = _tcsrchr( pFile->m_sName, '.' );
			if ( pszType ) strName += pszType;
			*pResult = pFile->Rename( strName );
			Library.Unlock( TRUE );
			
			if ( *pResult == FALSE )
			{
				CString strFormat, strMessage, strError = theApp.GetErrorString();
				LoadString( strFormat, IDS_LIBRARY_RENAME_FAIL );
				strMessage.Format( strFormat, (LPCTSTR)pFile->m_sName, (LPCTSTR)strName );
				strMessage += _T("\r\n\r\n") + strError;
				AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
			}
		}
	}
	
	m_bEditing = FALSE;
}

void CLibraryDetailView::OnFindItemW(NMLVFINDITEM* pNotify, LRESULT* pResult)
{
	USES_CONVERSION;
	LPCTSTR pszFind = W2CT( (LPCWSTR)pNotify->lvfi.psz );
	
	GET_LIST();
	Library.Lock();

	for ( int nLoop = 0 ; nLoop < 2 ; nLoop++ )
	{
		for ( int nItem = pNotify->iStart ; nItem < pList->GetItemCount() ; nItem++ )
		{
			if ( CLibraryFile* pFile = Library.LookupFile( m_pList[ nItem ].nIndex ) )
			{
				if ( pNotify->lvfi.flags & LVFI_STRING )
				{
					if ( _tcsnicmp( pszFind, pFile->m_sName, _tcslen( pszFind ) ) == 0 )
					{
						*pResult = nItem;
						Library.Unlock();
						return;
					}
				}
			}
		}
		pNotify->iStart = 0;
	}

	Library.Unlock();
	*pResult = -1;
}

void CLibraryDetailView::OnFindItemA(NMLVFINDITEM* pNotify, LRESULT* pResult)
{
	USES_CONVERSION;
	LPCTSTR pszFind = A2CT( (LPCSTR)pNotify->lvfi.psz );
	
	GET_LIST();
	Library.Lock();

	for ( int nLoop = 0 ; nLoop < 2 ; nLoop++ )
	{
		for ( int nItem = pNotify->iStart ; nItem < pList->GetItemCount() ; nItem++ )
		{
			if ( CLibraryFile* pFile = Library.LookupFile( m_pList[ nItem ].nIndex ) )
			{
				if ( pNotify->lvfi.flags & LVFI_STRING )
				{
					if ( _tcsnicmp( pszFind, pFile->m_sName, _tcslen( pszFind ) ) == 0 )
					{
						*pResult = nItem;
						Library.Unlock();
						return;
					}
				}
			}
		}
		pNotify->iStart = 0;
	}

	Library.Unlock();
	*pResult = -1;
}

void CLibraryDetailView::OnCustomDraw(NMLVCUSTOMDRAW* pNotify, LRESULT* pResult)
{
	if ( pNotify->nmcd.dwDrawStage == CDDS_PREPAINT )
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	else if (	pNotify->nmcd.dwDrawStage == CDDS_ITEMPREPAINT &&
				pNotify->nmcd.dwItemSpec < m_nList )
	{
		LDVITEM* pItem = m_pList + pNotify->nmcd.dwItemSpec;

		if ( pItem->nState & LDVI_SELECTED )
		{
			pNotify->clrText = CoolInterface.m_crHiText;
			pNotify->clrTextBk = CoolInterface.m_crHighlight;
		}
		else if ( pItem->nState & LDVI_UNSAFE )		pNotify->clrText = RGB( 255, 0, 0 );
		else if ( pItem->nState & LDVI_UNSCANNED )	pNotify->clrText = CoolInterface.m_crDisabled;
		else if ( pItem->nState & LDVI_PRIVATE )	pNotify->clrText = CoolInterface.m_crHighlight;

		if ( m_bCreateDragImage )
		{
			pNotify->clrTextBk = RGB( 250, 255, 250 );
		}

		*pResult = CDRF_DODEFAULT;
	}
	else
	{
		*pResult = 0;
	}
}

void CLibraryDetailView::OnDblClk(NMHDR* pNotify, LRESULT* pResult)
{
	*pResult = 0;
	SendMessage( WM_COMMAND, ID_LIBRARY_LAUNCH );
}

/////////////////////////////////////////////////////////////////////////////
// CLibraryDetailView column context menu

void CLibraryDetailView::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	CRect rcHeader;

	if ( CWnd* pHeader = GetWindow( GW_CHILD ) )
	{
		pHeader->GetWindowRect( &rcHeader );
	}

	if ( m_pSchema == NULL || rcHeader.PtInRect( point ) == FALSE || m_nStyle != LVS_REPORT )
	{
		CLibraryFileView::OnContextMenu( pWnd, point );
		return;
	}

	CMenu* pMenu = CSchemaColumnsDlg::BuildColumnMenu( m_pSchema, &m_pColumns );
	
	pMenu->AppendMenu( MF_SEPARATOR, ID_SEPARATOR, (LPCTSTR)NULL );
	pMenu->AppendMenu( MF_STRING, ID_LIBRARY_COLUMNS, _T("&Schemas...") );

	m_pCoolMenu = new CCoolMenu();
	m_pCoolMenu->AddMenu( pMenu, TRUE );
	m_pCoolMenu->SetWatermark( Skin.GetWatermark( _T("CCoolMenu") ) );
	
	UINT nCmd = pMenu->TrackPopupMenu( TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON|
		TPM_RETURNCMD, point.x, point.y, this );

	delete pMenu;
	delete m_pCoolMenu;
	m_pCoolMenu = NULL;

	if ( nCmd == ID_LIBRARY_COLUMNS )
	{
		OnLibraryColumns();
	}
	else if ( nCmd )
	{
		CPtrList pColumns;
		CSchemaColumnsDlg::ToggleColumnHelper( m_pSchema, &m_pColumns, &pColumns, nCmd, TRUE );
		SetViewSchema( m_pSchema, &pColumns, TRUE, TRUE );
	}
}

void CLibraryDetailView::OnUpdateBlocker(CCmdUI* pCmdUI)
{
	if ( m_pCoolMenu ) pCmdUI->Enable( TRUE );
	else pCmdUI->ContinueRouting();
}

void CLibraryDetailView::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	if ( m_pCoolMenu ) m_pCoolMenu->OnMeasureItem( lpMeasureItemStruct );
}

void CLibraryDetailView::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	if ( m_pCoolMenu ) m_pCoolMenu->OnDrawItem( lpDrawItemStruct );
}

/////////////////////////////////////////////////////////////////////////////
// CLibraryDetailView drag / drop

void CLibraryDetailView::OnBeginDrag(NM_LISTVIEW* pNotify, LRESULT* pResult)
{
	GET_LIST();

	CPoint ptAction( pNotify->ptAction );

	m_bCreateDragImage = TRUE;
	CImageList* pImage = CLiveList::CreateDragImage( pList, ptAction );
	m_bCreateDragImage = FALSE;

	if ( pImage == NULL ) return;

	UpdateWindow();

	ClientToScreen( &ptAction );
	DragObjects( pImage, ptAction );
}

/////////////////////////////////////////////////////////////////////////////
// CLibraryDetailView command handlers

void CLibraryDetailView::OnUpdateLibraryRename(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( GetSelectedCount() == 1 );
}

void CLibraryDetailView::OnLibraryRename() 
{
	int nItem = ListView_GetNextItem( GetSafeHwnd(), -1, LVNI_SELECTED );
	if ( nItem >= 0 ) ListView_EditLabel( GetSafeHwnd(), nItem );
}

void CLibraryDetailView::OnUpdateLibraryColumns(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( m_nStyle == LVS_REPORT );
}

void CLibraryDetailView::OnLibraryColumns() 
{
	CSchemaColumnsDlg dlg;

	dlg.m_pSchema = m_pSchema;
	dlg.m_pColumns.AddTail( &m_pColumns );

	if ( dlg.DoModal() != IDOK ) return;
	
	if ( Settings.Library.FilterURI.IsEmpty() )
	{
		Settings.Library.SchemaURI.Empty();
		if ( dlg.m_pSchema ) Settings.Library.SchemaURI = dlg.m_pSchema->m_sURI;
	}

	SetViewSchema( dlg.m_pSchema, &dlg.m_pColumns, TRUE, TRUE );
}

⌨️ 快捷键说明

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