Mercurial > repos > IBBoard.WarFoundry.GUI.WinForms
annotate FrmArmyTree.cs @ 44:1486ccd744dc
Fixes #135: Closing army crashes WinForms
* Simple solution: Cache the open windows as an array then close them from the array to avoid iterating over an collection that gets modified!
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 09 Sep 2009 19:37:14 +0000 |
parents | 740350673006 |
children | 1c74b51abac1 |
rev | line source |
---|---|
33 | 1 // This file (FrmArmyTree.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2009 IBBoard. |
2 // | |
36 | 3 // 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. |
33 | 4 |
5 using System; | |
6 using System.Drawing; | |
7 using System.Collections; | |
8 using System.ComponentModel; | |
9 using System.Windows.Forms; | |
10 using IBBoard.Commands; | |
11 using IBBoard.WarFoundry.API; | |
12 using IBBoard.WarFoundry.API.Commands; | |
13 using IBBoard.Windows.Forms; | |
27
526fefefb16b
Fixes #91: Fix WinForms rendering of unit stats
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
14 using IBBoard.WarFoundry.API.Objects; |
526fefefb16b
Fixes #91: Fix WinForms rendering of unit stats
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
15 |
40
740350673006
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
16 namespace IBBoard.WarFoundry.GUI.WinForms |
33 | 17 { |
18 /// <summary> | |
19 /// Summary description for FrmArmyTree. | |
20 /// </summary> | |
21 public class FrmArmyTree : IBBoard.Windows.Forms.IBBForm | |
22 { | |
23 private System.Windows.Forms.TreeView treeView; | |
24 /// <summary> | |
25 /// Required designer variable. | |
26 /// </summary> | |
27 private System.ComponentModel.Container components = null; | |
28 private Hashtable htNodes; | |
29 private System.Windows.Forms.ContextMenu contextMenu; | |
30 private System.Windows.Forms.MenuItem miDelete; | |
31 private System.Windows.Forms.MenuItem miEdit; | |
32 private Hashtable htUnitWindows; | |
33 | |
34 private ObjectAddDelegate UnitAddedMethod; | |
35 private ObjectRemoveDelegate UnitRemovedMethod; | |
36 private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod; | |
37 private CommandStack commandStack; | |
38 | |
39 public FrmArmyTree(CommandStack cmdStack) | |
40 { | |
41 commandStack = cmdStack; | |
42 InitializeComponent(); | |
43 UnitAddedMethod = new ObjectAddDelegate(AddUnit); | |
44 UnitRemovedMethod = new ObjectRemoveDelegate(RemoveUnit); | |
45 UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName); | |
46 ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName); | |
47 TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged); | |
48 htNodes = new Hashtable(); | |
49 htUnitWindows = new Hashtable(); | |
50 this.Name = "ArmyTree"; | |
51 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged); | |
52 } | |
53 | |
54 public FrmArmyTree(Army army, CommandStack cmdStack) : this(cmdStack) | |
55 { | |
56 SetArmy(army); | |
57 } | |
58 | |
59 /// <summary> | |
60 /// Clean up any resources being used. | |
61 /// </summary> | |
62 protected override void Dispose( bool disposing ) | |
63 { | |
64 if( disposing ) | |
65 { | |
66 if(components != null) | |
67 { | |
68 components.Dispose(); | |
69 } | |
70 } | |
71 base.Dispose( disposing ); | |
72 } | |
73 | |
74 #region Windows Form Designer generated code | |
75 /// <summary> | |
76 /// Required method for Designer support - do not modify | |
77 /// the contents of this method with the code editor. | |
78 /// </summary> | |
79 private void InitializeComponent() | |
80 { | |
81 this.treeView = new System.Windows.Forms.TreeView(); | |
82 this.contextMenu = new System.Windows.Forms.ContextMenu(); | |
83 this.miEdit = new System.Windows.Forms.MenuItem(); | |
84 this.miDelete = new System.Windows.Forms.MenuItem(); | |
85 this.SuspendLayout(); | |
86 // | |
87 // treeView | |
88 // | |
89 this.treeView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | |
90 | System.Windows.Forms.AnchorStyles.Left) | |
91 | System.Windows.Forms.AnchorStyles.Right))); | |
92 this.treeView.ContextMenu = this.contextMenu; | |
93 this.treeView.FullRowSelect = true; | |
94 this.treeView.ImageIndex = -1; | |
95 this.treeView.Location = new System.Drawing.Point(0, 0); | |
96 this.treeView.Name = "treeView"; | |
97 this.treeView.SelectedImageIndex = -1; | |
98 this.treeView.Size = new System.Drawing.Size(240, 278); | |
99 this.treeView.TabIndex = 0; | |
100 this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown); | |
101 this.treeView.DoubleClick += new System.EventHandler(this.treeView_DoubleClick); | |
102 // | |
103 // contextMenu | |
104 // | |
105 this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { | |
106 this.miEdit, | |
107 this.miDelete}); | |
108 this.contextMenu.Popup += new System.EventHandler(this.contextMenu_Popup); | |
109 // | |
110 // miEdit | |
111 // | |
112 this.miEdit.Index = 0; | |
113 this.miEdit.Text = "&Edit unit"; | |
114 this.miEdit.Click += new System.EventHandler(this.miEdit_Click); | |
115 // | |
116 // miDelete | |
117 // | |
118 this.miDelete.Index = 1; | |
119 this.miDelete.Text = "&Delete unit"; | |
120 this.miDelete.Click += new System.EventHandler(this.miDelete_Click); | |
121 // | |
122 // FrmArmyTree | |
123 // | |
124 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); | |
125 this.ClientSize = new System.Drawing.Size(240, 277); | |
126 this.ControlBox = false; | |
127 this.Controls.Add(this.treeView); | |
128 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow; | |
129 this.MaximizeBox = false; | |
130 this.MinimizeBox = false; | |
131 this.Name = "FrmArmyTree"; | |
132 this.Text = "FrmArmyTree"; | |
133 this.ResumeLayout(false); | |
134 | |
135 } | |
136 #endregion | |
137 | |
138 private void ClearArmy() | |
139 { | |
140 htNodes.Clear(); | |
141 treeView.Nodes.Clear(); | |
44
1486ccd744dc
Fixes #135: Closing army crashes WinForms
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
142 ICollection unitFormsCollection = htUnitWindows.Values; |
1486ccd744dc
Fixes #135: Closing army crashes WinForms
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
143 FrmUnit[] unitForms = new FrmUnit[unitFormsCollection.Count]; |
1486ccd744dc
Fixes #135: Closing army crashes WinForms
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
144 unitFormsCollection.CopyTo(unitForms, 0); |
33 | 145 |
44
1486ccd744dc
Fixes #135: Closing army crashes WinForms
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
146 foreach (FrmUnit window in unitForms) |
33 | 147 { |
148 window.Close(); | |
149 } | |
150 | |
151 htUnitWindows.Clear(); | |
152 } | |
153 | |
154 private void SetArmy(Army army) | |
155 { | |
156 ClearArmy(); | |
157 | |
158 if (army != null) | |
159 { | |
160 ArmyCategory[] cats = army.Categories; | |
161 TreeNode[] catNodes = new TreeNode[cats.Length]; | |
162 Unit[] units; | |
163 | |
164 for (int i = 0; i < cats.Length; i++) | |
165 { | |
166 units = army.GetUnits(cats[i]); | |
167 cats[i].NameChanged += TreeNameChangedMethod; | |
168 TreeNode[] unitNodes = new TreeNode[units.Length]; | |
169 TreeNode temp; | |
170 | |
171 for (int j = 0; j < units.Length; j++) | |
172 { | |
173 unitNodes[j] = createTreeNode(units[j]); | |
174 } | |
175 | |
176 temp = new TreeNode(cats[i].Name, unitNodes); | |
177 temp.Tag = cats[i]; | |
178 catNodes[i] = temp; | |
179 htNodes[cats[i].ID] = temp; | |
180 } | |
181 | |
182 TreeNode root = new TreeNode(army.Name, catNodes); | |
183 root.Tag = army; | |
184 treeView.Nodes.Add(root); | |
185 root.ExpandAll(); | |
186 } | |
187 } | |
188 | |
189 private TreeNode createTreeNode(Unit unit) | |
190 { | |
191 TreeNode temp = new TreeNode(unit.Name); | |
192 temp.Tag = unit; | |
193 unit.NameChanged+= UnitNameChangedMethod; | |
194 htNodes[unit.ID] = temp; | |
195 return temp; | |
196 } | |
197 | |
198 /*private void FrmArmyTree_Move(object sender, System.EventArgs e) | |
199 { | |
200 if (ParentForm is FrmMain) | |
201 { | |
202 FrmMain main = (FrmMain)ParentForm; | |
203 main.Invoke(new MethodInvoker(main.MdiChildMoved)); | |
204 } | |
205 }*/ | |
206 | |
207 public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy) | |
208 { | |
209 if (oldArmy != null) | |
210 { | |
211 oldArmy.UnitAdded -= UnitAddedMethod; | |
212 oldArmy.UnitRemoved -= UnitRemovedMethod; | |
213 } | |
214 | |
215 if (newArmy != null) | |
216 { | |
217 newArmy.UnitAdded += UnitAddedMethod; | |
218 newArmy.UnitRemoved += UnitRemovedMethod; | |
219 } | |
220 | |
221 SetArmy(newArmy); | |
222 } | |
223 | |
224 private void AddUnit(object obj) | |
225 { | |
226 if (obj is Unit) | |
227 { | |
228 Unit unit = (Unit)obj; | |
229 ArmyCategory cat = unit.Category; | |
230 TreeNode parent = (TreeNode)htNodes[cat.ID]; | |
231 TreeNode unitNode = createTreeNode(unit); | |
232 parent.Nodes.Add(unitNode); | |
233 parent.Expand(); //make sure it's expanded | |
234 } | |
235 } | |
236 | |
237 private void RemoveUnit(object obj) | |
238 { | |
239 if (obj is Unit) | |
240 { | |
241 Unit unit = (Unit)obj; | |
242 removeUnit(unit); | |
243 } | |
244 } | |
245 | |
246 private void removeUnit(Unit unit) | |
247 { | |
248 TreeNode unitNode = (TreeNode)htNodes[unit.ID]; | |
249 unit.NameChanged-= UnitNameChangedMethod; | |
250 | |
251 if (unitNode!=null) | |
252 { | |
253 unitNode.Remove(); | |
254 htNodes.Remove(unit.ID); | |
255 } | |
256 } | |
257 | |
258 | |
259 private void contextMenu_Popup(object sender, System.EventArgs e) | |
260 { | |
261 TreeNode node = treeView.SelectedNode; | |
262 if (node!=null && node.Tag is Unit) | |
263 { | |
264 foreach(MenuItem item in contextMenu.MenuItems) | |
265 { | |
266 item.Visible = true; | |
267 } | |
268 } | |
269 else | |
270 { | |
271 foreach(MenuItem item in contextMenu.MenuItems) | |
272 { | |
273 item.Visible = false; | |
274 } | |
275 } | |
276 } | |
277 | |
278 private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) | |
279 { | |
280 if (e.Button == MouseButtons.Right) | |
281 { | |
282 TreeNode tn = treeView.GetNodeAt(e.X, e.Y); | |
283 | |
284 if (tn!=null) | |
285 { | |
286 treeView.SelectedNode = tn; | |
287 } | |
288 else | |
289 { | |
290 treeView.SelectedNode = null; | |
291 } | |
292 } | |
293 } | |
294 | |
295 private void miDelete_Click(object sender, System.EventArgs e) | |
296 { | |
297 TreeNode selected = treeView.SelectedNode; | |
298 | |
299 if (selected.Tag!=null && selected.Tag is Unit) | |
300 { | |
301 Unit unit = (Unit)selected.Tag; | |
302 commandStack.Execute(new RemoveUnitCommand(unit)); | |
303 } | |
304 } | |
305 | |
306 private void miEdit_Click(object sender, System.EventArgs e) | |
307 { | |
308 editUnit(); | |
309 } | |
310 | |
311 private void treeView_DoubleClick(object sender, System.EventArgs e) | |
312 { | |
313 editUnit(); | |
314 } | |
315 | |
316 private void editUnit() | |
317 { | |
318 TreeNode selected = treeView.SelectedNode; | |
319 | |
320 if (selected.Tag!=null && selected.Tag is Unit) | |
321 { | |
322 Unit unit = (Unit)selected.Tag; | |
323 | |
324 if (htUnitWindows.ContainsKey(unit.ID)) | |
325 { | |
326 ((FrmUnit)htUnitWindows[unit.ID]).Focus(); | |
327 } | |
328 else | |
329 { | |
330 FrmUnit unitForm = new FrmUnit(unit, commandStack); | |
331 htUnitWindows.Add(unit.ID, unitForm); | |
332 unitForm.MdiParent = this.MdiParent; | |
333 unitForm.Closing+=new CancelEventHandler(unitForm_Closing); | |
334 unitForm.Show(); | |
335 } | |
336 } | |
337 } | |
338 | |
339 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue) | |
340 { | |
341 if (obj is Unit) | |
342 { | |
343 Unit unit = (Unit)obj; | |
344 TreeNode node = (TreeNode)htNodes[unit.ID]; | |
345 | |
346 if (node!=null) | |
347 { | |
348 node.Text = unit.Name; | |
349 } | |
350 } | |
351 } | |
352 | |
353 private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue) | |
354 { | |
355 if (obj is Army) | |
356 { | |
357 Army army = (Army)obj; | |
358 TreeNode node = treeView.Nodes[0]; | |
359 | |
360 if (node!=null) | |
361 { | |
362 node.Text = army.Name; | |
363 } | |
364 } | |
365 } | |
366 | |
367 private void unitForm_Closing(object sender, CancelEventArgs e) | |
368 { | |
369 if (sender is FrmUnit) | |
370 { | |
371 FrmUnit unitForm = (FrmUnit)sender; | |
372 htUnitWindows.Remove(unitForm.Unit.ID); | |
373 } | |
374 } | |
375 | |
376 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
377 { | |
378 TreeNode node = (TreeNode)htNodes[obj.ID]; | |
379 | |
380 if (node!=null) | |
381 { | |
382 node.Text = obj.Name; | |
383 } | |
384 } | |
385 } | |
386 } |