📄 bcgptoolbareditboxbutton.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.
//*******************************************************************************
// BCGToolbarEditBoxButton.cpp: implementation of the CBCGPToolbarEditBoxButton class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BCGPToolbar.h"
#include "BCGPToolbarEditBoxButton.h"
#include "BCGPVisualManager.h"
#include "trackmouse.h"
#include "BCGPContextMenuManager.h"
#include "BCGPWorkspace.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern CBCGPWorkspace* g_pWorkspace;
/////////////////////////////////////////////////////////////////////////////
// CBCGPToolbarEditCtrl
class CBCGPToolbarEditCtrl : public CEdit
{
// Construction
public:
CBCGPToolbarEditCtrl(CBCGPToolbarEditBoxButton& edit);
// Attributes
protected:
CBCGPToolbarEditBoxButton& m_buttonEdit;
BOOL m_bTracked;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBCGPToolbarEditCtrl)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CBCGPToolbarEditCtrl();
// Generated message map functions
protected:
//{{AFX_MSG(CBCGPToolbarEditCtrl)
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
//}}AFX_MSG
afx_msg LRESULT OnMouseLeave(WPARAM,LPARAM);
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_SERIAL(CBCGPToolbarEditBoxButton, CBCGPToolbarButton, 1)
static const int iDefaultSize = 150;
static const int iHorzMargin = 3;
static const int iVertMargin = 1;
BOOL CBCGPToolbarEditBoxButton::m_bFlat = FALSE;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBCGPToolbarEditBoxButton::CBCGPToolbarEditBoxButton()
{
m_dwStyle = WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL;
m_iWidth = iDefaultSize;
Initialize ();
}
//**************************************************************************************
CBCGPToolbarEditBoxButton::CBCGPToolbarEditBoxButton (UINT uiId,
int iImage,
DWORD dwStyle,
int iWidth) :
CBCGPToolbarButton (uiId, iImage)
{
m_dwStyle = dwStyle | WS_CHILD | WS_VISIBLE;
m_iWidth = (iWidth == 0) ? iDefaultSize : iWidth;
Initialize ();
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::Initialize ()
{
m_pWndEdit = NULL;
m_bHorz = TRUE;
m_bChangingText = FALSE;
m_bIsHotEdit = FALSE;
m_uiMenuResID = 0;
}
//**************************************************************************************
CBCGPToolbarEditBoxButton::~CBCGPToolbarEditBoxButton()
{
if (m_pWndEdit != NULL)
{
m_pWndEdit->DestroyWindow ();
delete m_pWndEdit;
}
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::CopyFrom (const CBCGPToolbarButton& s)
{
CBCGPToolbarButton::CopyFrom (s);
const CBCGPToolbarEditBoxButton& src = (const CBCGPToolbarEditBoxButton&) s;
m_dwStyle = src.m_dwStyle;
m_iWidth = src.m_iWidth;
m_strContents = src.m_strContents;
m_uiMenuResID = src.m_uiMenuResID;
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::Serialize (CArchive& ar)
{
CBCGPToolbarButton::Serialize (ar);
if (ar.IsLoading ())
{
ar >> m_iWidth;
m_rect.right = m_rect.left + m_iWidth;
ar >> m_dwStyle;
ar >> m_strContents;
if (g_pWorkspace != NULL &&
(g_pWorkspace->GetDataVersionMajor () == 6 &&
g_pWorkspace->GetDataVersionMinor () >= 41 ||
g_pWorkspace->GetDataVersionMajor () >= 7 ||
g_pWorkspace->GetDataVersionMajor () == -1 &&
g_pWorkspace->GetDataVersionMinor () == -1))
{
ar >> m_uiMenuResID;
}
}
else
{
ar << m_iWidth;
ar << m_dwStyle;
if (m_pWndEdit != NULL)
{
m_pWndEdit->GetWindowText(m_strContents);
}
else
{
m_strContents.Empty();
}
ar << m_strContents;
if (g_pWorkspace != NULL &&
(g_pWorkspace->GetDataVersionMajor () == 6 &&
g_pWorkspace->GetDataVersionMinor () >= 41 ||
g_pWorkspace->GetDataVersionMajor () >= 7 ||
g_pWorkspace->GetDataVersionMajor () == -1 &&
g_pWorkspace->GetDataVersionMinor () == -1))
{
ar << m_uiMenuResID;
}
}
}
//**************************************************************************************
SIZE CBCGPToolbarEditBoxButton::OnCalculateSize (CDC* pDC, const CSize& sizeDefault, BOOL bHorz)
{
if(!IsVisible())
{
return CSize(0,0);
}
m_bHorz = bHorz;
if (bHorz)
{
if (m_pWndEdit->GetSafeHwnd () != NULL && !m_bIsHidden)
{
m_pWndEdit->ShowWindow (SW_SHOWNOACTIVATE);
}
if (m_bTextBelow && !m_strText.IsEmpty())
{
CRect rectText (0, 0,
m_iWidth, sizeDefault.cy);
pDC->DrawText ( m_strText, rectText,
DT_CENTER | DT_CALCRECT | DT_WORDBREAK);
m_sizeText = rectText.Size ();
}
else
m_sizeText = CSize(0,0);
return CSize (m_iWidth, sizeDefault.cy + m_sizeText.cy);
}
else
{
if (m_pWndEdit->GetSafeHwnd () != NULL)
{
m_pWndEdit->ShowWindow (SW_HIDE);
}
m_sizeText = CSize(0,0);
return CBCGPToolbarButton::OnCalculateSize (pDC, sizeDefault, bHorz);
}
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::OnMove ()
{
if (m_pWndEdit->GetSafeHwnd () == NULL ||
(m_pWndEdit->GetStyle () & WS_VISIBLE) == 0)
{
return;
}
int cy = globalData.GetTextHeight();
int yOffset = max (0, (m_rect.Height () - m_sizeText.cy - cy) / 2);
m_pWndEdit->SetWindowPos (NULL,
m_rect.left + iHorzMargin,
m_rect.top + yOffset,
m_rect.Width () - 2 * iHorzMargin,
cy,
SWP_NOZORDER | SWP_NOACTIVATE);
m_pWndEdit->SetSel(-1, 0);
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::OnSize (int iSize)
{
m_iWidth = iSize;
m_rect.right = m_rect.left + m_iWidth;
OnMove ();
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::OnChangeParentWnd (CWnd* pWndParent)
{
CBCGPToolbarButton::OnChangeParentWnd (pWndParent);
if (m_pWndEdit->GetSafeHwnd () != NULL)
{
CWnd* pWndParentCurr = m_pWndEdit->GetParent ();
ASSERT (pWndParentCurr != NULL);
if (pWndParent != NULL &&
pWndParentCurr->GetSafeHwnd () == pWndParent->GetSafeHwnd ())
{
return;
}
m_pWndEdit->GetWindowText(m_strContents);
m_pWndEdit->DestroyWindow ();
delete m_pWndEdit;
m_pWndEdit = NULL;
}
if (pWndParent == NULL || pWndParent->GetSafeHwnd () == NULL)
{
return;
}
CRect rect = m_rect;
rect.DeflateRect (iHorzMargin, iVertMargin);
rect.bottom = rect.top + globalData.GetTextHeight ();
if ((m_pWndEdit = CreateEdit (pWndParent, rect)) == NULL)
{
ASSERT (FALSE);
return;
}
ASSERT_VALID (m_pWndEdit);
OnMove ();
m_pWndEdit->SetFont (&globalData.fontRegular);
CString sText;
m_pWndEdit->GetWindowText(sText);
if (sText.IsEmpty())
{
m_bChangingText = TRUE;
m_pWndEdit->SetWindowText(m_strContents);
m_bChangingText = FALSE;
}
else
{
m_strContents = sText;
}
}
//**************************************************************************************
BOOL CBCGPToolbarEditBoxButton::NotifyCommand (int iNotifyCode)
{
if (m_pWndEdit->GetSafeHwnd () == NULL)
{
return FALSE;
}
switch (iNotifyCode)
{
case EN_UPDATE:
{
m_pWndEdit->GetWindowText(m_strContents);
//------------------------------------------------------
// Try set selection in ALL editboxes with the same ID:
//------------------------------------------------------
CObList listButtons;
if (CBCGPToolBar::GetCommandButtons (m_nID, listButtons) > 0)
{
for (POSITION posCombo = listButtons.GetHeadPosition (); posCombo != NULL;)
{
CBCGPToolbarEditBoxButton* pEdit =
DYNAMIC_DOWNCAST (CBCGPToolbarEditBoxButton, listButtons.GetNext (posCombo));
if ((pEdit != NULL) && (pEdit != this))
{
pEdit->SetContents(m_strContents);
}
}
}
}
return !m_bChangingText;
}
return FALSE;
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::OnAddToCustomizePage ()
{
CObList listButtons; // Existing buttons with the same command ID
if (CBCGPToolBar::GetCommandButtons (m_nID, listButtons) == 0)
{
return;
}
CBCGPToolbarEditBoxButton* pOther =
(CBCGPToolbarEditBoxButton*) listButtons.GetHead ();
ASSERT_VALID (pOther);
ASSERT_KINDOF (CBCGPToolbarEditBoxButton, pOther);
CopyFrom (*pOther);
}
//**************************************************************************************
HBRUSH CBCGPToolbarEditBoxButton::OnCtlColor (CDC* pDC, UINT /*nCtlColor*/)
{
pDC->SetTextColor (globalData.clrWindowText);
pDC->SetBkColor (globalData.clrWindow);
return (HBRUSH) globalData.brWindow.GetSafeHandle ();
}
//**************************************************************************************
void CBCGPToolbarEditBoxButton::OnDraw (CDC* pDC, const CRect& rect, CBCGPToolBarImages* pImages,
BOOL bHorz, BOOL bCustomizeMode,
BOOL bHighlight, BOOL bDrawBorder,
BOOL bGrayDisabledButtons)
{
if (m_pWndEdit->GetSafeHwnd () == NULL ||
(m_pWndEdit->GetStyle () & WS_VISIBLE) == 0)
{
CBCGPToolbarButton::OnDraw (pDC, rect, pImages,
bHorz, bCustomizeMode,
bHighlight, bDrawBorder,
bGrayDisabledButtons);
return;
}
BOOL bDisabled = (bCustomizeMode && !IsEditable ()) ||
(!bCustomizeMode && (m_nStyle & TBBS_DISABLED));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -