//////////////////////////////////////////////////////////////////////////////// 
// 
// step7.cpp - H  
// 
// 1.                                            - WalkFromView 
// 2.                                   - WalkFromMacro 
// 3.                                      - WalkFromDoc
// 4.                                           - WalkViewDoc 
// 5.                     - WalkGroup 
// 6.                                           - WalkLayer 
// 7.                                          - WalkFromGroup 
// 8.        - WalkFromDocWithAttr 
// 9.                                - WalkFromObjWithAttr 
// 
//////////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h"
#include <afxdllx.h> 
#include "resource.h"

#ifndef __LIBTOOL_H
#include <libtool.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 WalkFromView();
void WalkFromMacro();
void WalkFromDoc();
void WalkViewDoc();
void WalkGroup();
void WalkLayer();
void WalkFromGroup();
void WalkFromDocWithAttr();
void WalkFromObjWithAttr();


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  if ( comm == 3 )
    WalkFromDoc(); //   
  else
  {
    if ( ksGetCurrentDocument( 1 ) )
    {
      switch ( comm )
      {
        case 1 : WalkFromView();        break; //   
        case 2 : WalkFromMacro();       break; //   
        case 4 : WalkViewDoc();         break; //   
        case 5 : WalkGroup();           break; //      
        case 6 : WalkLayer();           break; //   
        case 7 : WalkFromGroup();       break; //   
        case 8 : WalkFromDocWithAttr(); break; //       
        case 9 : WalkFromObjWithAttr(); break; //    
      }
    }
    else
        ErrorT( _T("   \n  /") );
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WalkFromView()
{
  //            
  reference rItObject = CreateIterator( ALL_OBJ, //    
                                        0 );     //    (        )
  if ( rItObject )  
  {
    int count = 0;
    reference rObject = MoveIterator( rItObject, 'F' ); //   ( F -      )  
        
    while ( rObject ) 
    {
      //   
      LightObj( rObject, 1 );
      MessageT( _T("  ") );
      LightObj( rObject, 0 );
      
      count ++;      
      rObject = MoveIterator( rItObject, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItObject ); //      
      
    TCHAR buf[255];
    _stprintf( buf, _T("count = %d"), count );
    MessageT( buf );    
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WalkFromMacro()
{
  //           
  reference rItMacro = CreateIterator( MACRO_OBJ, //   
                                       0 );       //    (        )
  if ( rItMacro )
  {   
    int count  = 0;
    int count1 = 0;
    reference rMacro = MoveIterator( rItMacro, 'F' ); //   ( F -      ) 
     
    while ( rMacro )
    {
      //   
      LightObj( rMacro, 1 );
      MessageT( _T("  ") );
      LightObj( rMacro, 0 );
       
      // C       
      reference rItMacroObject = CreateIterator( ALL_OBJ,  //   
                                                 rMacro ); //    (        )
      if ( rItMacroObject )
      {
        reference rObject = MoveIterator( rItMacroObject, 'F' ); //   ( F -      )
         
        while ( rObject ) 
        {
          //     
          LightObj( rObject, 1 );
          MessageT( _T("  ") );
          LightObj( rObject, 0 );
          
          count1++;          
          rObject = MoveIterator( rItMacroObject, 'N' ); //   ( N -      )
        }
         
        DeleteIterator( rItMacroObject ); //      
      }
      
      count++;      
      rMacro = MoveIterator( rItMacro, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItMacro ); //      
   
    TCHAR buf[255];
    _stprintf( buf, _T("count = %d, count1 = %d"), count, count1 );
    MessageT( buf );
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WalkFromDoc()
{
  DocumentParamT parDocument; //    

  lstrcpy( parDocument.comment, _T("Create document") ); //   
  lstrcpy( parDocument.author, _T("User") );   //  
  parDocument.regim                = 0;    //  ( 0 - , 1 -  )
  parDocument.type                 = 1;    //  
  parDocument.sheet.stPar.format   = 3;    //    0 (0) ... 4(4)
  parDocument.sheet.stPar.multiply = 1;    //  
  parDocument.sheet.stPar.direct   = 0;    //   ( 0 -   , 1 -   )
  parDocument.sheet.shtType        = 1;    //     			
  parDocument.sheet.layoutName[0]  = _T('\0'); //   ,   -  "Graphic.lyt"

  //    -
  lstrcpy( parDocument.fileName, _T("a.cdw") ); //    
  CreateDocumentT( &parDocument ); 
  lstrcpy( parDocument.fileName, _T("b.cdw") ); 
  CreateDocumentT( &parDocument ); 
  lstrcpy( parDocument.fileName, _T("c.cdw") ); 
  CreateDocumentT( &parDocument );  

  // C     
  reference rItDocument = CreateIterator( DOCUMENT_OBJ, //   
                                          0 );          //    (        )  
  if ( rItDocument ) 
  {
    int count = 0;
    reference rDocument = MoveIterator( rItDocument, 'F' ); //   ( F -      )
    
    while ( rDocument )
    {
      //  
      SetObjParam( rDocument, 0, 0, DOCUMENT_STATE );
      switch ( count ) 
      {
        case 1: //     
          LineSeg( 20, 10, 40, 10, 1 );
          break;
        case 2: //     
          Circle ( 50, 50, 20, 1 );
          break;
        case 3: //      
          ArcByAngle( 50, 50, 20, 45, 135, 1, 1 );
      }
      
      count ++;
      rDocument = MoveIterator( rItDocument, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItDocument ); //      
   
    TCHAR buf[255];
    _stprintf( buf, _T("count = %d"), count );
    MessageT( buf );   
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WalkViewDoc()
{ 
  ViewParamT par; //   
 
  par.x     = 10; //   
  par.y     = 20; 
  par.scale = 1;  //  
  par.ang   = 0;  //   
  par.color = RGB( 10, 20, 10 );   //     
  par.state = stACTIVE;            //  
  _tcscpy( par.name, _T("User view") ); //  
 
  //  3 ,      
  for ( int i = 1; i < 4; i++ ) 
    CreateSheetViewT( &par, &i ); //    ,    
                                 //  ,    

  
  //        
  reference rItView = CreateIterator( VIEW_OBJ, //   
                                      0 );      //    (        )
  if ( rItView ) 
  {
    int count = 0;
    reference rView = MoveIterator ( rItView, 'F' ); //   ( F -      )
    
    while ( rView )
    {
      //  
      int state = stCURRENT;
      SetObjParam( rView, &state, sizeof( int ), VIEW_LAYER_STATE );

      switch ( count )
      {
        case 1: //     
          LineSeg( 20, 20, 40, 20, 1 );
          break;
        case 2: //     
          Circle( 40, 20, 30, 1 );
          break;
        case 3: //      
          ArcByAngle( 50, 50, 20, 45, 135, 1, 1 );
          break;
      } 
      
      count++;
      rView = MoveIterator ( rItView, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItView ); //      
   
    TCHAR buf[255];
    _stprintf( buf, _T("count = %d"), count );
    MessageT( buf );   
  }
}


//-------------------------------------------------------------------------------
//      
// ---
void WalkGroup()
{
  //         
  reference rItNameGroup = CreateIterator( NAME_GROUP_OBJ, //   
                                         0 );            //    (        )
  if ( rItNameGroup ) 
  {
    int count = 0;
    reference rNameGrp = MoveIterator( rItNameGroup, 'F' ); //   ( F -      ) 
    
    while ( rNameGrp ) 
    {
      //   
      LightObj( rNameGrp, 1 );
      MessageT( _T("  ") );
      LightObj( rNameGrp, 0 );
      
      count ++;
      rNameGrp = MoveIterator( rItNameGroup, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItNameGroup ); //      
    
    TCHAR buf[255];
    _stprintf( buf, _T("count = %d"), count );
    MessageT( buf );     
  }
  
  //   , type -   ( 0 -  , 1 -  )
  NewGroup( 0 );
    Circle( 30, 30, 20, 1 );
    Circle( 30, 30, 10, 1 );
    //  
    Hatch( 0, 45, 2, 0, 0, 0 );
      Circle( 30, 30, 20, 1 );
      Circle( 30, 30, 10, 1 );
    EndObj(); 
  EndGroup(); //    
  
  //        
  //         
  reference rItWorkGroup = CreateIterator( WORK_GROUP_OBJ, //   
                                           0 );            //    (        )
  if ( rItWorkGroup ) 
  {
    int count1 = 0;
    reference rWorkGrp = MoveIterator( rItWorkGroup, 'F' ); //   ( F -      ) 
    
    while ( rWorkGrp )
    {
      //   
      LightObj( rWorkGrp, 1 );
      MessageT( _T("  ") );
      LightObj( rWorkGrp, 0 );
      
      count1 ++;
      rWorkGrp = MoveIterator( rItWorkGroup, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItWorkGroup ); //      
    
    TCHAR buf[255];
    _stprintf( buf, _T("count1 = %d"), count1 );
    MessageT( buf );     
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WalkLayer()
{
  //  3 ,      
  for ( int i = 0; i < 5; i++ )
  {
    Layer( i ); //   ,      ,   .
    Circle( 30, 30, 5 + i * 10, 1 ); //     
  }

  //    
  reference rItLayer = CreateIterator( LAYER_OBJ, //   
                                       0 );       //    (        )
  if ( rItLayer ) 
  {
    int count = 0;
    reference rLayer = MoveIterator( rItLayer, 'F' ); //   ( F -      )
    
    while ( rLayer )
    {
      //   
      LightObj( rLayer, 1 );
      MessageT( _T("  ") );
      LightObj( rLayer, 0 );
      
      count ++;
      rLayer = MoveIterator ( rItLayer, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItLayer ); //      
   
    TCHAR buf[255];
    _stprintf( buf, _T("count = %d"), count );
    MessageT( buf );   
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WalkFromGroup()
{
  //   , type -   ( 0 -  , 1 -  )
  reference rGroup = NewGroup( 0 );
    LineSeg( 10, 50, 50, 50, 1 );
    LineSeg( 10, 10, 50, 10, 1 );
    LineSeg( 10, 10, 10, 50, 1 );
    LineSeg( 50, 10, 50, 50, 1 );
    Circle( 30, 30, 20, 1 );
    Circle( 30, 30, 10, 1 );
    //  
    Hatch( 0, 45, 2, 0, 0, 0 );  
      Circle( 30, 30, 20, 1 );
      Circle( 30, 30, 10, 1 );
    EndObj();
  EndGroup(); //    

  //      
  reference rItObject = CreateIterator( ALL_OBJ,  //   
                                        rGroup ); //    (        )
  
  if ( rItObject ) 
  {
    int count = 0;
    reference rObject = MoveIterator ( rItObject, 'F' ); //   ( F -      )
    
    while ( rObject )
    {
      //   
      LightObj( rObject, 1 );
      MessageT( _T("  ") );
      LightObj( rObject, 0 );
      
      count ++;
      rObject = MoveIterator( rItObject, 'N' ); //   ( N -      )
    }
    
    DeleteIterator( rItObject ); //      
   
    TCHAR buf[255];
    _stprintf( buf, _T("count = %d"), count );
    MessageT( buf );   
  }
}


//-------------------------------------------------------------------------------
//       
//    ,      
// key1 = 10         
// ---
void WalkFromDocWithAttr()
{
  //        10
  reference rIterator = CreateAttrIterator( 0,   //   ,  0,       
                                                 //    ,       
                                            10,  //       0 
                                            0, 
                                            0, 
                                            0, 
                                            0 ); //         0  
  //   -     
  if ( rIterator ) 
  {
  
    reference rObject; 
    //     ( F -      )
    reference rAttribute = MoveAttrIterator( rIterator,  //   
                                             'F',        //   'F'/'N'
                                             &rObject ); //      ,  NULL,  
    int count = 0;
    TCHAR buf[255];

    while ( rAttribute && rObject )
    {
      //   
      LightObj( rObject, 1 );
       
      unsigned int countRows, countColumns;  
      
      //       
      if ( GetAttrTabInfo( rAttribute, &countRows, &countColumns ) )
      {
        _stprintf( buf, _T("count = %d, countRows = %d, columnsCount = %d"), count, countRows, countColumns );
        MessageT( buf );
      }
      else
        //   -     
        MessageBoxResult(); //         

      LightObj( rObject, 0 );
      
      count ++;       
      //     ( N -      )
      rAttribute = MoveAttrIterator( rIterator,  //   
                                     'N',        //   'F'/'N'
                                     &rObject ); //      ,  NULL,  
    }
    DeleteIterator( rIterator ); //      
 
    _stprintf( buf, _T("count = %d"), count );
    MessageT( buf );   
  }
}


//-------------------------------------------------------------------------------
//    
//   ,    
// key1 = 10         
// ---
void WalkFromObjWithAttr()
{
  //     
  RequestInfoT info;
  memset( &info, 0, sizeof( info ) );
  
  //       
  info.prompt = _T(" ");
  
  //   
  double x, y;  
  
  reference rObject;
  
  //     
  while( CursorExT( &info, &x, &y, 0, NULL ) )
  {
    //       
    rObject = FindObj( x, y,  //  
                       1e6 ); //   -     x,y
    //   
    if ( rObject )
    { 
      //         10
      reference rIterator = CreateAttrIterator( rObject, //   ,  0,       
                                                         //    ,       
                                                10,      //       0 
                                                0, 
                                                0, 
                                                0, 
                                                0 );     //         0        
      //   -     
      if ( rIterator ) 
      {
        //   
        LightObj( rObject, 1 );
     
        //     ( F -      ) 
        reference rAttribute = MoveAttrIterator( rIterator, //   
                                                 'F',       //   'F'/'N'
                                                 0 );       //      ,  NULL,  
        int count = 0;
        TCHAR buf[255];

        while ( rAttribute ) 
        {
          unsigned int countRows, countColumns;  
                  
          count ++;            
          //       
          if ( GetAttrTabInfo( rAttribute, &countRows, &countColumns ) )
          {
            _stprintf( buf, _T("count = %d, countRows = %d, columnsCount = %d"), count, countRows, countColumns );
            MessageT( buf );
          }
          else
            //   -     
            MessageBoxResult(); //        
          
          //     ( N -      )
          rAttribute = MoveAttrIterator( rIterator, //   
                                         'N',       //   'F'/'N'
                                          0 );      //      ,  NULL,  
        }
        DeleteIterator( rIterator ); //      

        //   
        LightObj( rObject, 0 );
      }
    }  
  }
}

