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

#include "stdafx.h"
#include "step8.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.
//

/////////////////////////////////////////////////////////////////////////////
// CStep8App

BEGIN_MESSAGE_MAP(CStep8App, CWinApp)
	//{{AFX_MSG_MAP(CStep8App)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CStep8App construction

CStep8App::CStep8App()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CStep8App object

CStep8App theApp;

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

void ShowColumns( ksDynamicArray& pCol, bool fl );
void FuncAttrType( ksDocument2D& doc, ksAttributeObject& attr );
void DelTypeAttr( ksDocument2D& doc, ksAttributeObject& attr );
void ShowTypeAttr( ksDocument2D& doc, ksAttributeObject& attr );
void ChangeType( ksDocument2D& doc, ksAttributeObject& attr );
void NewAttr( ksDocument2D& doc, ksAttributeObject& attr );
void DelObjAttr( ksDocument2D& doc, ksAttributeObject& attr );
void ReadObjAttr( ksDocument2D& doc, ksAttributeObject& attr );
void ShowObjAttr( ksDocument2D& doc, ksAttributeObject& attr );
void ShowLib( ksDocument2D& doc, ksAttributeObject& attr );
void ShowType( ksDocument2D& doc, ksAttributeObject& attr );
void WalkFromObjWithAttr( ksDocument2D& doc, ksAttributeObject& attr );

KompasObject kompas( NULL );

//-------------------------------------------------------------------------------
//
// ---
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();
	if ( kompas.m_lpDispatch ) {
		ksDocument2D doc( kompas.ActiveDocument2D() );
		ksAttributeObject attr( kompas.GetAttributeObject() );
		if ( doc.m_lpDispatch && doc.GetReference() && attr.m_lpDispatch ) {
			switch ( comm ) {
			case 1  : ::FuncAttrType( doc, attr );        break; //   
			case 2  : ::DelTypeAttr( doc, attr );         break; //    
			case 3  : ::ShowTypeAttr( doc, attr );        break; //    
			case 4  : ::ChangeType( doc, attr );          break; //    
			case 5  : ::NewAttr( doc, attr );             break; //    
			case 6  : ::DelObjAttr( doc, attr );          break; //  
			case 7  : ::ReadObjAttr( doc, attr );         break; //  
			case 8  : ::ShowObjAttr( doc, attr );         break; //  
			case 9  : ::ShowLib( doc, attr );             break; //  
			case 10 : ::ShowType( doc, attr );            break; //  
			case 11 : ::WalkFromObjWithAttr( doc, attr ); break; //  
			}
		}
	}
}


//-------------------------------------------------------------------------------
//
// ---
void ShowCol( ksColumnInfoParam& par, int iCol, bool fl ) {
  char buf[128];
  char s[10];
  if ( fl )
    ::strcpy( s, "" );
  else
    s[0]='\0';

  //    
  ::sprintf( buf, "%s i=%d header=%s type=%d def=%s flagEnum=%d", s, iCol, par.GetHeader(),
             par.GetType(), par.GetDef(), par.GetFlagEnum() );
  kompas.ksMessage( buf );
  if( par.GetType() == RECORD_ATTR_TYPE )  { // 
		ksDynamicArray pCol( par.GetColumns() );
		if ( pCol.m_lpDispatch ) {
			::ShowColumns( pCol, true );
			pCol.ksDeleteArray();
		}
	}
/*
  else {
    if ( par.GetFlagEnum() ) { //  
			ksDynamicArray arr( par.GetFieldEnum() );
      int n1 = arr.ksGetArrayCount();
      kompas.ksMessage( " " );
      for ( int i1 = 0; i1 < n1; i1++ ) {
        if ( arr.ksGetArrayItem( i1, &buf, sizeof(buf) ))
          kompas.ksMessageBoxResult();  //     
        else {
          kompas.ksMessage( buf );
        }
      }
    }
  }
*/
}

//-------------------------------------------------------------------------------
//
// ---
void ShowColumns( ksDynamicArray& pCol, bool fl ) {
  ksColumnInfoParam par( kompas.GetParamStruct(ko_ColumnInfoParam) );
	if ( par.m_lpDispatch ) {
		par.Init();
		int n = pCol.ksGetArrayCount();

	  for ( int i = 0; i < n; i++ ) {
		  if ( !pCol.ksGetArrayItem(i, par) )
			  kompas.ksMessageBoxResult();  //     
			else
				::ShowCol(par,i, fl );
		}
	}
}

