bcgtoolbarmenubutton.cpp

来自「用bcg库编写的java IDE 源码」· C++ 代码 · 共 1,592 行 · 第 1/3 页

CPP
1,592
字号
//*******************************************************************************
// 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"

#include "XPDrawLayer.h"

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

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

IMPLEMENT_SERIAL(CBCGToolbarMenuButton, CBCGToolbarButton, VERSIONABLE_SCHEMA | 1)

const UINT BGFSOFT_MSG_DROPDOWN = ::RegisterWindowMessage(_T("21F298B6-E144-4ac6-AEA2-AFBA0FC91401"));

/*************************************************************************/

COLORREF DarkenColorXP(COLORREF ColA)
{
  return RGB( MulDiv(GetRValue(ColA),7,10),
              MulDiv(GetGValue(ColA),7,10),
              MulDiv(GetBValue(ColA)+55,7,10));
}

// Function splits a color into its RGB components and
// transforms the color using a scale 0..255
COLORREF DarkenColor( long lScale, COLORREF lColor)
{ 
  long R = MulDiv(GetRValue(lColor),(255-lScale),255);
  long G = MulDiv(GetGValue(lColor),(255-lScale),255);
  long B = MulDiv(GetBValue(lColor),(255-lScale),255);

  return RGB(R, G, B); 
}

COLORREF MixedColor(COLORREF ColA,COLORREF ColB)
{
  // ( 86a + 14b ) / 100
  int Red   = MulDiv(86,GetRValue(ColA),100) + MulDiv(14,GetRValue(ColB),100);
  int Green = MulDiv(86,GetGValue(ColA),100) + MulDiv(14,GetGValue(ColB),100);
  int Blue  = MulDiv(86,GetBValue(ColA),100) + MulDiv(14,GetBValue(ColB),100);

  return RGB( Red,Green,Blue);
}

COLORREF MidColor(COLORREF ColA,COLORREF ColB)
{ 
  // (7a + 3b)/10
  int Red   = MulDiv(7,GetRValue(ColA),10) + MulDiv(3,GetRValue(ColB),10);
  int Green = MulDiv(7,GetGValue(ColA),10) + MulDiv(3,GetGValue(ColB),10);
  int Blue  = MulDiv(7,GetBValue(ColA),10) + MulDiv(3,GetBValue(ColB),10);

  return RGB( Red,Green,Blue);
}

// used for function gradientfill.
#pragma comment(lib,"Msimg32.lib")

void DrawGradient(CDC* pDC,CRect& Rect,
                  COLORREF StartColor,COLORREF EndColor, 
                  BOOL bHorizontal)
{
  TRIVERTEX vert[2] ;
  GRADIENT_RECT gRect;

  vert [0].y = Rect.top;
  vert [0].x = Rect.left;

  vert [0].Red    = COLOR16(COLOR16(GetRValue(StartColor))<<8);
  vert [0].Green  = COLOR16(COLOR16(GetGValue(StartColor))<<8);
  vert [0].Blue   = COLOR16(COLOR16(GetBValue(StartColor))<<8);
  vert [0].Alpha  = 0x0000;

  vert [1].y = Rect.bottom;
  vert [1].x = Rect.right;

  vert [1].Red    = COLOR16(COLOR16(GetRValue(EndColor))<<8);
  vert [1].Green  = COLOR16(COLOR16(GetGValue(EndColor))<<8);
  vert [1].Blue   = COLOR16(COLOR16(GetBValue(EndColor))<<8);
  vert [1].Alpha  = 0x0000;

  gRect.UpperLeft  = 0;
  gRect.LowerRight = 1;

  if(bHorizontal)
    GradientFill(pDC->m_hDC,vert,2,&gRect,1,GRADIENT_FILL_RECT_H);
  else
    GradientFill(pDC->m_hDC,vert,2,&gRect,1,GRADIENT_FILL_RECT_V);
}

/***************************************************************************/












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

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

	m_nID = uiID;
	m_bUserButton = bUserButton;

	SetImage (iImage);
	m_strText = (lpszText == NULL) ? _T("") : lpszText;
    m_bSendDropDownMsg = bSendDropMsg;
	CreateFromMenu(hMenu);
}

