/**	Whfunctions.c
  *
  *	Implement the Window access and control functions for rexxarplib.library.
  *
  *   AUTHOR/DATE:  W.G.J. Langeveld, November 1987.
  *   ============
  *
  *	CURRENT VERSION:
  *
  *	This version has been converted to SAS C 6.5 format. It has been modified
  *	for modern definition sequences for ANSI compilation. This no longer works
  *	with OS versions prior to 2.04.
  *
  *   AUTHOR/DATE:  Joanne Dow, jdow@bix.com, June 1998.
  *   ============
  *
  **/
#include <functions.h>
#include "ralprotos.h"
#include <exec/types.h>
#include <exec/exec.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <graphics/rastport.h>
#include <graphics/gfxmacros.h>
#include <graphics/text.h>
#include <libraries/MyARP.h>
#include <proto/rexxsyslib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "rexxarplib.h"
#include <simpmenu.h>
#include <simpreq.h>
#include "areapolydraw.h"

static struct TextFont *GetFont(struct TextAttr *, int);

/**
 *
 *   whf_draw
 *
**/
char *whf_draw( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	Draw(rp, atol(args[0]), atol(args[1]));
	
	return(NULL);
}


/**
 *
 *   whf_move
 *
**/
char *whf_move( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	Move(rp, atol(args[0]), atol(args[1]));
	
	return(NULL);
}


/**
 *
 *   whf_text
 *
**/
char *whf_text( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	int temp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	temp = strlen(args[0]);
	Text(rp, args[0], (long) temp);
	
	return(NULL);
}


/**
 *
 *   whf_writepixel
 *
**/
char *whf_writepixel( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	WritePixel(rp, atol(args[0]), atol(args[1]));
	
	return(NULL);
}


/**
 *
 *   whf_drawellipse
 *
**/
char *whf_drawellipse( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	long a, b;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	a = atol(args[2]);
	a = (a > 0L) ? a : -a;
	b = atol(args[3]);
	b = (b > 0L) ? b : -b;
	DrawEllipse(rp, atol(args[0]), atol(args[1]), a, b);
	
	return(NULL);
}


/**
 *
 *   whf_drawcircle
 *
**/
char *whf_drawcircle( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	long a;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	a = atol(args[2]);
	a = (a > 0L) ? a : -a;
	DrawCircle(rp, atol(args[0]), atol(args[1]), a);
	
	return(NULL);
}


/**
 *
 *   whf_flood
 *
**/
char *whf_flood( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	if (AllocTmpRas(rp, hp->Window->Width + 32, hp->Window->Height + 2)) 
	{
		Flood(rp, atol(args[0]), atol(args[1]), atol(args[2]));
		FreeTmpRas(rp);
	}
	
	return(NULL);
}


/**
 *
 *   whf_rectfill
 *
**/
char *whf_rectfill( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	long x1, y1, x2, y2;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	hp->Result = 12L;
	
	x1 = atol(args[0]);
	y1 = atol(args[1]);
	x2 = atol(args[2]);
	y2 = atol(args[3]);
	
	if (x1 > x2)
		return(NULL);
	if (y1 > y2)
		return(NULL);
	
	RectFill(rp, x1, y1, x2, y2);
	
	hp->Result = 0L;
	
	return(NULL);
}


/**
 *
 *   whf_setrgb4
 *
**/
char *whf_setrgb4( struct HostParams *hp, int n, char *args[] )
{
	struct ViewPort *vp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	vp = ViewPortAddress(hp->Window);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	SetRGB4(vp, atol(args[0]), atol(args[1]), atol(args[2]), atol(args[3]));
	
	return(NULL);
}


/**
 *
 *   whf_setapen
 *
**/
char *whf_setapen( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	SetAPen(rp, atol(args[0]));
	
	return(NULL);
}


/**
 *
 *   whf_setbpen
 *
**/
char *whf_setbpen( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	SetBPen(rp, atol(args[0]));
	
	return(NULL);
}


/**
 *
 *   whf_setopen
 *
**/
char *whf_setopen( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	SetOPen(rp, atol(args[0]));
	
	return(NULL);
}


/**
 *
 *   whf_setdrmd
 *
**/
char *whf_setdrmd( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	long Md = 0L;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	if (strstr(args[0], "JAM1"))
		Md |= JAM1;
	if (strstr(args[0], "JAM2"))
		Md |= JAM2;
	if (strstr(args[0], "COMPLEMENT"))
		Md |= COMPLEMENT;
	if (strstr(args[0], "INVERSVID"))
		Md |= INVERSVID;
	
	SetDrMd(rp, Md);
	
	return(NULL);
}


/**
 *
 *   whf_setdrpt
 *
**/
char *whf_setdrpt( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	SetDrPt(rp, atol(args[0]));
	
	return(NULL);
}


/**
 *
 *   whf_areamove
 *
**/
char *whf_areamove( struct HostParams *hp, int n, char *args[] )
{
	struct vtx *v;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	v = AddVertex(hp->Vertex);
	if (v) 
	{
		v->type = AREAMOVE;
		v->x = atoi(args[0]);
		v->y = atol(args[1]);
	}
	else
	{
		KillVertex(hp->Vertex);
		hp->Vertex = AddVertex(NULL);
		hp->Result = 12L;
	}
	
	return(NULL);
}


/**
 *
 *   whf_areadraw
 *
**/
char *whf_areadraw( struct HostParams *hp, int n, char *args[] )
{
	struct vtx *v;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	v = AddVertex(hp->Vertex);
	if (v) 
	{
		v->type = AREADRAW;
		v->x = atoi(args[0]);
		v->y = atol(args[1]);
	}
	else
	{
		KillVertex(hp->Vertex);
		hp->Vertex = AddVertex(NULL);
		hp->Result = 12L;
	}
	
	return(NULL);
}


/**
 *
 *   whf_areaend
 *
**/
char *whf_areaend( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	int pen;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	if (n == 1)
		pen = atoi(args[0]);
	else
		pen = rp->FgPen;
	
	if ((pen & 1024) == 0) 
	{
		if (AreaPolyDraw(hp->Window, hp->Vertex, pen, 0, 0) == 0)
			hp->Result = 12L;
	}
	
	KillVertex(hp->Vertex);
	hp->Vertex = AddVertex(NULL);
	
	return(NULL);
}


/**
 *
 *   whf_areaellipse
 *
**/
char *whf_areaellipse( struct HostParams *hp, int n, char *args[] )
{
	struct vtx *v;
	int a, b;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	v = AddVertex(hp->Vertex);
	if (v) 
	{
		v->type = AREAELLIPSE;
		v->x = atoi(args[0]);
		v->y = atol(args[1]);
		a = atoi(args[2]);
		a = (a > 0L) ? a : -a;
		b = atoi(args[3]);
		b = (b > 0L) ? b : -b;
		v->a = a;
		v->b = b;
	}
	else
	{
		KillVertex(hp->Vertex);
		hp->Vertex = AddVertex(NULL);
		hp->Result = 12L;
	}
	
	return(NULL);
}


/**
 *
 *   whf_areacircle
 *
**/
char *whf_areacircle( struct HostParams *hp, int n, char *args[] )
{
	struct vtx *v;
	long a;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	v = AddVertex(hp->Vertex);
	if (v) 
	{
		v->type = AREACIRCLE;
		v->x = atoi(args[0]);
		v->y = atol(args[1]);
		a = atoi(args[2]);
		a = (a > 0L) ? a : -a;
		v->a = a;
		v->b = a;
	}
	else
	{
		KillVertex(hp->Vertex);
		hp->Vertex = AddVertex(NULL);
		hp->Result = 12L;
	}
	
	return(NULL);
}


/**
 *
 *   whf_addgadget
 *
**/
char *whf_addgadget( struct HostParams *hp, int n, char *args[] )
{
	char *ps = NULL;
	int x = 0, y = 0, i = 0, strwidth = 0, numgad = 0;
	ULONG ec;
	struct Gadget *g = NULL;
	/*
	 *   Check the arguments
	 */
	hp->Result = 41L;
	if (hp->Window == NULL)
		return(NULL);
	
	hp->Result = 17L;
	if ((n < 5) || (n > 7))
		return(NULL);
	/*
	 *   Top, left edge of gadget
	 */
	if (args[0])
		x = atoi(args[0]);
	if (args[1])
		y = atoi(args[1]);
	/*
	 *   Gadget text or default string
	 */
	hp->Result = 3L;
	if (args[3])
		strwidth = strlen(args[3]);
	
	ps = mymalloc(strwidth + 1);
	if (ps == NULL)
		return(NULL);
	ps[0] = '\0';
	
	if (args[3])
		strcpy(ps, args[3]);
	/*
	 *   Add the gadget
	 */
	if (n == 5) 
	{
		for (i = 0; ps[i] != '\0'; i++)
			if (ps[i] == '\\') ps[i] = '\n';

		g = MakeSBoolGad( ps,
						  hp->ReqPens->reqp_BoxPen,
						  hp->ReqPens->reqp_ShadowPen,
						  hp->ReqPens->reqp_OkayPen,
						  x,
						  y,
						  2,
						  1,
						  0x71,
						  &ec);

		g->Activation |= GADGIMMEDIATE;

	}
	else
	{
		if (args[3])
			strwidth = strlen(args[3]) * 8;
		if (args[5])
			strwidth = atoi(args[5]);
		if (strwidth < 8)
			strwidth = 8;
		if ((n == 7) && args[6]) 
		{
			if (strstr(args[6], "RIDGEBORDER"))
				strwidth = -strwidth;
			if (strstr(args[6], "NUMERIC"))
				numgad = 1;
		}

		g = MakeSStrGad( ps,
						 hp->ReqPens->reqp_BoxPen,
						 hp->ReqPens->reqp_ShadowPen,
						 x,
						 y + 1,
						 strwidth,
						 0x72,
						 &ec);
		
		if (g && numgad)
			g->Activation |= LONGINT;
	}
	if (g == NULL)
		goto cleanup;

	/*
	 *   Store the message text
	 */
	g->UserData = MakeGUData(args[2], args[4]);
	/*
	 *   Refresh the gadget
	 */
	AddGadget(hp->Window, g, -1L);
	RefreshGadgets(g, hp->Window, NULL);
	
	hp->Result = 0L;
	
	cleanup:
	if (ps)
		myfree(ps);
	
	return(NULL);
}

APTR MakeGUData(char *s, char *t)
{
	struct GUData *result = NULL;
	
	if (s == NULL || t == NULL)
		return(NULL);
	result = (struct GUData *) mymalloc(sizeof(struct GUData));
	if (result) 
	{
		result->GadgetID = mymalloc(strlen(s) + 1);
		if (result->GadgetID)
			strcpy(result->GadgetID, s);
		result->MsgText  = mymalloc(strlen(t) + 1);
		if (result->MsgText)
			strcpy(result->MsgText,  t);
	}
	return((APTR) result);
}

APTR FreeGUData( struct GUData *g )
{
	if (g) 
	{
		if (g->GadgetID)
			myfree(g->GadgetID);
		if (g->MsgText)
			myfree(g->MsgText);
		myfree(g);
	}
	return(NULL);
}

/**
 *
 *   whf_removegadget
 *
**/
char *whf_removegadget( struct HostParams *hp, int n, char *args[] )
{
	struct Gadget *gadget = NULL;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	hp->Result = 12L;
	
	gadget = hp->Window->FirstGadget;
	while (gadget) 
	{
		if ((gadget->GadgetID & 0xF0) == 0x70) 
		{
			if (gadget->UserData) 
			{
				if (strcmpu( ((struct GUData *) gadget->UserData)->GadgetID,
				args[0]) == 0) 
				{
					RemoveGadget(hp->Window, gadget);
					gadget->UserData = FreeGUData(gadget->UserData);
					FreeSimpGad(gadget);
					hp->Result = 0L;
					break;
				}
			}
		}
		gadget = gadget->NextGadget;
	}
	
	return(NULL);
}


/**
 *
 *   whf_refreshgadget
 *
**/
char *whf_refreshgadgets( struct HostParams *hp, int n, char *args[] )
{
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	if (hp->Window->FirstGadget)
		RefreshGadgets(hp->Window->FirstGadget, hp->Window, NULL);
	
	return(NULL);
}


