/*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
                       Matthias Münch (matthias@amigaworld.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    In addition, as a special exception, Tom Parker and Matthias Münch give
    permission to link the code of this program with a TCP stack of your 
    choice, any official MUI libraries or classes and any custom MUI classes
    that should be necessary for the operation of this program.  This 
    exception also gives you permission to distribute linked combinations 
    including this software with any of the before-mentioned libraries and 
    classes.  You must obey the GNU General Public License in all respects for
    all of the code used other than that provided by the before-mentioned 
    libraries and classes.  As part of this exception you are obliged to 
    follow the license terms of the before-mentioned libraries, this license
    does not compel you to follow those terms, but if you do not then you may
    not link with those libraries.  If you modify this file, you may extend
    this exception to your version of the file, but you are not obligated to
    do so.  If you do not wish to do so, delete this exception statement from
    your version.
*/
/*
** MUI Helper
*/

#include <sys/types.h>
#include <stdlib.h>
#include <stdarg.h>

#include <libraries/gadtools.h>

#include <MUI/NListview_mcc.h>

#include "muihelp.h"

#ifdef __GNUC__
/* inline version of this function doesn't work well */
/* so this is the vararg stub which calls MUI_NewObjectA */
static ULONG var2stack(int more, Tag tag1, va_list ap)
{
	static ULONG mav[50];
	int i = 0;

	if(more) i = more;
	mav[i] = tag1;
	if(mav[i] != TAG_DONE)
	{
		mav[i+1] = va_arg(ap, ULONG);
		i += 2;
		while(i<49)
		{
			mav[i] = va_arg(ap, ULONG);
			if(mav[i] == TAG_DONE) break;
			mav[i+1] = va_arg(ap, ULONG);
			if(mav[i] == TAG_MORE) break;
			i += 2;
		}
	}

	if(more) return((ULONG)&mav[more]);
	return (ULONG)&mav[0];
}


Object *MUI_NewObject(char *classname, Tag tag1,...)
{
	struct TagItem *ti;
	va_list ap;

	va_start(ap, tag1);
	ti = (struct TagItem *)var2stack(0, tag1, ap);
	va_end(ap);

	return(MUI_NewObjectA(classname, ti));
}
#endif


#ifdef __MORPHOS__
/* ppc passes varargs on both registers and stack, so this is necessary */
ULONG DoSuperNew(struct IClass *cl, Object *obj, ...)
{
	ULONG mav[50];
	ULONG ret;
	va_list tags;
	struct opSet myopSet;
	int i=0;

	va_start(tags, obj);
	while(i<50)
	{
		mav[i] = va_arg(tags, ULONG);
		if(mav[i] == TAG_DONE) break;
		mav[i+1] = va_arg(tags, ULONG);
		if(mav[i] == TAG_MORE) break;
		i += 2;
	}
	va_end(tags);

	myopSet.MethodID = OM_NEW;
	myopSet.ops_AttrList = (struct TagItem *) &mav;
	myopSet.ops_GInfo = NULL;
	ret = DoSuperMethodA(cl, obj, (APTR)&myopSet);

	return ret;
}
#else
ULONG __stdargs DoSuperNew(struct IClass *cl,Object *obj,ULONG tag1,...)
{
	return(DoSuperMethod(cl, obj, OM_NEW, &tag1, NULL));
}
#endif


Object *mui_pix(char *fname, muidefpix *dp, ULONG tag1, ...)
{
	Object *obj;
#ifdef __MORPHOS__
	struct TagItem *ti;
	va_list ap;

	va_start(ap, tag1);
	ti = (struct TagItem *)var2stack(25, tag1, ap);
	va_end(ap);
#endif

	if(fname)
	{
		obj = MUI_NewObject("Dtpic.mui",
			MUIA_Dtpic_Name, fname,
#ifdef __MORPHOS__
			TAG_MORE, ti,
#else
			TAG_MORE, &tag1,
#endif
			TAG_DONE);

		if(obj) return(obj);
	}

	if(!dp) return(NULL);

	obj = MUI_NewObject(MUIC_Bodychunk,
		MUIA_FixWidth, dp->w,
		MUIA_FixHeight, dp->h,
		MUIA_Bitmap_UseFriend, TRUE,
		MUIA_Bitmap_Width, dp->w,
		MUIA_Bitmap_Height, dp->h,
		MUIA_Bitmap_SourceColors, dp->colors,
		MUIA_Bodychunk_Body, dp->body,
		MUIA_Bodychunk_Depth, dp->d,
		MUIA_Bodychunk_Compression, dp->comp,
		MUIA_Bodychunk_Masking, dp->mask,
#ifdef __MORPHOS__
		TAG_MORE, ti,
#else
		TAG_MORE, &tag1,
#endif
		TAG_DONE);

	return obj;
}


