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.Diagnostics;
using TradeIdeas.XML;
using TradeIdeas.MiscSupport;
namespace TradeIdeas.TIProGUI.RealTimeStockRace
{
public partial class RaceCarControl : UserControl
{
private bool _useCompanyLogo = true;
private string _symbol;
private double _price;
private DateTime? _indicatorTime;
private int _laneNumber;
private Font _origDayChangeFont;
//private string _companyLogosWebResource = "https://static.trade-ideas.com/CompanyLogos/";
public RaceCarControl(string symbol, int laneNumber, Color symbolTextColor, int symbolWidth, bool useCompanyLogo)
{
InitializeComponent();
_origDayChangeFont = lblDayChange.Font;
_symbol = symbol;
_laneNumber = laneNumber;
_useCompanyLogo = useCompanyLogo;
// If using logo, make image invisible until loaded.
if (_useCompanyLogo)
picRaceCar.Visible = false;
lblSymbol.Text = symbol;
lblSymbol.ForeColor = symbolTextColor;
SetRaceCarImage(laneNumber);
SetSymbolWidth(symbolWidth);
}
public Color SymbolTextColor
{
get { return lblSymbol.ForeColor; }
set { lblSymbol.ForeColor = value; }
}
public int SymbolWidth
{
get { return lblSymbol.Width; }
set
{
SetSymbolWidth(value);
}
}
public Color LabelBackColor
{
get { return lblSymbol.BackColor; }
set
{
lblSymbol.BackColor = value;
lblPrice.BackColor = value;
lblDayChange.BackColor = value;
}
}
///
/// Resize Symbol and control width.
///
///
private void SetSymbolWidth(int symbolWidth)
{
lblSymbol.Width = symbolWidth;
lblPrice.Left = symbolWidth;
//lblPrice.Width = symbolWidth;
lblDayChange.Width = symbolWidth + lblPrice.Width;
picRaceCar.Left = lblPrice.Right; //lblSymbol.Right;
this.Width = lblSymbol.Width + lblPrice.Width + picRaceCar.Width;
}
///
/// Price value for car.
///
public double Price
{
get { return _price; }
set
{
// Width test code
//if (_laneNumber % 2 == 0)
// value = "99,999.00";
_price = value;
lblPrice.Text = _price.ToString("N2");
SetPriceFont();
}
}
///
/// Indicator Time. This is the time associated with the price update.
/// Used to for display price indicator on charts.
///
public DateTime? IndicatorTime
{
get { return _indicatorTime; }
set
{
_indicatorTime = value;
Debug.WriteLine($"Race Car {_symbol} Indicator Time: {_indicatorTime}");
}
}
///
/// Day Change label for car.
///
public string DayChange
{
get { return lblDayChange.Text; }
set
{
lblDayChange.Text = value;
if (value.StartsWith("-"))
lblDayChange.ForeColor = Color.Red;
else if (value.StartsWith("0.00"))
lblDayChange.ForeColor = Color.White;
else
lblDayChange.ForeColor = Color.LimeGreen;
SetDayChangeFont();
}
}
///
/// Set Price font.
///
private void SetPriceFont()
{
using (Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(lblPrice.Text, lblPrice.Font);
int width = (int)Math.Ceiling(size.Width) + 3;
if (width > lblPrice.Width)
{
// Decrease font size.
lblPrice.Font = new Font(lblPrice.Font.FontFamily, lblPrice.Font.Size - 1, FontStyle.Regular);
size = g.MeasureString(lblPrice.Text, lblPrice.Font);
width = (int)Math.Ceiling(size.Width) + 3;
if (width > lblPrice.Width)
{
// Decrease font size again.
lblPrice.Font = new Font(lblPrice.Font.FontFamily, lblPrice.Font.Size - 1, FontStyle.Regular);
size = g.MeasureString(lblPrice.Text, lblPrice.Font);
width = (int)Math.Ceiling(size.Width) + 3;
}
}
}
}
///
/// Set Day Change font.
///
private void SetDayChangeFont()
{
using (Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(lblDayChange.Text, lblDayChange.Font);
int width = (int)Math.Ceiling(size.Width) + 3;
if (width > lblDayChange.Width)
{
// Decrease font size.
lblDayChange.Font = new Font(lblDayChange.Font.FontFamily, lblDayChange.Font.Size - 1, FontStyle.Regular);
size = g.MeasureString(lblDayChange.Text, lblDayChange.Font);
width = (int)Math.Ceiling(size.Width) + 3;
if (width > lblDayChange.Width)
{
// Decrease font size again.
lblDayChange.Font = new Font(lblDayChange.Font.FontFamily, lblDayChange.Font.Size - 1, FontStyle.Regular);
size = g.MeasureString(lblDayChange.Text, lblPrice.Font);
width = (int)Math.Ceiling(size.Width) + 3;
}
}
}
}
private void lblSymbol_TextChanged(object sender, EventArgs e)
{
//using (Graphics g = CreateGraphics())
//{
// SizeF size = g.MeasureString(lblSymbol.Text, lblSymbol.Font);
// lblSymbol.Width = (int)Math.Ceiling(size.Width) + 3;
//}
//picRaceCar.Left = lblSymbol.Right;
//this.Width = lblSymbol.Width + picRaceCar.Width;
}
///
/// Set the race car image.
///
///
private void SetRaceCarImage(int laneNumber)
{
if (_useCompanyLogo)
{
LoadCompanyLogo(_symbol);
}
else
{
// To reuse cars over ten lanes, if lane number is over 10 find the remainder when divided by 10
if (laneNumber > 10)
laneNumber = laneNumber % 10;
switch (laneNumber)
{
case 1:
picRaceCar.Image = TIProGUI.Properties.Resources.yellow_lamborghini;
break;
case 2:
picRaceCar.Image = TIProGUI.Properties.Resources.black_ferarri;
break;
case 3:
picRaceCar.Image = TIProGUI.Properties.Resources.green_mercedes;
break;
case 4:
picRaceCar.Image = TIProGUI.Properties.Resources.silver_mercedes;
break;
case 5:
case 0:
picRaceCar.Image = TIProGUI.Properties.Resources.red_ferarri;
break;
case 6:
picRaceCar.Image = TIProGUI.Properties.Resources.orange_mclaren;
break;
case 7:
picRaceCar.Image = TIProGUI.Properties.Resources.yellow_nissan;
break;
case 8:
picRaceCar.Image = TIProGUI.Properties.Resources.green_porsche;
break;
case 9:
picRaceCar.Image = TIProGUI.Properties.Resources.silver_porsche;
break;
case 10:
picRaceCar.Image = TIProGUI.Properties.Resources.green_lambo;
break;
}
}
}
private void SendToExternalLinking(string symbol)
{
RaceLaneControl raceLaneControl = Parent as RaceLaneControl;
if (raceLaneControl != null)
{
RealTimeStockRaceControl realTimeStockRaceControl = raceLaneControl.Parent as RealTimeStockRaceControl;
if (realTimeStockRaceControl != null)
{
Panel mainPanel = realTimeStockRaceControl.Parent as Panel;
if (mainPanel != null)
{
RealTimeStockRaceForm realTimeStockRaceForm = mainPanel.Parent as RealTimeStockRaceForm;
if (realTimeStockRaceForm != null)
{
realTimeStockRaceForm.SendToExternalLinking(symbol, AddPriceDataIndicatorFields());
}
}
}
}
}
///
/// Create RowData object with Price and Time for symbol linking.
///
///
private RowData AddPriceDataIndicatorFields()
{
RowData rowData = new RowData();
rowData.Data.Add("PRICE_INDICATOR", _price);
rowData.Data.Add("PRICE_INDICATOR_TIME", _indicatorTime);
return rowData;
}
private void lblSymbol_Click(object sender, EventArgs e)
{
SendToExternalLinking(_symbol);
}
private void picRaceCar_Click(object sender, EventArgs e)
{
SendToExternalLinking(_symbol);
}
///
/// Loads image of the company's logo
///
public void LoadCompanyLogo(string symbol)
{
//_companyLogosWebResource = GuiEnvironment.GlobalSettings.Node("SINGLE_STOCK_WINDOW").Node("SINGLE_STOCK_WINDOW_COMPANYLOGOS").Property("SOURCE", _companyLogosWebResource);
//var imageUrl = $"{_companyLogosWebResource}{symbol}.png";
picRaceCar.SizeMode = PictureBoxSizeMode.StretchImage;
picRaceCar.Width = picRaceCar.Height;
this.Width = lblSymbol.Width + picRaceCar.Width;
// Set the PictureBox Tag property to the Symbol so that CompanyLogoManager.symbolPictureBox_LoadCompleted() knows what it is.
picRaceCar.Tag = symbol;
picRaceCar.LoadCompleted -= CompanyLogoManager.symbolPictureBox_LoadCompleted;
picRaceCar.LoadCompleted += CompanyLogoManager.symbolPictureBox_LoadCompleted;
CompanyLogoManager.LoadCompanyLogo(picRaceCar, symbol);
}
private void lblPrice_Click(object sender, EventArgs e)
{
SendToExternalLinking(_symbol);
}
private void lblDayChange_Click(object sender, EventArgs e)
{
SendToExternalLinking(_symbol);
}
private void RaceCarControl_FontChanged(object sender, EventArgs e)
{
if (Font.Size > 8.25)
lblDayChange.Font = new Font(Font.FontFamily, (float)(Font.Size * 0.75), Font.Style);
else
lblDayChange.Font = _origDayChangeFont;
}
}
}