//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <string.h>
//#include "rrk.h"
//#include "gout.h"
#include "UnitGrap.h"

#pragma hdrstop

#include "UnitGraphic.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormGraphic *FormGraphic;
extern double mark_step;
extern double mark_size;
extern double mark_shift;
extern char    marker[5] ;

extern int npag;
extern double  bx ;
extern double  by ;

extern char    style[10] ;
extern char    font[40] ;
extern char    stmap[10] ;
extern int     flag_e;

static int flag_f  = 0;
static int flag_pg = 0;
static int type_line = 0;
extern double  hi;
static double  an = 0;
static double  it = 0;

extern FILE  * grout;
extern FILE  * output_g;
extern Func func[20];
extern int n_func;
extern int n_text;

static int     i;
extern int     H0;
static char str_format[120];
extern double Scale_grap;

//---------------------------------------------------------------------------
__fastcall TFormGraphic::TFormGraphic(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormGraphic::BitBtn_exitClick(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TFormGraphic::ImageMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
      if (Button == mbRight) Close();
}
//---------------------------------------------------------------------------
/*--------------------------------------------------------*/
/*   высота                                               */
/*--------------------------------------------------------*/
void g_h_(double h)
{
       int i;
       hi=h;
}
/*--------------------------------------------------------*/
/*   курсив                                               */
/*--------------------------------------------------------*/
void g_it_(double h)
{
      //------------
}
/*--------------------------------------------------------*/
/*   аргумент                                             */
/*--------------------------------------------------------
void g_x_(NOP *nop)
{
    int i;
    sprintf(str_format,"%10.4f",nop->x0);
    FormGrap->Edit_t_null->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.4f",nop->mx);
    FormGrap->Edit_t_scale->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.4f",nop->pf);
    FormGrap->Edit_t_pos->Text=(AnsiString)str_format;
    for (i=0; i<strlen(nop->gx); i++)
       if (nop->gx[i]=='_')
          nop->gx[i]=' ';
    FormGrap->Edit_t_name->Text=(AnsiString)nop->gx;
    FormGrap->Edit_t_format->Text=(AnsiString)nop->formx;
}
*/
/*--------------------------------------------------------*/
/*   оформление оси y                                     */
/*--------------------------------------------------------
void g_y_(nop)
NOP *nop;
{
    sprintf(str_format,"%10.3f",nop->y00);
    FormGrap->Edit_Y_null->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->my);
    FormGrap->Edit_Y_scale->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->py);
    FormGrap->Edit_Y_pos->Text=(AnsiString)str_format;
    for (i=0; i<strlen(nop->ny); i++)
       if (nop->ny[i]=='_')
          nop->ny[i]=' ';
    FormGrap->Edit_Y_name->Text=(AnsiString)nop->ny;
    FormGrap->Edit_Y_format->Text=(AnsiString)nop->formy;
}
*/
/*--------------------------------------------------------*/
/*   оформление поля графика                              */
/*--------------------------------------------------------
void g_gr_(nop)
NOP *nop;
{
    sprintf(str_format,"%10.3f",nop->x);
    FormGrap->Edit_x0->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->y);
    FormGrap->Edit_y0->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->xr);
    FormGrap->Edit_size_X->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->yr);
    FormGrap->Edit_size_Y->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->stx);
    FormGrap->Edit_step_X->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->sty);
    FormGrap->Edit_step_Y->Text=(AnsiString)str_format;
}
*/
/*--------------------------------------------------------*/
/*   оформление поля рисования                            */
/*--------------------------------------------------------
void g_pg_(nop)
NOP *nop;
{
    sprintf(str_format,"%10.3f",nop->xo);
    FormGrap->Edit_pict_x->Text=(AnsiString)str_format;
    sprintf(str_format,"%10.3f",nop->yo);
    FormGrap->Edit_pict_y->Text=(AnsiString)str_format;
}
*/
/*--------------------------------------------------------*/
/*   описание функции                                     */
/*--------------------------------------------------------
int g_fun_(NOP* nop,bool bin_func)
{
    func[n_func].mf=nop->mf;
    func[n_func].type_func=bin_func;
    strcpy(func[n_func].nf,nop->nf);
    strcpy(func[n_func].gf,nop->gf);
    strcpy(func[n_func].ei,nop->ei);
    strcpy(func[n_func].fi,nop->fi);
    strcpy(func[n_func].mark,nop->mark);
    func[n_func].mark_size=mark_size;
    func[n_func].mark_step=mark_step;
    func[n_func].mark_shift=mark_shift;
    FormGrap->CheckListBox_func->Items->Add((AnsiString)nop->nf+
        "("+(AnsiString)nop->gf+") - "+(AnsiString)nop->fi);
    FormGrap->CheckListBox_func->Checked[n_func]=false;
    n_func++;
    return 0;
}
*/
void g_ot_(TMetafileCanvas *pCanvas, double x, double y) {
    bx=x;
    by=y;
}
void g_bot_(TMetafileCanvas *pCanvas, double x, double y) {
    pCanvas->MoveTo((int)(bx*Scale_grap), H0-(int)(by*Scale_grap) );
    pCanvas->LineTo((int)(x*Scale_grap), H0-(int)(y*Scale_grap) );
    bx=x;
    by=y;
}
/*--------------------------------------------------------*/
/*   вывод   строки                                       */
/*--------------------------------------------------------*/
void g_str_(TMetafileCanvas *pCanvas,double x, double y, char *str)
{
      pCanvas->TextOut((int)(x*Scale_grap),H0-(int)(y*Scale_grap),
                       (AnsiString)str);
}
/*--------------------------------------------------------*/
/*   Оприходовать текст                                   */
/*--------------------------------------------------------
void g_text(double x, double y, char * text)
{
      int i,j,k;
      j=x; k=y;
      for (i=0; i<strlen(text); i++)
         if (text[i]=='-') text[i]=' ';
      FormText->Memo_Text->Lines->Add((AnsiString)text);
      FormText->Memo_X->Lines->Add(IntToStr(j));
      FormText->Memo_Y->Lines->Add(IntToStr(k));
      n_text++;
}
*/
/*--------------------------------------------------------*/
/*   вывод   маркера                                      */
/*--------------------------------------------------------*/
void g_mout_(TMetafileCanvas *pCanvas, double x, double y)
{
    if (marker[0] != '0')
    {
      switch (marker[0]) {
      case 'A': g_cyrc_(pCanvas,0.5*mark_size);
      break;
      case 'B': g_squar_(pCanvas);
      break;
      case 'C': g_trian_(pCanvas,1.0);
      break;
      case 'D': g_smul_(pCanvas);
      break;
      case 'E': g_ssum_(pCanvas);
      break;
      case 'F':
           g_squar_(pCanvas);g_smul_(pCanvas);
      break;
      case 'G':
           g_squar_(pCanvas);g_ssum_(pCanvas);
      break;
      case 'H':
           g_squar_(pCanvas);g_cyrc_(pCanvas,0.5*mark_size);
      break;
      case 'I':
           g_cyrc_(pCanvas,0.5*mark_size);g_ssum_(pCanvas);
      break;
      case 'J':
           g_cyrc_(pCanvas,0.5*mark_size);g_smul_(pCanvas);
      break;
      case 'K':
           g_cyrc_(pCanvas,0.5*mark_size);g_trian_(pCanvas,1.0);
      break;
      default:
           g_trian_(pCanvas,1.0); g_trian_(pCanvas,-1.0);
      }
    }

}  /* g_mout */
/*--------------------------------------------------------*/
/*   Окружность                                           */
/*--------------------------------------------------------*/
void g_cyrc_(TMetafileCanvas *pCanvas,double rad)
{
     pCanvas->Brush->Style=bsClear;
     pCanvas->Ellipse((int)((bx-rad)*Scale_grap), H0-(int)((by-rad)*Scale_grap),
                    (int)((bx+rad)*Scale_grap), H0-(int)((by+rad)*Scale_grap));
     pCanvas->Brush->Style=bsSolid;
}
/*--------------------------------------------------------*/
/*   прямоугольник                                        */
/*--------------------------------------------------------*/
void g_squar_(TMetafileCanvas *pCanvas)
{
     double u,v,ut,vt;

     u=bx;
     v=by;

     bx -= mark_size*0.5;
     ut = bx;
     by -= mark_size*0.5;
     vt = by;
     g_bot_(pCanvas,ut,vt+mark_size);
     g_bot_(pCanvas,ut+mark_size,vt+mark_size);
     g_bot_(pCanvas,ut+mark_size,vt);
     g_bot_(pCanvas,ut,vt);
     bx=u; by=v;
}
/*--------------------------------------------------------*/
/*   треугольник                                          */
/*--------------------------------------------------------*/
void g_trian_(TMetafileCanvas *pCanvas,double ss)
{
     double u,v,ut,vt,h;

     h = mark_size*ss;
     u = bx;
     v = by;
     bx -= h*0.5*0.866;
     ut = bx;
     by -= h*0.25;
     vt = by;
     g_bot_(pCanvas,u,v+h*0.5);
     g_bot_(pCanvas,ut+h*0.866,vt);
     g_bot_(pCanvas,ut,vt);
     bx=u; by=v;
}
/*--------------------------------------------------------*/
/*   крест - умножение                                    */
/*--------------------------------------------------------*/
void g_smul_(TMetafileCanvas *pCanvas)
{
     double u,v,g;
     u=bx;
     v=by;
     g= mark_size*0.5;
     bx -= g;
     by -= g;
     g_bot_(pCanvas,u+g,v+g);
     bx = u + g;
     by = v - g;
     g_bot_(pCanvas,u-g,v+g);
     bx=u;by=v;
}
/*--------------------------------------------------------*/
/*   крест - cложение                                     */
/*--------------------------------------------------------*/
void g_ssum_(TMetafileCanvas *pCanvas)
{
     double u,v,g;
     u=bx;
     v=by;
     g= mark_size*0.5;
     by -= g;
     g_bot_(pCanvas,u,v+g);
     bx = u - g;
     by = v;
     g_bot_(pCanvas,u+g,v);
     bx=u;by=v;
}


