diff FrmMain.cs @ 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 039b02006a8a
children 0e7c257ca8d6
line wrap: on
line diff
--- 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);
 			}
 		}