//-------------------------------------------------------------------------------
//   
// ---
void FuncAttrType( ksDocument2D& doc, ksAttributeObject& attr ) {
	ksAttributeTypeParam type( kompas.GetParamStruct(ko_AttributeType)   );
	ksColumnInfoParam    col ( kompas.GetParamStruct(ko_ColumnInfoParam) );
	if ( type.m_lpDispatch && col.m_lpDispatch ) {
		type.Init();
		col.Init();
		type.SetHeader( CString("double_str_long") ); // o- 
		type.SetRowsCount( 1 );												// -   
		type.SetFlagVisible( true );                  // ,     
		type.SetPassword( CString("") );              // ,      -     
		type.SetKey1( 10 );
		type.SetKey2( 20 );
		type.SetKey3( 30 );
		type.SetKey4( 0  );
		ksDynamicArray arr( type.GetColumns() );
		if ( arr.m_lpDispatch ) {
 	    //    
			col.SetHeader( CString("double") );  // o- 
			col.SetType( DOUBLE_ATTR_TYPE );    //     - .
			col.SetKey( 0 );                    //  ,        
			col.SetDef( CString("123456789") );    //   
			col.SetFlagEnum( false );                 //   ,    
			arr.ksAddArrayItem( -1, col );

		  //   
			col.SetHeader( CString("str") );  // o- 
			col.SetType( STRING_ATTR_TYPE );    //     - .
			col.SetKey( 0 );                    //  ,        
			col.SetDef( CString("string") );    //   
			col.SetFlagEnum( false );                 //   ,    
			arr.ksAddArrayItem( -1, col );

			//   
			col.SetHeader( CString("long") );  // o- 
			col.SetType( LINT_ATTR_TYPE );    //     - .
			col.SetKey( 0 );                    //  ,        
			col.SetDef( CString("1000000") );    //   
			col.SetFlagEnum( false );                 //   ,    
			arr.ksAddArrayItem( -1, col );
		}
		CString nameFile( "" );
		//  
		nameFile = kompas.ksChoiceFile( CString("*.lat"), NULL, false );
		//  
		double  numbType = attr.ksCreateAttrType( type,   //    
                                                nameFile ); //    
	  if ( numbType > 1 )  {
			char buf[128];
			::sprintf( buf, "numbType=%f ",numbType );
			kompas.ksMessage( buf );
		}
		else
			kompas.ksMessageBoxResult();  //     

		//   
		arr.ksDeleteArray();
	}
}

//-------------------------------------------------------------------------------
//
// ---
void DelTypeAttr( ksDocument2D& doc, ksAttributeObject& attr ) {
	double numb;
	int j;
	CString password( "" );
	//  
	CString nameFile( "" );
	nameFile = kompas.ksChoiceFile( CString("*.lat"),NULL, false );
  do {
    j = kompas.ksReadDouble( CString("   "), 1000., 0, 1e12, &numb );
		if ( j ) {
			password = kompas.ksReadString( "   ", "" );
			if( !attr.ksDeleteAttrType( numb, nameFile, password ) )
				kompas.ksMessageBoxResult();  //     
		}
	} while( j );
}

//--------------------------------------------------------------------------
//   
//--------------------------------------------------------------------------
void ShowTypeAttr( ksDocument2D& doc, ksAttributeObject& attr ) {
  double numb;
	//  
	CString nameFile( "" );
	nameFile = kompas.ksChoiceFile( CString("*.lat"),NULL, false );

	ksAttributeTypeParam type( kompas.GetParamStruct(ko_AttributeType)   );
	if ( type.m_lpDispatch ) {
		type.Init();
		char buf[128];
//		attrType.columns = CreateArray( ATTR_COLUMN_ARR,0 );

		do {
			numb = attr.ksChoiceAttrTypes( nameFile );
			if ( numb ) {
				if ( !attr.ksGetAttrType(numb, nameFile, type) )
					kompas.ksMessageBoxResult();  //     
				else {
					::sprintf( buf,"key1 = %d key2 =%d key3 = %d key4 =%d",
										 type.GetKey1(), type.GetKey2(), type.GetKey3(), type.GetKey4() );
					kompas.ksMessage( buf );
					::sprintf( buf,"header=%s rowsCount=%d flagVisible=%d password=%s",
										 type.GetHeader(), type.GetRowsCount(), type.GetFlagVisible(), 
										 type.GetPassword() );
					kompas.ksMessage( buf );
					ksDynamicArray pCol( type.GetColumns() );
					if ( pCol.m_lpDispatch ) {
						::ShowColumns( pCol, false );
						pCol.ksDeleteArray();
					}
//					ShowColumns( attrType.columns, FALSE ); // 
				}
			}
		} while( numb );
		//   
//		DeleteArray( attrType.columns );
	}
}

