using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
namespace DevComponents.DotNetBar.Layout
{
    [Designer("DevComponents.DotNetBar.Layout.Design.LayoutGroupDesigner, DevComponents.DotNetBar.Layout.Design, Version=14.1.0.37, Culture=neutral,  PublicKeyToken=7eb7c3a35b91de04")]
    public class LayoutGroup : LayoutItemBase
    {
        #region Constructor
        /// 
        /// Initializes a new instance of the LayoutGroup class.
        /// 
        public LayoutGroup()
        {
            _CaptionStyle = new SimpleStyle();
            _CaptionStyle.PropertyChanged += CaptionStylePropertyChanged;
            _Items = new LayoutItemCollection();
            _Items.SetParentItem(this);
        }
        protected override void OnDispose()
        {
            _CaptionStyle.PropertyChanged -= CaptionStylePropertyChanged;
            base.OnDispose();
        }
        #endregion
        #region Implementation
        private Rectangle _ActualBounds = Rectangle.Empty;
        /// 
        /// Gets actual bounds of elements inside of group. The actual bounds may differ from requested layout bounds due to minium
        /// size constraint.
        /// 
        internal Rectangle ActualBounds
        {
            get
            {
                return _ActualBounds;
            }
            set
            {
                _ActualBounds = value;
            }
        }
        protected override void OnPaint(PaintContext context)
        {
            OnPaintBackground(context);
            if (!_CaptionBounds.IsEmpty)
            {
                bool isImageVisible = this.IsImageVisible;
                Graphics g = context.Graphics;
                //context.Graphics.FillRectangle(Brushes.Aqua, _CaptionBounds);
                Color textColor = context.LabelTextColor;
                bool dispose = false;
                Brush textBrush = context.LabelTextBrush;
                if (_Appearance == eGroupAppearance.Panel)
                {
                    textColor = PaintPanelCaption(context);
                    textBrush = new SolidBrush(textColor);
                    dispose = true;
                }
                else if (_Appearance == eGroupAppearance.GroupPanel)
                {
                    textColor = PaintGroupPanelCaption(context);
                    textBrush = new SolidBrush(textColor);
                    dispose = true;
                }
                else if (_CaptionStyle.IsPainted)
                {
                    DrawingHelpers.PaintStyle(g, _CaptionStyle, _CaptionBounds);
                }
                if (!_CaptionStyle.TextColor.IsEmpty)
                {
                    textColor = _CaptionStyle.TextColor;
                    textBrush = new SolidBrush(textColor);
                    dispose = true;
                }
                if (this.TextPosition == eLayoutPosition.Top || this.TextPosition == eLayoutPosition.Bottom)
                {
                    PaintText(context, isImageVisible, textColor, ActualTextBounds, textBrush);
                }
                else
                {
                    // Rotate rendering
                    g.RotateTransform(-90);
                    //Rectangle r = new Rectangle(_CaptionBounds.Top, -_CaptionBounds.Right, _CaptionBounds.Height, _CaptionBounds.Width);
                    //Rectangle r = new Rectangle(-_CaptionBounds.Bottom, _CaptionBounds.X, _CaptionBounds.Height, _CaptionBounds.Width);
                    Rectangle r = new Rectangle(-ActualTextBounds.Bottom, ActualTextBounds.X, ActualTextBounds.Height, ActualTextBounds.Width);
                    PaintText(context, isImageVisible, textColor, r, textBrush);
                    g.ResetTransform();
                }
                if (dispose)
                    textBrush.Dispose();
                if (isImageVisible)
                    PaintImage(context, textColor);
            }
            foreach (LayoutItemBase item in _Items)
            {
                if (!item.Visible) continue;
                item.Paint(context);
            }
            //context.Graphics.DrawRectangle(Pens.Green, ActualBounds);
        }
        private Color PaintPanelCaption(PaintContext context)
        {
            Rectangle bounds = _CaptionBounds;
            if (!this.CaptionStyle.Margin.IsEmpty)
                bounds = Helpers.Deflate(bounds, this.CaptionStyle.Margin);
            Graphics g = context.Graphics;
            DevComponents.DotNetBar.Rendering.Office2007ColorTable table = ((DevComponents.DotNetBar.Rendering.Office2007Renderer)DevComponents.DotNetBar.Rendering.GlobalManager.Renderer).ColorTable;
            Color border = table.LegacyColors.PanelBorder;
            Color back1 = table.LegacyColors.PanelBackground;
            Color back2 = table.LegacyColors.PanelBackground2;
            Color textColor = table.LegacyColors.PanelText;
            int backAngle = table.LegacyColors.PanelBackgroundGradientAngle;
            PaintRectangle(bounds, g, border, back1, back2, backAngle);
            return textColor;
        }
        private Color PaintGroupPanelCaption(PaintContext context)
        {
            Rectangle r = _CaptionBounds;
            Graphics g = context.Graphics;
            ElementStyleDisplayInfo info = new ElementStyleDisplayInfo(_GroupPanelStyle, g, r);
            ElementStyleDisplay.Paint(info);
            
            return _GroupPanelStyle.TextColor;
        }
        const int CaptionEdgeDistance = 4;
        private Rectangle GetGroupPanelCaptionBounds(Rectangle r)
        {
            eLayoutPosition textPosition = TextPosition;
            eTextAlignment textAlignment = TextAlignment;
            Size imageSize = GetImageSize();
            if (!imageSize.IsEmpty) imageSize.Width += this.ImageTextSpacing;
            int captionWidth = this.RealTextSize.Width + TextPadding.Horizontal + imageSize.Width;
            if (textPosition == eLayoutPosition.Top || textPosition == eLayoutPosition.Bottom)
            {
                if (textAlignment == eTextAlignment.Left || textAlignment == eTextAlignment.Default)
                {
                    r.Width = captionWidth;
                    r.X += CaptionEdgeDistance;
                }
                else if (textAlignment == eTextAlignment.Right)
                {
                    r.X = r.Right - (captionWidth) - CaptionEdgeDistance;
                    r.Width = captionWidth;
                }
                else if (textAlignment == eTextAlignment.Center)
                {
                    r.X += (r.Width - captionWidth) / 2;
                    r.Width = captionWidth;
                }
            }
            else // Left/Right
            {
                if (textAlignment == eTextAlignment.Left || textAlignment == eTextAlignment.Default)
                {
                    r.Y = r.Bottom - captionWidth - CaptionEdgeDistance;
                    r.Height = captionWidth;
                }
                else if (textAlignment == eTextAlignment.Right)
                {
                    r.Height = captionWidth;
                    r.Y += CaptionEdgeDistance;
                }
                else if (textAlignment == eTextAlignment.Center)
                {
                    r.Y += (r.Height - captionWidth) / 2;
                    r.Height = captionWidth;
                }
            }
            return r;
        }
        /// 
        /// Paints the background of the item.
        /// 
        /// 
        protected override void OnPaintBackground(PaintContext context)
        {
            if (_Appearance == eGroupAppearance.Panel)
                PaintPanelBackground(context);
            else if (_Appearance == eGroupAppearance.GroupPanel)
                PaintGroupPanelBackground(context);
            else
                base.OnPaintBackground(context);
        }
        private void PaintPanelBackground(PaintContext context)
        {
            Rectangle bounds = this.Bounds;
            if (!this.Style.Margin.IsEmpty)
                bounds = Helpers.Deflate(bounds, this.Style.Margin);
            Graphics g = context.Graphics;
            DevComponents.DotNetBar.Rendering.Office2007ColorTable table = ((DevComponents.DotNetBar.Rendering.Office2007Renderer)DevComponents.DotNetBar.Rendering.GlobalManager.Renderer).ColorTable;
            Color border = table.LegacyColors.PanelBorder;
            Color back1 = table.LegacyColors.PanelBackground;
            Color back2 = table.LegacyColors.PanelBackground2;
            int backAngle = table.LegacyColors.PanelBackgroundGradientAngle;
            PaintRectangle(bounds, g, border, back1, back2, backAngle);
        }
        private static void PaintRectangle(Rectangle bounds, Graphics g, Color border, Color back1, Color back2, int backAngle)
        {
            if (bounds.Width <= 0 || bounds.Height <= 0)
                return;
            if (back2.IsEmpty)
            {
                if (!back1.IsEmpty)
                {
                    using (SolidBrush brush = new SolidBrush(back1))
                        g.FillRectangle(brush, bounds);
                }
            }
            else
            {
                using (System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(bounds, back1, back2, backAngle))
                    g.FillRectangle(brush, bounds);
            }
            if (!border.IsEmpty)
            {
                Rectangle borderBounds = bounds;
                borderBounds.Width--; borderBounds.Height--;
                using (Pen pen = new Pen(border))
                    g.DrawRectangle(pen, borderBounds);
            }
        }
        private ElementStyle _GroupPanelStyle = null;
        private void PaintGroupPanelBackground(PaintContext context)
        {
            InitializeGroupPanelStyle();
            DevComponents.DotNetBar.Rendering.Office2007ColorTable table = ((DevComponents.DotNetBar.Rendering.Office2007Renderer)DevComponents.DotNetBar.Rendering.GlobalManager.Renderer).ColorTable;
            _GroupPanelStyle.SetColorScheme(table.LegacyColors);
            Rectangle bounds = this.Bounds;
            if (!_CaptionBounds.IsEmpty)
            {
                eLayoutPosition tp = this.TextPosition;
                if (tp == eLayoutPosition.Top)
                {
                    bounds.Y += _CaptionBounds.Height / 2;
                    bounds.Height -= _CaptionBounds.Height / 2;
                }
                else if (tp == eLayoutPosition.Bottom)
                    bounds.Height -= _CaptionBounds.Height / 2;
                else if (tp == eLayoutPosition.Left || tp == eLayoutPosition.Default)
                {
                    bounds.X += _CaptionBounds.Width / 2;
                    bounds.Width -= _CaptionBounds.Width / 2;
                }
                else // Right
                    bounds.Width -= _CaptionBounds.Width / 2;
            }
            if (!this.Style.Margin.IsEmpty)
                bounds = Helpers.Deflate(bounds, this.Style.Margin);
            Graphics g = context.Graphics;
            ElementStyleDisplayInfo info = new ElementStyleDisplayInfo(_GroupPanelStyle, g, bounds);
            ElementStyleDisplay.Paint(info);
        }
        private void InitializeGroupPanelStyle()
        {
            if (_GroupPanelStyle != null) return;
            _GroupPanelStyle = new ElementStyle();
            _GroupPanelStyle.BackColorSchemePart = eColorSchemePart.PanelBackground;
            _GroupPanelStyle.BackColor2SchemePart = eColorSchemePart.PanelBackground2;
            _GroupPanelStyle.BorderColorSchemePart = eColorSchemePart.PanelBorder;
            _GroupPanelStyle.BackColorGradientAngle = 90;
            _GroupPanelStyle.CornerDiameter = 4;
            _GroupPanelStyle.BorderWidth = 1;
            _GroupPanelStyle.Border = DevComponents.DotNetBar.eStyleBorderType.Solid;
            _GroupPanelStyle.CornerType = DevComponents.DotNetBar.eCornerType.Rounded;
            _GroupPanelStyle.TextColorSchemePart = eColorSchemePart.PanelText;
        }
        protected override void OnUpdateLayout()
        {
            base.OnUpdateLayout();
        }
        private Rectangle _ContentBounds = Rectangle.Empty;
        private ItemLayout _Layout = new ItemLayout();
        /// 
        /// Performs group layout.
        /// 
        /// Layout context information.
        public void Layout(LayoutContext context)
        {
            Rectangle r = this.Bounds;
            // Calculate text-position
            r = LayoutGroupCaption(context, r);
            if (!_IsRootGroup)
                r = Helpers.Deflate(r, this.Padding);
            _ContentBounds = r;
            _Layout.Layout(this, r, context);
        }
        private int _CaptionHeight = 0;
        /// 
        /// Indicates explicit caption height in pixels.
        /// 
        [DefaultValue(0), Category("Appearance"), Description("Indicates caption height in pixels.")]
        public int CaptionHeight
        {
            get { return _CaptionHeight; }
            set
            {
                if (value != _CaptionHeight)
                {
                    int oldValue = _CaptionHeight;
                    _CaptionHeight = value;
                    OnCaptionHeightChanged(oldValue, value);
                }
            }
        }
        /// 
        /// Called when CaptionHeight property has changed.
        /// 
        /// Old property value
        /// New property value
        protected virtual void OnCaptionHeightChanged(int oldValue, int newValue)
        {
            this.InvalidateLayout();
            OnPropertyChanged(new PropertyChangedEventArgs("CaptionHeight"));
        }
        protected override Font GetTextFont(Font defaultFont)
        {
            Font font = defaultFont;
            if (_CaptionStyle.Font != null)
                font = _CaptionStyle.Font;
            return font;
        }
        /// 
        /// Updates the layout of the text inside of item.
        /// 
        protected override void UpdateTextBounds()
        {
            if (_CaptionBounds.IsEmpty) return;
            Rectangle actualTextBounds = Helpers.Deflate(_CaptionBounds, this.TextPadding);
            Rectangle actualImageBounds = Rectangle.Empty;
            bool isImageVisible = IsImageVisible;
            Size imageSize = Size.Empty;
            if (isImageVisible)
                imageSize = GetImageSize();
            eTextAlignment textAlignment = this.TextAlignment;
            Size realTextSize = this.RealTextSize;
            if (isImageVisible)
            {
                eLayoutPosition imagePosition = ImagePosition;
                int imageTextSpacing = ImageTextSpacing;
                if (TextPosition == eLayoutPosition.Top || TextPosition == eLayoutPosition.Bottom)
                {
                    if (imagePosition == eLayoutPosition.Default || imagePosition == eLayoutPosition.Right)
                    {
                        if (textAlignment == eTextAlignment.Right)
                            actualImageBounds = new Rectangle(actualTextBounds.Right - imageSize.Width - imageTextSpacing, actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                        else if (textAlignment == eTextAlignment.Center)
                        {
                            if (Appearance == eGroupAppearance.GroupPanel)
                            {
                                actualImageBounds = new Rectangle(actualTextBounds.Right - (imageSize.Width + CaptionEdgeDistance), actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                                actualTextBounds.Width = realTextSize.Width;
                            }
                            else
                                actualImageBounds = new Rectangle(actualTextBounds.X + actualTextBounds.Width / 2 + realTextSize.Width / 2 + this.ImageTextSpacing, actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                        }
                        else
                        {
                            actualImageBounds = new Rectangle(actualTextBounds.X + imageTextSpacing + realTextSize.Width, actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                            if (Appearance == eGroupAppearance.GroupPanel)
                                actualImageBounds.X -= CaptionEdgeDistance;
                        }
                    }
                    else if (imagePosition == eLayoutPosition.Left)
                    {
                        if (textAlignment == eTextAlignment.Right)
                        {
                            actualImageBounds = new Rectangle(actualTextBounds.Right - (realTextSize.Width + imageSize.Width + imageTextSpacing), actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                            if (Appearance == eGroupAppearance.GroupPanel)
                                actualImageBounds.X += CaptionEdgeDistance;
                        }
                        else if (textAlignment == eTextAlignment.Center)
                        {
                            if (Appearance == eGroupAppearance.GroupPanel)
                            {
                                actualImageBounds = new Rectangle(actualTextBounds.X + CaptionEdgeDistance, actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                                actualTextBounds.X = actualImageBounds.Right;
                                actualTextBounds.Width = realTextSize.Width;
                            }
                            else
                                actualImageBounds = new Rectangle(actualTextBounds.X + actualTextBounds.Width / 2 - (realTextSize.Width / 2 + this.ImageTextSpacing + imageSize.Width), actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                        }
                        else
                        {
                            actualImageBounds = new Rectangle(actualTextBounds.X, actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height);
                            actualTextBounds.X += actualImageBounds.Width + imageTextSpacing;
                            actualTextBounds.Width -= actualImageBounds.Width + imageTextSpacing;
                        }
                    }
                    else if (imagePosition == eLayoutPosition.Top)
                    {
                        actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2,
                                                 actualTextBounds.Y - (imageSize.Height + imageTextSpacing),
                                                 imageSize.Width,
                                                 imageSize.Height);
                    }
                    else if (imagePosition == eLayoutPosition.Bottom)
                    {
                        actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2,
                                                 actualTextBounds.Bottom + imageTextSpacing,
                                                 imageSize.Width,
                                                 imageSize.Height);
                    }
                }
                else
                {
                    if (imagePosition == eLayoutPosition.Default || imagePosition == eLayoutPosition.Right)
                    {
                        if (textAlignment == eTextAlignment.Right)
                        {
                            actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Y + imageTextSpacing, imageSize.Width, imageSize.Height);
                            actualTextBounds.Y += imageTextSpacing;
                            actualImageBounds.Height -= imageTextSpacing;
                            if (Appearance == eGroupAppearance.GroupPanel)
                            {
                                actualTextBounds.Y = actualImageBounds.Bottom - 10;
                                actualTextBounds.Height = (_CaptionBounds.Bottom - actualTextBounds.Y)+4;
                            }
                        }
                        else if (textAlignment == eTextAlignment.Center)
                        {
                            if (Appearance == eGroupAppearance.GroupPanel)
                            {
                                actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Y + CaptionEdgeDistance, imageSize.Width, imageSize.Height);
                                actualTextBounds.Y = actualImageBounds.Bottom;
                                actualTextBounds.Height = _CaptionBounds.Bottom - actualTextBounds.Y;
                            }
                            else
                                actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Y + actualTextBounds.Height / 2 - (realTextSize.Width / 2 + imageTextSpacing + imageSize.Height), imageSize.Width, imageSize.Height);
                        }
                        else
                        {
                            actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Bottom - (realTextSize.Width + imageTextSpacing + imageSize.Height), imageSize.Width, imageSize.Height);
                            if (Appearance == eGroupAppearance.GroupPanel)
                                actualImageBounds.Y += CaptionEdgeDistance;
                        }
                    }
                    else if (imagePosition == eLayoutPosition.Left)
                    {
                        if (textAlignment == eTextAlignment.Right)
                        {
                            actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Y + imageTextSpacing, imageSize.Width, imageSize.Height);
                            actualTextBounds.Y += imageTextSpacing;
                            actualImageBounds.Height -= imageTextSpacing;
                            if (Appearance == eGroupAppearance.GroupPanel)
                            {
                                actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Bottom - (CaptionEdgeDistance+imageSize.Width), imageSize.Width, imageSize.Height);
                                actualTextBounds.Height = actualImageBounds.Y - _CaptionBounds.Y;
                            }
                        }
                        else if (textAlignment == eTextAlignment.Center)
                        {
                            if (Appearance == eGroupAppearance.GroupPanel)
                            {
                                actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Bottom - (imageSize.Height + CaptionEdgeDistance), imageSize.Width, imageSize.Height);
                                actualTextBounds.Height -= realTextSize.Height;
                            }
                            else
                                actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Y + actualTextBounds.Height / 2 + realTextSize.Width / 2 + this.ImageTextSpacing, imageSize.Width, imageSize.Height);
                        }
                        else
                        {
                            actualImageBounds = new Rectangle(actualTextBounds.X + (actualTextBounds.Width - imageSize.Width) / 2, actualTextBounds.Bottom - imageSize.Height - imageTextSpacing, imageSize.Width, imageSize.Height);
                            actualTextBounds.Height -= actualImageBounds.Height + imageTextSpacing;
                        }
                    }
                    else if (imagePosition == eLayoutPosition.Top)
                    {
                        actualImageBounds = new Rectangle(actualTextBounds.X + imageTextSpacing,
                                                 actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2,
                                                 imageSize.Width,
                                                 imageSize.Height);
                        actualTextBounds.X += imageSize.Width + imageTextSpacing;
                        actualTextBounds.Width -= imageSize.Width + imageTextSpacing;
                    }
                    else if (imagePosition == eLayoutPosition.Bottom)
                    {
                        actualImageBounds = new Rectangle(actualTextBounds.Right - (imageSize.Width + imageTextSpacing),
                                                 actualTextBounds.Y + (actualTextBounds.Height - imageSize.Height) / 2,
                                                 imageSize.Width,
                                                 imageSize.Height);
                        actualTextBounds.Width -= imageSize.Width + imageTextSpacing;
                    }
                }
            }
            this.ActualTextBounds = actualTextBounds;
            this.ActualImageBounds = actualImageBounds;
        }
        private Rectangle _CaptionBounds = Rectangle.Empty;
        protected virtual Rectangle LayoutGroupCaption(LayoutContext context, Rectangle r)
        {
            _CaptionBounds = Rectangle.Empty;
            if (string.IsNullOrEmpty(this.Text) || !this.TextVisible)
                return r;
            eLayoutPosition textPosition = this.TextPosition;
            Size textSize = new Size();
            if (_CaptionHeight > 0)
            {
                textSize.Width = r.Width;
                textSize.Height = _CaptionHeight;
            }
            else
            {
                textSize = MeasureText(context);
                if (IsImageVisible)
                {
                    Size imageSize = GetImageSize();
                    textSize.Height = Math.Max(textSize.Height, imageSize.Height);
                }
            }
            if (textPosition == eLayoutPosition.Top)
            {
                _CaptionBounds = new Rectangle(r.X, r.Y, r.Width, textSize.Height);
                r.Y += _CaptionBounds.Height;
                r.Height -= _CaptionBounds.Height;
            }
            else if (textPosition == eLayoutPosition.Bottom)
            {
                _CaptionBounds = new Rectangle(r.X, r.Bottom - textSize.Height, r.Width, textSize.Height);
                r.Height -= _CaptionBounds.Height;
            }
            else if (textPosition == eLayoutPosition.Left || textPosition == eLayoutPosition.Default) // Text rotated to the side
            {
                _CaptionBounds = new Rectangle(r.X, r.Y, textSize.Height, r.Height);
                r.X += _CaptionBounds.Width;
                r.Width -= _CaptionBounds.Width;
            }
            else if (textPosition == eLayoutPosition.Right) // Text rotated to the side
            {
                _CaptionBounds = new Rectangle(r.Right - textSize.Height, r.Y, textSize.Height, r.Height);
                r.Width -= _CaptionBounds.Width;
            }
            if (Appearance == eGroupAppearance.GroupPanel)
                _CaptionBounds = GetGroupPanelCaptionBounds(_CaptionBounds);
            UpdateTextBounds();
            //if (Appearance == eGroupAppearance.GroupPanel && !ActualImageBounds.IsEmpty)
            //{
            //    if (textPosition == eLayoutPosition.Top || textPosition == eLayoutPosition.Bottom || textPosition == eLayoutPosition.Default)
            //    {
            //        if (TextAlignment != eTextAlignment.Center)
            //            ActualImageBounds = new Rectangle(ActualImageBounds.X + CaptionEdgeDistance, ActualImageBounds.Y, ActualImageBounds.Width, ActualImageBounds.Height);
            //    }
            //    else
            //    {
            //        ActualImageBounds = new Rectangle(ActualImageBounds.X, ActualImageBounds.Y + CaptionEdgeDistance, ActualImageBounds.Width, ActualImageBounds.Height);
            //    }
            //}
            return r;
        }
        private LayoutItemCollection _Items = null;
        /// 
        /// Gets the collection of layout items objects assigned to the current group. 
        /// 
        [Browsable(true), Category("Items"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public LayoutItemCollection Items
        {
            get
            {
                return _Items;
            }
        }
        //private eLayoutType _LayoutType = eLayoutType.Horizontal;
        ///// 
        ///// Indicates layout type performed by the group.
        ///// 
        //[DefaultValue(eLayoutType.Horizontal), Category("Appearance"), Description("Indicates layout type performed by the group.")]
        //public eLayoutType LayoutType
        //{
        //    get { return _LayoutType; }
        //    set
        //    {
        //        if (value != _LayoutType)
        //        {
        //            eLayoutType oldValue = _LayoutType;
        //            _LayoutType = value;
        //            OnLayoutTypeChanged(oldValue, value);
        //        }
        //    }
        //}
        ///// 
        ///// Called when LayoutType property has changed.
        ///// 
        ///// Old property value
        ///// New property value
        //protected virtual void OnLayoutTypeChanged(eLayoutType oldValue, eLayoutType newValue)
        //{
        //    OnPropertyChanged(new PropertyChangedEventArgs("LayoutType"));
        //}
        internal override void UpdateScrollBounds(int xScroll, int yScroll, bool moveControls)
        {
            base.UpdateScrollBounds(xScroll, yScroll, moveControls);
            _ActualBounds.Offset(xScroll, yScroll);
            _CaptionBounds.Offset(xScroll, yScroll);
            _ContentBounds.Offset(xScroll, yScroll);
            foreach (LayoutItemBase item in this.Items)
            {
                item.UpdateScrollBounds(xScroll, yScroll, moveControls);
            }
        }
        private bool _IsRootGroup = false;
        /// 
        /// Indicates whether group is used as a root group in LayoutControl.
        /// 
        [Browsable(false)]
        public bool IsRootGroup
        {
            get { return _IsRootGroup; }
            internal set
            {
                if (_IsRootGroup == value)
                    return;
                _IsRootGroup = value;
                OnPropertyChanged(new PropertyChangedEventArgs("IsRootGroup"));
            }
        }
        private SimpleStyle _CaptionStyle;
        /// 
        /// Gets the style for the group caption represented by Text property.
        /// 
        [Browsable(true), Category("Appearance"), Description("Gets the style for the group caption represented by Text property"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public SimpleStyle CaptionStyle
        {
            get { return _CaptionStyle; }
        }
        private void CaptionStylePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.Invalidate();
            //throw new Exception("The method or operation is not implemented.");
        }
        /// 
        /// Returns whether item is contained as child item by this item or one of its children.
        /// 
        /// Item to check.
        /// true if item is contained otherwise false
        public override bool IsChildItem(LayoutItemBase value)
        {
            foreach (LayoutItemBase item in _Items)
            {
                if (item == value) return true;
                if (item.IsChildItem(value)) return true;
            }
            return false;
        }
        private eGroupAppearance _Appearance = eGroupAppearance.None;
        /// 
        /// Indicates the built-in appearance of group. When set group is painted using one of the pre-defined styles.
        /// When set to None, default value, you can use Style and CaptionStyle properties to change the group appearance.
        /// 
        [DefaultValue(eGroupAppearance.None), Category("Appearance"), Description("Indicates the built-in appearance of group.")]
        public eGroupAppearance Appearance
        {
            get { return _Appearance; }
            set
            {
                if (value != _Appearance)
                {
                    eGroupAppearance oldValue = _Appearance;
                    _Appearance = value;
                    OnAppearanceChanged(oldValue, value);
                }
            }
        }
        /// 
        /// Called when Appearance property has changed.
        /// 
        /// Old property value
        /// New property value
        protected virtual void OnAppearanceChanged(eGroupAppearance oldValue, eGroupAppearance newValue)
        {
            this.Invalidate();
            this.InvalidateLayout();
            OnPropertyChanged(new PropertyChangedEventArgs("Appearance"));
        }
        /// 
        /// Scales item in response to the scaling of the LayoutControl. Should never be called directly.
        /// 
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override void ScaleItem(SizeF factor)
        {
            base.ScaleItem(factor);
            if (_CaptionHeight > 0)
            {
                bool widthChanged = factor.Width != 1f;
                bool heightChanged = factor.Height != 1f;
                if (heightChanged)
                {
                    _CaptionHeight = (int)(_CaptionHeight * factor.Height);
                }
            }
        }
        /// 
        /// Gets whether this item text-size is shared with other items inside of its parent group.
        /// 
        [Browsable(false)]
        public override bool IsTextSizeShared
        {
            get
            {
                return false;
            }
        }
        /// 
        /// Gets whether this item's text-baseline is shared with other items inside of its parent group.
        /// 
        [Browsable(false)]
        public override bool IsTextBaselineShared
        {
            get
            {
                return false;
            }
        }
        protected override void OnVisibleChanged(bool oldValue, bool newValue)
        {
            foreach (LayoutItemBase item in _Items)
            {
                item.Visible = newValue;
            }
            base.OnVisibleChanged(oldValue, newValue);
        }
        #endregion
    }
    /// 
    /// Specifies the layout type.
    /// 
    public enum eLayoutType
    {
        /// 
        /// Horizontal layout type where items are first ordered from left to right then wrapped to next line.
        /// 
        Horizontal,
        /// 
        /// Vertical layout type where items are ordered first from top to bottom then wrapped onto the next column.
        /// 
        Vertical
    }
    /// 
    /// Specifies built-in appearance for the LayoutGroup.
    /// 
    public enum eGroupAppearance
    {
        /// 
        /// Group does not use any built-in appearance.
        /// 
        None,
        /// 
        /// Group is rendered like standard DotNetBar Panel control.
        /// 
        Panel,
        /// 
        /// Group is rendered like GroupPanel control.
        /// 
        GroupPanel
    }
}