📄 bcgptoolbarcomboboxbutton.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.
//*******************************************************************************
// BCGToolbarComboBoxButton.cpp: implementation of the CBCGPToolbarComboBoxButton class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BCGPToolbar.h"
#include "BCGGlobals.h"
#include "BCGPToolbarComboBoxButton.h"
#include "BCGPToolbarMenuButton.h"
#include "MenuImages.h"
#include "BCGPWorkspace.h"
#include "trackmouse.h"
#include "BCGPVisualManager.h"
#include "BCGPContextMenuManager.h"
#include ".\bcgptoolbarcomboboxbutton.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern CBCGPWorkspace* g_pWorkspace;
/////////////////////////////////////////////////////////////////////////////
// CBCGPComboEdit
class CBCGPComboEdit : public CEdit
{
// Construction
public:
CBCGPComboEdit(CBCGPToolbarComboBoxButton& combo);
// Attributes
protected:
CBCGPToolbarComboBoxButton& m_combo;
BOOL m_bTracked;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBCGPComboEdit)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CBCGPComboEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CBCGPComboEdit)
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnChange();
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(CBCGPToolbarComboBoxButton, CBCGPToolbarButton, 1)
static const int iDefaultComboHeight = 150;
static const int iDefaultSize = 150;
static const int iHorzMargin = 1;
BOOL CBCGPToolbarComboBoxButton::m_bFlat = TRUE;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBCGPToolbarComboBoxButton::CBCGPToolbarComboBoxButton()
{
m_dwStyle = WS_CHILD | WS_VISIBLE | CBS_NOINTEGRALHEIGHT | CBS_DROPDOWNLIST | WS_VSCROLL;
m_iWidth = iDefaultSize;
Initialize ();
}
//**************************************************************************************
CBCGPToolbarComboBoxButton::CBCGPToolbarComboBoxButton (UINT uiId,
int iImage,
DWORD dwStyle,
int iWidth) :
CBCGPToolbarButton (uiId, iImage)
{
m_dwStyle = dwStyle | WS_CHILD | WS_VISIBLE | WS_VSCROLL;
m_iWidth = (iWidth == 0) ? iDefaultSize : iWidth;
Initialize ();
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::Initialize ()
{
m_iSelIndex = -1;
m_pWndCombo = NULL;
m_pWndEdit = NULL;
m_bHorz = TRUE;
m_rectCombo.SetRectEmpty ();
m_rectButton.SetRectEmpty ();
m_nDropDownHeight = iDefaultComboHeight;
m_bIsHotEdit = FALSE;
m_uiMenuResID = 0;
}
//**************************************************************************************
CBCGPToolbarComboBoxButton::~CBCGPToolbarComboBoxButton()
{
if (m_pWndCombo != NULL)
{
m_pWndCombo->DestroyWindow ();
delete m_pWndCombo;
}
if (m_pWndEdit != NULL)
{
m_pWndEdit->DestroyWindow ();
delete m_pWndEdit;
}
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::CopyFrom (const CBCGPToolbarButton& s)
{
CBCGPToolbarButton::CopyFrom (s);
POSITION pos;
m_lstItems.RemoveAll ();
const CBCGPToolbarComboBoxButton& src = (const CBCGPToolbarComboBoxButton&) s;
for (pos = src.m_lstItems.GetHeadPosition (); pos != NULL;)
{
m_lstItems.AddTail (src.m_lstItems.GetNext (pos));
}
ClearData ();
m_lstItemData.RemoveAll ();
for (pos = src.m_lstItemData.GetHeadPosition (); pos != NULL;)
{
m_lstItemData.AddTail (src.m_lstItemData.GetNext (pos));
}
DuplicateData ();
ASSERT (m_lstItemData.GetCount () == m_lstItems.GetCount ());
m_dwStyle = src.m_dwStyle;
m_iWidth = src.m_iWidth;
m_iSelIndex = src.m_iSelIndex;
m_nDropDownHeight = src.m_nDropDownHeight;
m_uiMenuResID = src.m_uiMenuResID;
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::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_iSelIndex;
ar >> m_strEdit;
ar >> m_nDropDownHeight;
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;
}
m_lstItems.Serialize (ar);
ClearData ();
m_lstItemData.RemoveAll ();
for (int i = 0; i < m_lstItems.GetCount (); i ++)
{
long lData;
ar >> lData;
m_lstItemData.AddTail ((DWORD) lData);
}
DuplicateData ();
ASSERT (m_lstItemData.GetCount () == m_lstItems.GetCount ());
SelectItem (m_iSelIndex);
}
else
{
ar << m_iWidth;
ar << m_dwStyle;
ar << m_iSelIndex;
ar << m_strEdit;
ar << m_nDropDownHeight;
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;
}
if (m_pWndCombo != NULL)
{
m_lstItems.RemoveAll ();
ClearData ();
m_lstItemData.RemoveAll ();
for (int i = 0; i < m_pWndCombo->GetCount (); i ++)
{
CString str;
m_pWndCombo->GetLBText (i, str);
m_lstItems.AddTail (str);
m_lstItemData.AddTail (m_pWndCombo->GetItemData (i));
}
}
m_lstItems.Serialize (ar);
for (POSITION pos = m_lstItemData.GetHeadPosition (); pos != NULL;)
{
DWORD dwData = m_lstItemData.GetNext (pos);
ar << (long) dwData;
}
ASSERT (m_lstItemData.GetCount () == m_lstItems.GetCount ());
}
}
//**************************************************************************************
SIZE CBCGPToolbarComboBoxButton::OnCalculateSize (CDC* pDC, const CSize& sizeDefault, BOOL bHorz)
{
m_bHorz = bHorz;
m_sizeText = CSize (0, 0);
if (!IsVisible())
{
if (m_bFlat)
{
if (m_pWndEdit->GetSafeHwnd () != NULL &&
(m_pWndEdit->GetStyle () & WS_VISIBLE))
{
m_pWndEdit->ShowWindow (SW_HIDE);
}
}
if (m_pWndCombo->GetSafeHwnd () != NULL &&
(m_pWndCombo->GetStyle () & WS_VISIBLE))
{
m_pWndCombo->ShowWindow (SW_HIDE);
}
return CSize(0,0);
}
if (m_bFlat &&
m_pWndCombo->GetSafeHwnd () != NULL &&
(m_pWndCombo->GetStyle () & WS_VISIBLE))
{
m_pWndCombo->ShowWindow (SW_HIDE);
}
if (bHorz)
{
if (!m_bFlat && m_pWndCombo->GetSafeHwnd () != NULL && !m_bIsHidden)
{
m_pWndCombo->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 ();
}
int cy = sizeDefault.cy;
if (m_pWndCombo != NULL && m_pWndCombo->GetSafeHwnd () != NULL)
{
CRect rectCombo;
m_pWndCombo->GetWindowRect (&rectCombo);
cy = rectCombo.Height ();
}
return CSize (m_iWidth, cy + m_sizeText.cy);
}
else
{
if (!m_bFlat && m_pWndCombo->GetSafeHwnd () != NULL &&
(m_pWndCombo->GetStyle () & WS_VISIBLE))
{
m_pWndCombo->ShowWindow (SW_HIDE);
}
return CBCGPToolbarButton::OnCalculateSize (pDC, sizeDefault, bHorz);
}
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::OnMove ()
{
if (m_pWndCombo->GetSafeHwnd () != NULL)
{
AdjustRect ();
}
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::OnSize (int iSize)
{
m_iWidth = iSize;
m_rect.right = m_rect.left + m_iWidth;
if (m_pWndCombo->GetSafeHwnd () != NULL)
{
AdjustRect ();
}
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::OnChangeParentWnd (CWnd* pWndParent)
{
CBCGPToolbarButton::OnChangeParentWnd (pWndParent);
if (m_pWndCombo->GetSafeHwnd () != NULL)
{
CWnd* pWndParentCurr = m_pWndCombo->GetParent ();
ASSERT (pWndParentCurr != NULL);
if (pWndParent != NULL &&
pWndParentCurr->GetSafeHwnd () == pWndParent->GetSafeHwnd ())
{
return;
}
m_pWndCombo->DestroyWindow ();
delete m_pWndCombo;
m_pWndCombo = NULL;
if (m_pWndEdit != NULL)
{
m_pWndEdit->DestroyWindow ();
delete m_pWndEdit;
m_pWndEdit = NULL;
}
}
if (pWndParent == NULL || pWndParent->GetSafeHwnd () == NULL)
{
return;
}
BOOL bDisabled = CBCGPToolBar::IsCustomizeMode () || (m_nStyle & TBBS_DISABLED);
CRect rect = m_rect;
rect.InflateRect (-2, 0);
rect.bottom = rect.top + m_nDropDownHeight;
if ((m_pWndCombo = CreateCombo (pWndParent, rect)) == NULL)
{
ASSERT (FALSE);
return;
}
if (m_pWndCombo != NULL && m_pWndCombo->GetSafeHwnd () != NULL)
{
m_pWndCombo->EnableWindow (!bDisabled);
m_pWndCombo->RedrawWindow ();
}
if (m_bFlat && (m_pWndCombo->GetStyle () & CBS_DROPDOWNLIST) == CBS_DROPDOWN)
{
m_pWndEdit = new CBCGPComboEdit (*this);
DWORD dwEditStyle = WS_CHILD | WS_VISIBLE | ES_WANTRETURN | ES_AUTOHSCROLL;
if (m_pWndCombo->GetStyle () & WS_TABSTOP)
{
dwEditStyle |= WS_TABSTOP;
}
m_pWndEdit->Create (dwEditStyle, rect, pWndParent, m_nID);
m_pWndEdit->SetFont (&globalData.fontRegular);
m_pWndEdit->SetOwner (m_pWndCombo->GetParent ()->GetOwner ());
if (m_pWndEdit != NULL && m_pWndEdit->GetSafeHwnd () != NULL)
{
m_pWndEdit->EnableWindow (!bDisabled);
m_pWndEdit->RedrawWindow ();
}
}
AdjustRect ();
m_pWndCombo->SetFont (&globalData.fontRegular);
if (m_pWndCombo->GetCount () > 0)
{
m_lstItems.RemoveAll ();
ClearData ();
m_lstItemData.RemoveAll ();
for (int i = 0; i < m_pWndCombo->GetCount (); i ++)
{
CString str;
m_pWndCombo->GetLBText (i, str);
m_lstItems.AddTail (str);
m_lstItemData.AddTail (m_pWndCombo->GetItemData (i));
}
m_iSelIndex = m_pWndCombo->GetCurSel ();
}
else
{
m_pWndCombo->ResetContent ();
ASSERT (m_lstItemData.GetCount () == m_lstItems.GetCount ());
POSITION posData = m_lstItemData.GetHeadPosition ();
for (POSITION pos = m_lstItems.GetHeadPosition (); pos != NULL;)
{
ASSERT (posData != NULL);
CString strItem = m_lstItems.GetNext (pos);
int iIndex = m_pWndCombo->AddString (strItem);
m_pWndCombo->SetItemData (iIndex, m_lstItemData.GetNext (posData));
}
if (m_iSelIndex != CB_ERR)
{
m_pWndCombo->SetCurSel (m_iSelIndex);
}
}
if (m_iSelIndex != CB_ERR &&
m_iSelIndex < m_pWndCombo->GetCount ())
{
m_pWndCombo->GetLBText (m_iSelIndex, m_strEdit);
m_pWndCombo->SetWindowText (m_strEdit);
if (m_pWndEdit != NULL)
{
m_pWndEdit->SetWindowText (m_strEdit);
}
}
}
//**************************************************************************************
int CBCGPToolbarComboBoxButton::AddItem (LPCTSTR lpszItem, DWORD dwData)
{
ASSERT (lpszItem != NULL);
if (m_strEdit.IsEmpty ())
{
m_strEdit = lpszItem;
if (m_pWndEdit != NULL)
{
m_pWndEdit->SetWindowText (m_strEdit);
}
}
if (m_lstItems.Find (lpszItem) == NULL)
{
m_lstItems.AddTail (lpszItem);
m_lstItemData.AddTail (dwData);
}
if (m_pWndCombo->GetSafeHwnd () != NULL)
{
int iIndex = m_pWndCombo->FindStringExact (-1, lpszItem);
if (iIndex == CB_ERR)
{
iIndex = m_pWndCombo->AddString (lpszItem);
}
m_pWndCombo->SetCurSel (iIndex);
m_pWndCombo->SetItemData (iIndex, dwData);
m_pWndCombo->SetEditSel (-1, 0);
}
return m_lstItems.GetCount () - 1;
}
int CBCGPToolbarComboBoxButton::AddItemHead(LPCTSTR lpszItem, DWORD dwData)
{
ASSERT (lpszItem != NULL);
if (m_strEdit.IsEmpty ())
{
m_strEdit = lpszItem;
if (m_pWndEdit != NULL)
{
m_pWndEdit->SetWindowText (m_strEdit);
}
}
// remove existed items from list
POSITION pos;
if ( ( pos = m_lstItems.Find (lpszItem) ) != NULL )
{
m_lstItems.RemoveAt(pos);
}
if ( ( pos = m_lstItemData.Find(dwData) ) != NULL )
{
m_lstItemData.RemoveAt(pos);
}
// add item at head of list
m_lstItems.AddHead(lpszItem);
m_lstItemData.AddHead(dwData);
if (m_pWndCombo->GetSafeHwnd () != NULL)
{
int iIndex = m_pWndCombo->FindStringExact (-1, lpszItem);
if (iIndex != CB_ERR)
{
m_pWndCombo->DeleteString(iIndex);
}
iIndex = m_pWndCombo->InsertString(0, lpszItem);
m_pWndCombo->SetCurSel (iIndex);
m_pWndCombo->SetItemData (iIndex, dwData);
m_pWndCombo->SetEditSel (-1, 0);
}
return 0;
}
//**************************************************************************************
LPCTSTR CBCGPToolbarComboBoxButton::GetItem (int iIndex) const
{
if (iIndex == -1) // Current selection
{
if (m_pWndCombo->GetSafeHwnd () == NULL)
{
if ((iIndex = m_iSelIndex) == -1)
{
return 0;
}
}
else
{
iIndex = m_pWndCombo->GetCurSel ();
}
}
POSITION pos = m_lstItems.FindIndex (iIndex);
if (pos == NULL)
{
return NULL;
}
return m_lstItems.GetAt (pos);
}
//**************************************************************************************
DWORD CBCGPToolbarComboBoxButton::GetItemData (int iIndex) const
{
if (iIndex == -1) // Current selection
{
if (m_pWndCombo->GetSafeHwnd () == NULL)
{
if ((iIndex = m_iSelIndex) == -1)
{
return 0;
}
}
else
{
iIndex = m_pWndCombo->GetCurSel ();
}
}
if (m_pWndCombo->GetSafeHwnd () != NULL)
{
return m_pWndCombo->GetItemData (iIndex);
}
else
{
POSITION pos = m_lstItemData.FindIndex (iIndex);
if (pos == NULL)
{
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -