context: namespace Steema.TeeChart
  {
      using Steema.TeeChart.Drawing;
      using Steema.TeeChart.Functions;
      using Steema.TeeChart.Styles;
      using System;
      using System.Collections;
      using System.ComponentModel;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Reflection;
      using System.Runtime.CompilerServices;
      using System.Runtime.InteropServices;
      using System.Windows.Forms;
  
      [ToolboxItem(false)]
      public class GalleryPanel : System.Windows.Forms.Panel
      {
          public GalleryChartCollection Charts = new GalleryChartCollection();
          public static bool CheckMaximize = true;
          private bool checkSeries = true;
          private int colWidth;
          public static Cursor CursorDisabled = Cursors.No;
          private bool displaySub = true;
          public bool FunctionsVisible = true;
          public static int GalleryNumCols = 4;
          public static int GalleryNumRows = 4;
          private ArrayList items = new ArrayList();
          private int numCols = 4;
          private int numRows = 4;
          private int rowHeight;
          public GalleryChart SelectedChart;
          public Series SelectedSeries;
          private SmoothingMode smooth = SmoothingMode.HighSpeed;
          private GalleryPanel tmpG;
          private Series tmpSeries;
          private Form tmpSub;
          private bool view3D = true;
  
          public event EventHandler OnChangeChart;
  
          public event EventHandler OnSelectedChart;
  
          public event EventHandler OnSubSelected;
  
          private void AddSubCharts(GalleryPanel AGallery)
          {
              AGallery.Charts.Clear();
              if (this.tmpSeries != null)
              {
                  this.tmpSeries.CreateSubGallery(new Series.SubGalleryEventHandler(this.CreateSubChart));
                  AGallery.CalcChartWidthHeight();
              }
          }
  
          internal void AddSubCharts(Series s)
          {
              this.tmpG = this;
              this.tmpSeries = s;
              this.AddSubCharts(this);
          }
  
          private void CalcChartWidthHeight()
          {
              if ((this.NumRows > 0) && (this.NumCols > 0))
              {
                  int num = 1;
                  this.rowHeight = (base.ClientRectangle.Height - num) / this.NumRows;
                  this.colWidth = (base.ClientRectangle.Width - num) / this.NumCols;
              }
              else
              {
                  this.rowHeight = 0;
                  this.colWidth = 0;
              }
          }
  
          private void ChartEvent(object sender, EventArgs e)
          {
              if (sender is GalleryChart)
              {
                  this.SelectChart((GalleryChart) sender);
              }
          }
  
          private void ChartOnDblClick(object sender, EventArgs e)
          {
              this.SelectedChart = (GalleryChart) sender;
              if (this.OnSelectedChart != null)
              {
                  this.OnSelectedChart(this, e);
              }
          }
  
          private GalleryChart CreateChart(System.Type seriesType)
          {
              return this.CreateChart(Utils.SeriesTypesIndex(seriesType), "", null);
          }
  
          private GalleryChart CreateChart(int index, System.Type function)
          {
              return this.CreateChart(index, this.FunctionDescription(function), function);
          }
  
          private GalleryChart CreateChart(int index, string title, System.Type function)
          {
              GalleryChart chart = new GalleryChart(this.Smooth);
              this.Charts.Add(chart);
              chart.Parent = this;
              this.ResizeChart(chart);
              if (function == null)
              {
                  this.CreateSeries(chart, index);
              }
              else
              {
                  for (int i = 1; i <= 2; i++)
                  {
                      if (function == typeof(CompressOHLC))
                      {
                          Series.CreateNewSeries(chart.Chart, typeof(Candle), function);
                      }
                      else
                      {
                          Series.CreateNewSeries(chart.Chart, typeof(Line), function);
                      }
                  }
              }
              chart.Header.Lines = ((title.Length == 0) ? chart[0].Description : title).Split(new char[] { '\n' }, 5);
              bool flag = (this.checkSeries && (this.SelectedSeries != null)) && !chart[0].IsValidSourceOf(this.SelectedSeries);
              if (flag)
              {
                  chart.Cursor = Cursors.No;
                  chart.Click -= new EventHandler(this.ChartEvent);
                  chart.Enter -= new EventHandler(this.ChartEvent);
                  chart.DoubleClick -= new EventHandler(this.ChartOnDblClick);
                  chart.Header.Font.Color = Color.Gray;
                  chart.Walls.Left.Pen.Color = Color.Gray;
                  chart.Walls.Bottom.Pen.Color = Color.Gray;
                  chart.Axes.Left.AxisPen.Width = 1;
                  chart.Axes.Left.AxisPen.Color = Color.White;
                  chart.Axes.Bottom.AxisPen.Width = 1;
                  chart.Axes.Bottom.AxisPen.Color = Color.White;
              }
              else
              {
                  chart.Cursor = Cursors.Hand;
                  chart.Click += new EventHandler(this.ChartEvent);
                  chart.DoubleClick += new EventHandler(this.ChartOnDblClick);
                  chart.Enter += new EventHandler(this.ChartEvent);
              }
              chart.Series[0].GalleryChanged3D(this.view3D);
              foreach (Series series in chart.Series)
              {
                  series.PrepareForGallery(!flag);
              }
              chart.SetMargins();
              chart.CheckShowLabels();
              return chart;
          }
  
          public void CreateGalleryPage(string pageName)
          {
              this.CreateGalleryPage(pageName, false);
          }
  
          public void CreateGalleryPage(string pageName, bool functions)
          {
              this.Charts.Clear();
              if (functions)
              {
                  for (int i = 0; i < 0x1b; i++)
                  {
                      if (this.ValidFunction(i, pageName))
                      {
                          this.items.Add(Utils.FunctionTypesOf[i].ToString());
                          this.CreateChart(i, Utils.FunctionTypesOf[i]);
                      }
                  }
              }
              else
              {
                  for (int j = 0; j < 0x2c; j++)
                  {
                      if (this.ValidSeries(j, pageName))
                      {
                          this.items.Add(Utils.SeriesTypesOf[j].ToString());
                          this.CreateChart(Utils.SeriesTypesOf[j]);
                      }
                  }
              }
              this.FindSelectedChart();
              this.ShowSelectedChart();
          }
  
          public void CreateGallerySeries(System.Type seriesType)
          {
              this.items.Add(seriesType.ToString());
              this.CreateChart(seriesType);
          }
  
          private void CreateSeries(GalleryChart c, int s)
          {
              for (int i = 1; i <= Math.Max(1, Utils.SeriesGalleryCount[s]); i++)
              {
                  Series.CreateNewSeries(c.Chart, Utils.SeriesTypesOf[s], null);
              }
          }
  
          private Chart CreateSubChart(string Name)
          {
              int count = this.tmpG.Charts.Count;
              GalleryChart chart = this.tmpG.CreateChart(Utils.SeriesTypesIndex(this.tmpSeries), Name, null);
              for (int i = 0; i < chart.Series.Count; i++)
              {
                  chart[i].SetSubGallery(count);
              }
              return chart.Chart;
          }
  
          private void CreateSubGallery(GalleryPanel AGallery, System.Type type)
          {
              this.tmpSeries = Series.CreateNewSeries(null, type, null);
              this.AddSubCharts(AGallery);
              int num = AGallery.Charts.Count / AGallery.NumCols;
              if ((AGallery.Charts.Count % AGallery.NumCols) > 0)
              {
                  num++;
              }
              AGallery.Parent.Height = 0x26 + (AGallery.rowHeight * num);
              AGallery.NumRows = num;
              this.tmpSeries.Dispose();
          }
  
          private GalleryChart FindChartXY(int x, int y)
          {
              foreach (GalleryChart chart in this.Charts)
              {
                  int num;
                  int num2;
                  this.GetChartXY(chart, out num, out num2);
                  if ((x == num) && (y == num2))
                  {
                      return chart;
                  }
              }
              return null;
          }
  
          private void FindSelectedChart()
          {
              this.SelectedChart = null;
              if (this.SelectedSeries != null)
              {
                  for (int i = 0; i < this.Charts.Count; i++)
                  {
                      if ((this.items[i] == this.SelectedSeries.GetType()) && (this.Charts[i].Series[0].Function == null))
                      {
                          this.SelectedChart = this.Charts[i];
                          return;
                      }
                  }
              }
          }
  
          private string FunctionDescription(System.Type f)
          {
              return Function.NewInstance(f).Description();
          }
  
          public static string GalleryPages(int index)
          {
              switch (index)
              {
                  case 0:
                      return Texts.GalleryStandard;
  
                  case 1:
                      return Texts.GalleryFinancial;
  
                  case 2:
                      return Texts.GalleryStats;
  
                  case 3:
                      return Texts.GalleryExtended;
  
                  case 4:
                      return Texts.Gallery3D;
              }
              return Texts.GallerySamples;
          }
  
          private void GetChartXY(GalleryChart c, out int x, out int y)
          {
              int index = this.Charts.IndexOf(c);
              y = index / this.numCols;
              x = index % this.numCols;
          }
  
          protected override void OnKeyDown(KeyEventArgs e)
          {
              int num;
              int num2;
              base.OnKeyDown(e);
              this.GetChartXY(this.SelectedChart, out num, out num2);
              switch (e.KeyCode)
              {
                  case Keys.Left:
                      if (num > 0)
                      {
                          num--;
                      }
                      break;
  
                  case Keys.Up:
                      if (num2 > 0)
                      {
                          num2--;
                      }
                      break;
  
                  case Keys.Right:
                      if (num < this.NumCols)
                      {
                          num++;
                      }
                      break;
  
                  case Keys.Down:
                      if (!e.Alt)
                      {
                          if (num2 < this.NumRows)
                          {
                              num2++;
                          }
                          break;
                      }
                      if (this.DisplaySub)
                      {
                          this.ShowSubGallery();
                      }
                      break;
  
                  case Keys.Return:
                      this.ChartOnDblClick(this.SelectedChart, new EventArgs());
                      break;
              }
  
gt:             GalleryChart c = this.FindChartXY(num, num2);