public void TestAddCommandGroupParent() { CommandGroupSpec res = null; var parentSpec = new CommandGroupSpec(125); var cmdParentMock = new Mock <IXCommandGroup>(); cmdParentMock.Setup(m => m.Spec).Returns(parentSpec); var mock = new Mock <IXCommandManager>(); mock.Setup(m => m.AddCommandGroup(It.IsAny <CommandGroupSpec>())) .Callback((CommandGroupSpec s) => { res = s; }).Returns(new Mock <IXCommandGroup>().Object); mock.Setup(m => m.CommandGroups).Returns(new IXCommandGroup[] { cmdParentMock.Object }); mock.Object.AddCommandGroup <Commands3_e>(); Assert.AreEqual(parentSpec, res.Parent); Assert.Throws <ParentGroupNotFoundException>(() => mock.Object.AddCommandGroup <Commands4_e>()); Assert.Throws <ParentGroupCircularDependencyException>(() => mock.Object.CreateSpecFromEnum <Commands5_e>(-1, new CommandGroupSpec(20))); }
internal SwCommandGroup AddCommandGroupOrContextMenu(CommandGroupSpec cmdBar, bool isContextMenu, swSelectType_e?contextMenuSelectType) { m_Logger.Log($"Creating command group: {cmdBar.Id}", LoggerMessageSeverity_e.Debug); if (m_CommandBars.FirstOrDefault(b => b.Spec.Id == cmdBar.Id) != null) { throw new GroupIdAlreadyExistsException(cmdBar); } var title = GetMenuPath(cmdBar); var cmdGroup = CreateCommandGroup(cmdBar.Id, title, cmdBar.Tooltip, cmdBar.Commands.Select(c => c.UserId).ToArray(), isContextMenu, contextMenuSelectType); using (var iconsConv = m_SvcProvider.GetService <IIconsCreator>()) { CreateIcons(cmdGroup, cmdBar, iconsConv); var bar = new SwCommandGroup(m_App, cmdBar, cmdGroup, isContextMenu); CreateCommandItems(bar); m_CommandBars.Add(bar); return(bar); } }
internal SwCommandGroup(ISwApplication app, CommandGroupSpec spec, CommandGroup cmdGroup, bool isContextMenu) { m_App = app; Spec = spec; CommandGroup = cmdGroup; IsContextMenu = isContextMenu; }
private void CreateIcons(CommandGroup cmdGroup, CommandGroupSpec cmdBar, IconsConverter iconsConv) { var mainIcon = cmdBar.Icon; if (mainIcon == null) { mainIcon = Defaults.Icon; } Image[] iconList = null; if (cmdBar.Commands != null) { iconList = cmdBar.Commands.Select(c => IconsConverter.FromXImage(c.Icon ?? Defaults.Icon)).ToArray(); } //NOTE: if commands are not used, main icon will fail if toolbar commands image list is not specified, so it is required to specify it explicitly if (CompatibilityUtils.SupportsHighResIcons(m_App.Sw, CompatibilityUtils.HighResIconsScope_e.CommandManager)) { var iconsList = iconsConv.ConvertIcon(new CommandGroupHighResIcon(IconsConverter.FromXImage(mainIcon))); cmdGroup.MainIconList = iconsList; if (iconList != null && iconList.Any()) { cmdGroup.IconList = iconsConv.ConvertIconsGroup( iconList.Select(i => new CommandGroupHighResIcon(i)).ToArray()); } else { cmdGroup.IconList = iconsList; } } else { var mainIconPath = iconsConv.ConvertIcon(new CommandGroupIcon(IconsConverter.FromXImage(mainIcon))); var smallIcon = mainIconPath[0]; var largeIcon = mainIconPath[1]; cmdGroup.SmallMainIcon = smallIcon; cmdGroup.LargeMainIcon = largeIcon; if (iconList != null && iconList.Any()) { var iconListPath = iconsConv.ConvertIconsGroup(iconList.Select(i => new CommandGroupIcon(i)).ToArray()); var smallIconList = iconListPath[0]; var largeIconList = iconListPath[1]; cmdGroup.SmallIconList = smallIconList; cmdGroup.LargeIconList = largeIconList; } else { cmdGroup.SmallIconList = smallIcon; cmdGroup.LargeIconList = largeIcon; } } }
public IXCommandGroup AddContextMenu(CommandGroupSpec cmdBar, SelectType_e?owner) { swSelectType_e?selType = null; if (owner.HasValue) { selType = (swSelectType_e)owner; } return(AddCommandGroupOrContextMenu(cmdBar, true, selType)); }
private CommandGroup GetRootCommandGroup(CommandGroupSpec cmdBar) { var root = cmdBar; while (root.Parent != null) { root = root.Parent; } return(m_CommandBars.FirstOrDefault(b => b.Spec == root).CommandGroup); }
private string GetMenuPath(CommandGroupSpec cmdBar) { var title = new StringBuilder(); var parent = cmdBar.Parent; while (parent != null) { title.Insert(0, parent.Title + SUB_GROUP_SEPARATOR); parent = parent.Parent; } title.Append(cmdBar.Title); return(title.ToString()); }
private void OnExtensionConnect(IXExtension ext) { try { var cmdGrp = Extension.CommandManager.AddCommandGroup <CadPlusCommands_e>(); cmdGrp.CommandClick += OnCommandClick; m_ParentGrpSpec = cmdGrp.Spec; Connect?.Invoke(); } catch { new GenericMessageService("CAD+").ShowError("Failed to connect add-in"); throw; } }
public void TestAddCommandGroup() { CommandGroupSpec res = null; var mock = new Mock <IXCommandManager>(); mock.Setup(m => m.AddCommandGroup(It.IsAny <CommandGroupSpec>())) .Callback((CommandGroupSpec s) => { res = s; }).Returns(new Mock <IXCommandGroup>().Object); mock.Object.AddCommandGroup <Commands1_e>(); Assert.AreEqual(2, res.Commands.Length); Assert.AreEqual("Cmd1", res.Commands[0].Title); Assert.AreEqual("Cmd2", res.Commands[1].Title); }
private static CommandGroupSpec GetEnumCommandGroupParent(IXCommandManager cmdMgr, Type cmdGroupType) { CommandGroupSpec parent = null; CommandGroupParentAttribute grpParentAtt = null; if (cmdGroupType.TryGetAttribute <CommandGroupParentAttribute>(x => grpParentAtt = x)) { var groups = cmdMgr.CommandGroups.Select(c => c.Spec); if (grpParentAtt.ParentGroupType != null) { var parentGrpSpec = groups.OfType <EnumCommandGroupSpec>() .FirstOrDefault(g => g.CmdGrpEnumType == grpParentAtt.ParentGroupType); if (parentGrpSpec == null) { throw new ParentGroupNotFoundException(grpParentAtt.ParentGroupType.FullName, cmdGroupType.FullName); } if (grpParentAtt.ParentGroupType == cmdGroupType) { throw new ParentGroupCircularDependencyException(grpParentAtt.ParentGroupType.FullName); } parent = parentGrpSpec; } else { var parentGrpSpec = groups.OfType <CommandGroupSpec>() .FirstOrDefault(g => g.Id == grpParentAtt.ParentGroupUserId); if (parentGrpSpec == null) { throw new ParentGroupNotFoundException(grpParentAtt.ParentGroupUserId.ToString(), cmdGroupType.FullName); } parent = parentGrpSpec; } } return(parent); }
internal SwCommandGroup AddCommandGroupOrContextMenu(CommandGroupSpec cmdBar, bool isContextMenu, swSelectType_e?contextMenuSelectType) { m_Logger.Log($"Creating command group: {cmdBar.Id}"); if (m_CommandBars.FirstOrDefault(b => b.Spec.Id == cmdBar.Id) != null) { throw new GroupIdAlreadyExistsException(cmdBar); } var title = GetMenuPath(cmdBar); var cmdGroup = CreateCommandGroup(cmdBar.Id, title, cmdBar.Tooltip, cmdBar.Commands.Select(c => c.UserId).ToArray(), isContextMenu, contextMenuSelectType); var bar = new SwCommandGroup(m_App, cmdBar, cmdGroup); m_CommandBars.Add(bar); using (var iconsConv = new IconsConverter()) { CreateIcons(cmdGroup, cmdBar, iconsConv); var createdCmds = CreateCommandItems(bar, cmdBar.Id, cmdBar.Commands); var tabGroup = GetRootCommandGroup(cmdBar); try { CreateCommandTabBox(tabGroup, createdCmds); } catch (Exception ex) { m_Logger.Log(ex); //not critical error - continue operation } } return(bar); }
public void TestAddCommandGroupCustomAttributes() { CommandGroupSpec res = null; var mock = new Mock <IXCommandManager>(); mock.Setup(m => m.AddCommandGroup(It.IsAny <CommandGroupSpec>())) .Callback((CommandGroupSpec s) => { res = s; }).Returns(new Mock <IXCommandGroup>().Object); mock.Object.AddCommandGroup <Commands2_e>(); Assert.AreEqual(150, res.Id); Assert.AreEqual("CG1", res.Title); Assert.AreEqual("D0", res.Tooltip); Assert.That(res.Icon.Buffer.SequenceEqual(new byte[] { 0, 1 })); Assert.AreEqual(2, res.Commands.Length); Assert.AreEqual("C1", res.Commands[0].Title); Assert.AreEqual("D1", res.Commands[0].Tooltip); Assert.AreEqual(true, res.Commands[0].HasSpacer); Assert.AreEqual(false, res.Commands[0].HasMenu); Assert.AreEqual(true, res.Commands[0].HasToolbar); Assert.AreEqual(true, res.Commands[0].HasTabBox); Assert.AreEqual(Xarial.XCad.UI.Commands.Enums.RibbonTabTextDisplay_e.NoText, res.Commands[0].TabBoxStyle); Assert.AreEqual(Xarial.XCad.UI.Commands.Enums.WorkspaceTypes_e.Assembly, res.Commands[0].SupportedWorkspace); Assert.That(res.Commands[0].Icon.Buffer.SequenceEqual(new byte[] { 2, 3 })); Assert.AreEqual("C2", res.Commands[1].Title); Assert.AreEqual("D2", res.Commands[1].Tooltip); Assert.AreEqual(Xarial.XCad.UI.Commands.Enums.WorkspaceTypes_e.All, res.Commands[1].SupportedWorkspace); Assert.That(res.Commands[1].Icon.Buffer.SequenceEqual(new byte[] { 4, 5 })); }
public IXCommandGroup AddCommandGroup(CommandGroupSpec cmdBar) => AddCommandGroupOrContextMenu(cmdBar, false, null);
public IXCommandGroup AddCommandGroup(CommandGroupSpec cmdBar) { return(AddCommandGroupOrContextMenu(cmdBar, false, null)); }
public void ConfigureTab(CommandGroupSpec cmdGrpSpec, CommandGroupTabConfiguration config) { }
internal SwCommandGroup(ISwApplication app, CommandGroupSpec spec, CommandGroup cmdGroup) { Spec = spec; CommandGroup = cmdGroup; m_App = app; }
/// <param name="id">Id or -1 to automatically assign</param> private static EnumCommandGroupSpec CreateEnumCommandGroup <TCmdEnum>(IXCommandManager cmdMgr, CommandGroupSpec parent, string tabName, int id) where TCmdEnum : Enum { var cmdGroupType = typeof(TCmdEnum); if (parent != null) { if (parent.Id == id) { throw new ParentGroupCircularDependencyException($"{parent.Title} ({parent.Id})"); } } var bar = new EnumCommandGroupSpec(cmdGroupType, id); bar.RibbonTabName = tabName; bar.Parent = parent; bar.InitFromEnum <TCmdEnum>(); bar.Commands = Enum.GetValues(cmdGroupType).Cast <TCmdEnum>().Select( c => { var enumCmdUserId = Convert.ToInt32(c); if (enumCmdUserId < 0) { enumCmdUserId = 0; //default id (not used) } else { //NOTE: adding one to the id as 0 id means not used while enums start with 0 enumCmdUserId++; } return(CreateEnumCommand(c, enumCmdUserId)); }).ToArray(); return(bar); }
public static CommandGroupSpec CreateSpecFromEnum <TCmdEnum>(this IXCommandManager cmdMgr, int id = -1, CommandGroupSpec parent = null) where TCmdEnum : Enum => CreateEnumCommandGroup <TCmdEnum>(cmdMgr, parent, id);
private static EnumCommandGroupSpec CreateEnumCommandGroup <TCmdEnum>(IXCommandManager cmdMgr, CommandGroupSpec parent, int id) where TCmdEnum : Enum { var cmdGroupType = typeof(TCmdEnum); if (id == -1) { id = GetEnumCommandGroupId(cmdMgr, cmdGroupType); } if (parent != null) { if (parent.Id == id) { throw new ParentGroupCircularDependencyException($"{parent.Title} ({parent.Id})"); } } var bar = new EnumCommandGroupSpec(cmdGroupType, id); bar.Parent = parent; bar.InitFromEnum <TCmdEnum>(); bar.Commands = Enum.GetValues(cmdGroupType).Cast <TCmdEnum>().Select( c => CreateCommand(c)).ToArray(); return(bar); }
internal GroupIdAlreadyExistsException(CommandGroupSpec cmdBar) : base($"Group {cmdBar.Title} id ({cmdBar.Id}) already exists. Make sure that all group enumerators decorated with {typeof(CommandGroupInfoAttribute)} have unique values for id") { }
public static EnumCommandGroupSpec CreateSpecFromEnum <TCmdEnum>(this IXCommandManager cmdMgr, CommandGroupSpec parent, int?id) where TCmdEnum : Enum { var isUserIdAssigned = TryGetUserAssignedGroupId(typeof(TCmdEnum), out string tabName, out int userId); if (!id.HasValue) { if (isUserIdAssigned) { id = userId; } else { throw new GroupUserIdNotAssignedException(); } } return(CreateEnumCommandGroup <TCmdEnum>(cmdMgr, parent, tabName, id.Value)); }