//--------------------------------------------------------------------------
//   
//--------------------------------------------------------------------------
void ChangeType( ksDocument2D& doc, ksAttributeObject& attr ) {
  double numb;
	CString password( "" );
	//  
	CString nameFile( "" );
	nameFile = kompas.ksChoiceFile( CString("*.lat"),NULL, false );
  int j;

	ksAttributeTypeParam type( kompas.GetParamStruct(ko_AttributeType)   );
	if ( type.m_lpDispatch ) {
		type.Init();
	  do {
	    j = kompas.ksReadDouble( CString("   "), 1000., 0, 1e12, &numb );
			if ( j ) {
				password = kompas.ksReadString( "   ", "" );
				//  
				if ( !attr.ksGetAttrType(numb, nameFile, type) )
					kompas.ksMessageBoxResult();  //     
				else {
					type.SetPassword( password );
					ksDynamicArray arr( type.GetColumns() );
					ksColumnInfoParam par1( kompas.GetParamStruct(ko_ColumnInfoParam) );
					ksColumnInfoParam parN( kompas.GetParamStruct(ko_ColumnInfoParam) );
					if ( arr.m_lpDispatch && par1.m_lpDispatch && parN.m_lpDispatch ) {
						par1.Init();
						parN.Init();
						//  
						int n = arr.ksGetArrayCount();
						//  
						arr.ksGetArrayItem( 0, par1 );
						//  
						arr.ksGetArrayItem( n-1, parN );
						//  
						arr.ksSetArrayItem( 0, parN );
						//  
						arr.ksSetArrayItem( n-1, par1 );

						//      
						double numbType = attr.ksSetAttrType( numb, nameFile, type, password );
						if ( numbType > 1 )  {
							char buf[128];
							::sprintf( buf, "numbType=%f ", numbType );
							kompas.ksMessage( buf );
						}
						else
							kompas.ksMessageBoxResult();  //   -     
						arr.ksDeleteArray();
					}
	      }
		  }
		} while( j );
	}
}


//----------------------------------------------------------------------------
//  ,   FuncTypeAttr
//----------------------------------------------------------------------------
void NewAttr( ksDocument2D& doc, ksAttributeObject& attr ) {
	ksAttributeParam attrPar( kompas.GetParamStruct(ko_Attribute)   );
	ksUserParam      usPar  ( kompas.GetParamStruct(ko_UserParam) ); 
	ksDynamicArray   fVisibl( kompas.GetDynamicArray(23) );
	ksDynamicArray   colKeys( kompas.GetDynamicArray(23) );
	if ( attrPar.m_lpDispatch && usPar.m_lpDispatch && fVisibl.m_lpDispatch 
				&& colKeys.m_lpDispatch ) {
		attrPar.Init();
		usPar.Init();
		attrPar.SetValues( usPar );
		attrPar.SetColumnKeys( colKeys );
		attrPar.SetFlagVisible( fVisibl );
		attrPar.SetKey1( 1 );
		attrPar.SetKey2( 10 );
		attrPar.SetKey3( 100 );
		attrPar.SetPassword( CString("111") );

		ksLtVariant item ( kompas.GetParamStruct(ko_LtVariant) );
		ksDynamicArray arr( kompas.GetDynamicArray(23) );
		if ( item.m_lpDispatch && arr.m_lpDispatch ) {
			usPar.SetUserArray( arr );
			item.Init();
			item.SetDoubleVal( 987654321.0 );
			arr.ksAddArrayItem( -1, item );
			item.Init();
			item.SetStrVal( CString("qwerty") );
			arr.ksAddArrayItem( -1, item );
			item.Init();
			item.SetLongVal( 999991 );
			arr.ksAddArrayItem( -1, item );

			item.Init();
			item.SetUCharVal( 1 );
			fVisibl.ksAddArrayItem( -1, item );
			fVisibl.ksAddArrayItem( -1, item );
			fVisibl.ksAddArrayItem( -1, item );
		}

		ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
		if ( info.m_lpDispatch ) {
			info.Init();
			CString prompt( " " );
			info.SetPrompt( prompt );
			double x, y;
			int j = doc.ksCursor( info, &x, &y, NULL );
			if ( j ) {
				reference pObj = doc.ksFindObj( x, y, 1e6 );
				if( doc.ksExistObj(pObj) ) {
					doc.ksLightObj( pObj, 1 );
					//  
					CString nameFile( "" );
					nameFile = kompas.ksChoiceFile( CString("*.lat"),NULL, false );
			    double numb;
				  j = kompas.ksReadDouble( CString("   "), 1000., 0, 1e12, &numb );
					if( j ) {
						reference pAttr = attr.ksCreateAttr( pObj, attrPar, numb, nameFile );
		        if( !pAttr )
			        kompas.ksMessageBoxResult();  //   -     
				  }
					doc.ksLightObj( pObj, 0 );
				}
	    }
		}
	}
}

