namespace DevComponents.DotNetBar
{
    using System;
	using System.Drawing;
	using System.Resources;
    using System.ComponentModel;
    using DevComponents.DotNetBar.Rendering;
    /// 
    /// Defines an item that allows the toolbar customization.
    /// 
    [System.ComponentModel.ToolboxItem(false),System.ComponentModel.DesignTimeVisible(false)]
    public class CustomizeItem:PopupItem
    {
		private bool m_MouseOver;
		private string m_CustomizeStr="",m_ResetStr="";
		
		private string m_ThisToolTip;
		private bool m_Localized=false;
		private bool m_CustomizeItemVisible=true;
		private bool m_AutoSizeMenuImages=true;
		private System.Drawing.Size m_MenuImageSize=Size.Empty;
		/// 
		/// Creates new instance of CustomizeItem object.
		/// 
        public CustomizeItem()//:base()
        {
			this.GlobalItem=false;
			m_MouseOver=false;
			m_SystemItem=true;
			this.CanCustomize=false;
			LoadResources();
			this.AutoCollapseOnClick=false;
			m_MenuImageSize=new Size(16,16);
        }
		/// 
		/// Returns copy of CustomizeItem item
		/// 
		public override BaseItem Copy()
		{
			CustomizeItem objCopy=new CustomizeItem();
			this.CopyToItem(objCopy);
			objCopy.CustomizeItemVisible=m_CustomizeItemVisible;
			return objCopy;
		}
		/// 
		/// Called when item container has changed. If you override this method you must call the base implementation to allow default processing to occur.
		/// 
		/// Previous container of the item.
		protected internal override void OnContainerChanged(object objOldContainer)
		{
			base.OnContainerChanged(objOldContainer);
			LoadResources();
		}
		/// 
		/// Occurs when tooltip is about to be shown or hidden.
		/// 
		/// Specifies whether tooltip is shown or hidden.
		protected override void OnTooltip(bool bShow)
		{
			LoadResources();
			base.OnTooltip(bShow);
		}
        /// 
        /// Loads the resources (text) used by this item.
        /// 
		protected virtual void LoadResources()
		{
			if(!m_Localized)
			{
				if(this.GetOwner()!=null)
					m_Localized=true;
				using(LocalizationManager lm=new LocalizationManager(this.GetOwner() as IOwnerLocalize))
				{
					m_ThisToolTip=lm.GetLocalizedString(LocalizationKeys.CustomizeItemTooltip);
					this.Text=lm.GetLocalizedString(LocalizationKeys.CustomizeItemAddRemove);
                    m_CustomizeStr = lm.GetLocalizedString(LocalizationKeys.CustomizeItemCustomize);
					m_ResetStr=lm.GetLocalizedString(LocalizationKeys.CustomizeItemReset);
				}
			}
		}
		/// 
		/// Overriden. Draws the item.
		/// 
		/// Target Graphics object.
        public override void Paint(ItemPaintArgs pa)
        {
            if (this.SuspendLayout)
                return;
            eDotNetBarStyle effectiveStyle = EffectiveStyle;
            if (effectiveStyle == eDotNetBarStyle.Office2000)
                PaintOffice(pa);
            else
            {
                if (this.IsThemed && !this.IsOnMenu)
                    PaintThemed(pa);
                else if (effectiveStyle == eDotNetBarStyle.Office2003 || effectiveStyle == eDotNetBarStyle.VS2005 || BarFunctions.IsOffice2007Style(effectiveStyle))
                {
                    PaintOffice2003(pa);
                    return;
                }
                else
                    PaintDotNet(pa);
            }
            if (BaseItem.IsOnPopup(this))
                return;
            Point[] p = new Point[3];
            if (m_Orientation == eOrientation.Vertical)
            {
                p[0].X = m_Rect.Left + Dpi.Width7;
                p[0].Y = m_Rect.Top + Dpi.Height4;
                p[1].X = p[0].X - Dpi.Width3;
                p[1].Y = p[0].Y + Dpi.Height3;
                p[2].X = p[0].X;
                p[2].Y = p[0].Y + Dpi.Height6;
                pa.Graphics.FillPolygon(SystemBrushes.ControlText, p);
            }
            else
            {
                p[0].X = m_Rect.Left + (m_Rect.Width / 2) - Dpi.Width1;
                p[0].Y = m_Rect.Bottom - Dpi.Height4;
                p[1].X = p[0].X - Dpi.Width2;
                p[1].Y = p[0].Y - Dpi.Height3;
                p[2].X = p[1].X + Dpi.Width5;
                p[2].Y = p[1].Y;
                pa.Graphics.FillPolygon(SystemBrushes.ControlText, p);
            }
            if (this.DesignMode && this.Focused)
            {
                Rectangle r = m_Rect;
                //r.Inflate(-1,-1);
                DesignTime.DrawDesignTimeSelection(pa.Graphics, r, pa.Colors.ItemDesignTimeBorder);
            }
        }
		private void PaintThemed(ItemPaintArgs pa)
		{
			ThemeToolbar theme=pa.ThemeToolbar;
			ThemeToolbarParts part=ThemeToolbarParts.Button;
			ThemeToolbarStates state=ThemeToolbarStates.Normal;
			if(m_Expanded)
				state=ThemeToolbarStates.Pressed;
			else if(m_MouseOver)
				state=ThemeToolbarStates.Hot;
			theme.DrawBackground(pa.Graphics,part,state,m_Rect);
			theme=null;
			if(!BaseItem.IsOnPopup(this))
				return;
			Font objFont=null;
			eTextFormat objStringFormat=GetStringFormat();
			Point[] p;
			objFont=GetFont();
			Rectangle rect=m_Rect;
			rect.Inflate(-1,-1);
			rect.Width-=6;
			
			TextDrawing.DrawString(pa.Graphics,m_Text,objFont,SystemColors.ControlText,rect,objStringFormat);
			p=new Point[3];
			p[0].X=m_Rect.Right-8;
			p[0].Y=m_Rect.Top+m_Rect.Height/2+3;
			p[1].X=p[0].X-2;
			p[1].Y=p[0].Y-3;
			p[2].X=p[1].X+5;
			p[2].Y=p[1].Y;
			pa.Graphics.FillPolygon(SystemBrushes.ControlText,p);
		}
		private void PaintDotNet(ItemPaintArgs pa)
		{
            Rectangle r = m_Rect;
			System.Drawing.Graphics g=pa.Graphics;
            eDotNetBarStyle effectiveStyle = EffectiveStyle;
            if (effectiveStyle == eDotNetBarStyle.Office2010 || effectiveStyle == eDotNetBarStyle.Office2007 || effectiveStyle == eDotNetBarStyle.Windows7)
            {
                Office2007ButtonItemStateColorTable stateColors = null;
                if (pa.Renderer is Office2007Renderer)
                {
                    Office2007ButtonItemColorTable ct = ((Office2007Renderer)pa.Renderer).ColorTable.ButtonItemColors[0];
                    if (this.Expanded)
                        stateColors = ct.Expanded;
                    else if (m_MouseOver)
                        stateColors = ct.MouseOver;
                    else
                        stateColors = ct.Default;
                }
                if (stateColors != null)
                    Office2007ButtonItemPainter.PaintBackground(g, stateColors, r, RoundRectangleShapeDescriptor.RoundCorner2, false, true);
            }
            else
            {
                if (this.Expanded)
                {
                    r = new Rectangle(m_Rect.Left + Dpi.Width2, m_Rect.Top + Dpi.Height2, m_Rect.Width - Dpi.Width2, m_Rect.Height - Dpi.Height2);
                    g.FillRectangle(SystemBrushes.ControlDark, r);
                    r.Offset(-Dpi.Width2, -Dpi.Height2);
                    r.Height += Dpi.Height2;
                    DisplayHelp.FillRectangle(g, r, pa.Colors.ItemExpandedBackground, pa.Colors.ItemExpandedBackground2, pa.Colors.ItemExpandedBackgroundGradientAngle);
                    DisplayHelp.DrawRectangle(g, pa.Colors.MenuBorder, r);
                }
                else if (m_MouseOver)
                {
                    r = new Rectangle(m_Rect.Left, m_Rect.Top, m_Rect.Width - Dpi.Width2, m_Rect.Height);
                    DisplayHelp.FillRectangle(g, r, pa.Colors.ItemHotBackground, pa.Colors.ItemHotBackground2, pa.Colors.ItemHotBackgroundGradientAngle);
                    DisplayHelp.DrawRectangle(g, pa.Colors.ItemHotBorder, r);
                }
                if (!BaseItem.IsOnPopup(this))
                    return;
                if (this.Expanded)
                {
                    DisplayHelp.FillRectangle(g, m_Rect, pa.Colors.ItemExpandedBackground, pa.Colors.ItemExpandedBackground2, pa.Colors.ItemExpandedBackgroundGradientAngle);
                    DisplayHelp.DrawRectangle(g, pa.Colors.MenuBorder, m_Rect);
                }
            }
            Font objFont = null;
            eTextFormat objStringFormat = GetStringFormat();
            Point[] p;
			objFont=GetFont();
            Rectangle rect=m_Rect;
            rect.Inflate(-Dpi.Width1, -Dpi.Height1);
            rect.Width -= Dpi.Width6;
            rect.X += Dpi.Width4;
			
			TextDrawing.DrawString(g,m_Text,objFont,SystemColors.ControlText,rect,objStringFormat);
			p=new Point[3];
            p[0].X = m_Rect.Right - Dpi.Width6;
            p[0].Y = m_Rect.Top + m_Rect.Height / 2 + Dpi.Height3;
            p[1].X = p[0].X - Dpi.Width2;
            p[1].Y = p[0].Y - Dpi.Height3;
            p[2].X = p[1].X + Dpi.Width5;
			p[2].Y=p[1].Y;
			g.FillPolygon(SystemBrushes.ControlText,p);
		}
        private void PaintOffice2003(ItemPaintArgs pa)
        {
            // When on popup the Customize Item is painted same as in .NET style...
            if (BaseItem.IsOnPopup(this))
            {
                PaintDotNet(pa);
                return;
            }
            Graphics g = pa.Graphics;
            Rectangle r = m_Rect;
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            if (this.Orientation == eOrientation.Vertical)
            {
                // When on docked toolbar it has a special look...
                r.Y += Dpi.Height2;
                r.Height -= Dpi.Height1;
                r.X -= Dpi.Width2;
                r.Width += Dpi.Width2;
                if (pa.RightToLeft)
                {
                    path.AddLine(r.Right, r.Y, r.Right - Dpi.Width2, r.Y + Dpi.Height2);
                    path.AddLine(r.X + Dpi.Width2, r.Y + Dpi.Height2, r.X, r.Y);
                    path.AddLine(r.X, r.Bottom - Dpi.Height2, r.X + Dpi.Width2, r.Bottom);
                    path.AddLine(r.Right - Dpi.Width2, r.Bottom, r.Right, r.Bottom - Dpi.Height2);
                }
                else
                {
                    path.AddLine(r.X, r.Y, r.X + Dpi.Width2, r.Y + Dpi.Height2);
                    path.AddLine(r.Right - Dpi.Width2, r.Y + Dpi.Height2, r.Right, r.Y);
                    path.AddLine(r.Right, r.Bottom - Dpi.Height2, r.Right - Dpi.Width2, r.Bottom);
                    path.AddLine(r.X + Dpi.Width2, r.Bottom, r.X, r.Bottom - Dpi.Height2);
                }
                path.CloseAllFigures();
            }
            else
            {
                // When on docked toolbar it has a special look...
                r.X += Dpi.Width2;
                r.Width -= Dpi.Width1;
                r.Y -= Dpi.Height2;
                r.Height += Dpi.Height3;
                if (pa.RightToLeft)
                {
                    r.X -= Dpi.Width2;
                    path.AddLine(r.Right, r.Y, r.Right - Dpi.Width2, r.Y + Dpi.Height2);
                    path.AddLine(r.Right - Dpi.Width2, r.Bottom - Dpi.Height2, r.Right, r.Bottom);
                    path.AddLine(r.X + Dpi.Width2, r.Bottom, r.X, r.Bottom - Dpi.Height2);
                    path.AddLine(r.X, r.Y + Dpi.Height2, r.X + Dpi.Width2, r.Y);
                }
                else
                {
                    path.AddLine(r.X, r.Y, r.X + Dpi.Width2, r.Y + Dpi.Height2);
                    path.AddLine(r.X + Dpi.Width2, r.Bottom - Dpi.Height2, r.X, r.Bottom);
                    path.AddLine(r.Right - Dpi.Width2, r.Bottom, r.Right, r.Bottom - Dpi.Height2);
                    path.AddLine(r.Right, r.Y + Dpi.Height2, r.Right - Dpi.Width2, r.Y);
                }
                path.CloseAllFigures();
            }
            System.Drawing.Drawing2D.SmoothingMode smooth = g.SmoothingMode;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            if (this.Expanded)
                DisplayHelp.FillPath(g, path, pa.Colors.ItemPressedBackground, pa.Colors.ItemPressedBackground2, pa.Colors.ItemPressedBackgroundGradientAngle);
            else if (m_MouseOver)
                DisplayHelp.FillPath(g, path, pa.Colors.ItemHotBackground, pa.Colors.ItemHotBackground2, pa.Colors.ItemHotBackgroundGradientAngle);
            else
                DisplayHelp.FillPath(g, path, pa.Colors.CustomizeBackground, pa.Colors.CustomizeBackground2, pa.Colors.CustomizeBackgroundGradientAngle);
            g.SmoothingMode = smooth;
            //using(Pen pen=new Pen(SystemColors.Window,1))
            //    g.DrawLine(pen,r.Left+(m_Rect.Width-4)/2+1,r.Bottom-11+1,r.Left+(m_Rect.Width-4)/2+4+1,r.Bottom-11+1);
            if (this.Orientation == eOrientation.Vertical)
            {
                // Draw Arrow Shade
                Point[] p = new Point[3];
                p[0].X = r.Left + (m_Rect.Width - Dpi.Width4) / 2 + Dpi.Width3;
                p[0].Y = r.Bottom - Dpi.Height3 + Dpi.Height1;
                p[1].X = p[0].X - Dpi.Width2;
                p[1].Y = p[0].Y - Dpi.Height3;
                p[2].X = p[1].X + Dpi.Width5;
                p[2].Y = p[1].Y;
                using (SolidBrush brush = new SolidBrush(SystemColors.Window)) // SystemColors.HighlightText))
                    g.FillPolygon(brush, p);
                // Draw Arrow
                using (Pen pen = new Pen(pa.Colors.CustomizeText, 1))
                    g.DrawLine(pen, r.Left + (m_Rect.Width - Dpi.Width4) / 2, r.Bottom - Dpi.Height9, r.Left + (m_Rect.Width - Dpi.Width4) / 2 + Dpi.Width4, r.Bottom - Dpi.Height9);
                p = new Point[3];
                p[0].X = r.Left + (m_Rect.Width - Dpi.Width4) / 2 + Dpi.Width2;
                p[0].Y = r.Bottom - Dpi.Height3;
                p[1].X = p[0].X - Dpi.Width2;
                p[1].Y = p[0].Y - Dpi.Height3;
                p[2].X = p[1].X + Dpi.Width5;
                p[2].Y = p[1].Y;
                using (SolidBrush brush = new SolidBrush(pa.Colors.CustomizeText))
                    g.FillPolygon(brush, p);
            }
            else
            {
                // Draw Arrow Shade
                Point[] p = new Point[3];
                p[0].X = r.Left + (m_Rect.Width - Dpi.Width4) / 2 + Dpi.Width3;
                p[0].Y = r.Bottom - Dpi.Height5 + Dpi.Height1;
                p[1].X = p[0].X - Dpi.Width2;
                p[1].Y = p[0].Y - Dpi.Height3;
                p[2].X = p[1].X + Dpi.Width5;
                p[2].Y = p[1].Y;
                using (SolidBrush brush = new SolidBrush(SystemColors.Window)) // SystemColors.HighlightText))
                    g.FillPolygon(brush, p);
                // Draw Arrow
                using (Pen pen = new Pen(pa.Colors.CustomizeText, 1))
                    g.DrawLine(pen, r.Left + (m_Rect.Width - Dpi.Width4) / 2, r.Bottom - Dpi.Height11, r.Left + (m_Rect.Width - Dpi.Width4) / 2 + Dpi.Width4, r.Bottom - Dpi.Height11);
                p = new Point[3];
                p[0].X = r.Left + (m_Rect.Width - Dpi.Width4) / 2 + Dpi.Width2;
                p[0].Y = r.Bottom - Dpi.Height5;
                p[1].X = p[0].X - Dpi.Width2;
                p[1].Y = p[0].Y - Dpi.Height3;
                p[2].X = p[1].X + Dpi.Width5;
                p[2].Y = p[1].Y;
                using (SolidBrush brush = new SolidBrush(pa.Colors.CustomizeText))
                    g.FillPolygon(brush, p);
            }
        }
		private void PaintOffice(ItemPaintArgs pa)
		{
			System.Drawing.Graphics g=pa.Graphics;
			if(this.Expanded)
			{
				System.Windows.Forms.ControlPaint.DrawBorder3D(g,m_Rect,System.Windows.Forms.Border3DStyle.SunkenOuter);
			}
			else if(m_MouseOver)
			{
				System.Windows.Forms.ControlPaint.DrawBorder3D(g,m_Rect,System.Windows.Forms.Border3DStyle.RaisedInner);
			}
			if(!BaseItem.IsOnPopup(this))
				return;
			
			Font objFont=null;
			eTextFormat objStringFormat=GetStringFormat();
			objFont=GetFont();
            Rectangle rect=m_Rect;
			rect.Inflate(-Dpi.Width1,-Dpi.Height1);
			rect.Width-=Dpi.Width6;
			if(this.Expanded)
			{
                rect.Offset(Dpi.Width1, Dpi.Height1);
                rect.Width -= Dpi.Width1;
                rect.Height -= Dpi.Height1;
			}
			TextDrawing.DrawString(g,m_Text,objFont,SystemColors.ControlText,rect,objStringFormat);
			Point[] p=new Point[3];
            p[0].X = m_Rect.Right - Dpi.Width8;
            p[0].Y = m_Rect.Top + m_Rect.Height / 2 + Dpi.Height3;
            p[1].X = p[0].X - Dpi.Width2;
            p[1].Y = p[0].Y - Dpi.Height3;
            p[2].X = p[1].X + Dpi.Width5;
			p[2].Y=p[1].Y;
			g.FillPolygon(SystemBrushes.ControlText,p);
		}
        /// 
        /// Sets the custom system tooltip text for the item.
        /// 
        /// Tooltip text.
        protected virtual void SetCustomTooltip(string text)
        {
            this.Tooltip = text;
        }
		/// 
		/// Overriden. Recalculates the size of the item.
		/// 
		public override void RecalcSize()
		{
			if(this.SuspendLayout)
				return;
			if(!BaseItem.IsOnPopup(this))
			{
				if(m_Orientation==eOrientation.Vertical)
				{
					// Take suggested width
					m_Rect.Height=Dpi.Width14;
					m_Rect.Width=Dpi.Height22;
				}
				else
				{
					// Take suggested height
                    m_Rect.Width = Dpi.Width14;
					m_Rect.Height=Dpi.Height22;
				}
				m_BeginGroup=false;
                SetCustomTooltip(GetTooltipText());
			}
			else
			{
				SetCustomTooltip("");
				m_BeginGroup=true;
				System.Windows.Forms.Control objCtrl=this.ContainerControl as System.Windows.Forms.Control;
				if(!IsHandleValid(objCtrl))
					return;
				Graphics g=Graphics.FromHwnd(objCtrl.Handle);
				// Get the right image size that we will use for calculation
				Size objImageSize=Size.Empty;
				if(m_Parent!=null)
				{
					ImageItem objParentImageItem=m_Parent as ImageItem;
					if(objParentImageItem!=null)
						objImageSize=new Size(objParentImageItem.SubItemsImageSize.Width,objParentImageItem.SubItemsImageSize.Height);
					else
						objImageSize=this.ImageSize;
				}
				else
					objImageSize=this.ImageSize;
				// Measure string
				Font objCurrentFont=null;
				objCurrentFont=GetFont();
				Size objStringSize=Size.Empty;
				eTextFormat objStringFormat=GetStringFormat();
				
				if(m_Text!="")
				{
					objStringSize=TextDrawing.MeasureString(g,m_Text,objCurrentFont,512,objStringFormat);
					objStringSize.Width+=Dpi.Width2;
				}
				// Calculate item height				
				if(objStringSize.Height>objImageSize.Height)
					m_Rect.Height=(int)objStringSize.Height+Dpi.Height4;
				else
					m_Rect.Height=objImageSize.Height+Dpi.Height4;
				m_Rect.Width=(int)objStringSize.Width+Dpi.Width15;
			}
			base.RecalcSize();
		}
        /// 
        /// Gets localized tooltip text for this instance of the item.
        /// 
        /// Tooltip text.
        protected virtual string GetTooltipText()
        {
            return m_ThisToolTip;
        }
		/*private bool IsOnPopUp()
		{
			if(this.ContainerControl is PopupMenu)
				return true;
			Bar objTlb=this.ContainerControl as Bar;
			if(objTlb!=null && objTlb.BarState==eBarState.Popup)
				return true;
			return false;
		}*/
		[System.ComponentModel.Browsable(false),System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
		public override void InternalMouseEnter()
		{
			base.InternalMouseEnter();
			m_MouseOver=true;
			this.Refresh();
		}
		[System.ComponentModel.Browsable(false),System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
		public override void InternalMouseHover()
		{
			base.InternalMouseHover();
            MouseHoverCustomize();
		}
		[System.ComponentModel.Browsable(false),System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
		public override void InternalMouseLeave()
		{
			base.InternalMouseLeave();
			m_MouseOver=false;
			this.Refresh();
		}
        [System.ComponentModel.Browsable(false),System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
		public override void InternalMouseDown(System.Windows.Forms.MouseEventArgs objArg)
		{
			base.InternalMouseDown(objArg);
			if(objArg.Button != System.Windows.Forms.MouseButtons.Left || this.DesignMode)
				return;
            MouseDownAction();
		}
        protected virtual void MouseDownAction()
        {
            this.Expanded = !m_Expanded;
        }
        /// 
        /// Called when mouse hovers over the customize item.
        /// 
        protected virtual void MouseHoverCustomize()
        {
            if (!this.Expanded && BaseItem.IsOnPopup(this))
                this.Expanded = true;
        }
        /// 
        /// Gets whether the mouse is over the item.
        /// 
        [Browsable(false)]
        public bool IsMouseOver
        {
            get { return m_MouseOver; }
        }
		protected internal override void OnExpandChange()
		{
            if (this.Expanded)
                SetupCustomizeItem();
			base.OnExpandChange();
            if (!this.Expanded)
                ClearCustomizeItem();
		}
        protected virtual void SetupCustomizeItem()
        {
            if (BaseItem.IsOnPopup(this))
            {
                AddCustomizeItems();
                this.PopupType = ePopupType.Menu;
            }
            else
            {
                this.SubItems.Clear();
                CustomizeItem btn = new CustomizeItem();
                btn.CustomizeItemVisible = this.CustomizeItemVisible;
                btn.IsRightToLeft = this.IsRightToLeft;
                this.SubItems.Add(btn);
                this.PopupType = ePopupType.ToolBar;
            }
        }
        protected virtual void ClearCustomizeItem()
        {
            this.SubItems.Clear();
        }
		private void AddRemoveClick(object sender)
		{
			((ButtonItem)sender).Expanded=!((ButtonItem)sender).Expanded;
		}
		private void ShowHideClick(object sender, System.EventArgs e)
		{
			BaseItem objBtn=((BaseItem)sender).Tag as BaseItem;
			bool bGlobal=objBtn.GlobalItem;
			objBtn.GlobalItem=false;
			objBtn.SetVisibleDirect(!objBtn.Visible);
            if (objBtn is TextBoxItem || objBtn is ComboBoxItem || objBtn is ControlContainerItem || objBtn.ContainerControl is NavigationBar)
                objBtn.OnVisibleChanged(objBtn.Visible);
            objBtn.UserCustomized = true;
			objBtn.GlobalItem=bGlobal;
			((BaseItem)sender).Visible=!((BaseItem)sender).Visible;
			
			if(objBtn.ContainerControl is Bar)
				((Bar)objBtn.ContainerControl).RecalcLayout();
			else if(objBtn.ContainerControl is MenuPanel)
				((MenuPanel)objBtn.ContainerControl).RecalcSize();
			else if(objBtn.ContainerControl is BarBaseControl)
				((BarBaseControl)objBtn.ContainerControl).RecalcLayout();
			
			((BaseItem)sender).Refresh();
			IOwner owner=this.GetOwner() as IOwner;
			if(owner!=null)
			{
				owner.InvokeUserCustomize(objBtn,new EventArgs());
				owner.InvokeEndUserCustomize(objBtn,new EndUserCustomizeEventArgs(eEndUserCustomizeAction.ItemVisibilityChanged));
			}
		}
		private void CustomizeClick(object sender, System.EventArgs e)
		{
			IOwner owner=this.GetOwner() as IOwner;
			if(owner==null)
				return;
			CollapseAll(this);
			owner.Customize();
		}
		private void ResetClick(object sender, System.EventArgs e)
		{
			IOwner owner=this.GetOwner() as IOwner;
			if(owner==null)
				return;
			BaseItem item=this;
			if(BaseItem.IsOnPopup(this) && this.Parent!=null)
				item=this.Parent;
			CollapseAll(this);
			owner.InvokeResetDefinition(item,new EventArgs());
		}
		/// 
		/// Forces the repaint the item.
		/// 
		public override void Refresh()
		{
			if(this.SuspendLayout)
				return;
            if ((EffectiveStyle == eDotNetBarStyle.Office2003 || EffectiveStyle == eDotNetBarStyle.VS2005 || BarFunctions.IsOffice2007Style(this.EffectiveStyle)) && !BaseItem.IsOnPopup(this))
			{
				if((m_Visible || this.IsOnCustomizeMenu) && m_Displayed)
				{
					System.Windows.Forms.Control objCtrl=this.ContainerControl as System.Windows.Forms.Control;
					if(objCtrl!=null && IsHandleValid(objCtrl) && !(objCtrl is ItemsListBox))
					{
						if(m_NeedRecalcSize)
						{
							RecalcSize();
							if(m_Parent!=null)
								m_Parent.SubItemSizeChanged(this);
						}
						Rectangle r=m_Rect;
						r.Inflate(2,2);
						objCtrl.Invalidate(r,true);
					}
				}
			}
			else
				base.Refresh();
		}
		private void AddCustomizeItems()
		{
			BaseItem objTmp;
			BaseItem objParent;
			this.SubItems.Clear();
			// Find the right parent item
			/*System.Windows.Forms.Control objCtrl=this.ContainerControl as System.Windows.Forms.Control;
			if(objCtrl is Bar)
			{
				Bar objTlb=objCtrl as Bar;
				if(objTlb.Parent==null)
					objParent=this.Parent;
				else
					objParent=objTlb.Parent;
			}
			else
			{
				objParent=this.Parent;
			}*/
			objParent=this.Parent;
			while(objParent!=null && objParent.SystemItem && !(objParent.SystemItem && objParent is GenericItemContainer))
				objParent=objParent.Parent;
			if(objParent==null)
				return;
			foreach(BaseItem objItem in objParent.SubItems)
			{
				if(!objItem.SystemItem && objItem.CanCustomize)
				{
					objTmp=objItem.Copy();
					objTmp.GlobalItem=false;
					objTmp.ClearClick();
					objTmp.BeginGroup=false;
					objTmp.Enabled=true;
					objTmp.SubItems.Clear();
					objTmp.Tooltip="";
					objTmp.SetIsOnCustomizeMenu(true);
					if(objItem is ButtonItem)
					{
						((ButtonItem)objTmp).HotTrackingStyle=eHotTrackingStyle.Default;
                        if (m_AutoSizeMenuImages && !m_MenuImageSize.IsEmpty && ((ButtonItem)objTmp).ImageSize != m_MenuImageSize)
                        {
                            ((ButtonItem)objTmp).ImageFixedSize = m_MenuImageSize;
                            ((ButtonItem)objTmp).UseSmallImage = true;
                        }
					}
					objTmp.Click+=new System.EventHandler(ShowHideClick);
					objTmp.Tag=objItem;
					this.SubItems.Add(objTmp);
				}
			}
			if(objParent is GenericItemContainer && ((GenericItemContainer)objParent).MoreItems!=null)
			{
				BaseItem objMore=((GenericItemContainer)objParent).MoreItems;
				foreach(BaseItem objItem in objMore.SubItems)
				{
					if(!objItem.SystemItem)
					{
						objTmp=objItem.Copy();
						objTmp.GlobalItem=false;
						objTmp.ClearClick();
						objTmp.BeginGroup=false;
						objTmp.Enabled=true;
						objTmp.SubItems.Clear();
						objTmp.Tooltip="";
						objTmp.SetIsOnCustomizeMenu(true);
						objTmp.Click+=new System.EventHandler(ShowHideClick);
						objTmp.Tag=objItem;
						this.SubItems.Add(objTmp);
					}
				}
			}
			objTmp=null;
			ButtonItem objBtn=null;
			IOwner owner=this.GetOwner() as IOwner;
			if(owner!=null && owner.ShowResetButton)
			{
				// Reset Bar Item
				objBtn=new ButtonItem();
				objBtn.GlobalItem=false;
				objBtn.BeginGroup=true;
				objBtn.Text=m_ResetStr; // "&Reset Bar";
				objBtn.SetIsOnCustomizeMenu(true);
				objBtn.SetSystemItem(true);
				objBtn.Orientation=eOrientation.Horizontal;
				objBtn.Click+=new System.EventHandler(ResetClick);
				this.SubItems.Add(objBtn);
			}
			if(m_CustomizeItemVisible)
			{
				// Customize
				objBtn=new ButtonItem();
				objBtn.GlobalItem=false;
				if(owner==null || owner!=null && !owner.ShowResetButton)
					objBtn.BeginGroup=true;
				objBtn.Text=m_CustomizeStr; //"&Customize...";
				objBtn.SetIsOnCustomizeMenu(true);
				objBtn.SetSystemItem(true);
				objBtn.Click+=new System.EventHandler(CustomizeClick);
				this.SubItems.Add(objBtn);
			}
			m_NeedRecalcSize=false;
		}
        private eTextFormat GetStringFormat()
		{
            eTextFormat format = eTextFormat.Default;
            format |= eTextFormat.SingleLine;
            //format |= eTextFormat.EndEllipsis;
            format |= eTextFormat.VerticalCenter;
            return format;
            //StringFormat sfmt=BarFunctions.CreateStringFormat(); //new StringFormat(StringFormat.GenericDefault);
            //sfmt.HotkeyPrefix=System.Drawing.Text.HotkeyPrefix.Show;
            ////sfmt.FormatFlags=sfmt.FormatFlags & ~(sfmt.FormatFlags & StringFormatFlags.DisableKerning);
            //sfmt.FormatFlags=sfmt.FormatFlags | StringFormatFlags.NoWrap;
            //sfmt.Alignment=System.Drawing.StringAlignment.Near;
            //sfmt.LineAlignment=System.Drawing.StringAlignment.Center;
            //return sfmt;
		}
		/// 
		/// Returns the Font object to be used for drawing the item text.
		/// 
		/// Font object.
		protected virtual Font GetFont()
		{
			System.Windows.Forms.Control objCtrl=this.ContainerControl as System.Windows.Forms.Control;
			if(objCtrl!=null)
				return (Font)objCtrl.Font;
            return SystemFonts.DefaultFont;// (Font)System.Windows.Forms.SystemInformation.MenuFont;
		}
		/// 
		/// Gets or sets a value indicating whether the item is visible.
		/// 
        [Browsable(true), DevCoBrowsable(true), DefaultValue(true), Category("Behavior"), Description("Determines whether the item is visible or hidden.")]
        public override bool Visible
		{
			get
			{
				Bar objTlb=this.ContainerControl as Bar;
				if(objTlb!=null)
				{
					if(objTlb.BarState==eBarState.Docked || objTlb.BarState==eBarState.Popup)
						return base.Visible;
					else
						return false;
				}
				return base.Visible;
			}
			set
			{
				base.Visible=value;
			}
		}
		/// 
		/// Gets or sets whether Customize menu item is visible.
		/// 
		[Browsable(true),DevCoBrowsable(true),Category("Behavior"),Description("Indicates whether Customize menu item is visible."),DefaultValue(true)]
		public virtual bool CustomizeItemVisible
		{
			get {return m_CustomizeItemVisible;}
			set {m_CustomizeItemVisible=value;}
		}
		/// 
		/// Overloaded. Serializes the item and all sub-items into the XmlElement.
		/// 
		/// XmlElement to serialize the item to.
		protected internal override void Serialize(ItemSerializationContext context)
		{
			base.Serialize(context);
            System.Xml.XmlElement ThisItem = context.ItemXmlElement;
			if(!m_CustomizeItemVisible)
				ThisItem.SetAttribute("customizeitemvisible",System.Xml.XmlConvert.ToString(m_CustomizeItemVisible));
		}
		/// 
		/// Overloaded. Deserializes the Item from the XmlElement.
		/// 
		/// Source XmlElement.
		public override void Deserialize(ItemSerializationContext context)
		{
			base.Deserialize(context);
            System.Xml.XmlElement ItemXmlSource = context.ItemXmlElement;
			if(ItemXmlSource.HasAttribute("customizeitemvisible"))
				m_CustomizeItemVisible=System.Xml.XmlConvert.ToBoolean(ItemXmlSource.GetAttribute("customizeitemvisible"));
		}
        /// 
        /// Indicates whether the item will auto-collapse (fold) when clicked. 
        /// When item is on popup menu and this property is set to false, menu will not
        /// close when item is clicked.
        /// 
        [Browsable(false), DevCoBrowsable(false), Category("Behavior"), DefaultValue(false), Description("Indicates whether the item will auto-collapse (fold) when clicked.")]
        public override bool AutoCollapseOnClick
        {
            get { return base.AutoCollapseOnClick; }
            set {base.AutoCollapseOnClick = value; }
        }
        /// 
        /// Gets or sets whether item can be customized by end user.
        /// 
        [Browsable(false), DevCoBrowsable(false), DefaultValue(false), Category("Behavior"), Description("Indicates whether item can be customized by user.")]
        public override bool CanCustomize
        {
            get { return base.CanCustomize; }
            set { base.CanCustomize = value; }
        }
        /// 
        /// Gets or sets whether item is global or not.
        /// This flag is used to propagate property changes to all items with the same name.
        /// Setting for example Visible property on the item that has GlobalItem set to true will
        /// set visible property to the same value on all items with the same name.
        /// 
        [Browsable(false), DevCoBrowsable(false), DefaultValue(false), Category("Behavior"), Description("Indicates whether certain global properties are propagated to all items with the same name when changed.")]
        public override bool GlobalItem
        {
            get { return base.GlobalItem; }
            set { base.GlobalItem = value; }
        }
    }
}