Click here to Skip to main content
15,886,362 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.7K   1.7K   65  
An owner drawn header control and a CListControl that uses it
// FilterListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "F03.h"
#include "FilterListCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFilterListCtrl

CFilterListCtrl::CFilterListCtrl()
{
}

CFilterListCtrl::~CFilterListCtrl()
{
	if(m_ctlHeader.m_hWnd)
		m_ctlHeader.UnsubclassWindow();
}


BEGIN_MESSAGE_MAP(CFilterListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CFilterListCtrl)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFilterListCtrl message handlers

void CFilterListCtrl::PreSubclassWindow() 
{
	m_ctlHeader.SubclassWindow(GetHeaderCtrl()->m_hWnd);

	CListCtrl::PreSubclassWindow();
}

int CFilterListCtrl::InsertColumn(int nCol, const LVCOLUMN* pColumn, int nFilter)
{
	int nRetVal=CListCtrl::InsertColumn(nCol,pColumn);

	if (nRetVal!=-1)
	{
		if (nFilter!=FILTER_NONE)
		{
			HDITEM hdr;
			memset(&hdr,0,sizeof(HDITEM));

			hdr.mask=HDI_FORMAT;
			m_ctlHeader.GetItem(nRetVal,&hdr);
			hdr.fmt|= HDF_OWNERDRAW;

			m_ctlHeader.SetItem(nRetVal,&hdr);

			m_ctlHeader.SetFilterStatus(nRetVal,nFilter);
		}
		else
			m_ctlHeader.SetFilterStatus(nRetVal,FILTER_NONE);
	}

	return nRetVal;
}

int CFilterListCtrl::InsertColumn(int nCol, LPCTSTR lpszColumnHeading,
	int nFormat , int nWidth , int nSubItem , int nFilter)
{
	int nRetVal=CListCtrl::InsertColumn(nCol,lpszColumnHeading,nFormat,nWidth,nSubItem);

	if (nRetVal!=-1)
	{
		if (nFilter!=FILTER_NONE)
		{
			HDITEM hdr;
			memset(&hdr,0,sizeof(HDITEM));

			hdr.mask=HDI_FORMAT;
			m_ctlHeader.GetItem(nRetVal,&hdr);
			hdr.fmt|= HDF_OWNERDRAW;

			m_ctlHeader.SetItem(nRetVal,&hdr);

			m_ctlHeader.SetFilterStatus(nRetVal,nFilter);
		}
		else
			m_ctlHeader.SetFilterStatus(nRetVal,FILTER_NONE);
	}

	return nRetVal;
}

BOOL CFilterListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
	BOOL bDefault=TRUE;

	if (GetHeaderCtrl())
	{
		if ((DWORD)wParam==(DWORD)GetHeaderCtrl()->m_hWnd)
		{
			NMHDR* pNM=(NMHDR*)lParam;

			if (pNM->code==FLCN_FILTERCHANGING ||
				pNM->code==FLCN_FILTERCHANGED ||
				pNM->code==FLCN_BEGINFILTEREDIT ||
				pNM->code==FLCN_ENDFILTEREDIT )
			{
				NMFILTERHDR* pNMF=(NMFILTERHDR*)lParam;
				NMFILTERHDR nmh;

				nmh.code=pNMF->code;
				nmh.hwndFrom=m_hWnd;
				nmh.idFrom=pNMF->idFrom;
				nmh.szText=pNMF->szText;

				if (GetParent())
				{
					*pResult=GetParent()->SendMessage(WM_NOTIFY,(WPARAM)m_hWnd,(LPARAM)&nmh);
					bDefault=FALSE;
				}
			}

			if (pNM->code==FLCN_SHOWINGEDIT)
			{
				CRect rc,rcWin;
				GetHeaderCtrl()->GetItemRect(pNM->idFrom,&rc);
				GetClientRect(rcWin);

				// scroll the window

				int nScrollBarGap=GetSystemMetrics(SM_CXVSCROLL);

				int nHScroll=GetScrollPos(SB_HORZ);

				if (rc.left<=nHScroll)
					Scroll(CSize(rc.left-nHScroll,0));
				else
				{
					if (rc.right>nHScroll+rcWin.Width()-nScrollBarGap)
					{
						if (rc.Width()>rcWin.Width()-nScrollBarGap)
							Scroll(CSize(rc.left-nHScroll,0));
						else
							Scroll(CSize(rc.right-(nHScroll+rcWin.Width())+nScrollBarGap*0,0));
					}
				}

				*pResult=0; // 0 continue editing, 1 stop editing

				bDefault=FALSE;
			}
		}
	}
	
	return (bDefault ? CListCtrl::OnNotify(wParam, lParam, pResult) : TRUE);
}


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