Click here to Skip to main content
12,635,630 members (25,744 online)
Click here to Skip to main content

Stats

104.6K views
4.5K downloads
35 bookmarked
Posted

Tree control templates

, 11 Jun 2001 CPOL
Three tree control templates to easily operate with CTreeCtrl derived objects
/////////////////////////////////////////////////////////////////////
// Template: TTCPersist
// Purpose : Template to allow CTreeCtrl derived objects state to
// 				survive after a reload of the control
// Created : SEN (1- Jun - 2001)
// History : None
// 
// Copyright (c) 2001 by Francisco Jos� Sen del Prado
// All rights reserved.
/////////////////////////////////////////////////////////////////////


// TCPersist.h : header file
//

#if !defined(_TCPERSIST_H__INCLUDED_)
#define _TCPERSIST_H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXTEMPL_H__
	#pragma message ("To avoid this warning message place #include <afxtempl.h> in your stdafx.h precompiler headers file")
	#include <afxtempl.h>
#endif

/////////////////////////////////////////////////////////////////////////////
// TTCPersist (Declaration)

template <class T>
class TTCPersist : public T
{
// Construction
public:
	TTCPersist();

// Operations
public:
	void SaveStatus();
	void RestoreStatus();
	void ResetStatus();

// Implementation
private:
	CString GetItemPath(HTREEITEM hti);

	CString		m_strFirstVisible;
	HTREEITEM	m_htiFirstVisible;

	CMapStringToString	m_strmExpanded,
						m_strmSelected;

	void SaveStatus_R	(HTREEITEM hItem);
	void RestoreStatus_R(HTREEITEM hItem);
};


/////////////////////////////////////////////////////////////////////////////
// TTCPersist (Implementation)

template <class T>
TTCPersist<T>::TTCPersist()
{
	m_htiFirstVisible= NULL;
}

template <class T>
void TTCPersist<T>::SaveStatus()
{
	m_strmExpanded.RemoveAll();
	m_strmSelected.RemoveAll();

	// First visible
	m_strFirstVisible= GetItemPath(GetFirstVisibleItem());

	SaveStatus_R(GetRootItem());
}

template <class T>
void TTCPersist<T>::RestoreStatus()
{
	m_htiFirstVisible= NULL;

	RestoreStatus_R(GetRootItem());

	// First visible
	if (m_htiFirstVisible != NULL)
	{
		SelectSetFirstVisible(m_htiFirstVisible);
	}
}

template <class T>
void TTCPersist<T>::ResetStatus()
{
	m_strmExpanded.RemoveAll();
	m_strmSelected.RemoveAll();

	// First visible
	m_strFirstVisible.Empty();
	m_htiFirstVisible= NULL;
}

template <class T>
CString TTCPersist<T>::GetItemPath(HTREEITEM hti)
{
	CString strRet;

	if (hti != NULL)
	{
		CString strAux= GetItemText(hti),
				strAux2= GetItemPath(GetParentItem(hti));

		if (strAux2.IsEmpty())
			strRet= strAux;
		else
			strRet= strAux2 + "\\" + strAux;
	}

	return strRet;
}

template <class T>
void TTCPersist<T>::SaveStatus_R(HTREEITEM hItem)
{
	if (hItem != NULL)
	{
		TRACE("Element: %s\n", (LPCSTR)GetItemPath(hItem));

		UINT uState= GetItemState(hItem, TVIS_EXPANDED | TVIS_SELECTED);

		if ((uState & TVIS_SELECTED) != 0)
		{
			CString strAux= GetItemPath(hItem);

			m_strmSelected[strAux]= "";
		}

		if (ItemHasChildren(hItem))
		{
			if ((uState & TVIS_EXPANDED) != 0)
			{
				CString strAux= GetItemPath(hItem);

				m_strmExpanded[strAux]= "";
			}

			// Recursive call
			SaveStatus_R(GetChildItem(hItem));
		}
	}

	// Recursive call
	if (GetNextSiblingItem(hItem) != NULL)
		SaveStatus_R(GetNextSiblingItem(hItem));
}

template <class T>
void TTCPersist<T>::RestoreStatus_R(HTREEITEM hItem)
{
	if (hItem != NULL)
	{
		CString strItem= GetItemPath(hItem),
				strAux;

		if (strItem == m_strFirstVisible)
		{
			ASSERT(m_htiFirstVisible == NULL);

			m_htiFirstVisible= hItem;
		}

		if (m_strmSelected.Lookup(strItem, strAux))
		{
			SelectItem(hItem);
		}

		if (m_strmExpanded.Lookup(strItem, strAux))
		{
			Expand(hItem, TVE_EXPAND);

			if (ItemHasChildren(hItem))
			{
				// Recursive call
				RestoreStatus_R(GetChildItem(hItem));
			}
		}
	}

	// Recursive call
	if (GetNextSiblingItem(hItem) != NULL)
		RestoreStatus_R(GetNextSiblingItem(hItem));
}

#endif // !defined(_TCPERSIST_H__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)

Share

About the Author

Francisco José Sen del Prado
Software Developer (Senior)
Spain Spain
I started programming in Basic language when I was 14 years old. The second programming language I learned was Z80 assembler. I've been programming with C++ for the last 20 years. I attended the 2012 C++ Now event in Aspen and it was the most exciting experience I've lived in my professional life. I met a lot of brilliant C++ minds and I have very good memories from that trip.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 12 Jun 2001
Article Copyright 2001 by Francisco José Sen del Prado
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid