Imported Upstream version 2.2.0
[deb_libcec.git] / src / LibCecTray / settings / CECSettingNumeric.cs
CommitLineData
cbbe90dd
JB
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33using System.Collections.Generic;
34using System.Windows.Forms;
35using LibCECTray.ui;
36
37namespace LibCECTray.settings
38{
39 /// <summary>
40 /// A setting of type integer that can be persisted in the registry
41 /// </summary>
42 class CECSettingNumeric : CECSetting
43 {
44 public CECSettingNumeric(CECSettingType type, string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler, ListControlConvertEventHandler format, List<int> items) :
45 base(type, CECSettingSerialisationType.Numeric, keyName, friendlyName, defaultValue, changedHandler)
46 {
47 BaseItems = items;
48 Format += format;
49 }
50
51 public CECSettingNumeric(CECSettingType type, string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler, ListControlConvertEventHandler format) :
52 this(type, keyName, friendlyName, defaultValue, changedHandler, format, new List<int>())
53 {
54 }
55
56 public CECSettingNumeric(string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler, ListControlConvertEventHandler format) :
57 this(CECSettingType.Numeric, keyName, friendlyName, defaultValue, changedHandler, format)
58 {
59 }
60
61 public CECSettingNumeric(string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler) :
62 this(CECSettingType.Numeric, keyName, friendlyName, defaultValue, changedHandler, null)
63 {
64 }
65
66 public CECSettingNumeric(CECSettingType type, string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler) :
67 this(type, keyName, friendlyName, defaultValue, changedHandler, null)
68 {
69 }
70
71 #region Serialisation methods
72 protected override string GetSerialisedValue()
73 {
74 return string.Format("{0}", Value);
75 }
76
77 protected override void SetSerialisedValue(string value)
78 {
79 int intValue;
80 Value = int.TryParse(value, out intValue) ? intValue : DefaultValue;
81 }
82
83 protected override string GetSerialisedDefaultValue()
84 {
85 return string.Format("{0}", DefaultValue);
86 }
87
88 protected override void SetSerialisedDefaultValue(string value)
89 {
90 int intValue;
91 DefaultValue = int.TryParse(value, out intValue) ? intValue : int.MinValue;
92 }
93 #endregion
94
95 public new int Value
96 {
97 get
98 {
99 return base.Value != null ? (int)base.Value : int.MaxValue;
100 }
101 set
102 {
103 base.Value = value;
104 ComboBox control = BaseValueControl as ComboBox;
105 if (Form != null && control != null && BaseItems.Contains(value))
106 {
107 Form.SetSelectedIndex(control, BaseItems.IndexOf(value));
108 }
109 }
110 }
111
112 public new int DefaultValue
113 {
114 get
115 {
116 return base.DefaultValue != null ? (int)base.DefaultValue : int.MaxValue;
117 }
118 set
119 {
120 base.DefaultValue = value;
121 }
122 }
123
124 public int LowerLimit { get; set; }
125
126 public int UpperLimit { get; set; }
127
128 public int Step
129 {
130 get { return _step; }
131 set { _step = value; }
132 }
133 private int _step = 1;
134
135 protected virtual bool AllowedValue(int value)
136 {
137 return true;
138 }
139
140 protected virtual void CreateValueControl()
141 {
142 if (BaseValueControl != null) return;
143 ComboBox control = new ComboBox
144 {
145 FormattingEnabled = true,
146 Name = KeyName,
147 Size = new System.Drawing.Size(Width, Height),
148 AutoCompleteMode = AutoCompleteMode.Append,
149 Enabled = InitialEnabledValue
150 };
151
152 if (Format != null)
153 {
154 control.Format += Format;
155 control.FormattingEnabled = true;
156 }
157 BaseValueControl = control;
158
159 ResetItems(BaseItems.Count == 0);
160
161 if (BaseItems.Count > 0 && control.SelectedIndex < BaseItems.Count)
162 {
163 control.SelectedValueChanged += delegate
164 {
165 Value = BaseItems[control.SelectedIndex];
166 };
167 }
168 }
169
170 public override Control ValueControl
171 {
172 get
173 {
174 CreateValueControl();
175 return BaseValueControl;
176 }
177 }
178
179 public void ResetItems(bool clear)
180 {
181 ComboBox control = BaseValueControl as ComboBox;
182 if (control == null) return;
183 if (clear)
184 BaseItems.Clear();
185
186 List<object> cbItems = new List<object>();
187 foreach (var item in Items)
188 cbItems.Add(string.Format("{0}", item));
189
190 Form.SetComboBoxItems(control, Items.IndexOf(Value), cbItems.ToArray());
191 }
192
193 public virtual void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, Control labelControl, ComboBox valueControl)
194 {
195 Form = form;
196 ReplaceControl(controls, labelControl, Label);
197 ReplaceControl(controls, valueControl, ValueControl);
198 }
199
200 public virtual void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, ComboBox valueControl)
201 {
202 Form = form;
203 ReplaceControl(controls, valueControl, ValueControl);
204 }
205
206 public override bool Enabled
207 {
208 set { base.Enabled = (SettingType == CECSettingType.Bool || BaseItems.Count > 1) && value; }
209 get { return base.Enabled; }
210 }
211
212 public int Width = 100;
213 public int Height = 21;
214
215 public event ListControlConvertEventHandler Format;
216 protected readonly List<int> BaseItems = new List<int>();
217 public virtual List<int> Items
218 {
219 get
220 {
221 if (BaseItems.Count == 0)
222 {
223 for (var i = LowerLimit; i < UpperLimit; i += Step)
224 {
225 if (AllowedValue(i))
226 BaseItems.Add(i);
227 }
228 }
229 return BaseItems;
230 }
231 protected set
232 {
233 ComboBox control = BaseValueControl as ComboBox;
234 if (control == null) return;
235 List<object> cbItems = new List<object>();
236 BaseItems.Clear();
237 foreach (var item in value)
238 {
239 BaseItems.Add(item);
240 cbItems.Add(string.Format("{0}", item));
241 }
242
243 Form.SetComboBoxItems(control, BaseItems.IndexOf(Value), cbItems.ToArray());
244 }
245 }
246 }
247}