changeset 21:2cf1af4c9baf

Re #53: Add XML saver * Add initial stub behind "save as" that saves to a fixed location
author IBBoard <dev@ibboard.co.uk>
date Mon, 17 Aug 2009 19:10:37 +0000
parents 1e301a6234fd
children 40485c6e5cb0
files FrmMainWindow.cs
diffstat 1 files changed, 114 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/FrmMainWindow.cs	Sat Aug 15 10:50:07 2009 +0000
+++ b/FrmMainWindow.cs	Mon Aug 17 19:10:37 2009 +0000
@@ -27,7 +27,7 @@
 using log4net;
 
 namespace IBBoard.WarFoundry.GTK
-{	
+{
 	public partial class FrmMainWindow: Gtk.Window
 	{
 		private static readonly string AppTitle = "WarFoundry";
@@ -45,14 +45,14 @@
 		private DoubleValChangedDelegate PointsValueChangedMethod;
 		private FailedUnitRequirementDelegate FailedUnitRequirementMethod;
 		private StringValChangedDelegate UnitNameChangedMethod;
-		
+
 		private GameSystem system;
 		private string loadedArmyPath;
-		
+
 		private MenuToolButton undoMenuButton, redoMenuButton;
-		
+
 		public static void Main (string[] args)
-		{			
+		{
 			try
 			{
 				Application.Init();
@@ -66,12 +66,12 @@
 				LogManager.GetLogger(typeof(FrmMainWindow)).Fatal("("+ex.GetType().Name+") "+ex.Message + Environment.NewLine + ex.StackTrace);
 			}
 		}
-		
+
 		public FrmMainWindow() : this(new string[0])
 		{
 			//Do nothing extra
 		}
-			
+
 		public FrmMainWindow (string[] args): base (Gtk.WindowType.Toplevel)
 		{
 			logger.Info("Opening FrmMainWindow");
@@ -90,7 +90,7 @@
 			toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-1]);
 			toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-2]);
 			toolbar.ShowAll();
-			
+
 			Title = AppTitle;
 			TreeViewColumn mainColumn = new TreeViewColumn ();
 			mainColumn.Title = "Army Categories";
@@ -102,7 +102,7 @@
 			logger.Debug("Loading preferences");
 			Preferences = new Preferences("WarFoundryGTK");
 			logger.Debug("Loading translations");
-			
+
 			try
 			{
 				Translation.InitialiseTranslations(Constants.ExecutablePath, Preferences["language"].ToString());
@@ -115,7 +115,7 @@
 				dialog.Run();
 				dialog.Destroy();
 			}
-			
+
 			logger.Debug("Initialising");
 			commandStack = new CommandStack();
 			commandStack.CommandStackUpdated+=new MethodInvoker(commandStack_CommandStackUpdated);
@@ -128,19 +128,20 @@
 			PointsValueChangedMethod = new DoubleValChangedDelegate(OnPointsValueChanged);
 			FailedUnitRequirementMethod = new FailedUnitRequirementDelegate(OnFailedUnitRequirement);
 			UnitNameChangedMethod = new StringValChangedDelegate(OnUnitNameChanged);
-			
+
 			//FIXME: Temporary hack to add paths and factories before we get preferences and plugins
 			WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + "data"));
 			WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory());
 			WarFoundryLoader.GetDefault().RegisterNonNativeFactory(RollcallFactory.GetFactory());
-			
+			WarFoundrySaver.SetFileSaver(new WarFoundryXmlSaver());
+
 			logger.Debug("Initialising complete - seeing if we can load default army or system");
