/////////////////////////////////////////////////////////////////////////////
//
//  2   Builder C++ (Auto)
//
/////////////////////////////////////////////////////////////////////////////

#include <vcl.h>
#pragma hdrstop

#ifndef _APPLICATIONEVENT_H
#include "ApplicationEvent.h"
#endif

#ifndef _DOCUMENTEVENT_H
#include "DocumentEvent.h"
#endif

#ifndef _OBJECT2DEVENT_H
#include "Object2DEvent.h"
#endif

#ifndef __LDEFIN2D_H
#include <ldefin2d.h>
#endif


//------------------------------------------------------------------------------
//
// ---
ApplicationEvent * aplEvent = NULL;
KompasObjectPtr kompasPtr;

//------------------------------------------------------------------------------
//
// ---
extern "C" int far __export __pascal LibraryID()
{
  return 100;
}

//------------------------------------------------------------------------------
//  
// ---
int LibMessage( char* str, int flags ) {
  int res = 0;
  if ( str && str[0] ) {                  //  
    if ( kompasPtr ) {
      bool enabse = kompasPtr->ksIsEnableTaskAccess(); //  
      if ( enabse )                                    //     
        kompasPtr->ksEnableTaskAccess(0);                //  

      //                                                    
      res = Application->MessageBox( str,   LoadStr(LibraryID()).c_str(), flags );

      if ( enabse )                         //      
        kompasPtr->ksEnableTaskAccess(1);              //    
    }
  }

  return res;
}

//------------------------------------------------------------------------------
//
// ---
extern "C" IDispatch * __export WINAPI CreateKompasObject();

/*
//-------------------------------------------------------------------------------
//
// ---
void AdviseDoc( IUnknownPtr docPtr, long docType )
{
  if ( kompasPtr )
  {
    if( docPtr )
    {
      NewDocumentEvent( docPtr );
      switch ( docType )
      {
        case lt_DocSheetStandart : // 1 -  
        case lt_DocSheetUser     : // 2 -  
        case lt_DocFragment      : // 3 - 
        {
          ksDocument2DPtr doc2DPtr( docPtr ); //  
          long type = ALL_OBJ;
          new Object2DEvent( doc2DPtr->GetObject2DNotify( type ) ); //    
          break;
        }
        case lt_DocPart3D     : // 5 - 3d- 
        case lt_DocAssemble3D : // 6 - 3d- 
        {
          // ksDocument3DPtr doc( pDoc ); //  
          break;
        }
        case lt_DocTxtStandart : // 7-   
        case lt_DocTxtUser     : // 8-   
        {
          // ksDocumentTxtPtr doc( pDoc ); //  
          break;
        }
        case lt_DocSpcUser : // 9-   
        case lt_DocSpc     : // 4- 
        {
          // ksSpcDocumentPtr doc( pDoc ); //  
          break;
        }
      }
    }
  }
}


//-------------------------------------------------------------------------------
//
// ---
void AdviseDoc( reference refDocument )
{
  if( kompasPtr )
    AdviseDoc( kompasPtr->ksGetDocumentByReference( refDocument ),
               kompasPtr->ksGetDocumentType( refDocument ) );
}
*/

//------------------------------------------------------------------------------
//
// ---
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
  if( reason == DLL_PROCESS_DETACH && aplEvent ) {
    EventListObject::TerminateEvents();
    Application->Handle = NULL;
  }
  return 1;
}



//------------------------------------------------------------------------------
//
// ---
extern "C" void far __export __pascal LibraryEntry( int comm )
{
  if ( !kompasPtr ) {
   kompasPtr = IDispatchPtr( CreateKompasObject(), false/*AddRef*/ );
   if ( kompasPtr )
     Application->Handle = (HWND)(kompasPtr->ksGetHWindow());
  }  
  if ( kompasPtr ) {
    switch ( comm ) {

      case 10: {
        if ( !aplEvent ) {
          aplEvent = new ApplicationEvent( kompasPtr );
          if ( !aplEvent->Advise() ) {
            delete aplEvent;
            aplEvent = NULL;
            LibMessage( " " );
          }
          else
            LibMessage( "" );
        }
        else
          LibMessage( " " );
        break;
      }

      case 11: {
        if ( aplEvent ) {
          delete aplEvent;
          aplEvent = NULL;
          LibMessage( "" );
        }
        else
          LibMessage( "  " );
        break;
      }

      case 20: {
        IUnknownPtr doc( kompasPtr->ksGetDocumentByReference(0) );
//        long docType = kompasPtr->ksGetDocumentType( 0 );
  			if ( doc ) {
          if ( !BaseEvent::FindEvent( DIID_ksDocumentFileNotify, (IDispatch*)(IUnknown*) doc ) ) {
   				  if ( NewDocumentEvent( doc ) )
						  LibMessage( "   " );
					  else
						  LibMessage( "  " );
				  }
				  else
  				  LibMessage( " " );
        }
			  else
				  LibMessage( "  " );
      }

      case 21: {
        IUnknownPtr doc( kompasPtr->ksGetDocumentByReference( 0 ) );
	  		if ( doc ) {
		  		BaseEvent::TerminateEvents( GUID_NULL, (IDispatch*)(IUnknown*) doc );
			  	if ( !BaseEvent::FindEvent( GUID_NULL, (IDispatch*)(IUnknown*) doc ) )
				    LibMessage( "      " );
				  else
            LibMessage( "        " );
        }
	  		else
		  		LibMessage( "  " );
	  		break;
      }
    }
  }
}


//-----------------------------------------------------------------------------
//      
// ---
extern "C" bool far __export WINAPI LibInterfaceNotifyEntry( IDispatch *application )
{
  KompasObject * kompas = (KompasObject*)application;
  kompasPtr = kompas;
  if ( kompas )
    Application->Handle = (HWND)kompas->ksGetHWindow();
  /*
  if( kompasPtr )
  {
    aplEvent = new ApplicationEvent( kompasPtr ); //    

    ksIteratorPtr iIterator = kompasPtr->GetIterator(); //  

    //      
  	if ( iIterator->ksCreateIterator( ALL_DOCUMENTS, 0 ) )
    {
		  reference rDocument = iIterator->ksMoveIterator( StringToOleStr("F") ); //  

      while ( rDocument )
      {
        //    
        AdviseDoc( rDocument );
  	 	  rDocument = iIterator->ksMoveIterator( StringToOleStr("N") ); //  
	    }

    	iIterator->ksDeleteIterator(); //  
	  }
  }
  */
  return false;
}
