Click here to Skip to main content
15,897,291 members
Articles / Desktop Programming / MFC

An MFC-CListCtrl derived class that allows other ‘controls’ to be inserted into a particular cell

Rate me:
Please Sign up or sign in to vote.
4.92/5 (54 votes)
5 Jan 2014CPOL12 min read 168.6K   14K   160  
A class derived from CListCtrl that allows edit controls, combo boxes, check boxes, date pickers, and color pickers to be inserted into or removed from particular cells extremely easily. The inserted 'controls' are not CWnd-derived.
#pragma once

#include "CellSpinCtrl.h"
#include "..\utilities.h"

#define CAPHOUR_CH		_T('H')
#define HOUR_CH			_T('h')
#define MIN_CH			_T('m')
#define SEC_CH			_T('s')
#define T_CH			_T('t')

class CCellTimeCtrl : public CCellSpinCtrl
{
	static BOOL		m_SystemTimeDetailsInitialized;
	static CString	m_SystemTimeFormat;
	static CString	m_AM, m_PM; 
	static BOOL CALLBACK InitSystemTimeFormatProc(LPTSTR lpSystemTimeFormat);
	static void InitializeSystemTimeDetails();
	CString	m_TimeFormat;

	inline const CString& GetTimeFormat() const;
	inline BOOL IsCharTag(TCHAR chr) const;
public:
	CCellTimeCtrl(void);
	virtual ~CCellTimeCtrl(void);
	void Initialize(HWND hParentWnd, BOOL *pParentEnabled, LPCTSTR strText  = _T(""));
	void SetTimeFormat(const CString& NewTimeFomat);
	int GetMinWidth();
protected:
	virtual void OnIncrement();
	virtual void OnDecrement();
	virtual void DrawTextArea(CDC *pDC, const LPRECT prcCell, BOOL bHighlighted);
	virtual BOOL OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	virtual BOOL OnLButtonDown(UINT nFlags, CPoint point);
	virtual BOOL OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	virtual void OnKillActive();

	enum TimeTagType {HHour, Hour, hhour, hour, mminute, minute, ssecond, second, tt, t, separator};
	struct CellTimeInfo
	{
		TimeTagType					m_TimeTagType;
		CString						m_Value;
		RECT						m_rcBound;
		BOOL						m_bIsInput;
	};
	CArray<CellTimeInfo> m_CellTimeInfoArray;

	inline WORD MinTimeValue(const CellTimeInfo &CTI) const;
	inline WORD MaxTimeValue(const CellTimeInfo &CTI) const;
	inline BOOL IsNumericField(const CellTimeInfo &CTI) const;

	void IncrementTime(BOOL Increment = TRUE);
	void UpdateTimeValue(TimeTagType TTT, int Value);
	void PopulateArrayTimeTags();
	void PopulateArrayTimeValues();
	void UpdateCellTimeInfoValue(CellTimeInfo &CTI);
	void IncrementTimeValue(TimeTagType TTT, int Value);
	void DecrementTimeValue(TimeTagType TTT, int Value);
	void OnKillActiveCellInput();
	void UpdateTextTime();
	void ComputeCellTimeInfoRect(CDC *pDC, CellTimeInfo &CTI, LONG &Incr);

	SYSTEMTIME m_Time;
	CellTimeInfo				*m_pActiveCellTimeInfo;  
};

inline const CString& CCellTimeCtrl::GetTimeFormat() const
{
	return (m_TimeFormat.IsEmpty()? m_SystemTimeFormat : m_TimeFormat);
}

inline BOOL CCellTimeCtrl::IsCharTag(TCHAR chr) const
{
	return chr == CAPHOUR_CH || chr == HOUR_CH || chr == MIN_CH || chr == SEC_CH || chr == T_CH;
}

inline WORD CCellTimeCtrl::MinTimeValue(const CellTimeInfo &CTI) const
{
	return 0;
}

inline WORD CCellTimeCtrl::MaxTimeValue(const CellTimeInfo &CTI) const
{
	switch (CTI.m_TimeTagType)
	{
		case HHour:
		case Hour:
		case hhour:
		case hour:
			return 23;
		default:
			return 59;
	}
}

inline BOOL CCellTimeCtrl::IsNumericField(const CellTimeInfo &CTI) const
{
	return (CTI.m_TimeTagType == HHour || CTI.m_TimeTagType == Hour || CTI.m_TimeTagType == hhour || CTI.m_TimeTagType == hour
		|| CTI.m_TimeTagType == mminute || CTI.m_TimeTagType == minute || CTI.m_TimeTagType == ssecond || CTI.m_TimeTagType == second);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions