Implementing Commands on Linux

This commit is contained in:
Michael Becker 2014-06-23 23:58:57 -04:00
parent daabf4a93d
commit f5db2c2173
10 changed files with 377 additions and 33 deletions

View File

@ -1,11 +1,74 @@
using System;
using Gtk;
using UniversalEditor.UserInterface;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
InitializeMenuBar();
}
private void InitializeMenuBar()
{
foreach (CommandItem item in Engine.CurrentEngine.MainMenu.Items)
{
CreateCommandItem(item, null);
}
menubar1.ShowAll ();
}
private void CreateCommandItem(CommandItem item, Menu parentMenu)
{
Gtk.MenuItem menuItem = null;
if (item is CommandReferenceCommandItem)
{
CommandReferenceCommandItem crci = (item as CommandReferenceCommandItem);
Command cmd = Engine.CurrentEngine.Commands[crci.CommandID];
if (cmd == null)
{
HostApplication.Messages.Add(HostApplicationMessageSeverity.Warning, "The command '" + crci.CommandID + "' was not found");
return;
}
menuItem = new Gtk.MenuItem(cmd.Title);
if (cmd.Items.Count > 0)
{
Menu submenu = CreateCommandItemSubmenu(cmd);
menuItem.Submenu = submenu;
}
}
else if (item is SeparatorCommandItem)
{
menuItem = new Gtk.SeparatorMenuItem();
}
if (menuItem != null)
{
if (parentMenu == null)
{
menubar1.Append(menuItem);
}
else
{
parentMenu.Append(menuItem);
}
}
}
private Menu CreateCommandItemSubmenu(Command cmd)
{
Menu menu = new Menu();
if (Engine.CurrentEngine.MainMenu.EnableTearoff && cmd.EnableTearoff)
{
menu.Append(new TearoffMenuItem());
}
foreach (CommandItem item in cmd.Items)
{
CreateCommandItem(item, menu);
}
return menu;
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)

View File

@ -20,6 +20,8 @@ public partial class MainWindow
private global::Gtk.Action CutAction;
private global::Gtk.VBox vbox1;
private global::Gtk.MenuBar menubar1;
private global::Gtk.Toolbar toolbar1;
private global::Gtk.Notebook notebook1;
private global::Gtk.Statusbar statusbar1;
private global::Gtk.Label lblStatus;
@ -151,7 +153,7 @@ public partial class MainWindow
this.vbox1.Name = "vbox1";
this.vbox1.Spacing = 6;
// Container child vbox1.Gtk.Box+BoxChild
this.UIManager.AddUiFromString ("<ui><menubar name='menubar1'><menu name='FileAction' action='FileAction'><menu name='FileNew' action='FileNew'><menuitem name='FileNewDocument' action='FileNewDocument'/><menuitem name='FileNewProject' action='FileNewProject'/></menu><menu name='FileOpen' action='FileOpen'><menuitem name='FileOpenDocument' action='FileOpenDocument'/><menuitem name='FileOpenProject' action='FileOpenProject'/></menu><menu name='CloseAction' action='CloseAction'><menuitem name='DocumentAction' action='DocumentAction'/><menuitem name='ProjectAction' action='ProjectAction'/><menuitem name='WindowAction' action='WindowAction'/></menu><menuitem name='quitAction' action='quitAction'/></menu><menu name='EditAction' action='EditAction'><menuitem name='CutAction' action='CutAction'/></menu></menubar></ui>");
this.UIManager.AddUiFromString ("<ui><menubar name='menubar1'/></ui>");
this.menubar1 = ((global::Gtk.MenuBar)(this.UIManager.GetWidget ("/menubar1")));
this.menubar1.Name = "menubar1";
this.vbox1.Add (this.menubar1);
@ -160,6 +162,24 @@ public partial class MainWindow
w2.Expand = false;
w2.Fill = false;
// Container child vbox1.Gtk.Box+BoxChild
this.UIManager.AddUiFromString ("<ui><toolbar name='toolbar1'/></ui>");
this.toolbar1 = ((global::Gtk.Toolbar)(this.UIManager.GetWidget ("/toolbar1")));
this.toolbar1.Name = "toolbar1";
this.toolbar1.ShowArrow = false;
this.vbox1.Add (this.toolbar1);
global::Gtk.Box.BoxChild w3 = ((global::Gtk.Box.BoxChild)(this.vbox1 [this.toolbar1]));
w3.Position = 1;
w3.Expand = false;
w3.Fill = false;
// Container child vbox1.Gtk.Box+BoxChild
this.notebook1 = new global::Gtk.Notebook ();
this.notebook1.CanFocus = true;
this.notebook1.Name = "notebook1";
this.notebook1.CurrentPage = -1;
this.vbox1.Add (this.notebook1);
global::Gtk.Box.BoxChild w4 = ((global::Gtk.Box.BoxChild)(this.vbox1 [this.notebook1]));
w4.Position = 2;
// Container child vbox1.Gtk.Box+BoxChild
this.statusbar1 = new global::Gtk.Statusbar ();
this.statusbar1.Name = "statusbar1";
this.statusbar1.Spacing = 6;
@ -170,19 +190,19 @@ public partial class MainWindow
this.lblStatus.Xalign = 0F;
this.lblStatus.LabelProp = global::Mono.Unix.Catalog.GetString ("Ready");
this.statusbar1.Add (this.lblStatus);
global::Gtk.Box.BoxChild w3 = ((global::Gtk.Box.BoxChild)(this.statusbar1 [this.lblStatus]));
w3.Position = 0;
global::Gtk.Box.BoxChild w5 = ((global::Gtk.Box.BoxChild)(this.statusbar1 [this.lblStatus]));
w5.Position = 0;
this.vbox1.Add (this.statusbar1);
global::Gtk.Box.BoxChild w4 = ((global::Gtk.Box.BoxChild)(this.vbox1 [this.statusbar1]));
w4.Position = 2;
w4.Expand = false;
w4.Fill = false;
global::Gtk.Box.BoxChild w6 = ((global::Gtk.Box.BoxChild)(this.vbox1 [this.statusbar1]));
w6.Position = 3;
w6.Expand = false;
w6.Fill = false;
this.Add (this.vbox1);
if ((this.Child != null)) {
this.Child.ShowAll ();
}
this.DefaultWidth = 400;
this.DefaultHeight = 300;
this.DefaultHeight = 299;
this.Show ();
this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
}

