Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.GTK
comparison FrmMainWindow.cs @ 45:5584860fb44b
Fixes #145: Add UI to remove units from army
* Remove tab on unit deletion
* Remove tab from widget map on close
Also:
* Alias IBBoard.WarFoundry.API.Objects to make access easier (Unit clashes with a GTK class)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 14 Aug 2010 15:43:30 +0000 |
parents | 44e6833bb41a |
children | 3314f6a46661 |
comparison
equal
deleted
inserted
replaced
44:44e6833bb41a | 45:5584860fb44b |
---|---|
17 using IBBoard.WarFoundry.API; | 17 using IBBoard.WarFoundry.API; |
18 using IBBoard.WarFoundry.API.Exporters; | 18 using IBBoard.WarFoundry.API.Exporters; |
19 using IBBoard.WarFoundry.API.Factories; | 19 using IBBoard.WarFoundry.API.Factories; |
20 using IBBoard.WarFoundry.API.Factories.Xml; | 20 using IBBoard.WarFoundry.API.Factories.Xml; |
21 using IBBoard.WarFoundry.API.Objects; | 21 using IBBoard.WarFoundry.API.Objects; |
22 using WFObjects = IBBoard.WarFoundry.API.Objects; | |
22 using IBBoard.WarFoundry.API.Commands; | 23 using IBBoard.WarFoundry.API.Commands; |
23 using IBBoard.WarFoundry.API.Savers; | 24 using IBBoard.WarFoundry.API.Savers; |
24 using IBBoard.WarFoundry.API.Requirements; | 25 using IBBoard.WarFoundry.API.Requirements; |
25 using IBBoard.WarFoundry.GTK.Widgets; | 26 using IBBoard.WarFoundry.GTK.Widgets; |
26 using IBBoard.WarFoundry.Plugin.Rollcall; | 27 using IBBoard.WarFoundry.Plugin.Rollcall; |
37 private Preferences preferences; | 38 private Preferences preferences; |
38 private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow)); | 39 private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow)); |
39 | 40 |
40 private CommandStack commandStack; | 41 private CommandStack commandStack; |
41 private Dictionary<ToolButton, Category> categoryMap = new Dictionary<ToolButton, Category>(); | 42 private Dictionary<ToolButton, Category> categoryMap = new Dictionary<ToolButton, Category>(); |
42 private Dictionary<IBBoard.WarFoundry.API.Objects.Unit, UnitDisplayWidget> unitToWidgetMap = new Dictionary<IBBoard.WarFoundry.API.Objects.Unit,UnitDisplayWidget>(); | 43 private Dictionary<WFObjects.Unit, UnitDisplayWidget> unitToWidgetMap = new Dictionary<WFObjects.Unit,UnitDisplayWidget>(); |
43 | 44 |
44 private ObjectAddDelegate UnitAddedMethod; | 45 private ObjectAddDelegate UnitAddedMethod; |
45 private ObjectRemoveDelegate UnitRemovedMethod; | 46 private ObjectRemoveDelegate UnitRemovedMethod; |
46 private DoubleValChangedDelegate PointsValueChangedMethod; | 47 private DoubleValChangedDelegate PointsValueChangedMethod; |
47 private FailedUnitRequirementDelegate FailedUnitRequirementMethod; | 48 private FailedUnitRequirementDelegate FailedUnitRequirementMethod; |
216 name = Translation.GetTranslation("categoryTreeCatNamePercentage", "{0} - {1}pts ({2}%)", c.Name, c.Points, (c.ParentArmy.Points > 0 ? Math.Round((c.Points / c.ParentArmy.Points) * 100) : 0)); | 217 name = Translation.GetTranslation("categoryTreeCatNamePercentage", "{0} - {1}pts ({2}%)", c.Name, c.Points, (c.ParentArmy.Points > 0 ? Math.Round((c.Points / c.ParentArmy.Points) * 100) : 0)); |
217 } | 218 } |
218 | 219 |
219 (cell as CellRendererText).Text = name; | 220 (cell as CellRendererText).Text = name; |
220 } | 221 } |
221 else if (o is IBBoard.WarFoundry.API.Objects.Unit) | 222 else if (o is WFObjects.Unit) |
222 { | 223 { |
223 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; | 224 WFObjects.Unit u = (WFObjects.Unit)o; |
224 string name = Translation.GetTranslation("unitTreeCatName", "{0} - {1}pts", u.Name, u.Points); | 225 string name = Translation.GetTranslation("unitTreeCatName", "{0} - {1}pts", u.Name, u.Points); |
225 (cell as CellRendererText).Text = name; | 226 (cell as CellRendererText).Text = name; |
226 } | 227 } |
227 } | 228 } |
228 | 229 |
232 Application.Quit(); | 233 Application.Quit(); |
233 } | 234 } |
234 | 235 |
235 private void OnUnitNameChanged(WarFoundryObject val, string oldValue, string newValue) | 236 private void OnUnitNameChanged(WarFoundryObject val, string oldValue, string newValue) |
236 { | 237 { |
237 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val; | 238 WFObjects.Unit unit = (WFObjects.Unit)val; |
238 UnitDisplayWidget widget; | 239 UnitDisplayWidget widget; |
239 unitToWidgetMap.TryGetValue(unit, out widget); | 240 unitToWidgetMap.TryGetValue(unit, out widget); |
240 logger.DebugFormat("Unit name changed for {0} - now called {1}", unit.ID, unit.Name); | 241 logger.DebugFormat("Unit name changed for {0} - now called {1}", unit.ID, unit.Name); |
241 treeUnits.QueueDraw(); | 242 treeUnits.QueueDraw(); |
242 | 243 |
246 } | 247 } |
247 } | 248 } |
248 | 249 |
249 private void OnUnitAdded(WarFoundryObject val) | 250 private void OnUnitAdded(WarFoundryObject val) |
250 { | 251 { |
251 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val; | 252 WFObjects.Unit unit = (WFObjects.Unit)val; |
252 unit.NameChanged+= UnitNameChangedMethod; | 253 unit.NameChanged+= UnitNameChangedMethod; |
253 AddUnitToTree(unit); | 254 AddUnitToTree(unit); |
254 } | 255 } |
255 | 256 |
256 private void AddUnitToTree(IBBoard.WarFoundry.API.Objects.Unit unit) | 257 private void AddUnitToTree(WFObjects.Unit unit) |
257 { | 258 { |
258 TreeStore model = (TreeStore)treeUnits.Model; | 259 TreeStore model = (TreeStore)treeUnits.Model; |
259 TreeIter iter; | 260 TreeIter iter; |
260 model.GetIterFirst(out iter); | 261 model.GetIterFirst(out iter); |
261 | 262 |
278 while (model.IterNext(ref iter)); | 279 while (model.IterNext(ref iter)); |
279 } | 280 } |
280 | 281 |
281 private void OnUnitRemoved(WarFoundryObject obj) | 282 private void OnUnitRemoved(WarFoundryObject obj) |
282 { | 283 { |
283 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj; | 284 WFObjects.Unit unit = (WFObjects.Unit)obj; |
284 unit.NameChanged-= UnitNameChangedMethod; | 285 unit.NameChanged-= UnitNameChangedMethod; |
285 RemoveUnitFromTree(unit); | 286 RemoveUnitFromTree(unit); |
286 | 287 RemoveUnitTab(unit); |
287 //See if unit has a tab open and close it if it does | 288 } |
288 } | 289 |
289 | 290 private void RemoveUnitFromTree(WFObjects.Unit unit) |
290 private void RemoveUnitFromTree(IBBoard.WarFoundry.API.Objects.Unit unit) | |
291 { | 291 { |
292 TreeStore model = (TreeStore)treeUnits.Model; | 292 TreeStore model = (TreeStore)treeUnits.Model; |
293 TreeIter iter; | 293 TreeIter iter; |
294 model.GetIterFirst(out iter); | 294 model.GetIterFirst(out iter); |
295 bool removed = false; | 295 bool removed = false; |
326 } | 326 } |
327 } | 327 } |
328 } | 328 } |
329 } | 329 } |
330 while (model.IterNext(ref iter)); | 330 while (model.IterNext(ref iter)); |
331 } | |
332 | |
333 private void RemoveUnitTab(WFObjects.Unit unit) | |
334 { | |
335 UnitDisplayWidget widget = DictionaryUtils.GetValue(unitToWidgetMap, unit); | |
336 | |
337 if (widget != null) | |
338 { | |
339 unitsNotebook.Remove(widget); | |
340 } | |
331 } | 341 } |
332 | 342 |
333 private void OnPointsValueChanged(WarFoundryObject obj, double before, double after) | 343 private void OnPointsValueChanged(WarFoundryObject obj, double before, double after) |
334 { | 344 { |
335 //Set points in panel | 345 //Set points in panel |
520 foreach (ArmyCategory cat in army.Categories) | 530 foreach (ArmyCategory cat in army.Categories) |
521 { | 531 { |
522 logger.DebugFormat("Append category {0}", cat.Name); | 532 logger.DebugFormat("Append category {0}", cat.Name); |
523 iter = store.AppendValues(cat); | 533 iter = store.AppendValues(cat); |
524 | 534 |
525 foreach (IBBoard.WarFoundry.API.Objects.Unit unit in cat.GetUnits()) | 535 foreach (WFObjects.Unit unit in cat.GetUnits()) |
526 { | 536 { |
527 store.AppendValues(iter, unit); | 537 store.AppendValues(iter, unit); |
528 } | 538 } |
529 } | 539 } |
530 | 540 |
909 | 919 |
910 protected virtual void ArmyRowActivated(object o, Gtk.RowActivatedArgs args) | 920 protected virtual void ArmyRowActivated(object o, Gtk.RowActivatedArgs args) |
911 { | 921 { |
912 object obj = TreeUtils.GetItemAtPath(treeUnits, args.Path); | 922 object obj = TreeUtils.GetItemAtPath(treeUnits, args.Path); |
913 | 923 |
914 if (obj is IBBoard.WarFoundry.API.Objects.Unit) | 924 if (obj is WFObjects.Unit) |
915 { | 925 { |
916 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj; | 926 WFObjects.Unit unit = (WFObjects.Unit)obj; |
917 ShowUnitWidget(unit); | 927 ShowUnitWidget(unit); |
918 } | 928 } |
919 } | 929 } |
920 | 930 |
921 private void ShowUnitWidget(IBBoard.WarFoundry.API.Objects.Unit unit) | 931 private void ShowUnitWidget(WFObjects.Unit unit) |
922 { | 932 { |
923 UnitDisplayWidget widget; | 933 UnitDisplayWidget widget; |
924 unitToWidgetMap.TryGetValue(unit, out widget); | 934 unitToWidgetMap.TryGetValue(unit, out widget); |
925 | 935 |
926 if (widget!=null) | 936 if (widget!=null) |
977 | 987 |
978 protected virtual void OnTreeUnitsPopupMenu (object o, Gtk.PopupMenuArgs args) | 988 protected virtual void OnTreeUnitsPopupMenu (object o, Gtk.PopupMenuArgs args) |
979 { | 989 { |
980 object selectedItem = TreeUtils.GetSelectedItem(treeUnits); | 990 object selectedItem = TreeUtils.GetSelectedItem(treeUnits); |
981 | 991 |
982 if (selectedItem is IBBoard.WarFoundry.API.Objects.Unit) | 992 if (selectedItem is WFObjects.Unit) |
983 { | 993 { |
984 Menu menu = new Menu(); | 994 Menu menu = new Menu(); |
985 ImageMenuItem delete = new ImageMenuItem("Remove unit"); | 995 ImageMenuItem delete = new ImageMenuItem("Remove unit"); |
986 delete.Image = new Gtk.Image(Stock.Delete, IconSize.Menu); | 996 delete.Image = new Gtk.Image(Stock.Delete, IconSize.Menu); |
987 delete.Activated+= new EventHandler(OnUnitDelete); | 997 delete.Activated+= new EventHandler(OnUnitDelete); |
992 } | 1002 } |
993 } | 1003 } |
994 | 1004 |
995 private void OnUnitDelete(object o, EventArgs args) | 1005 private void OnUnitDelete(object o, EventArgs args) |
996 { | 1006 { |
997 RemoveUnitCommand command = new RemoveUnitCommand((IBBoard.WarFoundry.API.Objects.Unit)((ImageMenuItem)o).Data["unit"]); | 1007 RemoveUnitCommand command = new RemoveUnitCommand((WFObjects.Unit)((ImageMenuItem)o).Data["unit"]); |
998 commandStack.Execute(command); | 1008 commandStack.Execute(command); |
999 } | 1009 } |
1000 | 1010 |
1001 [GLib.ConnectBefore] | 1011 [GLib.ConnectBefore] |
1002 protected virtual void UnitTreeButtonPressed(object o, Gtk.ButtonPressEventArgs args) | 1012 protected virtual void UnitTreeButtonPressed(object o, Gtk.ButtonPressEventArgs args) |
1012 if (args.Event.Type == Gdk.EventType.ButtonPress && args.Event.Button == 3) | 1022 if (args.Event.Type == Gdk.EventType.ButtonPress && args.Event.Button == 3) |
1013 { | 1023 { |
1014 OnTreeUnitsPopupMenu(o, null); | 1024 OnTreeUnitsPopupMenu(o, null); |
1015 } | 1025 } |
1016 } | 1026 } |
1027 | |
1028 protected virtual void NotebookPageRemoved(object o, Gtk.RemovedArgs args) | |
1029 { | |
1030 Widget widget = args.Widget; | |
1031 | |
1032 if (widget is UnitDisplayWidget) | |
1033 { | |
1034 unitToWidgetMap.Remove(((UnitDisplayWidget)widget).Unit); | |
1035 } | |
1036 } | |
1037 | |
1038 | |
1017 } | 1039 } |
1018 } | 1040 } |