/*	XGSend.cpp
 *
 *		This is the code to handle sending messages to specific
 *	XGDispatch objects
 */

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

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

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

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <XEvent.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGSend::XGSend
 *
 *		Sender construction. This creates a sender with no receivers.
 */

XGSend::XGSend()
{
}

/*$	XGSend::~XGSend
 *
 *		Remove everything receiving from me. This automatically removes
 *	all the receivers from me before releasing.
 */

XGSend::~XGSend()
{
	while (fReceive.Length()) {
		RemoveReceiver(fReceive[0].disp);
	}
}

/************************************************************************/
/*																		*/
/*	Add/Remove receiver													*/
/*																		*/
/************************************************************************/

/*$	XGSend::AddReceiver
 *
 *		Add the receiver to my list. This automatically updates both
 *	the sender and the receiver, so that senders and receivers may be
 *	automatically shut down when either is destroyed.
 */

void XGSend::AddReceiver(XGDispatch *d)
{
	long i,len;
	XGSDispRecord dr;
	
	/*
	 *	Make sure we're not already attached
	 */
	
	len = fReceive.Length();
	for (i = 0; i < len; i++) {
		if (fReceive[i].disp == d) return;
	}
	
	/*
	 *	Now attach this
	 */
	
	dr.disp = d;
	dr.mark = false;
	fReceive += dr;
	(d->fSend) += this;
}

/*$	XGSend::RemoveReceiver
 *
 *		Remove this receiver. Normally this is automatically called when
 *	either the sender or the receiver is destroyed. However, you may wish
 *	to call this in case you are no longer interested in events but don't
 *	want to go away quite yet.
 */

void XGSend::RemoveReceiver(XGDispatch *d)
{
	long i,len;
	
	/*
	 *	Find in my list and remove
	 */
	
	len = fReceive.Length();
	for (i = 0; i < len; i++) {
		if (fReceive[i].disp == d) {
			fReceive.Delete(i);
			fDelMark = true;
			break;
		}
	}
	
	/*
	 *	Find me in the sender's list
	 */
	 
	len = (d->fSend).Length();
	for (i = 0; i < len; i++) {
		if ((d->fSend)[i] == this) {
			(d->fSend).Delete(i);
			break;
		}
	}
}


/************************************************************************/
/*																		*/
/*	Send messages														*/
/*																		*/
/************************************************************************/

/*$	XGSend::SendMessage
 *
 *		Send a message to my receiver. This works, even if different
 *	objects delete themselves during exeuction. Note that if a receiver
 *	is added during execution of this method, those receivers are
 *	not sent this message.
 */

void XGSend::SendMessage(long msg, long arg, void *parg)
{
	long i,len;

	/*
	 *	Clear marks
	 */
		
	fDelMark = false;
	len = fReceive.Length();
	for (i = 0; i < len; i++) fReceive[i].mark = false;
	
	for (;;) {
		for (i = 0; i < len; i++) {
			if (!fReceive[i].mark) {
				fReceive[i].mark = true;
				fReceive[i].disp->DoDispatch(msg,arg,parg,false);
				if (fDelMark) break;
			}
		}
		
		if (i >= len) break;
		
		/* If we get here, a receiver was deleted. Restart this */
		len = fReceive.Length();
		fDelMark = false;
	}
}
