// 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;
using System.Windows.Forms;

#endregion

namespace VisualHint.SmartPropertyGrid
{
    /// <summary>
    /// Special property that contains only a text displayed as an hyperlink.
    /// </summary>
    public class PropertyHyperLink : Property
    {
        public PropertyHyperLink(int propertyId) : base(propertyId, "")
        {
            ForeColor = Color.Blue;
        }

        internal override void OnPropertyClicked(PropertyEnumerator enumSelf, Point point, Rectangle itemRect)
        {
            if (ParentGrid.Enabled == false)
                return;

            Rectangle strRect = GetStringRect(itemRect, enumSelf);

            if (strRect.Contains(point))
            {
                PropertyHyperLinkClickedEventArgs ev = new PropertyHyperLinkClickedEventArgs(enumSelf, HyperLink);
                ParentGrid.OnHyperLinkPropertyClicked(ev);
            }
        }

        protected Rectangle GetStringRect(Rectangle itemRect, PropertyEnumerator propEnum)
        {
            Font underlinedFont = new Font(ParentGrid.Font, FontStyle.Underline);

            Graphics graphics = ParentGrid.CreateGraphics();
            Size size = Win32Calls.GetTextExtent(graphics, DisplayName, underlinedFont);
            graphics.Dispose();

            Rectangle labelRect = GetLabelRect(itemRect, propEnum.GetVisibleDeepEnumerator());
            labelRect.Width = size.Width;
            labelRect.X = itemRect.Right - labelRect.Width - 6;

            return labelRect;
        }

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

            labelRect.X = ParentGrid.LeftColumnWidth + 1;
	        labelRect.Width = itemRect.Right - 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 bool OverrideCursor(Point mouseLocation, Rectangle itemRect, PropertyEnumerator propEnum)
        {
	        Rectangle labelRect = GetStringRect(itemRect, propEnum);

            if (labelRect.Contains(mouseLocation) && Enabled)
            {
                ParentGrid.Grid.Cursor = Cursors.Hand;
                return true;
	        }

	        return false;
        }

        public override void Draw(Graphics graphics, Rectangle itemRect, PropertyVisibleDeepEnumerator enumSelf)
        {
            // Get the label bounding box
	        Rectangle labelRect = GetLabelRect(itemRect, enumSelf);
	        labelRect.Width = itemRect.Right - labelRect.Left;

            // Fill the label background
            Rectangle rect = itemRect;
            rect.X += ParentGrid.LeftColumnWidth + 1;
            rect.Width -= ParentGrid.LeftColumnWidth + 1;
            ParentGrid.DrawManager.DrawPropertyLabelBackground(graphics, rect, enumSelf);

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

	        // Draw label text
	        //----------------

            // Set the font to underline
            Font underlinedFont = new Font(ParentGrid.Font, FontStyle.Underline);

            Color textColor;

            // Choose the colors of the background and text regarding the item selection
            if (Selected)
            {
                if (ParentGrid.GridContainsFocus)
                    textColor = SystemColors.HighlightText;
                else
                    textColor = SystemColors.WindowText;
            }
            else										// if not selected
            {
                if ((ParentGrid.Enabled == false) &&
                    ((ParentGrid.DisableMode & PropertyGrid.DisableModes.EverythingGrayedOut) != 0))
                {
                    textColor = ParentGrid.DisabledForeColor;
                }
                else
                {
                    if (Enabled == false)
                        textColor = DisabledForeColor;
                    else
                        textColor = ForeColor;
                }
            }

            labelRect.X += PropertyGrid.GlobalTextMargin;
            labelRect.Width -= 2 * PropertyGrid.GlobalTextMargin;
            labelRect.Height--;

        	// Draw the text, clipped by the given area
            Size textSize = Win32Calls.GetTextExtent(graphics, DisplayName, underlinedFont);

        	if (textSize.Width <= labelRect.Width)
                Win32Calls.DrawText(graphics, DisplayName, ref labelRect, underlinedFont, textColor,
                    Win32Calls.DT_RIGHT | Win32Calls.DT_SINGLELINE | Win32Calls.DT_VCENTER | Win32Calls.DT_NOPREFIX);
            else
                Win32Calls.DrawText(graphics, DisplayName, ref labelRect, underlinedFont, textColor,
                    Win32Calls.DT_LEFT | Win32Calls.DT_SINGLELINE | Win32Calls.DT_VCENTER | Win32Calls.DT_END_ELLIPSIS |
                    Win32Calls.DT_NOPREFIX);
        }
    }
}
