/************************************************************************
 CX.c		FindFile's Commodities exchange routines

 CX.c 1.2 1993/02/28 00:56:13 RUSS Exp

 1.2

 CX.c
 * Revision 1.2  1993/02/28  00:56:13  RUSS
 * Added #include "FindFile.h"
 *
 * Revision 1.1  1993/02/27  18:49:15  RUSS
 * Initial revision
 *
************************************************************************/
#include "FindFile.h"

extern struct Library *IntuitionBase;
extern struct Library *GadToolsBase;
extern struct Library *CxBase;
extern struct Library *IconBase;

extern char *cxTitle;

/******************************** Commodities Exchange Handling Routines *********/

void
OpenCxInterface( struct controlPanel *cp )
{
  struct NewBroker nb = {
    NB_VERSION,
    ( BYTE *)NAME,
    NULL,
    ( BYTE *)"Searches a volume for a file",
    NBU_UNIQUE | NBU_NOTIFY,
    COF_SHOW_HIDE,
    0,
    NULL,
    0
  };

  nb.nb_Title = (BYTE *)cxTitle;
  nb.nb_Pri = cp->cxInterface.cxPriority;


  /******* Allocate And Initialize MessagePort *********/
  if( !( cp->cxInterface.cxPort = CreateMsgPort() ) )
    Shutdown( cp, 20 );

  DB("Got MP.")


  nb.nb_Port = cp->cxInterface.cxPort;

  /******* Create Main Broker CxObj ********************/

  if( !( cp->cxInterface.ffBroker = CxBroker( &nb, NULL ) ) )
  {
    DB("Deleteing Msgport")

    DeleteMsgPort( cp->cxInterface.cxPort );

    DB("Cleaning up")

    cp->cxInterface.cxPort = NULL;
    cp->cxInterface.ffBroker = NULL;

    DB("Shutting Down")

    Shutdown( cp, 20 );
  }

  DB("Created Broker object")

  /******* Create HotKey CxObj *************************/

  AttachCxObj( cp->cxInterface.ffBroker, HotKey( cp->cxInterface.cxHotKeyString,
						 cp->cxInterface.cxPort,
						 OPEN_WINDOW ));

  DB("Hot Key attached")

  /******* If all went well, activate the Broker *******/

  if( ! CxObjError( cp->cxInterface.ffBroker ) )
    ActivateCxObj( cp->cxInterface.ffBroker , TRUE );
  else
    Shutdown( cp , 20 );

  DB("Broker activated.")

}

void
CloseCxInterface( struct controlPanel *cp )
{
  if( cp->cxInterface.cxPort )
  {
    struct Message *msg;

    if( cp->cxInterface.ffBroker )
      DeleteCxObjAll( cp->cxInterface.ffBroker );
    cp->cxInterface.ffBroker = NULL;

    if( cp->cxInterface.cxPort )
    {
	while( msg = GetMsg( cp->cxInterface.cxPort ) )
	ReplyMsg( msg );

	DeleteMsgPort( cp->cxInterface.cxPort );
	cp->cxInterface.cxPort = NULL;
    }

  }
}

void
HandleCxCommand( struct controlPanel *cp )
{
  CxMsg 	 *msg;

  while( msg = ( CxMsg *)GetMsg( cp->cxInterface.cxPort ) )
  {
    ULONG	    messageID;
    ULONG	    messageType;

    messageID = CxMsgID( msg );
    messageType = CxMsgType( msg );

    ReplyMsg( ( struct Message *)msg );

    switch( messageType )
    {
      case CXM_IEVENT : switch( messageID )
			{
			  case OPEN_WINDOW : CMD_Show( cp );
					     break;
			}
			break;

      case CXM_COMMAND :  switch( messageID )
			  {
			    case CXCMD_DISABLE : ActivateCxObj( cp->cxInterface.ffBroker,
								FALSE );
						 break;
			    case CXCMD_ENABLE :  ActivateCxObj( cp->cxInterface.ffBroker,
								TRUE );
						 break;

			    case CXCMD_APPEAR :
			    case CXCMD_UNIQUE :  CMD_Show( cp );
						 break;

			    case CXCMD_DISAPPEAR : CMD_Hide( cp );
						   break;

			    case CXCMD_KILL : CMD_Quit( cp );
					      break;
			  }
			  break;
    }
  }
}

/**********************************************************************************/

