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

namespace ryControls.Controls
{
    public partial class ContextMenuStripRichText : ContextMenuStrip
    {
        /// <summary>
        /// 
        /// </summary>
        public ContextMenuStripRichText()
        {
            InitializeComponent();
            AddMenu("撤销", "undo").Click += Undo_Click;
            AddMenu("重做", "redo").Click += Redo_Click;
            AddSeparatorMenu();
            AddMenu("剪切", "cut").Click += Cut_Click;
            AddMenu("复制", "copy").Click += Copy_Click;
            AddMenu("粘贴", "paste").Click += Paste_Click;
            AddMenu("删除", "del").Click += Del_Click;
            AddMenu("全选", "selectall").Click += SelectAll_Click;
        }
        private void SelectAll_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is RichTextBox)) { return; }
            var rich_txt = (RichTextBox)base.SourceControl;
            rich_txt.SelectAll();
        }
        private void Undo_Click(object sender, EventArgs e)
        {
           if(!(base.SourceControl is RichTextBox)) { return; }
            var rich_txt = (RichTextBox)base.SourceControl;
            rich_txt.Undo();
        }
        private void Redo_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is RichTextBox)) { return; }
            var rich_txt = (RichTextBox)base.SourceControl;
            rich_txt.Redo();
        }
        private void Cut_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is RichTextBox)) { return; }
            var rich_txt = (RichTextBox)base.SourceControl;
            rich_txt.Cut();
        }
        private void Copy_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is RichTextBox)) { return; }
            var rich_txt = (RichTextBox)base.SourceControl;
            rich_txt.Copy();
        }
        private void Paste_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is RichTextBox)) { return; }
            if (base.SourceControl is RichTextBox2)
            {
                var rich_txt = (RichTextBox2)base.SourceControl;
                if (rich_txt.OnlyInputText)
                {
                    var text = Clipboard.GetText();
                    rich_txt.SelectedText = text;
                }
                else { rich_txt.Paste(); }
            }
            else
            {
                var rich_txt = (RichTextBox)base.SourceControl;
                rich_txt.Paste();
            }
        }
        private void Del_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is RichTextBox)) { return; }
            var rich_txt = (RichTextBox)base.SourceControl;
            rich_txt.SelectedText="";
        }
        /// <summary>
        /// 添加菜单分隔线
        /// </summary>
        /// <returns></returns>
        public ToolStripSeparator AddSeparatorMenu()
        {
            ToolStripSeparator item = new ToolStripSeparator();
            base.Items.Add(item);
            return item;
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="name"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public ToolStripMenuItem AddMenu(string name, string tag)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(name)
            {
                Tag = tag
            };
            base.Items.Add(item);
            return item;
        }

        private void ContextMenuStripRichText_Opening(object sender, CancelEventArgs e)
        {
            if (!(base.SourceControl is RichTextBox)) { return; }
            var rich_txt = (RichTextBox)base.SourceControl;
            for (int i = 0; i < base.Items.Count; i++)
            {
                var item = base.Items[i];
                if (item.Tag == null) { continue; }
                switch(item.Tag.ToString())
                {
                    case "undo":
                        item.Enabled = rich_txt.CanUndo;
                        break;
                    case "redo":
                        item.Enabled = rich_txt.CanRedo;
                        break;
                    case "cut":
                        item.Enabled = (rich_txt.SelectedText != "" && !rich_txt.ReadOnly) ? true : false;
                        break;
                    case "copy":
                        item.Enabled = rich_txt.SelectedText != "" ? true : false;
                        break;
                    case "paste":
                        item.Enabled = rich_txt.CanPaste(DataFormats.GetFormat(DataFormats.Text));
                        break;
                    case "del":
                        item.Enabled =(rich_txt.SelectedText != "" && !rich_txt.ReadOnly) ? true : false;
                        break;
                    case "selectall":
                        item.Enabled = rich_txt.SelectedText != rich_txt.Text ? true : false;
                        break;
                }
            }
        }
    }
}
