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

📄 bcgtoolbarmenubutton.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This source code is a part of BCGControlBar library.
// You may use, compile or redistribute it as part of your application 
// for free. You cannot redistribute it as a part of a software development 
// library without the agreement of the author. If the sources are 
// distributed along with the application, you should leave the original 
// copyright notes in the source code without any changes.
// This code can be used WITHOUT ANY WARRANTIES on your own risk.
// 
// Stas Levin <stas@iet.co.il>
//*******************************************************************************

// BCGToolbarMenuButton.cpp: implementation of the CBCGToolbarMenuButton class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "bcgbarres.h"
#include "BCGToolbarMenuButton.h"
#include "BCGMenuBar.h"
#include "BCGPopupMenuBar.h"
#include "BCGCommandManager.h"
#include "globals.h"
#include "BCGKeyboardManager.h"

#include "BCGFrameWnd.h"
#include "BCGMDIFrameWnd.h"
#include "BCGOleIPFrameWnd.h"

#include "MenuImages.h"

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

static const int SEPARATOR_SIZE = 2;
static const int IMAGE_MARGIN = 2;

IMPLEMENT_SERIAL(CBCGToolbarMenuButton, CBCGToolbarButton, VERSIONABLE_SCHEMA | 1)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBCGToolbarMenuButton::CBCGToolbarMenuButton()
{
	Initialize ();
}
//*****************************************************************************************
CBCGToolbarMenuButton::CBCGToolbarMenuButton (UINT uiID, HMENU hMenu, 
								int iImage, LPCTSTR lpszText, BOOL bUserButton)
{
	Initialize ();

	m_nID = uiID;
	m_bUserButton = bUserButton;

	SetImage (iImage);
	m_strText = (lpszText == NULL) ? _T("") : lpszText;

	CreateFromMenu (hMenu);
}
//*****************************************************************************************
void CBCGToolbarMenuButton::Initialize ()
{
	m_bDrawDownArrow = FALSE;
	m_bMenuMode = FALSE;
	m_pPopupMenu = NULL;
	m_pWndParent = NULL;
	m_bDefault = FALSE;
	m_bClickedOnMenu = FALSE;
	m_bHorz = TRUE;
}
//*****************************************************************************************
CBCGToolbarMenuButton::CBCGToolbarMenuButton (const CBCGToolbarMenuButton& src)
{
	m_nID = src.m_nID;
	m_nStyle = src.m_nStyle;
	m_bUserButton = src.m_bUserButton;

	SetImage (src.GetImage ());
	m_strText = src.m_strText;
	m_bDragFromCollection = FALSE;
	m_bText = src.m_bText;
	m_bImage = src.m_bImage;
	m_bDrawDownArrow = src.m_bDrawDownArrow;
	m_bMenuMode = src.m_bMenuMode;
	m_bDefault = src.m_bDefault;

	HMENU hmenu = src.CreateMenu ();
	ASSERT (hmenu != NULL);

	CreateFromMenu (hmenu);
	::DestroyMenu (hmenu);

	m_rect.SetRectEmpty ();

	m_pPopupMenu = NULL;
	m_pWndParent = NULL;

	m_bClickedOnMenu = FALSE;
	m_bHorz = TRUE;
}
//*****************************************************************************************
CBCGToolbarMenuButton::~CBCGToolbarMenuButton()
{
	if (m_pPopupMenu != NULL)
	{
		m_pPopupMenu->m_pParentBtn = NULL;
	}

	while (!m_listCommands.IsEmpty ())
	{
		delete m_listCommands.RemoveHead ();
	}
}

//////////////////////////////////////////////////////////////////////
// Overrides:

