/// <summary> /// Tells the window to the add the given menu. /// </summary> /// <param name="menu">Menu and its item to add</param> public void AddMenu(MenuContribution menu) { if (iMenuBar == null) { iMenuBar = new MenuBar(); iVBox = new VBox(false, 2); Add(iVBox); } Menu fileMenu = new Menu(); MenuItem fileMenuItem = new MenuItem(menu.Label.Replace('&', '_')); fileMenuItem.Submenu = fileMenu; foreach (MenuItemContribution mi in menu.MenuItems) { MenuItem mnuItem = new MenuItem(mi.Label.Replace('&', '_')); mnuItem.Activated += (sender, args) => mi.ActionHandler.Run(); mnuItem.SetStateFlags(mi.ActionHandler.IsEnabled ? StateFlags.Normal : StateFlags.Insensitive, false); fileMenu.Append(mnuItem); } iMenuBar.Append(fileMenuItem); iVBox.PackStart(iMenuBar, false, false, 0); }
/// <inheritdoc /> public void AddMenu(MenuContribution menu) { if (MainMenuStrip == null) { MainMenuStrip = new MenuStrip(); iTopControlAddQueue.Add(MainMenuStrip); // Add it to the queue to handle it later (cf. topmost control problem) } ToolStripMenuItem subMenu = new ToolStripMenuItem(menu.Label); MainMenuStrip.Items.Add(subMenu); using (IEnumerator <MenuItemContribution> enmtor = menu.MenuItems.GetEnumerator()) { while (enmtor.MoveNext()) { MenuItemContribution menuItemContribution = enmtor.Current; // Support separator if (menuItemContribution.IsSeparator) { ToolStripSeparator separator = new ToolStripSeparator(); subMenu.DropDownItems.Add(separator); continue; } // Add normal menu items ToolStripMenuItem menuItem = new ToolStripMenuItem(menuItemContribution.Label); subMenu.DropDownItems.Add(menuItem); menuItem.Enabled = menuItemContribution.ActionHandler.IsEnabled; menuItem.Click += (sender, args) => menuItemContribution.ActionHandler.Run(); if (menuItemContribution.ImageStream != null) { menuItem.Image = new Bitmap(menuItemContribution.ImageStream); } if (Enum.TryParse(menuItemContribution.Shortcut, true, out Keys shortcutKeys)) { menuItem.ShortcutKeys = shortcutKeys; menuItem.ShowShortcutKeys = true; } menuItemContribution.ActionHandler.PropertyChanged += (sender, args) => { if (args.PropertyName != nameof(IActionHandler.IsEnabled)) { return; } // Get current value bool isEnabled = ((IActionHandler)sender).IsEnabled; // Delegate to UI thread BeginInvoke(new Action(() => menuItem.Enabled = isEnabled)); }; } } }
/// <summary> /// Tells the window to the add the given menu. /// </summary> /// <param name="menu">Menu and its item to add</param> public void AddMenu(MenuContribution menu) { if (iMainMenuItems == null) { iMainMenuItems = new List <MenuContribution>(); } MenuContribution clone = new MenuContribution(menu.Id, menu.Label.Replace('&', '_')); ((IEnumerable <MenuItemContribution>)menu.MenuItems).ForEach(x => { MenuItemContribution itemClone = new MenuItemContribution( x.Id, x.Label.Replace('&', '_'), x.Menu, x.ActionHandler, x.Shortcut, x.ImageStream); clone.MenuItems.Add(itemClone); }); iMainMenuItems.Add(clone); DispatchPropertyChanged(() => MainMenuItems); }