// @(#) 1.4 com/src/samples/scrsimpl/iodspext.cpp, odsimplescript, od96nt, odnt9712d 2/20/97 17:30:42 [3/21/97 17:38:11]
//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odscriptableparts
//
//   CLASSES: none
//
//   ORIGINS: 82,27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995,1996
//   All Rights Reserved
//   Licensed Materials - Property of IBM
//   US Government Users Restricted Rights - Use, duplication or
//   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//   	
//   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
//   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
//   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
//   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
//   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
//   OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//



// This file implements the ScriptExtension code for a scriptable part which
// inherits from 'SimplePart'.

#include <iodspcl.h>

#ifndef SOM_Module_iodspext_Source
#define SOM_Module_iodspext_Source
#endif
#define ScriptableSimplePartScrExt_Class_Source

#define  VARIABLE_MACROS
#include <iodspext.xih>
#include <scrcomp.xh>           // to get return value codes
#include <somtc.h>              // for TypeCodeNew declarations
#include <oddsemgr.xh>          // for ODDSEventManager
#include <iodscrsp.xh>          // for ScriptableSimplePart


//==========================================================================
// override Register to properly register this object as a base object.
//==========================================================================

SOM_Scope ODScriptExtension_ODScrRegRet  SOMLINK _CLx_P(Register)(_CLx_ *somSelf,
                                                                  Environment *ev,
                                                                 ODScriptExtension* RegScope,
                                                                 string Prefix)

{
    ODScrRegRet RetVal = ODScrRegOK ;
    ODObject* basepart ;
    char*     className ;
    char*     nameAssigned ;

    /* _CLx_P(Data) *somThis = _CLx_P(GetData)(somSelf); */
    _CLx_P(MethodDebug)(_CLx_q , _CLx_Pq(Register) );

    // when prefix is null, this object is being registered as a top level
    // object and we will proceed to do so.  If it is not null, then this
    // object was already registered as the 'containing extension object'
    // for this part and thus (because we only want to register this object
    // one time) we will bypass registration.

    if (( Prefix == NULL)  || ( *Prefix == '\0'))
    {
       basepart = somSelf ;              // register the extension object
       if (basepart == NULL) {return ODScrRegErr;}

       // The class for each scriptable object needs to be registered with the
       // ScriptComponent.

       className = basepart->somGetClassName() ;
       RetVal = RegScope->DoRegisterClass(ev, className) ;
       if (RetVal != ODScrRegOK)
       { return RetVal ; }

       // DoRegisterObject can take care of most normal naming issues.  We
       // call it here with the option option that will append a
       // numeric string to the name supplied here if needed to
       // make the name unique in the set of names assigned for this Part.

       RetVal = RegScope->DoRegisterObject(ev,
                        (ODScriptExtension *)somSelf , // the object to be scriptable (self)
                        className,        // the class name of that object
                        somSelf->getExtName(ev), // the default name to be used
                        Prefix,           // prefix that applies in the scope
                                          //  where registration begins
                        &nameAssigned,    // receives name actually assigned
                        somSelf,          // the scope/extension of the object
                        kODTrue);         // digits to be appended to make the
                                          //  name unique if needed.

       if (RetVal == ODScrRegOK)          // if registration was successful, we
       { SOMFree(nameAssigned) ; }        //  need to release the storage
                                          //  that holds the actual name assigned.
                                          //  For this part we don't need to
                                          //  save that information.
    }
    return RetVal ;

}


//==========================================================================
// override InitScriptExtension to ensure our default name is set properly
//==========================================================================

SOM_Scope void  SOMLINK _CLx_P(InitScriptExtension)(_CLx_ *somSelf,
                                                    Environment *ev,
                                                   ODPart* owner)
{
    _CLx_P(Data) *somThis = _CLx_P(GetData)(somSelf);
    _CLx_P(MethodDebug)( _CLx_q , _CLx_Pq(InitScriptExtension) );

    // Set the preferred name for this object.
    if (somSelf->getExtName(ev) == NULL)
    {
      somSelf->setExtName(ev, "Simple") ;
    }

    _CLx_P(_parent_ODScriptExtension_InitScriptExtension)(somSelf, ev, owner) ;

}


