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

📄 ctrlwndtabbar.cpp

📁 p2p软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// CtrlWndTabBar.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 "CtrlWndTabBar.h"
#include "CoolInterface.h"
#include "CoolMenu.h"
#include "Skin.h"

#include "WndMain.h"
#include "WndPlugin.h"

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

BEGIN_MESSAGE_MAP(CWndTabBar, CControlBar)
	//{{AFX_MSG_MAP(CWndTabBar)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	ON_WM_CREATE()
	ON_WM_MEASUREITEM()
	ON_WM_DRAWITEM()
	ON_WM_MBUTTONUP()
	ON_WM_LBUTTONUP()
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CWndTabBar construction

CWndTabBar::CWndTabBar()
{
	m_nMaximumWidth	= 140;
	m_nCookie		= 0;
	m_bTimer		= FALSE;
	m_bMenuGray		= FALSE;
	m_nMessage		= 0;
}

CWndTabBar::~CWndTabBar()
{
	for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
	{
		delete (TabItem*)m_pItems.GetNext( pos );
	}
	m_pItems.RemoveAll();
}

/////////////////////////////////////////////////////////////////////////////
// CWndTabBar operations

BOOL CWndTabBar::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
{
	CRect rc;
	dwStyle |= WS_CHILD;
	return CWnd::Create( NULL, NULL, dwStyle, rc, pParentWnd, nID, NULL );
}

void CWndTabBar::SetMessage(UINT nMessageID)
{
	if ( m_nMessage == nMessageID ) return;

	if ( m_nMessage = nMessageID )
		Skin.LoadString( m_sMessage, m_nMessage );
	else
		m_sMessage.Empty();

	if ( m_pItems.IsEmpty() ) Invalidate();
}

void CWndTabBar::SetMessage(LPCTSTR pszText)
{
	if ( m_sMessage == pszText ) return;
	m_nMessage = 0;
	m_sMessage = pszText;
	if ( m_pItems.IsEmpty() ) Invalidate();
}

void CWndTabBar::SetMaximumWidth(int nWidth)
{
	if ( m_nMaximumWidth == nWidth ) return;
	m_nMaximumWidth = nWidth;
	Invalidate();
}

void CWndTabBar::SetWatermark(HBITMAP hBitmap)
{
	if ( m_bmImage.m_hObject ) m_bmImage.DeleteObject();
	if ( hBitmap ) m_bmImage.Attach( hBitmap );

	for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
	{
		TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );
		pItem->m_nImage = -1;
	}

	m_pIcons.RemoveAll();
	m_pImages.DeleteImageList();
	m_pImages.Create( 16, 16, ILC_COLOR32|ILC_MASK, 1, 1 );
}

/////////////////////////////////////////////////////////////////////////////
// CWndTabBar message handlers

int CWndTabBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if ( CControlBar::OnCreate( lpCreateStruct ) == -1 ) return -1;
	
	m_pImages.Create( 16, 16, ILC_COLOR32|ILC_MASK, 1, 1 );

	m_mnuChild.LoadMenu( IDR_CHILDFRAME );
	CoolMenu.AddMenu( &m_mnuChild );
	
	m_dwStyle |= CBRS_BORDER_3D;

	return 0;
}

CSize CWndTabBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
	CSize size( 32767, 26 );

	if ( CWnd* pParent = AfxGetMainWnd() )
	{
		CRect rc;
		pParent->GetWindowRect( &rc );
		if ( rc.Width() > 32 ) size.cx = rc.Width() + 2;
	}

	return size;
}

void CWndTabBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
	if ( ! IsWindow( m_hWnd ) ) return;
	if ( ! pTarget->IsKindOf( RUNTIME_CLASS(CMainWnd) ) ) return;

	CMainWnd* pMainWnd			= (CMainWnd*)pTarget;
	CWindowManager* pManager	= &pMainWnd->m_pWindows;

	BOOL bChanged = FALSE;
	m_nCookie++;
	
	CChildWnd* pActive = pManager->GetActive();

	if ( pActive && pActive->m_bGroupMode )
	{
		if ( pActive->m_pGroupParent ) pActive = pActive->m_pGroupParent;
	}

	for ( POSITION posChild = pManager->GetIterator() ; posChild ; )
	{
		CChildWnd* pChild = pManager->GetNext( posChild );

		BOOL bFound = FALSE;
		CString strCaption;

		if ( pChild->m_bTabMode ) continue;

		pChild->GetWindowText( strCaption );

		if ( pChild->m_bPanelMode )
		{
			if ( strCaption.Find( _T("Search : ") ) == 0 ) strCaption = strCaption.Mid( 9 );
			if ( strCaption.Find( _T("Browse Host : ") ) == 0 ) strCaption = strCaption.Mid( 14 );
		}

		for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
		{
			TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );

			if ( pItem->m_hWnd == pChild->GetSafeHwnd() )
			{
				bFound = TRUE;
				pItem->m_nCookie = m_nCookie;
				
				if ( strCaption != pItem->m_sCaption )
				{
					pItem->m_nImage = ImageIndexForWindow( pChild );
					pItem->m_sCaption = strCaption;
					bChanged = TRUE;
				}
				else if ( pItem->m_nImage < 0 )
				{
					pItem->m_nImage = ImageIndexForWindow( pChild );
					bChanged = TRUE;
				}
				
				if ( pChild->IsWindowVisible() != pItem->m_bVisible )
				{
					pItem->m_bVisible = pChild->IsWindowVisible();
					bChanged = TRUE;
				}
				
				if ( pChild->m_bAlert != pItem->m_bAlert )
				{
					pItem->m_bAlert = pChild->m_bAlert;
					bChanged = TRUE;
				}
				
				if ( pChild == pActive && m_pSelected != pItem )
				{
					m_pSelected	= pItem;
					bChanged	= TRUE;
				}
				else if ( m_pSelected == pItem && pChild != pActive )
				{
					m_pSelected	= NULL;
					bChanged	= TRUE;
				}
				
				break;
			}
		}

		if ( bFound ) continue;

		TabItem* pItem = new TabItem( pChild, m_nCookie, strCaption );
		if ( pActive == pChild ) m_pSelected = pItem;
		pItem->m_nImage = ImageIndexForWindow( pChild );
		m_pItems.AddTail( pItem );
		bChanged = TRUE;
	}

	for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
	{
		POSITION posOld = pos;
		TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );

		if ( pItem->m_nCookie != m_nCookie )
		{
			if ( m_pSelected == pItem ) m_pSelected = NULL;
			delete pItem;
			m_pItems.RemoveAt( posOld );
			bChanged = TRUE;
		}
	}

	if ( bChanged ) Invalidate();
}

