/* * This file is part of the libCEC(R) library. * * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved. * libCEC(R) is an original work, containing original code. * * libCEC(R) is a trademark of Pulse-Eight Limited. * * This program is dual-licensed; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * * Alternatively, you can license this library under a commercial license, * please contact Pulse-Eight Licensing for more information. * * For more information contact: * Pulse-Eight Licensing * http://www.pulse-eight.com/ * http://www.pulse-eight.net/ */ using System.Drawing; using LibCECTray.ui; using Microsoft.Win32; using System.Windows.Forms; namespace LibCECTray.settings { enum CECSettingSerialisationType { Numeric, String } enum CECSettingType { Numeric, String, Bool, Byte, DeviceType, LogicalAddress, LogicalAddresses, UShort, VendorId, Button, Generic } /// /// Base class for settings that can be persisted in the registry /// abstract class CECSetting { /// /// Create a new setting /// /// The type of this setting /// The serialisationType of the setting /// The name of the key in the registry /// The name of the setting in the UI /// Default value of the setting /// Called when the setting changed protected CECSetting(CECSettingType type, CECSettingSerialisationType serialisationType, string keyName, string friendlyName, object defaultValue, SettingChangedHandler changedHandler) { SettingType = type; SettingSerialisationType = serialisationType; KeyName = keyName; FriendlyName = friendlyName; DefaultValue = defaultValue; _value = defaultValue; if (changedHandler != null) SettingChanged += changedHandler; } #region Serialisation methods /// /// Get the value of the setting in a form that can be stored in the registry /// /// The serialised value protected abstract string GetSerialisedValue(); /// /// Set the value from the serialised form of it. /// /// The serialised value protected abstract void SetSerialisedValue(string value); /// /// Get the default value of the setting in a form that can be stored in the registry /// /// The serialised default value protected abstract string GetSerialisedDefaultValue(); /// /// Set the default value from the serialised form of it. /// /// The serialised default value protected abstract void SetSerialisedDefaultValue(string value); #endregion /// /// Set the value to the default. /// public void ResetDefaultValue() { Value = DefaultValue; } #region Read/Write the corresponding registry key /// /// Load the value from the registry /// /// The registry key to read the value from public void Load(RegistryKey key) { _value = key.GetValue(KeyName) ?? DefaultValue; Changed = false; } /// /// Persist the value in the registry /// /// The registry key to save the value to public void Persist(RegistryKey key) { if (_value != DefaultValue) key.SetValue(KeyName, _value); } #endregion #region GUI control replacement /// /// Replaces the controls in the form that was generated by the gui designer /// /// The form which contains the controls that are to be replaced /// The controls container which contains the controls that are to be replaced /// The label control to replace /// The value control to replace public void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, Control labelControl, Control valueControl) { Form = form; ReplaceControl(controls, labelControl, Label); ReplaceControl(controls, valueControl, ValueControl); } /// /// Replaces the controls in the form that was generated by the gui designer /// /// The form which contains the controls that are to be replaced /// The controls container which contains the controls that are to be replaced /// The value control to replace public void ReplaceControls(AsyncForm form, Control.ControlCollection controls, Control valueControl) { Form = form; ReplaceControl(controls, valueControl, ValueControl); } /// /// Replaces the controls in the form that was generated by the gui designer /// /// The controls container which contains the controls that are to be replaced /// The control to replace /// The replacement protected static void ReplaceControl(Control.ControlCollection controls, Control originalControl, Control replacement) { if (originalControl == null) return; var location = originalControl.Location; var originalSize = originalControl.Size; var tabIndex = originalControl.TabIndex; controls.Remove(originalControl); if (replacement != null) { controls.Add(replacement); replacement.Location = location; replacement.Size = originalSize; replacement.TabIndex = tabIndex; } } #endregion /// /// A setting changed /// /// The setting that changed /// The old value /// The new value public delegate void SettingChangedHandler(CECSetting setting, object oldValue, object newValue); /// /// Checks if a setting may be enabled /// /// The setting /// The value that the controller wants to set /// The Enabled value that will be used public delegate bool EnableSettingHandler(CECSetting setting, bool value); #region Convenience methods public CECSettingBool AsSettingBool { get { return this as CECSettingBool; } } public CECSettingByte AsSettingByte { get { return this as CECSettingByte; } } public CECSettingDeviceType AsSettingDeviceType { get { return this as CECSettingDeviceType; } } public CECSettingLogicalAddress AsSettingLogicalAddress { get { return this as CECSettingLogicalAddress; } } public CECSettingLogicalAddresses AsSettingLogicalAddresses { get { return this as CECSettingLogicalAddresses; } } public CECSettingNumeric AsSettingNumeric { get { return this as CECSettingNumeric; } } public CECSettingString AsSettingString { get { return this as CECSettingString; } } public CECSettingUShort AsSettingUShort { get { return this as CECSettingUShort; } } public CECSettingVendorId AsSettingVendorId { get { return this as CECSettingVendorId; } } #endregion #region Members /// /// Name of the key in the registry /// public string KeyName { protected set; get; } /// /// Name of the setting in the UI /// public string FriendlyName { protected set; get; } /// /// The current value of the setting /// public object Value { get { return _value; } set { if (_value == value) return; Changed = true; var oldValue = _value; _value = value; if (SettingChanged != null) SettingChanged(this, oldValue, value); } } private object _value; /// /// The default value of the setting /// public object DefaultValue { protected set; get; } /// /// The serialisationType of this setting /// public CECSettingSerialisationType SettingSerialisationType { private set; get; } /// /// The type of this setting /// public CECSettingType SettingType { private set; get; } /// /// True when changed and changes have not been persisted yet, false otherwise. /// public bool Changed { protected set; get; } /// /// The gui Control that contains the value /// public virtual Control ValueControl { get { return BaseValueControl; } } /// /// The value control to use in the gui /// protected Control BaseValueControl; /// /// True when changing the value of the ValueControl requires an invoke, false otherwise /// public virtual bool InvokeRequired { get { return BaseValueControl != null && BaseValueControl.InvokeRequired; } } /// /// The label with the description for this setting /// public Label Label { get { return _label ?? (_label = new Label {AutoSize = true, Size = new Size(100, 13), Text = FriendlyName}); } } private Label _label; /// /// Setting changed /// public event SettingChangedHandler SettingChanged; /// /// Setting will be enabled /// public EnableSettingHandler EnableSetting; /// /// The initial enabled state /// protected bool InitialEnabledValue { get { return _enabled; } } /// /// The enabled state of the gui control /// public virtual bool Enabled { set { var newValue = value; if (EnableSetting != null) newValue = EnableSetting(this, value); _enabled = newValue; if (Form != null) Form.SetControlEnabled(ValueControl, newValue); } get { return ValueControl != null ? ValueControl.Enabled : _enabled; } } /// /// The for that contains the gui controls /// protected IAsyncControls Form; private bool _enabled = true; #endregion } }