Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommandBar by ID #1

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 75 additions & 69 deletions Source/ExcelDna.Integration/ExcelCommandBars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,6 @@ namespace ExcelDna.Integration.CustomUI
{
public delegate Bitmap GetImageDelegate(string imageName);

//public class ExcelCommandBars
//{
// public ExcelCommandBars()
// {
// }

// public virtual string GetCustomUI()
// {
// }

// public virtual object GetImage(string imageName)
// {

// }

// public CommandBarControls Controls
// {
// get
// {
// }
// }
//}

public static class ExcelCommandBarUtil
{
// List of loaded CustomUI
Expand Down Expand Up @@ -144,16 +121,8 @@ private static void AddCommandBarControls(Application excelApp, XmlNodeList xmlN
{
if (childNode.Name == "commandBar")
{
string barName = childNode.Attributes["name"].Value;
CommandBar bar = null;
for (int i = 1; i <= excelApp.CommandBars.Count; i++)
{
if (excelApp.CommandBars[i].Name == barName)
{
bar = excelApp.CommandBars[i];
break;
}
}
string barName;
CommandBar bar = GetCommandBarFromIdOrName(excelApp, childNode.Attributes, out barName);
if (bar != null)
{
AddControls(bar.Controls, childNode.ChildNodes, getImage);
Expand Down Expand Up @@ -187,16 +156,8 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x
{
if (childNode.Name == "commandBar")
{
string barName = childNode.Attributes["name"].Value;
CommandBar bar = null;
for (int i = 1; i <= excelApp.CommandBars.Count; i++)
{
if (excelApp.CommandBars[i].Name == barName)
{
bar = excelApp.CommandBars[i];
break;
}
}
string barName;
CommandBar bar = GetCommandBarFromIdOrName(excelApp, childNode.Attributes, out barName);
if (bar != null)
{
RemoveControls(bar.Controls, childNode.ChildNodes);
Expand All @@ -210,6 +171,52 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x
}
}

//We cannot rely only on name to recover the proper CommandBar so we have the possibility to use the ID (which is used in priority).
//Indeed there are two CommandBar for "Cell" see http://msdn.microsoft.com/en-us/library/office/gg469862(v=office.14).aspx
//However, at the time of the writing there is a mistake: "Application.CommandBars(Application.CommandBars("Cell").Index + 3)" is false in practice
// Note that the Id property is only available
private static CommandBar GetCommandBarFromIdOrName(Application excelApp, XmlAttributeCollection nodeAttributes, out string barName)
{
var id = nodeAttributes["id"];
var name = nodeAttributes["name"];
if(name ==null) throw new ArgumentException("commandBar attributes must contain name");

barName = name.Value;
if (id != null)
{
string barId = id.Value;
CommandBar bar = null;
for (int i = 1; i <= excelApp.CommandBars.Count; i++)
{
var currentBar = excelApp.CommandBars[i];
if (currentBar.Type == MsoBarType.msoBarTypePopup)
{
var currentBarPopup = new CommandBarPopup(currentBar.GetComObject());
if (currentBarPopup.Id == barId)
{
bar = currentBar;
break;
}
}
}
return bar;
}
else
{

CommandBar bar = null;
for (int i = 1; i <= excelApp.CommandBars.Count; i++)
{
if (excelApp.CommandBars[i].Name == barName)
{
bar = excelApp.CommandBars[i];
break;
}
}
return bar;
}
}

private static void AddControls(CommandBarControls parentControls, XmlNodeList xmlNodes, GetImageDelegate getImage)
{
foreach (XmlNode childNode in xmlNodes)
Expand Down Expand Up @@ -507,6 +514,14 @@ public bool Visible
}
}

public MsoBarType Type
{
get
{
return (MsoBarType)ComObjectType.InvokeMember("Type", BindingFlags.GetProperty, null, ComObject, null);
}
}

public CommandBarControl FindControl(object type, object id, object tag, object visible, object recursive)
{
object result = ComObjectType.InvokeMember("FindControl", BindingFlags.InvokeMethod, null, ComObject, new object[] { type, id, tag, visible, recursive });
Expand Down Expand Up @@ -571,16 +586,6 @@ public int Count
return Convert.ToInt32(i);
}
}

//public event EventHandler OnUpdate
//{
// add
// {
// }
// remove
// {
// }
//}
}

public class CommandBarControl
Expand Down Expand Up @@ -777,7 +782,16 @@ public int Index
return (int)ComObjectType.InvokeMember("Index", BindingFlags.GetProperty, null, ComObject, null);
}
}


public string Id
{
get
{
object controls = ComObjectType.InvokeMember("Id", BindingFlags.GetProperty, null, ComObject, null);
return controls.ToString();
}
}

public void Delete(object Temporary)
{
ComObjectType.InvokeMember("Delete", BindingFlags.InvokeMethod, null, ComObject, new object[] { Temporary });
Expand Down Expand Up @@ -941,11 +955,6 @@ public void RemovePopup(string name)
}
}

//public class CommandBarButtonClickEventArgs : EventArgs
//{
// public bool CancelDefault;
//}

public class CommandBarButton : CommandBarControl
{
internal CommandBarButton(object commandBarCom)
Expand Down Expand Up @@ -1003,16 +1012,6 @@ public string ShortcutText
ComObjectType.InvokeMember("ShortcutText", BindingFlags.SetProperty, null, ComObject, new object[] { value });
}
}

//public event EventHandler<CommandBarButtonClickEventArgs> Click
//{
// add
// {
// }
// remove
// {
// }
//}
}

public class CommandBarPopup : CommandBarControl
Expand Down Expand Up @@ -1099,4 +1098,11 @@ public enum MsoControlType
msoControlWorkPane = 25,
msoControlAutoCompleteCombo = 26,
}

public enum MsoBarType
{
msoBarTypeNormal = 0,
msoBarTypeMenuBar = 1,
msoBarTypePopup = 2,
}
}