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/
34 using System.Windows.Forms;
36 using LibCECTray.controller.applications;
38 namespace LibCECTray.ui
40 interface IAsyncControls
42 void SetControlEnabled(Control control, bool val);
43 void SetControlText(Control control, string val);
44 void SetToolStripMenuText(ToolStripMenuItem item, string val);
45 void SetCheckboxChecked(CheckBox control, bool val);
46 void SetCheckboxItemChecked(CheckedListBox control, int index, bool val);
47 void SetProgressValue(ProgressBar control, int val);
48 void SetComboBoxItems(ComboBox control, int selectedIndex, object[] val);
49 void SetControlVisible(Control control, bool val);
50 void DisplayDialog(Form control, bool modal);
51 void SafeHide(bool val);
52 void SetSelectedIndex(ComboBox control, int index);
53 string GetSelectedTabName(TabControl container, TabControl.TabPageCollection tabPages);
54 void SelectKeypressRow(Control container, DataGridView dgView, CecKeypress key);
58 /// Utility methods to change GUI content from another thread
63 /// Enable or disable a control
65 /// <param name="container">The control that contains the control to change</param>
66 /// <param name="control">The control to change</param>
67 /// <param name="val">True to enable, false to disable</param>
68 public static void SetControlEnabled(Control container, Control control, bool val)
70 if (container == null || control == null) return;
71 if (container.InvokeRequired)
73 SetControlEnabledCallback d = SetControlEnabled;
76 container.Invoke(d, new object[] { container, control, val });
82 control.Enabled = val;
85 private delegate void SetControlEnabledCallback(Control container, Control control, bool val);
88 /// Change the text label of a control
90 /// <param name="container">The control that contains the control to change</param>
91 /// <param name="control">The control to change</param>
92 /// <param name="val">The new text</param>
93 public static void SetControlText(Control container, Control control, string val)
95 if (container == null || control == null) return;
96 if (container.InvokeRequired)
98 SetControlTextCallback d = SetControlText;
101 container.Invoke(d, new object[] { container, control, val });
110 private delegate void SetControlTextCallback(Control container, Control control, string val);
113 /// Change the checked status of a checkbox
115 /// <param name="container">The control that contains the control to change</param>
116 /// <param name="control">The control to change</param>
117 /// <param name="val">True to change to checked, false to change to unchecked</param>
118 public static void SetCheckboxChecked(Control container, CheckBox control, bool val)
120 if (container.InvokeRequired)
122 SetCheckboxCheckedCallback d = SetCheckboxChecked;
125 container.Invoke(d, new object[] { container, control, val });
131 control.Checked = val;
134 private delegate void SetCheckboxCheckedCallback(Control container, CheckBox control, bool val);
137 /// Change the checked status of an item in a CheckedListBox
139 /// <param name="container">The control that contains the control to change</param>
140 /// <param name="control">The control to change</param>
141 /// <param name="index">The index of the checkbox in the list to change</param>
142 /// <param name="val">True to change to checked, false to change to unchecked</param>
143 public static void SetCheckboxItemChecked(Control container, CheckedListBox control, int index, bool val)
145 if (container.InvokeRequired)
147 SetCheckboxItemCheckedCallback d = SetCheckboxItemChecked;
150 container.Invoke(d, new object[] { container, control, index, val });
152 catch (Exception) { }
156 control.SetItemChecked(index, val);
159 private delegate void SetCheckboxItemCheckedCallback(Control container, CheckedListBox control, int index, bool val);
162 /// Changes the toolstrip menu text
164 /// <param name="container">The control that contains the control to change</param>
165 /// <param name="item">The toolstrip menu item to change</param>
166 /// <param name="val">The new value</param>
167 public static void SetToolStripMenuText(Control container, ToolStripMenuItem item, string val)
169 if (container.InvokeRequired)
171 SetToolStripMenuTextCallback d = SetToolStripMenuText;
174 container.Invoke(d, new object[] { container, item, val });
176 catch (Exception) { }
183 private delegate void SetToolStripMenuTextCallback(Control container, ToolStripMenuItem item, string val);
186 /// Changes the progress value of a progress bar
188 /// <param name="container">The control that contains the control to change</param>
189 /// <param name="control">The control to change</param>
190 /// <param name="val">The new percentage</param>
191 public static void SetProgressValue(Control container, ProgressBar control, int val)
193 if (container.InvokeRequired)
195 SetProgressValueCallback d = SetProgressValue;
198 container.Invoke(d, new object[] { container, control, val });
200 catch (Exception) { }
207 private delegate void SetProgressValueCallback(Control container, ProgressBar control, int val);
210 /// Replaces the items of a combobox
212 /// <param name="container">The control that contains the control to change</param>
213 /// <param name="control">The control to change</param>
214 /// <param name="selectedIndex">The new selection index</param>
215 /// <param name="val">The new content</param>
216 public static void SetComboBoxItems(Control container, ComboBox control, int selectedIndex, object[] val)
218 if (container.InvokeRequired)
220 SetComboBoxItemsCallback d = SetComboBoxItems;
223 container.Invoke(d, new object[] { container, control, selectedIndex, val });
225 catch (Exception) { }
229 control.Items.Clear();
230 control.Items.AddRange(val);
231 if (control.Items.Count > 0)
232 control.SelectedIndex = selectedIndex;
235 private delegate void SetComboBoxItemsCallback(Control container, ComboBox control, int selectedIndex, object[] val);
238 /// Make a control visible or invisible
240 /// <param name="container">The control that contains the control to change</param>
241 /// <param name="control">The control to change</param>
242 /// <param name="val">True to make it visible, false to make it invisible</param>
243 public static void SetControlVisible(Control container, Control control, bool val)
245 if (container.InvokeRequired)
247 SetControlVisibleCallback d = SetControlVisible;
250 container.Invoke(d, new object[] { container, control, val });
252 catch (Exception) { }
256 control.Visible = val;
259 private delegate void SetControlVisibleCallback(Control container, Control control, bool val);
262 /// Display a new dialog
264 /// <param name="container">The control that contains the control to change</param>
265 /// <param name="control">The control to display</param>
266 /// <param name="modal">True to make it a modal dialog</param>
267 public static void DisplayDialog(Control container, Form control, bool modal)
269 if (container.InvokeRequired)
271 DisplayDialogCallback d = DisplayDialog;
274 container.Invoke(d, new object[] { container, control, modal });
276 catch (Exception) { }
281 control.ShowDialog(container);
283 control.Show(container);
286 private delegate void DisplayDialogCallback(Control container, Form control, bool modal);
291 /// <param name="container">The control to hide</param>
292 /// <param name="val">True to hide, false to show</param>
293 public static void SafeHide(Control container, bool val)
295 if (container.InvokeRequired)
297 SafeHideCallback d = SafeHide;
300 container.Invoke(d, new object[] { container, val });
302 catch (Exception) { }
312 private delegate void SafeHideCallback(Control container, bool val);
315 /// Change the selected index
317 /// <param name="container">The control that contains the control to change</param>
318 /// <param name="control">The control to change</param>
319 /// <param name="index">The new selected index</param>
320 public static void SetSelectedIndex(Control container, ComboBox control, int index)
322 if (container.InvokeRequired)
324 SetSelectedIndexCallback d = SetSelectedIndex;
327 container.Invoke(d, new object[] { container, control, index });
329 catch (Exception) { }
333 control.SelectedIndex = index;
336 private delegate void SetSelectedIndexCallback(Control container, ComboBox control, int index);
339 /// Get the name of the selected tab in a TabControl
341 /// <param name="container">The tab container</param>
342 /// <param name="tabPages">The tab pages</param>
343 /// <returns>The name of the selected tab</returns>
344 public static string GetSelectedTabName(TabControl container, TabControl.TabPageCollection tabPages)
346 if (container.InvokeRequired)
348 GetSelectedTabNameCallback d = GetSelectedTabName;
351 return container.Invoke(d, new object[] { container, tabPages }) as string;
353 catch (Exception) { }
357 return tabPages[container.SelectedIndex].Name;
361 private delegate string GetSelectedTabNameCallback(TabControl container, TabControl.TabPageCollection tabPages);
364 /// Selects the row with the given CecKeypress for a datagrid
366 /// <param name="container">The datagrid container</param>
367 /// <param name="dgView">The datagrid</param>
368 /// <param name="key">The key to selected</param>
369 public static void SelectKeypressRow(Control container, DataGridView dgView, CecKeypress key)
371 if (dgView.InvokeRequired)
373 SelectKeypressRowCallback d = SelectKeypressRow;
376 container.Invoke(d, new object[] { container, dgView, key });
378 catch (Exception) { }
383 foreach (DataGridViewRow row in dgView.Rows)
385 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
386 if (item != null && item.Key.Keycode == key.Keycode)
388 rowIndex = row.Index;
394 row.Selected = false;
398 dgView.FirstDisplayedScrollingRowIndex = rowIndex;
401 private delegate void SelectKeypressRowCallback(Control container, DataGridView dgView, CecKeypress key);
405 /// Form that implements IAsyncControls
407 class AsyncForm : Form, IAsyncControls
410 /// Changes the ShowInTaskbar value
412 /// <param name="val">True to show, false to hide</param>
413 public void SetShowInTaskbar(bool val)
417 SetShowInTaskbarCallback d = SetShowInTaskbar;
420 Invoke(d, new object[] { val });
422 catch (Exception) { }
429 private delegate void SetShowInTaskbarCallback(bool val);
432 /// Enable or disable a control
434 /// <param name="control">The control to change</param>
435 /// <param name="val">True to enable, false to disable</param>
436 public void SetControlEnabled(Control control, bool val)
438 AsyncControls.SetControlEnabled(this, control, val);
442 /// Change the text label of a control
444 /// <param name="control">The control to change</param>
445 /// <param name="val">The new text</param>
446 public void SetControlText(Control control, string val)
448 AsyncControls.SetControlText(this, control, val);
452 /// Changes the toolstrip menu text
454 /// <param name="item">The toolstrip menu item to change</param>
455 /// <param name="val">The new value</param>
456 public void SetToolStripMenuText(ToolStripMenuItem item, string val)
458 AsyncControls.SetToolStripMenuText(this, item, val);
462 /// Change the checked status of a checkbox
464 /// <param name="control">The control to change</param>
465 /// <param name="val">True to change to checked, false to change to unchecked</param>
466 public void SetCheckboxChecked(CheckBox control, bool val)
468 AsyncControls.SetCheckboxChecked(this, control, val);
472 /// Change the checked status of an item in a CheckedListBox
474 /// <param name="control">The control to change</param>
475 /// <param name="index">The index of the checkbox in the list to change</param>
476 /// <param name="val">True to change to checked, false to change to unchecked</param>
477 public void SetCheckboxItemChecked(CheckedListBox control, int index, bool val)
479 AsyncControls.SetCheckboxItemChecked(this, control, index, val);
483 /// Changes the progress value of a progress bar
485 /// <param name="control">The control to change</param>
486 /// <param name="val">The new percentage</param>
487 public void SetProgressValue(ProgressBar control, int val)
489 AsyncControls.SetProgressValue(this, control, val);
493 /// Replaces the items of a combobox
495 /// <param name="control">The control to change</param>
496 /// <param name="selectedIndex">The new selection index</param>
497 /// <param name="val">The new content</param>
498 public void SetComboBoxItems(ComboBox control, int selectedIndex, object[] val)
500 AsyncControls.SetComboBoxItems(this, control, selectedIndex, val);
504 /// Make a control visible or invisible
506 /// <param name="control">The control to change</param>
507 /// <param name="val">True to make it visible, false to make it invisible</param>
508 public void SetControlVisible(Control control, bool val)
510 AsyncControls.SetControlVisible(this, control, val);
514 /// Display a new dialog
516 /// <param name="control">The control to display</param>
517 /// <param name="modal">True to make it a modal dialog</param>
518 public void DisplayDialog(Form control, bool modal)
520 AsyncControls.DisplayDialog(this, control, modal);
526 /// <param name="val">True to hide, false to show</param>
527 public void SafeHide(bool val)
529 AsyncControls.SafeHide(this, val);
533 /// Change the selected index
535 /// <param name="control">The control to change</param>
536 /// <param name="index">The new selected index</param>
537 public void SetSelectedIndex(ComboBox control, int index)
539 AsyncControls.SetSelectedIndex(this, control, index);
543 /// Get the name of the selected tab in a TabControl
545 /// <param name="container">The tab container</param>
546 /// <param name="tabPages">The tab pages</param>
547 /// <returns>The name of the selected tab</returns>
548 public string GetSelectedTabName(TabControl container, TabControl.TabPageCollection tabPages)
550 return AsyncControls.GetSelectedTabName(container, tabPages);
554 /// Selects the row with the given CecKeypress for a datagrid
556 /// <param name="container">The datagrid container</param>
557 /// <param name="dgView">The datagrid</param>
558 /// <param name="key">The key to selected</param>
559 public void SelectKeypressRow(Control container, DataGridView dgView, CecKeypress key)
561 AsyncControls.SelectKeypressRow(container, dgView, key);
566 /// TabPage that implements IAsyncControls
568 class AsyncTabPage : TabPage, IAsyncControls
571 /// Enable or disable a control
573 /// <param name="control">The control to change</param>
574 /// <param name="val">True to enable, false to disable</param>
575 public void SetControlEnabled(Control control, bool val)
577 AsyncControls.SetControlEnabled(this, control, val);
581 /// Change the text label of a control
583 /// <param name="control">The control to change</param>
584 /// <param name="val">The new text</param>
585 public void SetControlText(Control control, string val)
587 AsyncControls.SetControlText(this, control, val);
591 /// Changes the toolstrip menu text
593 /// <param name="item">The toolstrip menu item to change</param>
594 /// <param name="val">The new value</param>
595 public void SetToolStripMenuText(ToolStripMenuItem item, string val)
597 AsyncControls.SetToolStripMenuText(this, item, val);
601 /// Change the checked status of a checkbox
603 /// <param name="control">The control to change</param>
604 /// <param name="val">True to change to checked, false to change to unchecked</param>
605 public void SetCheckboxChecked(CheckBox control, bool val)
607 AsyncControls.SetCheckboxChecked(this, control, val);
611 /// Change the checked status of an item in a CheckedListBox
613 /// <param name="control">The control to change</param>
614 /// <param name="index">The index of the checkbox in the list to change</param>
615 /// <param name="val">True to change to checked, false to change to unchecked</param>
616 public void SetCheckboxItemChecked(CheckedListBox control, int index, bool val)
618 AsyncControls.SetCheckboxItemChecked(this, control, index, val);
622 /// Changes the progress value of a progress bar
624 /// <param name="control">The control to change</param>
625 /// <param name="val">The new percentage</param>
626 public void SetProgressValue(ProgressBar control, int val)
628 AsyncControls.SetProgressValue(this, control, val);
632 /// Replaces the items of a combobox
634 /// <param name="control">The control to change</param>
635 /// <param name="selectedIndex">The new selection index</param>
636 /// <param name="val">The new content</param>
637 public void SetComboBoxItems(ComboBox control, int selectedIndex, object[] val)
639 AsyncControls.SetComboBoxItems(this, control, selectedIndex, val);
643 /// Make a control visible or invisible
645 /// <param name="control">The control to change</param>
646 /// <param name="val">True to make it visible, false to make it invisible</param>
647 public void SetControlVisible(Control control, bool val)
649 AsyncControls.SetControlVisible(this, control, val);
653 /// Display a new dialog
655 /// <param name="control">The control to display</param>
656 /// <param name="modal">True to make it a modal dialog</param>
657 public void DisplayDialog(Form control, bool modal)
659 AsyncControls.DisplayDialog(this, control, modal);
665 /// <param name="val">True to hide, false to show</param>
666 public void SafeHide(bool val)
668 AsyncControls.SafeHide(this, val);
672 /// Change the selected index
674 /// <param name="control">The control to change</param>
675 /// <param name="index">The new selected index</param>
676 public void SetSelectedIndex(ComboBox control, int index)
678 AsyncControls.SetSelectedIndex(this, control, index);
682 /// Get the name of the selected tab in a TabControl
684 /// <param name="container">The tab container</param>
685 /// <param name="tabPages">The tab pages</param>
686 /// <returns>The name of the selected tab</returns>
687 public string GetSelectedTabName(TabControl container, TabControl.TabPageCollection tabPages)
689 return AsyncControls.GetSelectedTabName(container, tabPages);
693 /// Selects the row with the given CecKeypress for a datagrid
695 /// <param name="container">The datagrid container</param>
696 /// <param name="dgView">The datagrid</param>
697 /// <param name="key">The key to selected</param>
698 public void SelectKeypressRow(Control container, DataGridView dgView, CecKeypress key)
700 AsyncControls.SelectKeypressRow(container, dgView, key);