예제 #1
0
 /// <summary>
 /// Assign a new valid shortcut when keys are pressed
 /// </summary>
 private void ListViewKeyDown(Object sender, KeyEventArgs e)
 {
     if (this.listView.SelectedItems.Count > 0)
     {
         if (e.KeyData == Keys.Delete)
         {
             this.RemoveShortcutClick(null, null);
         }
         else
         {
             ListViewItem selected = this.listView.SelectedItems[0];
             ShortcutItem item     = selected.Tag as ShortcutItem;
             if (item.Custom != e.KeyData && ToolStripManager.IsValidShortcut(e.KeyData))
             {
                 selected.SubItems[1].Text = GetKeysAsString(e.KeyData);
                 item.Custom = e.KeyData; selected.Selected = true;
                 this.UpdateItemHighlightFont(selected, item);
                 if (this.CountItemsByKey(e.KeyData) > 1)
                 {
                     String message = TextHelper.GetString("Info.ShortcutIsAlreadyUsed");
                     ErrorManager.ShowWarning(message, null);
                     this.filterTextBox.Focus(); // Set focus to filter...
                     this.filterTextBox.Text = GetKeysAsString(e.KeyData);
                     this.filterTextBox.SelectAll();
                 }
             }
         }
     }
 }
 public void MethodIsShortcutValid()
 {
     Assert.AreEqual(true, ToolStripManager.IsValidShortcut(Keys.F1), "A1");
     Assert.AreEqual(true, ToolStripManager.IsValidShortcut(Keys.F7), "A1");
     Assert.AreEqual(true, ToolStripManager.IsValidShortcut(Keys.Shift | Keys.F1), "A1");
     Assert.AreEqual(true, ToolStripManager.IsValidShortcut(Keys.Control | Keys.F1), "A1");
     Assert.AreEqual(false, ToolStripManager.IsValidShortcut(Keys.Shift), "A1");
     Assert.AreEqual(false, ToolStripManager.IsValidShortcut(Keys.Alt), "A1");
     Assert.AreEqual(false, ToolStripManager.IsValidShortcut(Keys.D6), "A1");
     Assert.AreEqual(true, ToolStripManager.IsValidShortcut(Keys.Control | Keys.S), "A1");
     Assert.AreEqual(false, ToolStripManager.IsValidShortcut(Keys.L), "A1");
 }
예제 #3
0
        public static void UpdateShortcutItem(ToolStripMenuItem item, Control parent, string fieldName)
        {
            if (item.Tag == null)
            {
                item.Tag = new ShortcutInfo()
                {
                    KeyHandler = null, ShortcutKey = fieldName
                };
            }
            else if (((ShortcutInfo)item.Tag).KeyHandler != null)
            {
                ClearProcessCmdKeyHandler(item, parent);
            }

            Keys keys = (XmlKeys)typeof(DebuggerShortcutsConfig).GetField(fieldName).GetValue(ConfigManager.Config.DebugInfo.Shortcuts);

            if ((keys != Keys.None && !ToolStripManager.IsValidShortcut(keys)) || Program.IsMono)
            {
                //Support normally invalid shortcut keys as a shortcut
                item.ShortcutKeys             = Keys.None;
                item.ShortcutKeyDisplayString = GetShortcutDisplay(keys);

                Form parentForm = parent.FindForm();
                if (parentForm is BaseForm)
                {
                    ProcessCmdKeyHandler onProcessCmdKeyHandler = (Keys keyData) => {
                        if (parent.ContainsFocus && keyData == keys)
                        {
                            item.PerformClick();
                            return(true);
                        }
                        return(false);
                    };

                    ((ShortcutInfo)item.Tag).KeyHandler       = onProcessCmdKeyHandler;
                    (parentForm as BaseForm).OnProcessCmdKey += onProcessCmdKeyHandler;
                }
            }
            else
            {
                item.ShortcutKeys             = keys;
                item.ShortcutKeyDisplayString = GetShortcutDisplay(keys);
            }
        }
