Click here to Skip to main content
15,894,343 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;

namespace BinaryComponents.VectorGraphics.Primitives
{
	public sealed class Container : VisualItem, IEnumerable<VisualItem>
	{
		public Container()
			: this( new Types.Point( 0, 0 ), null )
		{
		}

		public Container( Types.Point offset )
			: this( offset, null )
		{
		}

		public Container( Types.Point offset, Path clip )
		{
			if( offset == null )
			{
				throw new ArgumentNullException( "offset" );
			}

			_offset = offset;
			_clip = clip;
		}

		public Types.Point Offset
		{
			get
			{
				return _offset;
			}
			set
			{
				if( value == null )
				{
					throw new ArgumentNullException( "value" );
				}

				_offset = value;

				DirtyBounds();
			}
		}

		public void AddBack( VisualItem item )
		{
			if( item == null )
			{
				throw new ArgumentNullException( "item" );
			}
			if( item.Parent != null )
			{
				throw new ArgumentException( "Item is already parented.", "item" );
			}

			item.Parent = this;
			_items.Add( item );
			DirtyBounds();
		}

		public void AddFront( VisualItem item )
		{
			if( item == null )
			{
				throw new ArgumentNullException( "item" );
			}
			if( item.Parent != null )
			{
				throw new ArgumentException( "Item is already parented.", "item" );
			}

			item.Parent = this;
			_items.Insert( 0, item );
			DirtyBounds();
		}

		public void Replace( VisualItem from, VisualItem to )
		{
			if( from == null )
			{
				throw new ArgumentNullException( "from" );
			}

			int index = _items.IndexOf( from );

			if( index < 0 )
			{
				throw new ArgumentException( "Item not found.", "from" );
			}

			_items.RemoveAt( index );

			if( to != null )
			{
				_items.Insert( index, to );
			}
		}

		public override void Visit( Visitor visitor )
		{
			visitor.PreVisitVisualItem( this );
			visitor.VisitContainer( this );

			if( visitor.VisitContainerPreChildren( this ) )
			{
				foreach( VisualItem child in this )
				{
					child.Visit( visitor );
				}
				visitor.VisitContainerPostChildren( this );
			}

			visitor.PostVisitVisualItem( this );
		}

		public override VisualItem Copy()
		{
			Container container = new Container( _offset, _clip );

			foreach( VisualItem child in this )
			{
				container.AddBack( child.Copy() );
			}

			return container;
		}

		protected override Types.Rectangle CalculateBounds( Renderers.Renderer renderer )
		{
			List<Types.Rectangle> childBoundsList = new List<Types.Rectangle>();

			foreach( VisualItem child in this )
			{
				Types.Rectangle childBounds = child.GetBounds( renderer );

				childBounds = Types.Rectangle.Offset( childBounds, Offset );

				childBoundsList.Add( childBounds );
			}

			Types.Rectangle bounds = Types.Rectangle.Union( childBoundsList.ToArray() );

			return bounds;
		}

		#region IEnumerable<VisualItem> Members

		public IEnumerator<VisualItem> GetEnumerator()
		{
			return _items.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return _items.GetEnumerator();
		}

		#endregion

		private Types.Point _offset;
		private Path _clip;
		private List<VisualItem> _items = new List<VisualItem>();
	}
}

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