view FrmNewArmy.cs @ 167:9808adf2d566

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 7e77e79e5fdd
children
line source
1 // This file (FrmNewArmy.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2008, 2009 IBBoard.
2 //
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.
5 using System;
6 using Gtk;
7 using IBBoard.Lang;
8 using IBBoard.WarFoundry.API;
9 using IBBoard.WarFoundry.API.Objects;
10 using IBBoard.GtkSharp;
11 using log4net;
12 using IBBoard.WarFoundry.GUI.GTK.Util;
13 using IBBoard.GtkSharp.Translatable;
15 namespace IBBoard.WarFoundry.GUI.GTK
16 {
17 public partial class FrmNewArmy : TranslatableDialog, ITranslatable
18 {
19 private ILog logger = LogManager.GetLogger(typeof(FrmNewArmy));
20 private Race race;
21 private string armyName;
22 private int pointsValue;
23 private bool isDefaultArmyName = true;
25 public FrmNewArmy(GameSystem gameSystem)
26 {
27 this.Build();
29 GameSystem[] gameSystems = WarFoundryLoader.GetDefault().GetGameSystems();
30 ComboBoxUtils.FillCombo(systemCombo, gameSystems, delegate(GameSystem sys){return sys.Name;});
32 lstRaces.Selection.Changed += new EventHandler(OnSelectionChanged);
34 TreeViewColumn raceColumn = new TreeViewColumn();
35 raceColumn.Title = Translation.GetTranslation("frmNewArmyRaceColumn", "race");
36 CellRendererText raceCell = new CellRendererText();
37 raceColumn.PackStart(raceCell, true);
38 lstRaces.AppendColumn(raceColumn);
39 raceColumn.SetCellDataFunc(raceCell, GtkWarFoundryUtil.RenderWarFoundryObjectName);
41 if (gameSystem != null)
42 {
43 ComboBoxUtils.SelectItem(systemCombo, gameSystem);
44 }
45 else if (gameSystems.Length == 1)
46 {
47 ComboBoxUtils.SelectIndex(systemCombo, 0);
48 }
50 Translation.TranslationChanged += Retranslate;
51 Translate();
52 }
54 private void Retranslate()
55 {
56 Translate();
57 }
59 public override void Dispose()
60 {
61 Translation.TranslationChanged -= Retranslate;
62 base.Dispose();
63 }
65 protected override void Translate()
66 {
67 base.Translate();
68 ControlTranslator.TranslateButtons(bttnCreate, bttnCancel);
69 }
71 protected virtual void OnSelectionChanged(object o, EventArgs e)
72 {
73 logger.Debug("Race selection changed");
74 SetArmyName();
75 SetOkayButtonState();
76 }
78 private void SetOkayButtonState()
79 {
80 bttnCreate.Sensitive = (lstRaces.Selection.CountSelectedRows() == 1 && txtArmyName.Text != "" && sbPointsValue.Value > 0);
81 }
83 protected virtual void OnCreateClicked(object sender, System.EventArgs e)
84 {
85 race = GetSelectedRace();
86 armyName = txtArmyName.Text;
87 pointsValue = (int)sbPointsValue.Value;
88 Respond(ResponseType.Ok);
89 }
91 private Race GetSelectedRace()
92 {
93 TreeModel model;
94 TreeIter iter;
95 lstRaces.Selection.GetSelected(out model, out iter);
96 return (Race)model.GetValue(iter, 0);
97 }
99 protected virtual void OnCancelClicked(object sender, System.EventArgs e)
100 {
101 Respond(ResponseType.Cancel);
102 }
104 protected virtual void OnTextChanged(object sender, System.EventArgs e)
105 {
106 SetOkayButtonState();
107 isDefaultArmyName = false;
108 }
110 protected virtual void OnSpinChangeValue(object o, Gtk.ChangeValueArgs args)
111 {
112 SetArmyName();
113 SetOkayButtonState();
114 }
116 protected virtual void OnSpinValueChanged(object sender, System.EventArgs e)
117 {
118 SetArmyName();
119 SetOkayButtonState();
120 }
122 protected virtual void OnSystemComboChanged(object sender, System.EventArgs e)
123 {
124 GameSystem system = ComboBoxUtils.GetSelectedItem<GameSystem>(systemCombo);
125 SetRaces(system);
126 sbPointsValue.Value = system.SystemArmyDefaultSize;
127 ListStore model = (ListStore)lstRaces.Model;
129 if (model.IterNChildren() == 1)
130 {
131 TreeIter iter;
132 model.GetIterFirst(out iter);
133 lstRaces.Selection.SelectIter(iter);
134 SetArmyName();
135 }
137 logger.Debug("System selection changed: " + (system == null ? "null" : system.Name));
138 SetOkayButtonState();
139 }
141 private void SetRaces(GameSystem system)
142 {
143 ListStore store = new ListStore(typeof(Race));
145 if (system != null)
146 {
147 foreach (Race race in WarFoundryLoader.GetDefault().GetRaces(system))
148 {
149 store.AppendValues(race);
150 }
151 }
153 lstRaces.Model = store;
154 }
156 private void SetArmyName()
157 {
158 if (isDefaultArmyName || txtArmyName.Text == "")
159 {
160 if (lstRaces.Selection.CountSelectedRows() == 1)
161 {
162 GameSystem currSystem = SelectedGameSystem;
163 int points = (int)sbPointsValue.Value;
164 txtArmyName.Text = Army.GenerateDefaultName(GetSelectedRace(), points, currSystem.GetPointsAbbrev(points));
165 }
166 else {
167 txtArmyName.Text = "";
168 }
170 isDefaultArmyName = true;
171 }
172 }
174 public Race SelectedRace
175 {
176 get { return race; }
177 }
179 public string ArmyName
180 {
181 get { return armyName; }
182 }
184 public int ArmySize
185 {
186 get { return pointsValue; }
187 }
189 private GameSystem SelectedGameSystem
190 {
191 get { return ComboBoxUtils.GetSelectedItem<GameSystem>(systemCombo); }
192 }
193 }
194 }