예제 #4
0
        /// <summary>
        /// Assign the new shortcut.
        /// </summary>
        void AssignNewShortcut(ShortcutListItem item, Keys shortcut)
        {
            if (shortcut == 0 || shortcut == Keys.Delete)
            {
                shortcut = 0;
            }
            else if (!ToolStripManager.IsValidShortcut(shortcut))
            {
                return;
            }

            if (item.Custom == shortcut)
            {
                return;
            }
            this.listView.BeginUpdate();
            var oldShortcut = item.Custom;

            item.Custom   = shortcut;
            item.Selected = true;
            this.GetConflictItems(item);
            this.listView.EndUpdate();
            if (item.HasConflicts)
            {
                string text    = TextHelper.GetString("Info.ShortcutIsAlreadyUsed");
                string caption = TextHelper.GetString("Title.WarningDialog");
                switch (MessageBox.Show(Globals.MainForm, text, " " + caption, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning))
                {
                case DialogResult.Abort:
                    this.listView.BeginUpdate();
                    item.Custom = oldShortcut;
                    this.GetConflictItems(item);
                    this.listView.EndUpdate();
                    break;

                case DialogResult.Retry:
                    this.filterTextBox.Text = ViewConflictsKey + item.KeysString;
                    this.filterTextBox.SelectAll();
                    this.filterTextBox.Focus();     // Set focus to filter...
                    break;
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Filters out a message before it is dispatched.
        /// </summary>
        /// <param name="m">The message to be dispatched. You cannot modify this message.</param>
        /// <returns>
        /// <code>true</code> to filter the message and stop it from being dispatched;
        /// <code>false</code> to allow the message to continue to the next filter or control.
        /// </returns>
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            if (PluginBase.MainForm.CurrentDocument != currentDoc)
            {
                OnCancel();
                return(false);
            }

            switch (m.Msg)
            {
            case 0x0100:     //WM_KEYDOWN
            case 0x0104:     //WM_SYSKEYDOWN
                var key      = (Keys)(int)m.WParam;
                var modifier = Control.ModifierKeys;
                switch (key)
                {
                case Keys.Escape:
                    OnCancel();
                    return(true);

                case Keys.Enter:
                    OnApply();
                    return(true);

                case Keys.Back:
                    if (CanBackspace)
                    {
                        break;
                    }
                    return(true);

                case Keys.Left:
                    if (!AtLeftmost)
                    {
                        break;
                    }
                    return(true);

                case Keys.Delete:
                    if (CanDelete)
                    {
                        break;
                    }
                    return(true);

                case Keys.Right:
                    if (!AtRightmost)
                    {
                        break;
                    }
                    if (sci.SelTextSize != 0)
                    {
                        sci.SetSel(end, end);
                    }
                    return(true);

                case Keys.PageUp:
                case Keys.PageDown:
                case Keys.Up:
                case Keys.Down:
                    if (!CanWrite)
                    {
                        break;
                    }
                    return(true);

                case Keys.End:
                    if (!CanWrite)
                    {
                        break;
                    }
                    sci.SetSel((modifier & Keys.Shift) == 0 ? end : sci.SelectionStart, end);
                    return(true);

                case Keys.Home:
                    if (!CanWrite)
                    {
                        break;
                    }
                    sci.SetSel((modifier & Keys.Shift) == 0 ? start : sci.SelectionEnd, start);
                    return(true);

                case Keys.Tab:
                    return(true);

                default:
                    if (ToolStripManager.IsValidShortcut(key | modifier))
                    {
                        return(HandleShortcuts(key | modifier));
                    }
                    break;
                }
                break;

            case 0x0102:     //WM_CHAR
            case 0x0103:     //WM_DEADCHAR
                if (CanWrite && IsValidChar((int)m.WParam))
                {
                    break;
                }
                return(true);

                //case 0x0200: //WM_MOUSEMOVE
                //case 0x0201: //WM_LBUTTONDOWN
                //case 0x0202: //WM_LBUTTONUP
                //case 0x0203: //WM_LBUTTONDBCLICK
                //case 0x0204: //WM_RBUTTONDOWN
                //case 0x0205: //WM_RBUTTONUP
                //case 0x0206: //WM_RBUTTONDBCLICK
                //case 0x0207: //WM_MBUTTONDOWN
                //case 0x0208: //WM_MBUTTONUP
                //case 0x0209: //WM_MBUTTONDBCLICK
                //    if (sci.ClientRectangle.Contains(sci.PointToClient(Control.MousePosition))) break;
                //    return true;

                //case 0x007B: //WM_CONTEXTMENU
                //    return true;
            }

            return(false);
        }
        /// <summary>
        ///  Here is where all the fun stuff starts.  We create the structure and apply the naming here.
        /// </summary>
        private void CreateStandardMenuStrip(IDesignerHost host, MenuStrip tool)
        {
            // build the static menu items structure.
            string[][] menuItemNames = new string[][]
            {
                new string[] { SR.StandardMenuFile, SR.StandardMenuNew, SR.StandardMenuOpen, "-", SR.StandardMenuSave, SR.StandardMenuSaveAs, "-", SR.StandardMenuPrint, SR.StandardMenuPrintPreview, "-", SR.StandardMenuExit },
                new string[] { SR.StandardMenuEdit, SR.StandardMenuUndo, SR.StandardMenuRedo, "-", SR.StandardMenuCut, SR.StandardMenuCopy, SR.StandardMenuPaste, "-", SR.StandardMenuSelectAll },
                new string[] { SR.StandardMenuTools, SR.StandardMenuCustomize, SR.StandardMenuOptions },
                new string[] { SR.StandardMenuHelp, SR.StandardMenuContents, SR.StandardMenuIndex, SR.StandardMenuSearch, "-", SR.StandardMenuAbout }
            };

            // build the static menu items image list that maps one-one with above menuItems structure. this is required so that the in LOCALIZED build we dont use the Localized item string.
            string[][] menuItemImageNames = new string[][]
            {
                new string[] { "", "new", "open", "-", "save", "", "-", "print", "printPreview", "-", "" },
                new string[] { "", "", "", "-", "cut", "copy", "paste", "-", "" },
                new string[] { "", "", "" },
                new string[] { "", "", "", "", "-", "" }
            };

            Keys[][] menuItemShortcuts = new Keys[][]
            {
                new Keys[] { /*File*/ Keys.None, /*New*/ Keys.Control | Keys.N, /*Open*/ Keys.Control | Keys.O, /*Separator*/ Keys.None, /*Save*/ Keys.Control | Keys.S, /*SaveAs*/ Keys.None, Keys.None, /*Print*/ Keys.Control | Keys.P, /*PrintPreview*/ Keys.None, /*Separator*/ Keys.None, /*Exit*/ Keys.None },
                new Keys[] { /*Edit*/ Keys.None, /*Undo*/ Keys.Control | Keys.Z, /*Redo*/ Keys.Control | Keys.Y, /*Separator*/ Keys.None, /*Cut*/ Keys.Control | Keys.X, /*Copy*/ Keys.Control | Keys.C, /*Paste*/ Keys.Control | Keys.V, /*Separator*/ Keys.None, /*SelectAll*/ Keys.None },
                new Keys[] { /*Tools*/ Keys.None, /*Customize*/ Keys.None, /*Options*/ Keys.None },
                new Keys[] { /*Help*/ Keys.None, /*Contents*/ Keys.None, /*Index*/ Keys.None, /*Search*/ Keys.None, /*Separator*/ Keys.None, /*About*/ Keys.None }
            };

            Debug.Assert(host != null, "can't create standard menu without designer _host.");
            if (host is null)
            {
                return;
            }

            tool.SuspendLayout();
            ToolStripDesigner.s_autoAddNewItems = false;
            // create a transaction so this happens as an atomic unit.
            DesignerTransaction createMenu = _host.CreateTransaction(SR.StandardMenuCreateDesc);

            try
            {
                INameCreationService nameCreationService = (INameCreationService)_provider.GetService(typeof(INameCreationService));
                string defaultName = "standardMainMenuStrip";
                string name        = defaultName;
                int    index       = 1;

                if (host != null)
                {
                    while (_host.Container.Components[name] != null)
                    {
                        name = defaultName + (index++).ToString(CultureInfo.InvariantCulture);
                    }
                }

                // now build the menu items themselves.
                for (int j = 0; j < menuItemNames.Length; j++)
                {
                    string[]          menuArray = menuItemNames[j];
                    ToolStripMenuItem rootItem  = null;
                    for (int i = 0; i < menuArray.Length; i++)
                    {
                        name = null;
                        // for separators, just use the default name.  Otherwise, remove any non-characters and  get the name from the text.
                        string itemText = menuArray[i];
                        name = NameFromText(itemText, typeof(ToolStripMenuItem), nameCreationService, true);
                        ToolStripItem item = null;
                        if (name.Contains("Separator"))
                        {
                            // create the componennt.
                            item = (ToolStripSeparator)_host.CreateComponent(typeof(ToolStripSeparator), name);
                            IDesigner designer = _host.GetDesigner(item);
                            if (designer is ComponentDesigner)
                            {
                                ((ComponentDesigner)designer).InitializeNewComponent(null);
                            }

                            item.Text = itemText;
                        }
                        else
                        {
                            // create the componennt.
                            item = (ToolStripMenuItem)_host.CreateComponent(typeof(ToolStripMenuItem), name);
                            IDesigner designer = _host.GetDesigner(item);
                            if (designer is ComponentDesigner)
                            {
                                ((ComponentDesigner)designer).InitializeNewComponent(null);
                            }

                            item.Text = itemText;
                            Keys shortcut = menuItemShortcuts[j][i];
                            if ((item is ToolStripMenuItem) && shortcut != Keys.None)
                            {
                                if (!ToolStripManager.IsShortcutDefined(shortcut) && ToolStripManager.IsValidShortcut(shortcut))
                                {
                                    ((ToolStripMenuItem)item).ShortcutKeys = shortcut;
                                }
                            }

                            Bitmap image = null;
                            try
                            {
                                image = GetImage(menuItemImageNames[j][i]);
                            }
                            catch
                            {
                                // eat the exception.. as you may not find image for all MenuItems.
                            }

                            if (image != null)
                            {
                                PropertyDescriptor imageProperty = TypeDescriptor.GetProperties(item)["Image"];
                                Debug.Assert(imageProperty != null, "Could not find 'Image' property in ToolStripItem.");
                                if (imageProperty != null)
                                {
                                    imageProperty.SetValue(item, image);
                                }

                                item.ImageTransparentColor = Color.Magenta;
                            }
                        }

                        // the first item in each array is the root item.
                        if (i == 0)
                        {
                            rootItem = (ToolStripMenuItem)item;
                            rootItem.DropDown.SuspendLayout();
                        }
                        else
                        {
                            rootItem.DropDownItems.Add(item);
                        }

                        //If Last SubItem Added the Raise the Events
                        if (i == menuArray.Length - 1)
                        {
                            // member is OK to be null...
                            MemberDescriptor member = TypeDescriptor.GetProperties(rootItem)["DropDownItems"];
                            _componentChangeSvc.OnComponentChanging(rootItem, member);
                            _componentChangeSvc.OnComponentChanged(rootItem, member, null, null);
                        }
                    }

                    // finally, add it to the MainMenu.
                    rootItem.DropDown.ResumeLayout(false);
                    tool.Items.Add(rootItem);
                    //If Last SubItem Added the Raise the Events
                    if (j == menuItemNames.Length - 1)
                    {
                        // member is OK to be null...
                        MemberDescriptor topMember = TypeDescriptor.GetProperties(tool)["Items"];
                        _componentChangeSvc.OnComponentChanging(tool, topMember);
                        _componentChangeSvc.OnComponentChanged(tool, topMember, null, null);
                    }
                }
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                {
                    IUIService uiService = (IUIService)_provider.GetService(typeof(IUIService));
                    uiService.ShowError(e.Message);
                }

                if (createMenu != null)
                {
                    createMenu.Cancel();
                    createMenu = null;
                }
            }
            finally
            {
                ToolStripDesigner.s_autoAddNewItems = true;
                if (createMenu != null)
                {
                    createMenu.Commit();
                    createMenu = null;
                }

                tool.ResumeLayout();
                // Select the Main Menu...
                ISelectionService selSvc = (ISelectionService)_provider.GetService(typeof(ISelectionService));
                if (selSvc != null)
                {
                    selSvc.SetSelectedComponents(new object[] { _designer.Component });
                }

                //Refresh the Glyph
                DesignerActionUIService actionUIService = (DesignerActionUIService)_provider.GetService(typeof(DesignerActionUIService));
                if (actionUIService != null)
                {
                    actionUIService.Refresh(_designer.Component);
                }

                // this will invalidate the Selection Glyphs.
                SelectionManager selMgr = (SelectionManager)_provider.GetService(typeof(SelectionManager));
                selMgr.Refresh();
            }
        }
 private void CreateStandardMenuStrip(IDesignerHost host, MenuStrip tool)
 {
     string[][] strArray   = new string[][] { new string[] { System.Design.SR.GetString("StandardMenuFile"), System.Design.SR.GetString("StandardMenuNew"), System.Design.SR.GetString("StandardMenuOpen"), "-", System.Design.SR.GetString("StandardMenuSave"), System.Design.SR.GetString("StandardMenuSaveAs"), "-", System.Design.SR.GetString("StandardMenuPrint"), System.Design.SR.GetString("StandardMenuPrintPreview"), "-", System.Design.SR.GetString("StandardMenuExit") }, new string[] { System.Design.SR.GetString("StandardMenuEdit"), System.Design.SR.GetString("StandardMenuUndo"), System.Design.SR.GetString("StandardMenuRedo"), "-", System.Design.SR.GetString("StandardMenuCut"), System.Design.SR.GetString("StandardMenuCopy"), System.Design.SR.GetString("StandardMenuPaste"), "-", System.Design.SR.GetString("StandardMenuSelectAll") }, new string[] { System.Design.SR.GetString("StandardMenuTools"), System.Design.SR.GetString("StandardMenuCustomize"), System.Design.SR.GetString("StandardMenuOptions") }, new string[] { System.Design.SR.GetString("StandardMenuHelp"), System.Design.SR.GetString("StandardMenuContents"), System.Design.SR.GetString("StandardMenuIndex"), System.Design.SR.GetString("StandardMenuSearch"), "-", System.Design.SR.GetString("StandardMenuAbout") } };
     string[][] strArray2  = new string[][] { new string[] { "", "new", "open", "-", "save", "", "-", "print", "printPreview", "-", "" }, new string[] { "", "", "", "-", "cut", "copy", "paste", "-", "" }, new string[] { "", "", "" }, new string[] { "", "", "", "", "-", "" } };
     Keys[][]   keysArray2 = new Keys[4][];
     Keys[]     keysArray3 = new Keys[11];
     keysArray3[1] = Keys.Control | Keys.N;
     keysArray3[2] = Keys.Control | Keys.O;
     keysArray3[4] = Keys.Control | Keys.S;
     keysArray3[7] = Keys.Control | Keys.P;
     keysArray2[0] = keysArray3;
     Keys[] keysArray4 = new Keys[9];
     keysArray4[1] = Keys.Control | Keys.Z;
     keysArray4[2] = Keys.Control | Keys.Y;
     keysArray4[4] = Keys.Control | Keys.X;
     keysArray4[5] = Keys.Control | Keys.C;
     keysArray4[6] = Keys.Control | Keys.V;
     keysArray2[1] = keysArray4;
     keysArray2[2] = new Keys[3];
     keysArray2[3] = new Keys[6];
     Keys[][] keysArray = keysArray2;
     if (host != null)
     {
         tool.SuspendLayout();
         ToolStripDesigner._autoAddNewItems = false;
         DesignerTransaction transaction = this._host.CreateTransaction(System.Design.SR.GetString("StandardMenuCreateDesc"));
         try
         {
             INameCreationService nameCreationService = (INameCreationService)this._provider.GetService(typeof(INameCreationService));
             string str  = "standardMainMenuStrip";
             string name = str;
             int    num  = 1;
             if (host != null)
             {
                 while (this._host.Container.Components[name] != null)
                 {
                     name = str + num++.ToString(CultureInfo.InvariantCulture);
                 }
             }
             for (int i = 0; i < strArray.Length; i++)
             {
                 string[]          strArray3 = strArray[i];
                 ToolStripMenuItem component = null;
                 for (int j = 0; j < strArray3.Length; j++)
                 {
                     name = null;
                     string text = strArray3[j];
                     name = this.NameFromText(text, typeof(ToolStripMenuItem), nameCreationService, true);
                     ToolStripItem item2 = null;
                     if (name.Contains("Separator"))
                     {
                         item2 = (ToolStripSeparator)this._host.CreateComponent(typeof(ToolStripSeparator), name);
                         IDesigner designer = this._host.GetDesigner(item2);
                         if (designer is ComponentDesigner)
                         {
                             ((ComponentDesigner)designer).InitializeNewComponent(null);
                         }
                         item2.Text = text;
                     }
                     else
                     {
                         item2 = (ToolStripMenuItem)this._host.CreateComponent(typeof(ToolStripMenuItem), name);
                         IDesigner designer2 = this._host.GetDesigner(item2);
                         if (designer2 is ComponentDesigner)
                         {
                             ((ComponentDesigner)designer2).InitializeNewComponent(null);
                         }
                         item2.Text = text;
                         Keys shortcut = keysArray[i][j];
                         if (((item2 is ToolStripMenuItem) && (shortcut != Keys.None)) && (!ToolStripManager.IsShortcutDefined(shortcut) && ToolStripManager.IsValidShortcut(shortcut)))
                         {
                             ((ToolStripMenuItem)item2).ShortcutKeys = shortcut;
                         }
                         Bitmap image = null;
                         try
                         {
                             image = this.GetImage(strArray2[i][j]);
                         }
                         catch
                         {
                         }
                         if (image != null)
                         {
                             PropertyDescriptor descriptor = TypeDescriptor.GetProperties(item2)["Image"];
                             if (descriptor != null)
                             {
                                 descriptor.SetValue(item2, image);
                             }
                             item2.ImageTransparentColor = Color.Magenta;
                         }
                     }
                     if (j == 0)
                     {
                         component = (ToolStripMenuItem)item2;
                         component.DropDown.SuspendLayout();
                     }
                     else
                     {
                         component.DropDownItems.Add(item2);
                     }
                     if (j == (strArray3.Length - 1))
                     {
                         MemberDescriptor member = TypeDescriptor.GetProperties(component)["DropDownItems"];
                         this.componentChangeSvc.OnComponentChanging(component, member);
                         this.componentChangeSvc.OnComponentChanged(component, member, null, null);
                     }
                 }
                 component.DropDown.ResumeLayout(false);
                 tool.Items.Add(component);
                 if (i == (strArray.Length - 1))
                 {
                     MemberDescriptor descriptor3 = TypeDescriptor.GetProperties(tool)["Items"];
                     this.componentChangeSvc.OnComponentChanging(tool, descriptor3);
                     this.componentChangeSvc.OnComponentChanged(tool, descriptor3, null, null);
                 }
             }
         }
         catch (Exception exception)
         {
             if (exception is InvalidOperationException)
             {
                 ((IUIService)this._provider.GetService(typeof(IUIService))).ShowError(exception.Message);
             }
             if (transaction != null)
             {
                 transaction.Cancel();
                 transaction = null;
             }
         }
         finally
         {
             ToolStripDesigner._autoAddNewItems = true;
             if (transaction != null)
             {
                 transaction.Commit();
                 transaction = null;
             }
             tool.ResumeLayout();
             ISelectionService service = (ISelectionService)this._provider.GetService(typeof(ISelectionService));
             if (service != null)
             {
                 service.SetSelectedComponents(new object[] { this._designer.Component });
             }
             DesignerActionUIService service4 = (DesignerActionUIService)this._provider.GetService(typeof(DesignerActionUIService));
             if (service4 != null)
             {
                 service4.Refresh(this._designer.Component);
             }
             ((SelectionManager)this._provider.GetService(typeof(SelectionManager))).Refresh();
         }
     }
 }