/**
 *
 *   whf_windowtext
 *
**/
char *whf_windowtext( struct HostParams *hp, int n, char *args[] )
{
	struct SimpleRequester s;
	char *sr_prompts[SR_MAXPROMPTS];
	char *ps;

	hp->Result = 41L;

	if (hp->Window == NULL)
		return(NULL);

	if (hp->Result = checkargs(n, args))
		return(NULL);

	setmem(sr_prompts, SR_MAXPROMPTS << 2, 0);
	setmem(&s, sizeof(struct SimpleRequester), 0);
	
	hp->Result = 3L;
	ps = mymalloc(strlen(args[0]) + 1);
	if (!ps)
		return(NULL);

	DecodeAutoText(args[0], sr_prompts, ps);
	s.sr_Prompt  = sr_prompts;
	s.sr_PenList = hp->ReqPens;
	UpdateMsgWindow(hp->Window, &s);

	myfree(ps);
	
	hp->Result = 0L;
	
	return(NULL);
}


/**
 *
 *   whf_backfill
 *
**/
char *whf_backfill( struct HostParams *hp, int n, char *args[] )
{
	register long pen;
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	pen = hp->Window->RPort->FgPen;
	BlankSReqWin(hp->Window, (LONG) hp->ReqPens->reqp_BackPen);
	SetAPen(hp->Window->RPort, pen);
	
	return(NULL);
}

/**
 *
 *   whf_modifyhost
 *
**/
char *whf_modifyhost( struct HostParams *hp, int n, char *args[] )
{
	int i;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	for (i = 0; i < MAXCLASSES; i++) 
	{
		if (strcmp(args[0], ch_classtext(i)) == 0) 
		{
			if (hp->ClassText[i])
				myfree(hp->ClassText[i]);
			hp->ClassText[i] = mymalloc(strlen(args[1]) + 1);
			if (hp->ClassText[i] == NULL) 
			{
				hp->Result = 3L;
			}
			else
			{
				strcpy(hp->ClassText[i], args[1]);
			}
			return(NULL);
		}
	}
	
	hp->Result = 12L;
	
	return(NULL);
}

/**
 *
 *   whf_setnotify
 *
**/
char *whf_setnotify( struct HostParams *hp, int n, char *args[] )
{
	int i;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	for (i = 0; i < MAXCLASSES; i++) 
	{
		if (strcmp(args[0], ch_classtext(i)) == 0) 
		{
			if (hp->NotifyPortName[i])
				myfree(hp->NotifyPortName[i]);
			hp->NotifyPortName[i] = mymalloc(strlen(args[1]) + 1);
			if (hp->NotifyPortName[i] == NULL) 
			{
				hp->Result = 3L;
			}
			else
			{
				strcpy(hp->NotifyPortName[i], args[1]);
			}
			return(NULL);
		}
	}
	
	hp->Result = 12L;
	
	return(NULL);
}

/**
 *
 *   whf_windowtoback
 *
**/
char *whf_windowtoback( struct HostParams *hp, int n, char *args[] )
{
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	WindowToBack(hp->Window);
	
	return(NULL);
}

/**
 *
 *   whf_windowtofront
 *
**/
char *whf_windowtofront( struct HostParams *hp, int n, char *args[] )
{
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	WindowToFront(hp->Window);
	
	return(NULL);
}

/**
 *
 *   whf_activatewindow
 *
**/
char *whf_activatewindow( struct HostParams *hp, int n, char *args[] )
{
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	ActivateWindow(hp->Window);
	
	return(NULL);
}

/**
 *
 *   whf_setfont
 *
**/
char *whf_setfont( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	struct TextAttr attr;
	int xsize = 0;
	struct TextFont *font;
	
	hp->Result = 41L;
	
	if (DiskfontBase == NULL)
		return(NULL);
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	attr.ta_Name  = (STRPTR) args[0];
	attr.ta_YSize = atoi(args[1]);
	attr.ta_Style = 0;
	attr.ta_Flags = 0;
	
	if ((n >= 3) && args[2])
		attr.ta_Style = atoi(args[2]);
	if ((n >= 4) && args[3])
		attr.ta_Flags = atoi(args[3]);
	if ((n >= 5) && args[4])
		xsize         = atoi(args[4]);
	
	if (font = GetFont(&attr, xsize)) 
	{
		if (hp->Font)
			CloseFont(hp->Font);
		SetFont(rp, font);
		if (font->tf_Style != attr.ta_Style) 
		{
			SetSoftStyle(rp, (ULONG) font->tf_Style ^ (ULONG) attr.ta_Style, 0xFF);
		}
		hp->Font = font;
	}
	else
	{
		hp->Result = 12L;
	}
	
	return(NULL);
}

