context: using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Globalization;
  using System.Windows.Forms;
  using iTrading.Core.Data;
  using iTrading.Core.IndicatorBase;
  using Steema.TeeChart;
  using Steema.TeeChart.Drawing;
  using Steema.TeeChart.Styles;
  using iTrading.Core.Kernel;
  using iTrading.Core.Data;
  
  namespace iTrading.Core.Chart
  {
      /// <summary>
      /// </summary>
      public class ChartControl : UserControl
      {
          private bool activatedRegistered = false;
          private BarUpdateEventHandler barUpdateEventHandler = null;
          private Candle candleStyles;
          private const int candleStylesOffset = 0;
          private Hashtable chart2Indicators = new Hashtable();
          private Hashtable chart2Levels = new Hashtable();
          private IContainer components = null;
          private TextBox dummyTextBox;
          private int dummyValues = 0;
          private int first = -1;
          private HScrollBar hScrollBar;
          private ArrayList indicatorCharts = new ArrayList();
          private IndicatorCollection indicators = new IndicatorCollection();
          private bool isActive = false;
          private int last = -1;
          private int lookbackDailyData = 300;
          private const int maxCandleWidth = 20;
          private ArrayList midnight = new ArrayList();
          private const int mouseWheelAccelerator = 10;
          private ArrayList newChartMenuItems = new ArrayList();
          private int points = 0;
          private Quotes quotes = null;
          private Hashtable series2IndicatorSeries = new Hashtable();
          private double splitRatio = 0.66666666666666663;
          private Splitter splitter;
          private TChart teeChart;
  
          /// <summary>
          /// </summary>
          public ChartControl()
          {
              this.InitializeComponent();
              this.dummyTextBox.MouseWheel += new MouseEventHandler(this.dummyTextBox_MouseWheel);
              //    throw new TMException(ErrorCode.Panic, "This feature is not yet supported");
          }
  
          private void applyButton_Click(object sender, EventArgs e)
          {
              this.Register(((IndicatorSelectForm) ((Button) sender).Parent).Indicators);
          }
  
          internal void Arrange()
          {
              if ((this.quotes != null) && (this.quotes.Bars.Count != 0))
              {
                  this.teeChart.AutoRepaint = false;
                  foreach (TChart chart in this.indicatorCharts)
                  {
                      chart.AutoRepaint = false;
                  }
                  this.teeChart.Axes.Bottom.MaximumOffset = (int) (1.5 * this.candleStyles.CandleWidth);
                  this.teeChart.Axes.Bottom.MinimumOffset = (int) (1.5 * this.candleStyles.CandleWidth);
                  foreach (TChart chart2 in indicatorCharts)
                  {
                      chart2.Axes.Bottom.MaximumOffset = (int) (1.5 * this.candleStyles.CandleWidth);
                      chart2.Axes.Bottom.MinimumOffset = (int) (1.5 * this.candleStyles.CandleWidth);
                  }
                  this.points = (int) ((((base.Width - this.teeChart.Chart.Panel.MarginRight) - this.teeChart.Chart.Panel.MarginLeft) - (3.0 * this.candleStyles.CandleWidth)) / (1.5 * this.candleStyles.CandleWidth));
                  if ((((this.last - this.points) + 1) < 0) && ((this.quotes.Bars.Count + this.dummyValues) > this.points))
                  {
                      this.first = 0;
                      this.last = this.points - 1;
                  }
                  else
                  {
                      first = Math.Max((this.last - this.points) + 1, 0);
                  }
                  this.midnight.Clear();
                  double minValue = double.MinValue;
                  double maxValue = double.MaxValue;
                  for (int i = Math.Max(this.first, this.dummyValues); i <= this.last; i++)
                  {
                      if (((this.quotes.Period.Id != PeriodTypeId.Day) && ((i - this.dummyValues) > this.first)) && (this.quotes.Bars[i - this.dummyValues].Time.Date > this.quotes.Bars[(i - this.dummyValues) - 1].Time.Date))
                      {
                          this.midnight.Add(i);
                      }
                      else if (((this.quotes.Period.Id == PeriodTypeId.Day) && (i > (this.first + this.dummyValues))) && (this.quotes.Bars[i - this.dummyValues].Time.Month > this.quotes.Bars[(i - this.dummyValues) - 1].Time.Month))
                      {
                          this.midnight.Add(i);
                      }
                      minValue = Math.Max(minValue, this.quotes.Bars[i - this.dummyValues].High);
                      maxValue = Math.Min(maxValue, this.quotes.Bars[i - this.dummyValues].Low);
                  }
                  if (minValue != double.MinValue)
                  {
                      this.teeChart.Axes.Bottom.SetMinMax((double) this.first, (double) this.last);
                      this.teeChart.Axes.Right.SetMinMax(maxValue, minValue);
                      foreach (Steema.TeeChart.Chart chart3 in this.chart2Indicators.Keys)
                      {
                          double num4 = (chart3 == this.teeChart.Chart) ? minValue : double.MinValue;
                          double num5 = (chart3 == this.teeChart.Chart) ? maxValue : double.MaxValue;
                          foreach (IndicatorBase.IndicatorBase base2 in (ArrayList) this.chart2Indicators[chart3])
                          {
                              foreach (IndicatorSeries series in base2.resultSeries)
                              {
                                  for (int j = Math.Max(this.first, this.dummyValues); j <= this.last; j++)
                                  {
                                      num4 = Math.Max(num4, series[j - this.dummyValues]);
                                      num5 = Math.Min(num5, series[j - this.dummyValues]);
                                  }
                              }
                          }
                          chart3.Axes.Bottom.SetMinMax((double) this.first, (double) this.last);
                          chart3.Axes.Right.SetMinMax(num5, num4);
                      }
                  }
                  this.teeChart.AutoRepaint = true;
                  foreach (TChart chart4 in this.indicatorCharts)
                  {
                      chart4.AutoRepaint = true;
                  }
                  if (!this.activatedRegistered && (this.Form != null))
                  {
                      this.Form.Activated += new EventHandler(this.Form_Activated);
                      this.Form.Deactivate += new EventHandler(this.Form_Deactivate);
                      this.activatedRegistered = true;
                  }
                  this.hScrollBar.LargeChange = this.points;
                  this.hScrollBar.Maximum = (this.quotes.Bars.Count + this.dummyValues) - 1;
                  this.hScrollBar.Minimum = 0;
                  this.hScrollBar.SmallChange = 1;
                  this.hScrollBar.Value = Math.Max(0, (this.last - this.points) + 1);
                  if (this.isActive)
                  {
                      this.dummyTextBox.Focus();
                  }
              }
          }
  
          private void ChartControl_Resize(object sender, EventArgs e)
          {
              this.ResizeControls();
          }
  
          private void CreateChartSeries(IndicatorBase.IndicatorBase indicator, TChart chart)
          {
              Trace.Assert(indicator.ChartSeries != null, "Chart.ChartControl.CreateSeries: Indicator.IndicatorBase.ChartSeries can not be NULL");
              for (int i = 0; i < indicator.CountSeries; i++)
              {
                  Series s = null;
                  if (i < indicator.ChartSeries.Count)
                  {
                      s = indicator.ChartSeries[i];
                  }
                  else
                  {
                      Line series = new Line();
                      series.Brush.Color = Color.DarkViolet;
                      indicator.ChartSeries.Add(series);
                      s = series;
                  }
                  s.HorizAxis = this.candleStyles.HorizAxis;
                  s.VertAxis = this.candleStyles.VertAxis;
                  chart.Series.Add(s);
                  this.series2IndicatorSeries.Add(s, indicator.resultSeries[i]);
              }
              ArrayList list = null;
              list = (ArrayList) this.chart2Indicators[chart.Chart];
              if (list == null)
              {
                  list = new ArrayList();
                  this.chart2Indicators[chart.Chart] = list;
              }
              list.Add(indicator);
              LevelCollection levels = null;
              levels = (LevelCollection) this.chart2Levels[chart.Chart];
              if (levels == null)
              {
                  levels = new LevelCollection();
                  this.chart2Levels[chart.Chart] = levels;
              }
              foreach (Level level in indicator.Levels)
              {
                  levels.Add(level);
              }
          }
  
          /// <summary> 
          /// </summary>
          protected override void Dispose(bool disposing)
          {
              if (disposing && (this.components != null))
              {
                  this.components.Dispose();
              }
              base.Dispose(disposing);
          }
  
          private void dummyTextBox_KeyDown(object sender, KeyEventArgs e)
          {
              bool flag = false;
              if ((e.KeyCode == Keys.Left) && (this.last > (this.points - 1)))
              {
                  this.last--;
                  flag = true;
              }
              else if ((e.KeyCode == Keys.Right) && (this.last < ((this.quotes.Bars.Count + this.dummyValues) - 1)))
              {
                  this.last++;
                  flag = true;
              }
              else if ((e.KeyCode == Keys.Next) && (this.last > (this.points - 1)))
              {
                  this.last = Math.Max((int) (this.points - 1), (int) ((this.last - this.points) + 1));
                  flag = true;
              }
              else if ((e.KeyCode == Keys.Prior) && (this.last < ((this.quotes.Bars.Count + this.dummyValues) - 1)))
              {
                  this.last = Math.Min((int) ((this.quotes.Bars.Count + this.dummyValues) - 1), (int) ((this.last + this.points) - 1));
                  flag = true;
              }
  
gt:             else if (e.KeyCode == Keys.Home)