Imported Upstream version 2.2.0
[deb_libcec.git] / src / LibCecTray / ui / AsyncControls.cs
CommitLineData
cbbe90dd
JB
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Windows.Forms;
5using CecSharp;
6using LibCECTray.controller.applications;
7
8namespace LibCECTray.ui
9{
10 /// <summary>
11 /// Utility methods to change GUI content from another thread
12 /// </summary>
13 class AsyncControls
14 {
15 /// <summary>
16 /// Enable or disable a control
17 /// </summary>
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)
22 {
23 if (container == null || control == null) return;
24 if (container.InvokeRequired)
25 {
26 SetControlEnabledCallback d = SetControlEnabled;
27 try
28 {
29 container.Invoke(d, new object[] { container, control, val });
30 }
31 catch { }
32 }
33 else
34 {
35 control.Enabled = val;
36 }
37 }
38 private delegate void SetControlEnabledCallback(Control container, Control control, bool val);
39
40 /// <summary>
41 /// Change the text label of a control
42 /// </summary>
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)
47 {
48 if (container == null || control == null) return;
49 if (container.InvokeRequired)
50 {
51 SetControlTextCallback d = SetControlText;
52 try
53 {
54 container.Invoke(d, new object[] { container, control, val });
55 }
56 catch { }
57 }
58 else
59 {
60 control.Text = val;
61 }
62 }
63 private delegate void SetControlTextCallback(Control container, Control control, string val);
64
65 /// <summary>
66 /// Change the checked status of a checkbox
67 /// </summary>
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)
72 {
73 if (container.InvokeRequired)
74 {
75 SetCheckboxCheckedCallback d = SetCheckboxChecked;
76 try
77 {
78 container.Invoke(d, new object[] { container, control, val });
79 }
80 catch { }
81 }
82 else
83 {
84 control.Checked = val;
85 }
86 }
87 private delegate void SetCheckboxCheckedCallback(Control container, CheckBox control, bool val);
88
89 /// <summary>
90 /// Change the checked status of an item in a CheckedListBox
91 /// </summary>
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)
97 {
98 if (container.InvokeRequired)
99 {
100 SetCheckboxItemCheckedCallback d = SetCheckboxItemChecked;
101 try
102 {
103 container.Invoke(d, new object[] { container, control, index, val });
104 }
105 catch (Exception) { }
106 }
107 else
108 {
109 control.SetItemChecked(index, val);
110 }
111 }
112 private delegate void SetCheckboxItemCheckedCallback(Control container, CheckedListBox control, int index, bool val);
113
114 /// <summary>
115 /// Changes the toolstrip menu text
116 /// </summary>
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)
121 {
122 if (container.InvokeRequired)
123 {
124 SetToolStripMenuTextCallback d = SetToolStripMenuText;
125 try
126 {
127 container.Invoke(d, new object[] { container, item, val });
128 }
129 catch (Exception) { }
130 }
131 else
132 {
133 item.Text = val;
134 }
135 }
136 private delegate void SetToolStripMenuTextCallback(Control container, ToolStripMenuItem item, string val);
137
138 /// <summary>
139 /// Changes the progress value of a progress bar
140 /// </summary>
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)
145 {
146 if (container.InvokeRequired)
147 {
148 SetProgressValueCallback d = SetProgressValue;
149 try
150 {
151 container.Invoke(d, new object[] { container, control, val });
152 }
153 catch (Exception) { }
154 }
155 else
156 {
157 control.Value = val;
158 }
159 }
160 private delegate void SetProgressValueCallback(Control container, ProgressBar control, int val);
161
162 /// <summary>
163 /// Replaces the items of a combobox
164 /// </summary>
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)
170 {
171 if (container.InvokeRequired)
172 {
173 SetComboBoxItemsCallback d = SetComboBoxItems;
174 try
175 {
176 container.Invoke(d, new object[] { container, control, selectedIndex, val });
177 }
178 catch (Exception) { }
179 }
180 else
181 {
182 control.Items.Clear();
183 control.Items.AddRange(val);
184 if (control.Items.Count > 0)
185 control.SelectedIndex = selectedIndex;
186 }
187 }
188 private delegate void SetComboBoxItemsCallback(Control container, ComboBox control, int selectedIndex, object[] val);
189
190 /// <summary>
191 /// Make a control visible or invisible
192 /// </summary>
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)
197 {
198 if (container.InvokeRequired)
199 {
200 SetControlVisibleCallback d = SetControlVisible;
201 try
202 {
203 container.Invoke(d, new object[] { container, control, val });
204 }
205 catch (Exception) { }
206 }
207 else
208 {
209 control.Visible = val;
210 }
211 }
212 private delegate void SetControlVisibleCallback(Control container, Control control, bool val);
213
214 /// <summary>
215 /// Display a new dialog
216 /// </summary>
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)
221 {
222 if (container.InvokeRequired)
223 {
224 DisplayDialogCallback d = DisplayDialog;
225 try
226 {
227 container.Invoke(d, new object[] { container, control, modal });
228 }
229 catch (Exception) { }
230 }
231 else
232 {
233 if (modal)
234 control.ShowDialog(container);
235 else
236 control.Show(container);
237 }
238 }
239 private delegate void DisplayDialogCallback(Control container, Form control, bool modal);
240
241 /// <summary>
242 /// Hides a control
243 /// </summary>
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)
247 {
248 if (container.InvokeRequired)
249 {
250 SafeHideCallback d = SafeHide;
251 try
252 {
253 container.Invoke(d, new object[] { container, val });
254 }
255 catch (Exception) { }
256 }
257 else
258 {
259 if (val)
260 container.Hide();
261 else
262 container.Show();
263 }
264 }
265 private delegate void SafeHideCallback(Control container, bool val);
266
267 /// <summary>
268 /// Change the selected index
269 /// </summary>
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)
274 {
275 if (container.InvokeRequired)
276 {
277 SetSelectedIndexCallback d = SetSelectedIndex;
278 try
279 {
280 container.Invoke(d, new object[] { container, control, index });
281 }
282 catch (Exception) { }
283 }
284 else
285 {
286 control.SelectedIndex = index;
287 }
288 }
289 private delegate void SetSelectedIndexCallback(Control container, ComboBox control, int index);
290
291 /// <summary>
292 /// Get the name of the selected tab in a TabControl
293 /// </summary>
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)
298 {
299 if (container.InvokeRequired)
300 {
301 GetSelectedTabNameCallback d = GetSelectedTabName;
302 try
303 {
304 return container.Invoke(d, new object[] { container, tabPages }) as string;
305 }
306 catch (Exception) { }
307 }
308 else
309 {
310 return tabPages[container.SelectedIndex].Name;
311 }
312 return string.Empty;
313 }
314 private delegate string GetSelectedTabNameCallback(TabControl container, TabControl.TabPageCollection tabPages);
315
316 /// <summary>
317 /// Selects the row with the given CecKeypress for a datagrid
318 /// </summary>
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)
323 {
324 if (dgView.InvokeRequired)
325 {
326 SelectKeypressRowCallback d = SelectKeypressRow;
327 try
328 {
329 container.Invoke(d, new object[] { container, dgView, key });
330 }
331 catch (Exception) { }
332 }
333 else
334 {
335 var rowIndex = -1;
336 foreach (DataGridViewRow row in dgView.Rows)
337 {
338 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
339 if (item != null && item.Key.Keycode == key.Keycode)
340 {
341 rowIndex = row.Index;
342 row.Selected = true;
343 item.Enabled = true;
344 }
345 else
346 {
347 row.Selected = false;
348 }
349 }
350 if (rowIndex > -1)
351 dgView.FirstDisplayedScrollingRowIndex = rowIndex;
352 }
353 }
354 private delegate void SelectKeypressRowCallback(Control container, DataGridView dgView, CecKeypress key);
355 }
356
357}