////////////////////////////////////////////////////////////////////////////////
//
// 
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "eventCom.h"

#ifndef _DOCUMENTEVENT_H
#include "DocumentEvent.h"
#endif

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

extern AFX_MODULE_STATE* pModuleState;

extern BaseEvent* NewDocumentFrameEvent( reference doc, LPUNKNOWN iObj );

//-------------------------------------------------------------------------------
//    
// ---
BaseEvent* NewDocumentEvent( reference doc ) {
	DocumentEvent* res = NULL;
	if ( doc ) {
		if ( !BaseEvent::FindEvents( ntDocumentFileNotify, doc ) ) {
			res = new DocumentEvent( doc );
			if ( !res->Advise() ) { 
				delete res;
				res = NULL;
			}
		}
	}
	return res;
}

//-------------------------------------------------------------------------------
//    3D 
// ---
BaseEvent* NewDocument3DEvent( reference doc ) {
	Document3DEvent* res = NULL;
	if ( doc ) {
		if ( !BaseEvent::FindEvents( ntDocument3DNotify, doc ) ) {
			IDocument3DPtr doc3D( ksGet3dDocumentFromReference(doc), false );
			if ( doc3D ) {
				NewDocumentEvent( doc ); //     
			  res = new Document3DEvent( doc, doc3D );
			  if ( !res->Advise() ) { 
				  delete res;
				  res = NULL;
				}
			}
		}
	}
	return res;
}

//-------------------------------------------------------------------------------
//    2D 
// ---
BaseEvent* NewDocument2DEvent( reference doc ) {
	Document2DEvent* res = NULL;
	if ( doc ) {
		if ( !BaseEvent::FindEvents( ntDocument2DNotify, doc ) ) {
			NewDocumentEvent( doc ); //     
			res = new Document2DEvent( doc );
			if ( !res->Advise() ) { 
				delete res;
				res = NULL;
			}
		}
	}
	return res;
}

////////////////////////////////////////////////////////////////////////////////
//
// DocumentEvent  -    
//
////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//
// ---
DocumentEvent::DocumentEvent(reference p ):
    BaseEvent(ntDocumentFileNotify, p, 0, p )
{
}


//-------------------------------------------------------------------------------
//
// ---
DocumentEvent::~DocumentEvent()
{

}

//-----------------------------------------------------------------------------
//
// ---
LPUNKNOWN DocumentEvent::GetUnknown(){
  m_xDocumentFileNotify.AddRef();
  return &m_xDocumentFileNotify;
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) DocumentEvent::XDocumentFileNotify::Release()
{
	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
	return (ULONG)pThis->InternalRelease();
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) DocumentEvent::XDocumentFileNotify::AddRef()
{
	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
	return (ULONG)pThis->InternalAddRef();
}


//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP DocumentEvent::XDocumentFileNotify::QueryInterface(
	REFIID iid, LPVOID* ppvObj)
{
	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)

	ASSERT(AfxIsValidAddress(ppvObj, sizeof(LPVOID), FALSE));

	if ( IsEqualIID(iid, IID_IUnknown) ||
		   IsEqualIID(iid, IID_IDocumentFileNotify) )
	{
		*ppvObj = this;
		AddRef();
		return S_OK;
	}

	return E_NOINTERFACE;
}


//-----------------------------------------------------------------------------
//      notifyType
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::IsNotifyProcess( int notifyType )
{
//	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
	return  notifyType >= kdBeginCloseDocument && notifyType <= kdDocumentFrameOpen;
}


//-----------------------------------------------------------------------------
//   
// ---
void DocumentEvent::ShowEventName( LPCTSTR eventName ) {
	if ( theApp.m_mes_DOC ) {
    CString res = eventName;
	  res += GetDocumentParam( refDoc );
		::_ShowEventName( (LPTSTR)(LPCTSTR)res );
	}
}

//-----------------------------------------------------------------------------
//  
// ---
CString DocumentEvent::EventCaption() {
	CString res = _T("DocumentEvent");
	res += GetDocumentParam( refDoc );
	return res;
}


