📄 bcgptoolbarbutton.cpp
字号:
rectText.left = rectText.right - ptTextOffset.x;
rectText.top = rectInternal.top + ptTextOffset.y + nTextMargin;
rectText.bottom = rectInternal.top + 3 * ptTextOffset.y;
uiTextFormat |= DT_NOCLIP;
strText.Replace (_T("&&"), strDummyAmpSeq);
int iAmpIndex = strText.Find (_T('&')); // Find a SINGLE '&'
strText.Remove (_T('&'));
strText.Replace (strDummyAmpSeq, _T("&&"));
if (iAmpIndex >= 0)
{
//-----------------------------------------
// Calculate underlined character position:
//-----------------------------------------
CRect rectSubText;
rectSubText.SetRectEmpty ();
CString strSubText = strText.Left (iAmpIndex + 1);
pDC->DrawText (strSubText, &rectSubText, uiTextFormat | DT_CALCRECT);
int y1 = rectSubText.right;
rectSubText.SetRectEmpty ();
strSubText = strText.Left (iAmpIndex);
pDC->DrawText (strSubText, &rectSubText, uiTextFormat | DT_CALCRECT);
int y2 = rectSubText.right;
pDC->DrawText (strText, &rectText, uiTextFormat);
int x = rectInternal.left + nTextMargin;
pDC->MoveTo (x, rectText.top + y1);
pDC->LineTo (x, rectText.top + y2);
}
else
{
pDC->DrawText (strText, &rectText, uiTextFormat);
}
}
}
//--------------------
// Draw button border:
//--------------------
if (!bCustomizeMode && HaveHotBorder () && bDrawBorder)
{
if (m_nStyle & (TBBS_PRESSED | TBBS_CHECKED))
{
//-----------------------
// Pressed in or checked:
//-----------------------
CBCGPVisualManager::GetInstance ()->OnDrawButtonBorder (pDC,
this, rect, CBCGPVisualManager::ButtonsIsPressed);
}
else if (bHot && !(m_nStyle & TBBS_DISABLED) &&
!(m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE)))
{
CBCGPVisualManager::GetInstance ()->OnDrawButtonBorder (pDC,
this, rect, CBCGPVisualManager::ButtonsIsHighlighted);
}
}
}
//***********************************************************************************
SIZE CBCGPToolbarButton::OnCalculateSize (
CDC* pDC,
const CSize& sizeDefault,
BOOL bHorz)
{
ASSERT_VALID (pDC);
if(!IsVisible())
return CSize(0,0);
CSize size = sizeDefault;
if (m_nStyle & TBBS_SEPARATOR)
{
if (bHorz)
{
size.cx = m_iImage > 0 ? m_iImage : nSeparatorWidth;
}
else
{
size.cy = nSeparatorWidth;
}
}
else
{
if (!IsDrawImage () || GetImage () < 0)
{
CSize sizeExtra = m_bExtraSize ?
CBCGPVisualManager::GetInstance ()->GetButtonExtraBorder () : CSize (0, 0);
if (bHorz)
{
size.cx = sizeExtra.cx;
}
else
{
size.cy = sizeExtra.cy;
}
}
m_sizeText = CSize (0, 0);
if (!m_strText.IsEmpty ())
{
if (m_bTextBelow && bHorz)
{
//----------------------------------------------------------
// Try format text that it ocuppies no more tow lines an its
// width less than 3 images:
//----------------------------------------------------------
CRect rectText (0, 0,
sizeDefault.cx * 3, sizeDefault.cy);
UINT uiTextFormat = DT_CENTER | DT_CALCRECT;
if (m_bWrapText)
{
uiTextFormat |= DT_WORDBREAK;
}
pDC->DrawText ( m_strText, rectText, uiTextFormat);
m_sizeText = rectText.Size ();
m_sizeText.cx += 2 * nTextMargin;
size.cx = max (size.cx, m_sizeText.cx) + 4 * nTextMargin;
size.cy += m_sizeText.cy + CY_BORDER;
}
else if (IsDrawText ())
{
CString strWithoutAmp = m_strText;
strWithoutAmp.Replace (_T("&&"), strDummyAmpSeq);
strWithoutAmp.Remove (_T('&'));
strWithoutAmp.Replace (strDummyAmpSeq, _T("&"));
int iTextLen = pDC->GetTextExtent (strWithoutAmp).cx + 3 * nTextMargin;
if (bHorz)
{
size.cx += iTextLen;
}
else
{
size.cy += iTextLen;
}
}
}
}
return size;
}
//************************************************************************************
BOOL CBCGPToolbarButton::PrepareDrag (COleDataSource& srcItem)
{
if (!CanBeStored ())
{
return TRUE;
}
try
{
CSharedFile globFile;
CArchive ar (&globFile,CArchive::store);
//---------------------------------
// Save run-time class information:
//---------------------------------
CRuntimeClass* pClass = GetRuntimeClass ();
ASSERT (pClass != NULL);
ar.WriteClass (pClass);
//---------------------
// Save button context:
//---------------------
Serialize (ar);
ar.Close();
srcItem.CacheGlobalData (GetClipboardFormat (), globFile.Detach());
}
catch (COleException* pEx)
{
TRACE(_T("CBCGPToolbarButton::PrepareDrag. OLE exception: %x\r\n"),
pEx->m_sc);
pEx->Delete ();
return FALSE;
}
catch (CArchiveException* pEx)
{
TRACE(_T("CBCGPToolbarButton::PrepareDrag. Archive exception\r\n"));
pEx->Delete ();
return FALSE;
}
return TRUE;
}
//****************************************************************************************
void CBCGPToolbarButton::SetImage (int iImage)
{
if (m_nStyle & TBBS_SEPARATOR)
{
m_iImage = iImage; // Actualy, separator width!
return;
}
if (m_bUserButton)
{
m_iUserImage = iImage;
}
else
{
m_iImage = iImage;
}
if (!m_bLocked)
{
if (m_nID != 0 && iImage != -1)
{
if (m_bUpdateImages || m_bUserButton)
{
CMD_MGR.SetCmdImage (m_nID, iImage, m_bUserButton);
}
}
else if (m_nID != 0)
{
m_iImage = CMD_MGR.GetCmdImage (m_nID, FALSE);
m_iUserImage = CMD_MGR.GetCmdImage (m_nID, TRUE);
if (m_iImage == -1 && !m_bUserButton)
{
m_bUserButton = TRUE;
}
else if (m_iImage == -1 && m_bUserButton)
{
m_bUserButton = FALSE;
}
}
}
if ((!m_bUserButton && m_iImage < 0) ||
(m_bUserButton && m_iUserImage < 0))
{
m_bImage = FALSE;
m_bText = TRUE;
}
}
/////////////////////////////////////////////////////////////////////////////
// CBCGPToolbarButton diagnostics
#ifdef _DEBUG
void CBCGPToolbarButton::AssertValid() const
{
CObject::AssertValid();
}
//******************************************************************************************
void CBCGPToolbarButton::Dump(CDumpContext& dc) const
{
CObject::Dump (dc);
CString strId;
strId.Format (_T("%x"), m_nID);
dc << "[" << strId << " " << m_strText << "]";
dc << "\n";
}
#endif
int CBCGPToolbarButton::OnDrawOnCustomizeList (
CDC* pDC, const CRect& rect, BOOL bSelected)
{
ASSERT_VALID (this);
ASSERT_VALID (pDC);
BOOL bText = m_bText;
m_bText = FALSE;
int iWidth = 0;
CBCGPToolBarImages* pImages = m_bUserButton ?
CBCGPToolBar::GetUserImages () :
CBCGPToolBar::GetImages ();
CBCGPUserTool* pUserTool = NULL;
if (g_pUserToolsManager != NULL && !m_bUserButton)
{
pUserTool = g_pUserToolsManager->FindTool (m_nID);
}
CBCGPToolBarImages* pCommonImages = CBCGPToolBar::GetImages ();
CSize sizeButton (0, 0);
if (pCommonImages != NULL)
{
sizeButton = CSize (pCommonImages->GetImageSize ().cx + 6,
pCommonImages->GetImageSize ().cy + 6);
}
CRect rectFill = rect;
if (bSelected && GetImage () >= 0 && pImages != NULL)
{
rectFill.left += sizeButton.cx;
}
COLORREF clrText = CBCGPVisualManager::GetInstance ()->OnFillCommandsListBackground
(pDC, rectFill, bSelected);
CRect rectRow = rect;
CRect rectText = rect;
rectText.left += pCommonImages->GetImageSize ().cx + 2 * IMAGE_MARGIN + 2;
iWidth = sizeButton.cx;
//-------------------
// Draw button image:
//-------------------
if (GetImage () >= 0 && pImages != NULL)
{
CRect rectImage = rect;
rectImage.right = rectImage.left + sizeButton.cx;
if (pUserTool != NULL)
{
pUserTool->DrawToolIcon (pDC, rectImage);
}
else
{
CBCGPDrawState ds;
pImages->PrepareDrawImage (ds,
CSize (0, 0),
CBCGPVisualManager::GetInstance ()->IsFadeInactiveImage ());
OnDraw (pDC, rectImage, pImages, TRUE, TRUE, bSelected);
pImages->EndDrawImage (ds);
}
rectRow.left = rectImage.right;
}
//-------------------
// Draw button text:
//-------------------
if (!m_strText.IsEmpty ())
{
COLORREF clrTextOld = pDC->SetTextColor (clrText);
pDC->SetBkMode (TRANSPARENT);
pDC->DrawText (m_strText, rectText, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
pDC->SetTextColor (clrTextOld);
int iTextWidth = min (rectText.Width (), pDC->GetTextExtent (m_strText).cx);
iWidth += iTextWidth;
}
m_bText = bText;
return iWidth;
}
//*************************************************************************************
BOOL CBCGPToolbarButton::OnToolHitTest(const CWnd* pWnd, TOOLINFO* pTI)
{
CFrameWnd* pTopFrame = (pWnd == NULL) ?
(CFrameWnd*) AfxGetMainWnd () :
BCGCBProGetTopLevelFrame (pWnd);
CBCGPMDIFrameWnd* pMainFrame = DYNAMIC_DOWNCAST (CBCGPMDIFrameWnd, pTopFrame);
if (pMainFrame != NULL)
{
return pMainFrame->OnMenuButtonToolHitTest (this, pTI);
}
else // Maybe, SDI frame...
{
CBCGPFrameWnd* pFrame = DYNAMIC_DOWNCAST (CBCGPFrameWnd, pTopFrame);
if (pFrame != NULL)
{
return pFrame->OnMenuButtonToolHitTest (this, pTI);
}
else // Maybe, OLE frame...
{
CBCGPOleIPFrameWnd* pOleFrame =
DYNAMIC_DOWNCAST (CBCGPOleIPFrameWnd, pFrame);
if (pOleFrame != NULL)
{
return pOleFrame->OnMenuButtonToolHitTest (this, pTI);
}
}
}
CBCGPFrameWnd* pFrame = DYNAMIC_DOWNCAST (CBCGPFrameWnd, pTopFrame);
if (pFrame != NULL)
{
return pFrame->OnMenuButtonToolHitTest (this, pTI);
}
return FALSE;
}
//*************************************************************************************
BOOL CBCGPToolbarButton::ExportToMenuButton (CBCGPToolbarMenuButton& menuButton) const
{
//-----------------------------------------------------
// Text may be undefined, bring it from the tooltip :-(
//-----------------------------------------------------
if (m_strText.IsEmpty () && m_nID != 0)
{
CString strMessage;
int iOffset;
if (strMessage.LoadString (m_nID) &&
(iOffset = strMessage.Find (_T('\n'))) != -1)
{
menuButton.m_strText = strMessage.Mid (iOffset + 1);
}
}
return TRUE;
}
//*******************************************************************************
void CBCGPToolbarButton::SetProtectedCommands (const CList<UINT, UINT>& lstCmds)
{
m_lstProtectedCommands.RemoveAll ();
m_lstProtectedCommands.AddTail ((CList<UINT,UINT>*) &lstCmds);
}
//********************************************************************************
void CBCGPToolbarButton::SetClipboardFormatName (LPCTSTR lpszName)
{
ASSERT (lpszName != NULL);
ASSERT (m_cFormat == 0);
m_strClipboardFormatName = lpszName;
}
//********************************************************************************
void CBCGPToolbarButton::FillInterior (CDC* pDC, const CRect& rect,
BOOL bHighlight)
{
if (m_bDisableFill)
{
return;
}
CBCGPVisualManager::BCGBUTTON_STATE state = CBCGPVisualManager::ButtonsIsRegular;
if (!CBCGPToolBar::IsCustomizeMode ())
{
if (bHighlight)
{
state = CBCGPVisualManager::ButtonsIsHighlighted;
}
else if (m_nStyle & (TBBS_PRESSED | TBBS_CHECKED))
{
//-----------------------
// Pressed in or checked:
//-----------------------
state = CBCGPVisualManager::ButtonsIsPressed;
}
}
CBCGPVisualManager::GetInstance ()->OnFillButtonInterior (pDC, this, rect, state);
}
//************************************************************************************
void CBCGPToolbarButton::ResetImageToDefault ()
{
if (m_bUserButton || (int) m_nID <= 0)
{
return;
}
if (g_pUserToolsManager != NULL &&
g_pUserToolsManager->FindTool (m_nID) != NULL)
{
// User tool has its own image
return;
}
BOOL bWasImage = m_bImage;
int iImage = CBCGPToolBar::GetDefaultImage (m_nID);
if (iImage >= 0)
{
SetImage (iImage);
}
else if (bWasImage)
{
m_bImage = FALSE;
m_bText = TRUE;
if (m_strText.IsEmpty ())
{
CString strMessage;
int iOffset;
if (strMessage.LoadString (m_nID) &&
(iOffset = strMessage.Find (_T('\n'))) != -1)
{
m_strText = strMessage.Mid (iOffset + 1);
}
}
}
}
//********************************************************************************
BOOL CBCGPToolbarButton::CompareWith (const CBCGPToolbarButton& other) const
{
return m_nID == other.m_nID;
}
//********************************************************************************
void CBCGPToolbarButton::OnChangeParentWnd (CWnd* pWndParent)
{
m_bExtraSize = FALSE;
if (pWndParent == NULL)
{
return;
}
CBCGPToolBar* pParentBar = DYNAMIC_DOWNCAST (CBCGPToolBar, pWndParent);
if (pParentBar != NULL && pParentBar->IsButtonExtraSizeAvailable ())
{
m_bExtraSize = TRUE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -