////////////////////////////////////////////////////////////////////////////////
//
// sdlg.cpp -  
//
////////////////////////////////////////////////////////////////////////////////
#ifndef VCL_H
#include <vcl.h>
#endif

#ifndef __WINDOWS_
#include <windows.h>
#endif

#ifndef __STDIO_H
#include <stdio.h>
#endif

#ifndef  __MATH_H
#include <math.h>
#endif

#pragma hdrstop

#ifndef __STUDS3D_RH
#include "studs3D.rh"
#endif

#ifndef SDlgH
#include "SDlg.h"
#endif

#pragma package(smart_init)
#pragma resource "*.dfm"
TShpeelDlg *ShpeelDlg;

#define MAX_LENGTH 1000        //   
#define MIN_LENGTH 0           //   

extern HINSTANCE _hinst;


//-------------------------------------------------------------------------------
//    
// ---
LPTSTR _LoadStr( int id );


//-------------------------------------------------------------------------------
//   
// ---
int	Shpeel::MacroElementParam() 
{
	int res = 0;
	Shpeel* bufS = Dublicate();
	if ( bufS && bufS->OpenBase() ) 
  {
    TShpeelDlg * dlg  = new TShpeelDlg( Application, *bufS );
		if ( dlg ) 
    {
			EnableTaskAccess( 0 ); //    
 			res = dlg->ShowModal();	//  
			EnableTaskAccess( 1 ); //    
			delete dlg;
		}
		if ( res == mrOk ) 
			Assign( *bufS );
		bufS->CloseBase();
		delete bufS;
	}
	return res;
}


//------------------------------------------------------------------------------
//  
// ---
static UINT ChoiceBMP( short gost, short f ) 
{
	UINT bmp = 0;
  if ( gost > 22041 ) 
		bmp = f & ALLST ? SH22042_3 : f & ISPOLN ? SH22042_2 : SH22042_1;
  else 
		bmp = f & ISPOLN ? SH22032_2 : SH22032_1;
	return bmp;
}


//------------------------------------------------------------------------------
//      
// ---
int GetTypeForGost( short gost ) 
{
	int n;
  switch ( gost ) 
  {
    case 22032 :
    case 22033 :  n = 0;  break; // 1d
    case 22034 :
    case 22035 :  n = 1;  break; // 1,25d
    case 22036 :
    case 22037 :  n = 2;  break; // 1,6d
    case 22038 :
    case 22039 :  n = 3;  break; // 2d
    case 22040 :
    case 22041 :  n = 4;  break; // 2,5d
    case 22042 :
    case 22043 :  n = 5;  break; // l0
  }
  return n;
}


//------------------------------------------------------------------------------
//     
// ---
int GetKlassForGost( short gost ) 
{
	int n;
  switch ( gost ) 
  {
    case 22032 :
    case 22034 :
    case 22036 :
    case 22038 :
    case 22040 :
    case 22042 : n = 1;  break; // B 
    case 22033 :
    case 22035 :
    case 22037 :
    case 22039 :
    case 22041 :
    case 22043 : n = 0;  break; // A
  }
  return n;
}


//------------------------------------------------------------------------------
//     
// ---
int GetGostForTypeAndKlass( int type, int klass ) 
{
	int n;
  switch ( type ) 
  {
    case 0 : n = klass ? 0  : 1;  break; // 22032 : 22033
    case 1 : n = klass ? 2  : 3;  break; // 22034 : 22035
    case 2 : n = klass ? 4  : 5;  break; // 22036 : 22037
    case 3 : n = klass ? 6  : 7;  break; // 22038 : 22039
    case 4 : n = klass ? 8  : 9;  break; // 22040 : 22041
    case 5 : n = klass ? 10 : 11; break; // 22042 : 22043
  }
  return n;
}