/////////////////////////////////////////////////////////////////////////////
// DocumentEvent message handlers
//-----------------------------------------------------------------------------
//kdBeginCloseDocument -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::BeginCloseDocument(){
	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName(_T("DocumentEvent::BeginCloseDocument"));

  return !theApp.m_mes_DOC || ::YesNoT( _T(" ?") ) == 1;
}


//-----------------------------------------------------------------------------
//kdCloseDocument -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::CloseDocument(){
	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName(_T("DocumentEvent::CloseDocument"));
  reference doc = pThis->GetDocReference();
	if ( doc )
		TerminateEvents( 0, doc );
  else 
    pThis->Disconnect();
  return true;
}


//-----------------------------------------------------------------------------
//kdBeginSaveDocument -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::BeginSaveDocument( LPSTR docName ){
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName(_T("DocumentEvent::BeginSaveDocument"));
  return !theApp.m_mes_DOC || ::YesNoT( _T(" ?") ) == 1;
}


//-----------------------------------------------------------------------------
//kdSaveDocument -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::SaveDocument(){
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName(_T("DocumentEvent::SaveDocument"));
  return true;
}


//-----------------------------------------------------------------------------
//kdActiveDocument -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::Activate(){
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName(_T("DocumentEvent::Activate"));
  return true;
}


//-----------------------------------------------------------------------------
//kdDeactiveDocument -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::Deactivate() {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName( _T("DocumentEvent::Deactivate"));
  return true;
}

//-----------------------------------------------------------------------------
//kdBeginSaveAsDocument -      .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::BeginSaveAsDocument() {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
//  pThis->ShowEventName( "DocumentEvent::BeginSaveAsDocument");
	bool res = !theApp.m_mes_DOC;
	if ( !res ) {
    int com = YesNoT( _T("DocumentEvent::BeginSaveAsDocument\n ")
			                _T(" -  \n") 
			                _T(" -     \n")
										  _T(" -   ") );
		switch ( com ) {
		  case 1: {
				int type = ksGetDocumentType( pThis->refDoc );
				switch ( type ) {
				  case lt_DocTxtStandart:             //               7-   
					case lt_DocTxtUser: {                //               8-   
  				  TextDocumentParam docParam;
            GetObjParam( pThis->refDoc, &docParam, sizeof(docParam), ALLPARAM );
            if ( !docParam.fileName[0] )
							res = !::SaveDocumentExT( 0, _T("C:\\1.kdw"), 0 );
						else
							res = !::SaveDocumentExT( 0, NULL, 0 );
						break;
					}
          default: {
  				  DocumentParam docParam;
            GetObjParam( pThis->refDoc, &docParam, sizeof(docParam), ALLPARAM );
            if ( !docParam.fileName[0] ) {
							switch ( type ) {
						  	case lt_DocFragment:           //               3- 
       						res = !::SaveDocumentExT( 0, _T("C:\\1.frw"), 0 );
                  break;
								case lt_DocSheetStandart:
								case lt_DocSheetUser:
                  res = !::SaveDocumentExT( 0, _T("C:\\1.cdw"), 0 );
							    break;
                case lt_DocSpc:                //               4- 
								case lt_DocSpcUser: 
                  res = !::SaveDocumentExT( 0, _T("C:\\1.spw"), 0 );
							    break;
                case lt_DocPart3D:                  //          5- 3d- 
                  res = !::SaveDocumentExT( 0, _T("C:\\1.m3d"), 0 );
							    break;
                case lt_DocAssemble3D:                  //      6- 3d- 
                  res = !::SaveDocumentExT( 0, _T("C:\\1.a3d"), 0 );
							    break;
									
							}
            }
            else 
              res = !::SaveDocumentExT( 0, NULL, 0 );
					}		 
        }
			}	
			case 0:
				res = true;
				break;
			case -1:
				res = false;
		}
	}	
  return res;
}

