Click here to Skip to main content
15,896,154 members
Articles / Desktop Programming / MFC

CFilterHeaderCtrl and CFilterListCtrl - Give user a chance to filter data you brought him!

Rate me:
Please Sign up or sign in to vote.
4.79/5 (23 votes)
11 May 2003CPOL2 min read 104.9K   1.7K   65  
An owner drawn header control and a CListControl that uses it
#if !defined(AFX_FILTERLISTCTRL_H__05FBF341_AD76_40E4_97C0_959D63420FA5__INCLUDED_)
#define AFX_FILTERLISTCTRL_H__05FBF341_AD76_40E4_97C0_959D63420FA5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// FilterListCtrl.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CFilterListCtrl window

// notify struct
struct NMFILTERHDR : public NMHDR
{
	char* szText; // read only!!!
};

// external notify messages
#define	FLCN_FILTERCHANGING		101
#define	FLCN_FILTERCHANGED		102
#define	FLCN_SHOWINGEDIT		103
#define	FLCN_BEGINFILTEREDIT	104
#define	FLCN_ENDFILTEREDIT		105

// const
#define FILTER_NONE				0
#define FILTER_ENABLED			1
#define FILTER_DISABLED			2

/////////////////////////////////////////////////////////////////////////////
// CFilterHeaderCtrl window

// Filter Edit Control ID
#define IDC_ED_EDIT		101

// internal messages
#define	FLM_EDITTEXTCHANGED		(WM_USER+100)

class CFilterHeaderCtrl : public CHeaderCtrl
{
	class CFilterEdit : public CEdit
	{
	// Construction
	public:
		CFilterEdit();

	// Attributes
	public:

		BOOL m_bNotifySent;

	// Operations
	public:

	// Overrides
		// ClassWizard generated virtual function overrides
		//{{AFX_VIRTUAL(CFilterEdit)
		public:
		virtual BOOL PreTranslateMessage(MSG* pMsg);
		virtual void PostNcDestroy( ); 
		//}}AFX_VIRTUAL

	// Implementation
	public:
		virtual ~CFilterEdit();

		// Generated message map functions
	protected:
		//{{AFX_MSG(CFilterEdit)
		afx_msg void OnKillFocus(CWnd* pNewWnd);
		//}}AFX_MSG

		DECLARE_MESSAGE_MAP()
	};

	class CFilterInfo
	{
	public:
		CFilterInfo()
		{
			m_nStatus=FILTER_ENABLED;
		};

		CString m_strFilter;
		int		m_nStatus;
	};

// Construction
public:
	CFilterHeaderCtrl();

// Attributes
public:

private:
	CFilterEdit*	m_pEdit;
	int m_nEditColumn;
	CFont	m_FilterFont;
	CString m_strFilterDisabled;
	CPtrArray m_arFilters;
	int m_nFont1Height;
	int m_nFont2Height;
	BOOL	m_bEndEditSent;

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CFilterHeaderCtrl)
	protected:
	virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
	//}}AFX_VIRTUAL

// Implementation
public:
	void SetFilterDisabledString(CString strText);
	void SetFilterStatus(int nIndex, UINT uStatus);

private:
	CFilterInfo* GetFilter(int nIndex)
	{
		return (CFilterInfo*)m_arFilters[nIndex];
	}

public:
	CString GetFilterText(int nIndex)
	{
		return GetFilter(nIndex)->m_strFilter;
	}

	int GetFilterStatus(int nIndex)
	{
		return GetFilter(nIndex)->m_nStatus;
	}

private:
	void HideEdit(BOOL bValidate);
	void ShowEdit(int nColumn);

public:
	int IndexToOrder(int nIndex);
	BOOL FilterEditing();
	void SetFilterFont(CFont* pFont);
	void CalcFontHeight();
	virtual ~CFilterHeaderCtrl();
	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

	// Generated message map functions
protected:
	//{{AFX_MSG(CFilterHeaderCtrl)
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg LRESULT OnEditTextChanged(WPARAM, LPARAM);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

class CFilterListCtrl : public CListCtrl
{
// Construction
public:
	CFilterListCtrl();

// Attributes
public:

private:
	CFilterHeaderCtrl	m_ctlHeader;

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CFilterListCtrl)
	protected:
	virtual void PreSubclassWindow();
	virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
	//}}AFX_VIRTUAL

// Implementation
public:

	int InsertColumn(int nCol, const LVCOLUMN* pColumn,int nFilter=FILTER_ENABLED);
	int InsertColumn(int nCol, LPCTSTR lpszColumnHeading,
		int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1,int nFilter=FILTER_ENABLED);

	virtual ~CFilterListCtrl();

	// Generated message map functions
protected:
	//{{AFX_MSG(CFilterListCtrl)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.


#endif // !defined(AFX_FILTERLISTCTRL_H__05FBF341_AD76_40E4_97C0_959D63420FA5__INCLUDED_)

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
Software Developer (Senior) Leonardo
Italy Italy
Hi Smile | :)
I was born in 1970 (Augusta - Italy).
I live in Taranto - Italy.
I work in Taranto - Italy.
I like computer science!!!
That's all!

Comments and Discussions