Esempio n. 1
0
        public static void InitTextContextMenuItems(this ContextMenuStrip menu, TextBoxBase textbox, TextContextMenuType cat)
        {
            menu.Tag = textbox;

            menu.Items.AddRange(
                new ToolStripItem[] {
                new ToolStripMenuItem()
                {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_SELECT_ALL),
                    Tag  = FormStringKeys.STR_MENU_ITEM_SELECT_ALL.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_SELECT_ALL.ToString(),
                },
                new ToolStripMenuItem()
                {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_CUT),
                    Tag  = FormStringKeys.STR_MENU_ITEM_CUT.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_CUT.ToString(),
                },
                new ToolStripMenuItem {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_COPY),
                    Tag  = FormStringKeys.STR_MENU_ITEM_COPY.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_COPY.ToString(),
                },
                new ToolStripMenuItem {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_COPY_ALL),
                    Tag  = FormStringKeys.STR_MENU_ITEM_COPY_ALL.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_COPY_ALL.ToString(),
                },
                new ToolStripMenuItem {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_PASTE),
                    Tag  = FormStringKeys.STR_MENU_ITEM_PASTE.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_PASTE.ToString(),
                },
                new ToolStripMenuItem {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_DELETE),
                    Tag  = FormStringKeys.STR_MENU_ITEM_DELETE.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_DELETE.ToString(),
                },
                new ToolStripMenuItem {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_CLEAR),
                    Tag  = FormStringKeys.STR_MENU_ITEM_CLEAR.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_CLEAR.ToString(),
                },
                new ToolStripMenuItem {
                    Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_UNDO),
                    Tag  = FormStringKeys.STR_MENU_ITEM_UNDO.ToString(),
                    Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_UNDO.ToString(),
                },
            });

            if ((int)(cat & TextContextMenuType.SaveContent) > 0)
            {
                menu.Items.AddRange(new ToolStripItem[] {
                    new ToolStripSeparator(),
                    new ToolStripMenuItem {
                        Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_SAVE),
                        Tag  = FormStringKeys.STR_MENU_ITEM_SAVE.ToString(),
                        Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_SAVE.ToString(),
                    }
                });
            }

            if ((int)(cat & TextContextMenuType.WordWrap) > 0)
            {
                menu.Items.AddRange(new ToolStripItem[] {
                    new ToolStripSeparator(),
                    new ToolStripMenuItem {
                        Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_WORDWRAP),
                        Tag  = FormStringKeys.STR_MENU_ITEM_WORDWRAP.ToString(),
                        Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_WORDWRAP.ToString(),
                    }
                });
            }

            if ((int)(cat & TextContextMenuType.RegexSource) > 0)
            {
                menu.Items.AddRange(new ToolStripItem[] {
                    new ToolStripSeparator(),
                    new ToolStripMenuItem {
                        Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_COPY_AS_REGEX),
                        Tag  = FormStringKeys.STR_MENU_ITEM_COPY_AS_REGEX.ToString(),
                        Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_COPY_AS_REGEX.ToString()
                    },
                    new ToolStripMenuItem {
                        Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_PUT_INTO_REGEX_BOX),
                        Tag  = FormStringKeys.STR_MENU_ITEM_PUT_INTO_REGEX_BOX.ToString(),
                        Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_PUT_INTO_REGEX_BOX.ToString()
                    }
                });
            }

            if ((int)(cat & TextContextMenuType.RegexTextBox) > 0)
            {
                menu.Items.AddRange(new ToolStripItem[] {
                    new ToolStripSeparator(),
                    new ToolStripMenuItem {
                        Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_TO_REGEX_FORMAT),
                        Tag  = FormStringKeys.STR_MENU_ITEM_TO_REGEX_FORMAT.ToString(),
                        Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_TO_REGEX_FORMAT.ToString()
                    },
                    //new ToolStripMenuItem { Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_PUT_INTO_REGEX_BOX) ,
                    //    Tag = FormStringKeys.STR_MENU_ITEM_PUT_INTO_REGEX_BOX.ToString(),
                    //    Name = menu.Name + "_" +FormStringKeys.STR_MENU_ITEM_PUT_INTO_REGEX_BOX.ToString() },
                    //new ToolStripSeparator(),
                    //new ToolStripMenuItem { Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_SELECTION_ONLY) ,
                    //    Tag = FormStringKeys.STR_MENU_ITEM_SELECTION_ONLY.ToString(),
                    //    Checked = true,
                    //    Name = menu.Name + "_" +FormStringKeys.STR_MENU_ITEM_SELECTION_ONLY.ToString() }
                });
            }

            if ((int)(cat & TextContextMenuType.RegexTextBox) > 0 ||
                (int)(cat & TextContextMenuType.RegexSource) > 0)
            {
                menu.Items.AddRange(new ToolStripItem[] {
                    new ToolStripSeparator(),
                    new ToolStripMenuItem {
                        Text    = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_SELECTION_ONLY),
                        Tag     = FormStringKeys.STR_MENU_ITEM_SELECTION_ONLY.ToString(),
                        Checked = true,
                        Name    = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_SELECTION_ONLY.ToString()
                    }
                });
            }

            if ((int)(cat & TextContextMenuType.SendToSource) > 0)
            {
                menu.Items.AddRange(new ToolStripItem[] {
                    new ToolStripSeparator(),
                    new ToolStripMenuItem {
                        Text = ResxManager.GetResourceString(FormStringKeys.STR_MENU_ITEM_SEND_TO_SOURCE),
                        Tag  = FormStringKeys.STR_MENU_ITEM_SEND_TO_SOURCE.ToString(),
                        Name = menu.Name + "_" + FormStringKeys.STR_MENU_ITEM_SEND_TO_SOURCE.ToString()
                    }
                });
            }
            foreach (ToolStripItem item in menu.Items)
            {
                if (item is ToolStripSeparator == false)
                {
                    if (item.Name.IndexOf(FormStringKeys.STR_MENU_ITEM_PUT_INTO_REGEX_BOX.ToString()) >= 0 ||
                        item.Name.IndexOf(FormStringKeys.STR_MENU_ITEM_SEND_TO_SOURCE.ToString()) >= 0)
                    {
                        //do nothing, the click event will be attached by a specific method
                    }
                    else
                    {
                        item.Click += (sender, e) => ToolHelper.ProcessTextContextMenu(menu.Tag as TextBoxBase, sender);
                    }
                }
            }

            menu.Opening += TextboxMenu_Opening;
        }
