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/
35 using Microsoft.Win32;
36 using System.Windows.Forms;
38 namespace LibCECTray.settings
40 enum CECSettingSerialisationType
62 /// Base class for settings that can be persisted in the registry
64 abstract class CECSetting
67 /// Create a new setting
69 /// <param name="type">The type of this setting</param>
70 /// <param name="serialisationType">The serialisationType of the setting</param>
71 /// <param name="keyName">The name of the key in the registry</param>
72 /// <param name="friendlyName">The name of the setting in the UI</param>
73 /// <param name="defaultValue">Default value of the setting</param>
74 /// <param name="changedHandler">Called when the setting changed</param>
75 protected CECSetting(CECSettingType type, CECSettingSerialisationType serialisationType, string keyName, string friendlyName, object defaultValue, SettingChangedHandler changedHandler)
78 SettingSerialisationType = serialisationType;
80 FriendlyName = friendlyName;
81 DefaultValue = defaultValue;
82 _value = defaultValue;
84 if (changedHandler != null)
85 SettingChanged += changedHandler;
88 #region Serialisation methods
90 /// Get the value of the setting in a form that can be stored in the registry
92 /// <returns>The serialised value</returns>
93 protected abstract string GetSerialisedValue();
96 /// Set the value from the serialised form of it.
98 /// <param name="value">The serialised value</param>
99 protected abstract void SetSerialisedValue(string value);
102 /// Get the default value of the setting in a form that can be stored in the registry
104 /// <returns>The serialised default value</returns>
105 protected abstract string GetSerialisedDefaultValue();
108 /// Set the default value from the serialised form of it.
110 /// <param name="value">The serialised default value</param>
111 protected abstract void SetSerialisedDefaultValue(string value);
115 /// Set the value to the default.
117 public void ResetDefaultValue()
119 Value = DefaultValue;
122 #region Read/Write the corresponding registry key
124 /// Load the value from the registry
126 /// <param name="key">The registry key to read the value from</param>
127 public void Load(RegistryKey key)
129 _value = key.GetValue(KeyName) ?? DefaultValue;
134 /// Persist the value in the registry
136 /// <param name="key">The registry key to save the value to</param>
137 public void Persist(RegistryKey key)
139 if (_value != DefaultValue)
140 key.SetValue(KeyName, _value);
144 #region GUI control replacement
146 /// Replaces the controls in the form that was generated by the gui designer
148 /// <param name="form">The form which contains the controls that are to be replaced</param>
149 /// <param name="controls">The controls container which contains the controls that are to be replaced</param>
150 /// <param name="labelControl">The label control to replace</param>
151 /// <param name="valueControl">The value control to replace</param>
152 public void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, Control labelControl, Control valueControl)
155 ReplaceControl(controls, labelControl, Label);
156 ReplaceControl(controls, valueControl, ValueControl);
160 /// Replaces the controls in the form that was generated by the gui designer
162 /// <param name="form">The form which contains the controls that are to be replaced</param>
163 /// <param name="controls">The controls container which contains the controls that are to be replaced</param>
164 /// <param name="valueControl">The value control to replace</param>
165 public void ReplaceControls(AsyncForm form, Control.ControlCollection controls, Control valueControl)
168 ReplaceControl(controls, valueControl, ValueControl);
172 /// Replaces the controls in the form that was generated by the gui designer
174 /// <param name="controls">The controls container which contains the controls that are to be replaced</param>
175 /// <param name="originalControl">The control to replace</param>
176 /// <param name="replacement">The replacement</param>
177 protected static void ReplaceControl(Control.ControlCollection controls, Control originalControl, Control replacement)
179 if (originalControl == null)
182 var location = originalControl.Location;
183 var originalSize = originalControl.Size;
184 var tabIndex = originalControl.TabIndex;
186 controls.Remove(originalControl);
188 if (replacement != null)
190 controls.Add(replacement);
191 replacement.Location = location;
192 replacement.Size = originalSize;
193 replacement.TabIndex = tabIndex;
199 /// A setting changed
201 /// <param name="setting">The setting that changed</param>
202 /// <param name="oldValue">The old value</param>
203 /// <param name="newValue">The new value</param>
204 public delegate void SettingChangedHandler(CECSetting setting, object oldValue, object newValue);
207 /// Checks if a setting may be enabled
209 /// <param name="setting">The setting</param>
210 /// <param name="value">The value that the controller wants to set</param>
211 /// <returns>The Enabled value that will be used</returns>
212 public delegate bool EnableSettingHandler(CECSetting setting, bool value);
214 #region Convenience methods
215 public CECSettingBool AsSettingBool
217 get { return this as CECSettingBool; }
219 public CECSettingByte AsSettingByte
221 get { return this as CECSettingByte; }
223 public CECSettingDeviceType AsSettingDeviceType
225 get { return this as CECSettingDeviceType; }
227 public CECSettingLogicalAddress AsSettingLogicalAddress
229 get { return this as CECSettingLogicalAddress; }
231 public CECSettingLogicalAddresses AsSettingLogicalAddresses
233 get { return this as CECSettingLogicalAddresses; }
235 public CECSettingNumeric AsSettingNumeric
237 get { return this as CECSettingNumeric; }
239 public CECSettingString AsSettingString
241 get { return this as CECSettingString; }
243 public CECSettingUShort AsSettingUShort
245 get { return this as CECSettingUShort; }
247 public CECSettingVendorId AsSettingVendorId
249 get { return this as CECSettingVendorId; }
255 /// Name of the key in the registry
257 public string KeyName { protected set; get; }
260 /// Name of the setting in the UI
262 public string FriendlyName { protected set; get; }
265 /// The current value of the setting
267 public object Value {
268 get { return _value; }
271 if (_value == value) return;
273 var oldValue = _value;
275 if (SettingChanged != null)
276 SettingChanged(this, oldValue, value);
279 private object _value;
282 /// The default value of the setting
284 public object DefaultValue { protected set; get; }
287 /// The serialisationType of this setting
289 public CECSettingSerialisationType SettingSerialisationType { private set; get; }
292 /// The type of this setting
294 public CECSettingType SettingType { private set; get; }
297 /// True when changed and changes have not been persisted yet, false otherwise.
299 public bool Changed { protected set; get; }
302 /// The gui Control that contains the value
304 public virtual Control ValueControl { get { return BaseValueControl; } }
307 /// The value control to use in the gui
309 protected Control BaseValueControl;
312 /// True when changing the value of the ValueControl requires an invoke, false otherwise
314 public virtual bool InvokeRequired
316 get { return BaseValueControl != null && BaseValueControl.InvokeRequired; }
320 /// The label with the description for this setting
326 (_label = new Label {AutoSize = true, Size = new Size(100, 13), Text = FriendlyName});
329 private Label _label;
334 public event SettingChangedHandler SettingChanged;
337 /// Setting will be enabled
339 public EnableSettingHandler EnableSetting;
342 /// The initial enabled state
344 protected bool InitialEnabledValue
346 get { return _enabled; }
350 /// The enabled state of the gui control
352 public virtual bool Enabled
356 var newValue = value;
357 if (EnableSetting != null)
358 newValue = EnableSetting(this, value);
362 Form.SetControlEnabled(ValueControl, newValue);
366 return ValueControl != null ? ValueControl.Enabled : _enabled;
371 /// The for that contains the gui controls
373 protected IAsyncControls Form;
375 private bool _enabled = true;