////////////////////////////////////////////////////////////////////////////////
//
// step12.cpp -     
// 
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h> 

#include "resource.h"

#ifndef  _CPROPMEN_H
#include "cPropMen.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 };

IApplicationPtr newKompasAPI;                                //  Application 7     
IPropertyManagerPtr propMng;                                 //    
IPropertySlideBoxPtr slideBox;                               //        

PropertyManagerLayout g_Layout = pmAlignRight;/*pmFloating*/ //  
CRect g_Rect( 20, 20, 270, 320 );                            // 

void ClosePropertyManager( bool mes );                       //     
void CreateAndSubscriptionPropertyManager( bool mes );       //   

//      
extern cBaseEvent* NewApplicationEvent();
extern cBaseEvent* NewDocumentEvent( reference doc );
extern cBaseEvent* NewDocument2DEvent( reference doc );
extern cBaseEvent* NewDocument3DEvent( reference doc );
extern cBaseEvent* NewObj2DEvent( reference doc, int objType );
extern cBaseEvent* NewObj3DEvent( reference doc, int objType, LPUNKNOWN iObj = NULL );
extern cBaseEvent* NewSpcDocEvent( reference doc );
extern cBaseEvent* NewSpcObjectEvent( reference doc, int objType );
extern cBaseEvent* NewSpecificationEvent( reference doc );
extern cBaseEvent* NewStampEvent( reference doc );
void AdviseDoc( long pDoc );

//-------------------------------------------------------------------------------
//   
//    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 )
  {
    //   
    ABaseEvent::TerminateEvents();
    cBaseEvent::TerminateEvents();
    //  
    ClosePropertyManager( false/* mes*/ );
    //   
    ABaseEvent::DestroyList();
    cBaseEvent::DestroyList();

    newKompasAPI = NULL;
    TRACE0( "DLL Terminating!" );
    AfxTermExtensionModule( StepDLL );
  }
  return 1;
}


//-------------------------------------------------------------------------------
//     API
// ---
void GetNewKompasAPI() {
	if ( !( IApplication* )newKompasAPI ) {
		CString filename;
		if( ::GetModuleFileName(NULL, filename.GetBuffer(255), 255) ) {
			filename.ReleaseBuffer( 255 );
			CString libname;

      #ifdef __LIGHT_VERSION__
        libname.LoadString( IDS_API7LT );  // klAPI7.dll
      #else
        libname.LoadString( IDS_API7 );    // kAPI7.dll
      #endif

			filename.Replace( filename.Right(filename.GetLength() - (filename.ReverseFind(_T('\\')) + 1)), 
												libname );

			HINSTANCE hAppAuto = LoadLibrary( filename ); //  kAPI7.dll
			if(  hAppAuto ) {
				//      KompasApplication  
        typedef LPDISPATCH ( WINAPI *FCreateKompasApplication )(); 
				 
        FCreateKompasApplication pCreateKompasApplication = 
					(FCreateKompasApplication)GetProcAddress( hAppAuto, "CreateKompasApplication" );	
				if ( pCreateKompasApplication )
					newKompasAPI = IDispatchPtr( pCreateKompasApplication(), false/*AddRef*/ ); 
				FreeLibrary( hAppAuto );  
			}
		}
	}
}


//----------------------------------------------------------------------------------------------
//    
//---
TCHAR * LoadStr( int ID ) {
  static TCHAR buf[255];
  ksConvertLangStrExT( StepDLL.hModule, ID, buf, 255 );
  return buf;
}


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

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

    //                                      
    res = MessageBox( (HWND) GetHWindow(), str, LoadStr(IDR_LIB), flags );

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

  return res;
}


//------------------------------------------------------------------------------
//  
// ---
int LibMessage( int strId, int flags = MB_OK ) {
  return ::LibMessage( LoadStr(strId), flags );
}


//-------------------------------------------------------------------------------
//   
// ---
int WINAPI LIBRARYID()
{
  return IDR_LIB;
}


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  //   
 	::GetNewKompasAPI(); //    
  if ( newKompasAPI ) 
  {
    switch ( comm ) 
    {       

			case 1 : //    
        CreateAndSubscriptionPropertyManager( true );
				break;
			case 2 : // 
        ClosePropertyManager( true );
        //      
				break;
    }
  }
}


//-----------------------------------------------------------------------------
//      
// ---
int WINAPI LibInterfaceNotifyEntry( IDispatch *application ) 
{
  newKompasAPI = application;                           //   
  CreateAndSubscriptionPropertyManager( false/*mes*/);  //    
  return 0;
}


//-------------------------------------------------------------------------------
//   API
// ---
int WINAPI LibIsOnApplication7() 
{
  return 1;
}