/**
 *
 *   Get a specific font
 *
**/
static struct TextFont *GetFont( struct TextAttr *attr, int xsize )
{
	struct TextFont *font = NULL;
	
	font = (struct TextFont *) OpenFont(attr);
	if (font) 
	{
		if ((font->tf_YSize != attr->ta_YSize) ||
		(xsize && (font->tf_XSize != xsize))    ) 
		{
			CloseFont(font);
			font = NULL;
		}
	}
	if (!font)
		font = (struct TextFont *) OpenDiskFont(attr);
	if (font) 
	{
		if ((font->tf_YSize != attr->ta_YSize) ||
		(xsize && (font->tf_XSize != xsize))    ) 
		{
			CloseFont(font);
			font = NULL;
		}
	}
	return(font);
}

/**
 *
 *   whf_activategadget
 *
**/
char *whf_activategadget( struct HostParams *hp, int n, char *args[] )
{
	struct Gadget *gadget;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	hp->Result = 12L;
	
	gadget = hp->Window->FirstGadget;
	while (gadget) 
	{
		if (gadget->GadgetID == 0x72) 
		{
			if (gadget->UserData) 
			{
				if (strcmpu( ((struct GUData *) gadget->UserData)->GadgetID,
				args[0]) == 0) 
				{
					ActivateGadget(gadget, hp->Window, NULL);
					hp->Result = 0L;
					break;
				}
			}
		}
		gadget = gadget->NextGadget;
	}
	
	return(NULL);
}

/**
 *
 *   whf_setgadget
 *
**/
char *whf_setgadget( struct HostParams *hp, int n, char *args[] )
{
	struct Gadget *gadget;
	int setit;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	hp->Result = 17L;
	
	if      (strcmpu(args[1], "ON")  == 0)
		setit = 1;
	else
	if (strcmpu(args[1], "OFF") == 0)
		setit = 0;
	else
		return(NULL);
	
	hp->Result = 12L;
	
	gadget = hp->Window->FirstGadget;
	while (gadget) 
	{
		if (gadget->GadgetID == 0x71) 
		{
			if (gadget->UserData) 
			{
				if (strcmpu( ((struct GUData *) gadget->UserData)->GadgetID,
				args[0]) == 0) 
				{
					if (setit) 
					{
						GadMXSet(hp->Window, gadget);
					}
					else
					{
						GadMXClr(hp->Window, gadget);
					}
					hp->Result = 0L;
					break;
				}
			}
		}
		gadget = gadget->NextGadget;
	}
	
	return(NULL);
}

/**
 *
 *   whf_readgadget
 *
**/
char *whf_readgadget( struct HostParams *hp, int n, char *args[] )
{
	struct Gadget *gadget;
	char *sg = NULL;
	int num = -1, na = 0;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	hp->Result = 12L;
	
	gadget = hp->Window->FirstGadget;
	while (gadget) 
	{
		if (gadget->GadgetID == 0x72) 
		{
			if (gadget->UserData) 
			{
				if (strcmpu( ((struct GUData *) gadget->UserData)->GadgetID,
				args[0]) == 0) 
				{
					sg = NULL;
					
					if ((gadget->GadgetID & 0x0F) == 0x02)
						sg = (char *)
					((struct StringInfo*) (gadget->SpecialInfo))->Buffer;
					
					na = ch_makestr(hp, ((struct GUData *) gadget->UserData)->MsgText,
					GADGETUP, 0, 0,
					((struct GUData *) gadget->UserData)->GadgetID,
					sg);
					
					num = ch_classtoindex(GADGETUP);
					
					if (na) 
					{
						if (SendPortMsg(hp->NotifyPortName[num], na, hp->MsgPtr,
							hp->MasterPort) == 0) hp->MsgCount++;
					}
					
					hp->Result = 0L;
					break;
				}
			}
		}
		gadget = gadget->NextGadget;
	}
	
	return(NULL);
}

/**
 *
 *   whf_readhost
 *
**/
char *whf_readhost( struct HostParams *hp, int n, char *args[] )
{
	int na = 0;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	hp->Result = 12L;
	
	na = ch_makestr(hp, args[1], 0, 0, 0, NULL, NULL);
	
	if (na) 
	{
		if (SendPortMsg(args[0], na, hp->MsgPtr, hp->MasterPort) == 0) 
		{
			hp->MsgCount++;
		}
	}
	
	hp->Result = 0L;
	
	return(NULL);
}