Esempio n. 2
0
        public static void ProcessTextContextMenu(TextBoxBase textbox, object sender)
        {
            var tsmi = sender as ToolStripMenuItem;

            if (tsmi == null)
            {
                return;
            }

            var cmd = tsmi.Tag as string;

            var text = string.Empty;

            try
            {
                if (cmd == FormStringKeys.STR_MENU_ITEM_COPY_ALL.ToString())
                {
                    if (textbox.Text != "")
                    {
                        Clipboard.SetText(textbox.Text);
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_COPY.ToString())
                {
                    if (textbox.SelectedText != "")
                    {
                        Clipboard.SetText(textbox.SelectedText);
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_CUT.ToString())
                {
                    if (textbox.SelectedText != "")
                    {
                        textbox.Cut();
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_PASTE.ToString())
                {
                    if (Clipboard.ContainsText())
                    {
                        //TODO no RTF
                        textbox.Paste();
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_SELECT_ALL.ToString())
                {
                    textbox.Focus();
                    textbox.SelectAll();
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_CLEAR.ToString())
                {
                    textbox.Clear();
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_DELETE.ToString())
                {
                    if (textbox.SelectionLength > 0)
                    {
                        var oldStart = textbox.SelectionStart;
                        textbox.Text           = textbox.Text.Remove(textbox.SelectionStart, textbox.SelectionLength);
                        textbox.SelectionStart = oldStart;
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_SAVE.ToString())
                {
                    var sfd = new SaveFileDialog();
                    sfd.Filter = ResxManager.GetResourceString(FormStringKeys.STR_TEXT_SAVE_FILTER);
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        File.WriteAllText(sfd.FileName, textbox.Text);
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_WORDWRAP.ToString())
                {
                    textbox.WordWrap = !tsmi.Checked;
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_SEND_TO_SOURCE.ToString())
                {
                    //TODO STR_MENUITEM_SEND_TO_SOURCE
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_UNDO.ToString())
                {
                    if (textbox.CanUndo)
                    {
                        textbox.Undo();
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_COPY_AS_REGEX.ToString())
                {
                    text = textbox.SelectedText;
                    if (text != string.Empty)
                    {
                        IRegexAnalyst ra = new RegexAnalyst();
                        text = ra.ToSimpleRegexString(text);
                        Clipboard.SetText(text);
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_TO_REGEX_FORMAT.ToString())
                {
                    text = textbox.SelectedText;

                    if (text == string.Empty)
                    {
                        text = textbox.Text;
                    }

                    if (text != string.Empty)
                    {
                        IRegexAnalyst ra = new RegexAnalyst();
                        text = ra.ToSimpleRegexString(text);

                        if (textbox.SelectedText.Length > 0)
                        {
                            if (text != textbox.SelectedText)
                            {
                                //TODO it can be undo,but affect the clipboard
                                Clipboard.SetText(text);
                                textbox.Paste();
                                Clipboard.Clear();
                            }
                        }
                        else
                        {
                            if (text != textbox.Text)
                            {
                                //TODO it can be undo,but affect the clipboard
                                Clipboard.SetText(text);
                                textbox.SelectAll();
                                textbox.Paste();
                                Clipboard.Clear();
                            }
                        }
                    }
                }
                else if (cmd == FormStringKeys.STR_MENU_ITEM_SELECTION_ONLY.ToString())
                {
                    tsmi.Checked = !tsmi.Checked;
                    //textbox.HideSelection = !tsmi.Checked;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }