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 diff
     1.1 --- a/FrmArmyTree.cs	Sat Sep 19 13:57:35 2009 +0000
     1.2 +++ b/FrmArmyTree.cs	Sat Sep 19 16:01:43 2009 +0000
     1.3 @@ -27,7 +27,6 @@
     1.4  		private System.Windows.Forms.ContextMenu contextMenu;
     1.5  		private IBBMenuItem miDelete;
     1.6  		private IBBMenuItem miEdit;
     1.7 -		private Dictionary<string, FrmUnit> unitWindows;
     1.8  		
     1.9  		private ObjectAddDelegate UnitAddedMethod;
    1.10  		private ObjectRemoveDelegate UnitRemovedMethod;
    1.11 @@ -42,13 +41,12 @@
    1.12  		{
    1.13  			commandStack = cmdStack;
    1.14  			InitializeComponent();
    1.15 -			UnitAddedMethod = new ObjectAddDelegate(AddUnit);
    1.16 -			UnitRemovedMethod = new ObjectRemoveDelegate(RemoveUnit);
    1.17 +			UnitAddedMethod = new ObjectAddDelegate(UnitAdded);
    1.18 +			UnitRemovedMethod = new ObjectRemoveDelegate(UnitRemoved);
    1.19  			UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
    1.20  			ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
    1.21  			TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
    1.22  			nodes = new Dictionary<string, TreeNode>();
    1.23 -			unitWindows = new Dictionary<string, FrmUnit>();
    1.24  			this.Name = "ArmyTree";			
    1.25  			WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
    1.26  
    1.27 @@ -68,11 +66,6 @@
    1.28  			}
    1.29  		}
    1.30  
    1.31 -		public FrmArmyTree(Army army, CommandStack cmdStack) : this(cmdStack)
    1.32 -		{
    1.33 -			SetArmy(army);
    1.34 -		}
    1.35 -
    1.36  		/// <summary>
    1.37  		/// Clean up any resources being used.
    1.38  		/// </summary>
    1.39 @@ -197,14 +190,6 @@
    1.40  		{
    1.41  			nodes.Clear();
    1.42  			treeView.Nodes.Clear();
    1.43 -			FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows);
    1.44 -
    1.45 -			foreach (FrmUnit window in unitForms)
    1.46 -			{
    1.47 -				window.Close();
    1.48 -			}
    1.49 -
    1.50 -			unitWindows.Clear();
    1.51  		}
    1.52  
    1.53  		private void SetArmy(Army army)
    1.54 @@ -226,7 +211,7 @@
    1.55  
    1.56  					for (int j = 0; j < units.Length; j++)
    1.57  					{
    1.58 -						unitNodes[j] = createTreeNode(units[j]);
    1.59 +						unitNodes[j] = CreateTreeNode(units[j]);
    1.60  					}
    1.61  
    1.62  					temp = new TreeNode(cats[i].Name, unitNodes);
    1.63 @@ -242,7 +227,7 @@
    1.64  			}
    1.65  		}
    1.66  
    1.67 -		private TreeNode createTreeNode(Unit unit)
    1.68 +		private TreeNode CreateTreeNode(Unit unit)
    1.69  		{
    1.70  			TreeNode temp = new TreeNode(unit.Name);
    1.71  			temp.Tag = unit;
    1.72 @@ -251,15 +236,6 @@
    1.73  			return temp;
    1.74  		}
    1.75  
    1.76 -		/*private void FrmArmyTree_Move(object sender, System.EventArgs e)
    1.77 -		{
    1.78 -			if (ParentForm is FrmMain)
    1.79 -			{
    1.80 -				FrmMain main = (FrmMain)ParentForm;
    1.81 -				main.Invoke(new MethodInvoker(main.MdiChildMoved));
    1.82 -			}
    1.83 -		}*/
    1.84 -
    1.85  		public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
    1.86  		{
    1.87  			if (oldArmy != null)
    1.88 @@ -277,20 +253,26 @@
    1.89  			SetArmy(newArmy);
    1.90  		}
    1.91  
    1.92 -		private void AddUnit(WarFoundryObject obj)
    1.93 +		private void UnitAdded(WarFoundryObject obj)
    1.94  		{
    1.95  			if (obj is Unit)
    1.96  			{
    1.97  				Unit unit = (Unit)obj;
    1.98  				ArmyCategory cat = unit.Category;
    1.99  				TreeNode parent = nodes[cat.ID];
   1.100 -				TreeNode unitNode = createTreeNode(unit);
   1.101 +				TreeNode unitNode = CreateTreeNode(unit);
   1.102  				parent.Nodes.Add(unitNode);
   1.103  				parent.Expand(); //make sure it's expanded
   1.104  			}
   1.105  		}
   1.106  
   1.107 -		private void RemoveUnit(WarFoundryObject obj)
   1.108 +		public new FrmMain MdiParent
   1.109 +		{
   1.110 +			get { return (FrmMain) base.MdiParent; }
   1.111 +			set { base.MdiParent = value; }
   1.112 +		}
   1.113 +
   1.114 +		private void UnitRemoved(WarFoundryObject obj)
   1.115  		{
   1.116  			if (obj is Unit)
   1.117  			{
   1.118 @@ -384,20 +366,7 @@
   1.119  			if (tagData is Unit)
   1.120  			{
   1.121  				Unit unit = (Unit) tagData;
   1.122 -				string unitID = unit.ID;
   1.123 -
   1.124 -				if (unitWindows.ContainsKey(unitID))
   1.125 -				{
   1.126 -					unitWindows[unitID].Focus();
   1.127 -				}
   1.128 -				else
   1.129 -				{
   1.130 -					FrmUnit unitForm = new FrmUnit(unit, commandStack);
   1.131 -					unitWindows.Add(unitID, unitForm);
   1.132 -					unitForm.MdiParent = this.MdiParent;
   1.133 -					unitForm.Closing+=new CancelEventHandler(unitForm_Closing);
   1.134 -					unitForm.Show();
   1.135 -				}
   1.136 +				MdiParent.OpenUnitDialog(unit);
   1.137  			}
   1.138  		}
   1.139  
   1.140 @@ -429,15 +398,6 @@
   1.141  			}
   1.142  		}
   1.143  
   1.144 -		private void unitForm_Closing(object sender, CancelEventArgs e)
   1.145 -		{
   1.146 -			if (sender is FrmUnit)
   1.147 -			{
   1.148 -				FrmUnit unitForm = (FrmUnit)sender;
   1.149 -				unitWindows.Remove(unitForm.Unit.ID);
   1.150 -			}
   1.151 -		}
   1.152 -
   1.153  		private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
   1.154  		{
   1.155  			TreeNode node = nodes[obj.ID];
     2.1 --- a/FrmMain.cs	Sat Sep 19 13:57:35 2009 +0000
     2.2 +++ b/FrmMain.cs	Sat Sep 19 16:01:43 2009 +0000
     2.3 @@ -3,6 +3,7 @@
     2.4  // 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.
     2.5  
     2.6  using System;
     2.7 +using System.Collections.Generic;
     2.8  using System.Drawing;
     2.9  using System.Drawing.Drawing2D;
    2.10  using System.ComponentModel;
    2.11 @@ -50,6 +51,7 @@
    2.12  
    2.13  		private FrmArmyTree armyTree;
    2.14  		private string loadedFilePath;
    2.15 +		private Dictionary<string, FrmUnit> unitWindows;
    2.16  
    2.17  		private System.ComponentModel.IContainer components;
    2.18  		private System.Windows.Forms.ToolBar toolBar;
    2.19 @@ -128,6 +130,7 @@
    2.20  
    2.21  			ControlTranslator.TranslateComponent(openArmyDialog);
    2.22  			ControlTranslator.TranslateComponent(saveArmyDialog);
    2.23 +			unitWindows = new Dictionary<string, FrmUnit>();
    2.24  
    2.25  			WarFoundryCore.GameSystemChanged+= new GameSystemChangedDelegate(FrmMain_GameSystemChanged);
    2.26  			WarFoundryCore.ArmyChanged += new ArmyChangedDelegate(FrmMain_ArmyChanged);
    2.27 @@ -801,11 +804,20 @@
    2.28  
    2.29  			if (oldArmy != null)
    2.30  			{
    2.31 -				oldArmy.UnitAdded += UnitAddedMethod;
    2.32 -				oldArmy.UnitRemoved += UnitRemovedMethod;
    2.33 -				oldArmy.PointsValueChanged += PointsValueChangedMethod;
    2.34 +				oldArmy.UnitAdded -= UnitAddedMethod;
    2.35 +				oldArmy.UnitRemoved -= UnitRemovedMethod;
    2.36 +				oldArmy.PointsValueChanged -= PointsValueChangedMethod;
    2.37  			}
    2.38  
    2.39 +			FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows);
    2.40 +
    2.41 +			foreach (FrmUnit window in unitForms)
    2.42 +			{
    2.43 +				window.Close();
    2.44 +			}
    2.45 +
    2.46 +			unitWindows.Clear();
    2.47 +
    2.48  			if (CurrentArmy==null)
    2.49  			{
    2.50  				miSaveArmyAs.Enabled = false;
    2.51 @@ -1021,6 +1033,34 @@
    2.52  			{
    2.53  				CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, CurrentArmy.GetCategory(cat));
    2.54  				commandStack.Execute(cmd);
    2.55 +				OpenUnitDialog(cmd.Unit);
    2.56 +			}
    2.57 +		}
    2.58 +
    2.59 +		internal void OpenUnitDialog(Unit unit)
    2.60 +		{
    2.61 +			string unitID = unit.ID;
    2.62 +
    2.63 +			if (unitWindows.ContainsKey(unitID))
    2.64 +			{
    2.65 +				unitWindows[unitID].Focus();
    2.66 +			}
    2.67 +			else
    2.68 +			{
    2.69 +				FrmUnit unitForm = new FrmUnit(unit, commandStack);
    2.70 +				unitWindows.Add(unitID, unitForm);
    2.71 +				unitForm.MdiParent = this;
    2.72 +				unitForm.Closing += new CancelEventHandler(unitForm_Closing);
    2.73 +				unitForm.Show();
    2.74 +			}
    2.75 +		}
    2.76 +
    2.77 +		private void unitForm_Closing(object sender, CancelEventArgs e)
    2.78 +		{
    2.79 +			if (sender is FrmUnit)
    2.80 +			{
    2.81 +				FrmUnit unitForm = (FrmUnit) sender;
    2.82 +				unitWindows.Remove(unitForm.Unit.ID);
    2.83  			}
    2.84  		}
    2.85