//-----------------------------------------------------------------------------
//kdDocumentFrameOpen    -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::DocumentFrameOpen( LPUNKNOWN v ) {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  NewDocumentFrameEvent( pThis->refDoc, v );
  pThis->ShowEventName( _T("DocumentEvent::DocumentFrameOpen"));
  return true;
}

//-----------------------------------------------------------------------------
// kdProcessActivate      -  
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::ProcessActivate( long Id ) {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName( _T("DocumentEvent::ProcessActivate"));
  return true;
}

//-----------------------------------------------------------------------------
// kdProcessDeactivate    -  
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::ProcessDeactivate( long Id ) {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  pThis->ShowEventName( _T("DocumentEvent::ProcessDeactivate"));
  return true;
}


////////////////////////////////////////////////////////////////////////////////
//
// Document2DEvent  -    2D 
//
////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//
// ---
Document2DEvent::Document2DEvent( reference p ):
  BaseEvent( ntDocument2DNotify, p, 0, p ) 
{
}


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

}

//-----------------------------------------------------------------------------
//
// ---
LPUNKNOWN Document2DEvent::GetUnknown(){
  m_xDocument2DNotify.AddRef();
  return &m_xDocument2DNotify;
}


//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) Document2DEvent::XDocument2DNotify::Release()
{
	METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
	return (ULONG)pThis->InternalRelease();
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) Document2DEvent::XDocument2DNotify::AddRef()
{
	METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
	return (ULONG)pThis->InternalAddRef();
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP Document2DEvent::XDocument2DNotify::QueryInterface(
	REFIID iid, LPVOID* ppvObj)
{
	METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)

	ASSERT(AfxIsValidAddress(ppvObj, sizeof(LPVOID), FALSE));

	if ( IsEqualIID(iid, IID_IUnknown) ||
		   IsEqualIID(iid, IID_IDocument2DNotify) )
	{
		*ppvObj = this;
		AddRef();
		return S_OK;
	}

	return E_NOINTERFACE;
}


//-----------------------------------------------------------------------------
//      notifyType
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::IsNotifyProcess( int notifyType )
{
//	METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
	return  notifyType >= d2BeginRebuild && notifyType <= d2BeginInsertFragment;
}


//-----------------------------------------------------------------------------
//   
// ---
void Document2DEvent::ShowEventName( LPCTSTR eventName ) {
	if ( theApp.m_mes_2DDOC ) {
		CString str = eventName;
		if ( refDoc ) 
      str += GetDocumentParam( refDoc );
		::_ShowEventName( str );
	}
}

//-----------------------------------------------------------------------------
//  
// ---
CString Document2DEvent::EventCaption() {
	CString res = _T("Document2DEvent");
	res += GetDocumentParam( refDoc );
	return res;
}

//-----------------------------------------------------------------------------
// d2BeginRebuild -   2D .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::BeginRebuild(){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  pThis->ShowEventName(_T("Document2DEvent::BeginRebuild"));
  return !theApp.m_mes_2DDOC || ::YesNoT(_T(" ?")) == 1;
}

//-----------------------------------------------------------------------------
// d2Rebuild -   2D .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::Rebuild() {
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  pThis->ShowEventName(_T("Document2DEvent::Rebuild"));
  return true;
}

//-----------------------------------------------------------------------------
// d2BeginChoiceMaterial -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::BeginChoiceMaterial(){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  pThis->ShowEventName(_T("Document2DEvent::BeginChoiceMaterial"));
  return !theApp.m_mes_2DDOC || ::YesNoT(_T(" ?")) == 1;
}

//-----------------------------------------------------------------------------
// dChoiceMaterial -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::ChoiceMaterial( LPSTR material, double density ){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  pThis->ShowEventName(_T("Document2DEvent::ChoiceMaterial"));
  return true;
}

//-----------------------------------------------------------------------------
// d2BeginInsertFragment -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::BeginInsertFragment(){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  pThis->ShowEventName(_T("Document2DEvent::BeginInsertFragment"));
  return !theApp.m_mes_2DDOC || ::YesNoT(_T("  ?")) == 1;
}

