2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 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/
34 using System.Windows.Forms;
37 using LibCECTray.Properties;
38 using LibCECTray.controller;
39 using LibCECTray.controller.applications;
40 using LibCECTray.settings;
42 namespace LibCECTray.ui
45 /// The tab pages in this application
47 internal enum ConfigTab
58 /// Main LibCecTray GUI
60 partial class CECTray : AsyncForm
64 Text = Resources.app_name;
65 InitializeComponent();
66 VisibleChanged += delegate
75 public override sealed string Text
77 get { return base.Text; }
78 set { base.Text = value; }
81 private void CECTrayLoad(object sender, EventArgs e)
83 Controller.Initialise();
86 protected override void Dispose(bool disposing)
93 if (disposing && (components != null))
97 base.Dispose(disposing);
100 #region Configuration tab
102 /// Replaces the gui controls by the ones that are bound to the settings.
103 /// this is a fugly way to do it, but the gui designer doesn't allow us to ref CECSettings, since it uses symbols from LibCecSharp
105 public void InitialiseSettingsComponent(CECSettings settings)
107 settings.WakeDevices.ReplaceControls(this, Configuration.Controls, lWakeDevices, cbWakeDevices);
108 settings.PowerOffDevices.ReplaceControls(this, Configuration.Controls, lPowerOff, cbPowerOffDevices);
109 settings.OverridePhysicalAddress.ReplaceControls(this, Configuration.Controls, cbOverrideAddress);
110 settings.OverrideTVVendor.ReplaceControls(this, Configuration.Controls, cbVendorOverride);
111 settings.PhysicalAddress.ReplaceControls(this, Configuration.Controls, tbPhysicalAddress);
112 settings.HDMIPort.ReplaceControls(this, Configuration.Controls, lPortNumber, cbPortNumber);
113 settings.ConnectedDevice.ReplaceControls(this, Configuration.Controls, lConnectedDevice, cbConnectedDevice);
114 settings.ActivateSource.ReplaceControls(this, Configuration.Controls, cbActivateSource);
115 settings.DeviceType.ReplaceControls(this, Configuration.Controls, lDeviceType, cbDeviceType);
116 settings.TVVendor.ReplaceControls(this, Configuration.Controls, cbVendorId);
117 settings.StartHidden.ReplaceControls(this, Configuration.Controls, cbStartMinimised);
120 private void BSaveClick(object sender, EventArgs e)
122 Controller.PersistSettings();
125 private void BReloadConfigClick(object sender, EventArgs e)
127 Controller.ResetDefaultSettings();
131 #region CEC Tester tab
132 delegate void SetActiveDevicesCallback(string[] activeDevices);
133 public void SetActiveDevices(string[] activeDevices)
135 if (cbCommandDestination.InvokeRequired)
137 SetActiveDevicesCallback d = SetActiveDevices;
140 Invoke(d, new object[] { activeDevices });
142 catch (Exception) { }
146 cbCommandDestination.Items.Clear();
147 foreach (string item in activeDevices)
148 cbCommandDestination.Items.Add(item);
152 delegate CecLogicalAddress GetTargetDeviceCallback();
153 private CecLogicalAddress GetTargetDevice()
155 if (cbCommandDestination.InvokeRequired)
157 GetTargetDeviceCallback d = GetTargetDevice;
158 CecLogicalAddress retval = CecLogicalAddress.Unknown;
161 retval = (CecLogicalAddress)Invoke(d, new object[] { });
163 catch (Exception) { }
167 return CECSettingLogicalAddresses.GetLogicalAddressFromString(cbCommandDestination.Text);
170 private void BSendImageViewOnClick(object sender, EventArgs e)
172 Controller.CECActions.SendImageViewOn(GetTargetDevice());
175 private void BStandbyClick(object sender, EventArgs e)
177 Controller.CECActions.SendStandby(GetTargetDevice());
180 private void BScanClick(object sender, EventArgs e)
182 Controller.CECActions.ShowDeviceInfo(GetTargetDevice());
185 private void BActivateSourceClick(object sender, EventArgs e)
187 Controller.CECActions.ActivateSource(GetTargetDevice());
190 private void CbCommandDestinationSelectedIndexChanged(object sender, EventArgs e)
192 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
193 bVolUp.Enabled = enableVolumeButtons;
194 bVolDown.Enabled = enableVolumeButtons;
195 bMute.Enabled = enableVolumeButtons;
196 bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
197 bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
200 private void BVolUpClick(object sender, EventArgs e)
202 Controller.Lib.VolumeUp(true);
205 private void BVolDownClick(object sender, EventArgs e)
207 Controller.Lib.VolumeDown(true);
210 private void BMuteClick(object sender, EventArgs e)
212 Controller.Lib.MuteAudio(true);
215 private void BRescanDevicesClick(object sender, EventArgs e)
217 Controller.CECActions.RescanDevices();
222 delegate void UpdateLogCallback();
223 private void UpdateLog()
225 if (tbLog.InvokeRequired)
227 UpdateLogCallback d = UpdateLog;
230 Invoke(d, new object[] { });
232 catch (Exception) { }
237 tbLog.Select(tbLog.Text.Length, 0);
238 tbLog.ScrollToCaret();
242 public void AddLogMessage(CecLogMessage message)
244 string strLevel = "";
245 bool display = false;
246 switch (message.Level)
248 case CecLogLevel.Error:
249 strLevel = "ERROR: ";
250 display = cbLogError.Checked;
252 case CecLogLevel.Warning:
253 strLevel = "WARNING: ";
254 display = cbLogWarning.Checked;
256 case CecLogLevel.Notice:
257 strLevel = "NOTICE: ";
258 display = cbLogNotice.Checked;
260 case CecLogLevel.Traffic:
261 strLevel = "TRAFFIC: ";
262 display = cbLogTraffic.Checked;
264 case CecLogLevel.Debug:
265 strLevel = "DEBUG: ";
266 display = cbLogDebug.Checked;
272 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + Environment.NewLine;
273 AddLogMessage(strLog);
277 public void AddLogMessage(string message)
281 if (_selectedTab == ConfigTab.Log)
285 private void BClearLogClick(object sender, EventArgs e)
291 private void BSaveLogClick(object sender, EventArgs e)
293 SaveFileDialog dialog = new SaveFileDialog
295 Title = Resources.where_do_you_want_to_store_the_log,
296 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
297 FileName = Resources.cec_log_filename,
298 Filter = Resources.cec_log_filter,
302 if (dialog.ShowDialog() == DialogResult.OK)
304 FileStream fs = (FileStream)dialog.OpenFile();
307 MessageBox.Show(string.Format(Resources.cannot_open_for_writing, dialog.FileName), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Error);
311 StreamWriter writer = new StreamWriter(fs);
316 MessageBox.Show(string.Format(Resources.log_stored_as, dialog.FileName), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
322 #region Tray icon and window controls
323 private void HideToolStripMenuItemClick(object sender, EventArgs e)
328 private void CloseToolStripMenuItemClick(object sender, EventArgs e)
333 private void AboutToolStripMenuItemClick(object sender, EventArgs e)
335 (new About(Controller.LibServerVersion, Controller.LibClientVersion, Controller.LibInfo)).ShowDialog();
338 private void AdvancedModeToolStripMenuItemClick(object sender, EventArgs e)
340 Controller.Settings.AdvancedMode.Value = !advancedModeToolStripMenuItem.Checked;
341 ShowHideAdvanced(!advancedModeToolStripMenuItem.Checked);
344 private void BCancelClick(object sender, EventArgs e)
349 private void TrayIconClick(object sender, EventArgs e)
351 if (e is MouseEventArgs && (e as MouseEventArgs).Button == MouseButtons.Left)
357 ShowInTaskbar = false;
359 tsMenuShowHide.Text = Resources.show;
364 ShowInTaskbar = true;
365 WindowState = FormWindowState.Normal;
367 tsMenuShowHide.Text = Resources.hide;
370 private void ShowHideToggle()
372 if (Visible && WindowState != FormWindowState.Minimized)
374 Controller.Settings.StartHidden.Value = true;
379 Controller.Settings.StartHidden.Value = false;
384 private void TsMenuCloseClick(object sender, EventArgs e)
389 private void CECTrayResize(object sender, EventArgs e)
391 if (WindowState == FormWindowState.Minimized)
397 private void TsMenuShowHideClick(object sender, EventArgs e)
402 public void ShowHideAdvanced(bool setTo)
406 tsAdvanced.Checked = true;
407 advancedModeToolStripMenuItem.Checked = true;
409 if (!tabPanel.Controls.Contains(tbTestCommands))
410 TabControls.Add(tbTestCommands);
411 if (!tabPanel.Controls.Contains(LogOutput))
412 TabControls.Add(LogOutput);
417 tsAdvanced.Checked = false;
418 advancedModeToolStripMenuItem.Checked = false;
420 tabPanel.Controls.Remove(tbTestCommands);
421 tabPanel.Controls.Remove(LogOutput);
426 private void TsAdvancedClick(object sender, EventArgs e)
428 Controller.Settings.AdvancedMode.Value = !tsAdvanced.Checked;
429 ShowHideAdvanced(!tsAdvanced.Checked);
432 public void SetStatusText(string status)
434 SetControlText(lStatus, status);
437 public void SetProgressBar(int progress, bool visible)
439 SetControlVisible(pProgress, visible);
440 SetProgressValue(pProgress, progress);
443 public void SetControlsEnabled(bool val)
446 SetControlEnabled(bClose, val);
447 SetControlEnabled(bSaveConfig, val);
448 SetControlEnabled(bReloadConfig, val);
451 SetControlEnabled(bRescanDevices, val);
452 SetControlEnabled(bSendImageViewOn, val);
453 SetControlEnabled(bStandby, val);
454 SetControlEnabled(bActivateSource, val);
455 SetControlEnabled(bScan, val);
457 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
458 SetControlEnabled(bVolUp, enableVolumeButtons);
459 SetControlEnabled(bVolDown, enableVolumeButtons);
460 SetControlEnabled(bMute, enableVolumeButtons);
463 private void TabControl1SelectedIndexChanged(object sender, EventArgs e)
465 switch (tabPanel.TabPages[tabPanel.SelectedIndex].Name)
467 case "tbTestCommands":
468 _selectedTab = ConfigTab.Tester;
471 _selectedTab = ConfigTab.Log;
475 _selectedTab = ConfigTab.Configuration;
481 #region Class members
482 private ConfigTab _selectedTab = ConfigTab.Configuration;
483 private string _log = string.Empty;
484 private CECController _controller;
485 public CECController Controller
487 get { return _controller ?? (_controller = new CECController(this)); }
489 public Control.ControlCollection TabControls
491 get { return tabPanel.Controls; }
493 public string SelectedTabName
495 get { return GetSelectedTabName(tabPanel, tabPanel.TabPages); }
499 private void AddNewApplicationToolStripMenuItemClick(object sender, EventArgs e)
501 ConfigureApplication appConfig = new ConfigureApplication(Controller.Settings, Controller);
502 Controller.DisplayDialog(appConfig, false);