private void OnControlAdded(object sender, ControlEventArgs e) { if (e.Control is ToolStrip) { this.panel.Padding = new System.Windows.Forms.Padding(0); if (this.containerSelectorGlyph != null) { this.containerSelectorGlyph.IsExpanded = false; } PropertyDescriptor descriptor = TypeDescriptor.GetProperties(e.Control)["Dock"]; if (descriptor != null) { descriptor.SetValue(e.Control, DockStyle.None); } if ((this.designerHost != null) && !this.designerHost.Loading) { SelectionManager service = (SelectionManager)this.GetService(typeof(SelectionManager)); if (service != null) { service.Refresh(); } } } }
/// <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(); } }
/// <summary> /// Here is where all the fun stuff starts. We create the structure and apply the naming here. /// </summary> private void CreateStandardToolStrip(IDesignerHost host, ToolStrip tool) { // build the static menu items structure. // string[] menuItemNames = new string[] { SR.StandardMenuNew, SR.StandardMenuOpen, SR.StandardMenuSave, SR.StandardMenuPrint, "-", SR.StandardToolCut, SR.StandardMenuCopy, SR.StandardMenuPaste, "-", SR.StandardToolHelp }; // build a image list mapping one-one the above menuItems list... this is required so that the in LOCALIZED build we dont use the Localized item string. string[] menuItemImageNames = new string[] { "new", "open", "save", "print", "-", "cut", "copy", "paste", "-", "help" }; 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 = "standardMainToolStrip"; string name = defaultName; int index = 1; if (host != null) { while (_host.Container.Components[name] != null) { name = defaultName + (index++).ToString(CultureInfo.InvariantCulture); } } //keep an index in the MenuItemImageNames .. so that mapping is maintained. int menuItemImageNamesCount = 0; // now build the menu items themselves. foreach (string itemText in menuItemNames) { name = null; // for separators, just use the default name. Otherwise, remove any non-characters and get the name from the text. defaultName = "ToolStripButton"; name = NameFromText(itemText, typeof(ToolStripButton), 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); } } else { // create the component. item = (ToolStripButton)_host.CreateComponent(typeof(ToolStripButton), name); IDesigner designer = _host.GetDesigner(item); if (designer is ComponentDesigner) { ((ComponentDesigner)designer).InitializeNewComponent(null); } PropertyDescriptor displayStyleProperty = TypeDescriptor.GetProperties(item)["DisplayStyle"]; Debug.Assert(displayStyleProperty != null, "Could not find 'Text' property in ToolStripItem."); if (displayStyleProperty != null) { displayStyleProperty.SetValue(item, ToolStripItemDisplayStyle.Image); } PropertyDescriptor textProperty = TypeDescriptor.GetProperties(item)["Text"]; Debug.Assert(textProperty != null, "Could not find 'Text' property in ToolStripItem."); if (textProperty != null) { textProperty.SetValue(item, itemText); } Bitmap image = null; try { image = GetImage(menuItemImageNames[menuItemImageNamesCount]); } 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; } } tool.Items.Add(item); //increment the counter... menuItemImageNamesCount++; } // finally, add it to the Main ToolStrip. 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 { //Reset the AutoAdd state 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(); } }
public void ShowMenu() { if (this.menuItem != null) { Control parent = this.designMenu.Parent; Form component = parent as Form; if (component != null) { this.parentFormDesigner = this.host.GetDesigner(component) as FormDocumentDesigner; if ((this.parentFormDesigner != null) && (this.parentFormDesigner.Menu != null)) { this.parentMenu = this.parentFormDesigner.Menu; this.parentFormDesigner.Menu = null; } } this.selected = true; this.designMenu.Visible = true; this.designMenu.BringToFront(); this.menuItem.Visible = true; if ((this.currentParent != null) && (this.currentParent != this.menuItem)) { ToolStripMenuItemDesigner designer = this.host.GetDesigner(this.currentParent) as ToolStripMenuItemDesigner; if (designer != null) { designer.RemoveTypeHereNode(this.currentParent); } } this.menuItem.DropDown = this.dropDown; this.menuItem.DropDown.OwnerItem = this.menuItem; if (this.dropDown.Items.Count > 0) { ToolStripItem[] array = new ToolStripItem[this.dropDown.Items.Count]; this.dropDown.Items.CopyTo(array, 0); foreach (ToolStripItem item in array) { if (item is DesignerToolStripControlHost) { this.dropDown.Items.Remove(item); } } } ToolStripMenuItemDesigner designer2 = (ToolStripMenuItemDesigner)this.host.GetDesigner(this.menuItem); BehaviorService service = (BehaviorService)this.GetService(typeof(BehaviorService)); if (service != null) { if ((designer2 != null) && (parent != null)) { Rectangle parentBounds = service.ControlRectInAdornerWindow(parent); if (ToolStripDesigner.IsGlyphTotallyVisible(service.ControlRectInAdornerWindow(this.designMenu), parentBounds)) { designer2.InitializeDropDown(); } } if (this.dummyToolStripGlyph == null) { Point pos = service.ControlToAdornerWindow(this.designMenu); Rectangle bounds = this.designMenu.Bounds; bounds.Offset(pos); this.dummyToolStripGlyph = new ControlBodyGlyph(bounds, Cursor.Current, this.menuItem, new ContextMenuStripBehavior(this.menuItem)); SelectionManager manager = (SelectionManager)this.GetService(typeof(SelectionManager)); if (manager != null) { manager.BodyGlyphAdorner.Glyphs.Insert(0, this.dummyToolStripGlyph); } } ToolStripKeyboardHandlingService service2 = (ToolStripKeyboardHandlingService)this.GetService(typeof(ToolStripKeyboardHandlingService)); if (service2 != null) { int num = this.dropDown.Items.Count - 1; if (num >= 0) { service2.SelectedDesignerControl = this.dropDown.Items[num]; } } } } }
internal virtual void CommitEdit(System.Type type, string text, bool commit, bool enterKeyPressed, bool tabKeyPressed) { System.Windows.Forms.ToolStripItem component = null; SelectionManager manager = (SelectionManager)this.GetService(typeof(SelectionManager)); BehaviorService service = (BehaviorService)this.GetService(typeof(BehaviorService)); ToolStrip immediateParent = this.ImmediateParent as ToolStrip; immediateParent.SuspendLayout(); this.HideDummyNode(); IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost)); ToolStripDesigner designer = (ToolStripDesigner)host.GetDesigner(this.ToolStripItem.Owner); if ((designer != null) && (designer.EditManager != null)) { designer.EditManager.ActivateEditor(null, false); } if ((immediateParent is MenuStrip) && (type == typeof(ToolStripSeparator))) { IDesignerHost host2 = (IDesignerHost)this.GetService(typeof(IDesignerHost)); if (host2 != null) { IUIService service2 = (IUIService)host2.GetService(typeof(IUIService)); if (service2 != null) { service2.ShowError(System.Design.SR.GetString("ToolStripSeparatorError")); commit = false; if (this.selSvc != null) { this.selSvc.SetSelectedComponents(new object[] { immediateParent }); } } } } if (!commit) { if (this.dummyItemAdded) { this.dummyItemAdded = false; this.RemoveItem(); if (designer.NewItemTransaction != null) { designer.NewItemTransaction.Cancel(); designer.NewItemTransaction = null; } } goto Label_0246; } if (this.dummyItemAdded) { try { this.RemoveItem(); component = designer.AddNewItem(type, text, enterKeyPressed, false); goto Label_020B; } finally { if (designer.NewItemTransaction != null) { designer.NewItemTransaction.Commit(); designer.NewItemTransaction = null; } } } DesignerTransaction transaction = host.CreateTransaction(System.Design.SR.GetString("ToolStripItemPropertyChangeTransaction")); try { PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.ToolStripItem)["Text"]; string str = (string)descriptor.GetValue(this.ToolStripItem); if ((descriptor != null) && (text != str)) { descriptor.SetValue(this.ToolStripItem, text); } if (enterKeyPressed && (this.selSvc != null)) { this.SelectNextItem(this.selSvc, enterKeyPressed, designer); } } catch (Exception exception) { if (transaction != null) { transaction.Cancel(); transaction = null; } if (manager != null) { manager.Refresh(); } if (System.Windows.Forms.ClientUtils.IsCriticalException(exception)) { throw; } } finally { if (transaction != null) { transaction.Commit(); transaction = null; } } Label_020B: this.dummyItemAdded = false; Label_0246: immediateParent.ResumeLayout(); if ((component != null) && !component.IsOnDropDown) { ToolStripDropDownItem item2 = component as ToolStripDropDownItem; if (item2 != null) { Rectangle glyphBounds = ((ToolStripItemDesigner)host.GetDesigner(component)).GetGlyphBounds(); Control rootComponent = host.RootComponent as Control; if ((rootComponent != null) && (service != null)) { Rectangle parentBounds = service.ControlRectInAdornerWindow(rootComponent); if (!ToolStripDesigner.IsGlyphTotallyVisible(glyphBounds, parentBounds)) { item2.HideDropDown(); } } } } if (manager != null) { manager.Refresh(); } }
internal virtual void ShowEditNode(bool clicked) { if (this.ToolStripItem is ToolStripMenuItem) { if (this._editorNode == null) { this.CreateDummyNode(); } IDesignerHost host = (IDesignerHost)base.Component.Site.GetService(typeof(IDesignerHost)); ToolStrip immediateParent = this.ImmediateParent as ToolStrip; if (immediateParent != null) { ToolStripDesigner designer = (ToolStripDesigner)host.GetDesigner(immediateParent); BehaviorService service = (BehaviorService)this.GetService(typeof(BehaviorService)); Point pos = service.ControlToAdornerWindow(immediateParent); Rectangle bounds = this.ToolStripItem.Bounds; bounds.Offset(pos); this.ToolStripItem.AutoSize = false; this._editorNode.SetWidth(this.ToolStripItem.Text); if (immediateParent.Orientation == Orientation.Horizontal) { this.ToolStripItem.Width = this._editorNode.EditorToolStrip.Width + 2; } else { this.ToolStripItem.Height = this._editorNode.EditorToolStrip.Height; } if (!this.dummyItemAdded) { service.SyncSelection(); } if (this.ToolStripItem.Placement != ToolStripItemPlacement.None) { Rectangle b = this.ToolStripItem.Bounds; b.Offset(pos); if (immediateParent.Orientation == Orientation.Horizontal) { b.X++; b.Y += (this.ToolStripItem.Height - this._editorNode.EditorToolStrip.Height) / 2; b.Y++; } else { b.X += (this.ToolStripItem.Width - this._editorNode.EditorToolStrip.Width) / 2; b.X++; } this._editorNode.Bounds = b; b = Rectangle.Union(bounds, b); service.Invalidate(b); if ((designer != null) && (designer.EditManager != null)) { designer.EditManager.ActivateEditor(this.ToolStripItem, clicked); } SelectionManager manager = (SelectionManager)this.GetService(typeof(SelectionManager)); if (this.bodyGlyph != null) { manager.BodyGlyphAdorner.Glyphs.Remove(this.bodyGlyph); } } else { this.ToolStripItem.AutoSize = this.AutoSize; if (this.ToolStripItem is ToolStripDropDownItem) { ToolStripDropDownItem toolStripItem = this.ToolStripItem as ToolStripDropDownItem; if (toolStripItem != null) { toolStripItem.HideDropDown(); } this.selSvc.SetSelectedComponents(new object[] { this.ImmediateParent }); } } } } }
private void OnSelectionChanged(object sender, EventArgs e) { ISelectionService service = sender as ISelectionService; if (service != null) { System.Windows.Forms.ToolStripItem primarySelection = service.PrimarySelection as System.Windows.Forms.ToolStripItem; System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject accessibilityObject = this.ToolStripItem.AccessibilityObject as System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject; if (accessibilityObject != null) { accessibilityObject.AddState(AccessibleStates.None); ToolStrip mainToolStrip = this.GetMainToolStrip(); if (service.GetComponentSelected(this.ToolStripItem)) { ToolStrip immediateParent = this.ImmediateParent as ToolStrip; int index = 0; if (immediateParent != null) { index = immediateParent.Items.IndexOf(primarySelection); } accessibilityObject.AddState(AccessibleStates.Selected); if (mainToolStrip != null) { System.Design.UnsafeNativeMethods.NotifyWinEvent(0x8007, new HandleRef(immediateParent, immediateParent.Handle), -4, index + 1); } if (primarySelection == this.ToolStripItem) { accessibilityObject.AddState(AccessibleStates.Focused); if (mainToolStrip != null) { System.Design.UnsafeNativeMethods.NotifyWinEvent(0x8005, new HandleRef(immediateParent, immediateParent.Handle), -4, index + 1); } } } } if ((((primarySelection != null) && (this.ToolStripItem != null)) && (primarySelection.IsOnDropDown && this.ToolStripItem.Equals(primarySelection))) && !(this.ToolStripItem is ToolStripMenuItem)) { IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost)); if (host != null) { ToolStripDropDown owner = primarySelection.Owner as ToolStripDropDown; if ((owner != null) && !owner.Visible) { ToolStripDropDownItem ownerItem = owner.OwnerItem as ToolStripDropDownItem; if (ownerItem != null) { ToolStripMenuItemDesigner designer = (ToolStripMenuItemDesigner)host.GetDesigner(ownerItem); if (designer != null) { designer.InitializeDropDown(); } SelectionManager manager = (SelectionManager)this.GetService(typeof(SelectionManager)); if (manager != null) { manager.Refresh(); } } } } } } }