//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#pragma hdrstop

#include "OutGraphic.h"
#include "UnitGraphic.h"
#include "ListConst.h"
//#include "UnitFunc.h"
//#include "UnitText.h"
#include "Gout.h"
//#include "rrk.h"
//#include "rrk_ext.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define NFUN0   1000


TOutGraphicForm *OutGraphicForm;

bool flag_out;
Func func[20];
int num_func;
int flag_func; // 0 - модификация, 1 - задание

        double  bx;
        double  by;
        double  hi;
        double  an;
        double  it;
        int     flag_q;
double mark_step;
double mark_size;
double mark_shift;
double repy = 0;  /* положение оси Y по X */
int flag_format  = 0;
int flag_funk  = 0;
int flag_f  = 0;
int flag_pg = 0;
int flag_e  = 0;
double  numf = 7e0;
char    marker[5] = "0";
int npag;
int type_line;

AnsiString  GrapFileName="Untitled";
extern double mark_step;
extern double mark_size;
extern double mark_shift;
extern char    marker[5] ;
extern FILE *  grap;
extern TMetafile *GrapMeta;
extern TFont * TheFont;
extern FILE  * input_g;
extern FILE  * output_g;
extern double repy;  /* положение оси Y по X */

extern int flag_q;
int flag_log;

double Scale_grap;
int H0;
static double num_dev=1;
static char str_dummy[100];
//void pFunc(TMetafileCanvas *pCanvas, NOP* nop);
//void pText(TMetafileCanvas *pCanvas, NOP* nop);
//int g_fun_out(TMetafileCanvas *pCanvas, NOP* nop);

//---------------------------------------------------------------------------
__fastcall TOutGraphicForm::TOutGraphicForm(TComponent* Owner)
        : TForm(Owner)
{
    repy = 0;  /* положение оси Y по X */
    flag_format  = 0;
    flag_funk  = 0;
    flag_f  = 0;
    flag_pg = 0;
    flag_log = 0;
    flag_e  = 0;
    npag = 1;
    type_line = 0;
    bx = 0e0;
    by = 0e0;
    an = 0;
    it = 0;
    hi = 5;
}
//---------------------------------------------------------------------------
void __fastcall TOutGraphicForm::N12Click(TObject *Sender)
{
// изменить параметры

}
//---------------------------------------------------------------------------
void __fastcall TOutGraphicForm::FormShow(TObject *Sender)
{
   flag_out=false;
}
//---------------------------------------------------------------------------

