📄 bcgpstatusbar.cpp
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a part of the BCGControlBar Library
// Copyright (C) 1998-2000 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions
// of the accompanying license agreement.
//*******************************************************************************
//
// BCGPStatusBar.cpp : implementation file
//
#include "stdafx.h"
#include "BCGGlobals.h"
#include "BCGPFrameWnd.h"
#include "BCGPMDIFrameWnd.h"
#include "BCGPOleIPFrameWnd.h"
#include "BCGPOleDocIPFrameWnd.h"
#include "BCGPMDIChildWnd.h"
#include "BCGPOleCntrFrameWnd.h"
#include "BCGPGlobalUtils.h"
#include "BCGPVisualManager.h"
#include "BCGPDrawManager.h"
#include "BCGPStatusBar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBCGPStatusBar
inline CBCGStatusBarPaneInfo* CBCGPStatusBar::_GetPanePtr(int nIndex) const
{
if (nIndex == 255 && m_nCount < 255)
{
// Special case for the simple pane
for (int i = 0; i < m_nCount; i++)
{
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr (i);
ASSERT (pSBP != NULL);
if (pSBP->nStyle & SBPS_STRETCH)
{
return pSBP;
}
}
}
if (nIndex < 0 || nIndex >= m_nCount)
{
ASSERT (FALSE);
return NULL;
}
if (m_pData == NULL)
{
ASSERT (FALSE);
return NULL;
}
return ((CBCGStatusBarPaneInfo*)m_pData) + nIndex;
}
#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif
const int nTextMargin = 4; // Gap between image and text
CBCGPStatusBar::CBCGPStatusBar()
{
m_hFont = NULL;
// setup correct margins
m_cxRightBorder = m_cxDefaultGap;
m_cxSizeBox = 0;
m_cxLeftBorder = 4;
m_cyTopBorder = 2;
m_cyBottomBorder = 0;
m_cxRightBorder = 0;
m_bPaneDoubleClick = FALSE;
m_rectSizeBox.SetRectEmpty ();
}
//********************************************************************************
void CBCGPStatusBar::OnSettingChange(UINT /*uFlags*/, LPCTSTR /* lpszSection */)
{
RecalcLayout ();
}
//********************************************************************************
CBCGPStatusBar::~CBCGPStatusBar()
{
}
//********************************************************************************
void CBCGPStatusBar::OnDestroy()
{
for (int i = 0; i < m_nCount; i++)
{
VERIFY(SetPaneText(i, NULL, FALSE)); // no update
SetTipText(i, NULL);
SetPaneIcon(i, NULL, FALSE);
}
CBCGPControlBar::OnDestroy();
}
//********************************************************************************
BOOL CBCGPStatusBar::PreCreateWindow(CREATESTRUCT& cs)
{
// in Win4, status bars do not have a border at all, since it is
// provided by the client area.
if ((m_dwStyle & (CBRS_ALIGN_ANY|CBRS_BORDER_ANY)) == CBRS_BOTTOM)
{
m_dwStyle &= ~(CBRS_BORDER_ANY|CBRS_BORDER_3D);
}
return CBCGPControlBar::PreCreateWindow(cs);
}
//********************************************************************************
BOOL CBCGPStatusBar::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
{
return CreateEx(pParentWnd, 0, dwStyle, nID);
}
//********************************************************************************
BOOL CBCGPStatusBar::CreateEx (CWnd* pParentWnd, DWORD dwCtrlStyle, DWORD dwStyle,
UINT nID)
{
ASSERT_VALID(pParentWnd); // must have a parent
// save the style
SetBarAlignment (dwStyle & CBRS_ALL);
// create the HWND
CRect rect;
rect.SetRectEmpty();
LPCTSTR lpszClass = AfxRegisterWndClass(CS_DBLCLKS, ::LoadCursor(NULL, IDC_ARROW),
(HBRUSH)(COLOR_BTNFACE+1), NULL);
m_dwBCGStyle = 0; // can't float, resize, close, slide
if (pParentWnd->GetStyle() & WS_THICKFRAME)
{
dwStyle |= SBARS_SIZEGRIP;
}
if (!CWnd::Create(lpszClass, NULL, dwStyle | WS_CLIPSIBLINGS, rect, pParentWnd, nID))
return FALSE;
if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPFrameWnd)))
{
((CBCGPFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPMDIFrameWnd)))
{
((CBCGPMDIFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPOleIPFrameWnd)))
{
((CBCGPOleIPFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPOleDocIPFrameWnd)))
{
((CBCGPOleDocIPFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPMDIChildWnd)))
{
((CBCGPMDIChildWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPOleCntrFrameWnd)))
{
((CBCGPOleCntrFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CDialog)))
{
globalUtils.m_bDialogApp = TRUE;
}
else
{
ASSERT (FALSE);
}
return TRUE;
}
//********************************************************************************
BOOL CBCGPStatusBar::SetIndicators(const UINT* lpIDArray, int nIDCount)
{
ASSERT_VALID(this);
ASSERT(nIDCount >= 1); // must be at least one of them
ASSERT(lpIDArray == NULL ||
AfxIsValidAddress(lpIDArray, sizeof(UINT) * nIDCount, FALSE));
// free strings before freeing array of elements
for (int i = 0; i < m_nCount; i++)
{
VERIFY(SetPaneText(i, NULL, FALSE)); // no update
//free Imagelist if any exist
SetPaneIcon(i, NULL, FALSE);
}
// first allocate array for panes and copy initial data
if (!AllocElements(nIDCount, sizeof(CBCGStatusBarPaneInfo)))
return FALSE;
ASSERT(nIDCount == m_nCount);
HFONT hFont = GetCurrentFont ();
BOOL bOK = TRUE;
if (lpIDArray != NULL)
{
ASSERT(hFont != NULL); // must have a font !
CString strText;
CClientDC dcScreen(NULL);
HGDIOBJ hOldFont = dcScreen.SelectObject(hFont);
for (int i = 0; i < nIDCount; i++)
{
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(i);
if (pSBP == NULL)
{
ASSERT (FALSE);
return FALSE;
}
pSBP->nID = *lpIDArray++;
if (pSBP->nID != 0)
{
if (!strText.LoadString(pSBP->nID))
{
TRACE1("Warning: failed to load indicator string 0x%04X.\n",
pSBP->nID);
bOK = FALSE;
break;
}
pSBP->cxText = dcScreen.GetTextExtent(strText,
strText.GetLength()).cx;
ASSERT(pSBP->cxText >= 0);
if (!SetPaneText(i, strText, FALSE))
{
bOK = FALSE;
break;
}
}
else
{
// no indicator (must access via index)
// default to 1/4 the screen width (first pane is stretchy)
pSBP->cxText = ::GetSystemMetrics(SM_CXSCREEN) / 4;
if (i == 0)
{
pSBP->nStyle |= (SBPS_STRETCH | SBPS_NOBORDERS);
}
}
}
dcScreen.SelectObject(hOldFont);
}
RecalcLayout ();
return bOK;
}
#ifdef AFX_CORE3_SEG
#pragma code_seg(AFX_CORE3_SEG)
#endif
/////////////////////////////////////////////////////////////////////////////
// CBCGPStatusBar attribute access
int CBCGPStatusBar::CommandToIndex(UINT nIDFind) const
{
ASSERT_VALID(this);
if (m_nCount <= 0)
{
return -1;
}
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(0);
if (pSBP == NULL)
{
ASSERT (FALSE);
return -1;
}
for (int i = 0; i < m_nCount; i++, pSBP++)
{
if (pSBP->nID == nIDFind)
{
return i;
}
}
return -1;
}
//*******************************************************************************
UINT CBCGPStatusBar::GetItemID(int nIndex) const
{
ASSERT_VALID(this);
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return 0;
}
return pSBP->nID;
}
//*******************************************************************************
void CBCGPStatusBar::GetItemRect(int nIndex, LPRECT lpRect) const
{
ASSERT_VALID(this);
ASSERT(AfxIsValidAddress(lpRect, sizeof(RECT)));
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return;
}
*lpRect = pSBP->rect;
}
//*******************************************************************************
UINT CBCGPStatusBar::GetPaneStyle(int nIndex) const
{
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return 0;
}
return pSBP->nStyle;
}
//*******************************************************************************
void CBCGPStatusBar::SetPaneStyle(int nIndex, UINT nStyle)
{
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return;
}
if (pSBP->nStyle != nStyle)
{
// just change the style of 1 pane, and invalidate it
pSBP->nStyle = nStyle;
InvalidateRect (&pSBP->rect, FALSE);
UpdateWindow ();
}
}
//*******************************************************************************
int CBCGPStatusBar::GetPaneWidth (int nIndex) const
{
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return 0;
}
CRect rect = pSBP->rect;
return rect.Width ();
}
//********************************************************************************
void CBCGPStatusBar::SetPaneWidth (int nIndex, int cx)
{
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return;
}
CRect rect = pSBP->rect;
int cxCurr = rect.Width () - CX_BORDER * 4;
int cxTextNew = cx - pSBP->cxIcon;
if (pSBP->cxIcon > 0)
{
cxTextNew -= nTextMargin;
}
pSBP->cxText = max (0, cxTextNew);
if (cx != cxCurr)
{
RecalcLayout ();
Invalidate();
UpdateWindow ();
}
}
//********************************************************************************
void CBCGPStatusBar::GetPaneInfo(int nIndex, UINT& nID, UINT& nStyle,
int& cxWidth) const
{
ASSERT_VALID(this);
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return;
}
nID = pSBP->nID;
nStyle = pSBP->nStyle;
CRect rect = pSBP->rect;
cxWidth = rect.Width ();
}
//*******************************************************************************
void CBCGPStatusBar::SetPaneInfo(int nIndex, UINT nID, UINT nStyle, int cxWidth)
{
ASSERT_VALID(this);
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return;
}
pSBP->nID = nID;
SetPaneStyle(nIndex, nStyle);
SetPaneWidth (nIndex, cxWidth);
}
//*******************************************************************************
void CBCGPStatusBar::GetPaneText(int nIndex, CString& s) const
{
ASSERT_VALID(this);
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return;
}
s = pSBP->lpszText == NULL ? _T("") : pSBP->lpszText;
}
//*******************************************************************************
CString CBCGPStatusBar::GetPaneText(int nIndex) const
{
ASSERT_VALID(this);
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return _T("");
}
CString s = pSBP->lpszText == NULL ? _T("") : pSBP->lpszText;
return s;
}
//*******************************************************************************
BOOL CBCGPStatusBar::SetPaneText(int nIndex, LPCTSTR lpszNewText, BOOL bUpdate)
{
ASSERT_VALID(this);
CBCGStatusBarPaneInfo* pSBP = _GetPanePtr(nIndex);
if (pSBP == NULL)
{
ASSERT (FALSE);
return FALSE;
}
if (pSBP->lpszText != NULL)
{
if (lpszNewText != NULL && lstrcmp(pSBP->lpszText, lpszNewText) == 0)
{
return TRUE; // nothing to change
}
free((LPVOID)pSBP->lpszText);
}
else if (lpszNewText == NULL || *lpszNewText == '\0')
{
return TRUE; // nothing to change
}
BOOL bOK = TRUE;
if (lpszNewText == NULL || *lpszNewText == '\0')
{
pSBP->lpszText = NULL;
}
else
{
pSBP->lpszText = _tcsdup(lpszNewText);
if (pSBP->lpszText == NULL)
bOK = FALSE; // old text is lost and replaced by NULL
}
if (bUpdate)
{
InvalidatePaneContent (nIndex);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -