//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright ?2006  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.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace DrawGraph
{
	/// 
	/// Class that holds the specific properties for the minor grid.
	/// 
	///  John Champion 
	///  $Revision: 3.1 $ $Date: 2006/06/24 20:26:44 $ 
	[Serializable]
	public class MinorGrid : ICloneable, ISerializable
	{
		internal bool	_isVisible;
		internal float _dashOn,
							_dashOff,
							_penWidth;
		internal Color _color;
	#region Constructors
		/// 
		/// Default constructor
		/// 
		public MinorGrid()
		{
			_dashOn = Default.DashOn;
			_dashOff = Default.DashOff;
			_penWidth = Default.PenWidth;
			_isVisible = Default.IsVisible;
			_color = Default.Color;
		}
		/// 
		/// Copy constructor
		/// 
		/// The source  to be copied.
		public MinorGrid( MinorGrid rhs )
		{
			_dashOn = rhs._dashOn;
			_dashOff = rhs._dashOff;
			_penWidth = rhs._penWidth;
			_isVisible = rhs._isVisible;
			_color = rhs._color;
		}
		/// 
		/// 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 MinorGrid Clone()
		{
			return new MinorGrid( this );
		}
	#endregion
	#region Properties
		/// 
		/// Gets or sets a value that determines if the major  gridlines
		/// (at each labeled value) will be visible
		/// 
		/// true to show the gridlines, false otherwise
		/// Default.IsShowGrid.
		/// 
		/// 
		/// 
		/// 
		/// 
		public bool IsVisible
		{
			get { return _isVisible; }
			set { _isVisible = value; }
		}
		/// 
		/// The "Dash On" mode for drawing the grid.
		/// 
		/// 
		/// This is the distance,
		/// in points (1/72 inch), of the dash segments that make up the dashed grid lines.
		/// 
		/// 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 grid.
		/// 
		/// 
		/// This is the distance,
		/// in points (1/72 inch), of the spaces between the dash segments that make up
		/// the dashed grid lines.
		/// 
		/// The dash off length is defined in points (1/72 inch)
		/// 
		/// 
		/// .
		public float DashOff
		{
			get { return _dashOff; }
			set { _dashOff = value; }
		}
		/// 
		/// The pen width used for drawing the grid lines.
		/// 
		/// The grid pen width is defined in points (1/72 inch)
		/// 
		/// .
		/// 
		public float PenWidth
		{
			get { return _penWidth; }
			set { _penWidth = value; }
		}
		/// 
		/// The color to use for drawing this  grid.
		/// 
		///  The color is defined using the
		///  class
		/// .
		/// 
		public Color Color
		{
			get { return _color; }
			set { _color = value; }
		}
	#endregion
	#region Serialization
		/// 
		/// Current schema value that defines the version of the serialized file
		/// 
		public const int schema = 10;
		/// 
		/// Constructor for deserializing objects
		/// 
		/// A  instance that defines the serialized data
		/// 
		/// A  instance that contains the serialized data
		/// 
		protected MinorGrid( 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( "schema" );
			_isVisible = info.GetBoolean( "isVisible" );
			_dashOn = info.GetSingle( "dashOn" );
			_dashOff = info.GetSingle( "dashOff" );
			_penWidth = info.GetSingle( "penWidth" );
			_color = (Color)info.GetValue( "color", typeof( Color ) );
		}
		/// 
		/// 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( "schema", schema );
			info.AddValue( "isVisible", _isVisible );
			info.AddValue( "dashOn", _dashOn );
			info.AddValue( "dashOff", _dashOff );
			info.AddValue( "penWidth", _penWidth );
			info.AddValue( "color", _color );
		}
	#endregion
	#region Defaults
		/// 
		/// A simple struct that defines the
		/// default property values for the  class.
		/// 
		public struct Default
		{
			/// 
			/// The default "dash on" size for drawing the  minor grid
			/// ( property). Units are in points (1/72 inch).
			/// 
			public static float DashOn = 1.0F;
			/// 
			/// The default "dash off" size for drawing the  minor grid
			/// ( property). Units are in points (1/72 inch).
			/// 
			public static float DashOff = 10.0F;
			/// 
			/// The default pen width for drawing the  minor grid
			/// ( property). Units are in points (1/72 inch).
			/// 
			public static float PenWidth = 1.0F;
			/// 
			/// The default color for the  minor grid lines
			/// ( property).  This color only affects the
			/// minor grid lines.
			/// 
			public static Color Color = Color.Gray;
			/// 
			/// The default display mode for the  minor grid lines
			/// ( property). true
			/// to show the minor grid lines, false to hide them.
			/// 
			public static bool IsVisible = false;
		}
	#endregion
	#region Methods
		internal void Draw( Graphics g, Pen pen, float pixVal, float topPix )
		{
			// draw the minor grid
			if ( _isVisible )
				g.DrawLine( pen, pixVal, 0.0F, pixVal, topPix );
		}
		internal Pen GetPen( GraphPane pane, float scaleFactor )
		{
			Pen pen = new Pen( _color,
						pane.ScaledPenWidth( _penWidth, scaleFactor ) );
			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;
			}
			return pen;
		}
	#endregion
	}
}