view FrmReplaceEquipment.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 b4416ca69153
children
line source
1 // This file (FrmAddEquipment.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2010 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.GtkSharp;
8 using IBBoard.GtkSharp.Translatable;
9 using IBBoard.Lang;
10 using IBBoard.WarFoundry.API.Objects;
11 using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
12 using IBBoard.WarFoundry.GUI.GTK.Util;
13 using log4net;
15 namespace IBBoard.WarFoundry.GUI.GTK
16 {
17 public partial class FrmReplaceEquipment : TranslatableDialog, IReplaceEquipmentUI
18 {
19 private static ILog log = LogManager.GetLogger(typeof(FrmReplaceEquipment));
21 public event SingleArgMethodInvoker<UnitEquipmentItem> UnitEquipmentItemChoiceChanged;
22 public event MethodInvoker UnitEquipmentAmountTypeChanged;
23 public event MethodInvoker UnitEquipmentAmountChanged;
25 private bool isRatioLimited;
27 public FrmReplaceEquipment()
28 {
29 this.Build();
30 lstEquipment.Selection.Changed += OnSelectionChanged;
31 TreeViewColumn equipColumn = new TreeViewColumn();
32 equipColumn.Title = "Equipment";
33 CellRendererText equipCell = new CellRendererText();
34 equipColumn.PackStart(equipCell, true);
35 equipColumn.SetCellDataFunc(equipCell, GtkWarFoundryUtil.RenderWarFoundryObjectName);
36 lstEquipment.AppendColumn(equipColumn);
38 Translation.TranslationChanged += Retranslate;
39 Translate();
40 }
42 private void Retranslate()
43 {
44 Translate();
45 }
47 public override void Dispose()
48 {
49 Translation.TranslationChanged -= Retranslate;
50 base.Dispose();
51 }
53 public void ListenToWidgets()
54 {
55 rbEquipAll.Clicked += RadioButtonClicked;
56 rbEquipNumeric.Clicked += RadioButtonClicked;
57 rbEquipPercent.Clicked += RadioButtonClicked;
58 numericAmount.ValueChanged += SpinButtonValueChanged;
59 percentageAmount.ValueChanged += SpinButtonValueChanged;
60 lstEquipment.Selection.Changed += OnSelectionChanged;
61 }
63 public void IgnoreWidgets()
64 {
65 rbEquipAll.Clicked -= RadioButtonClicked;
66 rbEquipNumeric.Clicked -= RadioButtonClicked;
67 rbEquipPercent.Clicked -= RadioButtonClicked;
68 numericAmount.ValueChanged -= SpinButtonValueChanged;
69 percentageAmount.ValueChanged -= SpinButtonValueChanged;
70 lstEquipment.Selection.Changed -= OnSelectionChanged;
71 }
73 private void OnUnitEquipmentAmountChanged()
74 {
75 if (UnitEquipmentAmountChanged != null)
76 {
77 UnitEquipmentAmountChanged();
78 }
79 }
81 private void OnUnitEquipmentAmountTypeChanged()
82 {
83 if (UnitEquipmentAmountChanged != null)
84 {
85 UnitEquipmentAmountTypeChanged();
86 }
87 }
89 protected void OnSelectionChanged(object o, EventArgs e)
90 {
91 if (UnitEquipmentItemChoiceChanged != null)
92 {
93 UnitEquipmentItemChoiceChanged(SelectedUnitEquipmentItem);
94 }
95 }
97 public void SetUnitEquipmentItems(UnitEquipmentItem[] items)
98 {
99 ListStore store = new ListStore(typeof(UnitEquipmentItem));
101 foreach (UnitEquipmentItem equipItem in items)
102 {
103 store.AppendValues(equipItem);
104 }
106 lstEquipment.Model = store;
107 }
109 public void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber)
110 {
111 log.DebugFormat("IsRatio? {0}. Limits: {1}->{2}, {3}%->{4}%", isRatioLimit, minNumber, maxNumber, minPercent, maxPercent);
112 numericAmount.SetRange(minNumber, maxNumber);
113 percentageAmount.SetRange(minPercent, maxPercent);
114 isRatioLimited = isRatioLimit;
116 if (isRatioLimit)
117 {
118 if (minPercent == 100)
119 {
120 rbEquipAll.Active = true;
121 }
122 else
123 {
124 rbEquipPercent.Active = true;
125 }
126 }
127 else
128 {
129 rbEquipNumeric.Active = true;
130 }
131 }
133 public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
134 {
135 SetNumericAmountEnabledState(isEnabled);
136 SetPercentageAmountEnabledState(isEnabled);
137 }
139 public bool ShowControl()
140 {
141 int result = Run();
142 bool okayClicked = (result == (int)ResponseType.Ok);
143 this.Hide();
144 return okayClicked;
145 }
147 protected virtual void CancelButtonClicked(object sender, System.EventArgs e)
148 {
149 log.Debug("Cancel clicked");
150 Respond(ResponseType.Cancel);
151 }
153 protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
154 {
155 log.Debug("Okay clicked");
156 Respond(ResponseType.Ok);
157 }
159 public void SetOkayEnabledState(bool enabled)
160 {
161 bttnOkay.Sensitive = enabled;
162 }
164 protected virtual void SpinButtonValueChanged(object sender, System.EventArgs e)
165 {
166 OnUnitEquipmentAmountChanged();
167 }
169 protected virtual void RadioButtonClicked(object sender, System.EventArgs e)
170 {
171 OnUnitEquipmentAmountTypeChanged();
172 }
174 public void SetNumericAmountEnabledState(bool enabled)
175 {
176 double minPercent = GetMinPercentage();
177 rbEquipNumeric.Sensitive = enabled && !(isRatioLimited && minPercent == 100);
178 numericAmount.Sensitive = rbEquipNumeric.Sensitive;
179 }
181 public void SetPercentageAmountEnabledState(bool enabled)
182 {
183 if (enabled)
184 {
185 double minPercentage = GetMinPercentage();
186 rbEquipPercent.Sensitive = isRatioLimited && minPercentage != 100;
187 percentageAmount.Sensitive = rbEquipPercent.Sensitive;
188 double maxPercentage = GetMaxPercentage();
189 rbEquipAll.Sensitive = isRatioLimited && maxPercentage == 100;
190 lblEquipAll.Sensitive = rbEquipAll.Sensitive;
191 }
192 else
193 {
194 rbEquipPercent.Sensitive = false;
195 percentageAmount.Sensitive = false;
196 rbEquipAll.Sensitive = false;
197 lblEquipAll.Sensitive = false;
198 }
199 }
201 private double GetMaxPercentage()
202 {
203 double min, max;
204 percentageAmount.GetRange(out min, out max);
205 return max;
206 }
208 private double GetMinPercentage()
209 {
210 double min, max;
211 percentageAmount.GetRange(out min, out max);
212 return min;
213 }
215 public UnitEquipmentItem SelectedUnitEquipmentItem
216 {
217 get
218 {
219 return (UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment);
220 }
221 }
223 public bool IsRatioEquipmentAmount
224 {
225 get
226 {
227 return !rbEquipNumeric.Active;
228 }
229 }
231 public int EquipmentNumericAmount
232 {
233 get
234 {
235 return (int)numericAmount.Value;
236 }
238 set
239 {
240 numericAmount.Value = value;
241 }
242 }
244 public double EquipmentPercentageAmount
245 {
246 get
247 {
248 double percent;
250 if (rbEquipAll.Active)
251 {
252 percent = 100;
253 }
254 else
255 {
256 percent = percentageAmount.Value;
257 }
259 return percent;
260 }
262 set
263 {
264 percentageAmount.Value = value;
265 }
266 }
267 }
268 }