void CBCGToolbarMenuButton::CopyFrom (const CBCGToolbarButton& s)
{
	CBCGToolbarButton::CopyFrom (s);

	const CBCGToolbarMenuButton& src = (const CBCGToolbarMenuButton&) s;

	m_bDefault = src.m_bDefault;

	while (!m_listCommands.IsEmpty ())
	{
		delete m_listCommands.RemoveHead ();
	}

	for (POSITION pos = src.m_listCommands.GetHeadPosition (); pos != NULL;)
	{
		CBCGToolbarMenuButton* pItem = (CBCGToolbarMenuButton*) src.m_listCommands.GetNext (pos);
		ASSERT (pItem != NULL);
		ASSERT_KINDOF (CBCGToolbarMenuButton, pItem);

		CRuntimeClass* pSrcClass = pItem->GetRuntimeClass ();
		ASSERT (pSrcClass != NULL);

		CBCGToolbarMenuButton* pNewItem = (CBCGToolbarMenuButton*) pSrcClass->CreateObject ();
		ASSERT (pNewItem != NULL);
		ASSERT_KINDOF (CBCGToolbarMenuButton, pNewItem);

		pNewItem->CopyFrom (*pItem);
		m_listCommands.AddTail (pNewItem);
	}

//	m_nID = 0;	?????
}
//*****************************************************************************************
void CBCGToolbarMenuButton::Serialize (CArchive& ar)
{
	CBCGToolbarButton::Serialize (ar);

	if (ar.IsLoading ())
	{
		while (!m_listCommands.IsEmpty ())
		{
			delete m_listCommands.RemoveHead ();
		}
	}

	m_listCommands.Serialize (ar);
}
//*****************************************************************************************
void CBCGToolbarMenuButton::OnDraw (CDC* pDC, const CRect& rect, CBCGToolBarImages* pImages,
			BOOL bHorz, BOOL bCustomizeMode, BOOL bHighlight,
			BOOL bDrawBorder, BOOL bGrayDisabledButtons)
{
	if (m_bMenuMode)
	{
		DrawMenuItem (pDC, rect, pImages, bCustomizeMode, bHighlight, bGrayDisabledButtons);
		return;
	}

	CSize sizeImage = CMenuImages::Size ();

	//---------------
	// Interior grey:
	//---------------
	pDC->FillRect (rect, &globalData.brBtnFace);

	if (!bCustomizeMode && !bHighlight &&
		(m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE)))
	{
		CRect rectDither = rect;
		rectDither.InflateRect (-afxData.cxBorder2, -afxData.cyBorder2);

		CBCGToolBarImages::FillDitheredRect (pDC, rectDither);
	}

	CRect rectParent = rect;
	CRect rectArrow = rect;

	if (m_bDrawDownArrow)
	{
		if (bHorz)
		{
			rectParent.right -= sizeImage.cx + SEPARATOR_SIZE;
			rectArrow.left = rectParent.right - 1;	// By Sven Ritter
		}
		else
		{
			rectParent.bottom -= sizeImage.cy + SEPARATOR_SIZE;
			rectArrow.top = rectParent.bottom - 1;	// By Sven Ritter
		}
	}

	UINT uiStyle = m_nStyle;

	if (m_bClickedOnMenu && m_nID != 0 && m_nID != (UINT) -1)
	{
		m_nStyle &= ~TBBS_PRESSED;
	}
	else if (m_pPopupMenu != NULL)
	{
		m_nStyle |= TBBS_PRESSED;
	}

	CBCGToolbarButton::OnDraw (pDC, rectParent, pImages, bHorz, 
			bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);

	if (m_bDrawDownArrow)
	{
		if (m_nStyle & (TBBS_PRESSED | TBBS_CHECKED))
		{
			rectArrow.OffsetRect (1, 1);
		}

		if ((bHighlight || (m_nStyle & TBBS_PRESSED) ||
			m_pPopupMenu != NULL) &&
			m_nID != 0 && m_nID != (UINT) -1)
		{
			//----------------
			// Draw separator:
			//----------------
			CRect rectSeparator = rectArrow;

			if (bHorz)
			{
				rectSeparator.right = rectSeparator.left + SEPARATOR_SIZE;
			}
			else
			{
				rectSeparator.bottom = rectSeparator.top + SEPARATOR_SIZE;
			}

			pDC->Draw3dRect (rectSeparator, ::GetSysColor (COLOR_3DSHADOW),
											::GetSysColor (COLOR_3DHILIGHT));
		}

		BOOL bDisabled = (bCustomizeMode && !IsEditable ()) ||
			(!bCustomizeMode && (m_nStyle & TBBS_DISABLED));

		int iImage;
		if (bHorz)
		{
			iImage = (bDisabled) ? CMenuImages::IdArowDownDsbl : CMenuImages::IdArowDown;
		}
		else
		{
			iImage = (bDisabled) ? CMenuImages::IdArowLeftDsbl : CMenuImages::IdArowLeft;
		}

		if (m_pPopupMenu != NULL &&
			(m_nStyle & (TBBS_PRESSED | TBBS_CHECKED)) == 0)
		{
			rectArrow.OffsetRect (1, 1);
		}

		CPoint pointImage (
			rectArrow.left + (rectArrow.Width () - sizeImage.cx) / 2,
			rectArrow.top + (rectArrow.Height () - sizeImage.cy) / 2);

		CMenuImages::Draw (pDC, (CMenuImages::IMAGES_IDS) iImage, pointImage);
	}

	if (!bCustomizeMode)
	{
		if ((m_nStyle & (TBBS_PRESSED | TBBS_CHECKED)) ||
			m_pPopupMenu != NULL)
		{
			//-----------------------
			// Pressed in or checked:
			//-----------------------
			if (m_bClickedOnMenu && m_nID != 0 && m_nID != (UINT) -1)
			{
				pDC->Draw3dRect (&rectParent,
					globalData.clrBtnHilite,
					globalData.clrBtnShadow);

				rectArrow.right --;
				rectArrow.bottom --;

				pDC->Draw3dRect (&rectArrow,
					globalData.clrBtnShadow,
					globalData.clrBtnHilite);
			}
			else
			{
				pDC->Draw3dRect (&rect,
					globalData.clrBtnShadow,
					globalData.clrBtnHilite);
			}
		}
		else if (bHighlight && !(m_nStyle & TBBS_DISABLED) &&
			!(m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE)))
		{
			pDC->Draw3dRect (&rect, globalData.clrBtnHilite,
									globalData.clrBtnShadow);
		}
	}

	m_nStyle = uiStyle;
}
//*****************************************************************************************
SIZE CBCGToolbarMenuButton::OnCalculateSize (CDC* pDC, const CSize& sizeDefault, BOOL bHorz)
{
	m_bHorz = bHorz;

	int iArrowSize = 0;

	if (m_bDrawDownArrow || m_bMenuMode)
	{
		iArrowSize = (bHorz) ? 
			CMenuImages::Size ().cx + SEPARATOR_SIZE - TEXT_MARGIN : 
			CMenuImages::Size ().cy + SEPARATOR_SIZE - TEXT_MARGIN;
	}

	//--------------------
	// Change accelerator:
	//--------------------
	if (g_pKeyboardManager != NULL &&
		m_bMenuMode &&
		(m_nID < 0xF000 || m_nID >= 0xF1F0))	// Not system.
	{
		//-----------------------------------
		// Remove standard aceleration label:
		//-----------------------------------
		int iTabOffset = m_strText.Find (_T('\t'));
		if (iTabOffset >= 0)
		{
			m_strText = m_strText.Left (iTabOffset);
		}

		//---------------------------------
		// Add an actual accelartion label:
		//---------------------------------
		CString strAccel;
		CFrameWnd* pParent = m_pWndParent == NULL ?
			DYNAMIC_DOWNCAST (CFrameWnd, AfxGetMainWnd ()) :
			m_pWndParent->GetTopLevelFrame ();

		if (pParent != NULL &&
			(CBCGKeyboardManager::FindDefaultAccelerator (
				m_nID, strAccel, pParent) ||
			CBCGKeyboardManager::FindDefaultAccelerator (
				m_nID, strAccel, pParent->GetActiveFrame ())))
		{
			m_strText += _T('\t');
			m_strText += strAccel;
		}
	}

	CSize size = CBCGToolbarButton::OnCalculateSize (pDC, sizeDefault, bHorz);
	if (bHorz)
	{	
		size.cx += iArrowSize;
	}
	else
	{
		size.cy += iArrowSize;
	}

	if (m_bMenuMode)
	{
		size.cx += sizeDefault.cx + 2 * TEXT_MARGIN;
	}

	CBCGPopupMenuBar* pParentMenu =
		DYNAMIC_DOWNCAST (CBCGPopupMenuBar, m_pWndParent);
	if (pParentMenu != NULL)
	{
		size.cy = pParentMenu->GetRowHeight ();
	}

	return size;
}
//*****************************************************************************************
BOOL CBCGToolbarMenuButton::OnClick (CWnd* pWnd, BOOL bDelay)
{
	ASSERT_VALID (pWnd);
	
	m_bClickedOnMenu = FALSE;

	if (m_bDrawDownArrow && !bDelay && !m_bMenuMode)
	{
		if (m_nID == 0 || m_nID == (UINT) -1)
		{
			m_bClickedOnMenu = TRUE;
		}
		else
		{
			CPoint ptMouse;
			::GetCursorPos (&ptMouse);
			pWnd->ScreenToClient (&ptMouse);

			CRect rectArrow = m_rect;
			if (m_bHorz)
			{
				rectArrow.left = rectArrow.right - 
					CMenuImages::Size ().cx - SEPARATOR_SIZE;
			}
			else
			{
				rectArrow.top = rectArrow.bottom - 

⌨️ 快捷键说明

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