#if FRAMEWORK20
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevComponents.DotNetBar.ScrollBar;
using DevComponents.DotNetBar.Touch;
namespace DevComponents.DotNetBar.Schedule
{
    public class CalendarPanel : BaseItem
    {
        #region Private variables
        private CalendarView _CalendarView;         // Associated CalendarView
        private HScrollBarAdv _HScrollBar;          // Horizontal scroll bar
        private int _HScrollPos;
        private int _HsMaximum;
        private VScrollBarAdv _VScrollBar;          // Vertical scroll bar
        private int _VScrollPos;
        private int _VsMaximum;
        private Size _AutoScrollMinSize = Size.Empty;
        private Point _AutoScrollPosition = Point.Empty;
        private TouchHandler _TouchHandler;
        private Rectangle _InnerBounds;
        private HScrollBarAdv _AHScrollBar;         // Active Horizontal scroll bar
        private VScrollBarAdv _AVScrollBar;         // Active Vertical scroll bar
        #endregion
        /// 
        /// This is the main container panel for all
        /// BaseView and TimeRulerPanel objects
        /// 
        /// CalendarView
        public CalendarPanel(CalendarView calendarView)
        {
            _CalendarView = calendarView;
            SetIsContainer(true);
            _CalendarView.SelectedViewChanged += _CalendarView_SelectedViewChanged;
            if (BarFunctions.IsWindows7 && TouchHandler.IsTouchEnabled)
            {
                _TouchHandler = new TouchHandler(_CalendarView, eTouchHandlerType.Gesture);
                _TouchHandler.PanBegin += TouchHandlerPanBegin;
                _TouchHandler.Pan += TouchHandlerPan;
                _TouchHandler.PanEnd += TouchHandlerPanEnd;
            }
        }
        #region Internal properties
        #region AutoScrollMinSize
        internal Size AutoScrollMinSize
        {
            get { return (_AutoScrollMinSize); }
            set
            {
                _AutoScrollMinSize = value;
                RecalcSize();
            }
        }
        #endregion
        #region AutoScrollPosition
        internal Point AutoScrollPosition
        {
            get
            {
                Point pt = Point.Empty;
                if (_AHScrollBar != null && _AHScrollBar.Visible == true)
                    pt.X = _AHScrollBar.Value * _CalendarView.TimeLineColumnWidth;
                if (_AVScrollBar != null && _AVScrollBar.Visible == true)
                    pt.Y = _AVScrollBar.Value;
                return (pt);
            }
            set
            {
                if (_CalendarView.SelectedView == eCalendarView.TimeLine)
                {
                    if (_CalendarView.TimeLineColumnWidth > 0)
                        value.X /= _CalendarView.TimeLineColumnWidth;
                }
                if (_AHScrollBar != null && _AHScrollBar.Visible == true)
                {
                    if (value.X != _AHScrollBar.Value)
                    {
                        _AHScrollBar.Value =
                            Math.Min(_AHScrollBar.Maximum,
                            Math.Max(_AHScrollBar.Minimum, value.X));
                    }
                }
                if (_AVScrollBar != null && _AVScrollBar.Visible == true)
                {
                    if (value.Y != _AVScrollBar.Value)
                    {
                        _AVScrollBar.Value =
                            Math.Min(_AVScrollBar.Maximum,
                            Math.Max(_AVScrollBar.Minimum, value.Y));
                    }
                }
            }
        }
        #endregion
        #region AHScrollBar
        internal HScrollBarAdv AHScrollBar
        {
            get { return (_AHScrollBar); }
            set { _AHScrollBar = value; }
        }
        #endregion
        #region AVScrollBar
        internal VScrollBarAdv AVScrollBar
        {
            get { return (_AVScrollBar); }
            set { _AVScrollBar = value; }
        }
        #endregion
        #region Checkit
        internal bool Checkit
        {
            get { return (m_CheckMouseMovePressed); }
            set { m_CheckMouseMovePressed = value; }
        }
        #endregion
        #region HScrollBar
        internal HScrollBarAdv HScrollBar
        {
            get { return (_HScrollBar); }
        }
        #endregion
        #region InnerBounds
        internal Rectangle InnerBounds
        {
            get { return (_InnerBounds); }
        }
        #endregion
        #region VScrollBar
        internal VScrollBarAdv VScrollBar
        {
            get { return (_VScrollBar); }
        }
        #endregion
        #endregion
        #region Event processing
        /// 
        /// Handles SelectedView changes
        /// 
        /// 
        /// 
        void _CalendarView_SelectedViewChanged(object sender, SelectedViewEventArgs e)
        {
            if (e.OldValue == eCalendarView.TimeLine)
            {
                if (_AVScrollBar != null)
                    _AVScrollBar.Visible = false;
            }
            if (e.NewValue == eCalendarView.TimeLine)
            {
                if (_HScrollBar != null)
                    _HScrollBar.Visible = false;
            }
            switch (e.NewValue)
            {
                case eCalendarView.TimeLine:
                    AVScrollBar = VScrollBar;
                    AHScrollBar = _CalendarView.TimeLineHScrollPanel.ScrollBar;
                    break;
                case eCalendarView.Day:
                case eCalendarView.Week:
                    AVScrollBar = _CalendarView.WeekDayVScrollBar;
                    AHScrollBar = HScrollBar;
                    break;
                case eCalendarView.Year:
                    AVScrollBar = _CalendarView.YearVScrollPanel.ScrollBar;
                    AHScrollBar = HScrollBar;
                    break;
                default:
                    AVScrollBar = null;
                    AHScrollBar = null;
                    break;
            }
            Size asMinSize = Size.Empty;
            if (_AVScrollBar != null && _AVScrollBar.Visible == true)
                asMinSize.Height = _AVScrollBar.Maximum;
            if (_AHScrollBar != null && _AHScrollBar.Visible == true)
            {
                asMinSize.Width = _AHScrollBar.Maximum;
                if (e.NewValue == eCalendarView.TimeLine)
                    asMinSize.Width *= _CalendarView.TimeLineColumnWidth;
            }
            _AutoScrollMinSize = asMinSize;
        }
        #endregion
        #region RecalcSize
        /// 
        /// Performs object recalc processing
        /// 
        public override void RecalcSize()
        {
            _InnerBounds = Bounds;
            if (SubItems.Count > 0)
            {
                if (Bounds.Width > 0 && Bounds.Height > 0)
                {
                    switch (_CalendarView.SelectedView)
                    {
                        case eCalendarView.Day:
                        case eCalendarView.Week:
                            RecalcWeekDay();
                            break;
                        case eCalendarView.Month:
                            RecalcMonth();
                            break;
                        case eCalendarView.Year:
                            RecalcYear();
                            break;
                        case eCalendarView.TimeLine:
                            RecalcTimeLine();
                            break;
                    }
                    base.RecalcSize();
                }
            }
        }
        #region RecalcWeekDay
        /// 
        /// Recalculates all WeekDay views
        /// 
        private void RecalcWeekDay()
        {
            // Calculate the necessary params for calculating
            // the divided width for each subItems in our panel
            int trWidth = 0;
            int vsWidth = 0;
            int vCount = SubItems.Count;
            if (SubItems[0] is VScrollPanel)
            {
                vCount--;
                vsWidth = _CalendarView.VsWidth;
                if (SubItems[1] is TimeRulerPanel)
                {
                    vCount--;
                    trWidth = _CalendarView.TimeRulerWidth;
                }
            }
            // Configure the subItems based upon the
            // previously calculated display params
            if (vCount > 0)
                ConfigureWeekDayItems(vCount, trWidth, vsWidth);
            _InnerBounds.X += trWidth;
            _InnerBounds.Width -= (trWidth + vsWidth);
            _InnerBounds.Y += _CalendarView.WeekDayVScrollBar.Location.Y;
            _InnerBounds.Height -= _InnerBounds.Y;
        }
        #region ConfigureWeekDayItems
        /// 
        /// Configures the CalendarPanel subItems
        /// 
        /// View count
        /// TimeRuler width
        /// Vert Scrollbar width
        private void ConfigureWeekDayItems(int vCount, int trWidth, int vsWidth)
        {
            Rectangle r = Bounds;
            // Calculate our default viewWidth
            int bvWidth = Bounds.Width - (trWidth + vsWidth);
            float viewWidth = (float)bvWidth / vCount;
            // Sync our current item params with our current
            // horizontal scrollBar states
            ConfigDhScrollBar(ref r,
                ref viewWidth, vCount, trWidth, vsWidth);
            // If we have a TimRulerPanel, then
            // set it's location and size
            if (trWidth > 0)
            {
                r.X = Bounds.X;
                r.Width = trWidth;
                SubItems[1].Bounds = r;
                SubItems[1].RecalcSize();
            }
            // Loop through each BaseView item,
            // setting it's location and size 
            _CalendarView.StartSlice = -1;
            _CalendarView.NumberOfSlices = 0;
            float fxPos = Bounds.X + trWidth + _HScrollPos;
            BaseView view = null;
            int n = vCount;
            for (int i = 0; i < SubItems.Count; i++)
            {
                if (SubItems[i] is BaseView)
                {
                    r.X = (int)fxPos;
                    fxPos += viewWidth;
                    r.Width = (int)(fxPos - r.X - (--n > 0 ? 2 : 0));
                    SubItems[i].Bounds = r;
                    SubItems[i].Displayed = r.IntersectsWith(Bounds);
                    if (SubItems[i].Displayed == true)
                    {
                        view = SubItems[i] as BaseView;
                        if (view != null)
                        {
                            view.NeedRecalcLayout = true;
                            view.RecalcSize();
                        }
                    }
                }
            }
            // If we need to align the WorkDay times, then
            // make sure all displayed WeekDay views are kept in sync
            if (_CalendarView.ShowOnlyWorkDayHours == true && _CalendarView.IsMultiCalendar == true)
            {
                for (int i = 0; i < SubItems.Count; i++)
                {
                    WeekDayView wv = SubItems[i] as WeekDayView;
                    if (wv != null && wv.Displayed == true)
                    {
                        if (wv.LocalStartSlice != _CalendarView.StartSlice ||
                            wv.LocalNumberOfSlices != _CalendarView.NumberOfSlices)
                        {
                            wv.NeedRecalcLayout = true;
                            wv.RecalcSize();
                        }
                    }
                }
            }
            // If we have a VScrollPanel, then
            // set it's location and size
            if (vsWidth > 0)
            {
                r.X = Bounds.Right - vsWidth;
                r.Width = vsWidth;
                int delta = _CalendarView.AllDayPanelHeight;
                r.Y = delta;
                r.Height = 50;
                if (view != null)
                {
                    r.Y += view.ClientRect.Y + view.DayOfWeekHeaderHeight;
                    r.Height = Math.Max(r.Height, view.ClientRect.Height - (delta + view.DayOfWeekHeaderHeight));
                }
                SubItems[0].Bounds = r;
                SubItems[0].RecalcSize();
            }
        }
        #endregion
        #endregion
        #region RecalcMonth
        /// 
        /// Recalculates Month views
        /// 
        private void RecalcMonth()
        {
            // Configure the subItems based upon the
            // previously calculated display params
            ConfigureMonthItems(SubItems.Count);
        }
        #region ConfigureMonthItems
        /// 
        /// Configures the CalendarPanel subItems
        /// 
        /// View count
        private void ConfigureMonthItems(int vCount)
        {
            Rectangle r = Bounds;
            // Calculate our default viewWidth
            float viewWidth = (float)Bounds.Width / vCount;
            // Sync our current item params with our current
            // horizontal scrollBar states
            ConfigDhScrollBar(ref r, ref viewWidth, vCount, 0, 0);
            // Loop through each BaseView item,
            // setting it's location and size 
            float fxPos = Bounds.X + _HScrollPos;
            int n = vCount;
            for (int i = 0; i < SubItems.Count; i++)
            {
                if (SubItems[i] is BaseView)
                {
                    r.X = (int)fxPos;
                    fxPos += viewWidth;
                    r.Width = (int)(fxPos - r.X - (--n > 0 ? 2 : 0));
                    SubItems[i].Bounds = r;
                    SubItems[i].Displayed = r.IntersectsWith(Bounds);
                    if (SubItems[i].Displayed == true)
                    {
                        BaseView view = SubItems[i] as BaseView;
                        if (view != null)
                        {
                            view.NeedRecalcLayout = true;
                            view.RecalcSize();
                        }
                    }
                }
            }
        }
        #endregion
        #endregion
        #region RecalcYear
        /// 
        /// Recalculates all Year views
        /// 
        private void RecalcYear()
        {
            // Calculate the necessary params for calculating
            // the divided width for each subItems in our panel
            int vsWidth = 0;
            int vCount = SubItems.Count;
            if (SubItems[0] is VScrollPanel)
            {
                vCount--;
                vsWidth = _CalendarView.VsWidth;
            }
            // Configure the subItems based upon the
            // previously calculated display params
            if (vCount > 0)
                ConfigureYearItems(vCount, vsWidth);
            _InnerBounds.Y = _CalendarView.YearVScrollPanel.ScrollBar.Location.Y;
            _InnerBounds.Height -= _InnerBounds.Y;
        }
        #region ConfigureYearItems
        /// 
        /// Configures the CalendarPanel subItems
        /// 
        /// View count
        /// Vert Scrollbar width
        private void ConfigureYearItems(int vCount, int vsWidth)
        {
            Rectangle r = Bounds;
            // Calculate our default viewWidth
            int bvWidth = Bounds.Width;
            float viewWidth = (float) bvWidth/vCount;
            // Sync our current item params with our current
            // horizontal scrollBar states
            ConfigDhScrollBar(ref r, ref viewWidth, vCount, 0, vsWidth);
            // Locate and calculate the default size of our YearView
            BaseView view = null;
            for (int i = 0; i < SubItems.Count; i++)
            {
                view = SubItems[i] as BaseView;
                if (view != null)
                {
                    Rectangle t = r;
                    t.Width = (int) viewWidth;
                    SubItems[i].Bounds = t;
                    view.NeedRecalcLayout = true;
                    view.RecalcSize();
                    break;
                }
            }
            if (view != null)
            {
                // If we have a VScrollPanel, then
                // set it's location and size
                if (vsWidth > 0)
                {
                    if (_CalendarView.YearViewMax + 1 == view.ClientRect.Height)
                    {
                        SubItems[0].Visible = false;
                        ((VScrollPanel)SubItems[0]).DisableScrollBar();
                    }
                    else
                    {
                        bvWidth = Bounds.Width - vsWidth;
                        viewWidth = (float)bvWidth / vCount;
                        r = Bounds;
                        ConfigDhScrollBar(ref r, ref viewWidth, vCount, 0, vsWidth);
                        Rectangle t = r;
                        t.Y += view.ClientRect.Y;
                        t.Height = Math.Max(50, view.ClientRect.Height);
                        t.X = Bounds.Right - vsWidth;
                        t.Width = vsWidth;
                        SubItems[0].Bounds = t;
                        SubItems[0].RecalcSize();
                        SubItems[0].Visible = true;
                    }
                }
                // Loop through each BaseView item,
                // setting it's location and size
                float fxPos = Bounds.X + _HScrollPos;
                int n = vCount;
                for (int i = 0; i < SubItems.Count; i++)
                {
                    if (SubItems[i] is BaseView)
                    {
                        r.X = (int) fxPos;
                        fxPos += viewWidth;
                        r.Width = (int) (fxPos - r.X - (--n > 0 ? 2 : 0));
                        SubItems[i].Bounds = r;
                        SubItems[i].Displayed = r.IntersectsWith(Bounds);
                        if (SubItems[i].Displayed == true)
                        {
                            view = SubItems[i] as BaseView;
                            if (view != null)
                            {
                                view.NeedRecalcLayout = true;
                                view.RecalcSize();
                            }
                        }
                    }
                }
            }
        }
        #endregion
        #endregion
        #region RecalcTimeLine
        /// 
        /// Recalculates TimeLine views
        /// 
        private void RecalcTimeLine()
        {
            int vCount = SubItems.Count;
            if (SubItems.Contains("TimeLineHScrollPanel"))
                vCount--;
            if (SubItems.Contains("TimeLineHeaderPanel"))
                vCount--;
            if (vCount > 0)
                ConfigureTItems(vCount);
        }
        #region ConfigureTItems
        /// 
        /// Configures TimeLine view items
        /// 
        /// 
        private void ConfigureTItems(int vCount)
        {
            Rectangle r = Bounds;
            if (_CalendarView.TimeLineShowHScrollBar == true)
                r.Height -= _CalendarView.HsHeight;
            // TimeLineHeaderPanel
            int headerHeight = 0;
            int iHeaderPanel = SubItems.IndexOf("TimeLineHeaderPanel");
            if (iHeaderPanel >= 0)
            {
                if (iHeaderPanel != 0)
                {
                    BaseItem bi = SubItems[iHeaderPanel];
                    SubItems.RemoveAt(iHeaderPanel);
                    SubItems.Insert(0, bi);
                    iHeaderPanel = 0;
                }
                if (_CalendarView.TimeLineShowIntervalHeader == true)
                    headerHeight += _CalendarView.TimeLineIntervalHeaderHeight;
                if (_CalendarView.TimeLineShowPeriodHeader == true)
                    headerHeight += _CalendarView.TimeLinePeriodHeaderHeight;
                r.Y += headerHeight;
                r.Height -= headerHeight;
            }
            _InnerBounds.Y = r.Y;
            _InnerBounds.Height = r.Height;
            // Calculate our viewHeight
            int vsWidth = 0;
            float viewHeight = (float) r.Height/vCount;
            if (_CalendarView.TimeLineHeight > viewHeight)
            {
                viewHeight = _CalendarView.TimeLineHeight;
                vsWidth = _CalendarView.VsWidth;
            }
            // VScrollPanel
            ConfigureTvScrollBar(r, vCount, vsWidth);
            // Sync our current item params with our current
            // horizontal scrollBar states
            r.Width -= vsWidth;
            Rectangle r2 = r;
            if (_CalendarView.IsMultiCalendar == true &&
                _CalendarView.ShowTabs == true)
            {
                if (_CalendarView.TimeLineMultiUserTabOrientation == eOrientation.Vertical)
                {
                    r2.X += _CalendarView.MultiUserTabHeight + 4;
                    r2.Width -= (_CalendarView.MultiUserTabHeight + 4);
                }
                else
                {
                    r2.X += _CalendarView.TimeLineMultiUserTabWidth + 4;
                    r2.Width -= (_CalendarView.TimeLineMultiUserTabWidth + 4);
                }
                _InnerBounds.X = r2.X;
                _InnerBounds.Width = r2.Width;
            }
            // TimeLineHeaderPanel
            if (iHeaderPanel >= 0)
            {
                r2.Y = Bounds.Y;
                r2.Height = headerHeight;
                SubItems[iHeaderPanel].Bounds = r2;
                SubItems[iHeaderPanel].RecalcSize();
            }
            // TimeLineView
            r2 = r;
            float fxPos = r2.Y + _VScrollPos;
            for (int i = 0; i < SubItems.Count; i++)
            {
                if (SubItems[i] is TimeLineView)
                {
                    r2.Y = (int) fxPos;
                    fxPos += viewHeight;
                    r2.Height = (int) (fxPos - r2.Y);
                    SubItems[i].Bounds = r2;
                    SubItems[i].Displayed = r2.IntersectsWith(Bounds);
                    if (SubItems[i].Displayed == true)
                        SubItems[i].RecalcSize();
                }
            }
            // If we have an HScrollPanel, then
            // set it's location and size
            int iScrollPanel = SubItems.IndexOf("TimeLineHScrollPanel");
            if (iScrollPanel >= 0)
            {
                if (_CalendarView.IsMultiCalendar == true &&
                    _CalendarView.ShowTabs == true)
                {
                    if (_CalendarView.TimeLineMultiUserTabOrientation == eOrientation.Vertical)
                    {
                        r.X += (_CalendarView.MultiUserTabHeight + 4);
                        r.Width -= (_CalendarView.MultiUserTabHeight + 4);
                    }
                    else
                    {
                        r.X += (_CalendarView.TimeLineMultiUserTabWidth + 4);
                        r.Width -= (_CalendarView.TimeLineMultiUserTabWidth + 4);
                    }
                }
                r.Y = Bounds.Bottom - _CalendarView.HsHeight;
                r.Height = _CalendarView.HsHeight;
                SubItems[iScrollPanel].Bounds = r;
                SubItems[iScrollPanel].RecalcSize();
            }
        }
        #endregion
        #endregion
        #region Horizontal scrollbar support
        #region ConfigDhScrollBar
        /// 
        /// Configures default view horizontal scrollBar
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        private void ConfigDhScrollBar(ref Rectangle r,
            ref float viewWidth, int vCount, int trWidth, int vsWidth)
        {
            if (_CalendarView.ViewWidth > viewWidth)
            {
                viewWidth = _CalendarView.ViewWidth + 2;
                r.Height -= _CalendarView.HsHeight;
                _HsMaximum = (int)(viewWidth * vCount);
                if (_HScrollBar == null)
                    SetUpDhScrollBar();
                if (_HScrollBar != null)
                {
                    UpdateDhScrollBar(trWidth, vsWidth);
                    if (_HScrollBar.Value > _HsMaximum - _HScrollBar.LargeChange + 1)
                    {
                        _HScrollPos = -_HsMaximum - _HScrollBar.LargeChange;
                        _HScrollBar.Value = _HsMaximum - _HScrollBar.LargeChange;
                    }
                }
            }
            else
            {
                if (_HScrollBar != null)
                {
                    _HScrollPos = 0;
                    _HScrollBar.Value = 0;
                    _HScrollBar.Visible = false;
                }
            }
        }
        #endregion
        #region SetUpDhScrollBar
        /// 
        /// Sets up default view horizontal scrollbar
        /// 
        private void SetUpDhScrollBar()
        {
            _HScrollBar = new HScrollBarAdv();
            _HScrollBar.Height = _CalendarView.HsHeight;
            Control c = (Control)this.GetContainerControl(true);
            if (c != null)
                c.Controls.Add(_HScrollBar);
            _HScrollBar.ValueChanged += DhScrollBarValueChanged;
        }
        #endregion
        #region UpdateDhScrollBar
        /// 
        /// Updates default view horizontal scrollbar
        /// 
        private void UpdateDhScrollBar(int trWidth, int vsWidth)
        {
            Point pt = new Point(Bounds.X + trWidth,
                Bounds.Bottom - _CalendarView.HsHeight);
            _HScrollBar.Location = pt;
            _HScrollBar.Width = Math.Max(50, Bounds.Width - (trWidth + vsWidth));
            _HScrollBar.SmallChange = 10;
            _HScrollBar.LargeChange = _HScrollBar.Width;
            _HScrollBar.Maximum = _HsMaximum;
            _HScrollBar.Visible = true;
            _HScrollBar.BringToFront();
            _HScrollBar.Refresh();
        }
        #endregion
        #region DhScrollBarValueChanged
        /// 
        /// Processes default view horizontal
        /// scrollbar value changes
        /// 
        /// 
        /// 
        void DhScrollBarValueChanged(object sender, EventArgs e)
        {
            if (_HScrollPos != -_HScrollBar.Value)
            {
                _HScrollPos = -_HScrollBar.Value;
                NeedRecalcSize = true;
                Refresh();
            }
        }
        #endregion
        #endregion
        #region Vertical scrollbar support
        #region ConfigureTvScrollBar
        /// 
        /// Configures TimeLine View vertical scrollbar
        /// 
        /// 
        /// 
        /// 
        private void ConfigureTvScrollBar(Rectangle r, int vCount, int vsWidth)
        {
            if (vsWidth > 0)
            {
                _VsMaximum = _CalendarView.TimeLineHeight * vCount + 2;
                if (_VScrollBar == null)
                    SetUpTvScrollBar();
                if (_VScrollBar != null)
                {
                    UpdateTvScrollBar(r, vsWidth);
                    if (_VScrollBar.Value > _VsMaximum)
                        _VScrollBar.Value = _VsMaximum;
                }
            }
            else
            {
                if (_VScrollBar != null)
                {
                    _VScrollPos = 0;
                    _VScrollBar.Value = 0;
                    _VScrollBar.Visible = false;
                }
            }
        }
        #endregion
        #region SetUpTvScrollBar
        /// 
        /// Sets up TimeLine vertical scrollbar
        /// 
        private void SetUpTvScrollBar()
        {
            _VScrollBar = new VScrollBarAdv();
            _VScrollBar.Width = _CalendarView.VsWidth;
            Control c = (Control)this.GetContainerControl(true);
            if (c != null)
                c.Controls.Add(_VScrollBar);
            _VScrollBar.ValueChanged += VScrollBar_ValueChanged;
        }
        #endregion
        #region UpdateTvScrollBar
        /// 
        /// Updates our vertical scrollbar
        /// 
        /// 
        /// 
        private void UpdateTvScrollBar(Rectangle r, int vsWidth)
        {
            Point pt = new Point(r.Right - vsWidth, r.Top);
            _VScrollBar.Location = pt;
            _VScrollBar.Height = Math.Max(30, r.Height);
            _VScrollBar.SmallChange = _CalendarView.TimeLineHeight;
            _VScrollBar.LargeChange = _VScrollBar.Height;
            _VScrollBar.Maximum = _VsMaximum;
            _VScrollBar.Visible = true;
            _VScrollBar.BringToFront();
            _VScrollBar.Refresh();
        }
        #endregion
        #region VScrollBar_ValueChanged
        /// 
        /// Processes TimeLine vertical scrollbar changes
        /// 
        /// 
        /// 
        void VScrollBar_ValueChanged(object sender, EventArgs e)
        {
            if (_VScrollPos != -_VScrollBar.Value)
            {
                _VScrollPos = -_VScrollBar.Value;
                NeedRecalcSize = true;
                Refresh();
            }
        }
        #endregion
        #endregion
        #endregion
        #region Paint
        /// 
        /// Paint processing
        /// 
        /// 
        public override void Paint(ItemPaintArgs e)
        {
            if (_CalendarView.IsUpdateSuspended == false)
            {
                if (SubItems.Count > 0)
                {
                    PaintNonViewItems(e);
                    PaintBaseViewItems(e);
                }
            }
        }
        /// 
        /// Paints the non-BaseView items
        /// 
        /// ItemPaintArgs
        private void PaintNonViewItems(ItemPaintArgs e)
        {
            // Loop through each item, initiating
            // a paint on each one
            for (int i = 0; i < SubItems.Count; i++)
            {
                if ((SubItems[i] is BaseView) == false)
                {
                    if (SubItems[i].Bounds.IntersectsWith(e.ClipRectangle))
                        SubItems[i].Paint(e);
                }
            }
        }
        /// 
        /// Paints the BaseView items
        /// 
        /// ItemPaintArgs
        private void PaintBaseViewItems(ItemPaintArgs e)
        {
            Rectangle r = Bounds;
            if (_CalendarView.SelectedView == eCalendarView.TimeLine)
            {
                int n = 0;
                if (_CalendarView.TimeLineShowIntervalHeader == true)
                    n += _CalendarView.TimeLineIntervalHeaderHeight;
                
                if (_CalendarView.TimeLineShowPeriodHeader == true)
                    n += _CalendarView.TimeLinePeriodHeaderHeight;
                r.Y += n;
                if (_CalendarView.TimeLineShowHScrollBar == true)
                    r.Height -= (_CalendarView.HsHeight + n);
            }
            else
            {
                if (SubItems[0] is VScrollPanel)
                {
                    if (SubItems[0].Visible == true)
                        r.Width -= _CalendarView.VsWidth;
                }
                if (SubItems.Count > 1 && SubItems[1] is TimeRulerPanel)
                {
                    r.X = _CalendarView.TimeRulerWidth;
                    r.Width -= _CalendarView.TimeRulerWidth;
                }
            }
            Region rgnSave = e.Graphics.Clip;
            using (Region rgn = new Region(r))
            {
                e.Graphics.Clip = rgn;
                for (int i = 0; i < SubItems.Count; i++)
                {
                    if (SubItems[i] is BaseView)
                    {
                        if (SubItems[i].Displayed == true)
                            SubItems[i].Paint(e);
                    }
                }
            }
            e.Graphics.Clip = rgnSave;
        }
        #endregion
        #region Touch Handling
        #region Private variables
        private bool _TouchDrag;
        private Point _TouchStartLocation;
        private Point _TouchStartScrollPosition;
        private Rectangle _TouchInnerBounds;
        #endregion
        #region Private properties
        private int MaximumReversePageOffset
        {
            get { return 0; }
        }
        #endregion
        #region TouchHandlerPanBegin
        private void TouchHandlerPanBegin(object sender, Touch.GestureEventArgs e)
        {
            if (_CalendarView.TouchEnabled == true)
            {
                _TouchInnerBounds = _InnerBounds;
                _TouchStartLocation = e.Location;
                _TouchStartScrollPosition = AutoScrollPosition;
                _TouchDrag = true;
                e.Handled = true;
            }
        }
        #endregion
        #region TouchHandlerPan
        private void TouchHandlerPan(object sender, Touch.GestureEventArgs e)
        {
            if (_TouchDrag)
            {
                Point autoScrollPosition = AutoScrollPosition;
                Size autoScrollMinSize = AutoScrollMinSize;
                int offset = (_TouchStartLocation.X - e.Location.X);
                int offsetChange = offset + _TouchStartScrollPosition.X;
                // X Scroll
                bool overflowH = false;
                if (autoScrollMinSize.Width > _TouchInnerBounds.Width)
                {
                    if (offsetChange + MaximumReversePageOffset > autoScrollMinSize.Width)
                    {
                        autoScrollPosition.X = autoScrollMinSize.Width;
                        overflowH = true;
                    }
                    else if (offsetChange < MaximumReversePageOffset)
                    {
                        autoScrollPosition.X = MaximumReversePageOffset;
                        overflowH = true;
                    }
                    else
                        autoScrollPosition.X = offsetChange;
                }
                // Y Scroll
                bool overflowV = false;
                if (autoScrollMinSize.Height > _TouchInnerBounds.Height)
                {
                    offset = (_TouchStartLocation.Y - e.Location.Y);
                    offsetChange = offset + _TouchStartScrollPosition.Y;
                    if (offsetChange + MaximumReversePageOffset > autoScrollMinSize.Height)
                    {
                        autoScrollPosition.Y = autoScrollMinSize.Height;
                        overflowV = true;
                    }
                    else if (offsetChange < MaximumReversePageOffset)
                    {
                        autoScrollPosition.Y = MaximumReversePageOffset;
                        overflowV = true;
                    }
                    else
                        autoScrollPosition.Y = offsetChange;
                }
                if (AutoScrollPosition != autoScrollPosition)
                {
                    AutoScrollPosition = autoScrollPosition;
                    Update();
                }
                if (overflowH && overflowV && e.IsInertia)
                    EndTouchPan();
                e.Handled = true;
            }
        }
        #endregion
        #region TouchHandlerPanEnd
        private void TouchHandlerPanEnd(object sender, Touch.GestureEventArgs e)
        {
            if (_TouchDrag)
            {
                EndTouchPan();
                e.Handled = true;
            }
        }
        #endregion
        #region EndTouchPan
        private void EndTouchPan()
        {
            _TouchDrag = false;
            Point autoScrollPosition = AutoScrollPosition;
            Size autoScrollMinSize = AutoScrollMinSize;
            if (autoScrollMinSize.Width > _TouchInnerBounds.Width)
            {
                if (autoScrollMinSize.Width < autoScrollPosition.X)
                    autoScrollPosition = new Point(autoScrollMinSize.Width, autoScrollPosition.Y);
                else if (autoScrollPosition.X < 0)
                    autoScrollPosition = new Point(0, autoScrollPosition.Y);
            }
            if (autoScrollMinSize.Height > _TouchInnerBounds.Height)
            {
                if (autoScrollMinSize.Height < autoScrollPosition.Y)
                    autoScrollPosition = new Point(autoScrollPosition.X, autoScrollMinSize.Height);
                else if (autoScrollPosition.Y < 0)
                    autoScrollPosition = new Point(autoScrollPosition.X, 0);
            }
            if (AutoScrollPosition != autoScrollPosition)
                AutoScrollPosition = autoScrollPosition;
        }
        #endregion
        #endregion
        #region Copy
        /// 
        /// Returns copy of the item.
        /// 
        public override BaseItem Copy()
        {
            CalendarPanel objCopy = new CalendarPanel(_CalendarView);
            CopyToItem(objCopy);
            return (objCopy);
        }
        /// 
        /// Copies the CalendarPanel specific properties to new instance of the item.
        /// 
        /// New CalendarPanel instance
        protected override void CopyToItem(BaseItem copy)
        {
            CalendarPanel objCopy = copy as CalendarPanel;
            base.CopyToItem(objCopy);
        }
        #endregion
    }
}
#endif