2 using System.Collections.Generic;
4 using System.Windows.Forms;
6 using LibCECTray.controller.applications;
8 namespace LibCECTray.ui
11 /// Utility methods to change GUI content from another thread
16 /// Enable or disable a control
18 /// <param name="container">The control that contains the control to change</param>
19 /// <param name="control">The control to change</param>
20 /// <param name="val">True to enable, false to disable</param>
21 public static void SetControlEnabled(Control container, Control control, bool val)
23 if (container == null || control == null) return;
24 if (container.InvokeRequired)
26 SetControlEnabledCallback d = SetControlEnabled;
29 container.Invoke(d, new object[] { container, control, val });
35 control.Enabled = val;
38 private delegate void SetControlEnabledCallback(Control container, Control control, bool val);
41 /// Change the text label of a control
43 /// <param name="container">The control that contains the control to change</param>
44 /// <param name="control">The control to change</param>
45 /// <param name="val">The new text</param>
46 public static void SetControlText(Control container, Control control, string val)
48 if (container == null || control == null) return;
49 if (container.InvokeRequired)
51 SetControlTextCallback d = SetControlText;
54 container.Invoke(d, new object[] { container, control, val });
63 private delegate void SetControlTextCallback(Control container, Control control, string val);
66 /// Change the checked status of a checkbox
68 /// <param name="container">The control that contains the control to change</param>
69 /// <param name="control">The control to change</param>
70 /// <param name="val">True to change to checked, false to change to unchecked</param>
71 public static void SetCheckboxChecked(Control container, CheckBox control, bool val)
73 if (container.InvokeRequired)
75 SetCheckboxCheckedCallback d = SetCheckboxChecked;
78 container.Invoke(d, new object[] { container, control, val });
84 control.Checked = val;
87 private delegate void SetCheckboxCheckedCallback(Control container, CheckBox control, bool val);
90 /// Change the checked status of an item in a CheckedListBox
92 /// <param name="container">The control that contains the control to change</param>
93 /// <param name="control">The control to change</param>
94 /// <param name="index">The index of the checkbox in the list to change</param>
95 /// <param name="val">True to change to checked, false to change to unchecked</param>
96 public static void SetCheckboxItemChecked(Control container, CheckedListBox control, int index, bool val)
98 if (container.InvokeRequired)
100 SetCheckboxItemCheckedCallback d = SetCheckboxItemChecked;
103 container.Invoke(d, new object[] { container, control, index, val });
105 catch (Exception) { }
109 control.SetItemChecked(index, val);
112 private delegate void SetCheckboxItemCheckedCallback(Control container, CheckedListBox control, int index, bool val);
115 /// Changes the toolstrip menu text
117 /// <param name="container">The control that contains the control to change</param>
118 /// <param name="item">The toolstrip menu item to change</param>
119 /// <param name="val">The new value</param>
120 public static void SetToolStripMenuText(Control container, ToolStripMenuItem item, string val)
122 if (container.InvokeRequired)
124 SetToolStripMenuTextCallback d = SetToolStripMenuText;
127 container.Invoke(d, new object[] { container, item, val });
129 catch (Exception) { }
136 private delegate void SetToolStripMenuTextCallback(Control container, ToolStripMenuItem item, string val);
139 /// Changes the progress value of a progress bar
141 /// <param name="container">The control that contains the control to change</param>
142 /// <param name="control">The control to change</param>
143 /// <param name="val">The new percentage</param>
144 public static void SetProgressValue(Control container, ProgressBar control, int val)
146 if (container.InvokeRequired)
148 SetProgressValueCallback d = SetProgressValue;
151 container.Invoke(d, new object[] { container, control, val });
153 catch (Exception) { }
160 private delegate void SetProgressValueCallback(Control container, ProgressBar control, int val);
163 /// Replaces the items of a combobox
165 /// <param name="container">The control that contains the control to change</param>
166 /// <param name="control">The control to change</param>
167 /// <param name="selectedIndex">The new selection index</param>
168 /// <param name="val">The new content</param>
169 public static void SetComboBoxItems(Control container, ComboBox control, int selectedIndex, object[] val)
171 if (container.InvokeRequired)
173 SetComboBoxItemsCallback d = SetComboBoxItems;
176 container.Invoke(d, new object[] { container, control, selectedIndex, val });
178 catch (Exception) { }
182 control.Items.Clear();
183 control.Items.AddRange(val);
184 if (control.Items.Count > 0)
185 control.SelectedIndex = selectedIndex;
188 private delegate void SetComboBoxItemsCallback(Control container, ComboBox control, int selectedIndex, object[] val);
191 /// Make a control visible or invisible
193 /// <param name="container">The control that contains the control to change</param>
194 /// <param name="control">The control to change</param>
195 /// <param name="val">True to make it visible, false to make it invisible</param>
196 public static void SetControlVisible(Control container, Control control, bool val)
198 if (container.InvokeRequired)
200 SetControlVisibleCallback d = SetControlVisible;
203 container.Invoke(d, new object[] { container, control, val });
205 catch (Exception) { }
209 control.Visible = val;
212 private delegate void SetControlVisibleCallback(Control container, Control control, bool val);
215 /// Display a new dialog
217 /// <param name="container">The control that contains the control to change</param>
218 /// <param name="control">The control to display</param>
219 /// <param name="modal">True to make it a modal dialog</param>
220 public static void DisplayDialog(Control container, Form control, bool modal)
222 if (container.InvokeRequired)
224 DisplayDialogCallback d = DisplayDialog;
227 container.Invoke(d, new object[] { container, control, modal });
229 catch (Exception) { }
234 control.ShowDialog(container);
236 control.Show(container);
239 private delegate void DisplayDialogCallback(Control container, Form control, bool modal);
244 /// <param name="container">The control to hide</param>
245 /// <param name="val">True to hide, false to show</param>
246 public static void SafeHide(Control container, bool val)
248 if (container.InvokeRequired)
250 SafeHideCallback d = SafeHide;
253 container.Invoke(d, new object[] { container, val });
255 catch (Exception) { }
265 private delegate void SafeHideCallback(Control container, bool val);
268 /// Change the selected index
270 /// <param name="container">The control that contains the control to change</param>
271 /// <param name="control">The control to change</param>
272 /// <param name="index">The new selected index</param>
273 public static void SetSelectedIndex(Control container, ComboBox control, int index)
275 if (container.InvokeRequired)
277 SetSelectedIndexCallback d = SetSelectedIndex;
280 container.Invoke(d, new object[] { container, control, index });
282 catch (Exception) { }
286 control.SelectedIndex = index;
289 private delegate void SetSelectedIndexCallback(Control container, ComboBox control, int index);
292 /// Get the name of the selected tab in a TabControl
294 /// <param name="container">The tab container</param>
295 /// <param name="tabPages">The tab pages</param>
296 /// <returns>The name of the selected tab</returns>
297 public static string GetSelectedTabName(TabControl container, TabControl.TabPageCollection tabPages)
299 if (container.InvokeRequired)
301 GetSelectedTabNameCallback d = GetSelectedTabName;
304 return container.Invoke(d, new object[] { container, tabPages }) as string;
306 catch (Exception) { }
310 return tabPages[container.SelectedIndex].Name;
314 private delegate string GetSelectedTabNameCallback(TabControl container, TabControl.TabPageCollection tabPages);
317 /// Selects the row with the given CecKeypress for a datagrid
319 /// <param name="container">The datagrid container</param>
320 /// <param name="dgView">The datagrid</param>
321 /// <param name="key">The key to selected</param>
322 public static void SelectKeypressRow(Control container, DataGridView dgView, CecKeypress key)
324 if (dgView.InvokeRequired)
326 SelectKeypressRowCallback d = SelectKeypressRow;
329 container.Invoke(d, new object[] { container, dgView, key });
331 catch (Exception) { }
336 foreach (DataGridViewRow row in dgView.Rows)
338 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
339 if (item != null && item.Key.Keycode == key.Keycode)
341 rowIndex = row.Index;
347 row.Selected = false;
351 dgView.FirstDisplayedScrollingRowIndex = rowIndex;
354 private delegate void SelectKeypressRowCallback(Control container, DataGridView dgView, CecKeypress key);