//=======================================================================
// Override somDefaultInit to add initialization for connection support
//=======================================================================
SOM_Scope void SOMLINK _CLx_P(somDefaultInit) (_CLx_  *somSelf,
                                             somInitCtrl* ctrl)
{
    _CLx_P(Data) *somThis; /* set in BeginInitializer */
    somInitCtrl globalCtrl;
    somBooleanVector myMask;
    _CLx_P(MethodDebug)( _CLx_q ,"somDefaultInit");
    _CLx_P(_BeginInitializer_somDefaultInit);

    _CLx_P(_Init_ODScriptExtension_somDefaultInit)(somSelf, ctrl);
    _CLx_P(_Init_ODDSConnection_somDefaultInit)(somSelf, ctrl);

    /*
     * local initialization code
     */

    ODGUID odguid = { 0xf31fb930,0x2376, 0x11d0,
                      { 0xa5, 0xbf, 0x00, 0x04, 0xac, 0x32, 0x80, 0xdc}};
    _fODGuid = odguid ;
    Environment *ev = SOM_CreateLocalEnvironment();
    _fEventManager =  new ODDSEventMgr(ev, _fODGuid);
    somSelf->AddEventMgr(ev, _fEventManager);
    SOM_DestroyLocalEnvironment(ev);

}

//=======================================================================
// Override somDestruct to properly dispose of connection data
//=======================================================================

SOM_Scope void SOMLINK _CLx_P(somDestruct)(_CLx_ *somSelf,
                                          octet doFree, somDestructCtrl* ctrl)
{
    _CLx_P(Data) *somThis; /* set in BeginDestructor */
    somDestructCtrl globalCtrl;
    somBooleanVector myMask;
    _CLx_P(MethodDebug)( _CLx_q , _CLx_Pq(somDestruct));
    _CLx_P(_BeginDestructor);

    /*
     * local deinitialization code
     */
    if (_fEventManager)
    {
      Environment *ev = SOM_CreateLocalEnvironment();
      somSelf->DeleteEventMgr(ev,&_fODGuid) ;
      _fEventManager = NULL ;
      SOM_DestroyLocalEnvironment(ev);
    }

    _CLx_P(_EndDestructor) ;
}


//=======================================================================
// Method to invoke any event sinks attached to the MouseDown event
//=======================================================================


SOM_Scope long  SOMLINK _CLx_P(TriggerMouseDown)(_CLx_ *somSelf,
                                                              Environment *ev,
                                                             long x,
                                                             long y)
{
    _CLx_P(Data)  *somThis = _CLx_P(GetData) (somSelf);
    _CLx_P(MethodDebug) (_CLx_q,_CLx_Pq(TriggerMouseDown) );

    _IDL_SEQUENCE_any  inParms;
    ODDSRESULT         result;
    long               sinkCount ;
    long               sinkNumber ;
    any                data[2];
    ODDSSinkEnum       *sinkEnum;

    data[0]._type = TypeCodeNew(tk_long);
    data[0]._value = (void *) &x;
    data[1]._type = TypeCodeNew(tk_long);
    data[1]._value = (void *) &y;

    inParms._length  = 2;
    inParms._maximum = 2;
    inParms._buffer = data;


    _fEventManager->PrepareEventFiring(ev, &sinkCount, &sinkEnum);
    for (sinkNumber = 0 ; sinkNumber < sinkCount ; sinkNumber++ )
    {
      _fEventManager->FireEvent(ev, sinkEnum, "MouseDown",&inParms,&result);
      // ignore any return value, we don't use it in this case
    }
    _fEventManager->StopEventFiring(ev, sinkEnum);

    return(0);

}


//=======================================================================
// Method to set the color of the part and force update
//=======================================================================

SOM_Scope void  SOMLINK _CLx_P(SetColor)(_CLx_ *somSelf,
                                         Environment *ev,
                                         long newColor)
{

    _CLx_P(Data)  *somThis = _CLx_P(GetData) (somSelf);
    _CLx_P(MethodDebug) (_CLx_q,_CLx_Pq(SetColor) );

    (( _CLp_ *) somSelf->GetBase(ev))->SetColor(ev, newColor) ;
}

//=======================================================================
// Method to return the color for the part
//=======================================================================

SOM_Scope long  SOMLINK _CLx_P(GetColor)(_CLx_ *somSelf,
                                         Environment *ev)
{
    _CLx_P(Data)  *somThis = _CLx_P(GetData) (somSelf);
    _CLx_P(MethodDebug) (_CLx_q,_CLx_Pq(GetColor) );

    return (( _CLp_ *) somSelf->GetBase(ev))->GetColor(ev) ;
}

