//////////////////////////////////////////////////////////////////////////////// 
// 
// step4.cpp -  
// 
// 1.                              - DrawTxtDB
// 2.  Cursor  Placement           - DrawRectCallBack, DrawRectNULL - <step4_1.cpp> 
// 3.   
// 4.                        - SelectFileName
// 5.                          - WriteSlideStep
// 6.                        - TestShowDialog                 - <slidefrm.cpp> 
// 7.      - TaskAccess
// 8.                 - WorkRelativePath
// 9.            - WorkSystemPath
// 
//////////////////////////////////////////////////////////////////////////////// 
#include <system.hpp>

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

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

#ifndef __LIBTOOL_H
#include <libtool.h>
#endif

#ifndef __LIBDB_H
#include "libdb.h"
#endif

#ifndef __STEP4_RH
#include "step4.rh"
#endif


//-------------------------------------------------------------------------------
//   
// ---
extern "C" unsigned int far __export __pascal LibraryId()
{
  return IDR_LIBID;
}


//-------------------------------------------------------------------------------
//   
// ---
void DrawTxtDB();
void DrawRectCallBack();
void SelectFileName();
void DrawRectNULL();
void WriteSlideStep();
void TaskAccess();
void WorkRelativePath();
void WorkSystemPath();
void TestShowDialog();


extern "C" void far __export __pascal  LibraryEntry( UINT comm )
{
  switch ( comm )
  {
    //   
    case 1 :
      DrawTxtDB();
      break;
 
    //   
    case 3 :
    {
      long index;
      //   
      if ( ReadLong( " ", //  
                     10000,           //    
                     -100000l,        //   
                     100000l,         
                     &index ) )       //  
      {
        char buf[255];
        sprintf( buf, " = %ld", index );
        Message( buf );
      }
      else
        Message( "" );
      break;
    }
 
    //   
    case 4 :
      SelectFileName();
      break;
 
    //  (    <step4_2.cpp> )
    case 7 :
      TestShowDialog();
      break;

    //     
    case 9 :
      WorkRelativePath();
      break;

    //    
    case 10 :
      WorkSystemPath();
      break;
      
    default:  
    {
      if ( ksGetCurrentDocument( 1 ) ) //   2D 
      {
        switch ( comm )
        {
          //  Cursor  Placement(    <step4_1.cpp> )
          case 2 :
          {
            //   ,    ( 1 - , 0 - , -1 -   )
            int res = YesNo( "  CallBack?" );     
            if ( res == 1 )
              DrawRectCallBack();
            else
              if ( res == 0 )
                DrawRectNULL();
              else
                Message( "" );
            break;
          }
          
          //  
          case 6 :
            WriteSlideStep();
            break;   
            
          //   
          case 8 :
            TaskAccess();
            break;
        }
      }
      else
        Message( "   \n  /" );        
    }
  }
}


//--------------------------------------------------------------------------------
//     
//       , 
//     
// ---
void DrawTxtDB()
{
  char fileName[255];
  //   ,     ,  0                             
  if ( ksChoiceFile( "*.loa",  //    
                     //   (   ,    )
                     " ( *.loa )|*.loa|  ( *.* )|*.*|",
                     fileName, //  
                     255,      //   
                     0 ) )     //    ( - 1 -    
                               //  , 0 -   
                               //   )
  { 
    // C ,    ( ODBC_DB -  ODBC, TXT_DB -   )
    reference rDataBase = CreateDB( "TXT_DB" );
 
    // C      ,    -  
    if ( ConnectDB( rDataBase, fileName ) )
    {
      //          record

      // C  -      
      //      double   int,      
      //  ,     >=   Relation
      reference rRelation = Relation( rDataBase );
        RDouble( "A" ); //    , 
        RDouble( "B" ); //       
        RInt( "" );
      EndRelation();

      //   -    (   
      //      )
      DoStatement( rDataBase, rRelation, "1 2 3" ); //  A - 1, B - 2,   - 3

      /*
      //      
      Condition( rDataBase, rRelation, "Index1000 = 2" ); //   2
      */

      /*
      //      "A = 100 || A = 120"
      //   (        A  100  120
      Condition( rDataBase, rRelation, "A = 100 || A = 120" );
      */

      char buf[255];
      
      //      
      struct {
        double a, b;
        short int c;
      } record;

      //    
      int repeat = 1;
      while ( repeat )
      {
        //         record
        repeat = ReadRecord( rDataBase, rRelation, &record );
        if ( repeat )
        {
          sprintf( buf, "a = %4.1f, b = %4.1f, c = %i", record.a, record.b, record.c );
          Message( buf );
        }
      }
      Message( " " );

      //      
      //          
      //        
      rRelation = Relation( rDataBase );
        RChar( "L", 20, 0 ); //   20 
      EndRelation();

      // (     
      //  ,      -"" )
      DoStatement( rDataBase, rRelation, "4" ); //     4  
 
      //    
      repeat = 1;
      while ( repeat )
      {
        //        buf
        repeat = ReadRecord( rDataBase, rRelation, buf );
        if ( repeat )
          Message( buf );
      }
      Message( " " );
    }
    //  ,   
    //    ,    
    DeleteDB( rDataBase );
  }
}


