Imported Upstream version 2.2.0
[deb_libcec.git] / src / LibCecTray / settings / CECSettingLogicalAddresses.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;
34using System.Windows.Forms;
35using CecSharp;
36using LibCECTray.Properties;
37using LibCECTray.ui;
38
39namespace LibCECTray.settings
40{
41 /// <summary>
42 /// A setting of type CecLogicalAddresses that can be persisted in the registry
43 /// </summary>
44 class CECSettingLogicalAddresses : CECSettingNumeric
45 {
46 public CECSettingLogicalAddresses(string keyName, string friendlyName, CecLogicalAddresses defaultValue, SettingChangedHandler changedHandler) :
47 base(CECSettingType.LogicalAddresses, keyName, friendlyName, SerialiseLogicalAddresses(defaultValue), changedHandler)
48 {
49 }
50
51 private static int SerialiseLogicalAddresses(CecLogicalAddresses addresses)
52 {
53 var retVal = 0;
54 for (int addr = (int)CecLogicalAddress.Tv; addr <= (int)CecLogicalAddress.Broadcast; addr++)
55 {
56 if (addresses.IsSet((CecLogicalAddress)addr))
57 {
58 retVal += (int)Math.Pow(2, addr);
59 }
60 }
61
62 return retVal;
63 }
64
65 private static CecLogicalAddresses DeserialiseLogicalAddresses(int addresses)
66 {
67 CecLogicalAddresses retVal = new CecLogicalAddresses();
68 if (addresses == 0)
69 return retVal;
70
71 for (int addr = (int)CecLogicalAddress.Tv; addr <= (int)CecLogicalAddress.Broadcast; addr++)
72 {
73 int val = (int)Math.Pow(2, addr);
74 if ((addresses & val) == val)
75 retVal.Set((CecLogicalAddress)addr);
76 }
77
78 return retVal;
79 }
80
81 public new CecLogicalAddresses Value
82 {
83 get { return DeserialiseLogicalAddresses(base.Value); }
84 set
85 {
86 base.Value = SerialiseLogicalAddresses(value);
87 if (Form == null) return;
88 for (int iPtr = 0; iPtr < 15; iPtr++)
89 Form.SetCheckboxItemChecked(ValueControl, iPtr, value.IsSet((CecLogicalAddress) iPtr));
90 }
91 }
92
93 public new CecLogicalAddresses DefaultValue
94 {
95 get { return DeserialiseLogicalAddresses(base.DefaultValue); }
96 set { base.DefaultValue = SerialiseLogicalAddresses(value); }
97 }
98
99 public static CecLogicalAddress GetLogicalAddressFromString(string name)
100 {
101 switch (name.Substring(0, 1).ToLower())
102 {
103 case "0":
104 return CecLogicalAddress.Tv;
105 case "1":
106 return CecLogicalAddress.RecordingDevice1;
107 case "2":
108 return CecLogicalAddress.RecordingDevice2;
109 case "3":
110 return CecLogicalAddress.Tuner1;
111 case "4":
112 return CecLogicalAddress.PlaybackDevice1;
113 case "5":
114 return CecLogicalAddress.AudioSystem;
115 case "6":
116 return CecLogicalAddress.Tuner2;
117 case "7":
118 return CecLogicalAddress.Tuner3;
119 case "8":
120 return CecLogicalAddress.PlaybackDevice2;
121 case "9":
122 return CecLogicalAddress.RecordingDevice3;
123 case "a":
124 return CecLogicalAddress.Tuner4;
125 case "b":
126 return CecLogicalAddress.PlaybackDevice3;
127 case "c":
128 return CecLogicalAddress.Reserved1;
129 case "d":
130 return CecLogicalAddress.Reserved2;
131 case "e":
132 return CecLogicalAddress.FreeUse;
133 case "f":
134 return CecLogicalAddress.Broadcast;
135 default:
136 return CecLogicalAddress.Unknown;
137 }
138 }
139
140 public new CheckedListBox ValueControl
141 {
142 get
143 {
144 if (BaseValueControl == null)
145 {
146 CheckedListBox control = new CheckedListBox
147 {
148 FormattingEnabled = true,
149 Name = KeyName,
150 Size = new System.Drawing.Size(118, 94),
151 Enabled = InitialEnabledValue
152 };
153 control.Items.AddRange(new object[] {
154 "0: " + Resources.device_tv,
155 "1: " + Resources.device_recorder + " 1",
156 "2: " + Resources.device_recorder + " 2",
157 "3: " + Resources.device_tuner + " 1",
158 "4: " + Resources.device_playbackdevice + " 1",
159 "5: " + Resources.device_audiosystem,
160 "6: " + Resources.device_tuner + " 2",
161 "7: " + Resources.device_tuner + " 3",
162 "8: " + Resources.device_playbackdevice + " 2",
163 "9: " + Resources.device_recorder + " 3",
164 "A: " + Resources.device_tuner + " 4",
165 "B: " + Resources.device_playbackdevice + " 3",
166 "C: " + Resources.device_reserved + " 1",
167 "D: " + Resources.device_reserved + " 2",
168 "E: " + Resources.device_free_use,
169 "F: " + Resources.device_broadcast});
170 control.SelectedValueChanged += delegate
171 {
172 CecLogicalAddresses addr = new CecLogicalAddresses();
173 foreach (var item in ((CheckedListBox)BaseValueControl).CheckedItems)
174 {
175 string c = item as string;
176 addr.Set(GetLogicalAddressFromString(c));
177 }
178 Value = addr;
179 };
180 BaseValueControl = control;
181 }
182 return BaseValueControl as CheckedListBox;
183 }
184 }
185
186 public void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, Control labelControl, CheckedListBox valueControl)
187 {
188 Form = form;
189 ReplaceControl(controls, labelControl, Label);
190 ReplaceControl(controls, valueControl, ValueControl);
191 }
192
193 public void ReplaceControls(IAsyncControls form, Control.ControlCollection controls, CheckedListBox valueControl)
194 {
195 Form = form;
196 ReplaceControl(controls, valueControl, ValueControl);
197 }
198
199 public override bool Enabled
200 {
201 set
202 {
203 if (Form != null)
204 Form.SetControlEnabled(ValueControl, value);
205 }
206 get { return base.Enabled; }
207 }
208 }
209}