View File

@ -6,9 +6,9 @@
</configuration>
<import>
<widget-library name="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<widget-library name="../../../Output/Debug/UniversalEditor.Environments.GTK.dll" internal="true" />
<widget-library name="../../../../../Output/Debug/UniversalEditor.Environments.GTK.dll" internal="true" />
</import>
<widget class="Gtk.Window" id="MainWindow" design-size="400 300">
<widget class="Gtk.Window" id="MainWindow" design-size="400 299">
<action-group name="Default">
<action id="FileAction">
<property name="Type">Action</property>
@ -93,27 +93,7 @@
<child>
<widget class="Gtk.MenuBar" id="menubar1">
<property name="MemberName" />
<node name="menubar1" type="Menubar">
<node type="Menu" action="FileAction">
<node type="Menu" action="FileNew">
<node type="Menuitem" action="FileNewDocument" />
<node type="Menuitem" action="FileNewProject" />
</node>
<node type="Menu" action="FileOpen">
<node type="Menuitem" action="FileOpenDocument" />
<node type="Menuitem" action="FileOpenProject" />
</node>
<node type="Menu" action="CloseAction">
<node type="Menuitem" action="DocumentAction" />
<node type="Menuitem" action="ProjectAction" />
<node type="Menuitem" action="WindowAction" />
</node>
<node type="Menuitem" action="quitAction" />
</node>
<node type="Menu" action="EditAction">
<node type="Menuitem" action="CutAction" />
</node>
</node>
<node name="menubar1" type="Menubar" />
</widget>
<packing>
<property name="Position">0</property>
@ -123,7 +103,28 @@
</packing>
</child>
<child>
<placeholder />
<widget class="Gtk.Toolbar" id="toolbar1">
<property name="MemberName" />
<property name="ShowArrow">False</property>
<node name="__gtksharp_143_Stetic_Editor_ActionToolbar" type="Toolbar" />
</widget>
<packing>
<property name="Position">1</property>
<property name="AutoSize">True</property>
<property name="Expand">False</property>
<property name="Fill">False</property>
</packing>
</child>
<child>
<widget class="Gtk.Notebook" id="notebook1">
<property name="MemberName" />
<property name="CanFocus">True</property>
<property name="CurrentPage">-1</property>
</widget>
<packing>
<property name="Position">2</property>
<property name="AutoSize">False</property>
</packing>
</child>
<child>
<widget class="Gtk.Statusbar" id="statusbar1">
@ -143,7 +144,7 @@
</child>
</widget>
<packing>
<property name="Position">2</property>
<property name="Position">3</property>
<property name="AutoSize">True</property>
<property name="Expand">False</property>
<property name="Fill">False</property>

