Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.GTK
annotate FrmMainWindow.cs @ 30:5fafbb1b4592
Re #145: Add UI to remove units from army
* Add initial right-click "remove unit" for GTK app
Existing problems:
* Right-click occurs before selection changes, so it can be a click out of synch
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 04 Sep 2009 20:11:02 +0000 |
parents | 2e5448a662b6 |
children | eab45344cd56 |
rev | line source |
---|---|
19
a191d0655f55
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
18
diff
changeset
|
1 // This file (FrmMainWindow.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2008, 2009 IBBoard. |
0 | 2 // |
19
a191d0655f55
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
18
diff
changeset
|
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. |
0 | 4 |
5 using System; | |
6 using System.IO; | |
7 using System.Collections.Generic; | |
8 using System.Configuration; | |
13 | 9 using Gtk; |
10 using IBBoard; | |
0 | 11 using IBBoard.Commands; |
12
685532d43a96
Re #88: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
12 using IBBoard.GtkSharp; |
0 | 13 using IBBoard.IO; |
13 | 14 using IBBoard.Lang; |
15 using IBBoard.Logging; | |
0 | 16 using IBBoard.CustomMath; |
17 using IBBoard.WarFoundry.API; | |
28 | 18 using IBBoard.WarFoundry.API.Exporters; |
0 | 19 using IBBoard.WarFoundry.API.Factories; |
13 | 20 using IBBoard.WarFoundry.API.Factories.Xml; |
21 using IBBoard.WarFoundry.API.Objects; | |
0 | 22 using IBBoard.WarFoundry.API.Commands; |
23 using IBBoard.WarFoundry.API.Savers; | |
24 using IBBoard.WarFoundry.API.Requirements; | |
6
cfc7683e73f9
Fix breakage from r33 by completing update of move to IBBoard.WarFoundry.GTK sub-packages
IBBoard <dev@ibboard.co.uk>
parents:
5
diff
changeset
|
25 using IBBoard.WarFoundry.GTK.Widgets; |
13 | 26 using IBBoard.WarFoundry.Plugin.Rollcall; |
0 | 27 using IBBoard.Xml; |
28 using log4net; | |
29 | |
13 | 30 namespace IBBoard.WarFoundry.GTK |
21 | 31 { |
0 | 32 public partial class FrmMainWindow: Gtk.Window |
33 { | |
34 private static readonly string AppTitle = "WarFoundry"; | |
13 | 35 private const int CATEGORY_BUTTON_SEPARATOR_INDEX = 6; |
36 | |
37 private Preferences preferences; | |
38 private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow)); | |
39 | |
0 | 40 private CommandStack commandStack; |
41 private Dictionary<ToolButton, Category> categoryMap = new Dictionary<ToolButton, Category>(); | |
13 | 42 private Dictionary<IBBoard.WarFoundry.API.Objects.Unit, UnitDisplayWidget> unitToWidgetMap = new Dictionary<IBBoard.WarFoundry.API.Objects.Unit,UnitDisplayWidget>(); |
43 | |
44 private ObjectAddDelegate UnitAddedMethod; | |
45 private ObjectRemoveDelegate UnitRemovedMethod; | |
46 private DoubleValChangedDelegate PointsValueChangedMethod; | |
0 | 47 private FailedUnitRequirementDelegate FailedUnitRequirementMethod; |
48 private StringValChangedDelegate UnitNameChangedMethod; | |
21 | 49 |
0 | 50 private GameSystem system; |
51 private string loadedArmyPath; | |
21 | 52 |
0 | 53 private MenuToolButton undoMenuButton, redoMenuButton; |
21 | 54 |
0 | 55 public static void Main (string[] args) |
21 | 56 { |
13 | 57 try |
0 | 58 { |
59 Application.Init(); | |
60 FrmMainWindow win = new FrmMainWindow(args); | |
61 win.Show(); | |
62 Application.Run(); | |
13 | 63 LogManager.GetLogger(typeof(FrmMainWindow)).Debug("Application ended"); |
64 } | |
65 catch(Exception ex) | |
66 { | |
67 LogManager.GetLogger(typeof(FrmMainWindow)).Fatal("("+ex.GetType().Name+") "+ex.Message + Environment.NewLine + ex.StackTrace); | |
0 | 68 } |
69 } | |
21 | 70 |
0 | 71 public FrmMainWindow() : this(new string[0]) |
72 { | |
73 //Do nothing extra | |
74 } | |
21 | 75 |
0 | 76 public FrmMainWindow (string[] args): base (Gtk.WindowType.Toplevel) |
77 { | |
78 logger.Info("Opening FrmMainWindow"); | |
79 Build (); | |
80 //Replace the undo/redo buttons with menu versions, which Monodevelop's GUI editor doesn't currently support | |
81 redoMenuButton = new MenuToolButton("gtk-redo"); | |
82 redoMenuButton.Label = "Redo"; | |
10
c687bbe901f8
Re #37 - Resolve deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
83 redoMenuButton.TooltipText = "Redo"; |
0 | 84 redoMenuButton.Clicked+= redoTBButtonActivated; |
85 toolbar.Insert(redoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); | |
86 undoMenuButton = new MenuToolButton("gtk-undo"); | |
87 undoMenuButton.Label = "Undo"; | |
10
c687bbe901f8
Re #37 - Resolve deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
88 undoMenuButton.TooltipText = "Undo"; |
0 | 89 undoMenuButton.Clicked+= undoTBButtonActivated; |
90 toolbar.Insert(undoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); | |
91 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-1]); | |
92 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-2]); | |
93 toolbar.ShowAll(); | |
21 | 94 |
0 | 95 Title = AppTitle; |
96 TreeViewColumn mainColumn = new TreeViewColumn (); | |
97 mainColumn.Title = "Army Categories"; | |
98 CellRendererText mainCell = new CellRendererText (); | |
99 mainColumn.PackStart (mainCell, true); | |
100 treeUnits.AppendColumn(mainColumn); | |
101 mainColumn.SetCellDataFunc(mainCell, new TreeCellDataFunc(RenderCategoryTreeObjectName)); | |
102 treeUnits.Model = new TreeStore(typeof(WarFoundryObject)); | |
103 logger.Debug("Loading preferences"); | |
104 Preferences = new Preferences("WarFoundryGTK"); | |
105 logger.Debug("Loading translations"); | |
21 | 106 |
8
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
107 try |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
108 { |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
109 Translation.InitialiseTranslations(Constants.ExecutablePath, Preferences["language"].ToString()); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
110 } |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
111 catch (TranslationLoadException ex) |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
112 { |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
113 logger.Error(ex); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
114 MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, ex.Message); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
115 dialog.Title = "Translation loading failed"; |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
116 dialog.Run(); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
117 dialog.Destroy(); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
118 } |
21 | 119 |
13 | 120 logger.Debug("Initialising"); |
121 commandStack = new CommandStack(); | |
0 | 122 commandStack.CommandStackUpdated+=new MethodInvoker(commandStack_CommandStackUpdated); |
123 WarFoundryCore.GameSystemChanged+= new GameSystemChangedDelegate(OnGameSystemChanged); | |
124 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(OnArmyChanged); | |
125 Destroyed+= new EventHandler(OnWindowDestroyed); | |
126 //TODO: Translate and subscribe to other events | |
13 | 127 UnitAddedMethod = new ObjectAddDelegate(OnUnitAdded); |
128 UnitRemovedMethod = new ObjectRemoveDelegate(OnUnitRemoved); | |
129 PointsValueChangedMethod = new DoubleValChangedDelegate(OnPointsValueChanged); | |
0 | 130 FailedUnitRequirementMethod = new FailedUnitRequirementDelegate(OnFailedUnitRequirement); |
131 UnitNameChangedMethod = new StringValChangedDelegate(OnUnitNameChanged); | |
21 | 132 |
18
c4d9b1ec75ed
Re #124: Remove "factory factory"
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
133 //FIXME: Temporary hack to add paths and factories before we get preferences and plugins |
0 | 134 WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + "data")); |
18
c4d9b1ec75ed
Re #124: Remove "factory factory"
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
135 WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory()); |
c4d9b1ec75ed
Re #124: Remove "factory factory"
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
136 WarFoundryLoader.GetDefault().RegisterNonNativeFactory(RollcallFactory.GetFactory()); |
21 | 137 WarFoundrySaver.SetFileSaver(new WarFoundryXmlSaver()); |
138 | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
139 logger.Debug("Initialising complete - seeing if we can load default army or system"); |
21 | 140 |
0 | 141 if (args.Length == 1) |
142 { | |
21 | 143 logger.Debug("Attempting to load from file"); |
0 | 144 FileInfo file = new FileInfo(args[0]); |
21 | 145 |
0 | 146 try |
147 { | |
5 | 148 ICollection<IWarFoundryObject> objects = WarFoundryLoader.GetDefault().LoadFile(file); |
149 | |
150 if (objects.Count == 1) | |
0 | 151 { |
5 | 152 List<IWarFoundryObject> objectList = new List<IWarFoundryObject>(); |
153 objectList.AddRange(objects); | |
154 IWarFoundryObject loadedObject = objectList[0]; | |
21 | 155 |
5 | 156 if (loadedObject is Army) |
157 { | |
158 WarFoundryCore.CurrentArmy = (Army)loadedObject; | |
159 logger.InfoFormat("Loaded army from {0}", file.FullName); | |
160 } | |
161 else if (loadedObject is GameSystem) | |
162 { | |
163 WarFoundryCore.CurrentGameSystem = (GameSystem)loadedObject; | |
164 logger.InfoFormat("Loaded game system from {0}", file.FullName); | |
165 } | |
0 | 166 } |
167 } | |
168 catch (InvalidFileException ex) | |
169 { | |
170 //TODO: show error dialog | |
171 logger.Error(ex); | |
172 } | |
173 } | |
174 else | |
175 { | |
176 string gameSystemID = Preferences.GetStringProperty("currSystem"); | |
177 | |
178 if (gameSystemID!=null && !"".Equals(gameSystemID)) | |
179 { | |
180 logger.Debug("Attempting to load current game system from properties"); | |
181 GameSystem sys = WarFoundryLoader.GetDefault().GetGameSystem(gameSystemID); | |
21 | 182 |
0 | 183 if (sys!=null) |
184 { | |
185 WarFoundryCore.CurrentGameSystem = sys; | |
186 logger.InfoFormat("Loaded game system {0} from properties", gameSystemID); | |
187 } | |
188 } | |
189 } | |
190 } | |
21 | 191 |
0 | 192 private void RenderCategoryTreeObjectName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) |
193 { | |
194 object o = model.GetValue(iter, 0); | |
21 | 195 |
0 | 196 if (o is ArmyCategory) |
197 { | |
198 ArmyCategory c = (ArmyCategory)o; | |
199 string name = ""; | |
21 | 200 |
0 | 201 if (Preferences.GetBooleanProperty("ShowCatPercentage")) |
202 { | |
203 name = Translation.GetTranslation("categoryTreeCatName", "{0} - {1}pts", c.Name, c.PointsTotal); | |
204 } | |
205 else | |
206 { | |
207 name = Translation.GetTranslation("categoryTreeCatNamePercentage", "{0} - {1}pts ({2}%)", c.Name, c.PointsTotal, (c.ParentArmy.PointsTotal > 0 ? Math.Round((c.PointsTotal / c.ParentArmy.PointsTotal) * 100) : 0)); | |
208 } | |
21 | 209 |
0 | 210 (cell as CellRendererText).Text = name; |
211 } | |
212 else if (o is IBBoard.WarFoundry.API.Objects.Unit) | |
213 { | |
214 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; | |
215 string name = Translation.GetTranslation("categoryTreeCatName", "{0} - {1}pts", u.Name, u.PointsValue); | |
216 (cell as CellRendererText).Text = name; | |
217 } | |
218 } | |
21 | 219 |
0 | 220 private void OnWindowDestroyed(object source, EventArgs args) |
221 { | |
222 logger.Info("Exiting"); | |
223 Application.Quit(); | |
224 } | |
21 | 225 |
0 | 226 private void OnUnitNameChanged(WarFoundryObject val, string oldValue, string newValue) |
227 { | |
228 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val; | |
229 UnitDisplayWidget widget; | |
230 unitToWidgetMap.TryGetValue(unit, out widget); | |
21 | 231 |
0 | 232 if (widget!=null) |
233 { | |
26
3a396783bfed
Fixes #125: Tabs lose close buttons
IBBoard <dev@ibboard.co.uk>
parents:
25
diff
changeset
|
234 unitsNotebook.SetTabLabel(widget, NotebookUtil.CreateNotebookTabLabelWithClose(unitsNotebook, widget, newValue)); |
0 | 235 } |
236 } | |
21 | 237 |
0 | 238 private void OnUnitAdded(WarFoundryObject val) |
239 { | |
240 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val; | |
241 unit.NameChanged+= UnitNameChangedMethod; | |
242 AddUnitToTree(unit); | |
243 } | |
21 | 244 |
0 | 245 private void AddUnitToTree(IBBoard.WarFoundry.API.Objects.Unit unit) |
246 { | |
247 TreeStore model = (TreeStore)treeUnits.Model; | |
248 TreeIter iter; | |
249 model.GetIterFirst(out iter); | |
21 | 250 |
0 | 251 do |
252 { | |
253 object obj = model.GetValue(iter, 0); | |
21 | 254 |
0 | 255 if (obj is ArmyCategory) |
256 { | |
257 ArmyCategory cat = (ArmyCategory)obj; | |
21 | 258 |
0 | 259 if (cat.Equals(unit.Category)) |
260 { | |
261 model.AppendValues(iter, unit); | |
262 TreePath path = model.GetPath(iter); | |
263 treeUnits.ExpandToPath(path); | |
264 } | |
265 } | |
266 } | |
267 while (model.IterNext(ref iter)); | |
268 } | |
21 | 269 |
0 | 270 private void OnUnitRemoved(WarFoundryObject obj) |
271 { | |
272 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj; | |
273 unit.NameChanged-= UnitNameChangedMethod; | |
274 RemoveUnitFromTree(unit); | |
21 | 275 |
0 | 276 //See if unit has a tab open and close it if it does |
277 } | |
21 | 278 |
0 | 279 private void RemoveUnitFromTree(IBBoard.WarFoundry.API.Objects.Unit unit) |
280 { | |
281 TreeStore model = (TreeStore)treeUnits.Model; | |
282 TreeIter iter; | |
283 model.GetIterFirst(out iter); | |
284 bool removed = false; | |
21 | 285 |
0 | 286 do |
287 { | |
288 object obj = model.GetValue(iter, 0); | |
21 | 289 |
0 | 290 if (obj is ArmyCategory) |
291 { | |
292 ArmyCategory cat = (ArmyCategory)obj; | |
21 | 293 |
0 | 294 if (unit.Category == null || cat.Equals(unit.Category)) |
295 { | |
296 TreeIter innerIter; | |
297 model.IterChildren(out innerIter, iter); | |
21 | 298 |
0 | 299 do |
300 { | |
301 object innerObj = model.GetValue(innerIter, 0); | |
21 | 302 |
0 | 303 if (unit.Equals(innerObj)) |
304 { | |
305 model.Remove(ref innerIter); | |
306 removed = true; | |
307 break; | |
308 } | |
309 } | |
310 while (model.IterNext(ref innerIter)); | |
21 | 311 |
0 | 312 if (removed) |
313 { | |
314 break; | |
315 } | |
316 } | |
317 } | |
318 } | |
319 while (model.IterNext(ref iter)); | |
320 } | |
21 | 321 |
0 | 322 private void OnPointsValueChanged(WarFoundryObject obj, double before, double after) |
323 { | |
324 //Set points in panel | |
325 } | |
21 | 326 |
0 | 327 private void OnFailedUnitRequirement(List<FailedUnitRequirement> failedRequirement) |
328 { | |
329 //Show error message in panel | |
330 } | |
21 | 331 |
0 | 332 public Preferences Preferences |
333 { | |
334 get { return preferences; } | |
21 | 335 set { preferences = value; } |
0 | 336 } |
21 | 337 |
0 | 338 /*public AbstractNativeWarFoundryFactory Factory |
339 { | |
340 get { return WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(Constants.ExecutablePath, factoryType); } | |
341 }*/ | |
21 | 342 |
0 | 343 protected void OnDeleteEvent (object sender, DeleteEventArgs a) |
344 { | |
345 Application.Quit (); | |
346 a.RetVal = true; | |
347 } | |
348 | |
349 protected virtual void OnExitActivated(object sender, System.EventArgs e) | |
350 { | |
351 Application.Quit(); | |
352 } | |
353 | |
354 protected virtual void OnChangeGameSystemActivated(object sender, System.EventArgs e) | |
355 { | |
356 ChangeCurrentGameSystem(); | |
357 } | |
358 | |
359 protected virtual void OnCreateArmyActivated(object sender, System.EventArgs e) | |
360 { | |
361 CreateNewArmy(); | |
362 } | |
363 | |
364 protected virtual void OnReloadFilesActivated(object sender, System.EventArgs e) | |
365 { | |
366 } | |
367 | |
368 protected virtual void OnSaveArmyAsActivated(object sender, System.EventArgs e) | |
369 { | |
370 SaveCurrentArmyAs(); | |
371 } | |
372 | |
373 protected virtual void OnCloseArmyActivated(object sender, System.EventArgs e) | |
374 { | |
375 CloseCurrentArmy(); | |
376 } | |
377 | |
378 protected virtual void OnOpenArmyActivated(object sender, System.EventArgs e) | |
379 { | |
380 OpenArmy(); | |
381 } | |
382 | |
383 protected virtual void OnSaveArmyActivated(object sender, System.EventArgs e) | |
384 { | |
385 SaveCurrentArmy(); | |
386 } | |
21 | 387 |
0 | 388 protected virtual void OnAddUnitActivated(object sender, System.EventArgs e) |
389 { | |
390 if (sender is ToolButton) | |
391 { | |
392 Category cat = null; | |
393 categoryMap.TryGetValue((ToolButton)sender, out cat); | |
21 | 394 |
0 | 395 if (cat!=null) |
396 { | |
397 logger.DebugFormat("Show FrmNewUnit for {0}", cat.Name); | |
398 FrmNewUnit newUnit = new FrmNewUnit(WarFoundryCore.CurrentArmy.Race, cat, WarFoundryCore.CurrentArmy); | |
399 ResponseType response = (ResponseType)newUnit.Run(); | |
400 newUnit.Hide(); | |
21 | 401 |
0 | 402 if (response==ResponseType.Ok) |
403 { | |
14
abbf8a3ac431
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
404 CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(cat)); |
0 | 405 commandStack.Execute(cmd); |
406 } | |
21 | 407 |
0 | 408 newUnit.Dispose(); |
409 } | |
410 } | |
411 } | |
21 | 412 |
13 | 413 public CommandStack CommandStack |
414 { | |
415 get { return commandStack; } | |
0 | 416 } |
21 | 417 |
0 | 418 private void SetAppTitle() |
419 { | |
420 if (WarFoundryCore.CurrentArmy!=null) | |
421 { | |
422 Title = AppTitle + " - " + WarFoundryCore.CurrentGameSystem.Name + " - " + WarFoundryCore.CurrentArmy.Name; | |
423 } | |
424 else if (WarFoundryCore.CurrentGameSystem!=null) | |
425 { | |
426 Title = AppTitle + " - " + WarFoundryCore.CurrentGameSystem.Name; | |
427 } | |
428 else | |
429 { | |
430 Title = AppTitle; | |
431 } | |
432 } | |
21 | 433 |
0 | 434 private void OnGameSystemChanged(GameSystem oldSys, GameSystem newSys) |
435 { | |
436 system = newSys; | |
437 SetAppTitle(); | |
438 miCreateArmy.Sensitive = system!=null; | |
439 newArmyButton.Sensitive = system!=null; | |
440 RemoveCategoryButtons(); | |
21 | 441 |
0 | 442 if (system!=null) |
443 { | |
444 AddCategoryButtons(system.Categories); | |
445 } | |
446 } | |
21 | 447 |
0 | 448 private void OnArmyChanged(Army oldArmy, Army newArmy) |
449 { | |
450 loadedArmyPath = null; | |
451 SetAppTitle(); | |
452 SetArmyTree(newArmy); | |
21 | 453 |
0 | 454 if (oldArmy!=null) |
13 | 455 { |
456 oldArmy.UnitAdded-= UnitAddedMethod; | |
457 oldArmy.UnitRemoved-= UnitRemovedMethod; | |
458 oldArmy.PointsValueChanged-= PointsValueChangedMethod; | |
21 | 459 oldArmy.FailedRequirement-=FailedUnitRequirementMethod; |
0 | 460 } |
21 | 461 |
0 | 462 unitToWidgetMap.Clear(); |
21 | 463 |
0 | 464 while (unitsNotebook.NPages > 0) |
465 { | |
466 unitsNotebook.RemovePage(0); | |
467 } | |
21 | 468 |
13 | 469 if (newArmy==null) |
470 { | |
471 DisableCategoryButtons(); | |
472 } | |
473 else | |
474 { | |
475 newArmy.UnitAdded+= UnitAddedMethod; | |
476 newArmy.UnitRemoved+= UnitRemovedMethod; | |
477 newArmy.PointsValueChanged+= PointsValueChangedMethod; | |
478 newArmy.FailedRequirement+=FailedUnitRequirementMethod; | |
479 //TODO: Clear all buttons | |
480 EnableCategoryButtons(); | |
481 | |
482 if (newArmy.Race.HasCategoryOverrides()) | |
483 { | |
484 RemoveCategoryButtons(); | |
485 AddCategoryButtons(newArmy.Race.Categories); | |
486 } | |
0 | 487 } |
21 | 488 |
0 | 489 miCloseArmy.Sensitive = newArmy!=null; |
490 miSaveArmyAs.Sensitive = newArmy!=null; | |
29 | 491 miExportArmy.Sensitive = newArmy!=null; |
0 | 492 //New army has no changes, so we can't save it |
493 miSaveArmy.Sensitive = false; | |
13 | 494 saveArmyButton.Sensitive = false; |
495 | |
496 CommandStack.Reset(); | |
0 | 497 SetPointsPanelText(); |
498 } | |
21 | 499 |
0 | 500 private void SetArmyTree(Army army) |
501 { | |
502 logger.Debug("Resetting tree"); | |
503 TreeStore store = (TreeStore)treeUnits.Model; | |
504 store.Clear(); | |
505 TreeIter iter; | |
21 | 506 |
0 | 507 if (army!=null) |
508 { | |
509 logger.Debug("Loading in categories to tree"); | |
21 | 510 |
0 | 511 foreach (ArmyCategory cat in army.Categories) |
512 { | |
513 logger.DebugFormat("Append category {0}", cat.Name); | |
514 iter = store.AppendValues(cat); | |
21 | 515 |
0 | 516 foreach (IBBoard.WarFoundry.API.Objects.Unit unit in cat.GetUnits()) |
517 { | |
518 store.AppendValues(iter, unit); | |
21 | 519 } |
0 | 520 } |
21 | 521 |
0 | 522 logger.Debug("Finished loading tree categories"); |
523 } | |
524 } | |
21 | 525 |
0 | 526 private void DisableCategoryButtons() |
527 { | |
528 SetCategoryButtonsSensitive(false); | |
529 } | |
21 | 530 |
0 | 531 private void EnableCategoryButtons() |
532 { | |
533 SetCategoryButtonsSensitive(true); | |
534 } | |
21 | 535 |
0 | 536 private void SetCategoryButtonsSensitive(bool state) |
537 { | |
538 int toolbarButtonCount = toolbar.Children.Length - 1; | |
539 logger.Debug("Last button index: "+toolbarButtonCount); | |
21 | 540 |
0 | 541 for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--) |
542 { | |
543 logger.DebugFormat("Setting button {0} state to {1}", i, state); | |
544 toolbar.Children[i].Sensitive = state; | |
545 } | |
546 } | |
21 | 547 |
0 | 548 private void RemoveCategoryButtons() |
549 { | |
550 int toolbarButtonCount = toolbar.Children.Length - 1; | |
21 | 551 |
0 | 552 for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--) |
553 { | |
554 toolbar.Remove(toolbar.Children[i]); | |
555 } | |
21 | 556 |
0 | 557 categoryMap.Clear(); |
558 } | |
21 | 559 |
0 | 560 private void AddCategoryButtons(Category[] cats) |
561 { | |
562 if (cats!=null && cats.Length > 0) | |
563 { | |
564 logger.DebugFormat("Toolbar button count: {0}. Adding {1} categories.", toolbar.Children.Length, cats.Length); | |
21 | 565 |
0 | 566 foreach (Category cat in cats) |
567 { | |
568 ToolButton button = new ToolButton("gtk-add"); | |
569 button.Label = cat.Name; | |
10
c687bbe901f8
Re #37 - Resolve deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
570 button.TooltipText = "Add unit from "+cat.Name; |
0 | 571 //TODO: See if we can associate data in some way, the same as we can with SWF. For now we just use the map. |
572 categoryMap.Add(button, cat); | |
573 button.Clicked+= new System.EventHandler(OnAddUnitActivated); | |
574 toolbar.Insert(button, -1); | |
575 } | |
576 } | |
21 | 577 |
0 | 578 toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX].Visible = cats!=null && cats.Length>0; |
21 | 579 |
0 | 580 toolbar.ShowAll(); |
581 } | |
21 | 582 |
0 | 583 private void SetPointsPanelText() |
584 { | |
585 //TODO: Set the points value in the status bar | |
586 } | |
21 | 587 |
13 | 588 private void commandStack_CommandStackUpdated() |
589 { | |
590 undoMenuButton.Sensitive = commandStack.CanUndo(); | |
591 miUndo.Sensitive = undoMenuButton.Sensitive; | |
592 redoMenuButton.Sensitive = commandStack.CanRedo(); | |
593 miRedo.Sensitive = redoMenuButton.Sensitive; | |
0 | 594 int redoLength = commandStack.RedoLength; |
13 | 595 //TODO: Build menus for undo/redo and find way of adding tooltips |
596 /*int maxRedo = Math.Min(10, redoLength); | |
597 MenuItem[] menuItems = null; | |
21 | 598 |
13 | 599 if (redoLength > 0) |
600 { | |
601 menuItems = new MenuItem[maxRedo]; | |
602 Command com; | |
603 MenuItem mi; | |
604 | |
605 for (int i = 0; i < maxRedo; i++) | |
606 { | |
607 com = commandStack.PeekRedoCommand(i+1); | |
608 | |
609 if (com == null) | |
610 { | |
21 | 611 break; |
13 | 612 } |
613 | |
614 mi = new MenuItem(com.Description); | |
615 mi.Click+=new EventHandler(redoMenu_Click); | |
616 menuItems[i] = mi; | |
617 } | |
618 } | |
619 | |
620 redoMenu.MenuItems.Clear(); | |
621 | |
622 if (menuItems!=null && menuItems[0]!=null) | |
623 { | |
624 bttnRedo.ToolTipText = menuItems[0].Text; | |
625 redoMenu.MenuItems.AddRange(menuItems); | |
0 | 626 }*/ |
627 //TODO: Put above code back when we have a dropdown version of the redo button | |
628 if (redoLength > 0) | |
629 { | |
630 //redoMenuButton.Tooltip = CommandStack.PeekRedoCommand().Description; | |
13 | 631 } |
632 | |
633 int undoLength = commandStack.UndoLength; | |
634 /*int maxUndo = Math.Min(10, undoLength); | |
635 MenuItem[] menuItemsUndo = null; | |
21 | 636 |
13 | 637 if (undoLength > 0) |
638 { | |
639 menuItemsUndo = new MenuItem[maxUndo]; | |
640 Command com; | |
641 MenuItem mi; | |
642 | |
643 for (int i = 0; i < maxUndo; i++) | |
644 { | |
645 com = commandStack.PeekUndoCommand(i+1); | |
646 | |
647 if (com == null) | |
648 { | |
21 | 649 break; |
13 | 650 } |
651 | |
652 mi = new MenuItem(com.UndoDescription); | |
653 mi.Click+=new EventHandler(undoMenu_Click); | |
654 menuItemsUndo[i] = mi; | |
655 } | |
656 } | |
657 | |
658 undoMenu.MenuItems.Clear(); | |
659 | |
660 if (menuItemsUndo!=null && menuItemsUndo[0]!=null) | |
661 { | |
662 bttnUndo.ToolTipText = menuItemsUndo[0].Text; | |
663 undoMenu.MenuItems.AddRange(menuItemsUndo); | |
21 | 664 }*/ |
13 | 665 //TODO: Put above code back when we have a dropdown version of the undo button |
0 | 666 if (undoLength > 0) |
667 { | |
668 //undoMenuButton.Tooltip = CommandStack.PeekUndoCommand().UndoDescription; | |
669 } | |
21 | 670 |
13 | 671 saveArmyButton.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null && CanSave(); |
672 miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null && CanSave(); | |
21 | 673 } |
674 | |
0 | 675 private bool CanSave() |
676 { | |
677 return loadedArmyPath!=null && WarFoundryCore.CurrentArmy!=null && WarFoundrySaver.GetSaver()!=null; | |
13 | 678 } |
679 | |
680 private bool SaveCurrentArmyOrSaveAs() | |
681 { | |
682 if (CanSave()) | |
683 { | |
684 return SaveCurrentArmy(); | |
685 } | |
21 | 686 else |
13 | 687 { |
688 return SaveCurrentArmyAs(); | |
689 } | |
0 | 690 } |
21 | 691 |
0 | 692 private bool OpenArmy() |
693 { | |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
694 FileChooserDialog fileDialog = new FileChooserDialog("Open army", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
695 FileFilter filter = new FileFilter(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
696 filter.AddPattern("*.army"); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
697 filter.Name = "WarFoundry Army files (*.army)"; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
698 fileDialog.AddFilter(filter); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
699 int response = fileDialog.Run(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
700 string filePath = null; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
701 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
702 if (response == (int)ResponseType.Accept) |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
703 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
704 filePath = fileDialog.Filename; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
705 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
706 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
707 fileDialog.Hide(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
708 fileDialog.Dispose(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
709 |
0 | 710 bool success = false; |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
711 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
712 if (filePath != null) |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
713 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
714 FileInfo file = new FileInfo(filePath); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
715 Army army = WarFoundryLoader.GetDefault().LoadArmy(file); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
716 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
717 if (army != null) |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
718 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
719 logger.Debug("Loaded army " + army.ID); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
720 success = true; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
721 loadedArmyPath = filePath; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
722 WarFoundryCore.CurrentArmy = army; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
723 logger.Debug("Army loading complete"); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
724 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
725 else |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
726 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
727 logger.ErrorFormat("Failed to load {0} as an army file", filePath); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
728 MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, file.Name + " could not be loaded.\n\nIf the file is an army file then please check your file loaders."); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
729 dialog.Title = "Failed to open army"; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
730 dialog.Run(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
731 dialog.Hide(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
732 dialog.Dispose(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
733 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
734 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
735 else |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
736 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
737 logger.Debug("Army open requested but cancelled"); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
738 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
739 |
0 | 740 return success; |
13 | 741 } |
742 | |
743 private bool SaveCurrentArmy() | |
0 | 744 { |
745 bool success = false; | |
21 | 746 |
13 | 747 if (CanSave()) |
0 | 748 { |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
749 success = SaveArmyToPath(WarFoundryCore.CurrentArmy, loadedArmyPath); |
0 | 750 } |
21 | 751 |
13 | 752 return success; |
753 } | |
754 | |
755 private bool SaveCurrentArmyAs() | |
756 { | |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
757 FileChooserDialog fileDialog = new FileChooserDialog("Save file as", this, FileChooserAction.Save, "Cancel", ResponseType.Cancel, "Save", ResponseType.Accept); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
758 FileFilter filter = new FileFilter(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
759 filter.AddPattern("*.army"); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
760 filter.Name = "WarFoundry Army files (*.army)"; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
761 fileDialog.AddFilter(filter); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
762 int response = fileDialog.Run(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
763 string filePath = null; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
764 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
765 if (response == (int)ResponseType.Accept) |
13 | 766 { |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
767 filePath = fileDialog.Filename; |
13 | 768 } |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
769 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
770 fileDialog.Hide(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
771 fileDialog.Dispose(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
772 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
773 return SaveArmyToPath(WarFoundryCore.CurrentArmy, filePath); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
774 } |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
775 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
776 private bool SaveArmyToPath(Army army, string filePath) |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
777 { |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
778 bool success = false; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
779 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
780 if (filePath!=null) |
13 | 781 { |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
782 if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, filePath)) |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
783 { |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
784 miSaveArmy.Sensitive = false; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
785 saveArmyButton.Sensitive = false; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
786 CommandStack.setCleanMark(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
787 loadedArmyPath = filePath; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
788 success = true; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
789 } |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
790 else |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
791 { |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
792 MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Failed to save file to "+filePath); |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
793 dialog.Title = "Army save failed"; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
794 dialog.Run(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
795 dialog.Hide(); |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
796 dialog.Dispose(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
797 } |
21 | 798 } |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
799 //else user cancelled |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
800 |
21 | 801 return success; |
0 | 802 } |
21 | 803 |
13 | 804 private bool CloseCurrentArmy() |
805 { | |
806 if (WarFoundryCore.CurrentArmy!=null) | |
807 { | |
808 bool canClose = false; | |
809 | |
810 if (CommandStack.IsDirty()) | |
0 | 811 { |
27
83c8945edac2
Re #143: GTK "Army has been modified" dialog has no buttons
IBBoard <dev@ibboard.co.uk>
parents:
26
diff
changeset
|
812 MessageDialog dia = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.YesNo, "The army \""+WarFoundryCore.CurrentArmy.Name+"\" has been modified.\r\nSave changes before closing army?"); |
0 | 813 ResponseType dr = (ResponseType)dia.Run(); |
27
83c8945edac2
Re #143: GTK "Army has been modified" dialog has no buttons
IBBoard <dev@ibboard.co.uk>
parents:
26
diff
changeset
|
814 dia.Hide(); |
83c8945edac2
Re #143: GTK "Army has been modified" dialog has no buttons
IBBoard <dev@ibboard.co.uk>
parents:
26
diff
changeset
|
815 dia.Dispose(); |
0 | 816 |
13 | 817 if (dr == ResponseType.Yes) |
0 | 818 { |
819 //They want to save so try to save it or prompt for save as | |
13 | 820 //If they cancel the save as then assume they don't want to close |
821 canClose = SaveCurrentArmyOrSaveAs(); | |
822 } | |
823 else if (dr == ResponseType.No) | |
0 | 824 { |
13 | 825 //They don't care about their changes |
826 canClose = true; | |
0 | 827 } |
828 else | |
829 { | |
830 //Assume cancel or close with the X button | |
831 canClose = false; | |
832 } | |
13 | 833 } |
834 else | |
835 { | |
836 //Nothing has changed so we can safely close | |
837 canClose = true; | |
838 } | |
839 | |
840 if (canClose) | |
0 | 841 { |
13 | 842 //do close |
843 WarFoundryCore.CurrentArmy = null; | |
844 return true; | |
845 } | |
846 else | |
847 { | |
848 return false; | |
849 } | |
850 } | |
851 else | |
852 { | |
853 //pretend we succeeded | |
854 return true; | |
855 } | |
856 } | |
857 | |
858 private void CreateNewArmy() | |
0 | 859 { |
860 logger.Debug("Create new army"); | |
861 FrmNewArmy newArmy = new FrmNewArmy(WarFoundryCore.CurrentGameSystem); | |
862 ResponseType type = (ResponseType)newArmy.Run(); | |
863 newArmy.Hide(); | |
21 | 864 |
0 | 865 if (type == ResponseType.Ok) |
13 | 866 { |
867 if (CloseCurrentArmy()) | |
0 | 868 { |
869 WarFoundryCore.CurrentArmy = new Army(newArmy.SelectedRace, newArmy.ArmyName, newArmy.ArmySize); | |
870 } | |
871 } | |
872 else | |
873 { | |
874 logger.Debug("Create new army cancelled"); | |
875 } | |
21 | 876 |
13 | 877 newArmy.Destroy(); |
0 | 878 } |
21 | 879 |
0 | 880 private void ChangeCurrentGameSystem() |
21 | 881 { |
0 | 882 logger.Debug("Changing game system"); |
883 FrmChangeGameSystem dialog = new FrmChangeGameSystem(this); | |
884 ResponseType type = (ResponseType)dialog.Run(); | |
885 dialog.Hide(); | |
21 | 886 |
0 | 887 if (type == ResponseType.Ok) |
888 { | |
889 WarFoundryCore.CurrentGameSystem = dialog.SelectedSystem; | |
890 } | |
891 else | |
892 { | |
893 logger.Debug("Game system change cancelled"); | |
894 } | |
21 | 895 |
0 | 896 dialog.Destroy(); |
897 } | |
898 | |
899 protected virtual void undoTBButtonActivated (object sender, System.EventArgs e) | |
900 { | |
901 CommandStack.Undo(); | |
902 } | |
903 | |
904 protected virtual void redoTBButtonActivated (object sender, System.EventArgs e) | |
905 { | |
906 CommandStack.Redo(); | |
907 } | |
908 | |
909 protected virtual void saveTBButtonActivated (object sender, System.EventArgs e) | |
910 { | |
911 SaveCurrentArmy(); | |
912 } | |
913 | |
914 protected virtual void openTBButtonActivated (object sender, System.EventArgs e) | |
915 { | |
916 OpenArmy(); | |
917 } | |
918 | |
919 protected virtual void newTBButtonActivated (object sender, System.EventArgs e) | |
920 { | |
921 CreateNewArmy(); | |
922 } | |
923 | |
924 protected virtual void ArmyRowActivated (object o, Gtk.RowActivatedArgs args) | |
925 { | |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
926 object obj = TreeUtils.GetItemAtPath(treeUnits, args.Path); |
21 | 927 |
0 | 928 if (obj is IBBoard.WarFoundry.API.Objects.Unit) |
929 { | |
930 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj; | |
21 | 931 |
0 | 932 UnitDisplayWidget widget; |
933 unitToWidgetMap.TryGetValue(unit, out widget); | |
21 | 934 |
0 | 935 if (widget!=null) |
936 { | |
937 logger.DebugFormat("Selecting existing page for "+unit.Name); | |
938 unitsNotebook.Page = unitsNotebook.PageNum(widget); | |
939 } | |
940 else | |
941 { | |
942 widget = new UnitDisplayWidget(unit, CommandStack); | |
943 logger.Debug("Adding page for "+unit.Name); | |
944 unitToWidgetMap[unit] = widget; | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
945 widget.Destroyed+= new EventHandler(UnitWidgetDestroyed); |
12
685532d43a96
Re #88: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
946 int pageNum = NotebookUtil.AddPageToNotebookWithCloseButton(unitsNotebook, widget, unit.Name); |
0 | 947 logger.Debug("Page added at index "+pageNum); |
948 unitsNotebook.ShowAll(); | |
949 unitsNotebook.Page = pageNum; | |
950 } | |
951 } | |
952 } | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
953 |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
954 private void UnitWidgetDestroyed(object sender, EventArgs e) |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
955 { |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
956 if (sender is UnitDisplayWidget) |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
957 { |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
958 unitToWidgetMap.Remove(((UnitDisplayWidget)sender).Unit); |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
959 } |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
960 } |
28 | 961 |
962 protected virtual void OnMiExportAsBasicHtmlActivated (object sender, System.EventArgs e) | |
963 { | |
964 FileChooserDialog fileDialog = new FileChooserDialog("Export army", this, FileChooserAction.Save, "Cancel", ResponseType.Cancel, "Export", ResponseType.Accept); | |
965 FileFilter filter = new FileFilter(); | |
966 filter.AddPattern("*.html"); | |
967 filter.Name = "HTML pages (*.html)"; | |
968 fileDialog.AddFilter(filter); | |
969 int response = fileDialog.Run(); | |
970 string filePath = null; | |
971 | |
972 if (response == (int)ResponseType.Accept) | |
973 { | |
974 filePath = fileDialog.Filename; | |
975 } | |
976 | |
977 fileDialog.Hide(); | |
978 fileDialog.Dispose(); | |
979 | |
980 if (filePath != null) | |
981 { | |
982 Army army = WarFoundryCore.CurrentArmy; | |
983 logger.DebugFormat("Exporting {0} to {1} as basic HTML", army.Name, filePath); | |
984 WarFoundryHtmlExporter.GetDefault().ExportArmy(army, filePath); | |
985 } | |
986 } | |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
987 |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
988 protected virtual void OnTreeUnitsPopupMenu (object o, Gtk.PopupMenuArgs args) |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
989 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
990 object selectedItem = TreeUtils.GetSelectedItem(treeUnits); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
991 |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
992 if (selectedItem is IBBoard.WarFoundry.API.Objects.Unit) |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
993 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
994 Menu menu = new Menu(); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
995 ImageMenuItem delete = new ImageMenuItem("Remove unit"); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
996 delete.Image = new Gtk.Image(Stock.Delete, IconSize.Menu); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
997 delete.Activated+= new EventHandler(OnUnitDelete); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
998 delete.Data["unit"] = selectedItem; |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
999 menu.Append(delete); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1000 menu.ShowAll(); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1001 menu.Popup(); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1002 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1003 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1004 |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1005 private void OnUnitDelete(object o, EventArgs args) |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1006 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1007 RemoveUnitCommand command = new RemoveUnitCommand((IBBoard.WarFoundry.API.Objects.Unit)((ImageMenuItem)o).Data["unit"]); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1008 commandStack.Execute(command); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1009 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1010 |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1011 [GLib.ConnectBefore] |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1012 protected virtual void UnitTreeButtonPressed (object o, Gtk.ButtonPressEventArgs args) |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1013 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1014 if (args.Event.Type == Gdk.EventType.ButtonPress && args.Event.Button == 3) |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1015 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1016 OnTreeUnitsPopupMenu(o, null); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1017 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1018 } |
0 | 1019 } |
1020 } |