Mercurial > repos > IBBoard.WarFoundry.GUI.GTK
view FrmNewUnit.cs @ 167:9808adf2d566 default tip
Re #419: Remove assumptions of a file-based install
* Update GTK# GUI to use Streams version of XSLT export
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 28 Nov 2012 20:25:53 +0000 |
parents | 354c1d2ad086 |
children |
line wrap: on
line source
// This file (FrmNewUnit.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2008, 2009 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 System.Collections.Generic; using Gtk; using IBBoard.GtkSharp.Translatable; using WFObjects = IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Objects; using log4net; using IBBoard.Lang; using IBBoard.WarFoundry.API.Objects.Requirement; using System.Text; using IBBoard.WarFoundry.API.Objects.Requirement.Context; namespace IBBoard.WarFoundry.GUI.GTK { public partial class FrmNewUnit : TranslatableDialog { private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit)); private UnitType unitType; private Army unitArmy; private string titlePart; private AddingContext context; public FrmNewUnit(Race race, Category category, Army army) : this(race.GetUnitTypes(category), army) { titlePart = category.Name; context = new NullAddingContext(); } public FrmNewUnit(WFObjects.Unit unit, Army army) : this(unit.UnitType.ContainedUnitTypes, army) { titlePart = unit.Name; context = new AddingToParentContext(unit); } private FrmNewUnit(UnitType[] unitTypes, Army army) { this.Build(); unitArmy = army; TreeViewColumn unitTypeColumn = new TreeViewColumn(); CellRendererText unitTypeCell = new CellRendererText(); unitTypeColumn.PackStart(unitTypeCell, true); lstUnitTypes.AppendColumn(unitTypeColumn); unitTypeColumn.SetCellDataFunc(unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName)); ListStore store = new ListStore(typeof(UnitType)); logger.DebugFormat("Listing {0} unit types", unitTypes.Length); foreach (UnitType type in unitTypes) { logger.DebugFormat("Adding unit type {0}", type.Name); store.AppendValues(type); } lstUnitTypes.Model = store; lstUnitTypes.Selection.Changed += new EventHandler(OnSelectionChanged); Translation.TranslationChanged += Retranslate; Translate(); } private void Retranslate() { Translate(); } public override void Dispose() { Translation.TranslationChanged -= Retranslate; base.Dispose(); } protected override void Translate() { base.Translate(); lstUnitTypes.Columns[0].Title = Translation.GetTranslation("frmNewUnitNewUnitColumn", "unit type"); Title = Translation.GetTranslation(Name, "Create new unit", titlePart); } private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { UnitType type = (UnitType)model.GetValue(iter, 0); (cell as CellRendererText).Text = type.Name; } protected virtual void OnSelectionChanged(object o, EventArgs e) { SetSelectUnitEnabledVal(); } public UnitType SelectedUnit { get { return unitType; } } private UnitType GetSelectedUnitType() { TreeModel model; TreeIter iter; lstUnitTypes.Selection.GetSelected(out model, out iter); UnitType toReturn = null; if (model != null) { toReturn = (UnitType)model.GetValue(iter, 0); } return toReturn; } private void SetSelectUnitEnabledVal() { UnitType type = GetSelectedUnitType(); if (type != null) { bttnCreate.Sensitive = true; ICollection<string> failureMessages; Validation result = RequirementHandler.AllowsAdding(type, unitArmy, context, out failureMessages); bool validationFailed = !Validates.AsOkay(result); validationWidget.Visible = validationFailed; if (validationFailed) { validationWidget.SetMessages(failureMessages); } } else { bttnCreate.Sensitive = false; validationWidget.Visible = false; } } protected virtual void OnButtonCancelActivated(object sender, System.EventArgs e) { Respond(ResponseType.Cancel); } protected virtual void OnRowActivated(object o, Gtk.RowActivatedArgs args) { unitType = GetSelectedUnitType(); Respond(ResponseType.Ok); } protected virtual void OnButtonOkClicked(object sender, System.EventArgs e) { unitType = GetSelectedUnitType(); Respond(ResponseType.Ok); } } }