//--------------------------------------------------------------------------
//       
//---------------------------------------------------------------------------
void DelObjAttr( ksDocument2D& doc, ksAttributeObject& attr ) {
	ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
	if ( info.m_lpDispatch ) {
		info.Init();
		CString prompt( " " );
		info.SetPrompt( prompt );
		double x, y;
		int j;
		do {
			j = doc.ksCursor( info, &x, &y, NULL );
			if ( j ) {
				reference pObj = doc.ksFindObj( x, y, 1e6 );
				if ( doc.ksExistObj(pObj) ) {
					doc.ksLightObj( pObj, 1 );
	        //      
					ksIterator iter( kompas.GetIterator() );
					if ( iter.m_lpDispatch && iter.ksCreateAttrIterator(pObj, 0, 0, 0, 0, 0) ) {
		        //   
						reference pAttr = iter.ksMoveAttrIterator( CString("F"), 0 );	
						if ( pAttr ) {
							CString password( kompas.ksReadString("   ", "") );
							if ( !attr.ksDeleteAttr(pObj, pAttr, password) ) 
								kompas.ksMessageBoxResult();
						}
						else 
							kompas.ksMessage( "  " );
						doc.ksLightObj( pObj, 0 );
					}
				}
			}
		} while ( j );
	}
}

//--------------------------------------------------------------------------
//      double_str_long
//---------------------------------------------------------------------------
void ReadObjAttr( ksDocument2D& doc, ksAttributeObject& attr ) {
	bool res = false;
	ksUserParam      usPar  ( kompas.GetParamStruct(ko_UserParam) ); 
	if ( usPar.m_lpDispatch ) {
		usPar.Init();
		ksLtVariant item ( kompas.GetParamStruct(ko_LtVariant) );
		ksDynamicArray arr( kompas.GetDynamicArray(23) );
		if ( item.m_lpDispatch && arr.m_lpDispatch ) {
			usPar.SetUserArray( arr );
			item.Init();
			item.SetDoubleVal( 987654321.0 );
			arr.ksAddArrayItem( -1, item );
			item.Init();
			item.SetStrVal( CString("qwerty") );
			arr.ksAddArrayItem( -1, item );
			item.Init();
			item.SetLongVal( 999991 );
			arr.ksAddArrayItem( -1, item );
			res = true;
		}
	}
	if ( res ) {
		ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
		if ( info.m_lpDispatch ) {
			info.Init();
			CString prompt( " " );
			info.SetPrompt( prompt );
			double x, y;
			int j;
			do {
				j = doc.ksCursor( info, &x, &y, NULL );
				if ( j ) {
					reference pObj = doc.ksFindObj( x, y, 1e6 );
					if ( doc.ksExistObj(pObj) ) {
						doc.ksLightObj( pObj, 1 );
		        //      
						ksIterator iter( kompas.GetIterator() );
						if ( iter.m_lpDispatch && iter.ksCreateAttrIterator(pObj, 0, 0, 0, 0, 0) ) {
				      //   
							reference pAttr = iter.ksMoveAttrIterator( CString("F"), 0 );	
							if ( pAttr ) {
				        kompas.ksMessage( "   " );
						    long k1,k2,k3,k4;
			          double numb;
						    attr.ksGetAttrKeysInfo( pAttr, &k1,&k2,&k3,&k4,&numb );
								char buf[255];
								::sprintf( buf, "k1=%d k2=%d k3=%d k4=%d numb=%f ", k1, k2, k3, k4, numb );
							  kompas.ksMessage( buf );

								kompas.ksMessage( " " );
								attr.ksGetAttrRow( pAttr, 0, 0, 0, usPar );

								kompas.ksMessage("  ");
								ksLtVariant item ( kompas.GetParamStruct(ko_LtVariant) );
								ksDynamicArray arr( usPar.GetUserArray() );
								if ( item.m_lpDispatch && arr.m_lpDispatch ) {
									item.Init();
									item.SetDoubleVal( numb );
									arr.ksSetArrayItem( 0, item );
									item.Init();
									item.SetStrVal( CString("1234567\nasdfgh\nzxcvb") );
									arr.ksSetArrayItem( 1, item );
									item.Init();
									item.SetLongVal( 888881 );
									arr.ksSetArrayItem( 2, item );
									attr.ksSetAttrRow( pAttr, 0, 0, 0, usPar, CString("111") );
								}
							}
							else 
								kompas.ksMessage( "  " );
						}
						doc.ksLightObj( pObj, 0 );
					}
				}
			} while ( j );
		}
	}
}

