# HG changeset patch # User IBBoard # Date 1251834453 0 # Node ID 25dfeb1db285eee022f315121b100f6b96257e58 # Parent a9c1e60d0e4d30050813f6c8958eff7e00dc2362 Close #70: Add export to WinForms * Add export menu that is only available when an army exists, with a default "basic HTML" export diff -r a9c1e60d0e4d -r 25dfeb1db285 FrmMain.cs --- a/FrmMain.cs Mon Aug 24 21:21:58 2009 +0000 +++ b/FrmMain.cs Tue Sep 01 19:47:33 2009 +0000 @@ -22,6 +22,7 @@ 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; @@ -89,6 +90,8 @@ private System.Windows.Forms.ContextMenu redoMenu; private IBBoard.Windows.Forms.ColorableStatusBar statusBar; private System.Windows.Forms.Timer statusBarTimer; + private MenuItem miExportArmyAs; + private MenuItem miExportArmyAsBasicHTML; private System.Windows.Forms.Panel pnlRight; public FrmMain(string[] args) @@ -151,48 +154,6 @@ WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + DefaultDataDir)); WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory()); WarFoundrySaver.SetFileSaver(new WarFoundryXmlSaver()); - - /* - if (args.Length == 1) - { - logger.Debug("Attempting to load from file"); - FileInfo file = new FileInfo(args[0]); - - try - { - if (file.Extension.Equals("."+Factory.GetArmyFileExtension())) - { - WarFoundryCore.CurrentArmy = Factory.CreateArmyFromFile(file); - logger.InfoFormat("Loaded army from {0}", file.FullName); - } - else if (file.Extension.Equals("."+Factory.GetSystemFileExtension())) - { - WarFoundryCore.CurrentGameSystem = Factory.CreateGameSystemFromFile(file); - logger.InfoFormat("Loaded game system from {0}", file.FullName); - } - } - catch (InvalidFileException ex) - { - MessageBox.Show(Translation.GetTranslation("InvalidFileLoadError", "The file loaded ({0}) was not a valid WarFoundry file", file.FullName), Translation.GetTranslation("InvalidFileLoadTitle", "Invalid data file", null), MessageBoxButtons.OK, MessageBoxIcon.Error); - logger.Error(ex); - } - } - else - { - string gameSystemID = Preferences.GetStringProperty("currSystem"); - - if (gameSystemID!=null && !"".Equals(gameSystemID)) - { - logger.Debug("Attempting to load current game system from properties"); - GameSystem sys = Factory.GetGameSystem(gameSystemID); - - if (sys!=null) - { - WarFoundryCore.CurrentGameSystem = sys; - logger.InfoFormat("Loaded game system {0} from properties", gameSystemID); - } - } - }*/ } public static string DataPath @@ -268,6 +229,8 @@ this.miOpenArmy = new IBBoard.Windows.Forms.IBBMenuItem(); this.miSaveArmy = new IBBoard.Windows.Forms.IBBMenuItem(); this.miSaveArmyAs = new IBBoard.Windows.Forms.IBBMenuItem(); + this.miExportArmyAs = new System.Windows.Forms.MenuItem(); + this.miExportArmyAsBasicHTML = new System.Windows.Forms.MenuItem(); this.miCloseArmy = new IBBoard.Windows.Forms.IBBMenuItem(); this.miSep1 = new System.Windows.Forms.MenuItem(); this.miChangeSystem = new IBBoard.Windows.Forms.IBBMenuItem(); @@ -295,9 +258,9 @@ this.statusBar.Location = new System.Drawing.Point(0, 548); this.statusBar.Name = "statusBar"; this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { - this.sbMainPanel, - this.sbErrorPanel, - this.sbPointsPanel}); + this.sbMainPanel, + this.sbErrorPanel, + this.sbPointsPanel}); this.statusBar.ShowPanels = true; this.statusBar.Size = new System.Drawing.Size(792, 22); this.statusBar.TabIndex = 1; @@ -331,12 +294,12 @@ // this.toolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat; this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { - this.bttnNewArmy, - this.bttnOpenArmy, - this.bttnSaveArmy, - this.bttnSep1, - this.bttnUndo, - this.bttnRedo}); + this.bttnNewArmy, + this.bttnOpenArmy, + this.bttnSaveArmy, + this.bttnSep1, + this.bttnUndo, + this.bttnRedo}); this.toolBar.ButtonSize = new System.Drawing.Size(16, 16); this.toolBar.DropDownArrows = true; this.toolBar.ImageList = this.buttonIcons; @@ -400,27 +363,27 @@ // mainMenu // this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { - this.menuFile, - this.menuEdit, - this.menuHelp}); + this.menuFile, + this.menuEdit, + this.menuHelp}); // // menuFile // this.menuFile.Index = 0; this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { - this.miNewArmy, - this.miOpenArmy, - this.miSaveArmy, - this.miSaveArmyAs, - this.miCloseArmy, - this.miSep1, - this.miChangeSystem, - this.miSep2, - this.miReloadFiles, - this.miSep3, - this.miExit}); + this.miNewArmy, + this.miOpenArmy, + this.miSaveArmy, + this.miSaveArmyAs, + this.miExportArmyAs, + this.miCloseArmy, + this.miSep1, + this.miChangeSystem, + this.miSep2, + this.miReloadFiles, + this.miSep3, + this.miExit}); this.menuFile.Text = "&file"; - this.menuFile.Name = "menuFile"; // // miNewArmy // @@ -428,14 +391,12 @@ this.miNewArmy.Index = 0; this.miNewArmy.Text = "&new army"; this.miNewArmy.Click += new System.EventHandler(this.miNewArmy_Click); - this.miNewArmy.Name = "miNewArmy"; // // miOpenArmy // this.miOpenArmy.Index = 1; this.miOpenArmy.Text = "&open army"; this.miOpenArmy.Click += new System.EventHandler(this.miOpenArmy_Click); - this.miOpenArmy.Name = "miOpenArmy"; // // miSaveArmy // @@ -443,7 +404,6 @@ this.miSaveArmy.Index = 2; this.miSaveArmy.Text = "&save army"; this.miSaveArmy.Click += new System.EventHandler(this.miSaveArmy_Click); - this.miSaveArmy.Name = "miSaveArmy"; // // miSaveArmyAs // @@ -451,60 +411,68 @@ this.miSaveArmyAs.Index = 3; this.miSaveArmyAs.Text = "save army &as..."; this.miSaveArmyAs.Click += new System.EventHandler(this.miSaveArmyAs_Click); - this.miSaveArmyAs.Name = "miSaveArmyAs"; + // + // miExportArmyAs + // + this.miExportArmyAs.Enabled = false; + this.miExportArmyAs.Index = 4; + this.miExportArmyAs.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { + this.miExportArmyAsBasicHTML}); + this.miExportArmyAs.Text = "export army as..."; + // + // miExportArmyAsBasicHTML + // + this.miExportArmyAsBasicHTML.Index = 0; + this.miExportArmyAsBasicHTML.Text = "Basic HTML"; + this.miExportArmyAsBasicHTML.Click += new System.EventHandler(this.miExportArmyAsBasicHTML_Click); // // miCloseArmy // this.miCloseArmy.Enabled = false; - this.miCloseArmy.Index = 4; + this.miCloseArmy.Index = 5; this.miCloseArmy.Text = "&close army"; this.miCloseArmy.Click += new System.EventHandler(this.miCloseArmy_Click); - this.miCloseArmy.Name = "miCloseArmy"; // // miSep1 // - this.miSep1.Index = 5; + this.miSep1.Index = 6; this.miSep1.Text = "-"; // // miChangeSystem // - this.miChangeSystem.Index = 6; + this.miChangeSystem.Index = 7; this.miChangeSystem.Text = "change &game system"; this.miChangeSystem.Click += new System.EventHandler(this.miChangeSystem_Click); - this.miChangeSystem.Name = "miChangeSystem"; // // miSep2 // - this.miSep2.Index = 7; + this.miSep2.Index = 8; this.miSep2.Text = "-"; // // miReloadFiles // - this.miReloadFiles.Index = 8; + this.miReloadFiles.Index = 9; this.miReloadFiles.Text = "&reload files"; this.miReloadFiles.Click += new System.EventHandler(this.miReloadFiles_Click); - this.miReloadFiles.Name = "miReloadFiles"; // // miSep3 // - this.miSep3.Index = 9; + this.miSep3.Index = 10; this.miSep3.Text = "-"; // // miExit // - this.miExit.Index = 10; + this.miExit.Index = 11; this.miExit.Text = "e&xit"; this.miExit.Click += new System.EventHandler(this.miExit_Click); - this.miExit.Name = "miExit"; // // menuEdit // this.menuEdit.Index = 1; this.menuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { - this.miUndo, - this.miRedo}); + this.miUndo, + this.miRedo}); this.menuEdit.Text = "&edit"; - this.menuEdit.Name = "menuEdit"; // // miUndo // @@ -512,7 +480,6 @@ this.miUndo.Index = 0; this.miUndo.Text = "&undo"; this.miUndo.Click += new System.EventHandler(this.miUndo_Click); - this.miUndo.Name = "miUndo"; // // miRedo // @@ -520,30 +487,26 @@ this.miRedo.Index = 1; this.miRedo.Text = "&redo"; this.miRedo.Click += new System.EventHandler(this.miRedo_Click); - this.miRedo.Name = "miRedo"; // // menuHelp // this.menuHelp.Index = 2; this.menuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { - this.miAbout, - this.miDebugWindow}); + this.miAbout, + this.miDebugWindow}); this.menuHelp.Text = "&help"; - this.menuHelp.Name = "menuHelp"; // // miAbout // this.miAbout.Enabled = false; this.miAbout.Index = 0; this.miAbout.Text = "&about"; - this.miAbout.Name = "miAbout"; // // miDebugWindow // this.miDebugWindow.Index = 1; this.miDebugWindow.Text = "&debug"; this.miDebugWindow.Click += new System.EventHandler(this.miDebugWindow_Click); - this.miDebugWindow.Name = "miDebugWindow"; // // saveArmyDialog // @@ -612,12 +575,12 @@ private void miNewArmy_Click(object sender, System.EventArgs e) { - createNewArmy(); + CreateNewArmy(); } - private void createNewArmy() + private void CreateNewArmy() { - if (closeCurrentArmy()) + if (CloseCurrentArmy()) { FrmNewArmy newArmy = new FrmNewArmy(CurrentGameSystem); DialogResult dr = newArmy.ShowDialog(); @@ -629,9 +592,9 @@ } } - private bool openArmy() + private bool OpenArmy() { - if (closeCurrentArmy()) + if (CloseCurrentArmy()) { if (openArmyDialog.Filter=="") { @@ -677,7 +640,7 @@ } } - private bool closeCurrentArmy() + private bool CloseCurrentArmy() { if (CurrentArmy!=null) { @@ -705,7 +668,7 @@ if (canClose) { //do close - disableCategoryButtons(); + DisableCategoryButtons(); CurrentArmy = null; return true; } @@ -716,13 +679,13 @@ } else { - disableCategoryButtons(); + DisableCategoryButtons(); //pretend we succeeded return true; } } - private void undoLastAction() + private void UndoLastAction() { if (commandStack.CanUndo()) { @@ -730,7 +693,7 @@ } } - private void redoAction() + private void RedoAction() { if (commandStack.CanRedo()) { @@ -746,7 +709,7 @@ if (filePath == null) { - filePath = PromptForFilePath(); + filePath = PromptForArmyFilePath(); } if (filePath != null) @@ -760,7 +723,7 @@ private bool SaveCurrentArmyAs() { bool saved = false; - string filePath = PromptForFilePath(); + string filePath = PromptForArmyFilePath(); if (filePath != null) { @@ -787,7 +750,7 @@ } } - private string PromptForFilePath() + private string PromptForArmyFilePath() { if (saveArmyDialog.Filter == "") { @@ -819,19 +782,19 @@ { if (e.Button == bttnUndo) { - undoLastAction(); + UndoLastAction(); } else if (e.Button == bttnRedo) { - redoAction(); + RedoAction(); } else if (e.Button == bttnNewArmy) { - createNewArmy(); + CreateNewArmy(); } else if (e.Button == bttnOpenArmy) { - openArmy(); + OpenArmy(); } else if (e.Button == bttnSaveArmy) { @@ -840,7 +803,7 @@ else { //it must be one of our extra buttons for the categories - addUnitFromCategory((Category)e.Button.Tag); + AddUnitFromCategory((Category)e.Button.Tag); } } @@ -863,7 +826,7 @@ if (dr==DialogResult.OK) { - closeCurrentArmy(); + CloseCurrentArmy(); CurrentGameSystem = selectSystem.GameSystem; } } @@ -877,14 +840,14 @@ { miNewArmy.Enabled = newSystem != null; bttnNewArmy.Enabled = newSystem != null; - setAppTitle(); - removeCategoryButtons(); - addCategoryButtons(); + SetAppTitle(); + RemoveCategoryButtons(); + AddCategoryButtons(); } private void FrmMain_ArmyChanged(Army oldArmy, Army newArmy) { - setAppTitle(); + SetAppTitle(); if (oldArmy != null) { @@ -897,7 +860,8 @@ { miSaveArmyAs.Enabled = false; miCloseArmy.Enabled = false; - disableCategoryButtons(); + miExportArmyAs.Enabled = false; + DisableCategoryButtons(); } else { @@ -907,12 +871,13 @@ //TODO: Clear all buttons miSaveArmyAs.Enabled = true; miCloseArmy.Enabled = true; - enableCategoryButtons(); + miExportArmyAs.Enabled = true; + EnableCategoryButtons(); if (newArmy.Race.HasCategoryOverrides()) { - removeCategoryButtons(); - addCategoryButtons(newArmy.Race.Categories); + RemoveCategoryButtons(); + AddCategoryButtons(newArmy.Race.Categories); } } @@ -921,18 +886,18 @@ miSaveArmy.Enabled = false; bttnSaveArmy.Enabled = false; - setPointsPanelText(); + SetPointsPanelText(); } - private void addCategoryButtons() + private void AddCategoryButtons() { if (CurrentGameSystem!=null) { - addCategoryButtons(CurrentGameSystem.Categories); + AddCategoryButtons(CurrentGameSystem.Categories); } } - private void addCategoryButtons(Category[] cats) + private void AddCategoryButtons(Category[] cats) { int catCount = cats.Length; Category cat; @@ -959,7 +924,7 @@ this.Invoke(new ToolBarButtonRangeDelegate(toolBar.Buttons.AddRange), new object[]{categoryButtons}); } - private void removeCategoryButtons() + private void RemoveCategoryButtons() { if (categoryButtons!=null) { @@ -970,17 +935,17 @@ } } - private void enableCategoryButtons() + private void EnableCategoryButtons() { - setCategoryButtonState(true); + SetCategoryButtonState(true); } - private void disableCategoryButtons() + private void DisableCategoryButtons() { - setCategoryButtonState(false); + SetCategoryButtonState(false); } - private void setCategoryButtonState(bool state) + private void SetCategoryButtonState(bool state) { if (categoryButtons!=null) { @@ -1078,7 +1043,7 @@ SaveCurrentArmy(); } - private void setAppTitle() + private void SetAppTitle() { string str = AppTitle; @@ -1095,7 +1060,7 @@ this.Text = str; } - private void addUnitFromCategory(Category cat) + private void AddUnitFromCategory(Category cat) { FrmNewUnit newUnit = new FrmNewUnit(CurrentArmy.Race, cat, CurrentArmy); DialogResult dr = newUnit.ShowDialog(this); @@ -1168,33 +1133,33 @@ private void miUndo_Click(object sender, System.EventArgs e) { - undoLastAction(); + UndoLastAction(); } private void miRedo_Click(object sender, System.EventArgs e) { - redoAction(); + RedoAction(); } private void miCloseArmy_Click(object sender, EventArgs e) { - closeCurrentArmy(); + CloseCurrentArmy(); } private void miOpenArmy_Click(object sender, EventArgs e) { - openArmy(); + OpenArmy(); } private void FrmMain_PointsValueChangedMethod(WarFoundryObject obj, double oldVal, double newVal) { if (obj is Army) { - setPointsPanelText(); + SetPointsPanelText(); } } - private void setPointsPanelText() + private void SetPointsPanelText() { if (CurrentArmy==null) { @@ -1271,7 +1236,7 @@ private void FrmMain_Closing(object sender, CancelEventArgs e) { - if (!closeCurrentArmy()) + if (!CloseCurrentArmy()) { e.Cancel = true; } @@ -1297,5 +1262,22 @@ MessageBox.Show(this, sbErrorPanel.TagString, Translation.GetTranslation("FailedRequirementMessage"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } } + + private void miExportArmyAsBasicHTML_Click(object sender, EventArgs e) + { + SaveFileDialog dialog = new SaveFileDialog(); + dialog.Filter = Translation.GetTranslation("armyExportBasicHtmlFilter", "HTML pages") + "|*.html"; + dialog.Title = Translation.GetTranslation("exportArmyDialog", "Export army"); + + DialogResult dr = dialog.ShowDialog(this); + + if (dr == DialogResult.OK) + { + Army army = WarFoundryCore.CurrentArmy; + string filePath = dialog.FileName; + logger.DebugFormat("Exporting {0} to {1} as basic HTML", army.Name, filePath); + WarFoundryHtmlExporter.GetDefault().ExportArmy(army, filePath); + } + } } }