Imported Upstream version 2.2.0
[deb_libcec.git] / src / LibCecTray / settings / CECSetting.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.Drawing;
34using LibCECTray.ui;
35using Microsoft.Win32;
36using System.Windows.Forms;
37
38namespace LibCECTray.settings
39{
40 enum CECSettingSerialisationType
41 {
42 Numeric,
43 String
44 }
45
46 enum CECSettingType
47 {
48 Numeric,
49 String,
50 Bool,
51 Byte,
52 DeviceType,
53 LogicalAddress,
54 LogicalAddresses,
55 UShort,
56 VendorId,
57 Button,
58 Generic
59 }
60
61 /// <summary>
62 /// Base class for settings that can be persisted in the registry
63 /// </summary>
64 abstract class CECSetting
65 {
66 /// <summary>
67 /// Create a new setting
68 /// </summary>
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)
76 {
77 SettingType = type;
78 SettingSerialisationType = serialisationType;
79 KeyName = keyName;
80 FriendlyName = friendlyName;
81 DefaultValue = defaultValue;
82 _value = defaultValue;
83
84 if (changedHandler != null)
85 SettingChanged += changedHandler;
86 }
87
88 #region Serialisation methods
89 /// <summary>
90 /// Get the value of the setting in a form that can be stored in the registry
91 /// </summary>
92 /// <returns>The serialised value</returns>
93 protected abstract string GetSerialisedValue();
94
95 /// <summary>
96 /// Set the value from the serialised form of it.
97 /// </summary>
98 /// <param name="value">The serialised value</param>
99 protected abstract void SetSerialisedValue(string value);
100
101 /// <summary>
102 /// Get the default value of the setting in a form that can be stored in the registry
103 /// </summary>
104 /// <returns>The serialised default value</returns>
105 protected abstract string GetSerialisedDefaultValue();
106
107 /// <summary>
108 /// Set the default value from the serialised form of it.
109 /// </summary>
110 /// <param name="value">The serialised default value</param>
111 protected abstract void SetSerialisedDefaultValue(string value);
112 #endregion
113
114 /// <summary>
115 /// Set the value to the default.
116 /// </summary>
117 public void ResetDefaultValue()
118 {
119 Value = DefaultValue;
120 }
121
122 #region Read/Write the corresponding registry key
123 /// <summary>
124 /// Load the value from the registry
125 /// </summary>
126 /// <param name="key">The registry key to read the value from</param>
127 public void Load(RegistryKey key)
128 {
129 _value = key.GetValue(KeyName) ?? DefaultValue;
130 Changed = false;
131 }
132
133 /// <summary>
134 /// Persist the value in the registry
135 /// </summary>
136 /// <param name="key">The registry key to save the value to</param>
137 public void Persist(RegistryKey key)
138 {
139 if (_value != DefaultValue)
140 key.SetValue(KeyName, _value);
141 }
142 #endregion
143
144 #region GUI control replacement
145 /// <summary>
146 /// Replaces the controls in the form that was generated by the gui designer
147 /// </summary>
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)
153 {
154 Form = form;
155 ReplaceControl(controls, labelControl, Label);
156 ReplaceControl(controls, valueControl, ValueControl);
157 }
158
159 /// <summary>
160 /// Replaces the controls in the form that was generated by the gui designer
161 /// </summary>
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)
166 {
167 Form = form;
168 ReplaceControl(controls, valueControl, ValueControl);
169 }
170
171 /// <summary>
172 /// Replaces the controls in the form that was generated by the gui designer
173 /// </summary>
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)
178 {
179 if (originalControl == null)
180 return;
181
182 var location = originalControl.Location;
183 var originalSize = originalControl.Size;
184 var tabIndex = originalControl.TabIndex;
185
186 controls.Remove(originalControl);
187
188 if (replacement != null)
189 {
190 controls.Add(replacement);
191 replacement.Location = location;
192 replacement.Size = originalSize;
193 replacement.TabIndex = tabIndex;
194 }
195 }
196 #endregion
197
198 /// <summary>
199 /// A setting changed
200 /// </summary>
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);
205
206 /// <summary>
207 /// Checks if a setting may be enabled
208 /// </summary>
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);
213
214 #region Convenience methods
215 public CECSettingBool AsSettingBool
216 {
217 get { return this as CECSettingBool; }
218 }
219 public CECSettingByte AsSettingByte
220 {
221 get { return this as CECSettingByte; }
222 }
223 public CECSettingDeviceType AsSettingDeviceType
224 {
225 get { return this as CECSettingDeviceType; }
226 }
227 public CECSettingLogicalAddress AsSettingLogicalAddress
228 {
229 get { return this as CECSettingLogicalAddress; }
230 }
231 public CECSettingLogicalAddresses AsSettingLogicalAddresses
232 {
233 get { return this as CECSettingLogicalAddresses; }
234 }
235 public CECSettingNumeric AsSettingNumeric
236 {
237 get { return this as CECSettingNumeric; }
238 }
239 public CECSettingString AsSettingString
240 {
241 get { return this as CECSettingString; }
242 }
243 public CECSettingUShort AsSettingUShort
244 {
245 get { return this as CECSettingUShort; }
246 }
247 public CECSettingVendorId AsSettingVendorId
248 {
249 get { return this as CECSettingVendorId; }
250 }
251 #endregion
252
253 #region Members
254 /// <summary>
255 /// Name of the key in the registry
256 /// </summary>
257 public string KeyName { protected set; get; }
258
259 /// <summary>
260 /// Name of the setting in the UI
261 /// </summary>
262 public string FriendlyName { protected set; get; }
263
264 /// <summary>
265 /// The current value of the setting
266 /// </summary>
267 public object Value {
268 get { return _value; }
269 set
270 {
271 if (_value == value) return;
272 Changed = true;
273 var oldValue = _value;
274 _value = value;
275 if (SettingChanged != null)
276 SettingChanged(this, oldValue, value);
277 }
278 }
279 private object _value;
280
281 /// <summary>
282 /// The default value of the setting
283 /// </summary>
284 public object DefaultValue { protected set; get; }
285
286 /// <summary>
287 /// The serialisationType of this setting
288 /// </summary>
289 public CECSettingSerialisationType SettingSerialisationType { private set; get; }
290
291 /// <summary>
292 /// The type of this setting
293 /// </summary>
294 public CECSettingType SettingType { private set; get; }
295
296 /// <summary>
297 /// True when changed and changes have not been persisted yet, false otherwise.
298 /// </summary>
299 public bool Changed { protected set; get; }
300
301 /// <summary>
302 /// The gui Control that contains the value
303 /// </summary>
304 public virtual Control ValueControl { get { return BaseValueControl; } }
305
306 /// <summary>
307 /// The value control to use in the gui
308 /// </summary>
309 protected Control BaseValueControl;
310
311 /// <summary>
312 /// True when changing the value of the ValueControl requires an invoke, false otherwise
313 /// </summary>
314 public virtual bool InvokeRequired
315 {
316 get { return BaseValueControl != null && BaseValueControl.InvokeRequired; }
317 }
318
319 /// <summary>
320 /// The label with the description for this setting
321 /// </summary>
322 public Label Label
323 {
324 get {
325 return _label ??
326 (_label = new Label {AutoSize = true, Size = new Size(100, 13), Text = FriendlyName});
327 }
328 }
329 private Label _label;
330
331 /// <summary>
332 /// Setting changed
333 /// </summary>
334 public event SettingChangedHandler SettingChanged;
335
336 /// <summary>
337 /// Setting will be enabled
338 /// </summary>
339 public EnableSettingHandler EnableSetting;
340
341 /// <summary>
342 /// The initial enabled state
343 /// </summary>
344 protected bool InitialEnabledValue
345 {
346 get { return _enabled; }
347 }
348
349 /// <summary>
350 /// The enabled state of the gui control
351 /// </summary>
352 public virtual bool Enabled
353 {
354 set
355 {
356 var newValue = value;
357 if (EnableSetting != null)
358 newValue = EnableSetting(this, value);
359
360 _enabled = newValue;
361 if (Form != null)
362 Form.SetControlEnabled(ValueControl, newValue);
363 }
364 get
365 {
366 return ValueControl != null ? ValueControl.Enabled : _enabled;
367 }
368 }
369
370 /// <summary>
371 /// The for that contains the gui controls
372 /// </summary>
373 protected IAsyncControls Form;
374
375 private bool _enabled = true;
376 #endregion
377 }
378}