/**
 *
 *   whf_addmenu
 *
**/
char *whf_addmenu( struct HostParams *hp, int n, char *args[] )
{
	struct Menu *menu = NULL, *temp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	menu = hp->Window->MenuStrip;
	if (menu)
		ClearMenuStrip(hp->Window);
	
	hp->Result = 12L;
	temp = SMenu(menu, args[0]);
	
	if (temp == NULL) 
	{
		if (menu)
			freesmenu(menu);
		return(NULL);
	}
	
	hp->Result = 0L;
	if (menu == NULL)
		menu = temp;
	
	SetMenuStrip(hp->Window, menu);
	
	return(NULL);
}

/**
 *
 *   whf_remmenu
 *
**/
char *whf_remmenu( struct HostParams *hp, int n, char *args[] )
{
	struct Menu *menu = NULL;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	menu = hp->Window->MenuStrip;
	if (menu) 
	{
		ClearMenuStrip(hp->Window);
		freesmenu(menu);
	}
	
	return(NULL);
}

freesmenu( struct Menu *menu )
{
	struct Menu *tempm;
	struct MenuItem *tempi, *temps;
	struct XMenuItem *tempxi, *tempxs;
	
	if (tempm = menu) 
	{
		while (tempm) 
		{
			if (tempi = tempm->FirstItem) 
			{
				while (tempi) 
				{
					if (temps = tempi->SubItem) 
					{
						while (temps) 
						{
							tempxs = (struct XMenuItem *) temps;
							if (tempxs->UserData)
								myfree(tempxs->UserData);
							temps = temps->NextItem;
						}
					}
					tempxi = (struct XMenuItem *) tempi;
					if (tempxi->UserData)
						myfree(tempxi->UserData);
					tempi = tempi->NextItem;
				}
			}
			tempm = tempm->NextMenu;
		}
	}
	FreeSMenu(menu);
	return;
}

/**
 *
 *   whf_additem
 *
**/
char *whf_additem( struct HostParams *hp, int n, char *args[] )
{
	struct Menu *menu = NULL;
	struct XMenuItem *temp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	hp->Result = 17L;
	if (args[0] == NULL)
		return(NULL);
	if (args[1] == NULL)
		return(NULL);
	
	hp->Result = 12L;
	if ((menu = hp->Window->MenuStrip) == NULL)
		return(NULL);
	
	ClearMenuStrip(hp->Window);
	
	if (n == 5 && (strncmpu(args[4], "SAME", 4) == 0)) 
	{
		temp = (struct XMenuItem *) SItemB(menu, args[0],
		args[2] ? args[2][0] : 0,
		args[3] ? atol(args[3]) : 0L);
	}
	else
	{
		temp = (struct XMenuItem *) SItem(menu, args[0],
		args[2] ? args[2][0] : 0,
		args[3] ? atol(args[3]) : 0L);
	}
	
	if (temp) 
	{
		hp->Result = 0L;
		temp->UserData = mymalloc(strlen(args[1]) + 1);
		if (temp->UserData)
			strcpy(temp->UserData, args[1]);
	}
	
	AdjustItems(menu);
	
	SetMenuStrip(hp->Window, menu);
	
	return(NULL);
}

/**
 *
 *   whf_addsubitem
 *
**/
char *whf_addsubitem( struct HostParams *hp, int n, char *args[] )
{
	struct Menu *menu = NULL;
	struct XMenuItem *temp;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	hp->Result = 17L;
	if (args[0] == NULL)
		return(NULL);
	if (args[1] == NULL)
		return(NULL);
	
	hp->Result = 12L;
	if ((menu = hp->Window->MenuStrip) == NULL)
		return(NULL);
	
	hp->Result = 0L;
	ClearMenuStrip(hp->Window);
	
	if (n <= 4) 
	{
		temp = (struct XMenuItem *) SSItem(menu, args[0],
		args[2] ? args[2][0] : 0,
		args[3] ? atol(args[3]) : 0L);
	}
	else
	{
		temp = (struct XMenuItem *) SSItemB(menu, args[0],
		args[2] ? args[2][0] : 0,
		args[3] ? atol(args[3]) : 0L);
	}
	
	if (temp) 
	{
		hp->Result = 0L;
		temp->UserData = mymalloc(strlen(args[1]) + 1);
		if (temp->UserData)
			strcpy(temp->UserData, args[1]);
	}
	
	AdjustItems(menu);
	
	SetMenuStrip(hp->Window, menu);
	
	return(NULL);
}

