using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ryControls.Controls
{
    public partial class RichTextBox2 : RichTextBox
    {
        /// <summary>
        /// RichTextBox
        /// </summary>
        public RichTextBox2()
        {
            InitializeComponent();
            this.DetectUrls = false;
            contextMenuStripRichText1.Opening += ContextMenuStripRichText1_Opening;
        }
        /// <summary>
        /// 在自带菜单弹出前激发
        /// </summary>
        [Description("在自带菜单弹出前激发")]
        public event CancelEventHandler OnMenuOpening;
        private void ContextMenuStripRichText1_Opening(object sender, CancelEventArgs e)
        {
            OnMenuOpening?.Invoke(sender,e);
        }
        /// <summary>
        /// 根据标签获得菜单项
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public ToolStripItem GetMenuItem(string tag)
        {
            for (int i = 0; i < contextMenuStripRichText1.Items.Count; i++)
            {
                var item = contextMenuStripRichText1.Items[i];
                if (item.Tag == null) { continue; }
                if(tag== item.Tag.ToString())
                {
                    return item;
                }   
            }
            return null;
        }
        /// <summary>
        /// 只允许输入文本(只对设置之后的人工输入有效)
        /// </summary>
        [Description("只允许输入文本(只对设置之后的人工输入有效)")]
        public bool OnlyInputText { get; set; } = false;
        /// <summary>
        /// 按下按键
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (OnlyInputText && e.Control && e.KeyCode == Keys.V)
            {
                e.SuppressKeyPress = true;
                this.Paste(DataFormats.GetFormat(DataFormats.Text));
                return;
            }
            base.OnKeyDown(e);
        }
       /// <summary>
       /// 
       /// </summary>
       /// <param name="e"></param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.Invalidate();
            base.OnLostFocus(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnEnter(EventArgs e)
        {
            if (base.Text.Length == 0)
            {
                base.Invalidate();
            }
            base.OnEnter(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            if (base.Text.Length == 0)
            {
                base.Invalidate();
            }
            base.OnMouseLeave(e);
        }
        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Right && this.ContextMenuStrip==null)
            {
                contextMenuStripRichText1.Show(this, e.Location);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)
            {
                if (this.BorderStyle == BorderStyle.None)
                {
                    System.Drawing.Pen pen = new Pen(SkinHelp.DefalutBorderColor, 1);
                    Graphics g = Graphics.FromHwnd(m.HWnd);
                    g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
                    pen.Dispose();
                }
                if(!base.Focused && this.Text.Length==0)
                {
                    Graphics g = Graphics.FromHwnd(m.HWnd);
                    Brush brush = new SolidBrush(Color.FromArgb(175, 185, 200)); 
                    g.DrawString(EmptyText,Font, brush, 3, 3);
                    brush.Dispose();
                }
                //返回结果
                m.Result = IntPtr.Zero;
            }
        }
        /// <summary>
        /// 当文本框为空时,显示的内容。
        /// </summary>
        [Description("当文本框为空时,显示的内容。")]
        public string EmptyText
        {
            get { return _EmptyText; }
            set
            {
                _EmptyText = value;
                base.Invalidate();
            }
        }
        private string _EmptyText = "";
        /// <summary>
        /// 添加菜单分隔线
        /// </summary>
        /// <returns></returns>
        public ToolStripSeparator AddSeparatorMenu()
        {
            return contextMenuStripRichText1.AddSeparatorMenu();
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="name"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public ToolStripMenuItem AddMenu(string name, string tag)
        {
            return contextMenuStripRichText1.AddMenu(name, tag);
        }
    }
}
