Added CommandBar implementation for Windows Forms

This commit is contained in:
Michael Becker 2014-06-26 11:28:44 -04:00
parent 40b9fd359c
commit 98c8990835
4 changed files with 279 additions and 6 deletions

View File

@ -84,11 +84,144 @@ namespace UniversalEditor.UserInterface.WindowsForms
mbMenuBar.Items.Clear();
foreach (CommandItem item in Engine.CurrentEngine.MainMenu.Items)
{
LoadCommandBarItem(item, null);
LoadCommandBarMenuItem(item, null);
}
foreach (CommandBar bar in Engine.CurrentEngine.CommandBars)
{
LoadCommandBar(bar);
}
}
private void LoadCommandBarItem(CommandItem item, ToolStripMenuItem parent)
private void LoadCommandBar(CommandBar bar)
{
AwesomeControls.CommandBars.CBToolBar tb = new AwesomeControls.CommandBars.CBToolBar();
tb.Text = bar.Title;
foreach (CommandItem item in bar.Items)
{
LoadCommandBarItem(item, tb);
}
cbc.TopToolStripPanel.Controls.Add(tb);
}
private ToolStripItem InitializeCommandBarItem(CommandItem item, bool isOnDropDown = false)
{
if (item is SeparatorCommandItem)
{
return new ToolStripSeparator();
}
else if (item is CommandReferenceCommandItem)
{
CommandReferenceCommandItem crci = (item as CommandReferenceCommandItem);
Command cmd = Engine.CurrentEngine.Commands[crci.CommandID];
if (cmd == null)
{
Console.WriteLine("could not find command '" + crci.CommandID + "'");
return null;
}
if (cmd.Items.Count > 0)
{
ToolStripDropDownItem tsi = null;
if (!isOnDropDown)
{
if (!String.IsNullOrEmpty(cmd.DefaultCommandID))
{
ToolStripSplitButton tsb = new ToolStripSplitButton();
tsb.ButtonClick += tsbCommandBarButton_Click;
tsi = tsb;
}
else
{
ToolStripDropDownButton tsb = new ToolStripDropDownButton();
tsi = tsb;
}
}
else
{
ToolStripMenuItem tsmi = new ToolStripMenuItem();
tsi = tsmi;
}
if (tsi == null)
{
Console.WriteLine("ToolStripDropDownItem was not loaded!");
return null;
}
tsi.Tag = cmd;
tsi.Text = cmd.Title.Replace("_", "&");
foreach (CommandItem item1 in cmd.Items)
{
LoadCommandBarItem(item1, tsi);
}
return tsi;
}
else
{
ToolStripItem tsi = null;
if (!isOnDropDown)
{
tsi = new ToolStripButton();
}
else
{
tsi = new ToolStripMenuItem();
}
tsi.Tag = cmd;
tsi.Text = cmd.Title.Replace("_", "&");
tsi.Click += tsbCommandBarButton_Click;
return tsi;
}
}
return null;
}
private void LoadCommandBarItem(CommandItem item, ToolStripDropDownItem parent)
{
ToolStripItem tsi = InitializeCommandBarItem(item, true);
if (tsi == null) return;
parent.DropDownItems.Add(tsi);
}
private void LoadCommandBarItem(CommandItem item, AwesomeControls.CommandBars.CBToolBar parent)
{
ToolStripItem tsi = InitializeCommandBarItem(item);
if (tsi == null) return;
parent.Items.Add(tsi);
}
private void tsbCommandBarButton_Click(object sender, EventArgs e)
{
if (sender is ToolStripDropDownItem)
{
ToolStripDropDownItem tsi = (sender as ToolStripDropDownItem);
Command cmd = (tsi.Tag as Command);
if (tsi.DropDownItems.Count > 0)
{
Command cmd1 = Engine.CurrentEngine.Commands[cmd.DefaultCommandID];
if (cmd1 == null)
{
Console.WriteLine("could not find command '" + cmd.DefaultCommandID + "'");
return;
}
cmd1.Execute();
}
else
{
cmd.Execute();
}
}
else if (sender is ToolStripItem)
{
ToolStripItem tsi = (sender as ToolStripItem);
Command cmd = (tsi.Tag as Command);
cmd.Execute();
}
}
private void LoadCommandBarMenuItem(CommandItem item, ToolStripMenuItem parent)
{
ToolStripItem tsi = null;
@ -108,7 +241,7 @@ namespace UniversalEditor.UserInterface.WindowsForms
tsmi.Text = cmd.Title.Replace("_", "&");
foreach (CommandItem item1 in cmd.Items)
{
LoadCommandBarItem(item1, tsmi);
LoadCommandBarMenuItem(item1, tsmi);
}
tsi = tsmi;
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.UserInterface
{
public class CommandBar
{
public class CommandBarCollection
: System.Collections.ObjectModel.Collection<CommandBar>
{
}
private string mvarID = String.Empty;
public string ID { get { return mvarID; } set { mvarID = value; } }
private string mvarTitle = String.Empty;
public string Title { get { return mvarTitle; } set { mvarTitle = value; } }
private CommandItem.CommandItemCollection mvarItems = new CommandItem.CommandItemCollection();
public CommandItem.CommandItemCollection Items { get { return mvarItems; } }
}
}

View File

@ -218,9 +218,15 @@ namespace UniversalEditor.UserInterface
private EngineMainMenu mvarMainMenu = new EngineMainMenu();
/// <summary>
/// The main menu of this application, which can hold multiple <see cref="Command"/>s.
/// The main menu of this application, which can hold multiple <see cref="CommandItem"/>s.
/// </summary>
public EngineMainMenu MainMenu { get { return mvarMainMenu; } }
private CommandBar.CommandBarCollection mvarCommandBars = new CommandBar.CommandBarCollection();
/// <summary>
/// The command bars loaded in this application, which can each hold multiple <see cref="CommandItem"/>s.
/// </summary>
public CommandBar.CommandBarCollection CommandBars { get { return mvarCommandBars; } }
/// <summary>
/// The aggregated raw markup of all the various XML files loaded in the current search path.
@ -310,6 +316,8 @@ namespace UniversalEditor.UserInterface
{
#region Load the XML files
string[] xmlfiles = System.IO.Directory.GetFiles(mvarBasePath, "*.xml", System.IO.SearchOption.AllDirectories);
UpdateSplashScreenStatus("Loading XML configuration files", 0, 0, xmlfiles.Length);
XMLDataFormat xdf = new XMLDataFormat();
foreach (string xmlfile in xmlfiles)
@ -321,11 +329,16 @@ namespace UniversalEditor.UserInterface
doc.Accessor.Open ();
doc.Load ();
doc.Close ();
markup.CopyTo (mvarRawMarkup);
markup.CopyTo(mvarRawMarkup);
UpdateSplashScreenStatus("Loading XML configuration files", Array.IndexOf(xmlfiles, xmlfile) + 1);
}
UpdateSplashScreenStatus("Loading available commands");
#endregion
#region Initialize the configuration with the loaded data
MarkupTagElement tagCommands = (mvarRawMarkup.FindElement ("UniversalEditor", "Application", "Commands") as MarkupTagElement);
@ -342,6 +355,12 @@ namespace UniversalEditor.UserInterface
Command cmd = new Command();
cmd.ID = attID.Value;
MarkupAttribute attDefaultCommandID = tagCommand.Attributes["DefaultCommandID"];
if (attDefaultCommandID != null)
{
cmd.DefaultCommandID = attDefaultCommandID.Value;
}
MarkupAttribute attTitle = tagCommand.Attributes["Title"];
if (attTitle != null)
@ -368,6 +387,8 @@ namespace UniversalEditor.UserInterface
mvarCommands.Add (cmd);
}
}
UpdateSplashScreenStatus("Loading main menu items");
MarkupTagElement tagMainMenuItems = (mvarRawMarkup.FindElement ("UniversalEditor", "Application", "MainMenu", "Items") as MarkupTagElement);
foreach (MarkupElement elItem in tagMainMenuItems.Elements)
@ -377,6 +398,19 @@ namespace UniversalEditor.UserInterface
InitializeMainMenuItem(tagItem, null);
}
UpdateSplashScreenStatus("Loading command bars");
MarkupTagElement tagCommandBars = (mvarRawMarkup.FindElement("UniversalEditor", "Application", "CommandBars") as MarkupTagElement);
foreach (MarkupElement elCommandBar in tagCommandBars.Elements)
{
MarkupTagElement tagCommandBar = (elCommandBar as MarkupTagElement);
if (tagCommandBar == null) continue;
if (tagCommandBar.FullName != "CommandBar") continue;
InitializeCommandBar(tagCommandBar);
}
UpdateSplashScreenStatus("Loading languages and translations");
MarkupTagElement tagLanguages = (mvarRawMarkup.FindElement("UniversalEditor", "Application", "Languages") as MarkupTagElement);
foreach (MarkupElement elLanguage in tagLanguages.Elements)
{
@ -393,6 +427,8 @@ namespace UniversalEditor.UserInterface
}
#endregion
UpdateSplashScreenStatus("Setting language");
if (mvarDefaultLanguage != null)
{
foreach (Command cmd in mvarCommands)
@ -400,6 +436,9 @@ namespace UniversalEditor.UserInterface
cmd.Title = mvarDefaultLanguage.GetCommandTitle(cmd.ID, cmd.ID);
}
}
UpdateSplashScreenStatus("Finalizing configuration");
}
private void InitializeLanguage(MarkupTagElement tag)
@ -461,6 +500,80 @@ namespace UniversalEditor.UserInterface
mvarLanguages.Add(lang);
}
private void InitializeCommandBar(MarkupTagElement tag)
{
MarkupAttribute attID = tag.Attributes["ID"];
if (attID == null) return;
CommandBar cb = new CommandBar();
cb.ID = attID.Value;
MarkupAttribute attTitle = tag.Attributes["Title"];
if (attTitle != null)
{
cb.Title = attTitle.Value;
}
else
{
cb.Title = cb.ID;
}
MarkupTagElement tagItems = tag.Elements["Items"] as MarkupTagElement;
if (tagItems != null)
{
foreach (MarkupElement elItem in tagItems.Elements)
{
MarkupTagElement tagItem = (elItem as MarkupTagElement);
if (tagItem == null) continue;
switch (tagItem.FullName)
{
case "CommandReference":
{
MarkupAttribute attCommandID = tagItem.Attributes["CommandID"];
if (attCommandID != null)
{
cb.Items.Add(new CommandReferenceCommandItem(attCommandID.Value));
}
break;
}
case "Separator":
{
cb.Items.Add(new SeparatorCommandItem());
break;
}
}
}
}
mvarCommandBars.Add(cb);
}
private void InitializeCommandBarItem(MarkupTagElement tag, CommandBar parent)
{
CommandItem item = null;
switch (tag.FullName)
{
case "CommandReference":
{
MarkupAttribute attCommandID = tag.Attributes["CommandID"];
if (attCommandID != null)
{
item = new CommandReferenceCommandItem(attCommandID.Value);
}
break;
}
case "Separator":
{
item = new SeparatorCommandItem();
break;
}
}
if (item != null)
{
parent.Items.Add(item);
}
}
private void InitializeMainMenuItem(MarkupTagElement tag, Command parent)
{
CommandItem item = null;
@ -525,6 +638,7 @@ namespace UniversalEditor.UserInterface
private void Initialize()
{
System.Threading.Thread threadLoader = new System.Threading.Thread(threadLoader_ThreadStart);
threadLoader.Name = "Initialization Thread";
threadLoader.Start();
ShowSplashScreen();

View File

@ -54,6 +54,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BookmarksManager.cs" />
<Compile Include="CommandBar.cs" />
<Compile Include="CommandDisplayStyle.cs" />
<Compile Include="CommandShortcutKey.cs" />
<Compile Include="Common\Reflection.cs" />