// This is a part of the Smart PropertyGrid Library
// Copyright (C) 2001-2006 VisualHint
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions
// of the accompanying license agreement.

#region Using directives

using System.Drawing;

#endregion

namespace VisualHint.SmartPropertyGrid
{
    /// <summary>
    /// Specific class for properties that have no value and act as a first level container for other properties
    /// and subcategories.
    /// </summary>
    public class RootCategory : Property
    {
        private string _valueText = "";

        public string ValueText
        {
            set { _valueText = value; }
            get { return _valueText; }
        }

        public RootCategory(int propertyId, string name) : base(propertyId, name)
        {
        }

        public override void Draw(Graphics graphics, Rectangle itemRect, PropertyVisibleDeepEnumerator enumSelf)
        {
	        // Choose the colors of the text regarding the item selection
	        Color textColor;
	        if (Selected)
	        {
                if (ParentGrid.GridContainsFocus)
                    textColor = ParentGrid.HighlightedTextColor;
		        else
			        textColor = ForeColor;
	        }
	        else	// if not selected
		        textColor = ForeColor;

	        // Draw the +/- sign if needed
	        //----------------------------

	        if (ParentGrid.HasOneVisibleChild(enumSelf))
                ParentGrid.DrawManager.DrawPlusMinusSign(graphics, itemRect, enumSelf);

            // Draw a background for the label
	        //--------------------------------

            Rectangle rect = itemRect;
	        rect.X += ParentGrid.LeftColumnWidth + 1;
            rect.Width -= ParentGrid.LeftColumnWidth + 1;
	        ParentGrid.DrawManager.DrawCategoryLabelBackground(graphics, rect, enumSelf);

	        // Draw the vertical separation between the columns and the bottom horizontal line
	        //--------------------------------------------------------------------------------

	        // Calculate the rectangle of the label. It can overlap one or both columns
	        Rectangle labelRect = GetLabelRect(itemRect, enumSelf);

	        ParentGrid.DrawManager.DrawSeparationLines(graphics, itemRect, labelRect, enumSelf);

	        // Draw checkbox used to be able to manually disable/enable this category
	        //-----------------------------------------------------------------------

	        if (CanBeDisabledManually)
	        {
                Rectangle frameRect = GetManuallyDisableRect(itemRect, enumSelf);
                ParentGrid.DrawManager.DrawCheckBox(graphics, frameRect, GetManuallyDisabledVariable(), true /* checkbox is enabled */);
            }

	        // Draw the category label
	        //------------------------

	        // Set some margins horizontally
	        rect = labelRect;
	        rect.Inflate(-PropertyGrid.GlobalTextMargin, 0);

            if (ImageIndex != -1)
	        {
		        // Draw the icon
                graphics.SetClip(labelRect);
                if (ParentGrid.DrawImage(ImageIndex, graphics, rect.Left, rect.Top + rect.Height / 2 - 8))
                {
                    rect.X += ParentGrid.BasicPropertyHeight + 3;
                    rect.Width -= ParentGrid.BasicPropertyHeight + 3;
                }
                graphics.ResetClip();
            }

	        ParentGrid.DrawManager.DrawCategoryLabelText(graphics, rect, textColor, enumSelf);

	        // Draw value text if any
	        //-----------------------

	        if (_valueText.Length > 0)
	        {
		        Rectangle valueRect = itemRect;
                int delta = ParentGrid.LeftColumnWidth + 1 + ParentGrid.LabelColumnWidth;
                valueRect.X += delta;
                valueRect.Width -= delta;
		        valueRect.Inflate(-PropertyGrid.GlobalTextMargin, 0);

                ParentGrid.DrawManager.DrawCategoryValue(graphics, valueRect, textColor, enumSelf);
            }
        }

		public override Rectangle GetLabelRect(Rectangle itemRect, PropertyVisibleDeepEnumerator enumSelf)
		{
			Rectangle labelRect = itemRect;

			labelRect.X += ParentGrid.LeftColumnWidth + 1;
			labelRect.Width -= ParentGrid.LeftColumnWidth + 1;

            if ((ValueText.Length > 0) || (ParentGrid.DrawManager.ShowCategoryVerticalLine && ParentGrid.DrawManager.ShowCategoryVerticalLine))
                labelRect.Width = ParentGrid.LabelColumnWidth;

			if (CanBeDisabledManually)
			{
				Rectangle frameRect = GetManuallyDisableRect(itemRect, enumSelf);
				labelRect.X += frameRect.Width + PropertyGrid.GlobalTextMargin;
				labelRect.Width -= frameRect.Width + PropertyGrid.GlobalTextMargin;
			}

			return labelRect;
		}

        public override Rectangle GetSignRect(Rectangle itemRect)
        {
            Rectangle signRect = itemRect;

            // Default rectangle in the left most column for root categories
            signRect.Y += itemRect.Height / 2 - (TreeGlyphWidth / 2);
            signRect.Height = TreeGlyphWidth;

            int treeLevel = (ParentGrid.DisplayMode == PropertyGrid.DisplayModes.Categorized ? TreeLevel : TreeLevelInNonCatMode);
            signRect.X = ParentGrid.LeftColumnWidth - 3 - TreeGlyphWidth;
            signRect.Width = TreeGlyphWidth;

            return signRect;
        }
    }
}
