////////////////////////////////////////////////////////////////////////////////
//
//      
// ILibHPObject -   
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h> 
#include "resource.h"

#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 __LIBHPPAR_H
#include "libhppar.h"
#endif

#ifndef _ABASEEVENT_H
#include "abaseEvent.h"
#endif

#ifndef __CPAR_H
#include "CPar.h"
#endif

#if !defined(_IFUNC)
# define _IFUNC STDMETHODCALLTYPE
#endif

//-------------------------------------------------------------------------------
// EXTENSION 
// ---
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 )
  {
    App7NULL();
    TRACE0( "DLL Terminating!" );
    AfxTermExtensionModule( StepDLL );
  }
  return 1;
}

//-------------------------------------------------------------------------------
//   
// ---
unsigned int WINAPI LIBRARYID()
{
  return ID_LIBID;
}


//-------------------------------------------------------------------------------
//    
// ---
CPar* NewGayka5915( reference obj );  //   5915


//-------------------------------------------------------------------------------
//    
// ---
CPar* NewCParObject( int comm, reference objRef ) {
  CPar* obj = NULL;
  switch (comm) 
  {
    case CM_GAYKA5915 : obj = NewGayka5915( objRef ); break;
      //     
  }
  return obj;
}


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  if ( ksGetCurrentDocument( 1 ) ) //   2D 
  {
    //  
    CPar* obj = NewCParObject( comm, EditMacroMode() );
    if ( obj ) {
      obj->Draw1();                     //   
 	    delete obj;                       //  
    }
    try
    {
      ABaseEvent::TerminateEvents();
    }
    catch(...)
    {
    }
  }
  else 
		ErrorT( LoadStr( ID_ERROR_2DDOC ) ); //    
}

//-------------------------------------------------------------------------------
//     Hot-
// ---
class HPObj : public ILibHPObject, public ILibHPObject1  
{
  protected :
    CPar* obj;
    int   count;
  public :
    HPObj( CPar* _obj ) : count (0), obj( _obj ) {}
   ~HPObj();
  public:
    /* IUnknown */
    virtual unsigned long _IFUNC AddRef();
    virtual unsigned long _IFUNC Release();
    virtual HRESULT				_IFUNC QueryInterface(const GUID far& iid, void far*far* iface);

    /* ILibHPObject */            
    // 
    virtual int  _IFUNC LibHotPnt_GetMenu       ()                                        { return obj ? obj->GetMenu() : 0;     }
    //   
    virtual BOOL _IFUNC LibHotPnt_Prepare       ( int index )                             { return  obj ? obj->Prepare() : true;  }
    //   -
    virtual BOOL _IFUNC LibHotPnt_Complete      ( int index, BOOL success )               { return obj ? obj->EditComplete( index,  success ) : TRUE;  }
    //    
    virtual BOOL _IFUNC LibHotPnt_Get           ( HotPointDescription* point, int index ) { return obj ? obj->GetHotPoints( point,  index )   : TRUE;  }
    //     
    virtual BOOL _IFUNC LibHotPnt_Set           ( HotPointDescription* point, int index ) { return obj ? obj->SetHotPoint( point, index  )    : TRUE;  }
    //    -
    virtual BOOL _IFUNC LibHotPnt_GetCursorText ( int index, char** text )                { return obj ? obj->GetCursorText( index, text )    : FALSE; }
    //     -
    virtual BOOL _IFUNC LibHotPnt_ExecuteCommand( int id )                                { return obj ? obj->ExecuteCommand( id ) : FALSE; }

    /* ILibHPObject1 */            
    //    
    virtual BOOL _IFUNC LibHotPnt_GetEx         ( HotPointDescription1* point, int index ) { return obj ? obj->GetHotPointsEx( point,  index )   : TRUE;  }

    //  popup-     
    // !!!      .
    //                   DestroyMenu()
    //    -   HMENU
    virtual int _IFUNC LibHotPnt_GetMenuEx      ( int index ) { return obj ? obj->GetMenu(index) : 0;     }

  //         UNICODE  ILibHPObject::LibHotPnt_GetCursorText
    virtual LPOLESTR _IFUNC  LibHotPnt_GetCursorTextEx( int index ) { return obj ? obj->GetCursorTextEx( index ) : NULL; }

    virtual BOOL _IFUNC LibHotPnt_UpdateCommand ( int id )                                { return obj ? obj->UpdateCommand( id ) : FALSE; }

    //" hot point
    virtual BOOL _IFUNC LibHotPnt_Select        ( int index ) { return obj ? obj->SelectHotPoint( index )   : TRUE;  }

    //" hot point
    virtual BOOL _IFUNC LibHotPnt_Unselect      ( int index ) { return obj ? obj->UnselectHotPoint( index )   : TRUE;  }
};

//------------------------------------------------------------------------------
// AddRef
// ---
unsigned long _IFUNC HPObj::AddRef() 
{
  count++;
  return count;
}

//------------------------------------------------------------------------------
// Release
// ---
unsigned long _IFUNC HPObj::Release() 
{
  if ( count ) 
    count--;
  if( !count )
    delete this;
  return count;
}

//------------------------------------------------------------------------------
// QueryInterface
// ---
HRESULT _IFUNC HPObj::QueryInterface( const GUID far& iid, void far*far* iface )
{
  if ( iid == IID_ILibHPObject ) 
  {
    *iface = static_cast<ILibHPObject*>(this);
    AddRef();
    return S_OK;
  }
  if ( iid == IID_ILibHPObject1 ) 
  {
    *iface = static_cast<ILibHPObject1*>(this);
    AddRef();
    return S_OK;
  }

  *iface = NULL;
  return E_NOINTERFACE;
}

//-------------------------------------------------------------------------------
// 
// ---
HPObj::~HPObj() 
{
  //    
  //     
  if ( obj )       
    delete obj;
}   

//-------------------------------------------------------------------------------
//       Hot 
// ---
void WINAPI LibObjInterfaceEntry( int idType, unsigned int comm, ILibHPObject** object ) 
{
  if ( object ) 
  {
    if ( idType == 1 ) { //   Hot  
      CPar* obj = NewCParObject( comm, EditMacroMode() );
      if ( obj ) {  
        *object = new HPObj( obj );
        (*object)->AddRef(); 
      } 
    }
  }
}  