//------------------------------------------------------------------------------
//
// ---
static int NumberStr( int gost ) 
{
	int n;
  switch ( gost ) 
  {
    case 22032 :	n = STR59;	break;
    case 22033 :  n = STR60;  break;
    case 22034 :  n = STR61;  break;
    case 22035 :  n = STR62;  break;
    case 22036 :  n = STR63;  break;
    case 22037 :  n = STR64;  break;
    case 22038 :  n = STR65;  break;
    case 22039 :  n = STR66;  break;
    case 22040 :  n = STR67;  break;
    case 22041 :  n = STR68;  break;
    case 22042 :  n = STR69;  break;
    case 22043 :  n = STR70;  break;
  }
  return n;
}

//-------------------------------------------------------------------------------
//  
// ---
__fastcall TShpeelDlg::TShpeelDlg(TComponent* Owner, Shpeel & _s )
        : TForm(Owner), S( _s)
{
  bitMapId = 0;

  StringGrid1->ColCount = 5;
  int colW = StringGrid1->Width/StringGrid1->ColCount;
  StringGrid1->ColWidths[0] = colW-1;
  StringGrid1->ColWidths[1] = colW-1;
  StringGrid1->ColWidths[2] = colW-1;
  StringGrid1->ColWidths[3] = colW-1;
  StringGrid1->ColWidths[4] = colW-1;

  char buf[128];
  LoadString( _hinst, STR247, buf, 128 ); //  
  StringGrid1->Cells[0][0] = buf;
  LoadString( _hinst, STR248, buf, 128 ); //  
  StringGrid1->Cells[1][0] = buf;
  LoadString( _hinst, STR231, buf, 128 ); //  
  StringGrid1->Cells[2][0] = buf;         
  LoadString( _hinst, STR236, buf, 128 ); // 
  StringGrid1->Cells[3][0] = buf;
  LoadString( _hinst, STR237, buf, 128 ); //  1000 
  StringGrid1->Cells[4][0] = buf;
}


//-------------------------------------------------------------------------------
//
// ---
__fastcall TShpeelDlg::~TShpeelDlg() 
{
  S.CloseBase();
}


//-------------------------------------------------------------------------------
//  
// ---
void __fastcall TShpeelDlg::helpButtonClick(TObject *Sender)
{
  Message( _LoadStr( STR_NOREALIZE ) );
}


//---------------------------------------------------------------------------
//  
// ---
void __fastcall TShpeelDlg::FormShow(TObject *Sender)
{
  char buf[128];

  //   
  static int idGOST[] = { STR59, STR60, STR61, STR62, STR63, STR64, STR65, STR66, STR67, STR68, STR69, STR70 };
  ComboBox5->Clear();
  for ( UINT i = 0; i < 12; i++ ) 
  {
    LoadString( _hinst, idGOST[i], buf, 128 );
    ComboBox5->Items->Add( buf );
  }
  LoadString( _hinst, ::NumberStr(S.GetShpeelParam().gost), buf, 128 );
  int ind = ComboBox5->Items->IndexOf( buf );
  ComboBox5->ItemIndex = ind; //    

  //   "  "
  for ( UINT i = 0; i < 6; i++ ) 
  {
    LoadString( _hinst, STR_STUD_1D + i, buf, 128 );
    ComboBox3->Items->Add( buf );
  }
  ComboBox3->ItemIndex = ::GetTypeForGost(S.GetShpeelParam().gost);  //    

  static int idMat[] = { STR238, STR239, STR240, STR246 };
  for ( UINT i = 0; i < 4; i++ ) 
  {
    LoadString( _hinst, idMat[i], buf, 128 );
    ComboBox1->Items->Add( buf );
  }
  ComboBox1->ItemIndex = S.GetShpeelParam().indexMassa;

  //   " "
  LoadString( _hinst, STR_STUD_A, buf, 128 );  // "A"
  ComboBox4->Items->Add( buf );
  LoadString( _hinst, STR_STUD_B, buf, 128 );  // "B"
  ComboBox4->Items->Add( buf );
  ComboBox4->ItemIndex = ::GetKlassForGost(S.GetShpeelParam().gost); //   

  //   ""
  CheckBox2->Checked = S.GetShpeelParam().f & SIMPLE;

  InitControls();
  cboxSpcObj->Checked = S.GetFlagAttr();

  ListParamShow(); 
}


//---------------------------------------------------------------------------
//  
// ---
void __fastcall TShpeelDlg::PaintBox1Click(TObject *Sender)
{
  DrawSlide( (void *)Panel1->Handle, bitMapId );
}


//------------------------------------------------------------------------------
//   
// ---
void TShpeelDlg::FillDiametr()
{
  struct {
    short t;
    char s[128];
  } tmpL;

  ComboBox->Clear();                //  

  float lMin         = MAX_LENGTH;  //    
  float lMax         = MIN_LENGTH;  //    
  bool  enterInRange = false;       //        
  bool  failDB       = true;        //  

  if ( !::Condition(S.GetShpeelBase().bg, S.GetShpeelBase().rg1, "t = 1") )
  {
    Error( _LoadStr( STR_KONSTR_ERROR ) ); //   
    return;
  }

  int i = 1;
  while( i ) //     
  {
    i = ::ReadRecord( S.GetShpeelBase().bg, S.GetShpeelBase().rg1, &tmpL );//  
    if ( i ) //   
    {
      ComboBox->Items->Add( tmpL.s ); //   

      char *c;
      float ln = ::strtod( tmpL.s, &c ); //    
      if ( S.GetShpeelParam().d == ln )
        enterInRange = true;             //       
      if ( ln < lMin )
        lMin = ln;                       //   
      if ( ln > lMax )
        lMax = ln;                       //   

      failDB = false;                    //  
    }
  }
  if ( !enterInRange )                   //          
    S.GetShpeelParam().d = lMin;         //    
  if ( S.GetShpeelParam().d < lMin )     //        
    S.GetShpeelParam().d = lMin;         //    
  if ( S.GetShpeelParam().d > lMax )     //        
    S.GetShpeelParam().d = lMax;         //    

  ::sprintf( tmpL.s, "%g", S.GetShpeelParam().d ); //      
  int ind = ComboBox->Items->IndexOf( tmpL.s ); //   
  ComboBox->ItemIndex = ind;

  if ( failDB ) //       
    Error( _LoadStr( STR_DIAM_ERROR ) ); //   
}


//------------------------------------------------------------------------------
//   
// ---
void TShpeelDlg::FillLenght()
{
  struct {
    short t;
    char s[128];
    float dr;
  } tmpL;

  //     
  ::sprintf( tmpL.s, "t = 1 and d=%.1f", S.GetShpeelParam().d );
  if ( !::Condition(S.GetShpeelBase().bs, S.GetShpeelBase().rs1, tmpL.s) )
  {
    Error( _LoadStr( STR_KONSTR_ERROR ) ); //   
    return;
  }

  ComboBox2->Clear(); //  

  float lMin         = MAX_LENGTH; //    
  float lMax         = MIN_LENGTH; //    
  bool  enterInRange = false;      //        
  bool  failDB       = true;       //  

  int i = 1;
  while( i ) 
  {                                //     
    i = ::ReadRecord( S.GetShpeelBase().bs, S.GetShpeelBase().rs1, &tmpL ); //  
    if ( i ) //    
    {                                     
      ComboBox2->Items->Add( tmpL.s ); //   

      char *c;
      float ln = ::strtod( tmpL.s, &c ); //    
      if ( S.GetShpeelParam().l == ln )
        enterInRange = true; //       
      if ( ln < lMin )
        lMin = ln; //   
      if ( ln > lMax )
        lMax = ln; //   

      failDB = false; //  
    }
  }
  if ( !enterInRange )                      //          
    S.GetShpeelParam().l = lMin;     //    
  if ( S.GetShpeelParam().l < lMin ) //        
    S.GetShpeelParam().l = lMin;     //    
  if ( S.GetShpeelParam().l > lMax ) //        
    S.GetShpeelParam().l = lMax;     //    

  ::sprintf( tmpL.s, "%g", S.GetShpeelParam().l ); //      
	int ind = ComboBox2->Items->IndexOf( tmpL.s ); //    
  ComboBox2->ItemIndex = ind;

  if ( failDB )                     //       
    Error( _LoadStr( STR_LEN_ERROR ) ); //   
}


