////////////////////////////////////////////////////////////////////////////////
//
// step4_1.cpp -  Cursor  Placement
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <math.h>
#include "step4.h"
#include <ldefin2d.h>

#ifdef __LIGHT_VERSION__
#include <klAPI5.h>
#else
#include <kAPI5.h>
#endif

#include <ksConstants.h>

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

extern KompasObject kompas;
extern CStep4App theApp;

ksDocument2D docActive;

int type, flag=1;


//------------------------------------------------------------------------------
//   ,   Cursor
// ---
int WINAPI CALLBACKPROCCURSOR( int comm, 
                               double* x, double*y, 
                               LPDISPATCH rInfo, LPDISPATCH rPhan, 
                               int dynamic ) 
{
	ksRequestInfo info( rInfo );
	ksPhantom     phan( rPhan );

	if ( info.m_lpDispatch && phan.m_lpDispatch ) 
  {
		phan.SetPhantom( 1 );
		ksType1 t1( phan.GetPhantomParam() );
		switch ( comm ) 
    {
			case 1 : 
			case 2 : 
        type = comm; 
        break;
			case -1 : //   
      { 
				docActive.ksMoveObj( t1.GetGr(), *x, *y );
				if ( ::fabs(t1.GetAngle()) > 0.001 )
					docActive.ksRotateObj( t1.GetGr(), *x, *y, t1.GetAngle() );
				docActive.ksStoreTmpGroup( t1.GetGr() );
				docActive.ksClearGroup( t1.GetGr(), true );
			}
			break;
		}

    //            
		if ( t1.GetGr() ) 
			docActive.ksDeleteObj( t1.GetGr() );
		t1.SetGr( docActive.ksNewGroup(1) ); //  

		if ( (flag == 1 && comm == 1) || (flag == 2 && comm == 2) )
		  type = 3;

    //         
	  switch ( type  ) 
    {
		  case 1:
			  docActive.ksCircle( 0, 0, 20, 1 );
				info.SetCommandsString( "! ! " );
				flag = 1;
				break;
			case 2:
				docActive.ksLineSeg( -10, 0,  10,  0,  1 );
				docActive.ksLineSeg( 10,  0,  0,   20, 1 );
				docActive.ksLineSeg( 0,   20, -10, 0,  1 );
				info.SetCommandsString( "! ! " );
				flag = 2;
				break;
			case 3:
	      docActive.ksLineSeg( -10, 0,  10,  0,  1 );
		    docActive.ksLineSeg( 10,  0,  10,  20, 1 );
			  docActive.ksLineSeg( 10,  20, -10, 20, 1 );
				docActive.ksLineSeg( -10, 20, -10, 0,  1 );
	      info.SetCommandsString( "! ! " );
		    flag = 0;
			  break;
		}
		docActive.ksEndGroup();
	}  
  // 
  info.DetachDispatch();
  phan.DetachDispatch();

	return 1;
}


//------------------------------------------------------------------------------
//   ,   Placement
// ---
int WINAPI CALLBACKPROCPLACEMENT( int comm, 
                                  double* x, double* y, double* ang, 
                                  LPDISPATCH rInfo,	LPDISPATCH rPhan, 
                                  int dynamic ) 
{
	ksRequestInfo info( rInfo );
	ksPhantom     phan( rPhan );

	if ( info.m_lpDispatch && phan.m_lpDispatch ) 
  {
		phan.SetPhantom( 1 );
		ksType1 t1( phan.GetPhantomParam() );
		switch ( comm ) 
    {
	    case 1 :
			case 2 : 
        type = comm; 
        break;
			case -1 : //   
      {                 
        docActive.ksMoveObj( t1.GetGr(), *x, *y );
        //   Cursor      
				if ( ::fabs(*ang) > 0.001 )
					docActive.ksRotateObj( t1.GetGr(), *x, *y, *ang );
				docActive.ksStoreTmpGroup( t1.GetGr() );   
        //     
				docActive.ksClearGroup( t1.GetGr(), true );
				break;
			}
		}

    //            
		if ( t1.GetGr() ) 
			docActive.ksDeleteObj( t1.GetGr() );
		t1.SetGr( docActive.ksNewGroup(1) ); //  

    //         
	  if ( (flag == 1 && comm == 1) || (flag == 2 && comm == 2) )
		  type = 3;

		switch ( type ) 
    {
			case 1:
				docActive.ksCircle( 0, 0, 20, 1 );
				info.SetCommandsString( "! ! " );
	      flag = 1;
		    break;
			case 2:
	      docActive.ksLineSeg( -10, 0,  10,  0,  1 );
		    docActive.ksLineSeg( 10,  0,  0,   20, 1 );
			  docActive.ksLineSeg( 0,   20, -10, 0,  1 );
				info.SetCommandsString( "! ! " );
	      flag = 2;
		    break;
			case 3:
	      docActive.ksLineSeg( -10, 0,  10,  0,  1 );
		    docActive.ksLineSeg( 10,  0,  10,  20, 1 );
			  docActive.ksLineSeg( 10,  20, -10, 20, 1 );
				docActive.ksLineSeg( -10, 20, -10, 0,  1 );
	      info.SetCommandsString( "! ! " );
			  flag = 0;
				break;
		}

		docActive.ksEndGroup();
	}
  // 
  info.DetachDispatch();
  phan.DetachDispatch(); 

	return 1;
}


//-------------------------------------------------------------------------------
//
// ---
void DrawRectCallBack( ksDocument2D& doc ) {
	docActive = doc;
  type      = 1;
	ksPhantom phan( kompas.GetParamStruct(ko_Phantom) );
	if ( phan.m_lpDispatch ) {
		phan.Init();
		phan.SetPhantom( 1 );
		ksType1 t1( phan.GetPhantomParam() );
		if ( t1.m_lpDispatch ) {
			t1.Init();
			t1.SetScale_( 1 );
			t1.SetGr( doc.ksNewGroup(1) );   //  
		    doc.ksCircle( 0, 0, 20, 1 );
			doc.ksEndGroup();

			double x, y, ang;
			ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
			if ( info.m_lpDispatch ) {
				info.Init();
				info.SetCommandsString( "! !" );
				//      Placement
				info.SetCallBackP( "CALLBACKPROCPLACEMENT", (long)theApp.m_hInstance, 0 );
				doc.ksPlacement( info, &x, &y, &ang, phan );

				t1.SetGr( doc.ksNewGroup(1) );   //  
			    doc.ksCircle( 0, 0, 20, 1 );
				doc.ksEndGroup();

				//      Cursor
				info.SetCallBackC( "CALLBACKPROCCURSOR", (long)theApp.m_hInstance, 0 );
				doc.ksCursor( info, &x, &y, phan );
			}
		}
	}
}

//-------------------------------------------------------------------------------
//
// ---
void DrawRectNULL( ksDocument2D& doc ) {
  int type = 1, flag=1;
  int j = 1;
	ksPhantom phan( kompas.GetParamStruct(ko_Phantom) );
	if ( phan.m_lpDispatch ) {
		phan.Init();
		phan.SetPhantom( 1 );
		ksType1 t1( phan.GetPhantomParam() );
		if ( t1.m_lpDispatch ) {
			t1.Init();
			t1.SetScale_( 1 );
			t1.SetGr( 0 );   //  

			double x, y, ang;
			ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
			if ( info.m_lpDispatch ) {
				info.Init();
			  while ( j ) {

					if ( t1.GetGr() )
						doc.ksDeleteObj( t1.GetGr() );

					t1.SetGr( doc.ksNewGroup(1) ); //  
					if ( (flag == 1 && j == 1) || (flag == 2 && j == 2) )
						type = 3;

			    switch ( type ) {
					  case 1:
							doc.ksCircle( 0, 0, 20, 1 );
							info.SetCommandsString( "! ! " );
					    flag = 1;
						  break;
					  case 2:
				      doc.ksLineSeg( -10, 0,  10,  0,  1 );
						  doc.ksLineSeg( 10,  0,  0,   20, 1 );
				      doc.ksLineSeg( 0,   20, -10, 0,  1 );
					    info.SetCommandsString( "! ! " );
							flag = 2;
							break;
					  case 3:
							doc.ksLineSeg( -10, 0,  10,  0,  1 );
			        doc.ksLineSeg( 10,  0,  10,  20, 1 );
					    doc.ksLineSeg( 10,  20, -10, 20, 1 );
							doc.ksLineSeg( -10, 20, -10, 0,  1 );
						  info.SetCommandsString( "! ! " );
							flag = 0;
							break;
					}

					doc.ksEndGroup();

					j = doc.ksPlacement( info, &x, &y, &ang, phan );
//					j = doc.ksCursor( info, &x, &y, phan );
			    switch ( j ) {
						case 1:
			      case 2:	type = j; break;
						case -1: //  
							doc.ksMoveObj( t1.GetGr(), x, y );
							if ( ::fabs(t1.GetAngle()) > 0.001 )
								doc.ksRotateObj( t1.GetGr(), x, y, ang /*t1.GetAngle()*/ );
							doc.ksStoreTmpGroup( t1.GetGr() );    //     
							doc.ksClearGroup( t1.GetGr(), true );
							break;
					}
				}
			}
		}
	}
}
