////////////////////////////////////////////////////////////////////////////////
//
// testsl.cpp -  
//
// 1.     - MoveSlide         - <testmov.cpp> 
// 2.     - WriteSlideStep 
// 3.   - TestShowDialog 
// 4.      - DecomposeSlideStep
//
////////////////////////////////////////////////////////////////////////////////
#ifndef __WINDOWS_
#include <windows.h>
#endif

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

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

#ifndef __LIBOLD_H
#include <libold.h>
#endif

#ifndef __TESTSL_RH
#include "testsl.rh"
#endif

#ifndef __TESTMOVE_H
#include "testmov.h"
#endif

HINSTANCE _hinst;


//-------------------------------------------------------------------------------
//   
// ---  
void WriteSlideStep();
void DecomposeSlideStep();
void TestShowDialog();
void symmetry( float x, float y,float x1, float y1,float x2, float y2,
               float *xc, float *yc );


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


//-------------------------------------------------------------------------------
//   
// ---
extern "C" void far __export __pascal LibraryEntry( UINT comm )
{
  switch ( comm )
  {
    //  
    case 4 :
      TestShowDialog();
      break;
    
    default:  
    {
      if ( ksGetCurrentDocument( 1 ) ) //   2D 
      {
        switch ( comm )
        {
          //  
          case 1 :
            MoveSlide();
            break;

          //  
          case 2 :
            WriteSlideStep();  
            break;

          //  
          case 3 :
            DecomposeSlideStep();    
            break;
        }
      }
      else
        Message( "   \n  /" );        
    }
  }
}


//-------------------------------------------------------------------------------
//  
// ---
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,     
      }
    }
  }
}


//-------------------------------------------------------------------------------
//  
// ---
static double g_x1, g_y1, g_x2, g_y2;
static int g_flag;


//-------------------------------------------------------------------------------
//  
// ---
static void DrawRamka() 
{
  if( fabs( g_y1 - g_y2 ) >0 ) 
  {
    LineSeg( g_x1, g_y1, g_x1, g_y2, 1 );
    LineSeg( g_x2, g_y1, g_x2, g_y2, 1 );
  }
  if( fabs( g_x1 - g_x2 ) >0 ) 
  {
    LineSeg( g_x1, g_y1, g_x2, g_y1, 1 );
    LineSeg( g_x1, g_y2, g_x2, g_y2, 1 );
  }
}


//-------------------------------------------------------------------------------
//   ,   Cursor
// ---
int WINAPI CallBackC( int comm,             //  
                      double *x, double *y, //  
                      RequestInfo *info,    //     
                      void *phantom,        //   
                      int dynamic )
{
  Phantom *pPhantom = ( Phantom * )phantom;

  if( !dynamic ) // 
  { 
    if( !g_flag ) //  1 
    {
      g_flag++;
      g_x1 = *x; 
      g_y1= *y;
      info->commands = "   ";
    }
    else 
    {
      g_flag++;
      g_x2 = *x; 
      g_y2 = *y;
      return 0;
    }
  }
  else  
  {
    if( g_flag > 0 )
    {
      g_x2 = *x; 
      g_y2 = *y;
      pPhantom->type1.xBase = g_x2; 
      pPhantom->type1.yBase = g_y2;
      if ( pPhantom->type1.gr)
        DeleteObj( pPhantom->type1.gr );
      
      pPhantom->type1.gr = NewGroup( 1 ); //  
        DrawRamka();
      EndGroup();
    }
  }
  return 1;
}


//-------------------------------------------------------------------------------
//  
// ---
void DecomposeSlideStep() 
{
  double x, y;
  RequestInfo  info;
  memset( &info, 0, sizeof(info));
  info.commands   = "   ";
  info.dynamic    = 1;
  info.callBack   = CallBackC; //      Cursor
  Phantom par;
  par.phType      = 1;
  par.type1.gr    = NewGroup( 1 );
  EndGroup();
  par.type1.xBase = 0; 
  par.type1.yBase = 0;
  par.type1.scale = 1;
  par.type1.ang   = 0;
  g_flag          = 0;

  Cursor(&info,&x,&y,&par);
  if ( par.type1.gr)
    DeleteObj( par.type1.gr );
  if( g_flag == 2 )
  {
    ClearGroup(0);
    if(SelectGroup( 0, 1, g_x1, g_y1, g_x2, g_y2 ) ) 
    {
      reference iGrp = CreateIterator ( SELECT_GROUP_OBJ, 0 );
      if ( iGrp )
      {
        par.type1.gr = NewGroup( 1 );
        EndGroup();
        reference rObj;
        reference gr;
        if ( ExistObj( rObj = MoveIterator ( iGrp, 'F' )) ) 
        {
          do 
          {
            gr = DecomposeObj( rObj, 0, 0.2, 0 );
            AddObjGroup( par.type1.gr, gr );
            ksClearGroup( gr, false );
            DeleteObj( gr );
          } while( ExistObj ( rObj = MoveIterator( iGrp, 'N' )) );
        }

        memset( &info, 0, sizeof(info));
        par.type1.xBase = g_x1; 
        par.type1.yBase = g_y1;
        info.commands = "   ";
        if( Cursor( &info, &x, &y, &par ) ) 
        {
          Mtr( x - g_x1, y - g_y1, 0, 1 );
            TransformObj( par.type1.gr );
          DeleteMtr();
          StoreTmpGroup( par.type1.gr );
        }
        ClearGroup( par.type1.gr );
        DeleteObj( par.type1.gr );
      }
      ClearGroup(0);
    }
    else
      Error("");
  }
} 