//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright ?2004  John Champion
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//=============================================================================
using System;
using System.Drawing;
using System.Collections.Generic;
namespace DrawGraph
{
	/// 
	/// A collection class containing a list of  objects
	/// to be displayed on the graph.
	/// 
	/// 
	///  John Champion 
	///  $Revision: 3.1 $ $Date: 2006/06/24 20:26:44 $ 
	[Serializable]
	public class GraphObjList : List, ICloneable
	{
	#region Constructors
		/// 
		/// Default constructor for the  collection class
		/// 
		public GraphObjList()
		{
		}
		/// 
		/// The Copy Constructor
		/// 
		/// The  object from which to copy
		public GraphObjList( GraphObjList rhs )
		{
			foreach ( GraphObj item in rhs )
				this.Add( (GraphObj) ((ICloneable)item).Clone() );
		}
		/// 
		/// Implement the  interface in a typesafe manner by just
		/// calling the typed version of 
		/// 
		/// A deep copy of this object
		object ICloneable.Clone()
		{
			return this.Clone();
		}
		/// 
		/// Typesafe, deep-copy clone method.
		/// 
		/// A new, independent copy of this class
		public GraphObjList Clone()
		{
			return new GraphObjList( this );
		}
		
	#endregion
	#region Methods
/*
		/// 
		/// Indexer to access the specified  object by its ordinal
		/// position in the list.
		/// 
		/// The ordinal position (zero-based) of the
		///  object to be accessed.
		/// A  object reference.
		public GraphObj this[ int index ]  
		{
			get { return( (GraphObj) List[index] ); }
			set { List[index] = value; }
		}
*/
		/// 
		/// Indexer to access the specified  object by its .
		/// Note that the  must be a  type for this method
		/// to work.
		/// 
		/// The  type tag to search for.
		/// A  object reference.
		/// 
		public GraphObj this[string tag]  
		{
			get
			{
				int index = IndexOfTag( tag );
				if ( index >= 0 )
					return( this[index]  );
				else
					return null;
			}
		}
/*
		/// 
		/// Add a  object to the 
		/// collection at the end of the list.
		/// 
		/// A reference to the  object to
		/// be added
		/// 
		public GraphObj Add( GraphObj item )
		{
			List.Add( item );
			return item;
		}
		/// 
		/// Insert a  object into the collection
		/// at the specified zero-based index location.
		/// 
		/// The zero-based index location for insertion.
		/// The  object that is to be
		/// inserted.
		/// 
		public void Insert( int index, GraphObj item )
		{
			List.Insert( index, item );
		}
*/
		/// 
		/// Return the zero-based position index of the
		///  with the specified .
		/// 
		/// In order for this method to work, the 
		/// property must be of type .
		/// The  tag that is in the
		///  attribute of the item to be found.
		/// 
		/// The zero-based index of the specified ,
		/// or -1 if the  is not in the list
		public int IndexOfTag( string tag )
		{
			int index = 0;
			foreach ( GraphObj p in this )
			{
				if ( p.Tag is string &&
							String.Compare( (string) p.Tag, tag, true ) == 0 )
					return index;
				index++;
			}
			return -1;
		}
		/// 
		/// Move the position of the object at the specified index
		/// to the new relative position in the list.
		/// For Graphic type objects, this method controls the
		/// Z-Order of the items.  Objects at the beginning of the list
		/// appear in front of objects at the end of the list.
		/// The zero-based index of the object
		/// to be moved.
		/// The relative number of positions to move
		/// the object.  A value of -1 will move the
		/// object one position earlier in the list, a value
		/// of 1 will move it one position later.  To move an item to the
		/// beginning of the list, use a large negative value (such as -999).
		/// To move it to the end of the list, use a large positive value.
		/// 
		/// The new position for the object, or -1 if the object
		/// was not found.
		public int Move( int index, int relativePos )
		{
			if ( index < 0 || index >= Count )
				return -1;
			GraphObj graphObj = this[index];
			this.RemoveAt( index );
			index += relativePos;
			if ( index < 0 )
				index = 0;
			if ( index > Count )
				index = Count;
			Insert( index, graphObj );
			return index;
		}
	#endregion
	#region Render Methods
		/// 
		/// Render text to the specified  device
		/// by calling the Draw method of each  object in
		/// the collection.
		/// 
		/// This method is normally only called by the Draw method
		/// of the parent  object.
		/// 
		/// 
		/// A graphic device object to be drawn into.  This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// 
		/// 
		/// A reference to the  object that is the parent or
		/// owner of this object.
		/// 
		/// 
		/// The scaling factor to be used for rendering objects.  This is calculated and
		/// passed down by the parent  object using the
		///  method, and is used to proportionally adjust
		/// font sizes, etc. according to the actual size of the graph.
		/// 
		/// A  enumeration that controls
		/// the placement of this  relative to other
		/// graphic objects.  The order of 's with the
		/// same  value is control by their order in
		/// this .
		public void Draw( Graphics g, PaneBase pane, float scaleFactor,
							ZOrder zOrder )
		{
			// Draw the items in reverse order, so the last items in the
			// list appear behind the first items (consistent with
			// CurveList)
			for ( int i=this.Count-1; i>=0; i-- )
			{
				GraphObj item = this[i];
				if ( item.ZOrder == zOrder && item.IsVisible )
				{
					Region region = null;
					if ( item.IsClippedToChartRect && pane is GraphPane )
					{
						region = g.Clip.Clone();
						g.SetClip( ((GraphPane)pane).Chart._rect );
					}
					item.Draw( g, pane, scaleFactor );
					if ( item.IsClippedToChartRect && pane is GraphPane )
						g.Clip = region;
				}
			}
		}
		/// 
		/// Determine if a mouse point is within any , and if so, 
		/// return the index number of the the .
		/// 
		/// The screen point, in pixel coordinates.
		/// 
		/// A reference to the  object that is the parent or
		/// owner of this object.
		/// 
		/// 
		/// A graphic device object to be drawn into.  This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// 
		/// 
		/// The scaling factor to be used for rendering objects.  This is calculated and
		/// passed down by the parent  object using the
		///  method, and is used to proportionally adjust
		/// font sizes, etc. according to the actual size of the graph.
		/// 
		/// The index number of the 
		///  that is under the mouse point.  The  object is
		/// accessible via the  indexer property.
		/// 
		/// true if the mouse point is within a  bounding
		/// box, false otherwise.
		/// 
		public bool FindPoint( PointF mousePt, PaneBase pane, Graphics g, float scaleFactor, out int index )
		{
			index = -1;
			
			// Search in reverse direction to honor the Z-order
			for ( int i=Count-1; i>=0; i-- )
			{
				if ( this[i].PointInBox( mousePt, pane, g, scaleFactor ) )
				{
					if ( ( index >= 0 && this[i].ZOrder > this[index].ZOrder ) || index < 0 )
						index = i;
				}
			}
			if ( index >= 0 )
				return true;
			else
				return false;
		}
		
	#endregion
	}
}