// This file (FrmMain.cs) is a part of the IBBoard.WarFoundry.Forge.WinForms project and is copyright 2007, 2008, 2009 IBBoard. // // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Threading; using log4net; using log4net.Config; using IBBoard; using IBBoard.CustomMath; using IBBoard.Commands; using IBBoard.IO; using IBBoard.Lang; using IBBoard.Windows.Forms; using IBBoard.Windows.Forms.I18N; using IBBoard.Xml; using IBBoard.WarFoundry.API; using IBBoard.WarFoundry.API.Commands; using IBBoard.WarFoundry.API.Exporters; using IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Savers; using IBBoard.WarFoundry.API.Factories; using IBBoard.WarFoundry.API.Factories.Xml; namespace IBBoard.WarFoundry.Forge.WinForms { /// /// Summary description for Form1. /// public class FrmMain : System.Windows.Forms.Form { private static readonly string AppTitle = "WarFoundry Forge"; internal static readonly string VERSION = "0.0.2"; const string DefaultDataDir = "data"; private Preferences preferences; private readonly ILog log = LogManager.GetLogger(typeof(FrmMain)); private CommandStack commandStack; private string loadedFilePath; // Form contents private System.ComponentModel.IContainer components; private System.Windows.Forms.MainMenu mainMenu; private MenuStrip menuStrip; private System.Windows.Forms.OpenFileDialog openFile; // File Menu and Contents private IBBToolStripMenuItem menuFile; private IBBToolStripMenuItem menuNew; private IBBToolStripMenuItem miNewSystem; private IBBToolStripMenuItem menuOpen; private IBBToolStripMenuItem miOpenSystem; private ToolStripSeparator toolStripSeparator2; private IBBToolStripMenuItem miExit; // About Menu and Contents private IBBToolStripMenuItem menuHelp; private IBBToolStripMenuItem miAbout; /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { try { XmlConfigurator.Configure(new System.IO.FileInfo("WarFoundryForge.exe.log4net")); LogManager.GetLogger(typeof(FrmMain)).Info("Starting WarFoundry Forge WinForms"); Application.EnableVisualStyles(); Application.Run(new FrmMain(args)); LogManager.GetLogger(typeof(FrmMain)).Info("Closing WarFoundry Forge WinForms"); } catch (Exception ex) { LogManager.GetLogger(typeof(FrmMain)).Fatal(ex); MessageBox.Show(null, "A major, unexpected and fatal error ocurred while starting the application: \r\n\r\n" + ex.Message + "\r\n\r\n" + ex.StackTrace, "Fatal error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public FrmMain(string[] args) { this.Closing += new CancelEventHandler(FrmMain_Closing); InitializeComponent(); Preferences = new Preferences("WarFoundryForge"); try { Translation.InitialiseTranslations(Constants.ExecutablePath, Preferences["language"].ToString()); } catch (TranslationLoadException ex) { log.Error("Translation loading failed for language " + Preferences["language"].ToString(), ex); MessageBox.Show(this, "Translation loading failed for language " + Preferences["language"].ToString(), "Translation failure", MessageBoxButtons.OK, MessageBoxIcon.Error); } Translation.TranslationChanged += new MethodInvoker(TranslationChanged); TranslateControls(); // hack to load default files WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + DefaultDataDir)); WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory()); WarFoundryLoader.GetDefault().FileLoadingFinished += FileLoadingFinished; WarFoundrySaver.SetFileSaver(new WarFoundryXmlSaver()); } private void TranslateControls() { ControlTranslator.TranslateControls(Controls); ControlTranslator.TranslateComponents(components.Components); } void TranslationChanged() { TranslateControls(); } public GameSystem CurrentGameSystem { get { return WarFoundryCore.CurrentGameSystem; } set { WarFoundryCore.CurrentGameSystem = value; } } public static string DataPath { get { return Constants.ExecutablePath + Constants.DirectoryChar + "data"; } } public Preferences Preferences { get { return preferences; } set { preferences = value; } } public CommandStack CommandStack { get { if (commandStack == null) { commandStack = new CommandStack(); } return commandStack; } } /// /// Clean up any resources being used. /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain)); this.mainMenu = new System.Windows.Forms.MainMenu(this.components); this.menuStrip = new System.Windows.Forms.MenuStrip(); this.openFile = new System.Windows.Forms.OpenFileDialog(); // File Menu and Contents this.menuFile = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); this.menuNew = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); this.miNewSystem = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); this.menuOpen = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); this.miOpenSystem = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); this.miExit = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); // About Menu and Contents this.menuHelp = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); this.miAbout = new IBBoard.Windows.Forms.IBBToolStripMenuItem(); // // menuStrip // this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.menuFile, this.menuHelp}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; this.menuStrip.Size = new System.Drawing.Size(790, 24); this.menuStrip.TabIndex = 6; this.menuStrip.Text = "menuStrip1"; // // menuFile // this.menuFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.menuNew, this.menuOpen, this.toolStripSeparator2, this.miExit}); this.menuFile.Name = "menuFile"; this.menuFile.Size = new System.Drawing.Size(33, 20); this.menuFile.Text = "&file"; // // menuNew // this.menuNew.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.miNewSystem}); this.menuNew.Name = "menuNew"; this.menuNew.Size = new System.Drawing.Size(164, 22); this.menuNew.Text = "&new"; // // miNewSystem // this.miNewSystem.Name = "miNewSystem"; this.miNewSystem.Size = new System.Drawing.Size(164, 22); this.miNewSystem.Text = "s&ystem"; this.miNewSystem.Click += new System.EventHandler(this.miNewSystem_Click); // // menuOpen // this.menuOpen.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.miOpenSystem}); this.menuOpen.Name = "menuOpen"; this.menuOpen.Size = new System.Drawing.Size(164, 22); this.menuOpen.Text = "&open"; // // miOpenSystem // this.miOpenSystem.Name = "miOpenSystem"; this.miOpenSystem.Size = new System.Drawing.Size(164, 22); this.miOpenSystem.Text = "s&ystem"; this.miOpenSystem.Click += new System.EventHandler(this.miOpenSystem_Click); // // toolStripSeparator2 // this.toolStripSeparator2.Name = "toolStripSeparator2"; this.toolStripSeparator2.Size = new System.Drawing.Size(161, 6); // // miExit // this.miExit.Name = "miExit"; this.miExit.Size = new System.Drawing.Size(164, 22); this.miExit.Text = "e&xit"; this.miExit.Click += new System.EventHandler(this.miExit_Click); // // menuHelp // this.menuHelp.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.miAbout}); this.menuHelp.Name = "menuHelp"; this.menuHelp.Size = new System.Drawing.Size(39, 20); this.menuHelp.Text = "&help"; // // miAbout // this.miAbout.Name = "miAbout"; this.miAbout.Size = new System.Drawing.Size(109, 22); this.miAbout.Text = "&about"; this.miAbout.Click += new System.EventHandler(this.miAbout_Click); // // FrmMain // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(790, 608); this.Controls.Add(this.menuStrip); this.IsMdiContainer = true; this.MainMenuStrip = this.menuStrip; this.Menu = this.mainMenu; this.Name = "FrmMain"; this.Text = AppTitle; this.menuStrip.ResumeLayout(false); this.menuStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private void FileLoadingFinished(List failures) { foreach (FileLoadFailure failure in failures) { log.Warn("Failed to load " + failure.FailedFile.FullName + ": " + failure.Message); } } private void miNewSystem_Click(object sender, System.EventArgs e) { FrmSystem system = new FrmSystem(new GameSystem(" ", " ", null)); system.ShowDialog(this); } private void miOpenSystem_Click(object sender, System.EventArgs e) { if(OpenFile()) { FrmSystem system = new FrmSystem(CurrentGameSystem); system.ShowDialog(this); } } private bool OpenFile() { if (openFile.Filter == "") { string savePath = DataPath; if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } openFile.InitialDirectory = savePath; openFile.Filter = Translation.GetTranslation("systemFileFilter") + "|*.system"; openFile.Title = Translation.GetTranslation("openSystemDialog"); } DialogResult dr = openFile.ShowDialog(this); if (dr == DialogResult.OK) { try { string newFilePath = openFile.FileName; ICollection fileContents = WarFoundryLoader.GetDefault().LoadFile(new FileInfo(newFilePath)); if(fileContents.Count > 0) { foreach(IWarFoundryObject fileObject in fileContents) { if(fileObject is GameSystem) { CurrentGameSystem = (GameSystem)fileObject; } } } log.DebugFormat("Game System set to: {0}", CurrentGameSystem.Name); loadedFilePath = newFilePath; return true; } catch (RequiredDataMissingException ex) { log.Error(ex); MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidArmyFileBoxTitle", "invalid army file"), MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } catch (InvalidFileException ex) { log.Error(ex); MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidArmyFileBoxTitle", "invalid army file"), MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } else { return false; } } private void miExit_Click(object sender, System.EventArgs e) { Application.Exit(); } private void miAbout_Click(object sender, EventArgs e) { FrmAbout about = new FrmAbout(); about.ShowDialog(this); } private void FrmMain_Closing(object sender, CancelEventArgs e) { } } }