using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
namespace DevComponents.DotNetBar.Charts.Style
{
    ///
    /// PieCenterVisualStyles
    ///
    [TypeConverter(typeof(VisualStylesConverter))]
    public class PieCenterVisualStyles : VisualStyles
    {
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public PieCenterVisualStyles Copy()
        {
            PieCenterVisualStyles styles = new PieCenterVisualStyles();
            for (int i = 0; i < Styles.Length; i++)
            {
                PieCenterVisualStyle vstyle = Styles[i];
                if (vstyle != null)
                    styles.Styles[i] = vstyle.Copy();
            }
            return (styles);
        }
        #endregion
    }
    /// 
    /// Represents the visual style of a Pie Center.
    /// 
    [ToolboxItem(false), DesignTimeVisible(false)]
    [TypeConverter(typeof(BlankExpandableObjectConverter))]
    public class PieCenterVisualStyle : BaseVisualStyle
    {
        #region Private variables
        private Tbool _AllowWrap = Tbool.NotSet;
        private Alignment _Alignment = Alignment.NotSet;
        private Background _Background;
        private ChartLineVisualStyle _Border;
        private Padding _Padding;
        private Color _TextColor = Color.Empty;
        private Font _Font;
        private Image _Image;
        private int _ImageIndex = -1;
        private ImageList _ImageList;
        private Padding _ImagePadding;
        private Alignment _ImageAlignment = Alignment.NotSet;
        private ImageOverlay _ImageOverlay = ImageOverlay.NotSet;
        private ImageSizeMode _ImageSizeMode = ImageSizeMode.NotSet;
        private double _ImageScale = double.NaN;
        private Size _FigureSizeEx;
        private Tbool _ImageInscribed = Tbool.NotSet;
        private Tbool _TextInscribed = Tbool.NotSet;
        private SymbolDef _SymbolDef;
        private SymbolDef _ScaleSymDef;
        private float _SymFontScale = -1;
        #endregion
        #region Public properties
        #region Alignment
        /// 
        /// Gets or sets the alignment of the text.
        /// 
        [DefaultValue(Alignment.NotSet), Category("Appearance")]
        [Description("Indicates the alignment of the text.")]
        public Alignment Alignment
        {
            get { return (_Alignment); }
            set
            {
                if (_Alignment != value)
                {
                    _Alignment = value;
                    OnPropertyChangedEx("Alignment", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region AllowWrap
        /// 
        /// Gets or sets whether text wrapping is permitted.
        /// 
        [DefaultValue(Tbool.NotSet), Category("Appearance")]
        [Description("Indicates whether text wrapping is permitted.")]
        public Tbool AllowWrap
        {
            get { return (_AllowWrap); }
            set
            {
                if (_AllowWrap != value)
                {
                    _AllowWrap = value;
                    OnPropertyChangedEx("AllowWrap", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region Background
        /// 
        /// Gets or sets the background style.
        /// 
        [Description("Indicates the background style")]
        public Background Background
        {
            get
            {
                if (_Background == null)
                {
                    _Background = Background.Empty;
                    UpdateChangeHandler(null, _Background);
                }
                return (_Background);
            }
            set
            {
                if (_Background != value)
                {
                    UpdateChangeHandler(_Background, value);
                    _Background = value;
                    OnPropertyChangedEx("Background", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeBackground()
        {
            return (_Background != null && _Background.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetBackground()
        {
            Background = null;
        }
        #endregion
        #region Border
        /// 
        /// Gets or sets the pie center border.
        /// 
        [Category("Style")]
        [Description("Indicates the pie center border.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ChartLineVisualStyle Border
        {
            get
            {
                if (_Border == null)
                {
                    _Border = new ChartLineVisualStyle();
                    UpdateChangeHandler(null, _Border);
                }
                return (_Border);
            }
            set
            {
                if (_Border != value)
                {
                    ChartLineVisualStyle oldValue = _Border;
                    _Border = value;
                    OnStyleChanged("Border", oldValue, value);
                    if (oldValue != null)
                        oldValue.Dispose();
                }
            }
        }
        #endregion
        #region Font
        /// 
        /// Gets or sets the style Font.
        /// 
        [DefaultValue(null)]
        [Description("Indicates the style Font.")]
        public Font Font
        {
            get { return (_Font); }
            set
            {
                if (_Font != value)
                {
                    _Font = value;
                    OnPropertyChangedEx("Font", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region Image
        /// 
        /// Gets or sets the element Image.
        /// 
        [DefaultValue(null), Category("Appearance")]
        [Description("Indicates the element image.")]
        public Image Image
        {
            get { return (_Image); }
            set
            {
                if (_Image != value)
                {
                    _Image = value;
                    OnPropertyChangedEx("Image", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImageAlignment
        /// 
        /// Gets or sets the alignment of the Image.
        /// 
        [DefaultValue(Alignment.NotSet), Category("Appearance")]
        [Description("Indicates the alignment of the Image.")]
        public Alignment ImageAlignment
        {
            get { return (_ImageAlignment); }
            set
            {
                if (_ImageAlignment != value)
                {
                    _ImageAlignment = value;
                    OnPropertyChangedEx("ImageAlignment", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImageIndex
        /// 
        /// Gets or sets the image index.
        /// 
        [DefaultValue(-1)]
        [Category("Appearance"), Description("Indicates the image index.")]
        [Editor("DevComponents.SuperGrid.Design.ImageIndexEditor, DevComponents.SuperGrid.Design, Version=14.1.0.37, Culture=neutral,  PublicKeyToken=90f470f34c89ccaf", typeof(UITypeEditor))]
        [TypeConverter(typeof(ImageIndexConverter))]
        public int ImageIndex
        {
            get { return (_ImageIndex); }
            set
            {
                if (_ImageIndex != value)
                {
                    _ImageIndex = value;
                    OnPropertyChangedEx("ImageIndex", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImageInscribed
        /// 
        /// Gets or sets whether the image is inscribed in the
        /// PieCenter area (true = inscribed, false = circumscribed).
        /// 
        [DefaultValue(Tbool.NotSet), Category("Appearance")]
        [Description("Indicates whether the image is inscribed in the PieCenter area (true = inscribed, false = circumscribed).")]
        public Tbool ImageInscribed
        {
            get { return (_ImageInscribed); }
            set
            {
                if (_ImageInscribed != value)
                {
                    _ImageInscribed = value;
                    OnPropertyChangedEx("ImageInscribed", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImageList
        /// 
        /// Gets or sets the ImageList.
        /// 
        [Browsable(true), Category("Appearance"), DefaultValue(null)]
        [Description("Indicates the ImageList.")]
        public ImageList ImageList
        {
            get { return (_ImageList); }
            set
            {
                if (_ImageList != value)
                {
                    _ImageList = value;
                    OnPropertyChangedEx("ImageList", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImageOverlay
        /// 
        /// Gets or sets how to overlay the image with respect to the center content.
        /// 
        [DefaultValue(ImageOverlay.NotSet), Category("Appearance")]
        [Description("Indicates how to overlay the image with respect to the center content.")]
        public ImageOverlay ImageOverlay
        {
            get { return (_ImageOverlay); }
            set
            {
                if (_ImageOverlay != value)
                {
                    _ImageOverlay = value;
                    OnPropertyChangedEx("ImageOverlay", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImagePadding
        /// 
        /// Gets or sets the spacing between content and edges of the Image.
        /// 
        [Description("Indicates the spacing between content and edges of the Image.")]
        public Padding ImagePadding
        {
            get
            {
                if (_ImagePadding == null)
                {
                    _ImagePadding = Padding.Empty;
                    UpdateChangeHandler(null, _ImagePadding);
                }
                return (_ImagePadding);
            }
            set
            {
                if (_ImagePadding != value)
                {
                    UpdateChangeHandler(_ImagePadding, value);
                    _ImagePadding = value;
                    OnPropertyChangedEx("ImagePadding", VisualChangeType.Layout);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeImagePadding()
        {
            return (_ImagePadding != null && _ImagePadding.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetImagePadding()
        {
            ImagePadding = null;
        }
        #endregion
        #region ImageScale
        /// 
        /// Gets or sets the 'scale' factor for the image/symbol. This property informs the
        /// chart to scale the image (or symbol) based upon the available space at its offset
        /// position in the slice - the image is moved in or out of the slice, the image will
        /// grow of shrink accordingly.
        /// 
        [DefaultValue(double.NaN), Category("Display")]
        [Description("Indicates the 'scale' factor for the image/symbol. This property informs the chart to scale the image (or symbol) based upon the available space at its offset position in the slice - the image is moved in or out of the slice, the image will grow of shrink accordingly.")]
        [Editor("DevComponents.Charts.Design.RadiusRangeValueEditor, DevComponents.Charts.Design, Version=14.1.0.37, Culture=neutral,  PublicKeyToken=90f470f34c89ccaf", typeof(UITypeEditor))]
        public double ImageScale
        {
            get { return (_ImageScale); }
            set
            {
                if (value != _ImageScale)
                {
                    if (value < 0)
                        throw new Exception("ImageScale can not be negative.");
                    _ImageScale = value;
                    OnPropertyChangedEx("ImageScale", VisualChangeType.Render);
                }
            }
        }
        #endregion
        #region ImageSizeMode
        /// 
        /// Gets or sets the 'mode' used to display the image.
        /// 
        [DefaultValue(ImageSizeMode.NotSet), Category("Appearance")]
        [Description("Indicates the 'mode' used to display the image.")]
        public ImageSizeMode ImageSizeMode
        {
            get { return (_ImageSizeMode); }
            set
            {
                if (value!= _ImageSizeMode)
                {
                    _ImageSizeMode = value;
                    OnPropertyChangedEx("ImageSizeMode", VisualChangeType.Render);
                }
            }
        }
        #endregion
        #region Padding
        /// 
        /// Gets or sets the spacing between content and edges of the Image.
        /// 
        [Description("Indicates the spacing between content and edges of the Image.")]
        public Padding Padding
        {
            get
            {
                if (_Padding == null)
                {
                    _Padding = Padding.Empty;
                    UpdateChangeHandler(null, _Padding);
                }
                return (_Padding);
            }
            set
            {
                if (_Padding != value)
                {
                    UpdateChangeHandler(_Padding, value);
                    _Padding = value;
                    OnPropertyChangedEx("Padding", VisualChangeType.Layout);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializePadding()
        {
            return (_Padding != null && _Padding.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetPadding()
        {
            Padding = null;
        }
        #endregion
        #region SymbolDef
        /// 
        /// Gets or sets the element Symbol Definition. Note that Symbol definition
        /// takes precedence over Image definition. Also note that the supporting Image
        /// properties (such as ImageAlignment, ImageOverlay, etc) apply to a set
        /// Symbol as well as to a set Image.
        /// 
        [DefaultValue(null), Category("Appearance")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Description("Indicates the element Symbol Definition. Note that Symbol definition takes precedence over Image definition. Also note that the supporting Image properties (such as ImageAlignment, ImageOverlay, etc) apply to a set Symbol as well as to a set Image.")]
        public SymbolDef SymbolDef
        {
            get
            {
                if (_SymbolDef == null)
                {
                    _SymbolDef = new SymbolDef();
                    UpdateChangeHandler(null, _SymbolDef);
                }
                return (_SymbolDef);
            }
            set
            {
                if (_SymbolDef != value)
                {
                    UpdateChangeHandler(_SymbolDef, value);
                    _SymbolDef = value;
                    OnPropertyChangedEx("SymbolDef", VisualChangeType.Layout);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeSymbolDef()
        {
            return (_SymbolDef != null && _SymbolDef.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetSymbolDef()
        {
            SymbolDef = null;
        }
        #endregion
        #region TextColor
        /// 
        /// Gets or sets the Text color.
        /// 
        [Description("Indicates the Text color.")]
        public Color TextColor
        {
            get { return (_TextColor); }
            set
            {
                if (_TextColor != value)
                {
                    _TextColor = value;
                    OnPropertyChangedEx("TextColor", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeTextColor()
        {
            return (_TextColor.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetTextColor()
        {
            TextColor = Color.Empty;
        }
        #endregion
        #region TextInscribed
        /// 
        /// Gets or sets whether the Text is inscribed in the
        /// PieCenter area (true = inscribed, false = circumscribed).
        /// 
        [DefaultValue(Tbool.NotSet), Category("Appearance")]
        [Description("Indicates whether the Text is inscribed in the PieCenter area (true = inscribed, false = circumscribed).")]
        public Tbool TextInscribed
        {
            get { return (_TextInscribed); }
            set
            {
                if (_TextInscribed != value)
                {
                    _TextInscribed = value;
                    OnPropertyChangedEx("TextInscribed", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region IsEmpty
        /// 
        /// Gets whether the style is logically Empty.
        /// 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Gets whether the style is logically Empty.")]
        public override bool IsEmpty
        {
            get
            {
                return ((_Alignment == Alignment.NotSet) &&
                    (_AllowWrap == Tbool.NotSet) &&
                    (_Background == null || _Background.IsEmpty == true) &&
                    (_Border == null || _Border.IsEmpty == true) &&
                    (_Font == null) &&
                    (_Image == null) &&
                    (_ImageAlignment == Alignment.NotSet) &&
                    (_ImageIndex == -1) &&
                    (_ImageInscribed == Tbool.NotSet) &&
                    (_ImageList == null) &&
                    (_ImageOverlay == ImageOverlay.NotSet) &&
                    (_ImagePadding == null || _ImagePadding.IsEmpty == true) &&
                    (double.IsNaN(_ImageScale) == true) &&
                    (_ImageSizeMode == Style.ImageSizeMode.NotSet) &&
                    (_Padding == null || _Padding.IsEmpty == true) &&
                    (_SymbolDef == null || _SymbolDef.IsEmpty == true) &&
                    (_TextColor.IsEmpty == true) &&
                    (_TextInscribed == Tbool.NotSet) &&
                    (base.IsEmpty == true));
            }
        }
        #endregion
        #endregion
        #region Internal properties
        #region IsOverlayImage
        internal bool IsOverlayImage
        {
            get
            {
                return (_ImageOverlay == ImageOverlay.Top ||
                    _ImageOverlay == ImageOverlay.Middle ||
                    _ImageOverlay == ImageOverlay.Bottom);
            }
        }
        #endregion
        #region IsValidFigure
        internal bool IsValidFigure
        {
            get
            {
                if (IsSymbolFigure == true)
                    return (true);
                return (GetImage() != null);
            }
        }
        #endregion
        #region IsSymbolFigure
        internal bool IsSymbolFigure
        {
            get { return (_SymbolDef != null &&  _SymbolDef.IsValidSymbol == true); }
        }
        #endregion
        #region FigureSizeEx
        internal Size FigureSizeEx
        {
            get { return (_FigureSizeEx); }
            set { _FigureSizeEx = value; }
        }
        #endregion
        #endregion
        #region GetFigure
        internal object GetFigure()
        {
            if (_SymbolDef != null && _SymbolDef.IsEmpty == false)
                return (_SymbolDef);
            return (GetImage());
        }
        #region GetImage
        internal Image GetImage()
        {
            if (_Image != null)
                return (_Image);
            if (_ImageIndex >= 0)
            {
                ImageList imageList = ImageList;
                if (imageList != null && _ImageIndex < imageList.Images.Count)
                    return (imageList.Images[_ImageIndex]);
            }
            return (null);
        }
        #endregion
        #region GetSymbol
        internal string GetSymbol()
        {
            if (_SymbolDef != null && _SymbolDef.IsEmpty == false)
                return (_SymbolDef.SymbolRealized);
            return (null);
        }
        #endregion
        #endregion
        #region GetFigureSize
        internal Size GetFigureSize(Graphics g)
        {
            Size size = Size.Empty;
            if (IsSymbolFigure == true)
                size = _SymbolDef.GetSymbolSize(g);
            else
            {
                Image image = GetImage();
                if (image != null)
                    size = Dpi.Size(image.Size);
            }
            return (size);
        }
        #endregion
        #region GetFigureBounds
        internal Rectangle GetFigureBounds(Graphics g, Rectangle r)
        {
            if (IsSymbolFigure == true)
                return (GetSymbolBounds(g, r));
            return (GetImageBounds(g, r));
        }
        #region GetImageBounds
        internal Rectangle GetImageBounds(Graphics g, Rectangle r)
        {
            Image image = GetImage();
            return (GetImageBounds(g, r, image));
        }
        private Rectangle GetImageBounds(Graphics g, Rectangle r, Image image)
        {
            if (image != null)
            {
                Size size = Dpi.Size(image.Size);
                if (double.IsNaN(ImageScale) == false)
                {
                    if (ImageScale != 1)
                        size = FigureSizeEx;
                }
                return (GetSizeBounds(r, size, ImageAlignment));
            }
            return (Rectangle.Empty);
        }
        #endregion
        #region GetSymbolBounds
        internal Rectangle GetSymbolBounds(Graphics g, Rectangle r)
        {
            if (_SymbolDef != null && _SymbolDef.IsEmpty == false)
            {
                Size size = _SymbolDef.GetSymbolSize(g);
                if (double.IsNaN(ImageScale) == false)
                {
                    if (ImageScale != 1)
                        size = FigureSizeEx;
                }
                return (GetSizeBounds(r, size, ImageAlignment));
            }
            return (Rectangle.Empty);
        }
        #endregion
        #region GetSizeBounds
        internal Rectangle GetSizeBounds(Rectangle r,
            Size size, Padding padding, Alignment alignment)
        {
            switch (alignment)
            {
                case Alignment.NotSet:
                case Alignment.MiddleLeft:
                    r.Y += (r.Height - size.Height) / 2;
                    r.X += Dpi.Width(padding.Left);
                    r.Y += Dpi.Height(padding.Top - padding.Bottom);
                    break;
                case Alignment.TopLeft:
                    r.X += Dpi.Width(padding.Left);
                    r.Y += Dpi.Height(padding.Top);
                    break;
                case Alignment.BottomLeft:
                    r.Y = r.Bottom - size.Height;
                    r.X += Dpi.Width(padding.Left);
                    r.Y -= Dpi.Height(padding.Bottom);
                    break;
                case Alignment.TopCenter:
                    r.X += (r.Width - size.Width) / 2;
                    r.X += Dpi.Width(padding.Left - padding.Right);
                    r.Y += Dpi.Height(padding.Top);
                    break;
                case Alignment.MiddleCenter:
                    r.X += (r.Width - size.Width) / 2;
                    r.Y += (r.Height - size.Height) / 2;
                    r.X += Dpi.Width(padding.Left - padding.Right);
                    r.Y += Dpi.Height(padding.Top - padding.Bottom);
                    break;
                case Alignment.BottomCenter:
                    r.X += (r.Width - size.Width) / 2;
                    r.Y = r.Bottom - size.Height;
                    r.X += Dpi.Width(padding.Left - padding.Right);
                    r.Y -= Dpi.Height(padding.Bottom);
                    break;
                case Alignment.TopRight:
                    r.X = r.Right - size.Width;
                    r.X -= Dpi.Width(padding.Right);
                    r.Y += Dpi.Height(padding.Top);
                    break;
                case Alignment.MiddleRight:
                    r.X = r.Right - size.Width;
                    r.Y += (r.Height - size.Height) / 2;
                    r.X -= Dpi.Width(padding.Right);
                    r.Y += Dpi.Height(padding.Top - padding.Bottom);
                    break;
                case Alignment.BottomRight:
                    r.X = r.Right - size.Width;
                    r.Y = r.Bottom - size.Height;
                    r.X -= Dpi.Width(padding.Right);
                    r.Y -= Dpi.Height(padding.Bottom);
                    break;
            }
            r.Size = size;
            return (r);
        }
        #endregion
        #region GetSizeBounds
        internal Rectangle GetSizeBounds(Rectangle r, Size size, Alignment alignment)
        {
            switch (alignment)
            {
                case Alignment.NotSet:
                case Alignment.MiddleLeft:
                    r.Y += (r.Height - size.Height) / 2;
                    break;
                case Alignment.BottomLeft:
                    r.Y = r.Bottom - size.Height;
                    break;
                case Alignment.TopCenter:
                    r.X += (r.Width - size.Width) / 2;
                    break;
                case Alignment.MiddleCenter:
                    r.X += (r.Width - size.Width) / 2;
                    r.Y += (r.Height - size.Height) / 2;
                    break;
                case Alignment.BottomCenter:
                    r.X += (r.Width - size.Width) / 2;
                    r.Y = r.Bottom - size.Height;
                    break;
                case Alignment.TopRight:
                    r.X = r.Right - size.Width;
                    break;
                case Alignment.MiddleRight:
                    r.X = r.Right - size.Width;
                    r.Y += (r.Height - size.Height) / 2;
                    break;
                case Alignment.BottomRight:
                    r.X = r.Right - size.Width;
                    r.Y = r.Bottom - size.Height;
                    break;
            }
            r.Size = size;
            return (r);
        }
        #endregion
        #endregion
        #region GetTextBounds
        internal Rectangle GetTextBounds(Rectangle r, Size size)
        {
            return (GetSizeBounds(r, size, ImagePadding, Alignment));
        }
        #endregion
        #region RenderBackgroundFigure
        internal void RenderBackgroundFigure(Graphics g, Rectangle bounds)
        {
            RenderBackgroundFigure(g, bounds, bounds);
        }
        internal void RenderBackgroundFigure(Graphics g, Rectangle bounds, Rectangle clipBounds)
        {
            if (IsSymbolFigure == true)
                RenderBackgroundSymbol(g, bounds, clipBounds);
            else
                RenderBackgroundImage(g, bounds, clipBounds);
        }
        #region RenderBackgroundImage
        internal void RenderBackgroundImage(Graphics g, Rectangle bounds, Rectangle clipBounds)
        {
            Image image = GetImage();
            if (image != null)
            {
                Region clip = g.Clip;
                if (ImageSizeMode == ImageSizeMode.NotSet ||
                    ImageSizeMode == ImageSizeMode.Normal)
                {
                    Rectangle dbounds = GetImageBounds(g, clipBounds, image);
                    g.SetClip(Rectangle.Intersect(bounds, dbounds), CombineMode.Intersect);
                    g.DrawImage(image, dbounds,
                        new Rectangle(Point.Empty, image.Size), GraphicsUnit.Pixel);
                }
                else
                {
                    Rectangle r = GetAdjustedBounds(clipBounds, ImagePadding);
                    g.SetClip(r, CombineMode.Intersect);
                    switch (ImageSizeMode)
                    {
                        case ImageSizeMode.Stretch:
                            g.PixelOffsetMode = PixelOffsetMode.Half;
                            g.DrawImage(image, r);
                            g.PixelOffsetMode = PixelOffsetMode.Default;
                            break;
                        case ImageSizeMode.Tile:
                            RenderImageTiled(g, image, r);
                            break;
                        case ImageSizeMode.Zoom:
                            RenderImageZoomed(g, image, r);
                            break;
                    }
                }
                g.Clip = clip;
            }
        }
        #region GetAdjustedBounds
        private Rectangle GetAdjustedBounds(Rectangle bounds, Thickness padding)
        {
            if (padding.IsEmpty == false)
            {
                if (bounds.Height > padding.Vertical)
                {
                    bounds.Y += padding.Top;
                    bounds.Height -= padding.Vertical;
                }
                if (bounds.Width > padding.Horizontal)
                {
                    bounds.X += padding.Left;
                    bounds.Width -= padding.Horizontal;
                }
            }
            return (bounds);
        }
        #endregion
        #region RenderImageTiled
        private void RenderImageTiled(Graphics g, Image image, Rectangle bounds)
        {
            Size size = image.Size;
            Rectangle sr = new Rectangle(Point.Empty, size);
            Rectangle dr = new Rectangle(bounds.Location, size);
            Rectangle r = new Rectangle();
            r.X = bounds.X + bounds.Width / 2;
            r.Y = bounds.Y + bounds.Height / 2;
            int dx = ((bounds.Width / 2) / size.Width + 2) * size.Width;
            int dy = ((bounds.Height / 2) / size.Height + 2) * size.Height;
            dr.X = r.X - dx + size.Width / 2;
            dr.Y = r.Y - dy + size.Height / 2;
            int x = dr.X;
            while (dr.Top < bounds.Bottom)
            {
                while (dr.Left < bounds.Right)
                {
                    g.DrawImage(image, dr, sr, GraphicsUnit.Pixel);
                    dr.X += image.Size.Width;
                }
                dr.X = x;
                dr.Y += image.Size.Height;
            }
        }
        #endregion
        #region RenderImageZoomed
        private void RenderImageZoomed(
            Graphics g, Image image, Rectangle r)
        {
            SizeF size = new SizeF(image.Width / image.HorizontalResolution,
                image.Height / image.VerticalResolution);
            float scale = Math.Min(r.Width / size.Width, r.Height / size.Height);
            size.Width *= scale;
            size.Height *= scale;
            Point pt = r.Location;
            if (Math.Abs(r.Width - size.Width) > 2)
            {
                switch (ImageAlignment)
                {
                    case Alignment.TopCenter:
                    case Alignment.MiddleCenter:
                    case Alignment.BottomCenter:
                        pt.X += (int)((r.Width - size.Width) / 2);
                        break;
                    case Alignment.TopRight:
                    case Alignment.MiddleRight:
                    case Alignment.BottomRight:
                        pt.X = (int)(r.Right - size.Width);
                        break;
                }
            }
            else
            {
                switch (ImageAlignment)
                {
                    case Alignment.MiddleLeft:
                    case Alignment.MiddleCenter:
                    case Alignment.MiddleRight:
                        pt.Y += (int)((r.Height - size.Height) / 2);
                        break;
                    case Alignment.BottomLeft:
                    case Alignment.BottomCenter:
                    case Alignment.BottomRight:
                        pt.Y = (int)(r.Bottom - size.Height);
                        break;
                }
            }
            g.PixelOffsetMode = PixelOffsetMode.Half;
            g.DrawImage(image,
                pt.X, pt.Y, size.Width, size.Height);
            g.PixelOffsetMode = PixelOffsetMode.Default;
        }
        #endregion
        #endregion
        #region RenderBackgroundSymbol
        internal void RenderBackgroundSymbol(Graphics g, Rectangle bounds, Rectangle clipBounds)
        {
            string symbol = GetSymbol();
            if (symbol != null)
            {
                Region clip = g.Clip;
                TextRenderingHint hint = g.TextRenderingHint;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                bounds.X += (ImagePadding.Left - ImagePadding.Right);
                bounds.Y += (ImagePadding.Top - ImagePadding.Bottom);
                if (ImageSizeMode == Style.ImageSizeMode.Stretch ||
                    ImageSizeMode == Style.ImageSizeMode.Zoom)
                {
                    if (_ScaleSymDef == null)
                        _ScaleSymDef = new SymbolDef();
                    float n = Math.Max(bounds.Width, bounds.Height);
                    if (_SymFontScale < 0)
                    {
                        _ScaleSymDef.SymbolSize = n;
                        _ScaleSymDef.SymbolSet = _SymbolDef.SymbolSet;
                        _SymFontScale = n / _ScaleSymDef.SymbolFont.Height * .8f;
                    }
                    _ScaleSymDef.SymbolSize = n * _SymFontScale;
                    _ScaleSymDef.SymbolSet = _SymbolDef.SymbolSet;
                    Rectangle t = bounds;
                    if (t.Height > _ScaleSymDef.SymbolFont.Height)
                    {
                        t.Height = _ScaleSymDef.SymbolFont.Height;
                        t.Y += (bounds.Height - _ScaleSymDef.SymbolFont.Height) / 2;
                    }
                    using (StringFormat sf = new StringFormat())
                    {
                        sf.Alignment = StringAlignment.Center;
                        using (Brush br = new SolidBrush(_SymbolDef.SymbolColor))
                            g.DrawString(_SymbolDef.SymbolRealized, _ScaleSymDef.SymbolFont, br, t, sf);
                    }
                }
                else if (ImageSizeMode == ImageSizeMode.Tile)
                {
                    Rectangle r = GetAdjustedBounds(clipBounds, ImagePadding);
                    g.SetClip(r, CombineMode.Intersect);
                    RenderSymbolTiled(g, symbol, r);
                }
                else
                {
                    Rectangle dbounds = GetSymbolBounds(g, clipBounds);
                    float n = Math.Max(bounds.Width, bounds.Height);
                    if (double.IsNaN(ImageScale) == false)
                    {
                        if (ImageScale != 1)
                            n = (int)(n * ImageScale);
                    }
                    n = dbounds.Width;
                    if (_ScaleSymDef == null)
                        _ScaleSymDef = new SymbolDef();
                    if (_SymFontScale < 0)
                    {
                        _ScaleSymDef.SymbolSize = n;
                        _ScaleSymDef.SymbolSet = _SymbolDef.SymbolSet;
                        _SymFontScale = n / _ScaleSymDef.SymbolFont.Height * .8f;
                    }
                    _ScaleSymDef.SymbolSize = n * _SymFontScale;
                    _ScaleSymDef.SymbolSet = _SymbolDef.SymbolSet;
                    Rectangle t = dbounds;
                    if (t.Height > _ScaleSymDef.SymbolFont.Height)
                    {
                        t.Height = _ScaleSymDef.SymbolFont.Height;
                        t.Y += (dbounds.Height - _ScaleSymDef.SymbolFont.Height) / 2;
                    }
                    g.SetClip(Rectangle.Intersect(bounds, t), CombineMode.Intersect);
                    using (Brush br = new SolidBrush(_SymbolDef.SymbolColor))
                        g.DrawString(symbol, _ScaleSymDef.SymbolFont, br, t);
                }
                g.TextRenderingHint = hint;
                g.Clip = clip;
            }
        }
        #region RenderSymbolTiled
        private void RenderSymbolTiled(Graphics g, string symbol, Rectangle bounds)
        {
            Font font = _SymbolDef.SymbolFont;
            Color color = _SymbolDef.SymbolColor;
            Size size = _SymbolDef.GetSymbolSize(g);
            Rectangle dr = new Rectangle(bounds.Location, size);
            Rectangle r = new Rectangle();
            r.X = bounds.X + bounds.Width / 2;
            r.Y = bounds.Y + bounds.Height / 2;
            int dx = ((bounds.Width / 2) / size.Width + 2) * size.Width;
            int dy = ((bounds.Height / 2) / size.Height + 2) * size.Height;
            
            dr.X = r.X - dx + size.Width / 2;
            dr.Y = r.Y - dy + size.Height / 2;
            int x = dr.X;
            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                while (dr.Top < bounds.Bottom)
                {
                    using (Brush br = new SolidBrush(_SymbolDef.SymbolColor))
                    {
                        while (dr.Left < bounds.Right)
                        {
                            g.DrawString(symbol, font, br, dr, sf);
                            dr.X += size.Width;
                        }
                    }
                    dr.X = x;
                    dr.Y += size.Height;
                }
            }
        }
        #endregion
        #endregion
        #endregion
        #region GetStringFormatFlags
        internal void GetStringFormatFlags(StringFormat sf)
        {
            if (AllowWrap == Tbool.False)
                sf.FormatFlags |= StringFormatFlags.NoWrap;
            switch (Alignment)
            {
                case Alignment.TopCenter:
                    sf.LineAlignment = StringAlignment.Near;
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.TopRight:
                    sf.LineAlignment = StringAlignment.Near;
                    sf.Alignment = StringAlignment.Far;
                    break;
                case Alignment.MiddleLeft:
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Near;
                    break;
                case Alignment.MiddleCenter:
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.MiddleRight:
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Far;
                    break;
                case Alignment.BottomLeft:
                    sf.LineAlignment = StringAlignment.Far;
                    sf.Alignment = StringAlignment.Near;
                    break;
                case Alignment.BottomCenter:
                    sf.LineAlignment = StringAlignment.Far;
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.BottomRight:
                    sf.LineAlignment = StringAlignment.Far;
                    sf.Alignment = StringAlignment.Far;
                    break;
            }
        }
        #endregion
        #region GetTextFormatFlags
        internal eTextFormat GetTextFormatFlags()
        {
            eTextFormat tf = eTextFormat.WordEllipsis |
                eTextFormat.NoPadding | eTextFormat.NoPrefix;
            if (AllowWrap == Tbool.True)
                tf |= eTextFormat.WordBreak;
            switch (Alignment)
            {
                case Alignment.TopCenter:
                    tf |= eTextFormat.HorizontalCenter;
                    break;
                case Alignment.TopRight:
                    tf |= eTextFormat.Right;
                    break;
                case Alignment.MiddleLeft:
                    tf |= eTextFormat.VerticalCenter;
                    break;
                case Alignment.NotSet:
                case Alignment.MiddleCenter:
                    tf |= eTextFormat.HorizontalCenter;
                    tf |= eTextFormat.VerticalCenter;
                    break;
                case Alignment.MiddleRight:
                    tf |= eTextFormat.Right;
                    tf |= eTextFormat.VerticalCenter;
                    break;
                case Alignment.BottomLeft:
                    tf |= eTextFormat.Bottom;
                    break;
                case Alignment.BottomCenter:
                    tf |= eTextFormat.Bottom;
                    tf |= eTextFormat.HorizontalCenter;
                    break;
                case Alignment.BottomRight:
                    tf |= eTextFormat.Bottom;
                    tf |= eTextFormat.Right;
                    break;
            }
            return (tf);
        }
        #endregion
        #region ApplyStyle
        /// 
        /// Applies the style to instance of this style.
        /// 
        /// Style to apply.
        public void ApplyStyle(PieCenterVisualStyle style)
        {
            base.ApplyStyle(style);
            if (style != null)
            {
                if (style.Alignment != Alignment.NotSet)
                    Alignment = style.Alignment;
                if (style.AllowWrap != Tbool.NotSet)
                    AllowWrap = style.AllowWrap;
                if (style._Background != null && style._Background.IsEmpty == false)
                    Background = style._Background.Copy();
                if (style._Border != null && style._Border.IsEmpty == false)
                    Border = style._Border.Copy();
                if (style.Font != null)
                    Font = style.Font;
                if (style.Image != null)
                {
                    Image = style.Image;
                    ImageIndex = -1;
                }
                if (style.ImageAlignment != Alignment.NotSet)
                    ImageAlignment = style.ImageAlignment;
                if (style.ImageIndex >= 0)
                {
                    Image = null;
                    ImageIndex = style.ImageIndex;
                }
                if (style.ImageInscribed != Tbool.NotSet)
                    ImageInscribed = style.ImageInscribed;
                if (style.ImageList != null)
                    ImageList = style.ImageList;
                if (style._ImageOverlay != ImageOverlay.NotSet)
                    ImageOverlay = style.ImageOverlay;
                if (style._ImagePadding != null && style._ImagePadding.IsEmpty == false)
                    ImagePadding = style._ImagePadding.Copy();
                if (double.IsNaN(style._ImageScale) == false)
                    ImageScale = style._ImageScale;
                if (style._ImageSizeMode != ImageSizeMode.NotSet)
                    ImageSizeMode = style.ImageSizeMode;
                if (style._Padding != null && style._Padding.IsEmpty == false)
                    Padding = style._Padding.Copy();
                if (style._SymbolDef != null && style._SymbolDef.IsEmpty == false)
                    SymbolDef = style._SymbolDef.Copy();
                if (style._TextColor.IsEmpty == false)
                    TextColor = style._TextColor;
                if (style._TextInscribed != Tbool.NotSet)
                    TextInscribed = style.TextInscribed;
            }
        }
        #endregion
        #region ApplyDefaults
        public override void ApplyDefaults()
        {
            if (_Alignment == Alignment.NotSet)
                Alignment = Alignment.MiddleCenter;
            if (_Border != null)
                Border.ApplyDefaults();
            if (_Font == null)
                Font = SystemFonts.DefaultFont;
            if (_ImageAlignment == Alignment.NotSet)
                ImageAlignment = Alignment.MiddleLeft;
            if (_ImageInscribed == Tbool.NotSet)
                ImageInscribed = Tbool.True;
            if (_ImageSizeMode == ImageSizeMode.NotSet)
                ImageSizeMode = ImageSizeMode.Normal;
            if (_SymbolDef != null && _SymbolDef.IsValidSymbol == true)
            {
                if (_SymbolDef.SymbolSize == 0)
                {
                    if (Font != null)
                        SymbolDef.SymbolSize = Font.SizeInPoints;
                }
                if (_SymbolDef.SymbolColor.IsEmpty == true)
                {
                    SymbolDef.SymbolColor = 
                        (TextColor.IsEmpty == false) ? TextColor : Color.Black;
                }
            }
            if (_TextColor.IsEmpty == true)
                TextColor = Color.Black;
            if (_TextInscribed == Tbool.NotSet)
                TextInscribed = Tbool.True;
            base.ApplyDefaults();
        }
        #endregion
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public new PieCenterVisualStyle Copy()
        {
            PieCenterVisualStyle style = new PieCenterVisualStyle();
            CopyTo(style);
            return (style);
        }
        #endregion
        #region CopyTo
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public void CopyTo(PieCenterVisualStyle style)
        {
            base.CopyTo(style);
            style.Alignment = _Alignment;
            style.AllowWrap = _AllowWrap;
            style.Background = (_Background == null) ? null : _Background.Copy();
            style.Border = (_Border != null) ? _Border.Copy() : null;
            style.Font = (_Font == null) ? null : (Font)_Font.Clone();
            style.Image = _Image;
            style.ImageAlignment = _ImageAlignment;
            style.ImageIndex = _ImageIndex;
            style.ImageInscribed = _ImageInscribed;
            style.ImageList = _ImageList;
            style.ImageOverlay = _ImageOverlay;
            style.ImagePadding = (_ImagePadding != null) ? _ImagePadding.Copy() : null;
            style.ImageScale = _ImageScale;
            style.ImageSizeMode = _ImageSizeMode;
            style.Padding = (_Padding != null) ? _Padding.Copy() : null;
            style.SymbolDef = (_SymbolDef != null) ? _SymbolDef.Copy() : null;
            style.TextColor = _TextColor;
            style.TextInscribed = _TextInscribed;
        }
        #endregion
        #region GetSerialData
        internal override SerialElementCollection GetSerialData(string serialName)
        {
            SerialElementCollection sec = new SerialElementCollection();
            if (serialName != null)
            {
                if (serialName.Equals("") == true)
                    serialName = "PieCenterVisualStyle";
                sec.AddStartElement(serialName);
            }
            sec.AddValue("Alignment", Alignment, Alignment.NotSet);
            sec.AddValue("AllowWrap", AllowWrap, Tbool.NotSet);
            if (_Background != null && _Background.IsEmpty == false)
                sec.AddElement(_Background.GetSerialData("Background"));
            if (_Border != null && _Border.IsEmpty == false)
                sec.AddElement(_Border.GetSerialData("Border"));
            if (_Font != null)
                sec.AddValue("Font", XmlSerializableFont.ConvertToString(Font));
            sec.AddValue("Image", Image);
            sec.AddValue("ImageAlignment", ImageAlignment, Alignment.NotSet);
            sec.AddValue("ImageIndex", ImageIndex, -1);
            sec.AddValue("ImageInscribed", ImageInscribed, Tbool.NotSet);
            if (_ImageList != null)
                sec.AddValue("ImageList", XmlSerializableImageList.ConvertToString(ImageList));
            sec.AddValue("ImageOverlay", ImageOverlay, ImageOverlay.NotSet);
            if (_ImagePadding != null && _ImagePadding.IsEmpty == false)
                sec.AddElement(_ImagePadding.GetSerialData("ImagePadding"));
            sec.AddValue("ImageScale", ImageScale, double.NaN);
            sec.AddValue("ImageSizeMode", ImageSizeMode, ImageSizeMode.NotSet);
            if (_Padding != null && _Padding.IsEmpty == false)
                sec.AddElement(_Padding.GetSerialData("Padding"));
            if (_SymbolDef != null && _SymbolDef.IsEmpty == false)
                sec.AddElement(_SymbolDef.GetSerialData("SymbolDef"));
            sec.AddValue("TextColor", TextColor, Color.Empty);
            sec.AddValue("TextInscribed", TextInscribed, Tbool.NotSet);
            sec.AddElement(base.GetSerialData(null));
            if (serialName != null)
                sec.AddEndElement("ContainerVisualStyle");
            return (sec);
        }
        #endregion
        #region PutSerialData
        #region ProcessValue
        internal override void ProcessValue(SerialElement se)
        {
            switch (se.Name)
            {
                case "Alignment":
                    Alignment = (Alignment)se.GetValueEnum(typeof(Alignment));
                    break;
                case "AllowWrap":
                    AllowWrap = (Tbool)se.GetValueEnum(typeof(Tbool));
                    break;
                case "Font":
                    Font = XmlSerializableFont.ConvertFromString(se.StringValue);
                    break;
                case "Image":
                    Image = se.GetValueImage();
                    break;
                case "ImageAlignment":
                    ImageAlignment = (Alignment)se.GetValueEnum(typeof(Alignment));
                    break;
                case "ImageIndex":
                    ImageIndex = int.Parse(se.StringValue);
                    break;
                case "ImageInscribed":
                    ImageInscribed = (Tbool)se.GetValueEnum(typeof(Tbool));
                    break;
                case "ImageList":
                    ImageList = XmlSerializableImageList.ConvertFromString(se.StringValue);
                    break;
                case "ImageOverlay":
                    ImageOverlay = (ImageOverlay)se.GetValueEnum(typeof(ImageOverlay));
                    break;
                case "ImageScale":
                    ImageScale = double.Parse(se.StringValue);
                    break;
                case "ImageSizeMode":
                    ImageSizeMode = (ImageSizeMode)se.GetValueEnum(typeof(ImageSizeMode));
                    break;
                case "TextColor":
                    TextColor = se.GetValueColor();
                    break;
                case "TextInscribed":
                    TextInscribed = (Tbool)se.GetValueEnum(typeof(Tbool));
                    break;
                default:
                    base.ProcessValue(se);
                    break;
            }
        }
        #endregion
        #region ProcessCollection
        internal override void ProcessCollection(SerialElement se)
        {
            SerialElementCollection sec = se.Sec;
            switch (se.Name)
            {
                case "Background":
                    sec.PutSerialData(Background);
                    break;
                case "Border":
                    sec.PutSerialData(Border);
                    break;
                case "ImagePadding":
                    sec.PutSerialData(ImagePadding);
                    break;
                case "Padding":
                    sec.PutSerialData(Padding);
                    break;
                case "SymbolDef":
                    sec.PutSerialData(SymbolDef);
                    break;
                default:
                    base.ProcessCollection(se);
                    break;
            }
        }
        #endregion
        #endregion
        #region IDisposable
        /// 
        /// Dispose
        /// 
        public override void Dispose()
        {
            Background = null;
            Border = null;
            ImagePadding = null;
            Padding = null;
            SymbolDef = null;
            _ScaleSymDef = null;
            base.Dispose();
        }
        #endregion
    }
}