//---------------------------------------------------------------------------
// Updated by: Chr. Monstein 27.01.2000  pixeltime included instead of 0.0005
// Updated by: Chr. Monstein 03.05.2001  horizontal zoom included
// Updated by: cCr. Monstein 07.03.2002  buffer allocation instead of arrays
// Updated by: Chr. Monstein 10.11.2004  vertical lines
// Updated by: Chr. Monstein 17.03.2005  scaling, labels etc
// Updated by: Chr. Monstein 18.09.2008  plott reduziert auf S,R
// Updated by: Chr. Monstein 22.09.2008  clean up
// Updated by: Chr. Monstein 16.01.2014  clean up
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include "UnitYT.h"
#include "imagewn.h"
#include <fstream.h>
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm2 *Form2;
extern long rows; // params
extern double *act, *Icold;
extern double Fmin, Fmax;
extern double mini, maxi;
extern int Rmin, Rmax;
extern int iFileDate;
extern char unit[10];
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)

    : TForm(Owner)
{
   Form2->Width=ImageForm->Left*0.75-5;
   Form2->Height=ImageForm->Top*0.75+50; // Basisgroesse Plottfenster
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormResize(TObject *Sender)
{
   Form2->PaintBox1->Width  = ClientWidth; //
   Form2->PaintBox1->Height = ClientHeight; //
   Form2->Left = 0;
   Form2->Top  = 0;
   Form2->Invalidate();
}
//---------------------------------------------------------------------------
void Xaxis(int BBottom, int BTop, int BLeft, int BRight, int ALeft, int ARight, int ATop)
{
    char Label[240];
    int Nx = 10; // number of ticks
    float dx; // Abstand der ticks in Pixels
    int w = Form2->PaintBox1->Canvas->TextWidth ("100000");
    int h;

    dx = (float)(BRight-BLeft)/(float)Nx; // Abstand der ticks in Pixels
    int i = 0;
    int y1 = BBottom; int y2 = y1+5; int y3 = y2+4;
    float x, xalt = -9999;

    Form2->PaintBox1->Canvas->Brush->Color=clWhite; // Schriftfarbe
    Form2->PaintBox1->Canvas->Pen->Color=clGray; // Liniefarbe

    do {
        x = (float)BLeft+(float)i*dx;
        Form2->PaintBox1->Canvas->MoveTo(x,y1);
        Form2->PaintBox1->Canvas->LineTo(x,y2);
        // sweepcalculation
        if (i == 0)   // 0
           sprintf(Label,"%7.2lf",act[(int)(Rmin)]);
        else
           //sprintf(Label,"%7.2lf",act[(int)(i*(rows-1)/Nx)]);
           sprintf(Label,"%7.2lf",act[(int)(i*(Rmax-1)/Nx)]);

        if ((x-xalt) > (w+5))
        {
            xalt = x;
            Form2->PaintBox1->Canvas->LineTo(x,y3);
            Form2->PaintBox1->Canvas->TextOut(x-w/2,y3+2,Label);
        }
        i++;
    } while (i <= Nx); // Nx

    char st[80];
    sprintf(Label,"Frequency [MHz]"); // y-label
    w = Form2->PaintBox1->Canvas->TextWidth(Label);
    h = Form2->PaintBox1->Canvas->TextHeight(Label);
    Form2->PaintBox1->Canvas->TextOut((BLeft+BRight)/2-w/2,y3+h,Label);
    Form2->PaintBox1->Canvas->Font->Size = 9;
    sprintf(Label,"%s%s","Intensity of ",ImageForm->FileListBox1->FileName.c_str()); // title
    Form2->Caption = Label; // top title
    sprintf(Label,"File->%s",ExtractFileName(ImageForm->FileListBox1->FileName));
    w = Form2->PaintBox1->Canvas->TextWidth(Label);
    Form2->PaintBox1->Canvas->TextOut((ALeft+ARight)/2-w/2+5,ATop+2,Label); // 2. titel
}
//---------------------------------------------------------------------------
void Yaxis(int BBottom, int BTop, int BLeft, int ALeft, int ATop, int BRight, int ARight)
{
    char Label[40];
    int Ny = 10;  // 22number of zwischenräume
    int i, w, h, x1, x2;
    float dy = (float)(BBottom-BTop)/(float)Ny;  // digits
    double y, ys;
    i = 0;
    x1 = BLeft;
    x2 = x1-5;
    do {
        y = (float)BBottom-(float)i*dy; // digits from bottom
        Form2->PaintBox1->Canvas->MoveTo(x1,y);
        Form2->PaintBox1->Canvas->LineTo(x2,y); // horizontal ticks
        Form2->PaintBox1->Canvas->Pen->Color=clBlack; // Linienfarbe
        ys = mini + (float)i*(maxi-mini)/(float)Ny; // y-label
        sprintf(Label,"%4.1f",ys); // y-label
        w = Form2->PaintBox1->Canvas->TextWidth(Label);
        h = Form2->PaintBox1->Canvas->TextHeight(Label);
        Form2->PaintBox1->Canvas->Brush->Color=clWhite; // Schriftfarbe
        Form2->PaintBox1->Canvas->TextOut(BLeft-6-w,y-h/2,Label);
        i++;
    } while (i<=Ny);
    //sprintf(Label,"dB"); // y-label
    Form2->PaintBox1->Canvas->TextOut(ALeft+15,ATop+2,unit);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::PaintBox1Click(TObject *Sender)
{
    float x, xa, y;
    int yp, ya;

    // Ausserer Rahmen (Bildflaeche)
    int ALeft=0, ATop=0, ARight=ClientWidth-15, ABottom=ClientHeight-15; //40
    TRect Rahmen = Rect(ALeft,ATop,ARight, ABottom); // ALeft, ATop,  ARight,  ABottom
    Form2->PaintBox1->Canvas->Brush->Color=clWhite;//clYellow;
    Form2->PaintBox1->Canvas->FillRect(Rahmen);

    // Innerer (Plott)Rahmen
    int BLeft=50, BTop=25, BRight=ARight-40, BBottom=ABottom-40; // 50
    TRect Plottarea = Rect(BLeft,BTop,BRight, BBottom); // ALeft, ATop,  ARight,  ABottom
    Form2->PaintBox1->Canvas->Brush->Color=clNavy;//clBlack;
    Form2->PaintBox1->Canvas->FillRect(Plottarea);

    if (!FileExists(ImageForm->FileListBox1->FileName)) return; // nothing to draw
    if (rows<1) return;  // don't draw if nothing to draw! (header only)
    Xaxis(BBottom, BTop, BLeft, BRight, ALeft, ARight, ATop); // x-ticks
    float vrange = (float)(BBottom - BTop);
    float hrange = (float)(BRight  - BLeft);
    int row = Rmin;

    ImageForm->Label4->Caption = Rmin;
    ImageForm->Label7->Caption = Rmax;
    ImageForm->Label8->Caption = Fmin;
    ImageForm->Label9->Caption = Fmax;
    ImageForm->Label10->Caption = hrange;
    ImageForm->Label11->Caption = BLeft;
    float ddx =  float(Rmax-Rmin)/float(BRight  - BLeft);
    if (Rmax>Rmin)
    {
      do { // graph
          x = BLeft + (float)(row-Rmin)/ddx; // resized x-axis
          yp = ((Icold[row]-mini)/float(maxi-mini))*vrange; // y-axis pointer
          y = BBottom - (float)yp; // resized y-axis
          if ((y >= BTop) && (y < BBottom))
          {
            Form2->PaintBox1->Canvas->MoveTo(x,BBottom);
            Form2->PaintBox1->Canvas->Pixels[x][y] = clYellow;//clBlue;yellow
          }
          row++;
      } while (row < Rmax);
    }
    Yaxis(BBottom, BTop, BLeft, ALeft, ATop, BRight, ARight); // y-ticks
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormClick(TObject *Sender)
{
    ShowMessage("Yellow = Signal-min(Signal) [dB]\nFiles can have the format:\n####.### ####.###\n####.###;####.###\n####.###,####.###\n");
}
//---------------------------------------------------------------------------


