/*
 *  help_macros.c - helpful little functions
 *  
 */

#include <string.h>

#include "prefs.h"
#include "macros.h"

/*
 * prototypes
 */

static void REGARGS IntuiMsgFunc(REG(a1, struct IntuiMessage*),REG(a2, struct FileRequester*));

/*
 * functions
 */

Object* 
MakeString(int maxlen, char* str)
{
	Object* obj = MUI_MakeObject(MUIO_String,str,maxlen);

	if (obj)
	{
		SetAttrs(obj,
		MUIA_CycleChain,1,
		MUIA_String_AdvanceOnCR,TRUE,
		TAG_DONE);
	}
	return obj;
}

Object*
MakeCheck(char* str)
{
	Object* obj = MUI_MakeObject(MUIO_Checkmark,str);
	if (obj)
		set(obj,MUIA_CycleChain,1);
	return obj;
}

Object*
MakeLLabel1(char* str)
{
	return MUI_MakeObject(  MUIO_Label,str,
							MUIO_Label_LeftAligned|MUIO_Label_SingleFrame);
}

Object* 
MakeSlider(int min,int max, char* str)
{
		Object* obj = MUI_MakeObject(MUIO_Slider, str, min, max);
		if (obj)
			set(obj, MUIA_CycleChain, 1);
		return obj;
}

ULONG STDARGS
DoSuperNew(struct IClass* cl,Object* obj,ULONG tag1,...)
{
	return DoSuperMethod(cl, obj, OM_NEW, &tag1, NULL);
}

BOOL
getbool(Object* obj)
{
		return (BOOL)xget(obj,MUIA_Selected);
}

LONG
xget(Object* obj,ULONG attribute)
{
	LONG x;
	get(obj, attribute, &x);
	return x;
}

Object* MakeButton(char* str)
{
	Object* obj = MUI_MakeObject(MUIO_Button, str);
	if (obj)
		set(obj, MUIA_CycleChain, 1);
	return obj;
}

static void REGARGS SAVEDS
IntuiMsgFunc(REG(a1, struct IntuiMessage* imsg),REG(a2, struct FileRequester* req))
{
	if (imsg->Class == IDCMP_REFRESHWINDOW)
		DoMethod(req->fr_UserData, MUIM_Application_CheckRefresh);
}

char*
getfilename(Object* win, char* title, char* path, char* pattern, BOOL save)
{
	static char buf[512];
	struct FileRequester* req;
	struct Window* w;
	static LONG left = -1,top = -1,width = -1,height = -1;
	Object* app = (Object *)xget(win, MUIA_ApplicationObject);
	char* res = NULL;
	static const struct Hook IntuiMsgHook = { { 0,0 },(void *)IntuiMsgFunc,NULL,NULL };

	get(win,MUIA_Window_Window,&w);
	if (left == -1)
	{
		left   = w->LeftEdge+w->BorderLeft+2;
		top    = w->TopEdge+w->BorderTop+2;
		width  = w->Width-w->BorderLeft-w->BorderRight-4;
		height = w->Height-w->BorderTop-w->BorderBottom-4;
	}

	if (req = MUI_AllocAslRequestTags(ASL_FileRequest,
				ASLFR_Window, w,
				ASLFR_TitleText, title,
				ASLFR_InitialLeftEdge, left,
				ASLFR_InitialTopEdge , top,
				ASLFR_InitialWidth   , width,
				ASLFR_InitialHeight  , height,
				ASLFR_InitialDrawer  , path,
				ASLFR_InitialPattern , pattern,
				ASLFR_DoSaveMode     , save,
				ASLFR_DoPatterns     , TRUE,
				ASLFR_RejectIcons    , TRUE,
				ASLFR_UserData       , app,
				ASLFR_IntuiMsgFunc   , &IntuiMsgHook,
				TAG_DONE))
	{
		set(app, MUIA_Application_Sleep, TRUE);
		if (MUI_AslRequestTags(req, TAG_DONE))
		{
			if (*req->fr_File)
			{
				res = buf;
				stccpy(buf, req->fr_Drawer, sizeof (buf));
				AddPart(buf, req->fr_File, sizeof (buf));
			}
			left   = req->fr_LeftEdge;
			top    = req->fr_TopEdge;
			width  = req->fr_Width;
			height = req->fr_Height;
		}
		MUI_FreeAslRequest(req);
		set(app, MUIA_Application_Sleep, FALSE);
	}
	return res;
}