//-----------------------------------------------------------------------------
// d2LocalFragmentEdit -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::LocalFragmentEdit(long pDoc, VARIANT_BOOL newFrw){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  return true;
}


////////////////////////////////////////////////////////////////////////////////
//
// Document3DEvent  -    3D 
//
////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//
// ---
Document3DEvent::Document3DEvent( reference p, LPUNKNOWN doc ):
  BaseEvent( ntDocument3DNotify, doc, 0, 0, p ) 
{
}


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

//-----------------------------------------------------------------------------
//
// ---
LPUNKNOWN Document3DEvent::GetUnknown(){
  m_xDocument3DNotify.AddRef();
  return &m_xDocument3DNotify;
}


//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) Document3DEvent::XDocument3DNotify::Release()
{
	METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
	return (ULONG)pThis->InternalRelease();
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) Document3DEvent::XDocument3DNotify::AddRef()
{
	METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
	return (ULONG)pThis->InternalAddRef();
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP Document3DEvent::XDocument3DNotify::QueryInterface(
	REFIID iid, LPVOID* ppvObj)
{
	METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)

	ASSERT(AfxIsValidAddress(ppvObj, sizeof(LPVOID), FALSE));

	if ( IsEqualIID(iid, IID_IUnknown) ||
		   IsEqualIID(iid, IID_IDocument3DNotify) )
	{
		*ppvObj = this;
		AddRef();
		return S_OK;
	}

	return E_NOINTERFACE;
}

//-----------------------------------------------------------------------------
//      notifyType
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::IsNotifyProcess( int notifyType )
{
//	METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
	return  notifyType >= d3BeginRebuild && notifyType <= d3BeginCreatePartFromFile;
}


//-----------------------------------------------------------------------------
//   
// ---
void Document3DEvent::ShowEventName( LPCTSTR eventName ) {
	if ( theApp.m_mes_3DDOC ) {
		CString str = eventName;
		if ( refDoc ) 
      str += GetDocumentParam( refDoc );
		::_ShowEventName( str );
	}
}

//-----------------------------------------------------------------------------
//  
// ---
CString Document3DEvent::EventCaption() {
	CString res = _T("Document3DEvent");
	res += GetDocumentParam( refDoc );
	return res;
}


//-----------------------------------------------------------------------------
// d3BeginRebuild -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginRebuild(){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::BeginRebuild"));
  return !theApp.m_mes_3DDOC || ::YesNoT(_T(" ?")) == 1;
}

//-----------------------------------------------------------------------------
// d3Rebuild -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::rebuild() {
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::Rebuild"));
  return true;
}

//-----------------------------------------------------------------------------
// d3BeginChoiceMaterial -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginChoiceMaterial(){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::BeginChoiceMaterial"));
  return !theApp.m_mes_3DDOC || ::YesNoT(_T(" ?")) == 1;
}

//-----------------------------------------------------------------------------
// d3ChoiceMaterial -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::ChoiceMaterial( LPSTR material, double density ){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::ChoiceMaterial"));
  return true;
}

//-----------------------------------------------------------------------------
// d3BeginChoiceMarking -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginChoiceMarking(){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::BeginChoiceMarking"));
  return !theApp.m_mes_3DDOC || ::YesNoT(_T(" ?")) == 1;
}

//-----------------------------------------------------------------------------
// d3ChoiceMarking -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::ChoiceMarking( LPSTR marking ){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::ChoiceMarking"));
  return true;
}

//-----------------------------------------------------------------------------
// d3BeginCreatePartFromFile -      .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginSetPartFromFile() {
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::BeginSetPartFromFile"));
  return !theApp.m_mes_3DDOC || ::YesNoT(_T("     ?")) == 1;
}

//-----------------------------------------------------------------------------
// d3BeginCreatePartFromFile -      .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginCreatePartFromFile( VARIANT_BOOL typeDoc, IEntity* plane ) {
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  pThis->ShowEventName(_T("Document3DEvent::BeginCreatePartFromFile"));
  return !theApp.m_mes_3DDOC || ::YesNoT(_T("   ?")) == 1;
}


