Mercurial > repos > IBBoard.WarFoundry.GUI.WinForms
annotate FrmArmyTree.cs @ 22:84f61658e320
Re #88: WinForms fixes
* Put back the menu item names removed in r130 so that translations will work again
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 27 Jun 2009 11:41:18 +0000 |
parents | 475d3cdb75f4 |
children | 7c366fe55635 |
rev | line source |
---|---|
17 | 1 // This file (FrmArmyTree.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
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; | |
14 using IBBoard.WarFoundry.API.Objects; | |
15 | |
16 namespace IBBoard.WarFoundry | |
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(); | |
142 | |
143 ArrayList list = new ArrayList(htUnitWindows.Values); | |
144 | |
145 foreach (FrmUnit window in list) | |
146 { | |
147 window.Close(); | |
148 } | |
149 | |
150 htUnitWindows.Clear(); | |
151 } | |
152 | |
153 private void SetArmy(Army army) | |
154 { | |
155 if (army!=null) | |
156 { | |
157 ArmyCategory[] cats = army.Categories; | |
158 TreeNode[] catNodes = new TreeNode[cats.Length]; | |
159 Unit[] units; | |
160 | |
161 for (int i = 0; i<cats.Length; i++) | |
162 { | |
163 units = army.GetUnits(cats[i]); | |
164 cats[i].NameChanged += TreeNameChangedMethod; | |
165 TreeNode[] unitNodes = new TreeNode[units.Length]; | |
166 TreeNode temp; | |
167 | |
168 for (int j = 0; j<units.Length; j++) | |
169 { | |
170 unitNodes[j] = createTreeNode(units[j]); | |
171 } | |
172 | |
173 temp = new TreeNode(cats[i].Name, unitNodes); | |
174 temp.Tag = cats[i]; | |
175 catNodes[i] = temp; | |
176 htNodes[cats[i].ID] = temp; | |
177 } | |
178 | |
179 TreeNode root = new TreeNode(army.Name, catNodes); | |
180 root.Tag = army; | |
181 treeView.Nodes.Add(root); | |
182 root.ExpandAll(); | |
183 } | |
184 } | |
185 | |
186 private TreeNode createTreeNode(Unit unit) | |
187 { | |
188 TreeNode temp = new TreeNode(unit.Name); | |
189 temp.Tag = unit; | |
190 unit.NameChanged+= UnitNameChangedMethod; | |
191 htNodes[unit.ID] = temp; | |
192 return temp; | |
193 } | |
194 | |
195 /*private void FrmArmyTree_Move(object sender, System.EventArgs e) | |
196 { | |
197 if (ParentForm is FrmMain) | |
198 { | |
199 FrmMain main = (FrmMain)ParentForm; | |
200 main.Invoke(new MethodInvoker(main.MdiChildMoved)); | |
201 } | |
0 | 202 }*/ |
203 | |
204 public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy) | |
17 | 205 { |
206 if (oldArmy != null) | |
207 { | |
208 oldArmy.UnitAdded -= UnitAddedMethod; | |
209 oldArmy.UnitRemoved -= UnitRemovedMethod; | |
210 } | |
211 | |
212 if (newArmy != null) | |
213 { | |
214 newArmy.UnitAdded += UnitAddedMethod; | |
215 newArmy.UnitRemoved += UnitRemovedMethod; | |
10
19bdbb80999c
Closes #47 - Remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents:
3
diff
changeset
|
216 SetArmy(newArmy); |
0 | 217 } |
218 else | |
219 { | |
220 ClearArmy(); | |
221 } | |
17 | 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 } |