//////////////////////////////////////////////////////////////////////////////// 
// 
// step7.cpp - H  
// 
// 1.                                            - WalkFromView
// 2.                                   - WalkFromMacro 
// 3.                                      - WalkFromDoc
// 4.                                           - WalkViewDoc 
// 5.                     - WalkGroup 
// 6.                                           - WalkLayer 
// 7.                                          - WalkFromGroup 
// 8.        - WalkFromDocWithAttr 
// 9.                                - WalkFromObjWithAttr 
// 
//////////////////////////////////////////////////////////////////////////////// 
#ifndef __WINDOWS_H  
#include <windows.h>
#endif

#ifndef __VALUES_H
#include "values.h"
#endif

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

#ifndef __STDLIB_H
#include <stdlib.h>
#endif

#ifndef __STRING_H
#include <string.h>
#endif

#ifndef __LIBTOOL_H
#include "libtool.h"
#endif

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


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


//-------------------------------------------------------------------------------
//   
// ---
void WalkFromView();
void WalkFromMacro();
void WalkFromDoc();
void WalkViewDoc();
void WalkGroup();
void WalkLayer();
void WalkFromGroup();
void WalkFromDocWithAttr();
void WalkFromObjWithAttr();


//-------------------------------------------------------------------------------
//   
// ---
extern "C" void __export __pascal 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
        Error( "   \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 );
      Message( "  " );
      LightObj( rObject, 0 );

      count ++;
      rObject = MoveIterator( rItObject, 'N' ); //   ( N -      )
    }

    DeleteIterator( rItObject ); //      

    char buf[255];
    sprintf( buf, "count = %d", count );
    Message( 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 );
      Message( "  " );
      LightObj( rMacro, 0 );

      // C       
      reference rItMacroObject = CreateIterator( ALL_OBJ,  //   
                                              rMacro ); //    (        )
      if ( rItMacroObject )
      {
        reference rObject = MoveIterator( rItMacroObject, 'F' ); //   ( F -      )

        while ( rObject )
        {
          //     
          LightObj( rObject, 1 );
          Message( "  " );
          LightObj( rObject, 0 );

          count1++;
          rObject = MoveIterator( rItMacroObject, 'N' ); //   ( N -      )
        }

        DeleteIterator( rItMacroObject ); //      
      }

      count++;
      rMacro = MoveIterator( rItMacro, 'N' ); //   ( N -      )
    }

    DeleteIterator( rItMacro ); //      

    char buf[255];
    sprintf( buf, "count = %d, count1 = %d", count, count1 );
    Message( buf );
  }
}


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

  lstrcpy( parDocument.comment, "Create document" ); //   
  lstrcpy( parDocument.author, "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]  = '\0'; //   ,   -  "Graphic.lyt"

  //    -
  lstrcpy( parDocument.fileName, "a.cdw" ); //    
  CreateDocument( &parDocument );
  lstrcpy( parDocument.fileName, "b.cdw" );
  CreateDocument( &parDocument );
  lstrcpy( parDocument.fileName, "c.cdw" );
  CreateDocument( &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 ); //      

    char buf[255];
    sprintf( buf, "count = %d", count );
    Message( buf );
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WalkViewDoc()
{
  ViewParam par; //   

  par.x     = 10; //   
  par.y     = 20;
  par.scale = 1;  //  
  par.ang   = 0;  //   
  par.color = RGB( 10, 20, 10 );   //     
  par.state = stACTIVE;            //  
  strcpy( par.name, "User view" ); //  

  //  3 ,      
  for ( int i = 1; i < 4; i++ )
    CreateSheetView( &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 ); //      

    char buf[255];
    sprintf( buf, "count = %d", count );
    Message( 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 );
      Message( "  " );
      LightObj( rNameGrp, 0 );

      count ++;
      rNameGrp = MoveIterator( rItNameGroup, 'N' ); //   ( N -      )
    }

    DeleteIterator( rItNameGroup ); //      

    char buf[255];
    sprintf( buf, "count = %d", count );
    Message( 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 );
      Message( "  " );
      LightObj( rWorkGrp, 0 );

      count1 ++;
      rWorkGrp = MoveIterator( rItWorkGroup, 'N' ); //   ( N -      )
    }

    DeleteIterator( rItWorkGroup ); //      

    char buf[255];
    sprintf( buf, "count1 = %d", count1 );
    Message( 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 );
      Message( "  " );
      LightObj( rLayer, 0 );

      count ++;
      rLayer = MoveIterator ( rItLayer, 'N' ); //   ( N -      )
    }

    DeleteIterator( rItLayer ); //      

    char buf[255];
    sprintf( buf, "count = %d", count );
    Message( 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 );
      Message( "  " );
      LightObj( rObject, 0 );

      count ++;
      rObject = MoveIterator( rItObject, 'N' ); //   ( N -      )
    }

    DeleteIterator( rItObject ); //      

    char buf[255];
    sprintf( buf, "count = %d", count );
    Message( 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;
    char buf[255];

    while ( rAttribute && rObject )
    {
      //   
      LightObj( rObject, 1 );

      unsigned int countRows, countColumns;

      //       
      if ( GetAttrTabInfo( rAttribute, &countRows, &countColumns ) )
      {
        sprintf( buf, "count = %d, countRows = %d, columnsCount = %d", count, countRows, countColumns );
        Message( buf );
      }
      else
        //   -     
        MessageBoxResult(); //      

      count ++;
      //     ( N -      )
      rAttribute = MoveAttrIterator( rIterator,  //   
                                     'N',        //   'F'/'N'
                                     &rObject ); //      ,  NULL,  
      //    
      LightObj( rObject, 0 );
    }
    DeleteIterator( rIterator ); //      

    sprintf( buf, "count = %d", count );
    Message( buf );
  }
}


//-------------------------------------------------------------------------------
//    
//   ,    
// key1 = 10         
// ---
void WalkFromObjWithAttr()
{
  //     
  RequestInfo info;
  memset( &info, 0, sizeof( info ) );

  //      
  info.prompt = " ";

  //   
  double x, y;

  reference rObject;

  //     
  while( Cursor( &info, &x, &y, 0 ) )
  {
    //       
    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;
        char buf[255];

        while ( rAttribute )
        {
          unsigned int countRows, countColumns;

          count ++;
          //       
          if ( GetAttrTabInfo( rAttribute, &countRows, &countColumns ) )
          {
            sprintf( buf, "count = %d, countRows = %d, columnsCount = %d", count, countRows, countColumns );
            Message( buf );
          }
          else
            //   -     
            MessageBoxResult(); //      

          //     ( N -      )
          rAttribute = MoveAttrIterator( rIterator, //   
                                         'N',       //   'F'/'N'
                                         0 );       //      ,  NULL,  
        }
        DeleteIterator( rIterator ); //      

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