//-----------------------------------------------------------------------------
//    
// ---
void CreateAndSubscriptionPropertyManager( bool mes ){
  if ( propMng == NULL ) {
    NewApplicationEvent();   
    propMng = newKompasAPI->CreatePropertyManager( TRUE/*FALSE*/ );
    propMng->Layout = g_Layout;
    propMng->Caption = _T("  ");
    propMng->SetGabaritRect( g_Rect.left, g_Rect.top, g_Rect.right, g_Rect.bottom );
    propMng->SpecToolbar = pnEnterEscCreateSaveSearchHelp;
    new PropertyManagerEvent( propMng );                 //     
    IPropertyTabPtr tab( propMng->PropertyTabs->Add(_T("  ")) );
    
    //   
    IPropertyControlsPtr collection = tab->PropertyControls;
    reference docRef = ::ksGetCurrentDocument(0);
    AdviseDoc( docRef );
    //   
    slideBox = collection->Add( ksControlSlideBox );            
    slideBox->SlideType      = ksKompasDocument;             //  
    slideBox->DrawingSlide   = docRef;                       //  reference  
    slideBox->Value              = docRef != 0;
    slideBox->CheckBoxVisibility = docRef != 0;
    slideBox->Hint           = _T("Hint  ");
    slideBox->Tips           = _T("Tips  ");
    slideBox->Id             = 10000;
    slideBox->Name           = _T(" ");
    slideBox->NameVisibility = ksNameHorizontalVisible;
    
    propMng->ShowTabs();
    propMng->Caption = _T("  1");
//    propMng->ShowTabs();
//    propMng->UpdateTabs();

  }
  else {
    propMng->Visible = true; //         
    if( mes ) {
      LibMessage( _T("  ") );
    }
  }
}
 

//-----------------------------------------------------------------------------
//        
// ---
void  ClosePropertyManager( bool mes ){
  if ( propMng ) {
  
    g_Layout = propMng->Layout;
    propMng->GetGabaritRect( &g_Rect.left, &g_Rect.top, &g_Rect.right, &g_Rect.bottom );
    ABaseEvent::TerminateEvents(DIID_ksPropertyManagerNotify);
    propMng->HideTabs();
    slideBox = NULL; 
    propMng = NULL;

  }
  else {
    if( mes )
      LibMessage( _T("  ") );
  }
}

//-----------------------------------------------------------------------------
//  
// ---
void UpdateSlideBox() 
{
  if ( slideBox )
    slideBox->UpdateParam();
}


//-----------------------------------------------------------------------------
//  
// ---
void UpdateSlideBox( reference docRef ) 
{
  if ( slideBox ) {
    slideBox->DrawingSlide       = docRef;
    slideBox->Value              = docRef != 0;
    slideBox->CheckBoxVisibility = docRef != 0;
    slideBox->UpdateParam();
  }
}


//-------------------------------------------------------------------------------
//      
// ---
int WINAPI LibToolBarId( int barType, //    ( 0 -  , 1 -    ) 
                         int index )  //  
{
	if ( !barType )
    return !index ? 2000 : -1;
	else 
		return -1; 
}


//-------------------------------------------------------------------------------
//    
// ---
void AdviseDoc( long pDoc ) {

	if ( !pDoc )
		pDoc = ::ksGetCurrentDocument( 0 );
	else
    ::SetObjParam(pDoc, NULL, 0, DOCUMENT_STATE);

	if ( pDoc ) {
		int docType = ::ksGetDocumentType( pDoc );
		if ( NewDocumentEvent( pDoc ) ) {
			switch( docType ) {
		  	case lt_DocSheetStandart :
			  case lt_DocSheetUser     :
					NewStampEvent( pDoc ); 
			  case lt_DocFragment      :
					NewDocument2DEvent( pDoc );
				  NewObj2DEvent( pDoc, ALL_OBJ );
				  break;
			  case lt_DocPart3D :
			  case lt_DocAssemble3D : {
  		    NewDocument3DEvent(pDoc);
				  NewObj3DEvent( pDoc, o3d_unknown, NULL ); //  
					break;
				}
				case lt_DocSpc:
				case lt_DocSpcUser: {
					NewSpcDocEvent( pDoc );
					NewStampEvent( pDoc ); 
					break;
				}	
				case lt_DocTxtStandart   :
				case lt_DocTxtUser       : 
					NewStampEvent( pDoc );
					break;
      }    

			switch ( docType ) {
		    case lt_DocSheetStandart:
				case lt_DocSheetUser:
				case lt_DocSpc:
        case lt_DocSpcUser:
				case lt_DocAssemble3D: {
          NewSpecificationEvent( pDoc );
					NewSpcObjectEvent( pDoc, SPC_BASE_OBJECT /*  */ );
				}
			}
		}
	}
}

