   // step11.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "step11.h"
#include <ldefin2d.h>

#ifdef __LIGHT_VERSION__
#include <klAPI5.h>
#else
#include <kAPI5.h>
#endif

#include <ksConstants.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CStep11App

BEGIN_MESSAGE_MAP(CStep11App, CWinApp)
	//{{AFX_MSG_MAP(CStep11App)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStep11App construction

CStep11App::CStep11App()
{
};

CStep11App step11App;
//CComModule _Module;

KompasObject kompas( NULL );

//------------------------------------------------------------------------------
//   
// 0 - , 1 - , 2 -  -  collection
// ---
int WINAPI CALLBACKPROCCOMMANDWINDOW( int comm, LPDISPATCH rInfo ) {
  char buf[10];
  ::itoa( comm, buf, 10 );
	kompas.ksMessage(" ");
  //        
  //       
	ksRequestInfo info( NULL );
  info.AttachDispatch( rInfo, false/*AutoRelease*/ );
	if ( info.m_lpDispatch ) {
		switch ( comm ) {
			case ID_COMMAND_1 : 
				info.SetTitle( "1" ); break;
			default : info.SetTitle("2");
		}
	}
  //   ,    
  //   :
  // TRUE - 
  // FALSE -    
  return comm == ID_COMMAND_2_2_1_3 ? FALSE : TRUE;
}

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


//-------------------------------------------------------------------------------
//
// ---
void GetKompas() {
	if ( !kompas.m_lpDispatch ) {
		CString filename;
		if( ::GetModuleFileName(NULL, filename.GetBuffer(255), 255) ) {
			filename.ReleaseBuffer( 255 );
			CString libname;
			
      #ifdef __LIGHT_VERSION__
        libname.LoadString( IDS_STRING5 );  //klAPI5.dll
      #else
        libname.LoadString( IDS_STRING4 );  //kAPI5.dll
      #endif

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

			HINSTANCE hAppAuto = LoadLibrary( filename ); //  kAPI5.dll
			if(  hAppAuto ) {
				typedef LPDISPATCH ( WINAPI *FCreateKompasObject )(); 
				FCreateKompasObject pCreateKompasObject = 
					(FCreateKompasObject)GetProcAddress( hAppAuto, "CreateKompasObject" );	
				if ( pCreateKompasObject ) 
					kompas = pCreateKompasObject();
				FreeLibrary( hAppAuto );
			}
		}
	}
}

//-------------------------------------------------------------------------------
//
// ---
void WINAPI LIBRARYENTRY( unsigned int comm ) {
	//      .exe ,     
	//   dll   define
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	GetKompas();
  ksDocument2D doc( kompas.Document2D() );
 	ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
	if ( kompas.m_lpDispatch && doc.m_lpDispatch && info.m_lpDispatch ) {
	  info.Init();

    info.SetMenuId(ID_TREE_CONTENTS);

    info.SetTitle(" ");

		HINSTANCE hInst = step11App.m_hInstance;
  	info.SetCallBackCm( "CALLBACKPROCCOMMANDWINDOW", (long)hInst, 0 );
    int command = doc.ksCommandWindow( info );
    char buf[10];
    ::itoa( command, buf, 10 );
    kompas.ksMessage(" ");
	}
}
