#include <vcl.h>
#pragma hdrstop
/////////////////////////////////////////////////////////////////////////////
//
//  3   Builder C++ (COM)
//
/////////////////////////////////////////////////////////////////////////////

#include <objbase.h>
#include <initguid.h> // Definitions for controlling GUID initialization
// Include after compobj.h to enable GUID initialization.  This
// must be done once per exe/dll.
// After this file, include one or more of the GUID definition files.
//

#ifndef _BASEEVENT_H
#include "BaseEvent.h"
#endif

#include <ksConstants.h>

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



extern BaseEvent* NewDocumentEvent( reference doc );

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

  return 1;
}


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

//------------------------------------------------------------------------------
//  
// ---
int LibMessage( char* str, int flags = MB_OK ) {
  int res = 0;

  if ( str && str[0] ) {                  //  
    bool enabse = ::IsEnableTaskAccess(); //  
    if ( enabse )                         //     
      ::EnableTaskAccess(0);              //  

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

    if ( enabse )                         //      
      ::EnableTaskAccess(1);              //    
  }

  return res;
}

//------------------------------------------------------------------------------
//
// ---
extern "C" void far __export __pascal LibraryEntry( int comm )
{

  switch ( comm ) {

		//     
	  case 10 : {
			if ( !BaseEvent::FindEvents( ntKompasObjectNotify ) ) {
      	ApplicationEvent * aplEvent = new ApplicationEvent();  //    
        if ( aplEvent->Advise() )            //     
					::LibMessage( "    " );
				else {
          ::LibMessage( "  ", MB_OK | MB_ICONERROR );
				  delete aplEvent;
					aplEvent = 0;
				}
			}
			else
        ::LibMessage( " " );
			break;
		}

		//     
    case 11 : {
      if ( BaseEvent::FindEvents( ntKompasObjectNotify ) ) {
        EventListObject::TerminateEvents();
				::LibMessage( "" );
      }
			else
				::LibMessage( "  " );
  		break;
    }

		//    
		case 20: {
      reference doc = ::ksGetCurrentDocument( 0 );
      if ( doc ) {
        if ( !BaseEvent::FindEvents( ntDocumentFileNotify, doc ) ) {
				  if ( NewDocumentEvent( doc ) )
						::LibMessage( "   " );
					else
						::LibMessage( "  " );
				}
				else
  				::LibMessage( " " );
      }
			else
				::LibMessage( "  " );
 			break;
		}

		//      
		case 21: {
      reference doc = ::ksGetCurrentDocument( 0 );
			if ( doc ) {
				BaseEvent::TerminateEvents( 0, doc );
				if ( !BaseEvent::FindEvents( 0, doc ) )
				  ::LibMessage( "      " );
				else
          ::LibMessage( "        " );
      }
			else
				::LibMessage( "  " );

			break;
		}

  }

}


//-----------------------------------------------------------------------------
//      
// ---
extern "C" bool far __export WINAPI LibInterfaceNotifyEntry( IDispatch *application )
{
  new ApplicationEvent(); //    
  return false;
}


