//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright ?2007  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.Drawing.Drawing2D;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace DrawGraph
{
	/// 
	/// A class that handles the basic attributes of a line segment.
	/// 
	/// 
	/// This is the base class for  and  classes.
	/// 
	///  John Champion 
	///  $Revision: 3.2 $ $Date: 2007/03/17 18:43:44 $ 
	[Serializable]
	public class LineBase : ICloneable, ISerializable
	{
	#region Fields
		/// 
		/// Private field that stores the pen width for this line.
		/// Use the public property  to access this value.
		/// 
		internal float _width;
		/// 
		/// Private field that stores the  for this
		/// line.  Use the public
		/// property  to access this value.
		/// 
		internal DashStyle _style;
		/// 
		/// private field that stores the "Dash On" length for drawing the line.  Use the
		/// public property  to access this value.
		/// 
		internal float _dashOn;
		/// 
		/// private field that stores the "Dash Off" length for drawing the line.  Use the
		/// public property  to access this value.
		/// 
		internal float _dashOff;
		/// 
		/// Private field that stores the visibility of this line.  Use the public
		/// property  to access this value.
		/// 
		internal bool _isVisible;
		/// 
		/// private field that determines if the line is drawn using
		/// Anti-Aliasing capabilities from the  class.
		/// Use the public property  to access
		/// this value.
		/// 
		internal bool _isAntiAlias;
		/// 
		/// Private field that stores the color of this line.  Use the public
		/// property  to access this value.  If this value is
		/// false, the line will not be shown (but the  may
		/// still be shown).
		/// 
		internal Color _color;
		/// 
		/// Internal field that stores a custom  class.  This
		/// fill is used strictly for ,
		/// , ,
		/// and  calculations to determine
		/// the color of the line.
		/// 
		internal Fill _gradientFill;
	#endregion
	#region Defaults
		/// 
		/// A simple struct that defines the
		/// default property values for the  class.
		/// 
		public struct Default
		{
			/// 
			/// The default mode for displaying line segments (
			/// property).  True to show the line segments, false to hide them.
			/// 
			public static bool IsVisible = true;
			/// 
			/// The default width for line segments ( property).
			/// Units are points (1/72 inch).
			/// 
			public static float Width = 1;
			/// 
			/// The default value for the 
			/// property.
			/// 
			public static bool IsAntiAlias = false;
			/// 
			/// The default drawing style for line segments ( property).
			/// This is defined with the  enumeration.
			/// 
			public static DashStyle Style = DashStyle.Solid;
			/// 
			/// The default "dash on" size for drawing the line
			/// ( property). Units are in points (1/72 inch).
			/// 
			public static float DashOn = 1.0F;
			/// 
			/// The default "dash off" size for drawing the the line
			/// ( property). Units are in points (1/72 inch).
			/// 
			public static float DashOff = 1.0F;
			/// 
			/// The default color for the line.
			/// This is the default value for the  property.
			/// 
			public static Color Color = Color.Black;
		}
	#endregion
	#region Properties
		/// 
		/// The color of the .  Note that this color value can be
		/// overridden if the GradientFill.Type is one of the
		/// ,
		/// , ,
		/// and  types.
		/// 
		/// 
		public Color Color
		{
			get { return _color; }
			set { _color = value; }
		}
		/// 
		/// The style of the , defined as a  enum.
		/// This allows the line to be solid, dashed, or dotted.
		/// 
		/// 
		/// 
		/// 
		public DashStyle Style
		{
			get { return _style; }
			set { _style = value; }
		}
		/// 
		/// The "Dash On" mode for drawing the line.
		/// 
		/// 
		/// This is the distance, in points (1/72 inch), of the dash segments that make up
		/// the dashed grid lines.  This setting is only valid if 
		///  is set to .
		/// 
		/// The dash on length is defined in points (1/72 inch)
		/// 
		/// 
		/// .
		public float DashOn
		{
			get { return _dashOn; }
			set { _dashOn = value; }
		}
		/// 
		/// The "Dash Off" mode for drawing the line.
		/// 
		/// 
		/// This is the distance, in points (1/72 inch), of the spaces between the dash
		/// segments that make up the dashed grid lines.  This setting is only valid if 
		///  is set to .
		/// 
		/// The dash off length is defined in points (1/72 inch)
		/// 
		/// 
		/// .
		public float DashOff
		{
			get { return _dashOff; }
			set { _dashOff = value; }
		}
		/// 
		/// The pen width used to draw the , in points (1/72 inch)
		/// 
		/// 
		public float Width
		{
			get { return _width; }
			set { _width = value; }
		}
		/// 
		/// Gets or sets a property that shows or hides the .
		/// 
		/// true to show the line, false to hide it
		/// 
		public bool IsVisible
		{
			get { return _isVisible; }
			set { _isVisible = value; }
		}
		/// 
		/// Gets or sets a value that determines if the lines are drawn using
		/// Anti-Aliasing capabilities from the  class.
		/// 
		/// 
		/// If this value is set to true, then the 
		/// property will be set to  only while
		/// this  is drawn.  A value of false will leave the value of
		///  unchanged.
		/// 
		public bool IsAntiAlias
		{
			get { return _isAntiAlias; }
			set { _isAntiAlias = value; }
		}
		/// 
		/// Gets or sets a custom  class.
		/// 
		/// This fill is used strictly for ,
		/// , ,
		/// and  calculations to determine
		/// the color of the line.  It overrides the  property if
		/// one of the above  values are selected.
		/// 
		/// 
		public Fill GradientFill
		{
			get { return _gradientFill; }
			set { _gradientFill = value; }
		}
	#endregion
	#region Constructors
		/// 
		/// Default constructor that sets all  properties to default
		/// values as defined in the  class.
		/// 
		public LineBase()
			: this( Color.Empty )
		{
		}
		/// 
		/// Constructor that sets the color property to the specified value, and sets
		/// the remaining  properties to default
		/// values as defined in the  class.
		/// 
		/// The color to assign to this new Line object
		public LineBase( Color color )
		{
			_width = Default.Width;
			_style = Default.Style;
			_dashOn = Default.DashOn;
			_dashOff = Default.DashOff;
			_isVisible = Default.IsVisible;
			_color = color.IsEmpty ? Default.Color : color;
			_isAntiAlias = Default.IsAntiAlias;
			_gradientFill = new Fill( Color.Red, Color.White );
			_gradientFill.Type = FillType.None;
		}
		/// 
		/// The Copy Constructor
		/// 
		/// The LineBase object from which to copy
		public LineBase( LineBase rhs )
		{
			_width = rhs._width;
			_style = rhs._style;
			_dashOn = rhs._dashOn;
			_dashOff = rhs._dashOff;
			_isVisible = rhs._isVisible;
			_color = rhs._color;
			_isAntiAlias = rhs._isAntiAlias;
			_gradientFill = new Fill( rhs._gradientFill );
		}
		/// 
		/// Implement the  interface in a typesafe manner by just
		/// calling the typed version of Clone.
		/// 
		/// 
		/// Note that this method must be called with an explicit cast to ICloneable, and
		/// that it is inherently virtual.  For example:
		/// 
		/// ParentClass foo = new ChildClass();
		/// ChildClass bar = (ChildClass) ((ICloneable)foo).Clone();
		/// 
		/// Assume that ChildClass is inherited from ParentClass.  Even though foo is declared with
		/// ParentClass, it is actually an instance of ChildClass.  Calling the ICloneable implementation
		/// of Clone() on foo actually calls ChildClass.Clone() as if it were a virtual function.
		/// 
		/// A deep copy of this object
		object ICloneable.Clone()
		{
			throw new NotImplementedException( "Can't clone an abstract base type -- child types must implement ICloneable" );
			//return new PaneBase( this );
		}
		// /// 
		// /// Typesafe, deep-copy clone method.
		// /// 
		// /// A new, independent copy of this class
		//public LineBase Clone()
		//{
		//	return new LineBase( this );
		//}
	#endregion
	#region Serialization
		/// 
		/// Current schema value that defines the version of the serialized file
		/// 
		public const int schema0 = 12;
		/// 
		/// Constructor for deserializing objects
		/// 
		/// A  instance that defines the
		/// serialized data
		/// 
		/// A  instance that contains
		/// the serialized data
		/// 
		protected LineBase( SerializationInfo info, StreamingContext context )
		{
			// The schema value is just a file version parameter.  You can use it to make future versions
			// backwards compatible as new member variables are added to classes
			int sch = info.GetInt32( "schema0" );
			_width = info.GetSingle( "width" );
			_style = (DashStyle)info.GetValue( "style", typeof( DashStyle ) );
			_dashOn = info.GetSingle( "dashOn" );
			_dashOff = info.GetSingle( "dashOff" );
			_isVisible = info.GetBoolean( "isVisible" );
			_isAntiAlias = info.GetBoolean( "isAntiAlias" );
			_color = (Color)info.GetValue( "color", typeof( Color ) );
			_gradientFill = (Fill)info.GetValue( "gradientFill", typeof( Fill ) );
		}
		/// 
		/// Populates a  instance with the data needed to serialize
		/// the target object
		/// 
		/// A  instance that defines the
		/// serialized data
		/// A  instance that contains the
		/// serialized data
		[SecurityPermissionAttribute( SecurityAction.Demand, SerializationFormatter = true )]
		public virtual void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			info.AddValue( "schema0", schema0 );
			info.AddValue( "width", _width );
			info.AddValue( "style", _style );
			info.AddValue( "dashOn", _dashOn );
			info.AddValue( "dashOff", _dashOff );
			info.AddValue( "isVisible", _isVisible );
			info.AddValue( "isAntiAlias", _isAntiAlias );
			info.AddValue( "color", _color );
			info.AddValue( "gradientFill", _gradientFill );
		}
	#endregion
	#region Methods
		/// 
		/// Create a  object based on the properties of this
		/// .
		/// 
		/// The owner  of this
		/// .
		/// 
		/// 
		/// 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  object with the properties of this 
		/// 
		public Pen GetPen( PaneBase pane, float scaleFactor )
		{
			return GetPen( pane, scaleFactor, null );
		}
		/// 
		/// Create a  object based on the properties of this
		/// .
		/// 
		/// The owner  of this
		/// .
		/// 
		/// 
		/// 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 data value to be used for a value-based
		/// color gradient.  This is only applicable if GradientFill.Type
		/// is one of ,
		/// , ,
		/// or .
		/// 
		/// A  object with the properties of this 
		/// 
		public Pen GetPen( PaneBase pane, float scaleFactor, PointPair dataValue )
		{
			Color color = _color;
			if ( _gradientFill.IsGradientValueType )
				color = _gradientFill.GetGradientColor( dataValue );
			Pen pen = new Pen( color,
						pane.ScaledPenWidth( _width, scaleFactor ) );
			pen.DashStyle = _style;
			if ( _style == DashStyle.Custom )
			{
				if ( _dashOff > 1e-10 && _dashOn > 1e-10 )
				{
					pen.DashStyle = DashStyle.Custom;
					float[] pattern = new float[2];
					pattern[0] = _dashOn;
					pattern[1] = _dashOff;
					pen.DashPattern = pattern;
				}
				else
					pen.DashStyle = DashStyle.Solid;
			}
			return pen;
		}
	#endregion
	}
}