//--------------------------------------------------------------------------
//   
//---------------------------------------------------------------------------
void ShowObjAttr( ksDocument2D& doc, ksAttributeObject& attr ) {
	ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
	if ( info.m_lpDispatch ) {
		info.Init();
		CString prompt( " " );
		info.SetPrompt( prompt );
		double x, y;
		int j;
		do {
			j = doc.ksCursor( info, &x, &y, NULL );
			if ( j ) {
				reference pObj = doc.ksFindObj( x, y, 1e6 );
				if ( doc.ksExistObj(pObj) ) {
					doc.ksLightObj( pObj, 1 );
	        attr.ksChoiceAttr( pObj );
					doc.ksLightObj( pObj, 0 );
				}
			}
		} while ( j );
	}
}

//-------------------------------------------------------------------------------
//
// ---
void ShowLib( ksDocument2D& doc, ksAttributeObject& attr ) {
	//  
	CString nameFile( "" );
	nameFile = kompas.ksChoiceFile( CString("*.lat"),NULL, false );

  double numb = attr.ksChoiceAttrTypes( nameFile );
  if ( numb > 1 )  {
    char buf[128];
    ::sprintf( buf, "numbType = %f ",numb );
    kompas.ksMessage( buf );
  }
}

//-------------------------------------------------------------------------------
//
// ---
void ShowType( ksDocument2D& doc, ksAttributeObject& attr ) {
	//  
	CString nameFile( "" );
	nameFile = kompas.ksChoiceFile( CString("*.lat"),NULL, false );
  CString password( "" );
  double numb;
  int j = kompas.ksReadDouble( CString("   "), 1000., 0, 1e12, &numb );
	if ( j ) {
		password = kompas.ksReadString( "   ", "" );
    attr.ksViewEditAttrType( nameFile, 2, numb, password );
  }
}

//---------------------------------------------------------------------------
//   ,     
// key1=10          
//---------------------------------------------------------------------------
void WalkFromObjWithAttr( ksDocument2D& doc, ksAttributeObject& attr ) {
	ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
	if ( info.m_lpDispatch ) {
		info.Init();
		CString prompt( " " );
		info.SetPrompt( prompt );
		double x, y;
		int j;
		do {
			j = doc.ksCursor( info, &x, &y, NULL );
			if ( j ) {
				reference pObj = doc.ksFindObj( x, y, 1e6 );
				if ( doc.ksExistObj(pObj) ) {
		      //        10
					ksIterator iter( kompas.GetIterator() );
					if ( iter.m_lpDispatch && iter.ksCreateAttrIterator(pObj, 0, 0, 0, 0, 0) ) {
						doc.ksLightObj( pObj, 1 );
			      //   
						reference pAttr = iter.ksMoveAttrIterator( CString("F"), 0 );	
						if ( pAttr ) {
		          do {
				        attr.ksViewEditAttr( pAttr, 1, CString("") );
						    pAttr = iter.ksMoveAttrIterator( CString("N"), 0 );
							} while( pAttr );
						}
						doc.ksLightObj( pObj, 0 );
					}
				}
			}
		} while ( j );
	}
}
