using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using TradeIdeas.TIProData; namespace TradeIdeas.TIProGUI { public partial class SymbolLinkingChannelsForm : Form { private Form _parentForm; private List
_formsList; private string _lastLinkChannelFilter; public SymbolLinkingChannelsForm(Form parentForm) { _parentForm = parentForm; InitializeComponent(); // set the font. SelectTheFont(); // pause surfing while this window is open PauseSurfing(); // load link channels into combobox foreach (KeyValuePair channel in SymbolLinkChannels.Channels) { cboLinkChannels.Items.Add(channel.Key); } if (cboLinkChannels.Items.Count > 0) cboLinkChannels.SelectedIndex = 0; // display the linked windows DisplayLinkedWindows(); } private void DisplayLinkedWindows(string linkChannelFilter = null) { if (_parentForm is MainDockForm) { MainDockForm mainDockForm = _parentForm as MainDockForm; _formsList = mainDockForm.ChildForms; } else { _formsList = Application.OpenForms.Cast().Where(x => !x.IsDisposed).ToList(); } lstLinkedWindows.Items.Clear(); // find all symbol linked windows and display them foreach (Form form in _formsList) { if (!form.IsDisposed) { ISymbolLinkingChannel symbolLinkingChannel = form as ISymbolLinkingChannel; if (symbolLinkingChannel != null) { // check to see if window accepts symbol linking // if it does and linking is disabled, skip it IAcceptSymbolLinking acceptSymbolLinking = form as IAcceptSymbolLinking; if (acceptSymbolLinking != null && !acceptSymbolLinking.LinkEnabled) continue; // get the link channel for the form string linkChannel = symbolLinkingChannel.GetLinkChannel(); if (linkChannel == "1") linkChannel = "Default"; // check to see if we are filtering the window list // if so, skip any window that doesn't match the filter if (!string.IsNullOrEmpty(linkChannelFilter) && linkChannel != linkChannelFilter) continue; // create item with form name ListViewItem listViewItem = new ListViewItem(form.Text); // store form to item tag for modifying listViewItem.Tag = form; // set image to form icon ISaveLayout saveable = form as ISaveLayout; if (saveable != null) listViewItem.ImageIndex = GetIconId(saveable.WindowIconCache); // create subitem for link channel ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem(); listViewSubItem.Text = linkChannel; // find the color for the link channel if (linkChannel != "Default") { SymbolLinkChannels.Channels.TryGetValue(linkChannel, out Color backColor); listViewSubItem.BackColor = backColor; Color foreColor = GradientInfo.AltColor(backColor); listViewSubItem.ForeColor = foreColor; listViewItem.UseItemStyleForSubItems = false; } listViewItem.SubItems.Add(listViewSubItem); // add the item to the list lstLinkedWindows.Items.Add(listViewItem); } } } if (lstLinkedWindows.Items.Count > 0) { // Resize first column. bool verticalScrollBarVisible = (lstLinkedWindows.GetItemRect(0).Height * (lstLinkedWindows.Items.Count + 1) > lstLinkedWindows.ClientRectangle.Height); lstLinkedWindows.Columns[0].Width = lstLinkedWindows.Width - lstLinkedWindows.Columns[1].Width - (verticalScrollBarVisible ? SystemInformation.VerticalScrollBarWidth : 0) - 4; } _lastLinkChannelFilter = linkChannelFilter; } private int GetIconId(WindowIconCache source) { Icon icon = source.Icon; try { // This is required to get a high quality version of the icon. If you // add the icon directly to the image list, the icon will be resized // automatically, but it won't look as good. // Need to convert icons to bitmaps before adding them to the image list. // Doing so will avoid a dark border around the image. using (Icon smallIcon = new Icon(icon, smallImageList.ImageSize)) { smallImageList.Images.Add(smallIcon.ToBitmap()); } using (Icon largeIcon = new Icon(icon, largeImageList.ImageSize)) { largeImageList.Images.Add(largeIcon.ToBitmap()); } } catch { // Is it possible that the image was added to one list but not the other? while (smallImageList.Images.Count > largeImageList.Images.Count) smallImageList.Images.RemoveAt(largeImageList.Images.Count); return -1; } return smallImageList.Images.Count - 1; } private void btnClose_Click(object sender, EventArgs e) { // unpause surfing UnPauseSurfing(); this.Close(); } private void cboLinkChannels_DrawItem(object sender, DrawItemEventArgs e) { // Get the item text string text = ((ComboBox)sender).Items[e.Index].ToString(); // find the color for the link channel SymbolLinkChannels.Channels.TryGetValue(text, out Color backColor); Color foreColor = GradientInfo.AltColor(backColor); // set background color e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds); // draw the text if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { Font font = new Font(e.Font, FontStyle.Bold); e.Graphics.DrawString(text, font, new SolidBrush(foreColor), new Point(e.Bounds.X, e.Bounds.Y)); } else e.Graphics.DrawString(text, e.Font, new SolidBrush(foreColor), new Point(e.Bounds.X, e.Bounds.Y)); } private void btnSetAllSelected_Click(object sender, EventArgs e) { if (cboLinkChannels.SelectedItem != null) { string linkChannel = cboLinkChannels.SelectedItem.ToString(); // loop thru selected windows in list view foreach (ListViewItem listViewItem in lstLinkedWindows.Items) { // check to see if the item is selected if (listViewItem.Checked) { // get the form and set the link channel ISymbolLinkingChannel symbolLinkingChannel = (Form)listViewItem.Tag as ISymbolLinkingChannel; if (symbolLinkingChannel != null) { symbolLinkingChannel.SetLinkChannel(linkChannel); } } } // if filtered, refresh window list with new channel as filter if (!string.IsNullOrEmpty(_lastLinkChannelFilter)) DisplayLinkedWindows(linkChannel); else DisplayLinkedWindows(); } } private void btnFilterBy_Click(object sender, EventArgs e) { if (cboLinkChannels.SelectedItem != null) DisplayLinkedWindows(cboLinkChannels.SelectedItem.ToString()); } private void btnClearFilter_Click(object sender, EventArgs e) { DisplayLinkedWindows(); } private void btnSelectAll_Click(object sender, EventArgs e) { // select all windows foreach (ListViewItem listViewItem in lstLinkedWindows.Items) { listViewItem.Checked = true; } } private void btnClearSelected_Click(object sender, EventArgs e) { // unselect all windows foreach (ListViewItem listViewItem in lstLinkedWindows.Items) { listViewItem.Checked = false; } } private void PauseSurfing() { // temporarily pause surfing Surfer.SurfManager surfManager = Surfer.SurfManager.Instance(); if (null != surfManager) { if (surfManager.IsRunning) surfManager.Stop(true); } } private void UnPauseSurfing() { // unpause surfing Surfer.SurfManager surfManager = Surfer.SurfManager.Instance(); if (null != surfManager) { if (surfManager.Paused) surfManager.Start(false); } } private void SelectTheFont() { Font = GuiEnvironment.FontSettings; foreach (Control control in Controls) { control.Font = GuiEnvironment.FontSettings; } } private void SymbolLinkingChannelsForm_ResizeEnd(object sender, EventArgs e) { try { if (lstLinkedWindows.Items.Count > 0) { // Resize first column. bool verticalScrollBarVisible = (lstLinkedWindows.GetItemRect(0).Height * (lstLinkedWindows.Items.Count + 1) > lstLinkedWindows.ClientRectangle.Height); lstLinkedWindows.Columns[0].Width = lstLinkedWindows.Width - lstLinkedWindows.Columns[1].Width - (verticalScrollBarVisible ? SystemInformation.VerticalScrollBarWidth : 0) - 4; } } catch { } } } public static class SymbolLinkingChannelsHelper { public static void SetFormIcon(Form form, string iconPrefix, string linkChannel) { if (linkChannel == SymbolLinkChannels.DefaultLinkChannel) linkChannel = "BLUE"; else if (linkChannel == SymbolLinkChannels.DockWindowChannel) linkChannel = "PINK"; string iconPath = $"Resources/{iconPrefix.ToUpper()}_{linkChannel.ToUpper()}.ico"; if (File.Exists(iconPath)) { form.Icon = new Icon(iconPath); if (form.ParentForm is WindowDocumentDock) { ((WindowDocumentDock)form.ParentForm).Icon = new Icon(iconPath); ((WindowDocumentDock)form.ParentForm).DockHandler.Pane.Refresh(); if (((WindowDocumentDock)form.ParentForm).ParentForm is MainDockForm) { ((MainDockForm)(((WindowDocumentDock)form.ParentForm).ParentForm)).SetLinkChannelIcon(); } } } } } }