//////////////////////////////////////////////////////////////////////////////// 
// 
// step4.cpp -  
// 
// 1.                              - DrawTxtDB
// 2.  Cursor  Placement           - DrawRectCallBack, DrawRectNULL - <step4_1.cpp> 
// 3.   
// 4.                        - SelectFileName
// 5.                          - WriteSlideStep
// 6.                        - TestShowDialog                 - <step4_2.cpp> 
// 7.      - TaskAccess
// 8.                 - WorkRelativePath
// 9.            - WorkSystemPath
// 
//////////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h"
#include <afxdllx.h> 
#include "resource.h"

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

#ifndef __LIBDB_H
#include <libdb.h>
#endif

#ifndef __LDEFIN2D_H
#include <ldefin2d.h>
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE StepDLL = { NULL, NULL };


//-------------------------------------------------------------------------------
//   
//    DLL
// ---
extern "C" int APIENTRY
DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved )
{
  UNREFERENCED_PARAMETER( lpReserved );

  if ( dwReason == DLL_PROCESS_ATTACH )
  {
    TRACE0( "DLL Initializing!" );
 
    if ( !AfxInitExtensionModule( StepDLL, hInstance ) )
      return 0;

    new CDynLinkLibrary( StepDLL );
  }
  else if ( dwReason == DLL_PROCESS_DETACH )
  {
    TRACE0( "DLL Terminating!" );
    AfxTermExtensionModule( StepDLL );
  }
  return 1;
}


//-------------------------------------------------------------------------------
//   
// ---
unsigned int WINAPI LIBRARYID()
{
  return IDR_LIBID;
}


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


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  switch ( comm )
  {
    //   
    case 1 :
      DrawTxtDB();
      break;
 
    //   
    case 3 :
    {
      long index;
      //   
      if ( ReadLongT( _T(" "), //  
                     10000,           //    
                     -100000l,        //   
                     100000l,         
                     &index ) )       //  
      {
        TCHAR buf[255];
        _stprintf( buf, _T(" = %ld"), index );
        MessageT( buf );
      }
      else
        MessageT( _T("") );
      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 = YesNoT( _T("  CallBack?") );     
            if ( res == 1 )
              DrawRectCallBack();
            else
              if ( res == 0 )
                DrawRectNULL();
              else
                MessageT( _T("") );
            break;
          }
          
          //  
          case 6 :
            WriteSlideStep();
            break;   
            
          //   
          case 8 :
            TaskAccess();
            break;
        }
      }
      else
        MessageT( _T("   \n  /") );        
    }
  }
}


//--------------------------------------------------------------------------------
//     
//       , 
//     
// ---
void DrawTxtDB()
{
  TCHAR fileName[255];
  //   ,     ,  0                             
  if ( ksChoiceFileT( _T("*.loa"),  //    
                      //   (   ,    )
                      _T(" ( *.loa )|*.loa|  ( *.* )|*.*|"),
                      fileName, //  
                      255,      //   
                      0 ) )     //    ( - 1 -    
                               //  , 0 -   
                               //   )
  { 
    // C ,    ( ODBC_DB -  ODBC, TXT_DB -   )
    reference rDataBase = CreateDBT( _T("TXT_DB") );
 
    // C      ,    -  
    if ( ConnectDBT( rDataBase, fileName ) )
    {
      //          record

      // C  -      
      //      double   int,      
      //  ,     >=   Relation
      reference rRelation = Relation( rDataBase );
        RDoubleT( _T("A") ); //    , 
        RDoubleT( _T("B") ); //       
        RIntT( _T("") );
      EndRelation();

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

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

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

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

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

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

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


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

      TCHAR buf[255];
      _stprintf( buf, _T(" \n%s\n 10,   %i "), newChoice, size );
      MessageT( buf );

      delete[] newChoice;
    }
    else
      MessageT( choice );
  }
  else
    MessageT( _T("") );
}


//-------------------------------------------------------------------------------
//      
// ---
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()
{
  TCHAR name[255];

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

    double x, y; //    -      
    
    //     
    if ( CursorExT( &info,   //       	
                    &x,      //   	
                    &y, 
                    NULL, NULL ) ) //     ,  	 
    {
      int slideId;
      //   
      if ( ReadIntT( _T("  "), 100, 0, 32000, &slideId ) )
      {
        //  
        if ( !WriteSlideT( name,    //     
                          slideId, //     
                          x, y ) ) //  
          ErrorT( _T("  ") );
        ClearGroup( 0 ); //   ,  0,     
      }
    }
  }
}


//---------------------------------------------------------------------------------
//     
// ---
void WorkRelativePath()
{
  TCHAR mainName[255];
  //   
  if ( ksChoiceFileT( _T("*.*"), _T("  ( *.* )|*.*|"), mainName, 255, 0 ) )
  {
    TCHAR fileName[255];
    //  
    if ( ksChoiceFileT( _T("*.*"), _T("  ( *.* )|*.*|"), fileName, 255, 0 ) )
    {
      TCHAR resName[255];

      //  
      ksGetRelativePathFromFullPathT( mainName,  //     
                                      fileName,  //     
                                      resName,   //  -     (        )
                                      255 );     //  
 
      CString strMess( _T("    :") );
      strMess += mainName;
      strMess += _T("\n");
      strMess += _T("    :");
      strMess += fileName;
      strMess += _T("\n");
      strMess += _T("    :");
      strMess += resName;
      MessageT( (LPTSTR)(LPCTSTR)strMess  );
 
      //  
      TCHAR fullName[255];
      ksGetFullPathFromRelativePathT( mainName,  //     
                                      resName,   //     (        )
                                      fullName,  //  -     
                                      255 );     //  
 
      strMess = _T("    :");
      strMess += mainName;
      strMess += _T("\n");
      strMess += _T("    :");
      strMess += resName;
      strMess += _T("\n");
      strMess += _T("    :");
      strMess += fullName;
      MessageT( (LPTSTR)(LPCTSTR)strMess );
    }
  }
}


//---------------------------------------------------------------------------------
//    
// ---
void WorkSystemPath()
{
  //      
  TCHAR* catalogName[] = { _T("  "), 
                           _T(" "), 
                           _T("  "), 
                           _T(" "), 
                           _T("INI-") };
  
  //     
  RequestInfoT info;
  memset( &info, 0, sizeof( info ) );
  
  info.title    = _T("  "); //      
  info.commands = _T("! ! ! ! !INI-"); //       
  info.prompt   = _T("  "); //    
 
  int comm = 1;
  static TCHAR *userFile = _T("user.ttt");
  TCHAR fileName[255];
  int typeCatalog;
  while ( comm > 0 )
  {
    comm = CommandWindowT( &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- 
      }

      //  
      ksGetFullPathFromSystemPathT( userFile,       //    (    )
                                    fileName,       //  -    
                                    255,            //  
                                    typeCatalog ); //    . ksSystemPath
      CString strMess( _T("    ") );
      strMess += userFile;
      strMess += _T("\n");
      strMess += catalogName[comm-1];
      strMess += _T("\n");
      strMess += fileName;
      MessageT( (LPTSTR)(LPCTSTR)strMess );
 
      TCHAR resName[255];
 
      //  
      ksGetRelativePathFromSystemPathT( fileName,      //    
                                        resName,       //  -    (    )
                                        255,           //  
                                        typeCatalog ); //    . ksSystemPath
      strMess = _T("    ");
      strMess += userFile;
      strMess += _T("\n");
      strMess += catalogName[comm-1];
      strMess += _T("\n");
      strMess += resName;
      MessageT( (LPTSTR)(LPCTSTR)strMess );
    }
  }
}