-								
+
 			if (args.Length == 1)
 			{
-				logger.Debug("Attempting to load from file");				
+				logger.Debug("Attempting to load from file");
 				FileInfo file = new FileInfo(args[0]);
-				
+
 				try
 				{
 					ICollection<IWarFoundryObject> objects = WarFoundryLoader.GetDefault().LoadFile(file);
@@ -150,7 +151,7 @@
 						List<IWarFoundryObject> objectList = new List<IWarFoundryObject>();
 						objectList.AddRange(objects);
 						IWarFoundryObject loadedObject = objectList[0];
-						
+
 						if (loadedObject is Army)
 						{
 							WarFoundryCore.CurrentArmy = (Army)loadedObject;
@@ -177,7 +178,7 @@
 				{
 					logger.Debug("Attempting to load current game system from properties");
 					GameSystem sys = WarFoundryLoader.GetDefault().GetGameSystem(gameSystemID);
-					
+
 					if (sys!=null)
 					{
 						WarFoundryCore.CurrentGameSystem = sys;
@@ -186,16 +187,16 @@
 				}
 			}
 		}
-		
+
 		private void RenderCategoryTreeObjectName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
 		{
 			object o = model.GetValue(iter, 0);
-			
+
 			if (o is ArmyCategory)
 			{
 				ArmyCategory c = (ArmyCategory)o;
 				string name = "";
-				
+
 				if (Preferences.GetBooleanProperty("ShowCatPercentage"))
 				{
 					name = Translation.GetTranslation("categoryTreeCatName", "{0} - {1}pts", c.Name, c.PointsTotal);
@@ -204,7 +205,7 @@
 				{
 					name = Translation.GetTranslation("categoryTreeCatNamePercentage", "{0} - {1}pts ({2}%)", c.Name, c.PointsTotal, (c.ParentArmy.PointsTotal > 0 ? Math.Round((c.PointsTotal / c.ParentArmy.PointsTotal) * 100) : 0));
 				}
-				
+
 				(cell as CellRendererText).Text = name;
 			}
 			else if (o is IBBoard.WarFoundry.API.Objects.Unit)
@@ -214,46 +215,46 @@
 				(cell as CellRendererText).Text = name;
 			}
 		}
-		
+
 		private void OnWindowDestroyed(object source, EventArgs args)
 		{
 			logger.Info("Exiting");
 			Application.Quit();
 		}
-		
+
 		private void OnUnitNameChanged(WarFoundryObject val, string oldValue, string newValue)
 		{
 			IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val;
 			UnitDisplayWidget widget;
 			unitToWidgetMap.TryGetValue(unit, out widget);
-			
+
 			if (widget!=null)
 			{
 				unitsNotebook.SetTabLabelText(widget, newValue);
 			}
 		}
-		
+
 		private void OnUnitAdded(WarFoundryObject val)
 		{
 			IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val;
 			unit.NameChanged+= UnitNameChangedMethod;
 			AddUnitToTree(unit);
 		}
-		
+
 		private void AddUnitToTree(IBBoard.WarFoundry.API.Objects.Unit unit)
 		{
 			TreeStore model = (TreeStore)treeUnits.Model;
 			TreeIter iter;
 			model.GetIterFirst(out iter);
-			
+
 			do
 			{
 				object obj = model.GetValue(iter, 0);
-				
+
 				if (obj is ArmyCategory)
 				{
 					ArmyCategory cat = (ArmyCategory)obj;
-					
+
 					if (cat.Equals(unit.Category))
 					{
 						model.AppendValues(iter, unit);
@@ -264,40 +265,40 @@
 			}
 			while (model.IterNext(ref iter));
 		}
-		
+
 		private void OnUnitRemoved(WarFoundryObject obj)
 		{
 			IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj;
 			unit.NameChanged-= UnitNameChangedMethod;
 			RemoveUnitFromTree(unit);
-			
+
 			//See if unit has a tab open and close it if it does
 		}
-		
+
 		private void RemoveUnitFromTree(IBBoard.WarFoundry.API.Objects.Unit unit)
 		{
 			TreeStore model = (TreeStore)treeUnits.Model;
 			TreeIter iter;
 			model.GetIterFirst(out iter);
 			bool removed = false;
-			
+
 			do
 			{
 				object obj = model.GetValue(iter, 0);
-				
+
 				if (obj is ArmyCategory)
 				{
 					ArmyCategory cat = (ArmyCategory)obj;
-					
+
 					if (unit.Category == null || cat.Equals(unit.Category))
 					{
 						TreeIter innerIter;
 						model.IterChildren(out innerIter, iter);
-						
+
 						do
 						{
 							object innerObj = model.GetValue(innerIter, 0);
-							
+
 							if (unit.Equals(innerObj))
 							{
 								model.Remove(ref innerIter);
@@ -306,7 +307,7 @@
 							}
 						}
 						while (model.IterNext(ref innerIter));
-						
+
 						if (removed)
 						{
 							break;
@@ -316,28 +317,28 @@
 			}
 			while (model.IterNext(ref iter));
 		}
-		
+
 		private void OnPointsValueChanged(WarFoundryObject obj, double before, double after)
 		{
 			//Set points in panel
 		}
-		
+
 		private void OnFailedUnitRequirement(List<FailedUnitRequirement> failedRequirement)
 		{
 			//Show error message in panel
 		}
-		
+
 		public Preferences Preferences
 		{
 			get { return preferences; }
-			set { preferences = value; }		
+			set { preferences = value; }
 		}
-		
+
 		/*public AbstractNativeWarFoundryFactory Factory
 		{
 			get { return WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(Constants.ExecutablePath, factoryType); }
 		}*/
-		
+
 		protected void OnDeleteEvent (object sender, DeleteEventArgs a)
 		{
 			Application.Quit ();
@@ -382,37 +383,37 @@
 		{
 			SaveCurrentArmy();
 		}
-		
+
 		protected virtual void OnAddUnitActivated(object sender, System.EventArgs e)
 		{
 			if (sender is ToolButton)
 			{
 				Category cat = null;
 				categoryMap.TryGetValue((ToolButton)sender, out cat);
-				
+
 				if (cat!=null)
 				{
 					logger.DebugFormat("Show FrmNewUnit for {0}", cat.Name);
 					FrmNewUnit newUnit = new FrmNewUnit(WarFoundryCore.CurrentArmy.Race, cat, WarFoundryCore.CurrentArmy);
 					ResponseType response = (ResponseType)newUnit.Run();
 					newUnit.Hide();
-					
+
 					if (response==ResponseType.Ok)
 					{
 						CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(cat));
 						commandStack.Execute(cmd);
 					}
-					
+
 					newUnit.Dispose();
 				}
 			}
 		}
-		
+
 		public CommandStack CommandStack
 		{
 			get { return commandStack; }
 		}
-		
+
 		private void SetAppTitle()
 		{
 			if (WarFoundryCore.CurrentArmy!=null)
@@ -428,7 +429,7 @@
 				Title = AppTitle;
 			}
 		}
-		
+
 		private void OnGameSystemChanged(GameSystem oldSys, GameSystem newSys)
 		{
 			system = newSys;
@@ -436,34 +437,34 @@
 			miCreateArmy.Sensitive = system!=null;
 			newArmyButton.Sensitive = system!=null;
 			RemoveCategoryButtons();
-			
+
 			if (system!=null)
 			{
 				AddCategoryButtons(system.Categories);
 			}
 		}
-		
+
 		private void OnArmyChanged(Army oldArmy, Army newArmy)
 		{
 			loadedArmyPath = null;
 			SetAppTitle();
 			SetArmyTree(newArmy);
-						
+
 			if (oldArmy!=null)
 			{
 				oldArmy.UnitAdded-= UnitAddedMethod;
 				oldArmy.UnitRemoved-= UnitRemovedMethod;
 				oldArmy.PointsValueChanged-= PointsValueChangedMethod;
-				oldArmy.FailedRequirement-=FailedUnitRequirementMethod;				
+				oldArmy.FailedRequirement-=FailedUnitRequirementMethod;
 			}
-			
+
 			unitToWidgetMap.Clear();
-			
+
 			while (unitsNotebook.NPages > 0)
 			{
 				unitsNotebook.RemovePage(0);
 			}
-			
+
 			if (newArmy==null)
 			{
 				DisableCategoryButtons();
@@ -483,7 +484,7 @@
 					AddCategoryButtons(newArmy.Race.Categories);
 				}
 			}
-			
+
 			miCloseArmy.Sensitive = newArmy!=null;
 			miSaveArmyAs.Sensitive = newArmy!=null;
 			//New army has no changes, so we can't save it
@@ -493,73 +494,73 @@
 			CommandStack.Reset();
 			SetPointsPanelText();
 		}
-		
+
 		private void SetArmyTree(Army army)
 		{
 			logger.Debug("Resetting tree");
 			TreeStore store = (TreeStore)treeUnits.Model;
 			store.Clear();
 			TreeIter iter;
-			
+
 			if (army!=null)
 			{
 				logger.Debug("Loading in categories to tree");
-								
+
 				foreach (ArmyCategory cat in army.Categories)
 				{
 					logger.DebugFormat("Append category {0}", cat.Name);
 					iter = store.AppendValues(cat);
-					
+
 					foreach (IBBoard.WarFoundry.API.Objects.Unit unit in cat.GetUnits())
 					{
 						store.AppendValues(iter, unit);
-					} 
+					}
 				}
-				
+
 				logger.Debug("Finished loading tree categories");
 			}
 		}
-		
+
 		private void DisableCategoryButtons()
 		{
 			SetCategoryButtonsSensitive(false);
 		}
-		
+
 		private void EnableCategoryButtons()
 		{
 			SetCategoryButtonsSensitive(true);
 		}
-		
+
 		private void SetCategoryButtonsSensitive(bool state)
 		{
 			int toolbarButtonCount = toolbar.Children.Length - 1;
 			logger.Debug("Last button index: "+toolbarButtonCount);
-			
+
 			for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--)
 			{
 				logger.DebugFormat("Setting button {0} state to {1}", i, state);
 				toolbar.Children[i].Sensitive = state;
 			}
 		}
-		
+
 		private void RemoveCategoryButtons()
 		{
 			int toolbarButtonCount = toolbar.Children.Length - 1;
-			
+
 			for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--)
 			{
 				toolbar.Remove(toolbar.Children[i]);
 			}
-			
+
 			categoryMap.Clear();
 		}
-		
+
 		private void AddCategoryButtons(Category[] cats)
 		{
 			if (cats!=null && cats.Length > 0)
 			{
 				logger.DebugFormat("Toolbar button count: {0}. Adding {1} categories.", toolbar.Children.Length, cats.Length);
-				
+
 				foreach (Category cat in cats)
 				{
 					ToolButton button = new ToolButton("gtk-add");
@@ -571,17 +572,17 @@
 					toolbar.Insert(button, -1);
 				}
 			}
-			
+
 			toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX].Visible = cats!=null && cats.Length>0;
-			
+
 			toolbar.ShowAll();
 		}
-		
+
 		private void SetPointsPanelText()
 		{
 			//TODO: Set the points value in the status bar
 		}
-		
+
 		private void commandStack_CommandStackUpdated()
 		{
 			undoMenuButton.Sensitive = commandStack.CanUndo();
@@ -592,7 +593,7 @@
 			//TODO: Build menus for undo/redo and find way of adding tooltips
 			/*int maxRedo = Math.Min(10, redoLength);
 			MenuItem[] menuItems = null;
-		
+
 			if (redoLength > 0)
 			{
 				menuItems = new MenuItem[maxRedo];
@@ -605,7 +606,7 @@
 
 					if (com == null)
 					{
-						break; 
+						break;
 					}
 
 					mi = new MenuItem(com.Description);
@@ -630,7 +631,7 @@
 			int undoLength = commandStack.UndoLength;
 			/*int maxUndo = Math.Min(10, undoLength);
 			MenuItem[] menuItemsUndo = null;
-			
+
 			if (undoLength > 0)
 			{
 				menuItemsUndo = new MenuItem[maxUndo];
@@ -643,7 +644,7 @@
 
 					if (com == null)
 					{
-						break; 
+						break;
 					}
 
 					mi = new MenuItem(com.UndoDescription);
@@ -658,17 +659,17 @@
 			{
 				bttnUndo.ToolTipText = menuItemsUndo[0].Text;
 				undoMenu.MenuItems.AddRange(menuItemsUndo);
-			}*/			
+			}*/
 			//TODO: Put above code back when we have a dropdown version of the undo button
 			if (undoLength > 0)
 			{
 				//undoMenuButton.Tooltip = CommandStack.PeekUndoCommand().UndoDescription;
 			}
-			
+
 			saveArmyButton.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null && CanSave();
 			miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null && CanSave();
-		}	
-		
+		}
+
 		private bool CanSave()
 		{
 			return loadedArmyPath!=null && WarFoundryCore.CurrentArmy!=null && WarFoundrySaver.GetSaver()!=null;
@@ -680,12 +681,12 @@
 			{
 				return SaveCurrentArmy();
 			}
-			else 
+			else
 			{
 				return SaveCurrentArmyAs();
 			}
 		}
-		
+
 		private bool OpenArmy()
 		{
 			//TODO: Open dialog for file selection then open army
@@ -697,7 +698,7 @@
 		private bool SaveCurrentArmy()
 		{
 			bool success = false;
-			
+
 			if (CanSave())
 			{
 				try
@@ -717,51 +718,30 @@
 					md.Show();
 				}
 			}
-			
+
 			return success;
 		}
 
 		private bool SaveCurrentArmyAs()
 		{
-			/*if (saveArmyDialog.Filter == "")
-			{
-				string savePath = UserDataPath+Constants.DirectoryString+"armies"+Constants.DirectoryString;
-				
-				if (!Directory.Exists(savePath))
-				{
-					Directory.CreateDirectory(savePath);
-				}
-
-				saveArmyDialog.InitialDirectory = savePath;
-				saveArmyDialog.Filter = Translation.GetTranslation("armyFileFilter")+"|*.army";
-				saveArmyDialog.Title = Translation.GetTranslation("saveArmyDialog");
-			}
-
-			DialogResult dr = saveArmyDialog.ShowDialog(this);
+			bool success = false;
 
-			if (dr == DialogResult.OK)
+			if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, Constants.UserDataPath+Constants.DirectoryString+"test.army"))
 			{
-				if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, saveArmyDialog.FileName))
-				{
-					miSaveArmy.Enabled = false;
-					bttnSaveArmy.Enabled = false;
-					CommandStack.setCleanMark();
-					loadedArmyPath = saveArmyDialog.FileName;
-					return true;
-				}
-				else
-				{
-					MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error);
-					return false;
-				}
+				miSaveArmy.Sensitive = false;
+				//bttnSaveArmy.Sensitive = false;
+				CommandStack.setCleanMark();
+				loadedArmyPath = Constants.UserDataPath+Constants.DirectoryString+"test.army";
+				success = true;
 			}
 			else
 			{
-				return false;
-			}*/
-			return false;
+				//MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error);
+			}
+
+			return success;
 		}
