Click here to Skip to main content
15,895,462 members
Articles / Multimedia / GDI+

Customizable Tree Control with Animation Support

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
8 Apr 2008CPOL4 min read 108.1K   23   115  
A tree control implementation, allowing complete customization and animation support
/////////////////////////////////////////////////////////////////////////////
//
// (c) 2007 BinaryComponents Ltd.  All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace BinaryComponents.SuperTree.Internal
{
	internal sealed class AnimationRequests : IDisposable
	{
		internal AnimationRequests( ITreeEvents treeEvents )
		{
			_treeEvents = treeEvents;

			_timer.Interval = 100;
			_timer.Tick += new EventHandler( _timer_Tick );
		}

		#region IDisposable Members

		public void Dispose()
		{
			if( _timer != null )
			{
				_timer.Enabled = false;
				_timer.Tick -= new EventHandler( _timer_Tick );
				_timer.Dispose();
				_timer = null;
			}
		}

		#endregion

		internal void BeginAnimating()
		{
			++_allCount;

			UpdateTimer();
		}

		internal void EndAnimating()
		{
			Debug.Assert( _allCount > 0 );

			--_allCount;

			UpdateTimer();
		}

		internal void BeginAnimating( TreeNode treeNode, Rectangle subRect )
		{
			_toAdd.Add( new NodeAndSubRect( treeNode, subRect ) );
			
			UpdateTimer();
		}

		internal void EndAnimating( TreeNode treeNode )
		{
			_toRemove.Add( new NodeAndSubRect( treeNode, Rectangle.Empty ) );

			UpdateTimer();
		}

		private void DoAdd( NodeAndSubRect nodeAndSubRect )
		{
			CountAndSubRect countAndSubRect;

			if( !_nodeCounts.TryGetValue( nodeAndSubRect.TreeNode, out countAndSubRect ) )
			{
				countAndSubRect = new CountAndSubRect();
				countAndSubRect.SubRect = nodeAndSubRect.SubRect;

				_nodeCounts[nodeAndSubRect.TreeNode] = countAndSubRect;
			}

			++countAndSubRect.Count;

			countAndSubRect.SubRect = Rectangle.Union( countAndSubRect.SubRect, nodeAndSubRect.SubRect );

			UpdateTimer();
		}

		private void DoRemove( NodeAndSubRect nodeAndSubRect )
		{
			CountAndSubRect countAndSubRect;

			if( _nodeCounts.TryGetValue( nodeAndSubRect.TreeNode, out countAndSubRect ) )
			{
				--countAndSubRect.Count;

				if( countAndSubRect.Count <= 0 )
				{
					_nodeCounts.Remove( nodeAndSubRect.TreeNode );
				}
			}

			UpdateTimer();
		}

		private void UpdateTimer()
		{
			_timer.Enabled = (_allCount > 0 || _nodeCounts.Count > 0 || _toAdd.Count > 0 || _toRemove.Count > 0);
		}

		private void _timer_Tick( object sender, EventArgs e )
		{
			foreach( NodeAndSubRect nodeAndSubRect in _toAdd )
			{
				DoAdd( nodeAndSubRect );
			}
			_toAdd.Clear();

			bool needsUpdate = false;

			if( _allCount > 0 )
			{
				if( Invalidate != null )
				{
					Invalidate( this, EventArgs.Empty );
					needsUpdate = true;
				}
			}
			else
			{
				foreach( KeyValuePair<TreeNode, CountAndSubRect> kvp in _nodeCounts )
				{
					TreeNode treeNode = kvp.Key;
					CountAndSubRect countAndSubRect = kvp.Value;

					if( InvalidateTreeNode != null )
					{
						InvalidateTreeNode( this, new TreeNodeRectangleEventArgs( treeNode, countAndSubRect.SubRect ) );
						needsUpdate = true;
					}
				}
			}

			if( needsUpdate && Update != null )
			{
				Update( this, EventArgs.Empty );
			}

			foreach( NodeAndSubRect nodeAndSubRect in _toRemove )
			{
				DoRemove( nodeAndSubRect );
			}
			_toRemove.Clear();

			_treeEvents.UpdateAnimations();
		}

		private sealed class NodeAndSubRect
		{
			internal NodeAndSubRect( TreeNode treeNode, Rectangle subRect )
			{
				TreeNode = treeNode;
				SubRect = subRect;
			}

			internal TreeNode TreeNode;
			internal Rectangle SubRect;
		}

		private sealed class CountAndSubRect
		{
			internal int Count;
			internal Rectangle SubRect;
		}

		public event EventHandler Invalidate;
		public event TreeNodeRectangleEventHandler InvalidateTreeNode;
		public event EventHandler Update;

		private ITreeEvents _treeEvents;
		private int _allCount;
		private Dictionary<TreeNode, CountAndSubRect> _nodeCounts = new Dictionary<TreeNode, CountAndSubRect>();
		private Timer _timer = new Timer();
		private List<NodeAndSubRect> _toAdd = new List<NodeAndSubRect>(), _toRemove = new List<NodeAndSubRect>();
	}
}

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
Web Developer
United Kingdom United Kingdom
I'm currently working for a small start-up company, BinaryComponents Ltd, producing the FeedGhost RSS reader.

FeedGhost RSS Reader:
http://www.feedghost.com

Bespoke Software Development
http://www.binarycomponents.com

Comments and Discussions