// 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 non first level container for other properties
    /// and subcategories.
    /// </summary>
    public class SubCategory : RootCategory
    {
        public SubCategory(int propertyId, string name) : base (propertyId, name)
        {
        }

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

            int delta = ParentGrid.Grid.LeftColumnWidth + 1 + GetLabelShift(enumerator);
            labelRect.X += delta;
            labelRect.Width -= delta;

            if ((ValueText.Length > 0) || ParentGrid.DrawManager.ShowCategoryVerticalLine)
                labelRect.Width = ParentGrid.Grid.LeftColumnWidth + 1 + ParentGrid.LabelColumnWidth - labelRect.Left;

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

	        return labelRect;
        }

        public override int GetLabelShift(PropertyVisibleDeepEnumerator enumerator)
        {
            return TreeLevel * (PropertyGrid.GlobalTextMargin + TreeGlyphWidth);
        }

        public override void Draw(Graphics graphics, Rectangle itemRect, PropertyVisibleDeepEnumerator enumSelf)
        {
	        Rectangle labelRect = GetLabelRect(itemRect, enumSelf);

	        Rectangle rect = itemRect;
            rect.X += ParentGrid.LeftColumnWidth + 1;
            rect.Width -= ParentGrid.LeftColumnWidth + 1;

            // Draw a background
	        //------------------

	        ParentGrid.DrawManager.DrawSubCategoryLabelBackground(graphics, labelRect.Left, rect, enumSelf);

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

	        ParentGrid.DrawManager.DrawSeparationLines(graphics, itemRect, labelRect, 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 category label
	        //------------------------

	        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.DrawSubCategoryLabelText(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);
            }

	        // Draw the +/- sign
	        //------------------

            if (ParentGrid.HasOneVisibleChild(enumSelf))
                ParentGrid.DrawManager.DrawPlusMinusSign(graphics, itemRect, 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(), !ParentGrid.Grid.IsAncestorDisabled(enumSelf));
            }
        }

        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 + 1 + (treeLevel - 1) * (PropertyGrid.GlobalTextMargin + TreeGlyphWidth) + PropertyGrid.GlobalTextMargin;
            signRect.Width = TreeGlyphWidth;

            return signRect;
        }
    }
}
