view FrmPreferences.cs @ 106:41e82ba675dc

Re #324: Add saving of Race and System data to files * Swap to new method call for saving army * Temporarily cause NullRef exceptions by removing the setting of a saver
author IBBoard <dev@ibboard.co.uk>
date Mon, 28 Feb 2011 21:10:18 +0000
parents b4416ca69153
children
line wrap: on
line source

//  This file (FrmPreferences.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2011 IBBoard
// 
//  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.

using System;
using IBBoard.GtkSharp.Translatable;
using IBBoard.Lang;
using Gtk;
using System.Collections.Generic;
using IBBoard.GtkSharp;
using System.Globalization;
using log4net;

namespace IBBoard.WarFoundry.GUI.GTK
{
	public partial class FrmPreferences : TranslatableDialog
	{
		private ILog logger = LogManager.GetLogger(typeof(FrmPreferences));
		private Preferences prefs;
		private string initialLang;

		public FrmPreferences(Preferences preferences)
		{
			prefs = preferences;
			initialLang = Translation.GetTranslationLanguage();
			this.Build();
			BuildPreferenceCategoryTree();
			BuildLanguageList();

			Translation.TranslationChanged += Retranslate;
			Translate();
		}

		private void Retranslate()
		{
			Translate();
		}

		public override void Dispose()
		{
			Translation.TranslationChanged -= Retranslate;
			base.Dispose();
		}

		protected override void Translate()
		{
			base.Translate();
			frameLabel.Text = Translation.GetTranslation("languagesGroup", "languages");
			TreeStore model = ((TreeStore)preferencesTree.Model);
			TreeIter iter;
			model.GetIterFirst(out iter);
			model.SetValue(iter, 0, Translation.GetTranslation("languagePrefSection", "Language"));
		}

		private void RenderPreference(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			object obj = model.GetValue(iter, 0);

			if (obj is string)
			{
				(cell as CellRendererText).Text = (string)obj;
			}
		}

		private void BuildPreferenceCategoryTree()
		{
			TreeViewColumn mainColumn = new TreeViewColumn();
			CellRendererText mainCell = new CellRendererText();
			mainColumn.PackStart(mainCell, true);
			preferencesTree.AppendColumn(mainColumn);
			mainColumn.SetCellDataFunc(mainCell, new TreeCellDataFunc(RenderPreference));
			TreeStore model = new TreeStore(typeof(string));
			preferencesTree.Model = model;
			model.AppendValues(Translation.GetTranslation("languagePrefSection", "Language"));
		}

		private void BuildLanguageList()
		{
			ICollection<TranslationLanguage> langs = Translation.GetLanguages();
			List<TranslationLanguage> sortedLangs = new List<TranslationLanguage>(langs);
			sortedLangs.Sort(CompareLanguages);
			ComboBoxUtils.FillCombo(languageList, sortedLangs, delegate(TranslationLanguage lang){return lang.Name;});
			ComboBoxUtils.SelectItem(languageList, Translation.GetTranslationSet(initialLang).Language);
		}

		private int CompareLanguages(TranslationLanguage lang1, TranslationLanguage lang2)
		{
			CompareOptions options = CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth;
			return CultureInfo.CurrentCulture.CompareInfo.Compare(lang1.Name, lang2.Name, options);
		}

		protected virtual void bttnOkayClicked(object sender, System.EventArgs e)
		{
			prefs["language"] = Translation.GetTranslationLanguage();
			prefs.Save();
			Respond(ResponseType.Ok);
		}

		protected virtual void bttnCancelClicked(object sender, System.EventArgs e)
		{
			Translation.LoadTranslation(initialLang);
			Respond(ResponseType.Cancel);
		}

		protected virtual void languageListChanged(object sender, System.EventArgs e)
		{
			TranslationLanguage selectedLang = ComboBoxUtils.GetSelectedItem<TranslationLanguage>(languageList);
			string code = selectedLang == null ? "" : selectedLang.Code;
			logger.Debug("New language: " + code);
			Translation.LoadTranslation(code);
			bttnOkay.Sensitive = !initialLang.Equals(code);

		}




	}
}