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

#ifndef _APPLICATIONEVENT_H
#include "ApplicationEvent.h"
#endif

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

extern AFX_MODULE_STATE* pModuleState;



BaseEvent* NewApplicationEvent() {
  ApplicationEvent* res = NULL;
	if ( !BaseEvent::FindEvents( ntKompasObjectNotify, 0 ) ) {
		res = new ApplicationEvent();
		if ( !res->Advise() ) { 
			delete res;
			res = NULL;
		}
	}
	return res;
}

////////////////////////////////////////////////////////////////////////////////
//
// ApplicationEvent  -    
//
////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//
// ---
ApplicationEvent::ApplicationEvent():
  BaseEvent(ntKompasObjectNotify, (reference)0 )
{
}

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


//-----------------------------------------------------------------------------
//
// ---
LPUNKNOWN ApplicationEvent::GetUnknown() {
  m_xKompasObjectNotify.AddRef();
  return &m_xKompasObjectNotify;
}

//-----------------------------------------------------------------------------
//   
// ---
void ApplicationEvent::ShowEventName( LPCTSTR eventName, reference pDoc ) {
	if ( theApp.m_mes_APP ) {
		CString str = eventName;
		if ( pDoc ) 
			str += GetDocumentParam( pDoc );  
		::_ShowEventName( str );
	}
}

//-----------------------------------------------------------------------------
//  
// ---
CString ApplicationEvent::EventCaption() {
	return _T("ApplicationEvent");
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) ApplicationEvent::XKompasObjectNotify::Release()
{
	METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
	return (ULONG)pThis->InternalRelease();
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP_(ULONG) ApplicationEvent::XKompasObjectNotify::AddRef()
{
	METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
	return (ULONG)pThis->InternalAddRef();
}

//-----------------------------------------------------------------------------
//
// ---
STDMETHODIMP ApplicationEvent::XKompasObjectNotify::QueryInterface(
	REFIID iid, LPVOID* ppvObj)
{
	METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)

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

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

	return E_NOINTERFACE;
}

//-----------------------------------------------------------------------------
//      notifyType
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::IsNotifyProcess( int notifyType )
{
//	METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
	return  notifyType >= koCreateDocument && notifyType <= koBeginRequestFiles;
}


/////////////////////////////////////////////////////////////////////////////
// ApplicationEvent message handlers
//-----------------------------------------------------------------------------
// koCreateDocument -  
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::CreateDocument( long pDoc, int docType ) { 
  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  pThis->ShowEventName( _T("EventCom::ApplicationEvent::CreateDocument"), pDoc );
//	pThis->ShowObjParam( pDoc );
  if ( theApp.m_auto ) 
    AdviseDoc( pDoc );
  return true;
}


//-----------------------------------------------------------------------------
// koOpenDocumenBegin -   
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::BeginOpenDocument( LPSTR docName ) {
  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  bool res = true;
  if ( theApp.m_mes_APP ) { 
    CString mess = _T("EventCom::ApplicationEvent::BeginOpenDocumen");
		mess += _T("\n");
		mess += docName;
    res = YesNoT( (LPTSTR) (LPCTSTR) mess ) == 1;
  }
  return res;
}


//-----------------------------------------------------------------------------
// koOpenDocumen -  
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::OpenDocument(long _pDoc, int docType) {
  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  pThis->ShowEventName( _T("EventCom::ApplicationEvent::OpenDocumen"), _pDoc );
  if ( theApp.m_auto ) 
	  AdviseDoc( _pDoc );

  return true;
}


//-----------------------------------------------------------------------------
// koActiveDocument -     
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::ChangeActiveDocument(long _pDoc, int docType) {
  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  pThis->ShowEventName( _T("EventCom::ApplicationEvent::ChangeActiveDocument"), _pDoc );
  return true;
}


//-----------------------------------------------------------------------------
// koApplicatinDestroy -  
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::ApplicationDestroy(){
  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  pThis->ShowEventName( _T("EventCom::ApplicationEvent::ApplicationDestroy"), 0 );
  pThis->Disconnect();
  return true;
}

//-----------------------------------------------------------------------------
// koBeginCreate -   
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::BeginCreate( long type ){
  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  bool res = !theApp.m_mes_APP;
	if ( !res ) {
    int com = YesNoT( _T("EventCom::ApplicationEvent::BeginCreate\n ")
			                _T(" -  \n") 
			                _T(" -     \n")
									    _T(" -   ") );
		switch ( com ) {
		  case 1: {
        DocumentParamT docParam;
				memset( &docParam, 0, sizeof(docParam) );
				docParam.type = type ? type : lt_DocSheetStandart;
				reference doc = ::CreateDocumentT( &docParam );
				res = !doc; //       
				if ( doc && theApp.m_auto )
					AdviseDoc( doc ); 
				break;
			}	
			case 0:
				res = true;
				break;
			case -1:
				res = false;
		}
	}
	return res;
}

//-----------------------------------------------------------------------------
// koBeginOpenFile -   
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::BeginOpenFile() {
  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  bool res = !theApp.m_mes_APP;
	if ( !res ) {
    int com = YesNoT( _T("EventCom::ApplicationEvent::BeginOpenFile\n ")
			                _T(" -  \n") 
			                _T(" -     \n")
										  _T(" -   ") );
		switch ( com ) {
		  case 1: {
        DocumentParam docParam;
				memset( &docParam, 0, sizeof(docParam) );
				docParam.type = lt_DocSheetStandart;
				reference doc = ::CreateDocument( &docParam );
				res = !doc; //       
				if ( doc && theApp.m_auto )
					AdviseDoc( doc ); 
				break;
			}	
			case 0:
				res = true;
				break;
			case -1:
				res = false;
		}
	}
	return res;
}


//-----------------------------------------------------------------------------
// BeginCloseAllDocument -    
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::BeginCloseAllDocument() {
  return !theApp.m_mes_APP || YesNoT( _T("EventCom::ApplicationEvent::BeginCloseAllDocument\n ?") ) == 1;
}


//-----------------------------------------------------------------------------
// KeyDown -  . (   )
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::KeyDown( long *       key,
                                                                            long         flags,
                                                                            VARIANT_BOOL sysKey ) 
{
//  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  return VARIANT_TRUE;
}


//-----------------------------------------------------------------------------
// KeyUp -  . (   )
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::KeyUp( long *       key,
                                                                          long         flags,
                                                                          VARIANT_BOOL sysKey ) 
{
//  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  return VARIANT_TRUE;
}


//-----------------------------------------------------------------------------
// KeyPress -  . (      )
// ---
STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::KeyPress( long *       key,
                                                                             VARIANT_BOOL sysKey ) 
{
//  METHOD_PROLOGUE_EX_(ApplicationEvent, KompasObjectNotify)
  return VARIANT_TRUE;
}


STDMETHODIMP_(VARIANT_BOOL) ApplicationEvent::XKompasObjectNotify::BeginRequestFiles(long requestID,
                                             VARIANT * files )
{
  VARIANT_BOOL res = VARIANT_TRUE;
  if ( requestID == koRFCopyBilletPart )
  {
    TCHAR filename[_MAX_PATH] = _T("");
    res = ksChoiceFileT( _T("m3d"), _T("- (*.m3d)|*.m3d"),filename, _MAX_PATH, 1 ) > 0;
    if ( res && files )
    {
      files->vt = VT_BSTR;
      files->bstrVal = _bstr_t(filename).copy(); 
    }  
  }
  return res;
}
