Mercurial > repos > IBBoard.WarFoundry.GUI.GTK
annotate FrmMainWindow.cs @ 113:4a33b3012100 WarFoundry_v0.1RC1
* Tag v0.1RC1 release
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 17 Jan 2011 19:43:47 +0000 |
parents | d68992a831df |
children | 5e8de1507cfb |
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.Collections.Generic; | |
7 using System.Configuration; | |
113 | 8 using System.IO; |
9 using GLib; | |
13 | 10 using Gtk; |
11 using IBBoard; | |
0 | 12 using IBBoard.Commands; |
113 | 13 using IBBoard.CustomMath; |
12
685532d43a96
Re #88: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
14 using IBBoard.GtkSharp; |
113 | 15 using IBBoard.GtkSharp.Translatable; |
0 | 16 using IBBoard.IO; |
13 | 17 using IBBoard.Lang; |
18 using IBBoard.Logging; | |
0 | 19 using IBBoard.WarFoundry.API; |
113 | 20 using IBBoard.WarFoundry.API.Commands; |
28 | 21 using IBBoard.WarFoundry.API.Exporters; |
0 | 22 using IBBoard.WarFoundry.API.Factories; |
13 | 23 using IBBoard.WarFoundry.API.Factories.Xml; |
24 using IBBoard.WarFoundry.API.Objects; | |
113 | 25 using IBBoard.WarFoundry.API.Requirements; |
0 | 26 using IBBoard.WarFoundry.API.Savers; |
113 | 27 using IBBoard.WarFoundry.GUI.GTK.Widgets; |
13 | 28 using IBBoard.WarFoundry.Plugin.Rollcall; |
0 | 29 using IBBoard.Xml; |
30 using log4net; | |
113 | 31 using WFObjects = IBBoard.WarFoundry.API.Objects; |
32 using System.Collections; | |
0 | 33 |
113 | 34 namespace IBBoard.WarFoundry.GUI.GTK |
21 | 35 { |
113 | 36 public partial class FrmMainWindow: TranslatableWindowWithActions |
0 | 37 { |
38 private static readonly string AppTitle = "WarFoundry"; | |
13 | 39 private const int CATEGORY_BUTTON_SEPARATOR_INDEX = 6; |
40 private Preferences preferences; | |
41 private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow)); | |
0 | 42 private CommandStack commandStack; |
113 | 43 private Dictionary<WFObjects.Unit, UnitDisplayWidget> unitToWidgetMap = new Dictionary<WFObjects.Unit,UnitDisplayWidget>(); |
13 | 44 private ObjectAddDelegate UnitAddedMethod; |
45 private ObjectRemoveDelegate UnitRemovedMethod; | |
46 private DoubleValChangedDelegate PointsValueChangedMethod; | |
0 | 47 private FailedUnitRequirementDelegate FailedUnitRequirementMethod; |
48 private StringValChangedDelegate UnitNameChangedMethod; | |
49 private GameSystem system; | |
50 private string loadedArmyPath; | |
51 private MenuToolButton undoMenuButton, redoMenuButton; | |
21 | 52 |
113 | 53 public static void Main(string[] args) |
21 | 54 { |
13 | 55 try |
0 | 56 { |
113 | 57 ExceptionManager.UnhandledException += HandleUnhandledException; |
0 | 58 Application.Init(); |
59 FrmMainWindow win = new FrmMainWindow(args); | |
60 win.Show(); | |
61 Application.Run(); | |
13 | 62 LogManager.GetLogger(typeof(FrmMainWindow)).Debug("Application ended"); |
63 } | |
113 | 64 catch (Exception ex) |
65 { | |
66 HandleUnhandledException(ex); | |
67 } | |
68 } | |
69 | |
70 private static void HandleUnhandledException(UnhandledExceptionArgs args) | |
71 { | |
72 object obj = args.ExceptionObject; | |
73 Exception ex = null; | |
74 | |
75 if (obj is Exception) | |
13 | 76 { |
113 | 77 ex = (Exception)obj; |
78 } | |
79 else | |
80 { | |
81 ex = new Exception("GLib returned unexpected exception object type " + obj.GetType()); | |
0 | 82 } |
113 | 83 |
84 HandleUnhandledException(ex); | |
85 } | |
86 | |
87 private static void HandleUnhandledException(Exception ex) | |
88 { | |
89 string msg = String.Format("({0}) {1}", ex.GetType().FullName, ex.Message); | |
90 LogManager.GetLogger(typeof(FrmMainWindow)).Fatal(msg, ex); | |
91 MessageDialog dialog = new MessageDialog(null, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, false, "An unhandled exception occurred. Please check the log for more details."); | |
92 dialog.Run(); | |
93 dialog.Hide(); | |
94 dialog.Dispose(); | |
0 | 95 } |
21 | 96 |
0 | 97 public FrmMainWindow() : this(new string[0]) |
98 { | |
99 //Do nothing extra | |
100 } | |
21 | 101 |
113 | 102 public FrmMainWindow(string[] args) : base(Gtk.WindowType.Toplevel) |
0 | 103 { |
104 logger.Info("Opening FrmMainWindow"); | |
113 | 105 Build(); |
0 | 106 //Replace the undo/redo buttons with menu versions, which Monodevelop's GUI editor doesn't currently support |
107 redoMenuButton = new MenuToolButton("gtk-redo"); | |
113 | 108 redoMenuButton.Name = "bttnRedo"; |
109 redoMenuButton.Clicked += redoTBButtonActivated; | |
110 redoMenuButton.Sensitive = false; | |
0 | 111 toolbar.Insert(redoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); |
112 undoMenuButton = new MenuToolButton("gtk-undo"); | |
113 | 113 undoMenuButton.Name = "bttnUndo"; |
114 undoMenuButton.Clicked += undoTBButtonActivated; | |
115 undoMenuButton.Sensitive = false; | |
0 | 116 toolbar.Insert(undoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); |
113 | 117 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX - 1]); |
118 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX - 2]); | |
0 | 119 toolbar.ShowAll(); |
21 | 120 |
113 | 121 TreeViewColumn mainColumn = new TreeViewColumn(); |
122 CellRendererText mainCell = new CellRendererText(); | |
123 mainColumn.PackStart(mainCell, true); | |
0 | 124 treeUnits.AppendColumn(mainColumn); |
125 mainColumn.SetCellDataFunc(mainCell, new TreeCellDataFunc(RenderCategoryTreeObjectName)); | |
126 treeUnits.Model = new TreeStore(typeof(WarFoundryObject)); | |
127 logger.Debug("Loading preferences"); | |
113 | 128 Preferences = new Preferences("WarFoundry-GTK"); |
0 | 129 logger.Debug("Loading translations"); |
21 | 130 |
8
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
131 try |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
132 { |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
133 Translation.InitialiseTranslations(Constants.ExecutablePath, Preferences["language"].ToString()); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
134 } |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
135 catch (TranslationLoadException ex) |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
136 { |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
137 logger.Error(ex); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
138 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
|
139 dialog.Title = "Translation loading failed"; |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
140 dialog.Run(); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
141 dialog.Destroy(); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
142 } |
21 | 143 |
13 | 144 logger.Debug("Initialising"); |
145 commandStack = new CommandStack(); | |
113 | 146 commandStack.CommandStackUpdated += new MethodInvoker(commandStack_CommandStackUpdated); |
147 WarFoundryCore.GameSystemChanged += new GameSystemChangedDelegate(OnGameSystemChanged); | |
148 WarFoundryCore.ArmyChanged += new ArmyChangedDelegate(OnArmyChanged); | |
149 Destroyed += new EventHandler(OnWindowDestroyed); | |
150 Translation.TranslationChanged += Retranslate; | |
151 Translate(); | |
13 | 152 UnitAddedMethod = new ObjectAddDelegate(OnUnitAdded); |
153 UnitRemovedMethod = new ObjectRemoveDelegate(OnUnitRemoved); | |
154 PointsValueChangedMethod = new DoubleValChangedDelegate(OnPointsValueChanged); | |
0 | 155 FailedUnitRequirementMethod = new FailedUnitRequirementDelegate(OnFailedUnitRequirement); |
156 UnitNameChangedMethod = new StringValChangedDelegate(OnUnitNameChanged); | |
21 | 157 |
18
c4d9b1ec75ed
Re #124: Remove "factory factory"
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
158 //FIXME: Temporary hack to add paths and factories before we get preferences and plugins |
0 | 159 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
|
160 WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory()); |
c4d9b1ec75ed
Re #124: Remove "factory factory"
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
161 WarFoundryLoader.GetDefault().RegisterNonNativeFactory(RollcallFactory.GetFactory()); |
32
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
162 WarFoundryLoader.GetDefault().FileLoadingFinished += FileLoadingFinished; |
21 | 163 WarFoundrySaver.SetFileSaver(new WarFoundryXmlSaver()); |
164 | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
165 logger.Debug("Initialising complete - seeing if we can load default army or system"); |
21 | 166 |
0 | 167 if (args.Length == 1) |
168 { | |
21 | 169 logger.Debug("Attempting to load from file"); |
0 | 170 FileInfo file = new FileInfo(args[0]); |
21 | 171 |
0 | 172 try |
173 { | |
5 | 174 ICollection<IWarFoundryObject> objects = WarFoundryLoader.GetDefault().LoadFile(file); |
175 | |
176 if (objects.Count == 1) | |
0 | 177 { |
5 | 178 List<IWarFoundryObject> objectList = new List<IWarFoundryObject>(); |
179 objectList.AddRange(objects); | |
180 IWarFoundryObject loadedObject = objectList[0]; | |
21 | 181 |
5 | 182 if (loadedObject is Army) |
183 { | |
184 WarFoundryCore.CurrentArmy = (Army)loadedObject; | |
185 logger.InfoFormat("Loaded army from {0}", file.FullName); | |
186 } | |
113 | 187 else |
5 | 188 { |
113 | 189 if (loadedObject is GameSystem) |
190 { | |
191 WarFoundryCore.CurrentGameSystem = (GameSystem)loadedObject; | |
192 logger.InfoFormat("Loaded game system from {0}", file.FullName); | |
193 } | |
194 | |
5 | 195 } |
0 | 196 } |
197 } | |
198 catch (InvalidFileException ex) | |
199 { | |
200 //TODO: show error dialog | |
201 logger.Error(ex); | |
202 } | |
203 } | |
204 else | |
205 { | |
206 string gameSystemID = Preferences.GetStringProperty("currSystem"); | |
207 | |
113 | 208 if (gameSystemID != null && !"".Equals(gameSystemID)) |
0 | 209 { |
210 logger.Debug("Attempting to load current game system from properties"); | |
211 GameSystem sys = WarFoundryLoader.GetDefault().GetGameSystem(gameSystemID); | |
21 | 212 |
113 | 213 if (sys != null) |
0 | 214 { |
215 WarFoundryCore.CurrentGameSystem = sys; | |
216 logger.InfoFormat("Loaded game system {0} from properties", gameSystemID); | |
217 } | |
218 } | |
219 } | |
220 } | |
21 | 221 |
113 | 222 protected override void Translate() |
223 { | |
224 base.Translate(); | |
225 SetAppTitle(); | |
226 treeUnits.GetColumn(0).Title = Translation.GetTranslation("armyCategoryColumnTitle", "categories"); | |
227 RebuildUndoRedoMenus(); | |
228 } | |
229 | |
230 private void Retranslate() | |
32
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
231 { |
113 | 232 Translate(); |
233 } | |
234 | |
235 public override void Dispose() | |
236 { | |
237 Translation.TranslationChanged -= Retranslate; | |
238 base.Dispose(); | |
239 } | |
240 | |
241 private void FileLoadingFinished(List<FileLoadFailure> failures) | |
242 { | |
243 foreach (FileLoadFailure failure in failures) | |
32
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
244 { |
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
245 logger.Warn("Failed to load " + failure.FailedFile.FullName + ": " + failure.Message); |
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
246 } |
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
247 } |
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
248 |
0 | 249 private void RenderCategoryTreeObjectName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) |
250 { | |
251 object o = model.GetValue(iter, 0); | |
21 | 252 |
0 | 253 if (o is ArmyCategory) |
254 { | |
255 ArmyCategory c = (ArmyCategory)o; | |
256 string name = ""; | |
21 | 257 |
32
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
258 if (!Preferences.GetBooleanProperty("ShowCatPercentage")) |
0 | 259 { |
32
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
260 name = Translation.GetTranslation("categoryTreeCatName", "{0} - {1}pts", c.Name, c.Points); |
0 | 261 } |
262 else | |
263 { | |
32
eab45344cd56
Re #172: Add GTK# UI for showing debugging messages
IBBoard <dev@ibboard.co.uk>
parents:
30
diff
changeset
|
264 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)); |
0 | 265 } |
21 | 266 |
0 | 267 (cell as CellRendererText).Text = name; |
268 } | |
113 | 269 else |
0 | 270 { |
113 | 271 if (o is WFObjects.Unit) |
272 { | |
273 WFObjects.Unit u = (WFObjects.Unit)o; | |
274 string name = Translation.GetTranslation("unitTreeCatName", "{0} - {1}pts", u.Name, u.Points); | |
275 (cell as CellRendererText).Text = name; | |
276 } | |
277 | |
0 | 278 } |
279 } | |
21 | 280 |
0 | 281 private void OnWindowDestroyed(object source, EventArgs args) |
282 { | |
283 logger.Info("Exiting"); | |
284 Application.Quit(); | |
285 } | |
21 | 286 |
0 | 287 private void OnUnitNameChanged(WarFoundryObject val, string oldValue, string newValue) |
288 { | |
113 | 289 WFObjects.Unit unit = (WFObjects.Unit)val; |
290 logger.DebugFormat("Unit name changed for {0} - now called {1}", unit.ID, unit.Name); | |
0 | 291 UnitDisplayWidget widget; |
292 unitToWidgetMap.TryGetValue(unit, out widget); | |
113 | 293 |
294 if (widget != null) | |
0 | 295 { |
113 | 296 unitsNotebook.SetTabLabel(widget, NotebookUtil.CreateNotebookTabLabelWithClose(unitsNotebook, widget, unit.Name)); |
0 | 297 } |
113 | 298 |
299 treeUnits.QueueDraw(); | |
0 | 300 } |
21 | 301 |
0 | 302 private void OnUnitAdded(WarFoundryObject val) |
303 { | |
113 | 304 WFObjects.Unit unit = (WFObjects.Unit)val; |
305 unit.NameChanged += UnitNameChangedMethod; | |
306 unit.PointsValueChanged += HandleUnitPointsValueChanged; | |
0 | 307 AddUnitToTree(unit); |
308 } | |
21 | 309 |
113 | 310 private void HandleUnitPointsValueChanged(WarFoundryObject obj, double oldValue, double newValue) |
311 { | |
312 treeUnits.QueueDraw(); | |
313 } | |
314 | |
315 private void AddUnitToTree(WFObjects.Unit unit) | |
0 | 316 { |
317 TreeStore model = (TreeStore)treeUnits.Model; | |
318 TreeIter iter; | |
319 model.GetIterFirst(out iter); | |
21 | 320 |
0 | 321 do |
322 { | |
323 object obj = model.GetValue(iter, 0); | |
21 | 324 |
0 | 325 if (obj is ArmyCategory) |
326 { | |
327 ArmyCategory cat = (ArmyCategory)obj; | |
21 | 328 |
0 | 329 if (cat.Equals(unit.Category)) |
330 { | |
331 model.AppendValues(iter, unit); | |
332 TreePath path = model.GetPath(iter); | |
333 treeUnits.ExpandToPath(path); | |
334 } | |
335 } | |
336 } | |
337 while (model.IterNext(ref iter)); | |
338 } | |
21 | 339 |
0 | 340 private void OnUnitRemoved(WarFoundryObject obj) |
341 { | |
113 | 342 WFObjects.Unit unit = (WFObjects.Unit)obj; |
343 unit.NameChanged -= UnitNameChangedMethod; | |
0 | 344 RemoveUnitFromTree(unit); |
113 | 345 RemoveUnitTab(unit); |
0 | 346 } |
21 | 347 |
113 | 348 private void RemoveUnitFromTree(WFObjects.Unit unit) |
0 | 349 { |
350 TreeStore model = (TreeStore)treeUnits.Model; | |
351 TreeIter iter; | |
352 model.GetIterFirst(out iter); | |
353 bool removed = false; | |
21 | 354 |
0 | 355 do |
356 { | |
357 object obj = model.GetValue(iter, 0); | |
21 | 358 |
0 | 359 if (obj is ArmyCategory) |
360 { | |
361 ArmyCategory cat = (ArmyCategory)obj; | |
21 | 362 |
0 | 363 if (unit.Category == null || cat.Equals(unit.Category)) |
364 { | |
365 TreeIter innerIter; | |
366 model.IterChildren(out innerIter, iter); | |
21 | 367 |
0 | 368 do |
369 { | |
370 object innerObj = model.GetValue(innerIter, 0); | |
21 | 371 |
0 | 372 if (unit.Equals(innerObj)) |
373 { | |
374 model.Remove(ref innerIter); | |
375 removed = true; | |
376 break; | |
377 } | |
378 } | |
379 while (model.IterNext(ref innerIter)); | |
21 | 380 |
0 | 381 if (removed) |
382 { | |
383 break; | |
384 } | |
385 } | |
386 } | |
387 } | |
388 while (model.IterNext(ref iter)); | |
389 } | |
21 | 390 |
113 | 391 private void RemoveUnitTab(WFObjects.Unit unit) |
392 { | |
393 UnitDisplayWidget widget = DictionaryUtils.GetValue(unitToWidgetMap, unit); | |
394 | |
395 if (widget != null) | |
396 { | |
397 unitsNotebook.Remove(widget); | |
398 } | |
399 } | |
400 | |
0 | 401 private void OnPointsValueChanged(WarFoundryObject obj, double before, double after) |
402 { | |
113 | 403 SetPointsPanelText(); |
0 | 404 } |
21 | 405 |
0 | 406 private void OnFailedUnitRequirement(List<FailedUnitRequirement> failedRequirement) |
407 { | |
408 //Show error message in panel | |
409 } | |
21 | 410 |
0 | 411 public Preferences Preferences |
412 { | |
413 get { return preferences; } | |
21 | 414 set { preferences = value; } |
0 | 415 } |
21 | 416 |
113 | 417 protected void OnDeleteEvent(object sender, DeleteEventArgs a) |
0 | 418 { |
113 | 419 Application.Quit(); |
0 | 420 a.RetVal = true; |
421 } | |
422 | |
423 protected virtual void OnExitActivated(object sender, System.EventArgs e) | |
424 { | |
425 Application.Quit(); | |
426 } | |
427 | |
428 protected virtual void OnCreateArmyActivated(object sender, System.EventArgs e) | |
429 { | |
430 CreateNewArmy(); | |
431 } | |
432 | |
433 protected virtual void OnReloadFilesActivated(object sender, System.EventArgs e) | |
434 { | |
113 | 435 WarFoundryLoader.GetDefault().LoadFiles(); |
0 | 436 } |
437 | |
438 protected virtual void OnSaveArmyAsActivated(object sender, System.EventArgs e) | |
439 { | |
440 SaveCurrentArmyAs(); | |
441 } | |
442 | |
443 protected virtual void OnCloseArmyActivated(object sender, System.EventArgs e) | |
444 { | |
445 CloseCurrentArmy(); | |
446 } | |
447 | |
448 protected virtual void OnOpenArmyActivated(object sender, System.EventArgs e) | |
449 { | |
450 OpenArmy(); | |
451 } | |
452 | |
453 protected virtual void OnSaveArmyActivated(object sender, System.EventArgs e) | |
454 { | |
33 | 455 SaveCurrentArmyOrSaveAs(); |
0 | 456 } |
21 | 457 |
0 | 458 protected virtual void OnAddUnitActivated(object sender, System.EventArgs e) |
459 { | |
460 if (sender is ToolButton) | |
461 { | |
113 | 462 ToolButton toolButton = (ToolButton)sender; |
463 Category cat = (Category)toolButton.Data["Category"]; | |
21 | 464 |
113 | 465 if (cat != null) |
0 | 466 { |
467 logger.DebugFormat("Show FrmNewUnit for {0}", cat.Name); | |
468 FrmNewUnit newUnit = new FrmNewUnit(WarFoundryCore.CurrentArmy.Race, cat, WarFoundryCore.CurrentArmy); | |
469 ResponseType response = (ResponseType)newUnit.Run(); | |
470 newUnit.Hide(); | |
21 | 471 |
113 | 472 if (response == ResponseType.Ok) |
0 | 473 { |
14
abbf8a3ac431
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
474 CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(cat)); |
0 | 475 commandStack.Execute(cmd); |
113 | 476 ShowUnitWidget(cmd.Unit); |
0 | 477 } |
21 | 478 |
0 | 479 newUnit.Dispose(); |
480 } | |
481 } | |
482 } | |
21 | 483 |
13 | 484 public CommandStack CommandStack |
485 { | |
486 get { return commandStack; } | |
0 | 487 } |
21 | 488 |
0 | 489 private void SetAppTitle() |
490 { | |
113 | 491 if (WarFoundryCore.CurrentArmy != null) |
0 | 492 { |
493 Title = AppTitle + " - " + WarFoundryCore.CurrentGameSystem.Name + " - " + WarFoundryCore.CurrentArmy.Name; | |
494 } | |
495 else | |
496 { | |
113 | 497 if (WarFoundryCore.CurrentGameSystem != null) |
498 { | |
499 Title = AppTitle + " - " + WarFoundryCore.CurrentGameSystem.Name; | |
500 } | |
501 else | |
502 { | |
503 Title = AppTitle; | |
504 } | |
505 | |
0 | 506 } |
507 } | |
21 | 508 |
0 | 509 private void OnGameSystemChanged(GameSystem oldSys, GameSystem newSys) |
510 { | |
511 system = newSys; | |
512 SetAppTitle(); | |
513 RemoveCategoryButtons(); | |
21 | 514 |
113 | 515 if (system != null) |
0 | 516 { |
517 AddCategoryButtons(system.Categories); | |
518 } | |
519 } | |
21 | 520 |
0 | 521 private void OnArmyChanged(Army oldArmy, Army newArmy) |
522 { | |
523 loadedArmyPath = null; | |
524 SetAppTitle(); | |
525 SetArmyTree(newArmy); | |
21 | 526 |
113 | 527 if (oldArmy != null) |
13 | 528 { |
113 | 529 oldArmy.UnitAdded -= UnitAddedMethod; |
530 oldArmy.UnitRemoved -= UnitRemovedMethod; | |
531 oldArmy.PointsValueChanged -= PointsValueChangedMethod; | |
532 oldArmy.FailedRequirement -= FailedUnitRequirementMethod; | |
0 | 533 } |
21 | 534 |
0 | 535 unitToWidgetMap.Clear(); |
21 | 536 |
0 | 537 while (unitsNotebook.NPages > 0) |
538 { | |
539 unitsNotebook.RemovePage(0); | |
540 } | |
21 | 541 |
113 | 542 if (newArmy == null) |
13 | 543 { |
544 DisableCategoryButtons(); | |
545 } | |
546 else | |
547 { | |
113 | 548 newArmy.UnitAdded += UnitAddedMethod; |
549 newArmy.UnitRemoved += UnitRemovedMethod; | |
550 newArmy.PointsValueChanged += PointsValueChangedMethod; | |
551 newArmy.FailedRequirement += FailedUnitRequirementMethod; | |
13 | 552 //TODO: Clear all buttons |
553 EnableCategoryButtons(); | |
554 | |
555 if (newArmy.Race.HasCategoryOverrides()) | |
556 { | |
557 RemoveCategoryButtons(); | |
558 AddCategoryButtons(newArmy.Race.Categories); | |
559 } | |
0 | 560 } |
21 | 561 |
113 | 562 bool nonNullNewArmy = (newArmy != null); |
563 miCloseArmy.Sensitive = nonNullNewArmy; | |
564 miSaveArmyAs.Sensitive = nonNullNewArmy; | |
565 miExportArmyAs.Sensitive = nonNullNewArmy; | |
566 hpaned2.Visible = nonNullNewArmy; | |
33 | 567 loadedArmyPath = null; |
0 | 568 //New army has no changes, so we can't save it |
569 miSaveArmy.Sensitive = false; | |
113 | 570 bttnSaveArmy.Sensitive = false; |
13 | 571 |
572 CommandStack.Reset(); | |
0 | 573 SetPointsPanelText(); |
574 } | |
21 | 575 |
0 | 576 private void SetArmyTree(Army army) |
577 { | |
578 logger.Debug("Resetting tree"); | |
579 TreeStore store = (TreeStore)treeUnits.Model; | |
580 store.Clear(); | |
581 TreeIter iter; | |
21 | 582 |
113 | 583 if (army != null) |
0 | 584 { |
585 logger.Debug("Loading in categories to tree"); | |
21 | 586 |
0 | 587 foreach (ArmyCategory cat in army.Categories) |
588 { | |
589 logger.DebugFormat("Append category {0}", cat.Name); | |
590 iter = store.AppendValues(cat); | |
21 | 591 |
113 | 592 foreach (WFObjects.Unit unit in cat.GetUnits()) |
0 | 593 { |
594 store.AppendValues(iter, unit); | |
21 | 595 } |
0 | 596 } |
21 | 597 |
0 | 598 logger.Debug("Finished loading tree categories"); |
599 } | |
600 } | |
21 | 601 |
0 | 602 private void DisableCategoryButtons() |
603 { | |
604 SetCategoryButtonsSensitive(false); | |
605 } | |
21 | 606 |
0 | 607 private void EnableCategoryButtons() |
608 { | |
609 SetCategoryButtonsSensitive(true); | |
610 } | |
21 | 611 |
0 | 612 private void SetCategoryButtonsSensitive(bool state) |
613 { | |
614 int toolbarButtonCount = toolbar.Children.Length - 1; | |
113 | 615 logger.Debug("Last button index: " + toolbarButtonCount); |
21 | 616 |
0 | 617 for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--) |
618 { | |
619 logger.DebugFormat("Setting button {0} state to {1}", i, state); | |
620 toolbar.Children[i].Sensitive = state; | |
621 } | |
622 } | |
21 | 623 |
0 | 624 private void RemoveCategoryButtons() |
625 { | |
626 int toolbarButtonCount = toolbar.Children.Length - 1; | |
21 | 627 |
0 | 628 for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--) |
629 { | |
630 toolbar.Remove(toolbar.Children[i]); | |
631 } | |
632 } | |
21 | 633 |
0 | 634 private void AddCategoryButtons(Category[] cats) |
635 { | |
113 | 636 if (cats != null && cats.Length > 0) |
0 | 637 { |
638 logger.DebugFormat("Toolbar button count: {0}. Adding {1} categories.", toolbar.Children.Length, cats.Length); | |
21 | 639 |
0 | 640 foreach (Category cat in cats) |
641 { | |
642 ToolButton button = new ToolButton("gtk-add"); | |
643 button.Label = cat.Name; | |
113 | 644 button.TooltipText = Translation.GetTranslation("bttnCreateFromCat", "{0}", cat.Name); |
645 button.Data["Category"] = cat; | |
646 button.Clicked += new System.EventHandler(OnAddUnitActivated); | |
0 | 647 toolbar.Insert(button, -1); |
648 } | |
649 } | |
21 | 650 |
113 | 651 toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX].Visible = cats != null && cats.Length > 0; |
21 | 652 |
0 | 653 toolbar.ShowAll(); |
654 } | |
21 | 655 |
0 | 656 private void SetPointsPanelText() |
657 { | |
113 | 658 if (WarFoundryCore.CurrentArmy != null) |
659 { | |
660 lblTotalPoints.Text = Translation.GetTranslation("statusPanelPoints", "{0}pts of {1} pts", WarFoundryCore.CurrentArmy.Points, WarFoundryCore.CurrentArmy.MaxPoints); | |
661 } | |
662 else | |
663 { | |
664 lblTotalPoints.Text = ""; | |
665 } | |
0 | 666 } |
21 | 667 |
13 | 668 private void commandStack_CommandStackUpdated() |
669 { | |
670 undoMenuButton.Sensitive = commandStack.CanUndo(); | |
671 miUndo.Sensitive = undoMenuButton.Sensitive; | |
672 redoMenuButton.Sensitive = commandStack.CanRedo(); | |
673 miRedo.Sensitive = redoMenuButton.Sensitive; | |
113 | 674 |
675 RebuildUndoRedoMenus(); | |
676 | |
677 bttnSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null; | |
678 miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null; | |
679 } | |
680 | |
681 private void RebuildUndoRedoMenus() | |
682 { | |
0 | 683 int redoLength = commandStack.RedoLength; |
113 | 684 int maxRedo = Math.Min(10, redoLength); |
685 | |
13 | 686 if (redoLength > 0) |
687 { | |
113 | 688 Menu menu = new Menu(); |
13 | 689 Command com; |
690 MenuItem mi; | |
113 | 691 |
13 | 692 for (int i = 0; i < maxRedo; i++) |
693 { | |
113 | 694 com = commandStack.PeekRedoCommand(i + 1); |
695 | |
13 | 696 if (com == null) |
697 { | |
21 | 698 break; |
13 | 699 } |
113 | 700 |
13 | 701 mi = new MenuItem(com.Description); |
113 | 702 mi.Activated += new EventHandler(RedoMenuActivated); |
703 menu.Append(mi); | |
13 | 704 } |
113 | 705 |
706 menu.ShowAll(); | |
707 redoMenuButton.Menu = menu; | |
13 | 708 } |
113 | 709 else |
13 | 710 { |
113 | 711 redoMenuButton.Menu = null; |
13 | 712 } |
113 | 713 |
13 | 714 int undoLength = commandStack.UndoLength; |
113 | 715 int maxUndo = Math.Min(10, undoLength); |
716 | |
13 | 717 if (undoLength > 0) |
718 { | |
113 | 719 Menu menu = new Menu(); |
13 | 720 Command com; |
721 MenuItem mi; | |
113 | 722 |
13 | 723 for (int i = 0; i < maxUndo; i++) |
724 { | |
113 | 725 com = commandStack.PeekUndoCommand(i + 1); |
726 | |
13 | 727 if (com == null) |
728 { | |
21 | 729 break; |
13 | 730 } |
113 | 731 |
13 | 732 mi = new MenuItem(com.UndoDescription); |
113 | 733 mi.Activated += new EventHandler(UndoMenuActivated); |
734 menu.Add(mi); | |
735 } | |
736 | |
737 menu.ShowAll(); | |
738 undoMenuButton.Menu = menu; | |
739 } | |
740 else | |
741 { | |
742 undoMenuButton.Menu = null; | |
743 } | |
744 } | |
745 | |
746 private void RedoMenuActivated(object sender, EventArgs e) | |
747 { | |
748 if (sender is MenuItem) | |
749 { | |
750 MenuItem item = (MenuItem)sender; | |
751 //we know it's an redo menu item so find it's index and redo everything | |
752 | |
753 int max = Arrays.IndexOf(((Menu)redoMenuButton.Menu).Children, item); | |
754 | |
755 if (max >= 0) | |
756 { | |
757 for (int i = 0; i <= max; i++) | |
758 { | |
759 commandStack.Redo(); | |
760 } | |
13 | 761 } |
762 } | |
113 | 763 } |
13 | 764 |
113 | 765 private void UndoMenuActivated(object sender, EventArgs e) |
766 { | |
767 if (sender is MenuItem) | |
13 | 768 { |
113 | 769 |
770 MenuItem item = (MenuItem)sender; | |
771 //we know it's an undo menu item so find it's index and undo everything | |
772 | |
773 int max = Arrays.IndexOf(((Menu)undoMenuButton.Menu).Children, item); | |
774 | |
775 if (max >= 0) | |
776 { | |
777 for (int i = 0; i <= max; i++) | |
778 { | |
779 commandStack.Undo(); | |
780 } | |
781 } | |
0 | 782 } |
13 | 783 } |
784 | |
785 private bool SaveCurrentArmyOrSaveAs() | |
786 { | |
33 | 787 if (loadedArmyPath != null) |
13 | 788 { |
789 return SaveCurrentArmy(); | |
790 } | |
21 | 791 else |
13 | 792 { |
793 return SaveCurrentArmyAs(); | |
794 } | |
0 | 795 } |
21 | 796 |
0 | 797 private bool OpenArmy() |
798 { | |
113 | 799 string title = Translation.GetTranslation("openArmyDialog", "open army"); |
800 string cancelText = Translation.GetTranslation("bttnCancel", "cancel"); | |
801 string openText = Translation.GetTranslation("bttnOpen", "open"); | |
802 FileChooserDialog fileDialog = new FileChooserDialog(title, this, FileChooserAction.Open, cancelText, ResponseType.Cancel, openText, ResponseType.Accept); | |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
803 FileFilter filter = new FileFilter(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
804 filter.AddPattern("*.army"); |
113 | 805 filter.Name = Translation.GetTranslation("armyFileFilter", "WarFoundry .army files"); |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
806 fileDialog.AddFilter(filter); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
807 int response = fileDialog.Run(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
808 string filePath = null; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
809 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
810 if (response == (int)ResponseType.Accept) |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
811 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
812 filePath = fileDialog.Filename; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
813 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
814 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
815 fileDialog.Hide(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
816 fileDialog.Dispose(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
817 |
0 | 818 bool success = false; |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
819 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
820 if (filePath != null) |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
821 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
822 FileInfo file = new FileInfo(filePath); |
113 | 823 Army army = null; |
824 | |
825 try | |
826 { | |
827 army = WarFoundryLoader.GetDefault().LoadArmy(file); | |
828 } | |
829 catch (Exception ex) | |
830 { | |
831 logger.Error("Error while loading army file " + filePath, ex); | |
832 } | |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
833 |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
834 if (army != null) |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
835 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
836 logger.Debug("Loaded army " + army.ID); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
837 success = true; |
33 | 838 WarFoundryCore.CurrentArmy = army; |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
839 loadedArmyPath = filePath; |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
840 logger.Debug("Army loading complete"); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
841 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
842 else |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
843 { |
113 | 844 MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, Translation.GetTranslation("OpenFailed", "File open failed. Please check log file")); |
845 dialog.Title = Translation.GetTranslation("OpenFailedTitle", "File open failed"); | |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
846 dialog.Run(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
847 dialog.Hide(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
848 dialog.Dispose(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
849 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
850 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
851 else |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
852 { |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
853 logger.Debug("Army open requested but cancelled"); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
854 } |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
855 |
0 | 856 return success; |
13 | 857 } |
858 | |
859 private bool SaveCurrentArmy() | |
0 | 860 { |
861 bool success = false; | |
21 | 862 |
113 | 863 if (loadedArmyPath != null) |
0 | 864 { |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
865 success = SaveArmyToPath(WarFoundryCore.CurrentArmy, loadedArmyPath); |
0 | 866 } |
21 | 867 |
13 | 868 return success; |
869 } | |
870 | |
871 private bool SaveCurrentArmyAs() | |
872 { | |
113 | 873 string title = Translation.GetTranslation("saveArmyDialog", "save army"); |
874 string cancelText = Translation.GetTranslation("bttnCancel", "cancel"); | |
875 string saveText = Translation.GetTranslation("bttnSave", "save"); | |
876 FileChooserDialog fileDialog = new FileChooserDialog(title, this, FileChooserAction.Save, cancelText, ResponseType.Cancel, saveText, ResponseType.Accept); | |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
877 FileFilter filter = new FileFilter(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
878 filter.AddPattern("*.army"); |
113 | 879 filter.Name = Translation.GetTranslation("armyFileFilter", "WarFoundry .army files"); |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
880 fileDialog.AddFilter(filter); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
881 int response = fileDialog.Run(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
882 string filePath = null; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
883 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
884 if (response == (int)ResponseType.Accept) |
13 | 885 { |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
886 filePath = fileDialog.Filename; |
13 | 887 } |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
888 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
889 fileDialog.Hide(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
890 fileDialog.Dispose(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
891 |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
892 return SaveArmyToPath(WarFoundryCore.CurrentArmy, filePath); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
893 } |
113 | 894 |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
895 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
|
896 { |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
897 bool success = false; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
898 |
113 | 899 if (filePath != null) |
13 | 900 { |
113 | 901 bool saveSuccess = false; |
902 | |
903 try | |
904 { | |
905 saveSuccess = WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, filePath); | |
906 } | |
907 catch (Exception ex) | |
908 { | |
909 logger.Error("Error while saving army file to " + filePath, ex); | |
910 } | |
911 | |
912 if (saveSuccess) | |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
913 { |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
914 miSaveArmy.Sensitive = false; |
113 | 915 bttnSaveArmy.Sensitive = false; |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
916 CommandStack.setCleanMark(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
917 loadedArmyPath = filePath; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
918 success = true; |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
919 } |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
920 else |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
921 { |
113 | 922 MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, Translation.GetTranslation("SaveFailed", "File save failed. Please check log file")); |
923 dialog.Title = Translation.GetTranslation("SaveFailedTitle", "File save failed"); | |
25
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
924 dialog.Run(); |
01ddadfa9653
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
925 dialog.Hide(); |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
926 dialog.Dispose(); |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
927 } |
21 | 928 } |
23
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
929 //else user cancelled |
d661cb257511
Re #86: Initial GTK# GUI (because of Re #53)
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
930 |
21 | 931 return success; |
0 | 932 } |
21 | 933 |
13 | 934 private bool CloseCurrentArmy() |
935 { | |
113 | 936 if (WarFoundryCore.CurrentArmy != null) |
13 | 937 { |
938 bool canClose = false; | |
939 | |
940 if (CommandStack.IsDirty()) | |
0 | 941 { |
113 | 942 string message = Translation.GetTranslation("SaveChangesQuestion", "Save changes to army before closing?", WarFoundryCore.CurrentArmy.Name); |
943 MessageDialog dia = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.None, message); | |
944 dia.AddButton(Translation.GetTranslation("bttnDiscard", "lose changes"), ResponseType.No); | |
945 Button button = (Button)dia.AddButton(Translation.GetTranslation("bttnCancel", "cancel"), ResponseType.Cancel); | |
946 button.Image = new Image("gtk-cancel", IconSize.Button); | |
947 button = (Button)dia.AddButton(Translation.GetTranslation("bttnSave", "save"), ResponseType.Yes); | |
948 button.Image = new Image("gtk-save", IconSize.Button); | |
949 dia.Title = Translation.GetTranslation("SaveChangesTitle", "Save changes?"); | |
0 | 950 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
|
951 dia.Hide(); |
83c8945edac2
Re #143: GTK "Army has been modified" dialog has no buttons
IBBoard <dev@ibboard.co.uk>
parents:
26
diff
changeset
|
952 dia.Dispose(); |
0 | 953 |
13 | 954 if (dr == ResponseType.Yes) |
0 | 955 { |
956 //They want to save so try to save it or prompt for save as | |
13 | 957 //If they cancel the save as then assume they don't want to close |
958 canClose = SaveCurrentArmyOrSaveAs(); | |
959 } | |
0 | 960 else |
961 { | |
113 | 962 if (dr == ResponseType.No) |
963 { | |
964 //They don't care about their changes | |
965 canClose = true; | |
966 } | |
967 else | |
968 { | |
969 //Assume cancel or close with the X button | |
970 canClose = false; | |
971 } | |
972 | |
0 | 973 } |
13 | 974 } |
975 else | |
976 { | |
977 //Nothing has changed so we can safely close | |
978 canClose = true; | |
979 } | |
980 | |
981 if (canClose) | |
0 | 982 { |
13 | 983 //do close |
984 WarFoundryCore.CurrentArmy = null; | |
985 return true; | |
986 } | |
987 else | |
988 { | |
989 return false; | |
990 } | |
991 } | |
992 else | |
993 { | |
994 //pretend we succeeded | |
995 return true; | |
996 } | |
997 } | |
998 | |
999 private void CreateNewArmy() | |
0 | 1000 { |
1001 logger.Debug("Create new army"); | |
1002 FrmNewArmy newArmy = new FrmNewArmy(WarFoundryCore.CurrentGameSystem); | |
1003 ResponseType type = (ResponseType)newArmy.Run(); | |
1004 newArmy.Hide(); | |
21 | 1005 |
0 | 1006 if (type == ResponseType.Ok) |
13 | 1007 { |
1008 if (CloseCurrentArmy()) | |
0 | 1009 { |
1010 WarFoundryCore.CurrentArmy = new Army(newArmy.SelectedRace, newArmy.ArmyName, newArmy.ArmySize); | |
1011 } | |
1012 } | |
1013 else | |
1014 { | |
1015 logger.Debug("Create new army cancelled"); | |
1016 } | |
21 | 1017 |
13 | 1018 newArmy.Destroy(); |
0 | 1019 } |
21 | 1020 |
113 | 1021 protected virtual void undoTBButtonActivated(object sender, System.EventArgs e) |
0 | 1022 { |
1023 CommandStack.Undo(); | |
1024 } | |
1025 | |
113 | 1026 protected virtual void redoTBButtonActivated(object sender, System.EventArgs e) |
0 | 1027 { |
1028 CommandStack.Redo(); | |
1029 } | |
1030 | |
113 | 1031 protected virtual void saveTBButtonActivated(object sender, System.EventArgs e) |
0 | 1032 { |
33 | 1033 SaveCurrentArmyOrSaveAs(); |
0 | 1034 } |
1035 | |
113 | 1036 protected virtual void openTBButtonActivated(object sender, System.EventArgs e) |
0 | 1037 { |
1038 OpenArmy(); | |
1039 } | |
1040 | |
113 | 1041 protected virtual void newTBButtonActivated(object sender, System.EventArgs e) |
0 | 1042 { |
1043 CreateNewArmy(); | |
1044 } | |
1045 | |
113 | 1046 protected virtual void ArmyRowActivated(object o, Gtk.RowActivatedArgs args) |
0 | 1047 { |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1048 object obj = TreeUtils.GetItemAtPath(treeUnits, args.Path); |
21 | 1049 |
113 | 1050 if (obj is WFObjects.Unit) |
0 | 1051 { |
113 | 1052 WFObjects.Unit unit = (WFObjects.Unit)obj; |
1053 ShowUnitWidget(unit); | |
0 | 1054 } |
1055 } | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
1056 |
113 | 1057 private void ShowUnitWidget(WFObjects.Unit unit) |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
1058 { |
113 | 1059 UnitDisplayWidget widget; |
1060 unitToWidgetMap.TryGetValue(unit, out widget); | |
1061 | |
1062 if (widget != null) | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
1063 { |
113 | 1064 logger.DebugFormat("Selecting existing page for " + unit.Name); |
1065 unitsNotebook.Page = unitsNotebook.PageNum(widget); | |
1066 } | |
1067 else | |
1068 { | |
1069 widget = new UnitDisplayWidget(unit, CommandStack); | |
1070 logger.Debug("Adding page for " + unit.Name); | |
1071 unitToWidgetMap[unit] = widget; | |
1072 int pageNum = NotebookUtil.AddPageToNotebookWithCloseButton(unitsNotebook, widget, unit.Name); | |
1073 logger.Debug("Page added at index " + pageNum); | |
1074 unitsNotebook.ShowAll(); | |
1075 unitsNotebook.CurrentPage = pageNum; | |
1076 unitsNotebook.SetTabReorderable(widget, true); | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
1077 } |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
1078 } |
28 | 1079 |
113 | 1080 protected virtual void OnMiExportAsBasicHtmlActivated(object sender, System.EventArgs e) |
28 | 1081 { |
113 | 1082 string exportArmyTitle = Translation.GetTranslation("exportBasicHtmlDialogTitle", "export army"); |
1083 string cancelText = Translation.GetTranslation("exportBasicHtmlCancel", "cancel"); | |
1084 string exportText = Translation.GetTranslation("exportBasicHtmlExport", "export"); | |
1085 FileChooserDialog fileDialog = new FileChooserDialog(exportArmyTitle, this, FileChooserAction.Save, cancelText, ResponseType.Cancel, exportText, ResponseType.Accept); | |
28 | 1086 FileFilter filter = new FileFilter(); |
1087 filter.AddPattern("*.html"); | |
113 | 1088 filter.Name = Translation.GetTranslation("exportBasicHtmlHtmlFilter", "HTML pages (*.html)"); |
28 | 1089 fileDialog.AddFilter(filter); |
1090 int response = fileDialog.Run(); | |
1091 string filePath = null; | |
1092 | |
1093 if (response == (int)ResponseType.Accept) | |
1094 { | |
1095 filePath = fileDialog.Filename; | |
1096 } | |
1097 | |
1098 fileDialog.Hide(); | |
1099 fileDialog.Dispose(); | |
1100 | |
1101 if (filePath != null) | |
1102 { | |
1103 Army army = WarFoundryCore.CurrentArmy; | |
1104 logger.DebugFormat("Exporting {0} to {1} as basic HTML", army.Name, filePath); | |
1105 WarFoundryHtmlExporter.GetDefault().ExportArmy(army, filePath); | |
1106 } | |
1107 } | |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1108 |
113 | 1109 protected virtual void OnTreeUnitsPopupMenu(object o, Gtk.PopupMenuArgs args) |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1110 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1111 object selectedItem = TreeUtils.GetSelectedItem(treeUnits); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1112 |
113 | 1113 if (selectedItem is WFObjects.Unit) |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1114 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1115 Menu menu = new Menu(); |
113 | 1116 ImageMenuItem delete = new ImageMenuItem(Translation.GetTranslation("menuRemoveUnit", "remove unit")); |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1117 delete.Image = new Gtk.Image(Stock.Delete, IconSize.Menu); |
113 | 1118 delete.Activated += new EventHandler(OnUnitDelete); |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1119 delete.Data["unit"] = selectedItem; |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1120 menu.Append(delete); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1121 menu.ShowAll(); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1122 menu.Popup(); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1123 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1124 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1125 |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1126 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
|
1127 { |
113 | 1128 RemoveUnitCommand command = new RemoveUnitCommand((WFObjects.Unit)((ImageMenuItem)o).Data["unit"]); |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1129 commandStack.Execute(command); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1130 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1131 |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1132 [GLib.ConnectBefore] |
113 | 1133 |
1134 protected virtual void UnitTreeButtonPressed(object o, Gtk.ButtonPressEventArgs args) | |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1135 { |
113 | 1136 TreePath path; |
1137 treeUnits.GetPathAtPos((int)args.Event.X, (int)args.Event.Y, out path); | |
1138 | |
1139 if (!treeUnits.Selection.PathIsSelected(path)) | |
1140 { | |
1141 treeUnits.Selection.SelectPath(path); | |
1142 } | |
1143 | |
30
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1144 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
|
1145 { |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1146 OnTreeUnitsPopupMenu(o, null); |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1147 } |
5fafbb1b4592
Re #145: Add UI to remove units from army
IBBoard <dev@ibboard.co.uk>
parents:
29
diff
changeset
|
1148 } |
113 | 1149 |
1150 protected virtual void NotebookPageRemoved(object o, Gtk.RemovedArgs args) | |
1151 { | |
1152 Widget widget = args.Widget; | |
1153 | |
1154 if (widget is UnitDisplayWidget) | |
1155 { | |
1156 unitToWidgetMap.Remove(((UnitDisplayWidget)widget).Unit); | |
1157 } | |
1158 } | |
1159 | |
1160 protected virtual void HelpAboutActivated(object sender, System.EventArgs e) | |
1161 { | |
1162 FrmAbout form = FrmAbout.GetForm(); | |
1163 form.Run(); | |
1164 form.Hide(); | |
1165 } | |
1166 | |
1167 protected virtual void miPreferencesClicked(object sender, System.EventArgs e) | |
1168 { | |
1169 FrmPreferences form = new FrmPreferences(Preferences); | |
1170 form.Run(); | |
1171 form.Hide(); | |
1172 } | |
1173 | |
1174 public override ICollection<Action> Actions | |
1175 { | |
1176 get | |
1177 { | |
1178 List<Action> actions = new List<Action>(); | |
1179 | |
1180 foreach (ActionGroup actionGroup in this.UIManager.ActionGroups) | |
1181 { | |
1182 actions.AddRange(actionGroup.ListActions()); | |
1183 } | |
1184 | |
1185 return actions; | |
1186 } | |
1187 } | |
0 | 1188 } |
1189 } |