diff FrmMainWindow.cs @ 98:fac0636ae13b

Re #308: Make GTK# UI translatable * Make undo/redo menus translate on language change * Organise English language file * Resolve warnings by adding in dialogs that use translated strings when equipment amounts are out of range
author IBBoard <dev@ibboard.co.uk>
date Sun, 02 Jan 2011 21:03:15 +0000
parents b4416ca69153
children 8777e4f64d8e
line wrap: on
line diff
--- a/FrmMainWindow.cs	Sun Jan 02 15:50:29 2011 +0000
+++ b/FrmMainWindow.cs	Sun Jan 02 21:03:15 2011 +0000
@@ -40,7 +40,6 @@
 		private Preferences preferences;
 		private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow));
 		private CommandStack commandStack;
-		private Dictionary<ToolButton, Category> categoryMap = new Dictionary<ToolButton, Category>();
 		private Dictionary<WFObjects.Unit, UnitDisplayWidget> unitToWidgetMap = new Dictionary<WFObjects.Unit,UnitDisplayWidget>();
 		private ObjectAddDelegate UnitAddedMethod;
 		private ObjectRemoveDelegate UnitRemovedMethod;
@@ -225,6 +224,7 @@
 			base.Translate();
 			SetAppTitle();
 			treeUnits.GetColumn(0).Title = Translation.GetTranslation("armyCategoryColumnTitle", "categories");
+			RebuildUndoRedoMenus();
 		}
 
 		private void Retranslate()
@@ -414,11 +414,6 @@
 			set { preferences = value; }
 		}
 