static double hs;/*--------------------------------------------------------*/
/*   Последовательный вывод функций                       */
/*--------------------------------------------------------*/
void __fastcall TOutGraphicForm::pFunc(TMetafileCanvas *pCanvas, NOP* nop)
{
   int i,rc;
   for (i=0; i<n_fun; i++) {
       //strcpy(nop->fi, func[i].fi);
       //strcpy(nop->nf, func[i].nf);
       //strcpy(nop->gf, func[i].gf);
       //strcpy(nop->ei, func[i].ei);
       //nop->mf = func[i].mf;
       nop->mf = 1;
       //marker[0] = func[i].mark[0];
       //mark_size = func[i].mark_size;
       //mark_step = func[i].mark_step;
       //mark_shift = func[i].mark_shift;

       rc=g_fun_out(pCanvas, nop, index_fun[i]);
   }

}
/*--------------------------------------------------------*/
/*   вывод   функции                                      */
/*--------------------------------------------------------*/
int __fastcall TOutGraphicForm::g_fun_out(TMetafileCanvas *pCanvas, NOP* nop, int num_fun)
{
    //WOGL   grogl;
    //char   nfu[NFUN0*8];
    int i;
    float *rez;
    double X0,Y0,x,y,Mt,Mf,t,delx,tm,yr0,up;
    int j,k,rc;
    double max_f, min_f;
    AnsiString format;


    flag_funk = 1;
    j=nop->yr/(2*nop->sty);
    yr0=j*nop->sty*2;
    max_f=-1e20;
    min_f=1e20;
    format=", max="+Edit_Y_format->Text+", min="+Edit_Y_format->Text;
    //OPENRB(nop->fi, grap);
    //if (grap==NULL)
    //{
    //    g_outc("nФайл (%s) не найден",nop->fi);
    //    return 1;
    //}

    //READ (&grogl,sizeof(WOGL),1,grap);
    //if (grogl.nrez <1 || grogl.dt <0)
    //{
    //    g_outc("Ошибочный файл %s",nop->fi);
    //    CLOSE(grap);
    //    return 1;
    //}

    //READ(nfu,8,grogl.nrez,grap);
    //if (strcmp(nop->nx,"")!=0) {
    //   for (j=0; j<grogl.nrez; j++)
    //    {
    //        if (strncmp(nfu+j*8,nop->nx,8)==0)
    //            break;
    //    }

    //    if (j==grogl.nrez)
    //    {
    //        sprintf(str_dummy,"Файл %s. Нет функции %s ",nop->fi,nop->nx);
    //        g_outop(str_dummy);
    //        CLOSE(grap);
    //        return 1;
    //    }
    //}
    //else jx = -1;

    //for (j=0; j<grogl.nrez; j++)
    //{
    //    if (strncmp(nfu+j*8,nop->nf,8)==0)
    //        break;
    //}
    //if (j==grogl.nrez)
    //{
    //    sprintf(str_dummy,"Файл %s. Нет функции %s ",nop->fi,nop->nf);
    //    g_outop(str_dummy);
    //    CLOSE(grap);
    //    return 1;
    //}
    /*
    if (jx == -1) {
    // аргумент неявный
        X0=nop->x;
        Y0=nop->y + yr0*0.5*(nop->py + 1);
        Mf=nop->mf/nop->my;
        Mt=1/nop->mx;
        READ(rez,8,grogl.nrez,grap);

        max_f=min_f=rez[j]*nop->mf;

        y = Y0 + (rez[j] - nop->y00)*Mf;
        delx = grogl.dt * Mt;

        g_ot_(pCanvas,X0, y);
        if (flag_funk==0)
            g_mout_(pCanvas,X0,y);
        tm=mark_shift;
        pCanvas->Pen->Width=2;
        for (t = X0 + delx; t < X0 + nop->xr + delx; t += delx, tm += delx)
        {
            READ(rez,8,grogl.nrez,grap);
            if (rc == 0) break;
            max_f=MAX(max_f,rez[j]*nop->mf);
            min_f=MIN(min_f,rez[j]*nop->mf);
            y = Y0 + (rez[j] - nop->y00)*Mf;
            if (flag_funk==1) {
               g_bot_(pCanvas,t, y);
               if (tm>=mark_step)
               {
                  g_mout_(pCanvas,t,y);
                  tm =0;
               }
            }
            else {
                  g_ot_(pCanvas,t, y);
                  g_mout_(pCanvas,t,y);
            }
        }
        t += 1-delx;
        if (flag_q ==0)
           g_str_(pCanvas,t, y, nop->gf);
    }
    */
    //else {
    // аргумент явный
        j = num_fun;  // номер функции

        X0=nop->x;
        Y0=nop->y + yr0*0.5*(nop->py + 1);
        Mf=nop->mf/nop->my;
        Mt=1/nop->mx;
        //READ(rez, 8, grogl.nrez, grap);
        rez = matr_result[0];
        max_f=min_f=rez[j]*nop->mf;

        y = Y0 + (rez[j] - nop->y00)*Mf;
        if (flag_log==0)
           //x = X0 + (rez[jx] - nop->x0)*Mt;
           x = X0 + (x_result[0] - nop->x0)*Mt;
        else {
           //if (rez[jx]<=0)
           if (x_result[0]<=0)
               x=X0;
           else
               //x = X0 + (log10(rez[jx]) - nop->x0)*Mt;
               x = X0 + (log10(x_result[0]) - nop->x0)*Mt;
        }
        g_ot_(pCanvas,x, y);
        if (flag_funk==0)
            g_mout_(pCanvas,x,y);
        tm=mark_shift;
        up=X0;
        delx=0;
        //for (t = X0 + delx; t < X0 + nop->xr + delx; t += delx, tm += delx)
        for (i = 1;  i< n_result; i++)
        {
            //READ(rez,8,grogl.nrez,grap);
            //if (rc == 0) break;
            rez = matr_result[i];

            max_f=MAX(max_f,rez[j]*nop->mf);
            min_f=MIN(min_f,rez[j]*nop->mf);
            y = Y0 + (rez[j] - nop->y00)*Mf;
            if (flag_log==0)
               //x = X0 + (rez[jx] - nop->x0)*Mt;
               x = X0 + (x_result[i] - nop->x0)*Mt;
            else
              //if (rez[jx]<=0)
              if (x_result[i]<=0)
                x=X0;
              else
                //x = X0 + (log10(rez[jx]) - nop->x0)*Mt;
                x = X0 + (log10(x_result[i]) - nop->x0)*Mt;
            if (flag_funk==1) {
               g_bot_(pCanvas,x, y);
               if ((ABS(tm))>=mark_step)
               {
                   g_mout_(pCanvas,x,y);
                   tm =0;
               }
            }
            else {
                  g_ot_(pCanvas,x, y);
                  g_mout_(pCanvas,x,y);
            }
            delx = x - up;
            up=x;
        }
        x += 1;
        /* g_h((double)5.0);  */
        if (flag_q ==0)
           g_str_(pCanvas,x, y, nop->gf);
    //}
    pCanvas->Pen->Width=1;
    if (flag_e !=0)
    {
        up = numf+ 2;
        g_ot_(pCanvas,nop->x,nop->y - up - 0.5*hs);
        g_bot_( pCanvas,nop->x + 20, nop->y - up - 0.5*hs);
        g_mout_(pCanvas,nop->x + 10, nop->y - up );
        sprintf(str_dummy," %8s - %25s, масштаб 1 мм = %8.2f %s",nop->nf,nop->gf,nop->my,
            nop->ei);
        g_str_(pCanvas,nop->x + 20,nop->y - up - hi, str_dummy);
        numf += hs+2;
    }
    if (flag_q !=0)
    {
        up = numf+ 2;
        g_ot_(pCanvas,nop->x,nop->y - up - 0.5*hs);
        g_bot_( pCanvas,nop->x + 10, nop->y - up - 0.5*hs);
        g_ot_(pCanvas,nop->x+5,nop->y - up - 0.5*hs);
        g_mout_(pCanvas,nop->x + 5, nop->y - up );
        if (nop->ei[0]=='.')
           sprintf(str_dummy,"  %s ",nop->gf);
        else
           sprintf(str_dummy,"  %s (%s)",nop->gf,nop->ei);
        if (flag_q>1) {  // вывод MAX/MIN
           sprintf(str_dummy+strlen(str_dummy),format.c_str(),max_f,min_f);
           g_str_(pCanvas,nop->x + 15,nop->y - up -hi*0.75 , str_dummy);
        }
        g_str_(pCanvas,nop->x + 15,nop->y - up -hi*0.75 , str_dummy);
        numf += hs+2;
    }

    //CLOSE(grap);
    return 0;
}   /* g_fun_  */

