464547bad2a57b173f633b7f61598065cdf15504
[deb_libcec.git] / src / LibCecTray / ui / AsyncForm.cs
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
33 using System;
34 using System.Windows.Forms;
35 using CecSharp;
36 using LibCECTray.controller.applications;
37
38 namespace LibCECTray.ui
39 {
40 interface IAsyncControls
41 {
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);
55 }
56
57 /// <summary>
58 /// Utility methods to change GUI content from another thread
59 /// </summary>
60 class AsyncControls
61 {
62 /// <summary>
63 /// Enable or disable a control
64 /// </summary>
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)
69 {
70 if (container == null || control == null) return;
71 if (container.InvokeRequired)
72 {
73 SetControlEnabledCallback d = SetControlEnabled;
74 try
75 {
76 container.Invoke(d, new object[] { container, control, val });
77 }
78 catch { }
79 }
80 else
81 {
82 control.Enabled = val;
83 }
84 }
85 private delegate void SetControlEnabledCallback(Control container, Control control, bool val);
86
87 /// <summary>
88 /// Change the text label of a control
89 /// </summary>
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)
94 {
95 if (container == null || control == null) return;
96 if (container.InvokeRequired)
97 {
98 SetControlTextCallback d = SetControlText;
99 try
100 {
101 container.Invoke(d, new object[] { container, control, val });
102 }
103 catch { }
104 }
105 else
106 {
107 control.Text = val;
108 }
109 }
110 private delegate void SetControlTextCallback(Control container, Control control, string val);
111
112 /// <summary>
113 /// Change the checked status of a checkbox
114 /// </summary>
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)
119 {
120 if (container.InvokeRequired)
121 {
122 SetCheckboxCheckedCallback d = SetCheckboxChecked;
123 try
124 {
125 container.Invoke(d, new object[] { container, control, val });
126 }
127 catch { }
128 }
129 else
130 {
131 control.Checked = val;
132 }
133 }
134 private delegate void SetCheckboxCheckedCallback(Control container, CheckBox control, bool val);
135
136 /// <summary>
137 /// Change the checked status of an item in a CheckedListBox
138 /// </summary>
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)
144 {
145 if (container.InvokeRequired)
146 {
147 SetCheckboxItemCheckedCallback d = SetCheckboxItemChecked;
148 try
149 {
150 container.Invoke(d, new object[] { container, control, index, val });
151 }
152 catch (Exception) { }
153 }
154 else
155 {
156 control.SetItemChecked(index, val);
157 }
158 }
159 private delegate void SetCheckboxItemCheckedCallback(Control container, CheckedListBox control, int index, bool val);
160
161 /// <summary>
162 /// Changes the toolstrip menu text
163 /// </summary>
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)
168 {
169 if (container.InvokeRequired)
170 {
171 SetToolStripMenuTextCallback d = SetToolStripMenuText;
172 try
173 {
174 container.Invoke(d, new object[] { container, item, val });
175 }
176 catch (Exception) { }
177 }
178 else
179 {
180 item.Text = val;
181 }
182 }
183 private delegate void SetToolStripMenuTextCallback(Control container, ToolStripMenuItem item, string val);
184
185 /// <summary>
186 /// Changes the progress value of a progress bar
187 /// </summary>
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)
192 {
193 if (container.InvokeRequired)
194 {
195 SetProgressValueCallback d = SetProgressValue;
196 try
197 {
198 container.Invoke(d, new object[] { container, control, val });
199 }
200 catch (Exception) { }
201 }
202 else
203 {
204 control.Value = val;
205 }
206 }
207 private delegate void SetProgressValueCallback(Control container, ProgressBar control, int val);
208
209 /// <summary>
210 /// Replaces the items of a combobox
211 /// </summary>
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)
217 {
218 if (container.InvokeRequired)
219 {
220 SetComboBoxItemsCallback d = SetComboBoxItems;
221 try
222 {
223 container.Invoke(d, new object[] { container, control, selectedIndex, val });
224 }
225 catch (Exception) { }
226 }
227 else
228 {
229 control.Items.Clear();
230 control.Items.AddRange(val);
231 if (control.Items.Count > 0)
232 control.SelectedIndex = selectedIndex;
233 }
234 }
235 private delegate void SetComboBoxItemsCallback(Control container, ComboBox control, int selectedIndex, object[] val);
236
237 /// <summary>
238 /// Make a control visible or invisible
239 /// </summary>
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)
244 {
245 if (container.InvokeRequired)
246 {
247 SetControlVisibleCallback d = SetControlVisible;
248 try
249 {
250 container.Invoke(d, new object[] { container, control, val });
251 }
252 catch (Exception) { }
253 }
254 else
255 {
256 control.Visible = val;
257 }
258 }
259 private delegate void SetControlVisibleCallback(Control container, Control control, bool val);
260
261 /// <summary>
262 /// Display a new dialog
263 /// </summary>
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)
268 {
269 if (container.InvokeRequired)
270 {
271 DisplayDialogCallback d = DisplayDialog;
272 try
273 {
274 container.Invoke(d, new object[] { container, control, modal });
275 }
276 catch (Exception) { }
277 }
278 else
279 {
280 if (modal)
281 control.ShowDialog(container);
282 else
283 control.Show(container);
284 }
285 }
286 private delegate void DisplayDialogCallback(Control container, Form control, bool modal);
287
288 /// <summary>
289 /// Hides a control
290 /// </summary>
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)
294 {
295 if (container.InvokeRequired)
296 {
297 SafeHideCallback d = SafeHide;
298 try
299 {
300 container.Invoke(d, new object[] { container, val });
301 }
302 catch (Exception) { }
303 }
304 else
305 {
306 if (val)
307 container.Hide();
308 else
309 container.Show();
310 }
311 }
312 private delegate void SafeHideCallback(Control container, bool val);
313
314 /// <summary>
315 /// Change the selected index
316 /// </summary>
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)
321 {
322 if (container.InvokeRequired)
323 {
324 SetSelectedIndexCallback d = SetSelectedIndex;
325 try
326 {
327 container.Invoke(d, new object[] { container, control, index });
328 }
329 catch (Exception) { }
330 }
331 else
332 {
333 control.SelectedIndex = index;
334 }
335 }
336 private delegate void SetSelectedIndexCallback(Control container, ComboBox control, int index);
337
338 /// <summary>
339 /// Get the name of the selected tab in a TabControl
340 /// </summary>
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)
345 {
346 if (container.InvokeRequired)
347 {
348 GetSelectedTabNameCallback d = GetSelectedTabName;
349 try
350 {
351 return container.Invoke(d, new object[] { container, tabPages }) as string;
352 }
353 catch (Exception) { }
354 }
355 else
356 {
357 return tabPages[container.SelectedIndex].Name;
358 }
359 return string.Empty;
360 }
361 private delegate string GetSelectedTabNameCallback(TabControl container, TabControl.TabPageCollection tabPages);
362
363 /// <summary>
364 /// Selects the row with the given CecKeypress for a datagrid
365 /// </summary>
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)
370 {
371 if (dgView.InvokeRequired)
372 {
373 SelectKeypressRowCallback d = SelectKeypressRow;
374 try
375 {
376 container.Invoke(d, new object[] { container, dgView, key });
377 }
378 catch (Exception) { }
379 }
380 else
381 {
382 var rowIndex = -1;
383 foreach (DataGridViewRow row in dgView.Rows)
384 {
385 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
386 if (item != null && item.Key.Keycode == key.Keycode)
387 {
388 rowIndex = row.Index;
389 row.Selected = true;
390 item.Enabled = true;
391 }
392 else
393 {
394 row.Selected = false;
395 }
396 }
397 if (rowIndex > -1)
398 dgView.FirstDisplayedScrollingRowIndex = rowIndex;
399 }
400 }
401 private delegate void SelectKeypressRowCallback(Control container, DataGridView dgView, CecKeypress key);
402 }
403
404 /// <summary>
405 /// Form that implements IAsyncControls
406 /// </summary>
407 class AsyncForm : Form, IAsyncControls
408 {
409 /// <summary>
410 /// Changes the ShowInTaskbar value
411 /// </summary>
412 /// <param name="val">True to show, false to hide</param>
413 public void SetShowInTaskbar(bool val)
414 {
415 if (InvokeRequired)
416 {
417 SetShowInTaskbarCallback d = SetShowInTaskbar;
418 try
419 {
420 Invoke(d, new object[] { val });
421 }
422 catch (Exception) { }
423 }
424 else
425 {
426 ShowInTaskbar = val;
427 }
428 }
429 private delegate void SetShowInTaskbarCallback(bool val);
430
431 /// <summary>
432 /// Enable or disable a control
433 /// </summary>
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)
437 {
438 AsyncControls.SetControlEnabled(this, control, val);
439 }
440
441 /// <summary>
442 /// Change the text label of a control
443 /// </summary>
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)
447 {
448 AsyncControls.SetControlText(this, control, val);
449 }
450
451 /// <summary>
452 /// Changes the toolstrip menu text
453 /// </summary>
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)
457 {
458 AsyncControls.SetToolStripMenuText(this, item, val);
459 }
460
461 /// <summary>
462 /// Change the checked status of a checkbox
463 /// </summary>
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)
467 {
468 AsyncControls.SetCheckboxChecked(this, control, val);
469 }
470
471 /// <summary>
472 /// Change the checked status of an item in a CheckedListBox
473 /// </summary>
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)
478 {
479 AsyncControls.SetCheckboxItemChecked(this, control, index, val);
480 }
481
482 /// <summary>
483 /// Changes the progress value of a progress bar
484 /// </summary>
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)
488 {
489 AsyncControls.SetProgressValue(this, control, val);
490 }
491
492 /// <summary>
493 /// Replaces the items of a combobox
494 /// </summary>
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)
499 {
500 AsyncControls.SetComboBoxItems(this, control, selectedIndex, val);
501 }
502
503 /// <summary>
504 /// Make a control visible or invisible
505 /// </summary>
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)
509 {
510 AsyncControls.SetControlVisible(this, control, val);
511 }
512
513 /// <summary>
514 /// Display a new dialog
515 /// </summary>
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)
519 {
520 AsyncControls.DisplayDialog(this, control, modal);
521 }
522
523 /// <summary>
524 /// Hides a control
525 /// </summary>
526 /// <param name="val">True to hide, false to show</param>
527 public void SafeHide(bool val)
528 {
529 AsyncControls.SafeHide(this, val);
530 }
531
532 /// <summary>
533 /// Change the selected index
534 /// </summary>
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)
538 {
539 AsyncControls.SetSelectedIndex(this, control, index);
540 }
541
542 /// <summary>
543 /// Get the name of the selected tab in a TabControl
544 /// </summary>
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)
549 {
550 return AsyncControls.GetSelectedTabName(container, tabPages);
551 }
552
553 /// <summary>
554 /// Selects the row with the given CecKeypress for a datagrid
555 /// </summary>
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)
560 {
561 AsyncControls.SelectKeypressRow(container, dgView, key);
562 }
563 }
564
565 /// <summary>
566 /// TabPage that implements IAsyncControls
567 /// </summary>
568 class AsyncTabPage : TabPage, IAsyncControls
569 {
570 /// <summary>
571 /// Enable or disable a control
572 /// </summary>
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)
576 {
577 AsyncControls.SetControlEnabled(this, control, val);
578 }
579
580 /// <summary>
581 /// Change the text label of a control
582 /// </summary>
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)
586 {
587 AsyncControls.SetControlText(this, control, val);
588 }
589
590 /// <summary>
591 /// Changes the toolstrip menu text
592 /// </summary>
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)
596 {
597 AsyncControls.SetToolStripMenuText(this, item, val);
598 }
599
600 /// <summary>
601 /// Change the checked status of a checkbox
602 /// </summary>
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)
606 {
607 AsyncControls.SetCheckboxChecked(this, control, val);
608 }
609
610 /// <summary>
611 /// Change the checked status of an item in a CheckedListBox
612 /// </summary>
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)
617 {
618 AsyncControls.SetCheckboxItemChecked(this, control, index, val);
619 }
620
621 /// <summary>
622 /// Changes the progress value of a progress bar
623 /// </summary>
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)
627 {
628 AsyncControls.SetProgressValue(this, control, val);
629 }
630
631 /// <summary>
632 /// Replaces the items of a combobox
633 /// </summary>
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)
638 {
639 AsyncControls.SetComboBoxItems(this, control, selectedIndex, val);
640 }
641
642 /// <summary>
643 /// Make a control visible or invisible
644 /// </summary>
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)
648 {
649 AsyncControls.SetControlVisible(this, control, val);
650 }
651
652 /// <summary>
653 /// Display a new dialog
654 /// </summary>
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)
658 {
659 AsyncControls.DisplayDialog(this, control, modal);
660 }
661
662 /// <summary>
663 /// Hides a control
664 /// </summary>
665 /// <param name="val">True to hide, false to show</param>
666 public void SafeHide(bool val)
667 {
668 AsyncControls.SafeHide(this, val);
669 }
670
671 /// <summary>
672 /// Change the selected index
673 /// </summary>
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)
677 {
678 AsyncControls.SetSelectedIndex(this, control, index);
679 }
680
681 /// <summary>
682 /// Get the name of the selected tab in a TabControl
683 /// </summary>
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)
688 {
689 return AsyncControls.GetSelectedTabName(container, tabPages);
690 }
691
692 /// <summary>
693 /// Selects the row with the given CecKeypress for a datagrid
694 /// </summary>
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)
699 {
700 AsyncControls.SelectKeypressRow(container, dgView, key);
701 }
702 }
703 }