📄 bcgtoolbarcomboboxbutton.cpp
字号:
//*******************************************************************************
// 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>
//*******************************************************************************
// BCGToolbarComboBoxButton.cpp: implementation of the CBCGToolbarComboBoxButton class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BCGToolbar.h"
#include "globals.h"
#include "BCGToolbarComboBoxButton.h"
#include "MenuImages.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_SERIAL(CBCGToolbarComboBoxButton, CBCGToolbarButton, 1)
static const int iComboHeight = 150;
static const int iDefaultSize = 150;
static const int iHorzMargin = 3;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBCGToolbarComboBoxButton::CBCGToolbarComboBoxButton()
{
m_dwStyle = WS_CHILD | WS_VISIBLE | CBS_NOINTEGRALHEIGHT | CBS_DROPDOWNLIST | WS_VSCROLL;
m_iWidth = iDefaultSize;
Initialize ();
}
//**************************************************************************************
CBCGToolbarComboBoxButton::CBCGToolbarComboBoxButton (UINT uiId,
int iImage,
DWORD dwStyle,
int iWidth) :
CBCGToolbarButton (uiId, iImage)
{
m_dwStyle = dwStyle | WS_CHILD | WS_VISIBLE | WS_VSCROLL;
m_iWidth = (iWidth == 0) ? iDefaultSize : iWidth;
Initialize ();
}
//**************************************************************************************
void CBCGToolbarComboBoxButton::Initialize ()
{
m_iSelIndex = -1;
m_pWndCombo = NULL;
m_bHorz = TRUE;
}
//**************************************************************************************
CBCGToolbarComboBoxButton::~CBCGToolbarComboBoxButton()
{
if (m_pWndCombo != NULL)
{
m_pWndCombo->DestroyWindow ();
delete m_pWndCombo;
}
}
//**************************************************************************************
void CBCGToolbarComboBoxButton::CopyFrom (const CBCGToolbarButton& s)
{
CBCGToolbarButton::CopyFrom (s);
POSITION pos;
m_lstItems.RemoveAll ();
const CBCGToolbarComboBoxButton& src = (const CBCGToolbarComboBoxButton&) s;
for (pos = src.m_lstItems.GetHeadPosition (); pos != NULL;)
{
m_lstItems.AddTail (src.m_lstItems.GetNext (pos));
}
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;
}
//**************************************************************************************
void CBCGToolbarComboBoxButton::Serialize (CArchive& ar)
{
CBCGToolbarButton::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;
m_lstItems.Serialize (ar);
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;
if (m_pWndCombo != NULL)
{
m_lstItems.RemoveAll ();
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 CBCGToolbarComboBoxButton::OnCalculateSize (CDC* pDC, const CSize& sizeDefault, BOOL bHorz)
{
m_bHorz = bHorz;
m_sizeText = CSize (0, 0);
if (bHorz)
{
if (m_pWndCombo->GetSafeHwnd () != NULL && !m_bIsHidden)
{
m_pWndCombo->ShowWindow (SW_SHOWNOACTIVATE);
}
//----------------
// By Guy Hachlili
//----------------
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 ();
}
return CSize (m_iWidth, sizeDefault.cy + m_sizeText.cy);
}
else
{
if (m_pWndCombo->GetSafeHwnd () != NULL)
{
m_pWndCombo->ShowWindow (SW_HIDE);
}
return CBCGToolbarButton::OnCalculateSize (pDC, sizeDefault, bHorz);
}
}
//**************************************************************************************
void CBCGToolbarComboBoxButton::OnMove ()
{
if (m_pWndCombo->GetSafeHwnd () == NULL ||
(m_pWndCombo->GetStyle () & WS_VISIBLE) == 0)
{
return;
}
CRect rectCombo;
m_pWndCombo->GetWindowRect (rectCombo);
m_pWndCombo->SetWindowPos (NULL,
m_rect.left + iHorzMargin,
m_rect.top + (m_rect.Height () - m_sizeText.cy - rectCombo.Height ()) / 2,
m_rect.Width () - 2 * iHorzMargin, iComboHeight,
SWP_NOZORDER | SWP_NOACTIVATE);
m_pWndCombo->SetEditSel (-1, 0);
AdjustRect ();
}
//**************************************************************************************
void CBCGToolbarComboBoxButton::OnSize (int iSize)
{
m_iWidth = iSize;
m_rect.right = m_rect.left + m_iWidth;
if (m_pWndCombo->GetSafeHwnd () != NULL &&
(m_pWndCombo->GetStyle () & WS_VISIBLE))
{
m_pWndCombo->SetWindowPos (NULL,
m_rect.left + iHorzMargin, m_rect.top,
m_rect.Width () - 2 * iHorzMargin, iComboHeight,
SWP_NOZORDER | SWP_NOACTIVATE);
m_pWndCombo->SetEditSel (-1, 0);
AdjustRect ();
}
}
//**************************************************************************************
void CBCGToolbarComboBoxButton::OnChangeParentWnd (CWnd* 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 (pWndParent == NULL || pWndParent->GetSafeHwnd () == NULL)
{
return;
}
CRect rect = m_rect;
rect.InflateRect (-2, 0);
rect.bottom = rect.top + iComboHeight;
if ((m_pWndCombo = CreateCombo (pWndParent, rect)) == NULL)
{
ASSERT (FALSE);
return;
}
AdjustRect ();
m_pWndCombo->SetFont (&globalData.fontRegular);
if (m_pWndCombo->GetCount () > 0)
{
m_lstItems.RemoveAll ();
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);
}
}
}
//**************************************************************************************
int CBCGToolbarComboBoxButton::AddItem (LPCTSTR lpszItem, DWORD dwData)
{
ASSERT (lpszItem != NULL);
if (m_strEdit.IsEmpty ())
{
m_strEdit = lpszItem;
}
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;
}
//**************************************************************************************
LPCTSTR CBCGToolbarComboBoxButton::GetItem (int iIndex) const
{
if (iIndex == -1) // Current selection
{
if (m_pWndCombo->GetSafeHwnd () == NULL)
{
return NULL;
}
iIndex = m_pWndCombo->GetCurSel ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -