//////////////////////////////////////////////////////////////////////////////// 
// 
// slideWrk.cpp -  
// 
// 1.     - MoveSlide         - <testmov.cpp> 
// 2.     - WriteSlideStep 
// 3.      - DecomposeSlideStep
// 4.   - TestShowDialog 
// 
//////////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h"
#include <afxdllx.h> 
#include "resource.h"

#ifndef  _INC_MATH
#include "math.h"
#endif 

#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 MoveSlide();    
void WriteSlideStep();
void TestShowDialog();
void DecomposeSlideStep();


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int 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
        MessageT( _T("   \n  /") );        
    }
  }
}


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


//////////////////////////////////////////////////////////////////////////////// 
// 
//   
// 
//////////////////////////////////////////////////////////////////////////////// 
class TestShowDlg : public CDialog
{
public:
  TestShowDlg( CWnd* pParent = NULL );
  
  // {{AFX_DATA( TestShowDlg )
  enum { IDD = IDD_TESTDIALOG };
  CStatic m_StaticShow;
  // }}AFX_DATA

  // {{AFX_VIRTUAL( TestShowDlg )
protected:
  virtual void DoDataExchange( CDataExchange* pDX );
  // }}AFX_VIRTUAL

protected:
  // {{AFX_MSG( TestShowDlg )
  afx_msg void OnPaint();
  // }}AFX_MSG

  DECLARE_MESSAGE_MAP()
};


//-------------------------------------------------------------------------------
// 
// ---
TestShowDlg::TestShowDlg( CWnd* pParent /*=NULL*/ )
  : CDialog( TestShowDlg::IDD, pParent )
{ 
  // {{AFX_DATA_INIT( TestShowDlg )
  // }}AFX_DATA_INIT
}


//-------------------------------------------------------------------------------
//    
// ---
void TestShowDlg::DoDataExchange( CDataExchange* pDX )
{
  CDialog::DoDataExchange( pDX );
  // {{AFX_DATA_MAP( TestShowDlg )
  DDX_Control( pDX, IDC_SHOW_STATIC, m_StaticShow );
  // }}AFX_DATA_MAP
}


//-------------------------------------------------------------------------------
//  
// ---
BEGIN_MESSAGE_MAP( TestShowDlg, CDialog )
  // {{AFX_MSG_MAP( TestShowDlg )
  ON_WM_PAINT()
  // }}AFX_MSG_MAP
END_MESSAGE_MAP()


//-------------------------------------------------------------------------------
//  
// ---
void TestShowDlg::OnPaint()
{
  CPaintDC dc( this );

  TCHAR  fName[_MAX_PATH];
  ::GetModuleFileName( StepDLL.hModule, fName, _MAX_PATH);
  LPTSTR c = ::_tcsrchr( fName, _T('\\') );
  if ( c )
    *( c + 1 ) = _T('\0');
  ::_tcscat( fName, _T("Example.rc") );

  ::ksDrawSlideFromFileT( m_StaticShow.GetSafeHwnd(), fName );
}


//-------------------------------------------------------------------------------
//    
// ---
void TestShowDialog()
{
  TestShowDlg* dlg = new TestShowDlg();         
  if ( dlg )
  {         
    EnableTaskAccess( 0 ); //       
    dlg->DoModal();        //  
    EnableTaskAccess( 1 ); //       

    delete dlg;
  }   
}


//-------------------------------------------------------------------------------
//  
// ---
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, //  
                      RequestInfoT *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 = _T("   ");
    }
    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;
  RequestInfoT  info;
  memset( &info, 0, sizeof(info));
  info.commands   = _T("   ");
  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;

  CursorExT(&info,&x,&y,&par, NULL);
  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 = _T("   ");
        if( CursorExT( &info, &x, &y, &par, NULL) ) 
        {
          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
      ErrorT(_T(""));
  }
}

