view FrmNewArmy.cs @ 30:5fafbb1b4592

Re #145: Add UI to remove units from army * Add initial right-click "remove unit" for GTK app Existing problems: * Right-click occurs before selection changes, so it can be a click out of synch
author IBBoard <dev@ibboard.co.uk>
date Fri, 04 Sep 2009 20:11:02 +0000
parents a191d0655f55
children d68992a831df
line wrap: on
line source

// This file (FrmNewArmy.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 Gtk;
using IBBoard.Lang;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Objects;
using log4net;

namespace IBBoard.WarFoundry.GTK
{	
	public partial class FrmNewArmy : Dialog, ITranslatable
	{
		private ILog logger = LogManager.GetLogger(typeof(FrmNewArmy));
		private GameSystem system;
		private Race race;
		private string armyName;
		private int pointsValue;
		
		public FrmNewArmy(GameSystem gameSystem)
		{
			this.Build();
			
			system = gameSystem;
			lstRaces.Selection.Changed+= new EventHandler(OnSelectionChanged);
			TreeViewColumn raceColumn = new TreeViewColumn ();
			raceColumn.Title = "Race";
			CellRendererText raceCell = new CellRendererText ();
			raceColumn.PackStart (raceCell, true);
			lstRaces.AppendColumn(raceColumn);
			raceColumn.SetCellDataFunc(raceCell, new TreeCellDataFunc(RenderRaceName));
			ListStore store = new ListStore(typeof(Race));
			
			foreach (Race r in WarFoundryLoader.GetDefault().GetRaces(system))
			{
				store.AppendValues(r);
			}
			
			lstRaces.Model = store;
		}
		
		public string Text
		{
			get { return Title; }
			set { Title = value; }
		}
		
		private void RenderRaceName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			Race r = (Race)model.GetValue(iter, 0);
			(cell as CellRendererText).Text = r.Name;
		}

		protected virtual void OnSelectionChanged(object o, EventArgs e)
		{
			logger.Debug("Selection changed");
			setOkayButtonState();
		}
		
		private void setOkayButtonState()
		{
			bttnCreate.Sensitive = (lstRaces.Selection.CountSelectedRows() > 0 && txtArmyName.Text!="" && sbPointsValue.Value > 0);
		}

		protected virtual void OnCreateClicked (object sender, System.EventArgs e)
		{
			TreeModel model;
			TreeIter iter;
			lstRaces.Selection.GetSelected (out model, out iter);
			race = (Race) model.GetValue(iter, 0);
			armyName = txtArmyName.Text;
			pointsValue = (int)sbPointsValue.Value;
			Respond(ResponseType.Ok);
		}

		protected virtual void OnCancelClicked (object sender, System.EventArgs e)
		{
			Respond(ResponseType.Cancel);
		}

		protected virtual void OnTextChanged (object sender, System.EventArgs e)
		{
			setOkayButtonState();
		}

		protected virtual void OnSpinChangeValue (object o, Gtk.ChangeValueArgs args)
		{
			setOkayButtonState();
		}

		protected virtual void OnSpinValueChanged (object sender, System.EventArgs e)
		{
			setOkayButtonState();
		}
		
		public Race SelectedRace
		{
			get { return race; }
		}
		
		public string ArmyName
		{
			get { return armyName; }
		}
		
		public int ArmySize 
		{
			get { return pointsValue; }
		}
	}
}