-		/*public AbstractNativeWarFoundryFactory Factory
-		{
-			get { return WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(Constants.ExecutablePath, factoryType); }
-		}*/
-
 		protected void OnDeleteEvent(object sender, DeleteEventArgs a)
 		{
 			Application.Quit();
@@ -464,8 +459,8 @@
 		{
 			if (sender is ToolButton)
 			{
-				Category cat = null;
-				categoryMap.TryGetValue((ToolButton)sender, out cat);
+				ToolButton toolButton = (ToolButton)sender;
+				Category cat = (Category)toolButton.Data["Category"];
 
 				if (cat != null)
 				{
@@ -634,8 +629,6 @@
 			{
 				toolbar.Remove(toolbar.Children[i]);
 			}
-
-			categoryMap.Clear();
 		}
 
 		private void AddCategoryButtons(Category[] cats)
@@ -649,8 +642,7 @@
 					ToolButton button = new ToolButton("gtk-add");
 					button.Label = cat.Name;
 					button.TooltipText = Translation.GetTranslation("bttnCreateFromCat", "{0}", cat.Name);
-					//TODO: See if we can associate data in some way, the same as we can with SWF. For now we just use the map.
-					categoryMap.Add(button, cat);
+					button.Data["Category"] = cat;
 					button.Clicked += new System.EventHandler(OnAddUnitActivated);
 					toolbar.Insert(button, -1);
 				}
@@ -672,26 +664,33 @@
 			miUndo.Sensitive = undoMenuButton.Sensitive;
 			redoMenuButton.Sensitive = commandStack.CanRedo();
 			miRedo.Sensitive = redoMenuButton.Sensitive;
+
+			RebuildUndoRedoMenus();
+
+			bttnSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
+			miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
+		}
+
+		private void RebuildUndoRedoMenus()
+		{
 			int redoLength = commandStack.RedoLength;
-
 			int maxRedo = Math.Min(10, redoLength);
 			
-			//TODO: Add tooltips
 			if (redoLength > 0)
 			{
 				Menu menu = new Menu();
 				Command com;
 				MenuItem mi;
-
+			
 				for (int i = 0; i < maxRedo; i++)
 				{
 					com = commandStack.PeekRedoCommand(i + 1);
-
+			
 					if (com == null)
 					{
 						break;
 					}
-
+			
 					mi = new MenuItem(com.Description);
 					mi.Activated += new EventHandler(RedoMenuActivated);
 					menu.Append(mi);
@@ -707,22 +706,22 @@
 			
 			int undoLength = commandStack.UndoLength;
 			int maxUndo = Math.Min(10, undoLength);
-
+			
 			if (undoLength > 0)
 			{
 				Menu menu = new Menu();
 				Command com;
 				MenuItem mi;
-
+			
 				for (int i = 0; i < maxUndo; i++)
 				{
 					com = commandStack.PeekUndoCommand(i + 1);
-
+			
 					if (com == null)
 					{
 						break;
 					}
-
+			
 					mi = new MenuItem(com.UndoDescription);
 					mi.Activated += new EventHandler(UndoMenuActivated);
 					menu.Add(mi);
@@ -735,9 +734,6 @@
 			{
 				undoMenuButton.Menu = null;
 			}
-
-			bttnSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
-			miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
 		}
 
 		private void RedoMenuActivated(object sender, EventArgs e)
@@ -793,10 +789,13 @@
 
 		private bool OpenArmy()
 		{
-			FileChooserDialog fileDialog = new FileChooserDialog("Open army", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
+			string title = Translation.GetTranslation("openArmyDialog", "open army");
+			string cancelText = Translation.GetTranslation("bttnCancel", "cancel");
+			string openText = Translation.GetTranslation("bttnOpen", "open");
+			FileChooserDialog fileDialog = new FileChooserDialog(title, this, FileChooserAction.Open, cancelText, ResponseType.Cancel, openText, ResponseType.Accept);
 			FileFilter filter = new FileFilter();
 			filter.AddPattern("*.army");
-			filter.Name = "WarFoundry Army files (*.army)";
+			filter.Name = Translation.GetTranslation("armyFileFilter", "WarFoundry .army files");
 			fileDialog.AddFilter(filter);
 			int response = fileDialog.Run();
 			string filePath = null;
@@ -814,7 +813,16 @@
 			if (filePath != null)
 			{
 				FileInfo file = new FileInfo(filePath);
-				Army army = WarFoundryLoader.GetDefault().LoadArmy(file);
+				Army army = null;
+
+				try
+				{
+					army = WarFoundryLoader.GetDefault().LoadArmy(file);
+				}
+				catch (Exception ex)
+				{
+					logger.Error("Error while loading army file " + filePath, ex);
+				}
 
 				if (army != null)
 				{
@@ -826,9 +834,8 @@
 				}
 				else
 				{
-					logger.ErrorFormat("Failed to load {0} as an army file", filePath);
-					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.");
-					dialog.Title = "Failed to open army";
+					MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, Translation.GetTranslation("OpenFailed", "File open failed. Please check log file"));
+					dialog.Title = Translation.GetTranslation("OpenFailedTitle", "File open failed");
 					dialog.Run();
 					dialog.Hide();
 					dialog.Dispose();
@@ -856,10 +863,13 @@
 
 		private bool SaveCurrentArmyAs()
 		{
-			FileChooserDialog fileDialog = new FileChooserDialog("Save file as", this, FileChooserAction.Save, "Cancel", ResponseType.Cancel, "Save", ResponseType.Accept);
+			string title = Translation.GetTranslation("saveArmyDialog", "save army");
+			string cancelText = Translation.GetTranslation("bttnCancel", "cancel");
+			string saveText = Translation.GetTranslation("bttnSave", "save");
+			FileChooserDialog fileDialog = new FileChooserDialog(title, this, FileChooserAction.Save, cancelText, ResponseType.Cancel, saveText, ResponseType.Accept);
 			FileFilter filter = new FileFilter();
 			filter.AddPattern("*.army");
-			filter.Name = "WarFoundry Army files (*.army)";
+			filter.Name = Translation.GetTranslation("armyFileFilter", "WarFoundry .army files");
 			fileDialog.AddFilter(filter);
 			int response = fileDialog.Run();
 			string filePath = null;
@@ -881,7 +891,18 @@
 			
 			if (filePath != null)
 			{
-				if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, filePath))
+				bool saveSuccess = false;
+
+				try
+				{
+					saveSuccess = WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, filePath);
+				}
+				catch (Exception ex)
+				{
+					logger.Error("Error while saving army file to " + filePath, ex);
+				}
+
+				if (saveSuccess)
 				{
 					miSaveArmy.Sensitive = false;
 					bttnSaveArmy.Sensitive = false;
@@ -891,8 +912,8 @@
 				}
 				else
 				{
-					MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Failed to save file to " + filePath);
-					dialog.Title = "Army save failed";
+					MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok,  Translation.GetTranslation("SaveFailed", "File save failed. Please check log file"));
+					dialog.Title = Translation.GetTranslation("SaveFailedTitle", "File save failed");
 					dialog.Run();
 					dialog.Hide();
 					dialog.Dispose();
@@ -911,8 +932,14 @@
 
 				if (CommandStack.IsDirty())
 				{
-					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?");
-					dia.AddButton("Cancel", ResponseType.Cancel);
+					string message = Translation.GetTranslation("SaveChangesQuestion", "Save changes to army before closing?", WarFoundryCore.CurrentArmy.Name);
+					MessageDialog dia = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.None, message);
+					dia.AddButton(Translation.GetTranslation("bttnDiscard", "lose changes"), ResponseType.No);
+					Button button = (Button)dia.AddButton(Translation.GetTranslation("bttnCancel", "cancel"), ResponseType.Cancel);
+					button.Image = new Image("gtk-cancel", IconSize.Button);
+					button = (Button)dia.AddButton(Translation.GetTranslation("bttnSave", "save"), ResponseType.Yes);
+					button.Image = new Image("gtk-save", IconSize.Button);
+					dia.Title = Translation.GetTranslation("SaveChangesTitle", "Save changes?");
 					ResponseType dr = (ResponseType)dia.Run();
 					dia.Hide();
 					dia.Dispose();