📄 ctrlmatchtip.cpp
字号:
//
// CtrlMatchTip.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "CtrlCoolTip.h"
#include "CtrlMatchTip.h"
#include "CoolInterface.h"
#include "ShellIcons.h"
#include "Library.h"
#include "SharedFile.h"
#include "MatchObjects.h"
#include "QueryHit.h"
#include "Schema.h"
#include "SchemaCache.h"
#include "VendorCache.h"
#include "TigerTree.h"
#include "SHA.h"
#include "ED2K.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BEGIN_MESSAGE_MAP(CMatchTipCtrl, CWnd)
//{{AFX_MSG_MAP(CMatchTipCtrl)
ON_WM_TIMER()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_MOUSEMOVE()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#define TIP_DELAY 500
#define TIP_OFFSET_X 0
#define TIP_OFFSET_Y 24
#define TIP_MARGIN 6
#define TIP_TEXTHEIGHT 14
/////////////////////////////////////////////////////////////////////////////
// CMatchTipCtrl construction
CMatchTipCtrl::CMatchTipCtrl()
{
m_pOwner = NULL;
m_bVisible = FALSE;
m_pFile = NULL;
m_pHit = NULL;
m_tOpen = 0;
m_nIcon = 0;
if ( m_hUser32 = LoadLibrary( _T("User32.dll") ) )
{
(FARPROC&)m_pfnSetLayeredWindowAttributes = GetProcAddress(
m_hUser32, "SetLayeredWindowAttributes" );
}
else
{
m_pfnSetLayeredWindowAttributes = NULL;
}
if ( ! m_hClass ) m_hClass = AfxRegisterWndClass( CS_SAVEBITS );
m_crBack = CoolInterface.m_crTipBack;
m_crText = CoolInterface.m_crTipText;
m_crBorder = CCoolInterface::CalculateColour( m_crBack, (COLORREF)0, 100 );
if ( m_brBack.m_hObject ) m_brBack.DeleteObject();
m_brBack.CreateSolidBrush( m_crBack );
}
CMatchTipCtrl::~CMatchTipCtrl()
{
if ( m_hUser32 ) FreeLibrary( m_hUser32 );
}
LPCTSTR CMatchTipCtrl::m_hClass = NULL;
CBrush CMatchTipCtrl::m_brBack;
COLORREF CMatchTipCtrl::m_crBack;
COLORREF CMatchTipCtrl::m_crText;
COLORREF CMatchTipCtrl::m_crBorder;
/////////////////////////////////////////////////////////////////////////////
// CMatchTipCtrl operations
BOOL CMatchTipCtrl::Create(CWnd* pParentWnd)
{
CRect rc( 0, 0, 0, 0 );
m_pOwner = pParentWnd;
return CWnd::CreateEx( WS_EX_TOPMOST, m_hClass, NULL, WS_POPUP|WS_DISABLED, rc, pParentWnd, 0, NULL );
}
void CMatchTipCtrl::Show(CMatchFile* pFile, CQueryHit* pHit)
{
if ( AfxGetMainWnd() != GetForegroundWindow() ) return;
if ( ! Settings.Interface.TipSearch ) return;
CPoint point;
GetCursorPos( &point );
if ( m_bVisible )
{
if ( pFile == m_pFile && pHit == m_pHit ) return;
Hide();
m_pFile = pFile;
m_pHit = pHit;
ShowInternal();
}
else if ( point != m_pOpen )
{
m_pFile = pFile;
m_pHit = pHit;
m_pOpen = point;
m_tOpen = GetTickCount() + TIP_DELAY;
}
}
void CMatchTipCtrl::Hide()
{
m_pFile = NULL;
m_pHit = NULL;
m_tOpen = 0;
if ( m_bVisible )
{
ShowWindow( SW_HIDE );
ModifyStyleEx( WS_EX_LAYERED, 0 );
m_bVisible = FALSE;
GetCursorPos( &m_pOpen );
}
}
/////////////////////////////////////////////////////////////////////////////
// CMatchTipCtrl system message handlers
int CMatchTipCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if ( CWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
SetTimer( 1, 250, NULL );
return 0;
}
void CMatchTipCtrl::OnDestroy()
{
KillTimer( 1 );
CWnd::OnDestroy();
}
/////////////////////////////////////////////////////////////////////////////
// CMatchTipCtrl show logic message handlers
void CMatchTipCtrl::OnTimer(UINT nIDEvent)
{
CPoint point;
GetCursorPos( &point );
if ( WindowFromPoint( point ) != m_pOwner )
{
if ( m_bVisible ) Hide();
return;
}
else
{
CWnd* pWnd = GetForegroundWindow();
if ( pWnd != this && pWnd != AfxGetMainWnd() )
{
if ( m_bVisible ) Hide();
return;
}
}
if ( ! m_bVisible && m_tOpen && GetTickCount() >= m_tOpen )
{
m_tOpen = 0;
if ( point == m_pOpen ) ShowInternal();
}
}
void CMatchTipCtrl::ShowInternal()
{
if ( m_bVisible ) return;
if ( m_pHit != NULL )
{
LoadFromHit();
}
else if ( m_pFile != NULL )
{
LoadFromFile();
}
else
{
return;
}
if ( m_sName.GetLength() > 128 ) m_sName = m_sName.Left( 128 );
m_bVisible = TRUE;
CSize sz = ComputeSize();
CRect rc( m_pOpen.x + TIP_OFFSET_X, m_pOpen.y + TIP_OFFSET_Y, 0, 0 );
rc.right = rc.left + sz.cx;
rc.bottom = rc.top + sz.cy;
if ( rc.right >= GetSystemMetrics( SM_CXSCREEN ) )
{
rc.OffsetRect( GetSystemMetrics( SM_CXSCREEN ) - rc.right - 4, 0 );
}
if ( rc.bottom >= GetSystemMetrics( SM_CYSCREEN ) )
{
rc.OffsetRect( 0, -sz.cy - TIP_OFFSET_Y - 4 );
}
if ( Settings.Interface.TipAlpha == 255 || m_pfnSetLayeredWindowAttributes == NULL )
{
ModifyStyleEx( WS_EX_LAYERED, 0 );
}
else
{
ModifyStyleEx( 0, WS_EX_LAYERED );
(*m_pfnSetLayeredWindowAttributes)( GetSafeHwnd(),
0, (BYTE)Settings.Interface.TipAlpha, LWA_ALPHA );
}
SetWindowPos( &wndTopMost, rc.left, rc.top, rc.Width(), rc.Height(),
SWP_SHOWWINDOW|SWP_NOACTIVATE );
UpdateWindow();
}
/////////////////////////////////////////////////////////////////////////////
// CMatchTipCtrl load from content
void CMatchTipCtrl::LoadFromFile()
{
m_sName = m_pFile->m_pBest->m_sName;
m_sSize = m_pFile->m_sSize;
LoadTypeInfo();
if ( m_pFile->m_bSHA1 )
{
m_sSHA1 = _T("sha1:") + CSHA::HashToString( &m_pFile->m_pSHA1 );
}
else
{
m_sSHA1.Empty();
}
if ( m_pFile->m_bTiger )
{
m_sTiger = _T("tree:tiger/:") + CTigerNode::HashToString( &m_pFile->m_pTiger );
}
else
{
m_sTiger.Empty();
}
if ( m_pFile->m_bED2K )
{
m_sED2K = _T("ed2k:") + CED2K::HashToString( &m_pFile->m_pED2K );
}
else
{
m_sED2K.Empty();
}
if ( m_pFile->m_nFiltered == 1 && m_pFile->m_pBest->m_nPartial )
{
CString strFormat;
LoadString( strFormat, IDS_TIP_PARTIAL );
m_sPartial.Format( strFormat, 100.0f * (float)m_pFile->m_pBest->m_nPartial / (float)m_pFile->m_nSize );
}
else
{
m_sPartial.Empty();
}
if ( m_pFile->m_nFiltered == 1 && m_pFile->m_pBest->m_nUpSlots )
{
CString strFormat;
LoadString( strFormat, IDS_TIP_QUEUE );
m_sQueue.Format( strFormat, m_pFile->m_pBest->m_nUpSlots,
max( 0, m_pFile->m_pBest->m_nUpQueue - m_pFile->m_pBest->m_nUpSlots ) );
}
else
{
m_sQueue.Empty();
}
m_pSchema = NULL;
for ( CQueryHit* pHit = m_pFile->m_pHits ; pHit ; pHit = pHit->m_pNext )
{
m_pSchema = SchemaCache.Get( pHit->m_sSchemaURI );
if ( m_pSchema ) break;
}
m_pMetadata.Setup( m_pSchema );
if ( m_pSchema != NULL )
{
for ( pHit = m_pFile->m_pHits ; pHit ; pHit = pHit->m_pNext )
{
if ( pHit->m_pXML && m_pSchema->CheckURI( pHit->m_sSchemaURI ) )
{
m_pMetadata.Combine( pHit->m_pXML );
}
}
m_pMetadata.Vote();
m_pMetadata.Clean( 72 );
}
m_nRating = m_pFile->m_nRated ? m_pFile->m_nRating / m_pFile->m_nRated : 0;
m_sStatus.Empty();
if ( m_pFile->m_bExisting )
{
CLibraryFile* pExisting = NULL;
if ( pExisting == NULL && m_pFile->m_bSHA1 == TRUE )
pExisting = LibraryMaps.LookupFileBySHA1( &m_pFile->m_pSHA1, TRUE );
if ( pExisting == NULL && m_pFile->m_bTiger == TRUE )
pExisting = LibraryMaps.LookupFileByTiger( &m_pFile->m_pTiger, TRUE );
if ( pExisting == NULL && m_pFile->m_bED2K == TRUE )
pExisting = LibraryMaps.LookupFileByED2K( &m_pFile->m_pED2K, TRUE );
if ( pExisting != NULL )
{
if ( pExisting->IsAvailable() )
{
LoadString( m_sStatus, IDS_TIP_EXISTS_LIBRARY );
m_crStatus = RGB( 0, 128, 0 );
}
else
{
LoadString( m_sStatus, IDS_TIP_EXISTS_DELETED );
m_crStatus = RGB( 255, 0, 0 );
if ( pExisting->m_sComments.GetLength() )
{
LoadString( m_sStatus, IDS_TIP_EXISTS_BLACKLISTED );
m_sStatus += pExisting->m_sComments;
}
}
Library.Unlock();
}
}
else if ( m_pFile->m_bDownload || m_pFile->m_pBest->m_bDownload )
{
LoadString( m_sStatus, IDS_TIP_EXISTS_DOWNLOAD );
m_crStatus = RGB( 0, 128, 0 );
}
else if ( m_pFile->m_pBest->m_bBogus || ! m_pFile->m_bOneValid )
{
LoadString( m_sStatus, IDS_TIP_BOGUS );
m_crStatus = RGB( 255, 0, 0 );
}
if ( m_pFile->m_nFiltered == 1 )
{
if ( m_pFile->m_pBest->m_sNick.GetLength() )
{
m_sUser.Format( _T("%s (%s - %s)"),
(LPCTSTR)m_pFile->m_pBest->m_sNick,
(LPCTSTR)CString( inet_ntoa( m_pFile->m_pBest->m_pAddress ) ),
(LPCTSTR)m_pFile->m_pBest->m_pVendor->m_sName );
}
else
{
m_sUser.Format( _T("%s - %s"),
(LPCTSTR)CString( inet_ntoa( m_pFile->m_pBest->m_pAddress ) ),
(LPCTSTR)m_pFile->m_pBest->m_pVendor->m_sName );
}
}
else
{
m_sUser.Empty();
}
}
void CMatchTipCtrl::LoadFromHit()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -