changeset 61:a0ec03c24cd9

Fixes #171: Open unit form when unit is created * Move unit form opening code to parent window (i.e. FrmMain) * Add code to open form on first create (but not on re-add - e.g. undo create and redo - because we don't know if it was open before or not)
author IBBoard <dev@ibboard.co.uk>
date Sat, 19 Sep 2009 16:01:43 +0000
parents c283545d2d0b
children 0e7c257ca8d6
files FrmArmyTree.cs FrmMain.cs
diffstat 2 files changed, 57 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/FrmArmyTree.cs	Sat Sep 19 13:57:35 2009 +0000
+++ b/FrmArmyTree.cs	Sat Sep 19 16:01:43 2009 +0000
@@ -27,7 +27,6 @@
 		private System.Windows.Forms.ContextMenu contextMenu;
 		private IBBMenuItem miDelete;
 		private IBBMenuItem miEdit;
-		private Dictionary<string, FrmUnit> unitWindows;
 		
 		private ObjectAddDelegate UnitAddedMethod;
 		private ObjectRemoveDelegate UnitRemovedMethod;
@@ -42,13 +41,12 @@
 		{
 			commandStack = cmdStack;
 			InitializeComponent();
-			UnitAddedMethod = new ObjectAddDelegate(AddUnit);
-			UnitRemovedMethod = new ObjectRemoveDelegate(RemoveUnit);
+			UnitAddedMethod = new ObjectAddDelegate(UnitAdded);
+			UnitRemovedMethod = new ObjectRemoveDelegate(UnitRemoved);
 			UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
 			ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
 			TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
 			nodes = new Dictionary<string, TreeNode>();
-			unitWindows = new Dictionary<string, FrmUnit>();
 			this.Name = "ArmyTree";			
 			WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
 
@@ -68,11 +66,6 @@
 			}
 		}
 
-		public FrmArmyTree(Army army, CommandStack cmdStack) : this(cmdStack)
-		{
-			SetArmy(army);
-		}
-
 		/// <summary>
 		/// Clean up any resources being used.
 		/// </summary>
@@ -197,14 +190,6 @@
 		{
 			nodes.Clear();
 			treeView.Nodes.Clear();
-			FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows);
-
-			foreach (FrmUnit window in unitForms)
-			{
-				window.Close();
-			}
-
-			unitWindows.Clear();
 		}
 
 		private void SetArmy(Army army)
@@ -226,7 +211,7 @@
 
 					for (int j = 0; j < units.Length; j++)
 					{
-						unitNodes[j] = createTreeNode(units[j]);
+						unitNodes[j] = CreateTreeNode(units[j]);
 					}
 
 					temp = new TreeNode(cats[i].Name, unitNodes);
@@ -242,7 +227,7 @@
 			}
 		}
 
-		private TreeNode createTreeNode(Unit unit)
+		private TreeNode CreateTreeNode(Unit unit)
 		{
 			TreeNode temp = new TreeNode(unit.Name);
 			temp.Tag = unit;
@@ -251,15 +236,6 @@
 			return temp;
 		}
 
-		/*private void FrmArmyTree_Move(object sender, System.EventArgs e)
-		{
-			if (ParentForm is FrmMain)
-			{
-				FrmMain main = (FrmMain)ParentForm;
-				main.Invoke(new MethodInvoker(main.MdiChildMoved));
-			}
-		}*/
-
 		public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
 		{
 			if (oldArmy != null)
@@ -277,20 +253,26 @@
 			SetArmy(newArmy);
 		}
 
-		private void AddUnit(WarFoundryObject obj)
+		private void UnitAdded(WarFoundryObject obj)
 		{
 			if (obj is Unit)
 			{
 				Unit unit = (Unit)obj;
 				ArmyCategory cat = unit.Category;
 				TreeNode parent = nodes[cat.ID];
-				TreeNode unitNode = createTreeNode(unit);
+				TreeNode unitNode = CreateTreeNode(unit);
 				parent.Nodes.Add(unitNode);
 				parent.Expand(); //make sure it's expanded
 			}
 		}
 
-		private void RemoveUnit(WarFoundryObject obj)
+		public new FrmMain MdiParent
+		{
+			get { return (FrmMain) base.MdiParent; }
+			set { base.MdiParent = value; }
+		}
+
+		private void UnitRemoved(WarFoundryObject obj)
 		{
 			if (obj is Unit)
 			{
@@ -384,20 +366,7 @@
 			if (tagData is Unit)
 			{
 				Unit unit = (Unit) tagData;
-				string unitID = unit.ID;
-
-				if (unitWindows.ContainsKey(unitID))
-				{
-					unitWindows[unitID].Focus();
-				}
-				else
-				{
-					FrmUnit unitForm = new FrmUnit(unit, commandStack);
-					unitWindows.Add(unitID, unitForm);
-					unitForm.MdiParent = this.MdiParent;
-					unitForm.Closing+=new CancelEventHandler(unitForm_Closing);
-					unitForm.Show();
-				}
+				MdiParent.OpenUnitDialog(unit);
 			}
 		}
 
@@ -429,15 +398,6 @@
 			}
 		}
 
-		private void unitForm_Closing(object sender, CancelEventArgs e)
-		{
-			if (sender is FrmUnit)
-			{
-				FrmUnit unitForm = (FrmUnit)sender;
-				unitWindows.Remove(unitForm.Unit.ID);
-			}
-		}
-
 		private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
 		{
 			TreeNode node = nodes[obj.ID];
--- a/FrmMain.cs	Sat Sep 19 13:57:35 2009 +0000
+++ b/FrmMain.cs	Sat Sep 19 16:01:43 2009 +0000
@@ -3,6 +3,7 @@
 // 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;
@@ -50,6 +51,7 @@
 
 		private FrmArmyTree armyTree;
 		private string loadedFilePath;
+		private Dictionary<string, FrmUnit> unitWindows;
 
 		private System.ComponentModel.IContainer components;
 		private System.Windows.Forms.ToolBar toolBar;
@@ -128,6 +130,7 @@
 
 			ControlTranslator.TranslateComponent(openArmyDialog);
 			ControlTranslator.TranslateComponent(saveArmyDialog);
+			unitWindows = new Dictionary<string, FrmUnit>();
 
 			WarFoundryCore.GameSystemChanged+= new GameSystemChangedDelegate(FrmMain_GameSystemChanged);
 			WarFoundryCore.ArmyChanged += new ArmyChangedDelegate(FrmMain_ArmyChanged);
@@ -801,11 +804,20 @@
 
 			if (oldArmy != null)
 			{
-				oldArmy.UnitAdded += UnitAddedMethod;
-				oldArmy.UnitRemoved += UnitRemovedMethod;
-				oldArmy.PointsValueChanged += PointsValueChangedMethod;
+				oldArmy.UnitAdded -= UnitAddedMethod;
+				oldArmy.UnitRemoved -= UnitRemovedMethod;
+				oldArmy.PointsValueChanged -= PointsValueChangedMethod;
 			}
 
+			FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows);
+
+			foreach (FrmUnit window in unitForms)
+			{
+				window.Close();
+			}
+
+			unitWindows.Clear();
+
 			if (CurrentArmy==null)
 			{
 				miSaveArmyAs.Enabled = false;
@@ -1021,6 +1033,34 @@
 			{
 				CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, CurrentArmy.GetCategory(cat));
 				commandStack.Execute(cmd);
+				OpenUnitDialog(cmd.Unit);
+			}
+		}
+
+		internal void OpenUnitDialog(Unit unit)
+		{
+			string unitID = unit.ID;
+
+			if (unitWindows.ContainsKey(unitID))
+			{
+				unitWindows[unitID].Focus();
+			}
+			else
+			{
+				FrmUnit unitForm = new FrmUnit(unit, commandStack);
+				unitWindows.Add(unitID, unitForm);
+				unitForm.MdiParent = this;
+				unitForm.Closing += new CancelEventHandler(unitForm_Closing);
+				unitForm.Show();
+			}
+		}
+
+		private void unitForm_Closing(object sender, CancelEventArgs e)
+		{
+			if (sender is FrmUnit)
+			{
+				FrmUnit unitForm = (FrmUnit) sender;
+				unitWindows.Remove(unitForm.Unit.ID);
 			}
 		}