//---------------------------------------------------------------------------
/*--------------------------------------------------------*/
/*   Установка шрифта                                     */
/*--------------------------------------------------------*/
void pFont(TMetafileCanvas *pCanvas, NOP* nop)
{
   int h; 
   TheFont = new TFont();

   TheFont->Charset = RUSSIAN_CHARSET;

   TheFont->Name=OutGraphicForm->ComboBox_font->Text;
   TheFont->Height=StrToInt(OutGraphicForm->ComboBox_height->Text);
   if (OutGraphicForm->SpeedButton_italic->Down)
      TheFont->Style=TFontStyles()<<fsItalic;
   pCanvas->Font=TheFont;
   h=pCanvas->Font->Height;
}
/*--------------------------------------------------------*/
/*   сетка                                                */
/*--------------------------------------------------------*/
void pSetka(TMetafileCanvas *pCanvas, NOP* nop)
{
    double t;
    int type;
    int j,i;
    double xx,yy,ss;

        for (t=nop->x; t<nop->x+nop->xr; t += nop->stx )
        {
            g_ot_(pCanvas,t, nop->y);
            g_bot_(pCanvas,t, nop->y + nop->yr);
            if (flag_log==1) {
                xx=nop->stx*nop->mx;
                ss=pow(10e0,xx);
                yy=ss/num_dev;
                for (i=1, ss=yy; i<num_dev; i++) {
                    xx=log10(ss);
                    xx /= nop->mx;
                    g_ot_(pCanvas,t+xx, nop->y);
                    g_bot_(pCanvas,t+xx, nop->y + nop->yr);
                    ss += yy;
                }
            }
        }
        g_ot_(pCanvas,nop->x+nop->xr, nop->y);
        g_bot_(pCanvas,nop->x+nop->xr, nop->y + nop->yr);
        for (t=nop->y; t < nop->y + nop->yr; t += nop->sty)
        {
            g_ot_(pCanvas,nop->x, t);
            g_bot_(pCanvas,nop->x + nop->xr, t);
        }
        g_ot_(pCanvas,nop->x, nop->y + nop->yr);
        g_bot_(pCanvas,nop->x + nop->xr, nop->y + nop->yr);
        flag_e=0;
        numf=7e0;
}
/*--------------------------------------------------------*/
/*   Вывод текста                                                */
/*--------------------------------------------------------*/
void __fastcall TOutGraphicForm::pText(TMetafileCanvas *pCanvas, NOP* nop)
{
    int j,i;
    double x,y;

    for (i=0; i<n_text; i++) {
      /*
      if (FormText->Memo_X->Lines->Count<=i) {
         x = 0;
      }
      else {
         x=StrToFloat(FormText->Memo_X->Lines->Strings[i]);
      }
      if (FormText->Memo_Y->Lines->Count<=i) {
         y=10;
      }
      else {
         y=StrToFloat(FormText->Memo_Y->Lines->Strings[i]);
      }
      g_str_(pCanvas,x,y,FormText->Memo_Text->Lines->Strings[i].c_str());
      */
      g_str_(pCanvas, x_text[i], y_text[i], out_text[i]);
   }
}
/*--------------------------------------------------------*/
/*   Оформление оси X                                     */
/*--------------------------------------------------------*/
void __fastcall TOutGraphicForm::pAx_X(TMetafileCanvas *pCanvas, NOP* nop)
{
    double t,x,y;
    double yr0;
    char str[124];
    int j,l;
    double xx,yy;

    j=nop->yr/(2*nop->sty);
    yr0=j*nop->sty*2;
    x = nop->x;
    y = nop->y + yr0*(nop->pf+1)*0.5 - hi-1;
    if (flag_log==0) {
        for (t=x+nop->stx+1; t<=x+nop->xr+1; t += nop->stx) {
            sprintf(str,nop->formx,(t-nop->x-1)*nop->mx + nop->x0);
            g_vir_(pCanvas,t,y,str);
        }
    }
    else {
        for (t=x+1; t<=x+nop->xr+1; t += nop->stx) {
            xx = (t-nop->x-1)*nop->mx + nop->x0;
            yy=pow(10e0,xx);
            sprintf(str,nop->formx,yy);
            g_vir_(pCanvas,t,y,str);
        }
    }
    t= x+nop->xr+1;
    y = nop->y + yr0*(nop->pf+1)*0.5 + 1 + nop->hi/Scale_grap;
    g_str_(pCanvas,t,y,nop->gx);
    return;
}
/*--------------------------------------------------------*/
/*   оформление оси y                                     */
/*--------------------------------------------------------*/
void __fastcall TOutGraphicForm::pAx_Y(TMetafileCanvas *pCanvas, NOP* nop)
{
    double t,x,y;
    double yr0;
    double ss;
    char str[120];
    int i,j;

    repy=0;
    j=nop->yr/(2*nop->sty);
    yr0=j*nop->sty*2;
    x = nop->x - 1 + repy;
    y = nop->y;
    if (repy != 0)
    {
        g_ot_(pCanvas,x+1,y);
        g_bot_(pCanvas,x+1,y+yr0);
    }
    //g_an(90e0);
    for (t=y; t<=y+nop->yr+1; t += nop->sty)
    {
        if (repy != 0)
        {
            g_ot_(pCanvas,x-nop->hi/2+1,t);
            g_bot_(pCanvas,x+nop->hi/2+1,t);
        }
        ss = (t-nop->y-yr0*0.5*(nop->py+1))*nop->my + nop->y00;
        sprintf(str,nop->formy,ss);
        for (i=0; i<9; i++)
            if (str[i] != ' ')
                break;
        g_str_(pCanvas,x+0.5*hs,t+0.5,str+i);
    }
    t -= nop->sty;
    g_str_(pCanvas,x+1+hi , t+10, nop->ny);
    repy -= nop->stx*0.75;
    //g_an(nop->an);
}
//------------------------------------
void g_vir_(TMetafileCanvas *pCanvas, double t, double y, char *str)
{
    char *st0;
    int i,l;
    st0=strchr(str,(int)'.');
    if (st0 != NULL) {
        l=strlen(str);
        for (i=l-1; i>0; i--)
            if (str[i]!='0') break;
        str[i+1]='\0';
    }
    for (i=0; i<9; i++)
        if (str[i] != ' ')
            break;
    g_str_(pCanvas,t,y,str+i);
}
//--------------------------------------------------------

void __fastcall TOutGraphicForm::NExportClick(TObject *Sender)
{
    if (GrapMeta!=NULL) {
      SaveDialog_EMF->FileName=Edit_name->Text;
      if(SaveDialog_EMF->Execute())
      {
         GrapMeta->SaveToFile(SaveDialog_EMF->FileName);
      }
    }
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void __fastcall TOutGraphicForm::NExitClick(TObject *Sender)
{
     Close();
}
//---------------------------------------------------------------------------












