////////////////////////////////////////////////////////////////////////////////
//
// Document2DEvent -    2D 
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "eventsAuto.h"

#ifndef _DOCUMENT2DEVENT_H
#include "Document2DEvent.h"
#endif

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

////////////////////////////////////////////////////////////////////////////////
//
// Document2DEvent -    2D 
//
////////////////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------------------
//
// ---
Document2DEvent::Document2DEvent( LPUNKNOWN pObject, LPDISPATCH doc, bool selfAdvise /*true*/ )
: BaseEvent( pObject, DIID_ksDocument2DNotify, doc, -1, NULL, selfAdvise )
{
}


//-------------------------------------------------------------------------------
//
// ---
Document2DEvent::~Document2DEvent()
{
}


//-------------------------------------------------------------------------------
//
// ---
BEGIN_EVENTSINK_MAP(Document2DEvent, BaseEvent)
	ON_EVENT (Document2DEvent, (unsigned int)-1, d2BeginRebuild,        BeginRebuild,        VTS_NONE)
	ON_EVENT (Document2DEvent, (unsigned int)-1, d2Rebuild,             Rebuild,             VTS_NONE)
	ON_EVENT (Document2DEvent, (unsigned int)-1, d2BeginChoiceMaterial, BeginChoiceMaterial, VTS_NONE)
	ON_EVENT (Document2DEvent, (unsigned int)-1, d2ChoiceMaterial,      ChoiceMaterial,      VTS_BSTR VTS_R8)
	ON_EVENT (Document2DEvent, (unsigned int)-1, d2BeginInsertFragment, BeginInsertFragment, VTS_NONE)
END_EVENTSINK_MAP()


//-------------------------------------------------------------------------------
// d3BeginRebuild -   
// ---
afx_msg VARIANT_BOOL Document2DEvent::BeginRebuild()
{ 
  if ( IsSelfAdvise() && theApp.m_mes_2DDOC )
  {
    CString str( m_libName + " --> Document2DEvent::BeginRebuild" );
    str += "\n  = " + GetDocName();
    return !!kompas->ksYesNo( str.GetBuffer(0) );
  }
  return true;
}


//-------------------------------------------------------------------------------
// d3Rebuild -  
// ---
afx_msg VARIANT_BOOL Document2DEvent::Rebuild()
{   
  if ( IsSelfAdvise() && theApp.m_mes_2DDOC )
  {
    CString str( m_libName + " --> Document2DEvent::Rebuild" );
    str += "\n  = " + GetDocName();
    kompas->ksMessage( str.GetBuffer(0) );
  }
  return true;
}


//-------------------------------------------------------------------------------
// d3BeginChoiceMaterial -   
// ---
afx_msg VARIANT_BOOL Document2DEvent::BeginChoiceMaterial()
{ 
  bool res = false;
  if ( IsSelfAdvise() && theApp.m_mes_2DDOC )
  {
    CString str( m_libName + " --> Document2DEvent::BeginChoiceMaterial" );
    str += "\n  = " + GetDocName();
    res = !!kompas->ksYesNo( str.GetBuffer(0) );

    if( !res )
    {   
      ksTextLineParamPtr parLine ( kompas->GetParamStruct(ko_TextLineParam) );
      ksTextItemParamPtr parItem( kompas->GetParamStruct(ko_TextItemParam) );

      // C   
      ksDynamicArrayPtr arMaterial( kompas->GetDynamicArray(TEXT_LINE_ARR) );
      if( (bool)arMaterial && (bool)parLine && (bool)parItem )
      {
        parLine->Init();
	      parItem->Init();

	      //    
	      ksDynamicArrayPtr item( parLine->GetTextItemArr() );
	      if ( item ) 
        {
		      ksTextItemFontPtr font( parItem->GetItemFont() );
		      if ( font ) 
          {
				    //    
				    font->height = 10;   //  
				    font->ksu = 1;       //  
				    font->color = 1000;  // 
				    font->bitVector = 1; //   (, , ,   (, ,   ))
				    parItem->s = "(1- )";
				    //  1-     
				    item->ksAddArrayItem( -1, parItem );

				    font->height = 20;   //  
				    font->ksu = 2;       //  
				    font->color = 2000;  // 
				    font->bitVector = 2; //   (, , ,   (, ,   ))
				    parItem->s = "(2- )";
				    //  2-     
				    item->ksAddArrayItem( -1, parItem );
				    
            parLine->style = 1;

				    // 1-        
            //      
				    arMaterial->ksAddArrayItem( -1, parLine );

            ksDocument2DPtr doc2D;
            m_doc->QueryInterface( DIID_ksDocument2D, (LPVOID*)&doc2D );
            if( doc2D ) 
            {
              doc2D->ksSetMaterialParam( arMaterial, 36.6 );
            }
				  }
        }
      }
    }
  }
  return res;
}


//-------------------------------------------------------------------------------
// d3hoiceMaterial -   
// ---
afx_msg VARIANT_BOOL Document2DEvent::ChoiceMaterial( LPCTSTR material, double density )
{
  if ( IsSelfAdvise() && theApp.m_mes_2DDOC )
  {
    CString str;
    str.Format( "%s --> Document2DEvent::ChoiceMaterial\nmaterial = %s\ndensity = %f", m_libName, material, density );
    str += "\n  = " + GetDocName();
    kompas->ksMessage( str.GetBuffer(0) );
  }
  return true;
}


//-------------------------------------------------------------------------------
// d2BeginInsertFragment -    (   )
// ---
afx_msg VARIANT_BOOL Document2DEvent::BeginInsertFragment()
{
  if ( IsSelfAdvise() && theApp.m_mes_2DDOC )
  {
    CString str( m_libName + " --> Document2DEvent::BeginInsertFragment" );
    str += "\n  = " + GetDocName();
    return !!kompas->ksYesNo( str.GetBuffer(0) );
  }
  return true;
}
