//============================================================================ //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; using System.Runtime.Serialization; using System.Security.Permissions; namespace DrawGraph { /// /// A class that represents a text object on the graph. A list of /// objects is maintained by the /// collection class. /// /// /// John Champion /// $Revision: 3.4 $ $Date: 2007/01/25 07:56:09 $ [Serializable] public class TextObj : GraphObj, ICloneable, ISerializable { #region Fields /// Private field to store the actual text string for this /// . Use the public property /// to access this value. /// private string _text; /// /// Private field to store the class used to render /// this . Use the public property /// to access this class. /// private FontSpec _fontSpec; /* /// /// Private field to indicate whether this is to be /// wrapped when rendered. Wrapping is to be done within . /// Use the public property /// to access this value. /// private bool isWrapped; */ /// /// Private field holding the SizeF into which this /// should be rendered. Use the public property /// to access this value. /// private SizeF _layoutArea; #endregion #region Defaults /// /// A simple struct that defines the /// default property values for the class. /// new public struct Default { /* /// /// The default wrapped flag for rendering this . /// public static bool IsWrapped = false ; /// /// The default RectangleF for rendering this /// public static SizeF WrappedSize = new SizeF( 0,0 ); */ /// /// The default font family for the text /// ( property). /// public static string FontFamily = "Arial"; /// /// The default font size for the text /// ( property). Units are /// in points (1/72 inch). /// public static float FontSize = 12.0F; /// /// The default font color for the text /// ( property). /// public static Color FontColor = Color.Black; /// /// The default font bold mode for the text /// ( property). true /// for a bold typeface, false otherwise. /// public static bool FontBold = false; /// /// The default font underline mode for the text /// ( property). true /// for an underlined typeface, false otherwise. /// public static bool FontUnderline = false; /// /// The default font italic mode for the text /// ( property). true /// for an italic typeface, false otherwise. /// public static bool FontItalic = false; } #endregion #region Properties /* /// /// /// internal bool IsWrapped { get { return (this.isWrapped); } set { this.isWrapped = value; } } */ /// /// /// public SizeF LayoutArea { get { return _layoutArea; } set { _layoutArea = value; } } /// /// The to be displayed. This text can be multi-line by /// including newline ('\n') characters between the lines. /// public string Text { get { return _text; } set { _text = value; } } /// /// Gets a reference to the class used to render /// this /// /// /// /// /// /// /// public FontSpec FontSpec { get { return _fontSpec; } set { if ( value == null ) throw new ArgumentNullException( "Uninitialized FontSpec in TextObj" ); _fontSpec = value; } } #endregion #region Constructors /// /// Constructor that sets all properties to default /// values as defined in the class. /// /// The text to be displayed. /// The x position of the text. The units /// of this position are specified by the /// property. The text will be /// aligned to this position based on the /// property. /// The y position of the text. The units /// of this position are specified by the /// property. The text will be /// aligned to this position based on the /// property. public TextObj( string text, double x, double y ) : base( x, y ) { Init( text ); } private void Init( string text ) { if ( text != null ) _text = text; else text = "Text"; _fontSpec = new FontSpec( Default.FontFamily, Default.FontSize, Default.FontColor, Default.FontBold, Default.FontItalic, Default.FontUnderline ); //this.isWrapped = Default.IsWrapped ; _layoutArea = new SizeF( 0, 0 ); } /// /// Constructor that sets all properties to default /// values as defined in the class. /// /// The text to be displayed. /// The x position of the text. The units /// of this position are specified by the /// property. The text will be /// aligned to this position based on the /// property. /// The y position of the text. The units /// of this position are specified by the /// property. The text will be /// aligned to this position based on the /// property. /// The enum value that /// indicates what type of coordinate system the x and y parameters are /// referenced to. public TextObj( string text, double x, double y, CoordType coordType ) : base( x, y, coordType ) { Init( text ); } /// /// Constructor that sets all properties to default /// values as defined in the class. /// /// The text to be displayed. /// The x position of the text. The units /// of this position are specified by the /// property. The text will be /// aligned to this position based on the /// property. /// The y position of the text. The units /// of this position are specified by the /// property. The text will be /// aligned to this position based on the /// property. /// The enum value that /// indicates what type of coordinate system the x and y parameters are /// referenced to. /// The enum that specifies /// the horizontal alignment of the object with respect to the (x,y) location /// The enum that specifies /// the vertical alignment of the object with respect to the (x,y) location public TextObj( string text, double x, double y, CoordType coordType, AlignH alignH, AlignV alignV ) : base( x, y, coordType, alignH, alignV ) { Init( text ); } /// /// Parameterless constructor that initializes a new . /// public TextObj() : base( 0, 0 ) { Init( "" ); } /// /// The Copy Constructor /// /// The object from which to copy public TextObj( TextObj rhs ) : base( rhs ) { _text = rhs.Text; _fontSpec = new FontSpec( rhs.FontSpec ); } /// /// 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 TextObj Clone() { return new TextObj( this ); } #endregion #region Serialization /// /// Current schema value that defines the version of the serialized file /// public const int schema2 = 10; /// /// Constructor for deserializing objects /// /// A instance that defines the serialized data /// /// A instance that contains the serialized data /// protected TextObj( SerializationInfo info, StreamingContext context ) : base( info, 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( "schema2" ); _text = info.GetString( "text" ); _fontSpec = (FontSpec) info.GetValue( "fontSpec", typeof(FontSpec) ); //isWrapped = info.GetBoolean ("isWrapped") ; _layoutArea = (SizeF) info.GetValue( "layoutArea", typeof(SizeF) ); } /// /// 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 override void GetObjectData( SerializationInfo info, StreamingContext context ) { base.GetObjectData( info, context ); info.AddValue( "schema2", schema2 ); info.AddValue( "text", _text ); info.AddValue( "fontSpec", _fontSpec ); //info.AddValue( "isWrapped", isWrapped ); info.AddValue( "layoutArea", _layoutArea ); } #endregion #region Rendering Methods /// /// Render this object to the specified device /// This method is normally only called by the Draw method /// of the parent collection 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. /// override public void Draw( Graphics g, PaneBase pane, float scaleFactor ) { // transform the x,y location from the user-defined // coordinate frame to the screen pixel location PointF pix = _location.Transform( pane ); // Draw the text on the screen, including any frame and background // fill elements if ( pix.X > -100000 && pix.X < 100000 && pix.Y > -100000 && pix.Y < 100000 ) { //if ( this.layoutSize.IsEmpty ) // this.FontSpec.Draw( g, pane.IsPenWidthScaled, this.text, pix.X, pix.Y, // this.location.AlignH, this.location.AlignV, scaleFactor ); //else this.FontSpec.Draw( g, pane, _text, pix.X, pix.Y, _location.AlignH, _location.AlignV, scaleFactor, _layoutArea ); } } /// /// Determine if the specified screen point lies inside the bounding box of this /// . This method takes into account rotation and alignment /// parameters of the text, as specified in the . /// /// The screen point, in pixels /// /// 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. /// /// true if the point lies in the bounding box, false otherwise override public bool PointInBox( PointF pt, PaneBase pane, Graphics g, float scaleFactor ) { if ( ! base.PointInBox(pt, pane, g, scaleFactor ) ) return false; // transform the x,y location from the user-defined // coordinate frame to the screen pixel location PointF pix = _location.Transform( pane ); return _fontSpec.PointInBox( pt, g, _text, pix.X, pix.Y, _location.AlignH, _location.AlignV, scaleFactor, this.LayoutArea ); } /// /// Determines the shape type and Coords values for this GraphObj /// override public void GetCoords( PaneBase pane, Graphics g, float scaleFactor, out string shape, out string coords ) { // transform the x,y location from the user-defined // coordinate frame to the screen pixel location PointF pix = _location.Transform( pane ); PointF[] pts = _fontSpec.GetBox( g, _text, pix.X, pix.Y, _location.AlignH, _location.AlignV, scaleFactor, new SizeF() ); shape = "poly"; coords = String.Format( "{0:f0},{1:f0},{2:f0},{3:f0},{4:f0},{5:f0},{6:f0},{7:f0},", pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, pts[2].X, pts[2].Y, pts[3].X, pts[3].Y ); } #endregion } }