view FrmEditEquipment.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 6d2c8ad0d8f1
children
line source
1 // This file (FrmEditEquipment.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 IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
7 using IBBoard.WarFoundry.API.Objects;
8 using Gtk;
9 using IBBoard.WarFoundry.GUI.GTK.Util;
10 using IBBoard.GtkSharp;
11 using log4net.Repository.Hierarchy;
12 using log4net;
13 using IBBoard.GtkSharp.Translatable;
14 using IBBoard.Lang;
16 namespace IBBoard.WarFoundry.GUI.GTK
17 {
18 public partial class FrmEditEquipment : TranslatableDialog, IEditEquipmentUI
19 {
20 private static ILog log = LogManager.GetLogger(typeof(FrmAddEquipment));
22 public event MethodInvoker UnitEquipmentAmountTypeChanged;
23 public event MethodInvoker UnitEquipmentAmountChanged;
25 private bool isRatioLimited;
27 public FrmEditEquipment()
28 {
29 this.Build();
30 Translation.TranslationChanged += Retranslate;
31 Translate();
32 }
34 private void Retranslate()
35 {
36 Translate();
37 }
39 public override void Dispose()
40 {
41 Translation.TranslationChanged -= Retranslate;
42 base.Dispose();
43 }
45 public void ListenToWidgets()
46 {
47 rbEquipAll.Clicked += RadioButtonClicked;
48 rbEquipNumeric.Clicked += RadioButtonClicked;
49 rbEquipPercent.Clicked += RadioButtonClicked;
50 numericAmount.ValueChanged += SpinButtonValueChanged;
51 percentageAmount.ValueChanged += SpinButtonValueChanged;
52 }
54 public void IgnoreWidgets()
55 {
56 rbEquipAll.Clicked -= RadioButtonClicked;
57 rbEquipNumeric.Clicked -= RadioButtonClicked;
58 rbEquipPercent.Clicked -= RadioButtonClicked;
59 numericAmount.ValueChanged -= SpinButtonValueChanged;
60 percentageAmount.ValueChanged -= SpinButtonValueChanged;
61 }
63 private void OnUnitEquipmentAmountChanged()
64 {
65 if (UnitEquipmentAmountChanged != null)
66 {
67 UnitEquipmentAmountChanged();
68 }
69 }
71 private void OnUnitEquipmentAmountTypeChanged()
72 {
73 if (UnitEquipmentAmountTypeChanged != null)
74 {
75 UnitEquipmentAmountTypeChanged();
76 }
77 }
79 public void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber)
80 {
81 log.DebugFormat("IsRatio? {0}. Limits: {1}->{2}, {3}%->{4}%", isRatioLimit, minNumber, maxNumber, minPercent, maxPercent);
82 numericAmount.SetRange(minNumber, maxNumber);
83 percentageAmount.SetRange(minPercent, maxPercent);
84 SetEquipmentAmountType(isRatioLimit);
85 isRatioLimited = isRatioLimit;
86 }
88 public void SetEquipmentAmountType(bool isRatioAmount)
89 {
90 if (isRatioAmount)
91 {
92 if (percentageAmount.Value == 100)
93 {
94 rbEquipAll.Active = true;
95 }
96 else
97 {
98 rbEquipPercent.Active = true;
99 }
100 }
101 else
102 {
103 rbEquipNumeric.Active = true;
104 }
105 }
107 public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
108 {
109 SetNumericAmountEnabledState(isEnabled);
110 SetPercentageAmountEnabledState(isEnabled);
111 }
113 public bool ShowControl()
114 {
115 int result = Run();
116 bool okayClicked = (result == (int)ResponseType.Ok);
117 this.Hide();
118 return okayClicked;
119 }
121 protected virtual void CancelButtonClicked(object sender, System.EventArgs e)
122 {
123 log.Debug("Cancel clicked");
124 Respond(ResponseType.Cancel);
125 }
127 protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
128 {
129 log.Debug("Okay clicked");
130 Respond(ResponseType.Ok);
131 }
133 public void SetOkayEnabledState(bool enabled)
134 {
135 bttnOkay.Sensitive = enabled;
136 }
138 protected virtual void SpinButtonValueChanged(object sender, System.EventArgs e)
139 {
140 OnUnitEquipmentAmountChanged();
141 }
143 protected virtual void RadioButtonClicked(object sender, System.EventArgs e)
144 {
145 OnUnitEquipmentAmountTypeChanged();
146 }
148 public void SetNumericAmountEnabledState(bool enabled)
149 {
150 double minPercent = GetMinPercentage();
151 rbEquipNumeric.Sensitive = enabled && !(isRatioLimited && minPercent == 100);
152 numericAmount.Sensitive = rbEquipNumeric.Sensitive;
153 }
155 public void SetPercentageAmountEnabledState(bool enabled)
156 {
157 if (enabled)
158 {
159 double minPercentage = GetMinPercentage();
160 rbEquipPercent.Sensitive = isRatioLimited && minPercentage != 100;
161 percentageAmount.Sensitive = rbEquipPercent.Sensitive;
162 double maxPercentage = GetMaxPercentage();
163 rbEquipAll.Sensitive = isRatioLimited && maxPercentage == 100;
164 lblEquipAll.Sensitive = rbEquipAll.Sensitive;
165 }
166 else
167 {
168 rbEquipPercent.Sensitive = false;
169 percentageAmount.Sensitive = false;
170 rbEquipAll.Sensitive = false;
171 lblEquipAll.Sensitive = false;
172 }
173 }
175 private double GetMaxPercentage()
176 {
177 double min, max;
178 percentageAmount.GetRange(out min, out max);
179 return max;
180 }
182 private double GetMinPercentage()
183 {
184 double min, max;
185 percentageAmount.GetRange(out min, out max);
186 return min;
187 }
189 public bool IsRatioEquipmentAmount
190 {
191 get
192 {
193 return !rbEquipNumeric.Active;
194 }
195 }
197 public int EquipmentNumericAmount
198 {
199 get
200 {
201 return (int)numericAmount.Value;
202 }
204 set
205 {
206 numericAmount.Value = value;
207 }
208 }
210 public double EquipmentPercentageAmount
211 {
212 get
213 {
214 double percent;
216 if (rbEquipAll.Active)
217 {
218 percent = 100;
219 }
220 else
221 {
222 percent = percentageAmount.Value;
223 }
225 return percent;
226 }
228 set
229 {
230 percentageAmount.Value = value;
231 }
232 }
233 }
234 }