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