-		
+
 		private bool CloseCurrentArmy()
 		{
 			if (WarFoundryCore.CurrentArmy!=null)
@@ -770,7 +750,7 @@
 
 				if (CommandStack.IsDirty())
 				{
-					MessageDialog dia  = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.YesNo | ButtonsType.Cancel, "The army \""+WarFoundryCore.CurrentArmy.Name+"\" has been modified.\r\nSave changes before closing army?");   
+					MessageDialog dia  = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.YesNo | ButtonsType.Cancel, "The army \""+WarFoundryCore.CurrentArmy.Name+"\" has been modified.\r\nSave changes before closing army?");
 					ResponseType dr = (ResponseType)dia.Run();
 
 					if (dr == ResponseType.Yes)
@@ -789,7 +769,7 @@
 						//Assume cancel or close with the X button
 						canClose = false;
 					}
-					
+
 					dia.Dispose();
 				}
 				else
@@ -822,7 +802,7 @@
 			FrmNewArmy newArmy = new FrmNewArmy(WarFoundryCore.CurrentGameSystem);
 			ResponseType type = (ResponseType)newArmy.Run();
 			newArmy.Hide();
-		
+
 			if (type == ResponseType.Ok)
 			{
 				if (CloseCurrentArmy())
@@ -834,17 +814,17 @@
 			{
 				logger.Debug("Create new army cancelled");
 			}
-			
+
 			newArmy.Destroy();
 		}
-		
+
 		private void ChangeCurrentGameSystem()
-		{				
+		{
 			logger.Debug("Changing game system");
 			FrmChangeGameSystem dialog = new FrmChangeGameSystem(this);
 			ResponseType type = (ResponseType)dialog.Run();
 			dialog.Hide();
-			
+
 			if (type == ResponseType.Ok)
 			{
 				WarFoundryCore.CurrentGameSystem = dialog.SelectedSystem;
@@ -853,7 +833,7 @@
 			{
 				logger.Debug("Game system change cancelled");
 			}
-			
+
 			dialog.Destroy();
 		}
 
@@ -888,14 +868,14 @@
 			TreeIter iter;
 			model.GetIter(out iter, args.Path);
 			object obj = model.GetValue(iter, 0);
-			
+
 			if (obj is IBBoard.WarFoundry.API.Objects.Unit)
 			{
 				IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj;
-				
+
 				UnitDisplayWidget widget;
 				unitToWidgetMap.TryGetValue(unit, out widget);
-				
+
 				if (widget!=null)
 				{
 					logger.DebugFormat("Selecting existing page for "+unit.Name);