//------------------------------------------------------------------------------
//  
// ---
void TShpeelDlg::InitControls() 
{
  if ( S.GetShpeelParam().gost > 22041 ) 
  {
    if ( ::fabs(S.GetShpeelParam().b) > 0.001 )
      S.GetShpeelParam().f &=~ ALLST;
    else
      S.GetShpeelParam().f |= ALLST;
  }
  else
    S.GetShpeelParam().f &=~ ALLST;

	CheckBox1->Checked = S.GetShpeelParam().f & PITCH;
	RadioGroup1->ItemIndex = S.GetShpeelParam().f & ISPOLN;

  FillDiametr(); //   
  FillLenght();  //   

  RadioGroup1->Controls[1]->Enabled = S.GetShpeelParam().f & ALLST ? false : true;
  if ( S.GetShpeelParam().f & ALLST )
		RadioGroup1->ItemIndex = 0;

  CalcMassa();
  ChoicePitch();
  bitMapId = ChoiceBMP( S.GetShpeelParam().gost, S.GetShpeelParam().f );
  PaintBox1->Repaint(); //  
}


//------------------------------------------------------------------------------
//     
// ---
void __fastcall TShpeelDlg::ComboBox5Change(TObject *Sender)
{
  AnsiString Str;

    Str = ComboBox5->Text; //  

	int index = Str.Pos( "-" );
	if ( index > 0 ) //    
		Str.SetLength( index-1 );
	short gost = (short)::atoi( Str.c_str() ); //   
	if ( gost != S.GetShpeelParam().gost ) 
  {
		S.GetShpeelParam().gost = gost;

	  ComboBox3->ItemIndex = ::GetTypeForGost(S.GetShpeelParam().gost);  //     l1
	  ComboBox4->ItemIndex = ::GetKlassForGost(S.GetShpeelParam().gost); //   

    if ( S.IsBaseOpen() ) //   
      S.CloseBase();      //   

    //       
    if ( !S.OpenBase() )
      { ModalResult = mrCancel; return; }
    if ( !S.ReadShpeelBase(S.GetShpeelParam().d) )
      { ModalResult = mrCancel; return; }
    if ( S.ReadShpeelStBase() == -1 )              
      { ModalResult = mrCancel; return; }

    InitControls();  //  
    ListParamShow(); //   
  }
}


//------------------------------------------------------------------------------
//    " "
// ---
void __fastcall TShpeelDlg::ComboBox4Change(TObject *Sender)
{
  int k = ComboBox4->ItemIndex;
  int k1 = ComboBox3->ItemIndex;
  if ( (k > -1) && (k1 > -1) ) 
  {
    ComboBox5->ItemIndex = ::GetGostForTypeAndKlass(k1, k);
    ComboBox5Change(Sender);
  }
}


//------------------------------------------------------------------------------
//   ""
// ---
void __fastcall TShpeelDlg::CheckBox2Click(TObject *Sender)
{
  if (CheckBox2->Checked)
    S.GetShpeelParam().f |= SIMPLE;
  else
    S.GetShpeelParam().f &= ~SIMPLE;
}


//---------------------------------------------------------------------------
//    "  "
// ---
void __fastcall TShpeelDlg::ComboBox3Change(TObject *Sender)
{
  int k = ComboBox3->ItemIndex;
  int k1 = ComboBox4->ItemIndex;
  if ( (k > -1) && (k1 > -1) ) 
  {
    ComboBox5->ItemIndex = ::GetGostForTypeAndKlass(k, k1);
    ComboBox5Change(Sender);
  }
}


//------------------------------------------------------------------------------
//  
// ---
void TShpeelDlg::ViewParamShow() 
{
  CalcMassa();
  ChoicePitch();
  ListParamShow();
}


