////////////////////////////////////////////////////////////////////////////////
//
// DocumentEvent.cpp
// DocumentEvent -     (Auto)
//
////////////////////////////////////////////////////////////////////////////////

#include <vcl.h>
#pragma hdrstop

#include <ksConstants.h>

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

extern int LibMessage( char* str, int flags  = MB_OK );

//-------------------------------------------------------------------------------
//    
// ---
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;
}

//-------------------------------------------------------------------------------
//
// ---
DocumentEvent::DocumentEvent( reference refDoc ) :
  TBaseEvent<IDocumentFileNotify, & IID_IDocumentFileNotify>( ntDocumentFileNotify, refDoc, 0, refDoc )
{
}

//-----------------------------------------------------------------------------
//      notifyType
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::IsNotifyProcess( int notifyType )
{
	return  notifyType >= kdBeginCloseDocument && notifyType <= kdBeginSaveAsDocument;
}


//-------------------------------------------------------------------------------
//   
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::BeginCloseDocument( void ) {
  return LibMessage( "DocumentEvent -   \n ?", MB_YESNO ) == IDYES;
}


//-------------------------------------------------------------------------------
//  
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::CloseDocument( void )
{
  LibMessage( "DocumentEvent -  " );
  TerminateEvents( 0, refDoc );
  return TRUE;
}


//-------------------------------------------------------------------------------
//   
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::BeginSaveDocument( LPCSTR docName )
{
  AnsiString s = docName;
  s += "\n\nDocumentEvent -   \n ?";
  return LibMessage( s.c_str(), MB_YESNO ) == IDYES;
}


//-------------------------------------------------------------------------------
//  
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::SaveDocument( void )
{
  LibMessage( "DocumentEvent -  " );
  return TRUE;
}


//-------------------------------------------------------------------------------
//  
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::Activate( void )
{
  LibMessage( "DocumentEvent -  " );
  return TRUE;
}


//-------------------------------------------------------------------------------
//  
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::Deactivate( void )
{
  LibMessage( "DocumentEvent -  " );
  return TRUE;
}

//-----------------------------------------------------------------------------
//kdBeginSaveAsDocument -      .
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::BeginSaveAsDocument() {
	bool res = true;
  int com = YesNo( "DocumentEvent::BeginSaveAsDocument\n "
   	               " -  \n"
	 	               " -     \n"
	 								 " -   " );
 	switch ( com ) {
    case 1: {
      int type = ksGetDocumentType( refDoc );
      switch ( type ) {
        case lt_DocTxtStandart:             //               7-   
        case lt_DocTxtUser: {                //               8-   
          TextDocumentParam docParam;
          GetObjParam( refDoc, &docParam, sizeof(docParam), ALLPARAM );
          if ( !docParam.fileName[0] )
            res = !::SaveDocument( 0, "C:\\1.kdw" );
          else
            res = !::SaveDocument( 0, 0 );
          break;
        }
        default: {
          DocumentParam docParam;
          GetObjParam( refDoc, &docParam, sizeof(docParam), ALLPARAM );
          if ( !docParam.fileName[0] ) {
            switch ( type ) {
              case lt_DocFragment:           //               3- 
                res = !::SaveDocument( 0, "C:\\1.frw" );
                break;
              case lt_DocSheetStandart:
              case lt_DocSheetUser:
                res = !::SaveDocument( 0, "C:\\1.cdw" );
                break;
              case lt_DocSpc:                //               4- 
              case lt_DocSpcUser:
                res = !::SaveDocument( 0, "C:\\1.spw" );
                break;
              case lt_DocPart3D:                  //          5- 3d- 
                res = !::SaveDocument( 0, "C:\\1.m3d" );
                break;
              case lt_DocAssemble3D:                  //      6- 3d- 
                res = !::SaveDocument( 0, "C:\\1.a3d" );
                break;

            }
          }
          else
            res = !::SaveDocument( 0, 0 );
        }
      }
    }
    case 0:
      res = true;
      break;
    case -1:
      res = false;
  }
  return res;
}

//-----------------------------------------------------------------------------
//kdDocumentFrameOpen    -   .
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::DocumentFrameOpen( LPUNKNOWN v ) {
  return true;
}

//-----------------------------------------------------------------------------
// kdProcessActivate      -  
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::ProcessActivate( long Id ) {
  return true;
}

//-----------------------------------------------------------------------------
// kdProcessDeactivate    -  
// ---
VARIANT_BOOL STDMETHODCALLTYPE DocumentEvent::ProcessDeactivate( long Id ) {
  return true;
}