View File

@ -0,0 +1,71 @@
using System;
namespace UniversalEditor.UserInterface
{
public class Command
{
public class CommandCollection
: System.Collections.ObjectModel.Collection<Command>
{
public Command this[string ID]
{
get
{
foreach (Command command in this)
{
if (command.ID == ID) return command;
}
return null;
}
}
}
private bool mvarEnableTearoff = false;
public bool EnableTearoff { get { return mvarEnableTearoff; } set { mvarEnableTearoff = value; } }
private string mvarID = String.Empty;
/// <summary>
/// The ID of the command, used to reference it in <see cref="CommandReferenceCommandItem"/>.
/// </summary>
public string ID { get { return mvarID; } set { mvarID = value; } }
private string mvarTitle = String.Empty;
/// <summary>
/// The title of the command (including mnemonic prefix, if applicable).
/// </summary>
public string Title { get { return mvarTitle; } set { mvarTitle = value; } }
private StockCommandType mvarStockCommandType = StockCommandType.None;
/// <summary>
/// A <see cref="StockCommandType"/> that represents a predefined, platform-themed command.
/// </summary>
public StockCommandType StockCommandType { get { return mvarStockCommandType; } set { mvarStockCommandType = value; } }
private string mvarImageFileName = String.Empty;
/// <summary>
/// The file name of the image to be displayed on the command.
/// </summary>
public string ImageFileName { get { return mvarImageFileName; } set { mvarImageFileName = value; } }
private CommandItem.CommandItemCollection mvarItems = new CommandItem.CommandItemCollection();
/// <summary>
/// The child <see cref="CommandItem"/>s that are contained within this <see cref="Command"/>.
/// </summary>
public CommandItem.CommandItemCollection Items { get { return mvarItems; } }
/// <summary>
/// The event that is fired when the command is executed.
/// </summary>
public event EventHandler Executed;
/// <summary>
/// Executes this <see cref="Command"/>.
/// </summary>
public void Execute()
{
if (Executed != null) Executed(this, EventArgs.Empty);
}
}
}

View File

@ -0,0 +1,25 @@
using System;
namespace UniversalEditor.UserInterface
{
public abstract class CommandItem
{
public class CommandItemCollection
: System.Collections.ObjectModel.Collection<CommandItem>
{
}
}
public class CommandReferenceCommandItem : CommandItem
{
private string mvarCommandID = String.Empty;
public string CommandID { get { return mvarCommandID; } set { mvarCommandID = value; } }
public CommandReferenceCommandItem(string commandID)
{
mvarCommandID = commandID;
}
}
public class SeparatorCommandItem : CommandItem
{
}
}

View File