//------------------------------------------------------------------------------
//     -         
// ---
void __fastcall TShpeelDlg::ComboBoxChange(TObject *Sender)
{
  AnsiString Str;

  Str = ComboBox->Text;
  char *c;
  float dr = (float)::strtod( Str.c_str(), &c ); //   
  if ( ::fabs(dr - S.GetShpeelParam().d) > 0.001 ) 
  {
    if ( !S.ReadShpeelBase(dr) ) 
      { ModalResult = mrCancel; return ; } //  

    FillLenght(); //   

    if ( S.ReadShpeelStBase() == -1 ) 
      {  ModalResult = mrCancel; return; } //  

                RadioGroup1->Controls[1]->Enabled = S.GetShpeelParam().f & ALLST ? false : true;
                if ( S.GetShpeelParam().f & ALLST )
	          RadioGroup1->ItemIndex = 0;

    bitMapId = ::ChoiceBMP( S.GetShpeelParam().gost, S.GetShpeelParam().f ); //  
    ViewParamShow();      //   
    PaintBox1->Repaint(); //  
  }
}


//---------------------------------------------------------------------------
//     -     
// ---
void __fastcall TShpeelDlg::ComboBox2Change(TObject *Sender)
{
  AnsiString Str;

  Str = ComboBox2->Text;  // C 
  char *c;
  float l = (float)::strtod( Str.c_str(), &c ); //   
  if ( ::fabs(l - S.GetShpeelParam().l) > 0.001 ) 
  {
    S.GetShpeelParam().l = l; //    k    
    if ( S.ReadShpeelStBase() == -1 ) 
      { ModalResult = mrCancel; return; } //  

		RadioGroup1->Controls[1]->Enabled = S.GetShpeelParam().f & ALLST ? false : true;
    if ( S.GetShpeelParam().f & ALLST )
	    RadioGroup1->ItemIndex = 0;

    bitMapId = ::ChoiceBMP( S.GetShpeelParam().gost, S.GetShpeelParam().f ); //  
    ViewParamShow();      //   
    PaintBox1->Repaint(); //  
  }   
}


//---------------------------------------------------------------------------
// 
// ---
void __fastcall TShpeelDlg::CheckBox1Click(TObject *Sender)
{
  if (CheckBox1->Checked)
    S.GetShpeelParam().f |= PITCH;
  else
    S.GetShpeelParam().f &= ~PITCH;
  ViewParamShow();
}


//---------------------------------------------------------------------------
// 
// ---
void __fastcall TShpeelDlg::ComboBox1Change(TObject *Sender)
{
  int k = ComboBox1->ItemIndex;
  if ( k > -1 ) 
  {
    S.GetShpeelParam().indexMassa =(byte) k;
    ViewParamShow();
  }
}


//---------------------------------------------------------------------------
//   
// ---
void __fastcall TShpeelDlg::cboxSpcObjClick(TObject *Sender)
{
	S.GetFlagAttr() = cboxSpcObj->Checked == 1 ? true : false;
}


//---------------------------------------------------------------------------
//    
// ---
void TShpeelDlg::ListParamShow()
{
  char buf[128];
  sprintf(buf,"%.0f",S.GetShpeelParam().b);  //  
  StringGrid1->Cells[0][1] = buf;
  sprintf(buf,"%.0f",S.GetShpeelParam().b1); //  
  StringGrid1->Cells[1][1] = buf;
  sprintf(buf,"%.1f",p);                     //  
  StringGrid1->Cells[2][1] = buf;
  sprintf(buf,"%.1f",S.GetShpeelParam().c);  // 
  StringGrid1->Cells[3][1] = buf;
  sprintf(buf,"%.1f",massa);                 //  1000 
  StringGrid1->Cells[4][1] = buf;
}


//---------------------------------------------------------------------------
// 
// ---
void __fastcall TShpeelDlg::RadioGroup1Click(TObject *Sender)
{
  if ( RadioGroup1->ItemIndex ==0 )
		S.GetShpeelParam().f &=~ ISPOLN;
	else
		S.GetShpeelParam().f |= ISPOLN;

  bitMapId = ::ChoiceBMP( S.GetShpeelParam().gost, S.GetShpeelParam().f ); //  
  ViewParamShow();      //   
  PaintBox1->Repaint(); //  
} 