int CWndTabBar::ImageIndexForWindow(CWnd* pChild)
{
	CRuntimeClass* pClass = pChild->GetRuntimeClass();
	WORD nImage;
	
	if ( pClass != RUNTIME_CLASS(CPluginWnd) )
	{
		if ( m_pIcons.Lookup( pClass, nImage ) ) return (int)nImage;
		nImage = m_pImages.Add( pChild->GetIcon( FALSE ) );
		m_pIcons.SetAt( pClass, nImage );
	}
	else
	{
		HICON hIcon = pChild->GetIcon( FALSE );
		if ( m_pIcons.Lookup( (LPVOID)hIcon, nImage ) ) return (int)nImage;
		nImage = m_pImages.Add( hIcon );
		m_pIcons.SetAt( (LPVOID)hIcon, nImage );
	}

	return (int)nImage;
}

CWndTabBar::TabItem* CWndTabBar::HitTest(const CPoint& point, CRect* pItemRect) const
{
	if ( m_pItems.IsEmpty() ) return NULL;

	CRect rc;
	GetClientRect( &rc );
	CalcInsideRect( rc, FALSE );

	rc.left -= m_cyTopBorder;
	rc.top -= m_cxLeftBorder;
	rc.right += m_cyBottomBorder;
	rc.bottom += m_cxRightBorder;

	CRect rcItem( rc.left + 3, rc.top + 1, 0, rc.bottom - 1 );
	rcItem.right = ( rc.Width() - 3 * m_pItems.GetCount() ) / m_pItems.GetCount() + 3;
	rcItem.right = min( rcItem.right, m_nMaximumWidth );

	for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
	{
		TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );
		if ( rcItem.PtInRect( point ) )
		{
			if ( pItemRect ) *pItemRect = rcItem;
			return pItem;
		}
		rcItem.OffsetRect( rcItem.Width() + 3, 0 );
	}

	return NULL;
}

int CWndTabBar::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
	CRect rcItem;
	TabItem* pItem = HitTest( point, &rcItem );

	if ( pItem == NULL ) return -1;
	if ( ! pTI ) return 1;

	pTI->uFlags		= 0;
	pTI->hwnd		= GetSafeHwnd();
	pTI->uId		= (UINT)pItem->m_hWnd;
	pTI->rect		= rcItem;
	pTI->lpszText	= _tcsdup( pItem->m_sCaption );

	return pTI->uId;
}

void CWndTabBar::DoPaint(CDC* pDC)
{
	ASSERT_VALID(this);
	ASSERT_VALID(pDC);
	
	CDC* pOutDC = pDC;
	CRect rc;
	
	GetClientRect( &rc );
	
	if ( m_bmImage.m_hObject != NULL )
	{
		pDC = CoolInterface.GetBuffer( *pDC, rc.Size() );
		CoolInterface.DrawWatermark( pDC, &rc, &m_bmImage );
	}
	
	DrawBorders( pDC, rc );
	
	CFont* pOldFont = (CFont*)pDC->SelectObject( &CoolInterface.m_fntNormal );
	
	if ( m_pItems.GetCount() )
	{
		m_nCloseImage = CoolInterface.ImageForID( ID_CHILD_CLOSE );
		
		CRect rcItem( rc.left + 3, rc.top + 1, 0, rc.bottom - 1 );
		rcItem.right = ( rc.Width() - 3 * m_pItems.GetCount() ) / m_pItems.GetCount() + 3;
		rcItem.right = min( rcItem.right, m_nMaximumWidth );
		
		for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
		{
			TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );

			pItem->Paint( this, pDC, &rcItem, m_pSelected == pItem, m_pHot == pItem,
				pDC != pOutDC );
			pDC->ExcludeClipRect( &rcItem );
			

⌨️ 快捷键说明

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