/*=========================================================================
*
* CPASectO.hpp		- CPA_SectionObject : implementation
*
*
* Version			1.0
* Creation date		20.06.97
* Revision date
*
* Shaitan
*=======================================================================*/

#include "stdafx.h"

#ifdef ACTIVE_EDITOR
#include "acp_base.h"

#include "itf/CPAMdfO.hpp"
#include "itf/CPAEditB.hpp"

/*-----------------------------------------------------------------------------
 *  MACRO
 *---------------------------------------------------------------------------*/
#define lMAKE_MODIF( bExists, bInitialState )   ( (long) ((bExists ? 0x0000ffff : 0) + (bInitialState ? 0xffff0000 : 0)) )

#define M_bExists( lModif )                     ( (BOOL ) ( lModif & 0x0000ffff ) )
#define M_bOnInitialState( lModif )             ( (BOOL ) ( lModif & 0xffff0000 ) )

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
CPA_ModifiableObject::CPA_ModifiableObject(CPA_EditorBase *pEditor, const CString csType, tdeSaveStatus eStatus,
                                           CPA_BaseObject *pOwner, BOOL bAvailable,
					                       char *p_szDataPath, SCR_tdpfn_Ntfy_Callback pCallBack)
                     :CPA_SaveObject(pEditor, csType, eStatus, pOwner, bAvailable, p_szDataPath, pCallBack)
{
    m_p_oListOfModifNames = pEditor -> fn_p_oGetModifNamesList(csType);
    //
    m_oListOfModifications . RemoveAll();
    if( m_p_oListOfModifNames != NULL )
    {
        for( int i=0 ; i < m_p_oListOfModifNames -> GetCount() ; i++ )
            m_oListOfModifications . AddTail( lMAKE_MODIF( FALSE, TRUE ) );
    }
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
CPA_ModifiableObject::CPA_ModifiableObject(CPA_ModifiableObject &r_oSource)
                     :CPA_SaveObject(r_oSource)
{
    m_p_oListOfModifNames = r_oSource . m_p_oListOfModifNames;
    m_oListOfModifications . RemoveAll();
    if( m_p_oListOfModifNames != NULL )
    {
        for( int i=0 ; i < m_p_oListOfModifNames -> GetCount() ; i++ )
            m_oListOfModifications . AddTail( lMAKE_MODIF( FALSE, TRUE ) );
    }
}


/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
CPA_ModifiableObject::~CPA_ModifiableObject()
{
    m_oListOfModifications . RemoveAll();
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
void CPA_ModifiableObject::fn_vSetCurrentState(const CString _ocsModificationType, BOOL _bInitialState)
{
    POSITION stPos = fn_stGetModificationPos( _ocsModificationType );
    if( stPos )
    {
        long lModif     = m_oListOfModifications . GetAt( stPos );
        BOOL bHasModif  = M_bExists( lModif );

        fn_vSetOnState( _ocsModificationType, _bInitialState );

        lModif = lMAKE_MODIF( bHasModif, _bInitialState );
        m_oListOfModifications . SetAt( stPos, lModif );
    }
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
void CPA_ModifiableObject::fn_vSetModification(const CString _ocsModificationType, BOOL _bModified /*=TRUE*/, BOOL _bInitialState /*=TRUE*/)
{
    fn_vSetOnState( _ocsModificationType, _bInitialState );
	fn_vUpdateModification(_ocsModificationType, _bModified, _bInitialState);
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
void CPA_ModifiableObject::fn_vUpdateModification (const CString _ocsModificationType, BOOL _bModified, BOOL bInitialState)
{
    POSITION stPos = fn_stGetModificationPos( _ocsModificationType );
    if( stPos )
        m_oListOfModifications . SetAt( stPos, lMAKE_MODIF( _bModified, bInitialState ) );
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
BOOL CPA_ModifiableObject::fn_bIsOnInitialState(const CString _ocsModificationType)
{
    POSITION stPos = fn_stGetModificationPos( _ocsModificationType );
    if( stPos )
        return M_bOnInitialState( m_oListOfModifications . GetAt( stPos ) );
    return TRUE;
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
BOOL CPA_ModifiableObject::fn_bIsModified(const CString _ocsModificationType)
{
    POSITION stPos = fn_stGetModificationPos( _ocsModificationType );
    if( stPos )
        return M_bExists( m_oListOfModifications . GetAt( stPos ) );
    return FALSE;
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
void CPA_ModifiableObject::fn_vValidateState(const CString _ocsModificationType, BOOL bInitialState)
{
    fn_vSetModification( _ocsModificationType, FALSE, bInitialState);
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
POSITION CPA_ModifiableObject::fn_stGetModificationPos( const CString _ocsModificationType )
{
    if( m_p_oListOfModifNames != NULL )
    {    
        POSITION xPosName = m_p_oListOfModifNames -> GetHeadPosition();
        POSITION xPosModif = m_oListOfModifications . GetHeadPosition();
        while( xPosName )
        {
            ASSERT( xPosModif );
            CString csName = m_p_oListOfModifNames -> GetNext( xPosName );
            if( csName == _ocsModificationType )
                return xPosModif;
            m_oListOfModifications . GetNext( xPosModif );
        }
    }
    return NULL;
}

#endif // ACTIVE_EDITOR
