//============================================================================
//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.Collections;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace DrawGraph
{
	/// 
	/// A class that maintains hyperlink information for a clickable object on the graph.
	/// 
	/// 
	///  John Champion 
	///  $Revision: 3.6 $ $Date: 2007/04/16 00:03:02 $ 
	// /// 
	[Serializable]
	public class Link : ISerializable, ICloneable
	{
	#region Fields
		/// 
		/// Internal field that stores the title string for this link.  
		/// 
		internal string _title;
		/// 
		/// Internal field that stores the url string for this link
		/// 
		internal string _url;
		/// 
		/// Internal field that stores the target string for this link
		/// 
		internal string _target;
		/// 
		/// Internal field that determines if this link is "live".
		/// 
		internal bool _isEnabled;
	#endregion
	#region Properties
		/// 
		/// Gets or sets the title string for this link.
		/// 
		/// 
		/// For web controls, this title will be shown as a tooltip when the mouse
		/// hovers over the area of the object that owns this link.  Set the value to
		///  to have no title.
		/// 
		public string Title
		{
			get { return _title; }
			set { _title = value; }
		}
		/// 
		/// Gets or sets the url string for this link.
		/// 
		/// 
		/// Set this value to  if you don't want to have
		/// a hyperlink associated with the object to which this link belongs.
		/// 
		public string Url
		{
			get { return _url; }
			set { _url = value; }
		}
		/// 
		/// Gets or sets the target string for this link.
		/// 
		/// 
		/// This value should be set to a valid target associated with the "Target"
		/// property of an html hyperlink.  Typically, this would be "_blank" to open
		/// a new browser window, or "_self" to open in the current browser.
		/// 
		public string Target
		{
			get { return _target != string.Empty ? _target : "_self"; }
			set { _target = value; }
		}
		/// 
		/// A tag object for use by the user.  This can be used to store additional
		/// information associated with the .  ZedGraph does
		/// not use this value for any purpose.
		/// 
		/// 
		/// Note that, if you are going to Serialize ZedGraph data, then any type
		/// that you store in  must be a serializable type (or
		/// it will cause an exception).
		/// 
		public object Tag;
		/// 
		/// Gets or sets a property that determines if this link is active.  True to have
		/// a clickable link, false to ignore the link.
		/// 
		public bool IsEnabled
		{
			get { return _isEnabled; }
			set { _isEnabled = value; }
		}
		/// 
		/// Gets a value that indicates if this  is enabled
		/// (see ), and that either the
		///  or the  is non-null.
		/// 
		public bool IsActive
		{
			get { return _isEnabled && ( _url != null || _title != null ); }
		}
	#endregion
	#region Constructors
		/// 
		/// Default constructor.  Set all properties to string.Empty, or null.
		/// 
		public Link()
		{
			_title = string.Empty;
			_url = string.Empty;
			_target = string.Empty;
			this.Tag = null;
			_isEnabled = false;
		}
		/// 
		/// Construct a Link instance from a specified title, url, and target.
		/// 
		/// The title for the link (which shows up in the tooltip).
		/// The URL destination for the link.
		/// The target for the link (typically "_blank" or "_self").
		public Link( string title, string url, string target )
		{
			_title = title;
			_url = url;
			_target = target;
			Tag = null;
			_isEnabled = true;
		}
		/// 
		/// The Copy Constructor
		/// 
		/// The  object from which to copy
		public Link( Link rhs )
		{
			// Copy value types
			_title = rhs._title;
			_url = rhs._url;
			_target = rhs._target;
			_isEnabled = false;
			// copy reference types by cloning
			if ( rhs.Tag is ICloneable )
				this.Tag = ((ICloneable) rhs.Tag).Clone();
			else
				this.Tag = rhs.Tag;
		}
		/// 
		/// 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 Link Clone()
		{
			return new Link( this );
		}
	#endregion
	#region methods
		/// 
		/// Create a URL for a  that includes the index of the
		/// point that was selected.
		/// 
		/// 
		/// An "index" parameter is added to the  property for this
		/// link to indicate which point was selected.  Further, if the 
		/// X or Y axes that correspond to this  are of
		/// , then an
		/// additional parameter will be added containing the text value that
		/// corresponds to the  of the selected point.
		/// The  text parameter will be labeled "xtext", and
		/// the  text parameter will be labeled "ytext".
		/// 
		/// The zero-based index of the selected point
		/// The  of interest
		/// The  for which to
		/// make the url string.
		/// A string containing the url with an index parameter added.
		public virtual string MakeCurveItemUrl( GraphPane pane, CurveItem curve, int index )
		{
			string url = _url;
			if ( url.IndexOf( '?' ) >= 0 )
				url += "&index=" + index.ToString();
			else
				url += "?index=" + index.ToString();
			Axis xAxis = curve.GetXAxis( pane );
			if (	xAxis.Type == AxisType.Text && index >= 0 &&
					xAxis.Scale.TextLabels != null &&
					index <= xAxis.Scale.TextLabels.Length )
				url += "&xtext=" + xAxis.Scale.TextLabels[index];
			Axis yAxis = curve.GetYAxis( pane );
			if (	yAxis != null && yAxis.Type == AxisType.Text && index >= 0 &&
					yAxis.Scale.TextLabels != null &&
					index <= yAxis.Scale.TextLabels.Length )
				url += "&ytext=" + yAxis.Scale.TextLabels[index];
			return url;
		}
	#endregion
	#region Serialization
		/// 
		/// Current schema value that defines the version of the serialized file
		/// 
		/// 
		/// schema started with 10 for ZedGraph version 5
		/// 
		public const int schema = 10;
		/// 
		/// Constructor for deserializing objects
		/// 
		/// A  instance that defines the serialized data
		/// 
		/// A  instance that contains the serialized data
		/// 
		protected Link( 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" );
			_title = info.GetString( "title" );
			_url = info.GetString( "url" );
			_target = info.GetString( "target" );
			_isEnabled = info.GetBoolean( "isEnabled" );
			Tag = info.GetValue( "Tag", typeof(object) );
		}
		/// 
		/// 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( "title", _title );
			info.AddValue( "url", _url );
			info.AddValue( "target", _target );
			info.AddValue( "isEnabled", _isEnabled );
			info.AddValue( "Tag", Tag );
		}
	#endregion
	}
}