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