SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
ViTabControl.xaml.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 
15 namespace SeciControls
16 {
20  public partial class ViTabControl : UserControl
21  {
22  private Dictionary<String, ViHostWrapper> _hostLookup = new Dictionary<String, ViHostWrapper>();
23  private String _prevTab = "";
24 
25  private String _dummyTabName = "";
26 
27  public ViTabControl()
28  {
29  InitializeComponent();
30  }
31 
32  #region Tabs
33 
34  public Boolean AddDummyTab(String tabname)
35  {
36  if (_hostLookup.ContainsKey(tabname))
37  {
38  HideTab(tabname);
39  return false;
40  }
41 
42  ViHostWrapper newHost = new ViHostWrapper();
43  TabItem newTab = new TabItem();
44  newTab.Header = tabname;
45  newTab.Content = newHost;
46 
47  ScrollingTabControl.Items.Add(newTab);
48 
49  _hostLookup.Add(tabname, newHost);
50  _dummyTabName = tabname;
51  HideTab(tabname);
52 
53  return true;
54  }
55 
56  public Boolean AddTab(String tabname)
57  {
58  if (_hostLookup.ContainsKey(tabname))
59  {
60  return false;
61  }
62 
63  ViHostWrapper newHost = new ViHostWrapper();
64  TabItem newTab = new TabItem();
65  newTab.FontSize = 13;
66  newTab.Header = tabname;
67  newTab.Content = newHost;
68 
69  if (_hostLookup.ContainsKey(_dummyTabName))
70  {
71  int index = GetTabIndex(_dummyTabName);
72 
73  if (index != -1)
74  {
75  ScrollingTabControl.Items.Insert(index, newTab);
76  }
77  else
78  {
79  throw new Exception("Cannot get index of ScrollingTabControl");
80  }
81  }
82  else
83  {
84  ScrollingTabControl.Items.Add(newTab);
85  }
86 
87  _hostLookup.Add(tabname, newHost);
88 
89  return true;
90  }
91 
92  public void RemoveTab(String tabname)
93  {
94  TabItem temp = GetTab(tabname);
95 
96  if (temp != null)
97  {
98  _hostLookup.Remove(tabname);
99  ScrollingTabControl.Items.Remove(temp);
100  }
101  }
102 
103  public void RenameTab(String current, String newname)
104  {
105  if (_hostLookup.ContainsKey(newname))
106  {
107  throw new Exception("Name already used");
108  }
109 
110  TabItem temp = GetTab(current);
111  temp.Header = newname;
112 
113  ViHostWrapper tokeep = _hostLookup[current];
114  _hostLookup.Remove(current);
115  _hostLookup.Add(newname, tokeep);
116  }
117 
118  public Boolean TabNameValid(string tabname)
119  {
120  return _hostLookup.ContainsKey(tabname);
121  }
122 
123  private TabItem GetTab(String tabname)
124  {
125  foreach (Object tab in ScrollingTabControl.Items)
126  {
127  TabItem temp = (TabItem)tab;
128  if (temp.Header.ToString() == tabname)
129  {
130  return temp;
131  }
132  }
133  return null;
134  }
135 
136  private int GetTabIndex(String tabname)
137  {
138  for (int i = 0; i < ScrollingTabControl.Items.Count; ++i)
139  {
140  TabItem temp = (TabItem)ScrollingTabControl.Items[i];
141  if (temp.Header.ToString() == tabname)
142  {
143  return i;
144  }
145  }
146  return -1;
147  }
148 
149  public void SwapTabs(String tab1, String tab2)
150  {
151  int item1 = GetTabIndex(tab1);
152  int item2 = GetTabIndex(tab2);
153 
154  if (item1 != -1 && item2 != -1)
155  {
156  TabItem temp = (TabItem)ScrollingTabControl.Items[item1];
157  ScrollingTabControl.Items.RemoveAt(item1);
158  ScrollingTabControl.Items.Insert(item2, temp);
159  }
160  }
161 
162  public List<String> GetTabNames(Boolean includeEmpty)
163  {
164  List<String> names = new List<string>();
165 
166  for (int i = 0; i < ScrollingTabControl.Items.Count; ++i)
167  {
168  TabItem temp = (TabItem)ScrollingTabControl.Items[i];
169 
170  if (includeEmpty || _hostLookup[temp.Header.ToString()].GetViNames().Count > 0)
171  {
172  names.Add(temp.Header.ToString());
173  }
174  }
175 
176  return names;
177  }
178 
179  public List<String> GetTabVis(String tabname)
180  {
181  return _hostLookup[tabname].GetViNames();
182  }
183 
184  public Boolean DoesTabContainsVi(String tabname, String viname)
185  {
186  foreach (String vi in GetTabVis(tabname))
187  {
188  if (vi.ToLower() == viname.ToLower())
189  {
190  return true;
191  }
192  }
193 
194  return false;
195  }
196 
197  public void RemoveAllTabs()
198  {
199  List<String> keys = _hostLookup.Keys.ToList<String>();
200 
201  for (int i = keys.Count - 1; i >= 0; --i)
202  {
203  _hostLookup[keys[i]].RemoveAllVIs();
204  RemoveTab(keys[i]);
205  }
206  }
207 
208  public void CascadeAllTabs()
209  {
210  List<String> keys = _hostLookup.Keys.ToList<String>();
211 
212  foreach (String key in keys)
213  {
214  _hostLookup[key].CascadeVIs();
215  }
216  }
217 
218  public void HideTab(string tabname)
219  {
220  if (ScrollingTabControl.Items != null && !string.IsNullOrEmpty(tabname))
221  {
222  foreach (TabItem tab in ScrollingTabControl.Items)
223  {
224  if (tab.Header.ToString() == tabname)
225  {
226  tab.Visibility = Visibility.Collapsed;
227  return;
228  }
229  }
230  }
231  }
232 
233  public void UnhideTab(string tabname)
234  {
235  if (ScrollingTabControl.Items != null && !string.IsNullOrEmpty(tabname))
236  {
237  foreach (TabItem tab in ScrollingTabControl.Items)
238  {
239  if (tab.Header.ToString() == tabname)
240  {
241  tab.Visibility = Visibility.Visible;
242  return;
243  }
244  }
245  }
246  }
247 
248  public void SelectTab(string tabname)
249  {
250  if (ScrollingTabControl.Items != null && !string.IsNullOrEmpty(tabname))
251  {
252  foreach (TabItem tab in ScrollingTabControl.Items)
253  {
254  if (tab.Header.ToString() == tabname)
255  {
256  ScrollingTabControl.SelectedItem = tab;
257  return;
258  }
259  }
260  }
261  }
262 
263  public void SelectTab(int index)
264  {
265  ScrollingTabControl.SelectedIndex = index;
266  }
267 
268  public void SetToFirstVisible()
269  {
270  if (ScrollingTabControl.Items != null)
271  {
272  for (int i = 0; i < ScrollingTabControl.Items.Count; ++i)
273  {
274  TabItem tab = ScrollingTabControl.Items[i] as TabItem;
275 
276  if (tab != null && tab.Visibility == Visibility.Visible)
277  {
278  SelectTab(i);
279  return;
280  }
281  }
282  }
283  }
284 
285  Boolean _settingOrder;
286 
287  public void SetTabOrder(Seci.SerialisableList<Seci.Definitions.Tab> list)
288  {
289  _settingOrder = true;
290 
291  try
292  {
293  for (int i = 0; i < list.Count; ++i)
294  {
295  int index = GetTabIndex(list[i].Name);
296  if (index != -1)
297  {
298  TabItem temp = (TabItem)ScrollingTabControl.Items[index];
299  ScrollingTabControl.Items.RemoveAt(index);
300  ScrollingTabControl.Items.Insert(i, temp);
301  }
302  }
303  }
304  catch
305  {
306  }
307 
308  _settingOrder = false;
309  }
310 
311  private void ScrollingTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
312  {
313  if (e.Source is TabControl && !_settingOrder)
314  {
315  TabControl cntrl = sender as TabControl;
316  String tabname = "";
317 
318  if (cntrl.SelectedIndex != -1)
319  {
320  tabname = cntrl.Items[cntrl.SelectedIndex].ToString();
321  tabname = tabname.Substring(0, tabname.IndexOf(" Content")).Substring(tabname.IndexOf("Header:") + 7);
322  _hostLookup[tabname].SetVisible(true);
323  }
324 
325  if (_prevTab != "" && tabname != "")
326  {
327  _hostLookup[_prevTab].SetVisible(false);
328  }
329  _prevTab = tabname;
330  }
331  }
332 
333  public void StartTimers(int interval)
334  {
335  List<String> keys = _hostLookup.Keys.ToList<String>();
336 
337  foreach (String k in keys)
338  {
339  _hostLookup[k].StartTimer(interval);
340  }
341  }
342 
343  #endregion
344 
345  #region VIs
346 
347  public void AddVi(String tabname, String filename)
348  {
349  _hostLookup[tabname].AddVI(filename);
350  }
351 
352  public void AddVi(String tabname, String filename, int x, int y)
353  {
354  _hostLookup[tabname].AddVI(filename, x, y);
355  }
356 
357  public void RemoveVi(String tabname, String filename)
358  {
359  _hostLookup[tabname].RemoveVI(filename);
360  }
361 
362  public void RemoveVi(String filename)
363  {
364  foreach (String tabname in _hostLookup.Keys.ToList<String>())
365  {
366  if (_hostLookup[tabname].GetViNames().Contains(filename))
367  {
368  _hostLookup[tabname].RemoveVI(filename);
369  }
370  }
371  }
372 
373  public List<String> GetAllVisInTabs()
374  {
375  List<String> names = new List<string>();
376  List<String> keys = _hostLookup.Keys.ToList<String>();
377 
378  foreach (String key in keys)
379  {
380  names.AddRange(_hostLookup[key].GetViNames());
381  }
382 
383  return names;
384  }
385 
386  #endregion
387 
388  }
389 }
Boolean AddDummyTab(String tabname)
Dictionary< String, ViHostWrapper > _hostLookup
void HideTab(string tabname)
void SwapTabs(String tab1, String tab2)
void StartTimers(int interval)
void RemoveVi(String filename)
void ScrollingTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
int GetTabIndex(String tabname)
Interaction logic for ViHostWrapper.xaml
void AddVi(String tabname, String filename)
void SetTabOrder(Seci.SerialisableList< Seci.Definitions.Tab > list)
Boolean DoesTabContainsVi(String tabname, String viname)
List< String > GetTabVis(String tabname)
List< String > GetTabNames(Boolean includeEmpty)
Boolean AddTab(String tabname)
void RenameTab(String current, String newname)
Boolean TabNameValid(string tabname)
TabItem GetTab(String tabname)
void UnhideTab(string tabname)
void RemoveVi(String tabname, String filename)
void SelectTab(string tabname)
Interaction logic for ViTabControl.xaml
void RemoveTab(String tabname)
void AddVi(String tabname, String filename, int x, int y)
List< String > GetAllVisInTabs()