/**
 *
 *   whf_setreqcolor
 *
**/
char *whf_setreqcolor( struct HostParams *hp, int n, char *args[] )
{
	hp->Result = 41L;
	
	if (hp->Result = checkargs(n, args))
		return(NULL);
	
	if      (strncmpu(args[0], "BLO", 3) == 0) 
	{
		hp->ReqPens->reqp_BlockPen  = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "DET", 3) == 0) 
	{
		hp->ReqPens->reqp_DetailPen = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "BAC", 3) == 0) 
	{
		hp->ReqPens->reqp_BackPen   = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "PRO", 3) == 0) 
	{
		hp->ReqPens->reqp_PromptPen = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "BOX", 3) == 0) 
	{
		hp->ReqPens->reqp_BoxPen    = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "SHA", 3) == 0) 
	{
		hp->ReqPens->reqp_ShadowPen = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "OKA", 3) == 0) 
	{
		hp->ReqPens->reqp_OkayPen   = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "GTE", 3) == 0) 
	{
		hp->ReqPens->reqp_OkayPen   = atoi(args[1]);
	}
	else
	if (strncmpu(args[0], "CAN", 3) == 0) 
	{
		hp->ReqPens->reqp_CancelPen = atoi(args[1]);
	}
	
	return(NULL);
}


/**
 *
 *   whf_setitem
 *
**/
char *whf_setitem( struct HostParams *hp, int n, char *args[] )
{
	struct Menu *menu = NULL;
	struct MenuItem *item = NULL;
	USHORT menunum = 0;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	
	hp->Result = 17L;
	if (args[3] == NULL)
		return(NULL);
	
	hp->Result = 12L;
	if ((menu = hp->Window->MenuStrip) == NULL)
		return(NULL);
	
	menunum  = ((args[2] ? atoi(args[2]) : -1) & 0x1F) << 11;
	menunum |= ((args[1] ? atoi(args[1]) : -1) & 0x3F) << 5 ;
	menunum |= ((args[0] ? atoi(args[0]) : -1) & 0x1F)      ;
	
	item = ItemAddress(menu, (ULONG) menunum);
	
	ClearMenuStrip(hp->Window);
	
	if (item) 
	{
		if (item->Flags & CHECKIT) 
		{
			if (strncmpu(args[3], "ON", 2) == 0)
				item->Flags |= CHECKED;
			else
				item->Flags &= ~CHECKED;
		}
		hp->Result = 0L;
	}
	
	SetMenuStrip(hp->Window, menu);
	
	return(NULL);
}

/**
 *
 *   whf_drawiff
 *
**/
char *whf_drawiff( struct HostParams *hp, int n, char *args[] )
{
	struct RastPort *rp;
	int x = 0, y = 0, w = 0, h = 0;
	struct Screen *screen = NULL;
	
	hp->Result = 41L;
	
	if (hp->Window == NULL)
		return(NULL);
	rp = hp->Window->RPort;
	
	if (hp->Result = checkargs(1, args))
		return(NULL);
	
	if (args[1])
		x = atoi(args[1]);
	else
		x = hp->Window->BorderLeft;
	
	if (args[2])
		y = atoi(args[2]);
	else
		y = hp->Window->BorderTop;
	
	if (args[3])
		w = atoi(args[3]);
	else
		w = hp->Window->Width - hp->Window->BorderRight - x;
	
	if (args[4])
		h = atoi(args[4]);
	else
		h = hp->Window->Height - hp->Window->BorderBottom - y;
	
	screen = hp->Window->WScreen;
	if ((screen->Flags & SCREENTYPE) == WBENCHSCREEN)
		screen = NULL;
	if (args[5]) 
	{
		if ((args[5][0] | ' ') == 'n')
			screen = NULL;
	}
	
	hp->Result = 12L;
	if (DrawIFF(args[0], rp, x, y, w, h, screen) == 0)
		hp->Result = 0L;
	
	return(NULL);
}