@ -3,10 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UniversalEditor.Accessors;
using UniversalEditor.ObjectModels.FileSystem;
using UniversalEditor.ObjectModels.PropertyList;
using UniversalEditor.DataFormats.Markup.XML;
using UniversalEditor.ObjectModels.Markup;
namespace UniversalEditor.UserInterface
{
public abstract class Engine
@ -77,6 +81,23 @@ namespace UniversalEditor.UserInterface
protected abstract void MainLoop();
private Command.CommandCollection mvarCommands = new Command.CommandCollection();
/// <summary>
/// The commands defined for this application.
/// </summary>
public Command.CommandCollection Commands { get { return mvarCommands; } }
private EngineMainMenu mvarMainMenu = new EngineMainMenu();
/// <summary>
/// The main menu of this application, which can hold multiple <see cref="Command"/>s.
/// </summary>
public EngineMainMenu MainMenu { get { return mvarMainMenu; } }
/// <summary>
/// The aggregated raw markup of all the various XML files loaded in the current search path.
/// </summary>
private MarkupObjectModel mvarRawMarkup = new MarkupObjectModel();
private PropertyListObjectModel mvarConfiguration = new PropertyListObjectModel();
public PropertyListObjectModel Configuration { get { return mvarConfiguration; } }
@ -125,6 +146,108 @@ namespace UniversalEditor.UserInterface
}
}
protected virtual void InitializeXMLConfiguration()
{
#region Load the XML files
string[] xmlfiles = System.IO.Directory.GetFiles(mvarBasePath, "*.xml", System.IO.SearchOption.AllDirectories);
XMLDataFormat xdf = new XMLDataFormat();
foreach (string xmlfile in xmlfiles)
{
MarkupObjectModel markup = new MarkupObjectModel();
Document doc = new Document(markup, xdf, new FileAccessor(xmlfile));
doc.Accessor.Open ();
doc.Load ();
doc.Close ();
markup.CopyTo (mvarRawMarkup);
}
#endregion
#region Initialize the configuration with the loaded data
MarkupTagElement tagCommands = (mvarRawMarkup.FindElement ("UniversalEditor", "Application", "Commands") as MarkupTagElement);
if (tagCommands != null)
{
foreach (MarkupElement elCommand in tagCommands.Elements)
{
MarkupTagElement tagCommand = (elCommand as MarkupTagElement);
if (tagCommand == null) continue;
if (tagCommand.FullName != "Command") continue;
MarkupAttribute attID = tagCommand.Attributes["ID"];
if (attID == null) continue;
Command cmd = new Command();
cmd.ID = attID.Value;
MarkupAttribute attTitle = tagCommand.Attributes["Title"];
if (attTitle != null)
{
cmd.Title = attTitle.Value;
}
MarkupTagElement tagItems = (tagCommand.Elements["Items"] as MarkupTagElement);
if (tagItems != null)
{
foreach (MarkupElement el in tagItems.Elements)
{
MarkupTagElement tag = (el as MarkupTagElement);
if (tag == null) continue;
InitializeMainMenuItem(tag, cmd);
}
}
mvarCommands.Add (cmd);
}
}
MarkupTagElement tagMainMenuItems = (mvarRawMarkup.FindElement ("UniversalEditor", "Application", "MainMenu", "Items") as MarkupTagElement);
foreach (MarkupElement elItem in tagMainMenuItems.Elements)
{
MarkupTagElement tagItem = (elItem as MarkupTagElement);
if (tagItem == null) continue;
InitializeMainMenuItem(tagItem, null);
}
#endregion
}
private void InitializeMainMenuItem(MarkupTagElement tag, Command 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)
{
if (parent == null)
{
mvarMainMenu.Items.Add(item);
}
else
{
parent.Items.Add(item);
}
}
}
protected virtual void InitializeBranding()
{
@ -161,6 +284,9 @@ namespace UniversalEditor.UserInterface
// overridden with a switch (/basepath:...) ?
mvarBasePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
// Initialize the XML files
InitializeXMLConfiguration();
// Initialize the branding for the selected application
InitializeBranding();

View File

@ -0,0 +1,16 @@
using System;
namespace UniversalEditor.UserInterface
{
public class EngineMainMenu
{
private bool mvarEnableTearoff = false;
/// <summary>
/// Determines whether menus can be torn off to display their contents in a separate window.
/// </summary>
public bool EnableTearoff { get { return mvarEnableTearoff; } set { mvarEnableTearoff = value; } }
private CommandItem.CommandItemCollection mvarItems = new CommandItem.CommandItemCollection();
public CommandItem.CommandItemCollection Items { get { return mvarItems; } }
}
}

View File

@ -8,12 +8,21 @@ namespace UniversalEditor.UserInterface
public static class HostApplication
{
private static IHostApplicationWindow mvarCurrentWindow = null;
/// <summary>
/// Gets or sets the current window of the host application.
/// </summary>
public static IHostApplicationWindow CurrentWindow { get { return mvarCurrentWindow; } set { mvarCurrentWindow = value; } }
private static HostApplicationOutputWindow mvarOutputWindow = new HostApplicationOutputWindow();
/// <summary>
/// Gets or sets the output window of the host application, where other plugins can read from and write to.
/// </summary>
public static HostApplicationOutputWindow OutputWindow { get { return mvarOutputWindow; } set { mvarOutputWindow = value; } }
private static HostApplicationMessage.HostApplicationMessageCollection mvarMessages = new HostApplicationMessage.HostApplicationMessageCollection();
/// <summary>
/// A collection of messages to display in the Error List panel.
/// </summary>
public static HostApplicationMessage.HostApplicationMessageCollection Messages { get { return mvarMessages; } }
}
}

View File

@ -0,0 +1,9 @@
using System;
namespace UniversalEditor.UserInterface
{
public enum StockCommandType
{
None
}
}

View File

@ -74,6 +74,10 @@
<Compile Include="Toolbox.cs" />
<Compile Include="UnsavedDocumentOption.cs" />
<Compile Include="WindowState.cs" />
<Compile Include="CommandItem.cs" />
<Compile Include="Command.cs" />
<Compile Include="StockCommandType.cs" />
<Compile Include="EngineMainMenu.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">