2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 using System.Collections.Generic;
34 using System.Windows.Forms;
37 namespace LibCECTray.settings
40 /// A setting of type integer that can be persisted in the registry
42 class CECSettingNumeric : CECSetting
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)
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>())
56 public CECSettingNumeric(string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler, ListControlConvertEventHandler format) :
57 this(CECSettingType.Numeric, keyName, friendlyName, defaultValue, changedHandler, format)
61 public CECSettingNumeric(string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler) :
62 this(CECSettingType.Numeric, keyName, friendlyName, defaultValue, changedHandler, null)
66 public CECSettingNumeric(CECSettingType type, string keyName, string friendlyName, int defaultValue, SettingChangedHandler changedHandler) :
67 this(type, keyName, friendlyName, defaultValue, changedHandler, null)
71 #region Serialisation methods
72 protected override string GetSerialisedValue()
74 return string.Format("{0}", Value);
77 protected override void SetSerialisedValue(string value)
80 Value = int.TryParse(value, out intValue) ? intValue : DefaultValue;
83 protected override string GetSerialisedDefaultValue()
85 return string.Format("{0}", DefaultValue);
88 protected override void SetSerialisedDefaultValue(string value)
91 DefaultValue = int.TryParse(value, out intValue) ? intValue : int.MinValue;
99 return base.Value != null ? (int)base.Value : int.MaxValue;
104 ComboBox control = BaseValueControl as ComboBox;
105 if (Form != null && control != null && BaseItems.Contains(value))
107 Form.SetSelectedIndex(control, BaseItems.IndexOf(value));
112 public new int DefaultValue
116 return base.DefaultValue != null ? (int)base.DefaultValue : int.MaxValue;
120 base.DefaultValue = value;
124 public int LowerLimit { get; set; }
126 public int UpperLimit { get; set; }
130 get { return _step; }
131 set { _step = value; }
133 private int _step = 1;
135 protected virtual bool AllowedValue(int value)
140 protected virtual void CreateValueControl()
142 if (BaseValueControl != null) return;
143 ComboBox control = new ComboBox
145 FormattingEnabled = true,
147 Size = new System.Drawing.Size(Width, Height),
148 AutoCompleteMode = AutoCompleteMode.Append,
149 Enabled = InitialEnabledValue
154 control.Format += Format;
155 control.FormattingEnabled = true;
157 BaseValueControl = control;
159 ResetItems(BaseItems.Count == 0);
161 control.SelectedValueChanged += delegate
163 Value = BaseItems[control.SelectedIndex];
167 public override Control ValueControl
171 CreateValueControl();
172 return BaseValueControl;
176 public void ResetItems(bool clear)
178 ComboBox control = BaseValueControl as ComboBox;
179 if (control == null) return;
183 List<object> cbItems = new List<object>();
184 foreach (var item in Items)
185 cbItems.Add(string.Format("{0}", item));
187 Form.SetComboBoxItems(control, Items.IndexOf(Value), cbItems.ToArray());
190 public virtual void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, Control labelControl, ComboBox valueControl)
193 ReplaceControl(controls, labelControl, Label);
194 ReplaceControl(controls, valueControl, ValueControl);
197 public virtual void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, ComboBox valueControl)
200 ReplaceControl(controls, valueControl, ValueControl);
203 public override bool Enabled
205 set { base.Enabled = (SettingType == CECSettingType.Bool || BaseItems.Count > 1) && value; }
206 get { return base.Enabled; }
209 public int Width = 100;
210 public int Height = 21;
212 public event ListControlConvertEventHandler Format;
213 protected readonly List<int> BaseItems = new List<int>();
214 public virtual List<int> Items
218 if (BaseItems.Count == 0)
220 for (var i = LowerLimit; i < UpperLimit; i += Step)
230 ComboBox control = BaseValueControl as ComboBox;
231 if (control == null) return;
232 List<object> cbItems = new List<object>();
234 foreach (var item in value)
237 cbItems.Add(string.Format("{0}", item));
240 Form.SetComboBoxItems(control, BaseItems.IndexOf(Value), cbItems.ToArray());