int mui_classes_setup(muiclass mcl[])
{
	int i=0;
	struct MUI_CustomClass *tmp;

	while(mcl[i].supername)
	{
		tmp = MUI_CreateCustomClass(NULL, mcl[i].supername, NULL, mcl[i].datasize, mcl[i].dispatcher);
		if(!tmp) return 0;
		*mcl[i].ptr = tmp;
		i++;
	}
	return 1;
}


void mui_classes_cleanup(muiclass mcl[])
{
	int i=0;

	while(mcl[i].supername)
	{
		if(*mcl[i].ptr) MUI_DeleteCustomClass(*mcl[i].ptr);
		i++;
	}
}


/*
char *mui_string_file(
{

}


char *mui_string(int max)
{
	Object *str;

	if(max == 0) max = 256;
	str = StringObject,
		StringFrame,
		MUIA_String_AdvanceOnCR, TRUE,
		MUIA_String_MaxLen, max,
		MUIA_CycleChain, 1,
		TAG_DONE);

	return str;
}
*/


char *mui_sget(Object *sobj)
{
	char *tmp;
	GetAttr(MUIA_String_Contents, sobj, (u_long *)&tmp);
	if(tmp && tmp[0]!='\0') return(tmp);
	return NULL;
}


u_long mui_id(Object *app, u_char *pre)
{
	static const char values[] = "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
	u_long bitmap = 0;
	long id = 0;
	struct List *wins;
	struct Node *lala;
	Object *win;
	int i;

	if(!pre[0] || !pre[1] || !pre[2]) return(0);
	id = (pre[0] << 24) | (pre[1] << 16) | (pre[2] << 8);

	GetAttr(MUIA_Application_WindowList, app, (u_long *)&wins);
	if(!wins) return(0);
	lala = wins->lh_Head;
	while(win = NextObject(&lala))
	{
		long id2;
		char *s;
		GetAttr(MUIA_Window_ID, win, (u_long *)&id2);
		if(id == (id2 & 0xFFFFFF00))
		{
			s = strchr(values, id2 & 0xFF);
			if(s) bitmap |= (1L << (s - values));
		}
	}

	for(i=0; i<sizeof(values); i++)
	{
		if((bitmap & (1L << i)) == 0)
		{
			id |= values[i];
			return id;
		}
	}

	id |= values[0];
	return id;
}


Object *mui_button(const UBYTE *label)
{
	Object *but;

	but = MUI_MakeObject(MUIO_Button, label);
	if(but) set(but, MUIA_CycleChain, 1);
	return but;
}


Object *mui_tmenu(const UBYTE *msg)
{
	char *title = (char *)msg;

	if(title[1]==0)
		return(MUI_NewObject(MUIC_Menuitem,
		       MUIA_Menuitem_Title, title+2,
		       MUIA_Menuitem_Shortcut, title,
		       MUIA_Menuitem_Toggle, TRUE,
		       MUIA_Menuitem_Checkit, TRUE,
		       TAG_DONE));
	else
		return(MUI_NewObject(MUIC_Menuitem,
		       MUIA_Menuitem_Title, title,
		       MUIA_Menuitem_Toggle, TRUE,
		       MUIA_Menuitem_Checkit, TRUE,
		       TAG_DONE));
}


Object *mui_menu(const UBYTE *msg)
{
	char *title = (char *)msg;

	if(msg==NULL) return(MUI_NewObject(MUIC_Menuitem, MUIA_Menuitem_Title, NM_BARLABEL, TAG_DONE));

	if(title[1]==0)
		return(MUI_NewObject(MUIC_Menuitem,
		       MUIA_Menuitem_Title, title+2,
		       MUIA_Menuitem_Shortcut, title,
		       TAG_DONE));
	else
		return(MUI_NewObject(MUIC_Menuitem, MUIA_Menuitem_Title , title, TAG_DONE));
}


Object *mui_toggle(const UBYTE *msg, Object **tog)
{
	Object *obj,*obj2;

	obj = MUI_NewObject(MUIC_Group,
		MUIA_Group_Horiz, TRUE,
		Child, obj2 = MUI_NewObject(MUIC_Image,
			ImageButtonFrame,
			MUIA_Background, MUII_ButtonBack,
			MUIA_InputMode, MUIV_InputMode_Toggle,
			MUIA_Image_Spec, MUII_CheckMark,
			MUIA_Image_FreeVert, TRUE,
			MUIA_ShowSelState, FALSE,
			MUIA_CycleChain, 1,
		TAG_DONE),
		Child, LLabel1(msg),
		Child, HSpace(0),
	TAG_DONE);

	if(obj) {
		*tog = obj2;
		return(obj);
	} else
		return(NULL);
}
