////////////////////////////////////////////////////////////////////////////////
//
// ApplicationEvent.cpp
// ApplicationEvent -     (Auto)
//
////////////////////////////////////////////////////////////////////////////////

#include <vcl.h>
#pragma hdrstop

#ifndef __LDEFIN2D_H
#include <ldefin2d.h>
#endif
#include <ksConstants.h>

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

extern KompasObjectPtr kompasPtr;
//-------------------------------------------------------------------------------
//
// ---
void AdviseDoc( IUnknownPtr doc, long docType );


//-------------------------------------------------------------------------------
//
// ---
HRESULT ApplicationEvent::InvokeEvent(DISPID id, BOOL & resEvent, TVariant *params)
{
  HRESULT hr = E_NOTIMPL;
  switch(id)
  {
    case koCreateDocument       : //  
      resEvent = CreateDocument( (LPDISPATCH)params[0], (long)params[1] );
      hr = S_OK;
      break;
    case koBeginOpenDocument    : //   
      resEvent = BeginOpenDocument( WideString( params[0] ) );
      hr = S_OK;
      break;
    case koOpenDocument         : //  
      resEvent = OpenDocument( (LPDISPATCH)params[0], (long)params[1] );
      hr = S_OK;
      break;
    case koChangeActiveDocument : //     
      resEvent = ChangeActiveDocument( (LPDISPATCH)params[0], (long)params[1] );
      hr = S_OK;
      break;
    case koApplicatinDestroy    : //  
      resEvent = ApplicationDestroy();
      hr = S_OK;
      break;
    case koBeginCreate          : //   
      resEvent = BeginCreate( (long)params[0] );
      hr = S_OK;
      break;
    case koBeginOpenFile        : //   
      resEvent = BeginOpenFile();
      hr = S_OK;
      break;
    case koBeginCloseAllDocument: //    
      resEvent = BeginCloseAllDocument();
      hr = S_OK;
      break;

  }
  return hr;
}


//-------------------------------------------------------------------------------
// koCreateDocument -  
// ---
BOOL ApplicationEvent::CreateDocument( LPDISPATCH newDoc, long docType )
{
//  AdviseDoc( newDoc, docType );

  LibMessage( "ApplicationEvent -  " );
  return TRUE;
}


//-------------------------------------------------------------------------------
//   
// ---
BOOL ApplicationEvent::BeginOpenDocument( BSTR fileName )
{
  return LibMessage( "ApplicationEvent -   \n ?", MB_YESNO ) == IDYES;;
}


//-------------------------------------------------------------------------------
//  
// ---
BOOL ApplicationEvent::OpenDocument( LPDISPATCH newDoc, long docType )
{
  LibMessage( "ApplicationEvent -  " );
  return TRUE;
}


//-------------------------------------------------------------------------------
//     
// ---
BOOL ApplicationEvent::ChangeActiveDocument( LPDISPATCH newDoc, long docType )
{
  LibMessage( "ApplicationEvent -     " );
  return TRUE;
}


//-------------------------------------------------------------------------------
//  
// ---
BOOL ApplicationEvent::ApplicationDestroy()
{
  LibMessage( "ApplicationEvent -  " );
  Disconnect();
  EventListObject::TerminateEvents();
  return TRUE;
}

//-----------------------------------------------------------------------------
// koBeginCreate -   (   )
// ---
BOOL ApplicationEvent::BeginCreate( long docType )
{
  bool res = true;
  AnsiString str( "ApplicationEvent::BeginCreate\n"
                 " -  \n"
			           " -     \n"
							 	 " -   " );
  int comm = LibMessage( str.c_str(), MB_YESNOCANCEL );
  switch ( comm ) {
    case ID_YES: {
      ksDocumentParamPtr docParam( kompasPtr->GetParamStruct( ko_DocumentParam ) );
      docParam->Init();
      docParam->type = docType ? docType : lt_DocSheetStandart;
      ksDocument2DPtr doc(kompasPtr->Document2D() );
      res = !(bool)doc->ksCreateDocument( docParam );
      break;
    }
    case ID_CANCEL:
      res = false;
      break;
  }
  return res;
}

//-----------------------------------------------------------------------------
// koBeginOpenFile -   (   )
// ---
BOOL ApplicationEvent::BeginOpenFile()
{
  bool res = true;
  AnsiString str( "ApplicationEvent::BeginOpenFile\n"
                 " -  \n"
  	             " -     \n"
					   	 	 " -   " );
  int comm = LibMessage( str.c_str(), MB_YESNOCANCEL );
	switch ( comm ) {
    case ID_YES:
    {
      ksDocumentParamPtr docParam( kompasPtr->GetParamStruct( ko_DocumentParam ) );
      docParam->Init();
      docParam->type = lt_DocSheetStandart;
      ksDocument2DPtr doc( kompasPtr->Document2D() );
      res = !(bool)doc->ksCreateDocument( docParam );
      break;
    }
    case ID_CANCEL:
      res = false;
      break;
  }
  return res;
}

//-----------------------------------------------------------------------------
// BeginCloseAllDocument -    
// ---
BOOL ApplicationEvent::BeginCloseAllDocument() {
  return LibMessage( "ApplicationEvent::BeginCloseAllDocument\n ?", MB_YESNO ) == ID_YES;
}