//*****************************************************************************************
CBCGToolbarMenuButton::CBCGToolbarMenuButton(UINT uiID,CWnd* pwndPanel, int iImage,
								BOOL bUserButton /*= FALSE*/)
{
	Initialize ();

	m_nID = uiID;
	m_bUserButton = bUserButton;

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

    m_pwndContainer = pwndPanel;
}


//*****************************************************************************************
void CBCGToolbarMenuButton::Initialize ()
{
	m_bDrawDownArrow = FALSE;
	m_bMenuMode = FALSE;
	m_pPopupMenu = NULL;
	m_pWndParent = NULL;
	m_bDefault = FALSE;
	m_bClickedOnMenu = FALSE;
	m_bHorz = TRUE;
	m_pwndContainer = NULL;
	m_bSendDropDownMsg = FALSE;
}
//*****************************************************************************************
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;
	m_pwndContainer = NULL;
}
//*****************************************************************************************
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_pwndContainer = src.m_pwndContainer;
    m_bSendDropDownMsg = src.m_bSendDropDownMsg;
	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();
	CRect rectParent = rect;
	CRect rectArrow  = rect;

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



	//put this here so everything else is drawn over it
	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);
				// 
				// 
				//**pDC->Draw3dRect(&rectParent,GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorShadow());
                //
                //
				//rectArrow.right --;
				//rectArrow.bottom --;
                //
				//
				//pDC->Draw3dRect (&rectArrow,
				//	globalData.clrBtnShadow,
				//	globalData.clrBtnHilite);
				//
				// 
				//pDC->Draw3dRect(&rectArrow,GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorShadow());
			    CRect rcBtn(rect);

			    rcBtn.InflateRect(0,0,0,1);
                pDC->Draw3dRect(&rcBtn,GuiDrawLayer::GetRGBCaptionXP(),
				                                GuiDrawLayer::GetRGBCaptionXP());
				//rcBtn.InflateRect(0,0,sizeImage.cx,0);
				//DrawShade(pDC,rcBtn);
			}
			else
			{
				//////////Original/////////////
				//pDC->Draw3dRect (&rect,    //
				//	globalData.clrBtnShadow, //
				//	globalData.clrBtnHilite);//
				///////////////////////////////
				if(!m_bDrawDownArrow)
				 DrawShade(pDC,rect);
				else
				{
			       pDC->Draw3dRect(&rect,GuiDrawLayer::GetRGBCaptionXP(),GuiDrawLayer::GetRGBCaptionXP());

			       CRect rcBtn(rect);

			       rcBtn.DeflateRect(1,1);

			       CBrush cblu;
				   cblu.CreateSolidBrush(GuiDrawLayer::GetRGBMenuItemHighLite());
			       pDC->FillRect(rcBtn,&cblu);
			       cblu.DeleteObject();

			       if(m_bDrawDownArrow)
				   {
		             CRect rcSep(rect);
		             rcSep.DeflateRect(0,0,sizeImage.cx,0);
                     pDC->Draw3dRect(&rcSep,GuiDrawLayer::GetRGBCaptionXP(),GuiDrawLayer::GetRGBCaptionXP());
				   }
				}
			}
		}
		else 
		if(bHighlight && !(m_nStyle & TBBS_DISABLED) &&
			!(m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE)))
		{
			//pDC->Draw3dRect (&rect, globalData.clrBtnHilite,
			//						globalData.clrBtnShadow);
            
			pDC->Draw3dRect(&rect,GuiDrawLayer::GetRGBCaptionXP(),GuiDrawLayer::GetRGBCaptionXP());

			CRect rcBtn(rect);

			rcBtn.DeflateRect(1,1);

			CBrush cblu;
			cblu.CreateSolidBrush(GuiDrawLayer::GetRGBFondoXP());
			pDC->FillRect(rcBtn,&cblu);
			cblu.DeleteObject();

			if(m_bDrawDownArrow)
			{
		      CRect rcSep(rect);
		      rcSep.DeflateRect(0,0,sizeImage.cx,0);
              pDC->Draw3dRect(&rcSep,GuiDrawLayer::GetRGBCaptionXP(),GuiDrawLayer::GetRGBCaptionXP());
			}
		}
	}



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

		CBCGToolBarImages::FillDitheredRect (pDC, rectDither);
	}


	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);
	}
	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);
		}

		//---------------------------------

⌨️ 快捷键说明

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