//------------------------------------------------------------------------
//! Overrides OnEditBegin() to provide a CComboBox cell value editor
//!
//! @param owner The list control starting edit
//! @param nRow The index of the row for the cell to edit
//! @param nCol The index of the column for the cell to edit
//! @param pt The position clicked, in client coordinates.
//! @return Pointer to the cell editor to use (NULL if cell edit is not possible)
//------------------------------------------------------------------------
CWnd* CGridColumnTraitCombo::OnEditBegin(CGridListCtrlEx& owner, int nRow, int nCol, CPoint pt)
{
	// Check if the user clicked the cell icon (or the label-column checkbox)
	CRect labelRect;
	if (owner.GetCellRect(nRow, nCol, LVIR_LABEL, labelRect) && !labelRect.PtInRect(pt))
		return CGridColumnTraitImage::OnEditBegin(owner, nRow, nCol, pt);

	// Get position of the cell to edit
	CRect rectCell = GetCellEditRect(owner, nRow, nCol);

	// Create edit control to edit the cell
	//	- Stores the pointer, so elements can be dynamically added later
	m_pComboBox = CreateComboBox(owner, nRow, nCol, rectCell);
	VERIFY(m_pComboBox!=NULL);

	// Add all items to list
	if (m_ComboList.GetSize()>0)
	{
		LoadList(m_ComboList, -1);

		// Guess the currently selected item in the list
		CString item = owner.GetItemText(nRow, nCol);
		int nCurSel = m_pComboBox->FindString(-1, item);
		if (nCurSel!=-1)
			m_pComboBox->SetCurSel(nCurSel);
		else
			m_pComboBox->SetWindowText(item);
	}
	else
	{
		CString item = owner.GetItemText(nRow, nCol);
		m_pComboBox->SetWindowText(item);
	}

	// Adjust the item-height to font-height (Must be done after resizing)
	CRect rectCombo;
	m_pComboBox->GetClientRect(&rectCombo);
	if (rectCombo.Height() < rectCell.Height())
	{
		// Expand to fit cell
		int padding = rectCell.Height() - rectCombo.Height();
		m_pComboBox->SetItemHeight(-1, m_pComboBox->GetItemHeight(-1)+padding);
	}
	else
	if (rectCombo.Height() > rectCell.Height() + ::GetSystemMetrics(SM_CXBORDER))
	{
		// Compress to fit cell
		int margin = rectCombo.Height() - m_pComboBox->GetItemHeight(-1);
		int padding = margin - 2*::GetSystemMetrics(SM_CXEDGE);
		if ((m_pComboBox->GetStyle() & CBS_DROPDOWNLIST) == CBS_DROPDOWNLIST)
			padding -= ::GetSystemMetrics(SM_CXEDGE);
		if (padding > 0)
			m_pComboBox->SetItemHeight(-1, m_pComboBox->GetItemHeight(-1)-padding);
	}
	return m_pComboBox;
}
Example #2
0
//------------------------------------------------------------------------
//! Overrides OnEditBegin() to provide a CEdit cell value editor
//!
//! @param owner The list control starting edit
//! @param nRow The index of the row for the cell to edit
//! @param nCol The index of the column for the cell to edit
//! @return Pointer to the cell editor to use (NULL if cell edit is not possible)
//------------------------------------------------------------------------
CWnd* CGridColumnTraitEdit::OnEditBegin(CGridListCtrlEx& owner, int nRow, int nCol)
{
	// Get position of the cell to edit
	CRect rectCell = GetCellEditRect(owner, nRow, nCol);

	// Get the text-style of the cell to edit
	DWORD dwStyle = m_EditStyle;
	HDITEM hditem = {0};
	hditem.mask = HDI_FORMAT;
	VERIFY( owner.GetHeaderCtrl()->GetItem(nCol, &hditem) );
	if (hditem.fmt & HDF_CENTER)
		dwStyle |= ES_CENTER;
	else if (hditem.fmt & HDF_RIGHT)
		dwStyle |= ES_RIGHT;
	else
		dwStyle |= ES_LEFT;

	// Create edit control to edit the cell
	CEdit* pEdit = CreateEdit(owner, nRow, nCol, dwStyle, rectCell);
	VERIFY(pEdit!=NULL);
	if (pEdit==NULL)
		return NULL;

	// Configure font
	pEdit->SetFont(owner.GetCellFont());

	// First column (Label) doesn't have a margin when imagelist is assigned
	if (nCol==0 && owner.GetImageList(LVSIL_SMALL)!=NULL)
		pEdit->SetMargins(0, 0);
	else
	// First column (Label) doesn't have a margin when checkboxes are enabled
	if (nCol==0 && owner.GetExtendedStyle() & LVS_EX_CHECKBOXES)
		pEdit->SetMargins(1, 0);
	else
	// Label column doesn't have margin when not first in column order
	if (nCol==0 && owner.GetFirstVisibleColumn()!=nCol)
		pEdit->SetMargins(1, 0);
	else
	if (dwStyle & ES_CENTER)
		pEdit->SetMargins(0, 0);
	else
	if (dwStyle & ES_RIGHT)
		pEdit->SetMargins(0, 7);
	else
		pEdit->SetMargins(4, 0);

	if (m_EditLimitText!=UINT_MAX)
		pEdit->SetLimitText(m_EditLimitText);

	CString cellText = owner.GetItemText(nRow, nCol);
	pEdit->SetWindowText(cellText);
	pEdit->SetSel(0, -1, 0);
	return pEdit;
}
//------------------------------------------------------------------------
//! Overrides OnEditBegin() to provide a CEdit cell value editor
//!
//! @param owner The list control starting edit
//! @param nRow The index of the row for the cell to edit
//! @param nCol The index of the column for the cell to edit
//! @return Pointer to the cell editor to use (NULL if cell edit is not possible)
//------------------------------------------------------------------------
CWnd* CGridColumnTraitEdit::OnEditBegin(CGridListCtrlEx& owner, int nRow, int nCol)
{
	// Get position of the cell to edit
	CRect rcItem = GetCellEditRect(owner, nRow, nCol);

	// Create edit control to edit the cell
	CEdit* pEdit = CreateEdit(owner, nRow, nCol, rcItem);
	VERIFY(pEdit!=NULL);

	pEdit->SetWindowText(owner.GetItemText(nRow, nCol));
	pEdit->SetSel(0, -1, 0);

	return pEdit;
}
//------------------------------------------------------------------------
//! Overrides OnEditBegin() to provide a CDateTimeCtrl cell value editor
//!
//! @param owner The list control starting edit
//! @param nRow The index of the row for the cell to edit
//! @param nCol The index of the column for the cell to edit
//! @return Pointer to the cell editor to use (NULL if cell edit is not possible)
//------------------------------------------------------------------------
CWnd* CGridColumnTraitDateTime::OnEditBegin(CGridListCtrlEx& owner, int nRow, int nCol)
{
	// Convert cell-text to date/time format
	CString cellText = owner.GetItemText(nRow, nCol);
	COleDateTime dateTime;
	ParseDateTime(cellText, dateTime);

	// Get position of the cell to edit
	CRect rectCell = GetCellEditRect(owner, nRow, nCol);

	// Get the text-style of the cell to edit
	DWORD dwStyle = m_DateTimeCtrlStyle;
	HDITEM hd = {0};
	hd.mask = HDI_FORMAT;
	VERIFY( owner.GetHeaderCtrl()->GetItem(nCol, &hd) );
	if (hd.fmt & HDF_RIGHT)
		dwStyle |= DTS_RIGHTALIGN;

	// Create control to edit the cell
	CDateTimeCtrl* pDateTimeCtrl = CreateDateTimeCtrl(owner, nRow, nCol, dwStyle, rectCell);
	VERIFY(pDateTimeCtrl!=NULL);
	if (pDateTimeCtrl==NULL)
		return NULL;

	// Configure font
	pDateTimeCtrl->SetFont(owner.GetCellFont());

	// Configure datetime format
	if (!m_Format.IsEmpty())
		pDateTimeCtrl->SetFormat(m_Format);

	pDateTimeCtrl->SetTime(dateTime);

	// Check with the original string
	CString timeText;
	pDateTimeCtrl->GetWindowText(timeText);
	if (cellText!=timeText)
	{
		dateTime.SetDateTime(1970, 1, 1, 0, 0, 0);
		pDateTimeCtrl->SetTime(dateTime);
	}

	return pDateTimeCtrl;
}
//------------------------------------------------------------------------
//! Overrides OnEditBegin() to provide a CEdit cell value editor
//!
//! @param owner The list control starting edit
//! @param nRow The index of the row for the cell to edit
//! @param nCol The index of the column for the cell to edit
//! @return Pointer to the cell editor to use (NULL if cell edit is not possible)
//------------------------------------------------------------------------
CWnd* CGridColumnTraitEdit::OnEditBegin(CGridListCtrlEx& owner, int nRow, int nCol)
{
	// Get position of the cell to edit
	CRect rectCell = GetCellEditRect(owner, nRow, nCol);

	// Get the text-style of the cell to edit
	DWORD dwStyle = m_EditStyle;
	HDITEM hditem = {0};
	hditem.mask = HDI_FORMAT;
	VERIFY( owner.GetHeaderCtrl()->GetItem(nCol, &hditem) );
	if (hditem.fmt & HDF_CENTER)
		dwStyle |= ES_CENTER;
	else if (hditem.fmt & HDF_RIGHT)
		dwStyle |= ES_RIGHT;
	else
		dwStyle |= ES_LEFT;

	// Create edit control to edit the cell
	CEdit* pEdit = CreateEdit(owner, nRow, nCol, dwStyle, rectCell);
	VERIFY(pEdit!=NULL);
	if (pEdit==NULL)
		return NULL;

	// Configure font
	pEdit->SetFont(owner.GetCellFont());

	// First item (Label) doesn't have a margin (Subitems does)
	if (nCol==0 || (dwStyle & ES_CENTER))
		pEdit->SetMargins(0, 0);
	else
	if (dwStyle & ES_RIGHT)
		pEdit->SetMargins(0, 7);
	else
		pEdit->SetMargins(4, 0);

	if (m_EditLimitText!=UINT_MAX)
		pEdit->SetLimitText(m_EditLimitText);

	CString cellText = owner.GetItemText(nRow, nCol);
	pEdit->SetWindowText(cellText);
	pEdit->SetSel(0, -1, 0);
	return pEdit;
}