#include <TOS.H>
#include "skel.h"
#include "skel.rsh"

CPXINFO *cpx_init();
BOOLEAN cpx_call();

XCPB *xcpb;
CPXINFO cpxinfo;

CPXINFO
*cpx_init( Xcpb )
XCPB *Xcpb;
{
	xcpb = Xcpb;

	appl_init();

	if(xcpb->booting)
	{

		/* CPX's that do boot-time initialization do it here */

		/* Returning TRUE here tells XCONTROL to retain the header
		 * for later access by the user. If CPX is Set-Only,
		 * return FALSE.
		 */
	
		return ( (CPXINFO *) TRUE )
	} 
	else
	{
		/* If you haven't already done so, fix resource tree.
		 *
		 * DEFINE's and variables are from an RSH file generated
		 * by the Atari Resource Construction Set.
		 */

		if(!SkipRshFix)
			(*xcpb->rsh_fix)( NUM_OBS, NUM_FRSTR, NUM_FRIMG,	NUM_TREE, rs_object, 	
				rs_tedinfo, rs_strings, rs_iconblk, rs_bitblk, rs_frstr, rs_frimg, 	
				rs_trindex, rs_imdope );

		cpxinfo.cpx_call = cpx_call;
		cpxinfo.cpx_draw = NULL;
		cpxinfo.cpx_wmove = NULL;
		cpxinfo.cpx_timer = NULL;
		cpxinfo.cpx_key = NULL;
		cpxinfo.cpx_button = NULL;
		cpxinfo.cpx_m1 = NULL;
		cpxinfo.cpx_m2 = NULL;
		cpxinfo.cpx_hook = NULL;
		cpxinfo.cpx_close = NULL;

		/* Tell XCONTROL to send generic and keyboard
		 * messages.
		 */

		return ( &cpxinfo );
	}
}

BOOLEAN
cpx_call( rect )
GRECT *rect;
{
	/* Put MAINFORM tree in *tree for object macros */

	OBJECT *tree = (OBJECT *)rs_trindex[ MAINFORM ];
	WORD button, quit = FALSE;
	WORD msg[8];

	ObX( ROOT ) = rect->g_x;
	ObY( ROOT ) = rect->g_y;

	objc_draw( tree, ROOT, MAX_DEPTH, PTRS( rect ) );

	do
	{
		button = (*xcpb->Xform_do)( tree, 0, msg );

		/* Be sure and mask off double-clicks if you’re
		 * not interested in them.
		 */

		if( ( button & 0x8000 ) && ( button != 0xFFFF ) ) {
		button &= 0x7FFF;
			
		button &= 0x7FFF;

		switch( button )
		{
			/* Check for EXIT or TOUCHEXIT resource objects */

			case OK:
				break;
			case CANCEL:
				break;
			case -1:
				switch( msg[0] )
				{
					case WM_REDRAW:
						break;
					case AC_CLOSE:
						quit = TRUE;
						break;
					case WM_CLOSED:
						quit = TRUE;
						break;
					case CT_KEY:
						break;
				}
				break;
		}
	} while( !quit );

	return( FALSE );
}	