//-------------------------------------------------------------------------------
//    
// ---
void SelectFileName()
{
  char choice[10]; //  10 
  //   ,     ,  0  
  int size = ChoiceFile( "*.cdw", //    
                         //   (   ,    ) 
                         "( *.cdw )|*.cdw|( *.frw )|*.frw|  ( *.* )|*.*|",
                         choice, //  
                         10 );   //   
  if ( size )
  {
    if ( size > 10 ) //    10 
    {
      //      
      char* newChoice = new char[ size+1 ];
      //    ,     
      size = GetRightFileName( newChoice, //    
                               size+1 );  //   

      char buf[255];
      sprintf( buf, " \n%s\n 10,   %i ", newChoice, size );
      Message( buf );

      delete[] newChoice;
    }
    else
      Message( choice );
  }
  else
    Message( "" );
}


//-------------------------------------------------------------------------------
//      
// ---
void  TaskAccess()
{
  EnableTaskAccess( 0 ); //    
  for ( int i = 0; i < 10000; i++ )
  {
    LineSeg( 10, 10 + i, 20, 10 + i, 1 );
    if ( !( i % 100 ) )
    {
      //     
      //      
      PostThreadMessage( GetCurrentThreadId(), 0, 0, 0 );
      //   100    
      //   Windows    
      // ,     
      PumpWaitingMessages();
    }
  }
  EnableTaskAccess( 1 ); //    
}  


//-------------------------------------------------------------------------------
//  
// ---
void WriteSlideStep()
{
  char name[255];

  //    
  if ( SaveFile( "*.rc", NULL, NULL, name, 255 ) )
  {
    //     
    RequestInfo info;
    memset( &info, 0, sizeof( info ) );
    
    //       
    info.commands = "   ";

    double x, y; //    -      
    
    //     
    if ( Cursor( &info,   //       	
                 &x,      //   	
                 &y, 
                 NULL ) ) //     ,  	 
    {
      int slideId;
      //   
      if ( ReadInt( "  ", 100, 0, 32000, &slideId ) )
      {
        //  
        if ( !WriteSlide( name,    //     
                          slideId, //     
                          x, y ) ) //  
          Error( "  " );
        ClearGroup( 0 ); //   ,  0,     
      }
    }
  }
}


//---------------------------------------------------------------------------------
//     
// ---
void WorkRelativePath()
{
  char mainName[255];
  //   
  if ( ChoiceFile( "*.*", "  ( *.* )|*.*|", mainName, 255 ) )
  {
    char fileName[255];
    //  
    if ( ChoiceFile( "*.*", "  ( *.* )|*.*|", fileName, 255 ) )
    {
      char resName[255];

      //  
      ksGetRelativePathFromFullPath( mainName,  //     
                                     fileName,  //     
                                     resName,   //  -     (        )
                                     255 );     //  
 
      String strMess( "    :" );
      strMess += mainName;
      strMess += "\n";
      strMess += "    :";
      strMess += fileName;
      strMess += "\n";
      strMess += "    :";
      strMess += resName;
      Message( strMess.c_str() );
 
      //  
      char fullName[255];
      ksGetFullPathFromRelativePath( mainName,  //     
                                     resName,   //     (        )
                                     fullName,  //  -     
                                     255 );     //  
 
      strMess = "    :";
      strMess += mainName;
      strMess += "\n";
      strMess += "    :";
      strMess += resName;
      strMess += "\n";
      strMess += "    :";
      strMess += fullName;
      Message( strMess.c_str() );
    }
  }
}


//---------------------------------------------------------------------------------
//    
// ---
void WorkSystemPath()
{
  //      
  char* catalogName[] = { "  ", 
                          " ", 
                          "  ", 
                          " ", 
                          "INI-" };
  
  //     
  RequestInfo info;
  memset( &info, 0, sizeof( info ) );
  
  info.title    = "  "; //      
  info.commands = "! ! ! ! !INI-"; //       
  info.prompt   = "  "; //    
 
  int comm = 1;
  static char *userFile = "user.ttt";
  char fileName[255];
  int typeCatalog;
  while ( comm > 0 )
  {
    comm = CommandWindow( &info ); //     
    if ( comm > 0 )
    {
      switch ( comm )
      {
        case 1 : typeCatalog = sptSYSTEM_FILES; break; //    
        case 2 : typeCatalog = sptLIBS_FILES;   break; //    
        case 3 : typeCatalog = sptTEMP_FILES;   break; //     
        case 4 : typeCatalog = sptCONFIG_FILES; break; //     
        case 5 : typeCatalog = sptINI_FILE;     break; //    INI- 
      }

      //  
      ksGetFullPathFromSystemPath( userFile,       //    (    )
                                   fileName,       //  -    
                                   255,            //  
                                   typeCatalog ); //    . ksSystemPath
      String strMess( "    " );
      strMess += userFile;
      strMess += "\n";
      strMess += catalogName[comm-1];
      strMess += "\n";
      strMess += fileName;
      Message( strMess.c_str() );
 
      char resName[255];
 
      //  
      ksGetRelativePathFromSystemPath( fileName,      //    
                                       resName,       //  -    (    )
                                       255,           //  
                                       typeCatalog ); //    . ksSystemPath
      strMess = "    ";
      strMess += userFile;
      strMess += "\n";
      strMess += catalogName[comm-1];
      strMess += "\n";
      strMess += resName;
      Message( strMess.c_str() );
    }
  }
}  