////////////////////////////////////////////////////////////////////////////////
//
//  
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.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 void UpdateSlideBox(); 
extern void UpdateSlideBox( reference docRef ); 
extern void AdviseDoc( reference docRef );


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


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


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


////////////////////////////////////////////////////////////////////////////////
//
// DocumentEvent  -    
//
////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//
// ---
DocumentEvent::DocumentEvent( reference p )
  : cBaseEvent( 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;
}


/////////////////////////////////////////////////////////////////////////////
// DocumentEvent message handlers
//-----------------------------------------------------------------------------
//kdBeginCloseDocument -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::BeginCloseDocument(){
	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  return true;
}


//-----------------------------------------------------------------------------
//kdCloseDocument -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::CloseDocument(){
	METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  cBaseEvent::TerminateEvents( 0, pThis->refDoc );
  UpdateSlideBox( 0 ); 
  return true;
}


//-----------------------------------------------------------------------------
//kdBeginSaveDocument -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::BeginSaveDocument( LPSTR docName ){
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  return true;
}


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


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


//-----------------------------------------------------------------------------
//kdDeactiveDocument -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::Deactivate() {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  UpdateSlideBox( 0 ); 
  return true;
}

//-----------------------------------------------------------------------------
//kdBeginSaveAsDocument -      .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::BeginSaveAsDocument() {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  return true;
}

//-----------------------------------------------------------------------------
//kdDocumentFrameOpen    -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::DocumentFrameOpen( LPUNKNOWN v ) {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  UpdateSlideBox(); 
  return true;
}

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

//-----------------------------------------------------------------------------
// kdProcessDeactivate    -  
// ---
STDMETHODIMP_(VARIANT_BOOL) DocumentEvent::XDocumentFileNotify::ProcessDeactivate( long Id ) {
  METHOD_PROLOGUE_EX_(DocumentEvent, DocumentFileNotify)
  UpdateSlideBox(); 
  return true;
}


////////////////////////////////////////////////////////////////////////////////
//
// Document2DEvent  -    2D 
//
////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//
// ---
Document2DEvent::Document2DEvent( reference p ):
  cBaseEvent( 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 <= d2LocalFragmentEdit;
}


//-----------------------------------------------------------------------------
// d2BeginRebuild -   2D .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::BeginRebuild(){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  return true;
}


//-----------------------------------------------------------------------------
// d2Rebuild -   2D .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::Rebuild() {
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  UpdateSlideBox(); 
  return true;
}


//-----------------------------------------------------------------------------
// d2BeginChoiceMaterial -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::BeginChoiceMaterial(){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  return true;
}


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


//-----------------------------------------------------------------------------
// d2BeginInsertFragment -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document2DEvent::XDocument2DNotify::BeginInsertFragment(){
  METHOD_PROLOGUE_EX_(Document2DEvent, Document2DNotify)
  return true;
}


//-----------------------------------------------------------------------------
// 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 ):
  cBaseEvent( 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;
}

//-----------------------------------------------------------------------------
// d3BeginRebuild -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginRebuild(){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  return true;
}

//-----------------------------------------------------------------------------
// d3Rebuild -   .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::rebuild() {
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  UpdateSlideBox(); 
  return true;
}

//-----------------------------------------------------------------------------
// d3BeginChoiceMaterial -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginChoiceMaterial(){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  return true;
}

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

//-----------------------------------------------------------------------------
// d3BeginChoiceMarking -  .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginChoiceMarking(){
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  return true;
}

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

//-----------------------------------------------------------------------------
// d3BeginCreatePartFromFile -      .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginSetPartFromFile() {
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  return true;
}

//-----------------------------------------------------------------------------
// d3BeginCreatePartFromFile -      .
// ---
STDMETHODIMP_(VARIANT_BOOL) Document3DEvent::XDocument3DNotify::BeginCreatePartFromFile( VARIANT_BOOL typeDoc, IEntity* plane ) {
  METHOD_PROLOGUE_EX_(Document3DEvent, Document3DNotify)
  return true;
}
