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

#include "UnitGrap.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

extern 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";


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 int npag;
extern double repy;  /* положение оси Y по X */
extern int flag_funk;
extern int flag_format;
extern int flag_f;
extern int flag_pg;
extern int flag_e;
extern int flag_q;
int flag_log;
extern int type_line;
extern double  bx;
extern double  by;
extern double  an;
extern double  it;
extern double  numf;
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);
int SaveGrapFile();
char *cherta(AnsiString Stroka);
//---------------------------------------------------------------------------
__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;
       marker[0]=func[i].mark[0];
       mark_size=func[i].mark_size;
       mark_step=func[i].mark_step;
       mark_shift=func[i].mark_shift;
       if (func[i].type_func) {
           rc=g_fun_out(pCanvas, nop);
       }
   }
}
/*--------------------------------------------------------*/
/*   вывод   функции                                      */
/*--------------------------------------------------------*//* g_fun_  */

//---------------------------------------------------------------------------
/*--------------------------------------------------------*/
/*   Установка шрифта                                     */
/*--------------------------------------------------------*/
void pFont(TMetafileCanvas *pCanvas, NOP* nop)
{
   int h; 
   TheFont = new TFont();
   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::NSaveClick(TObject *Sender)
{
  //
  int rc;
  if (GrapFileName=="Untitled") {
      NSaveAsClick(Sender);
      return;
  }
  rc=SaveGrapFile();
  if (rc!=0) {
    g_outop("Ошибка при записи файла данных график");
  }
}
//---------------------------------------------------------------------------


void __fastcall TOutGraphicForm::NSaveAsClick(TObject *Sender)
{
      int rc;
      SaveDialog_Grap->FileName=Edit_name->Text;
      if(SaveDialog_Grap->Execute())
      {
         Caption="Построение графика "+ SaveDialog_Grap->FileName;
         GrapFileName=SaveDialog_Grap->FileName;
         rc=SaveGrapFile();
         if (rc!=0) {
              g_outop("Ошибка при записи файла данных график");
         }
      }
}
char *cherta(AnsiString Stroka) {
     char * str;
     int i;
     str=Stroka.c_str();

     for (i=0; i<Stroka.Length(); i++)
       if (str[i]==' ')
         str[i]='_';
     return str;
}


//---------------------------------------------------------------------------

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










