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