#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <intuition/intuitionbase.h>
#include <intuition/classusr.h>
#include <intuition/gadgetclass.h>
#include <intuition/cghooks.h>
#include <intuition/icclass.h>
#include <intuition/classes.h>
#include <intuition/sghooks.h>
#include <intuition/screens.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/datatypes.h>
#include <datatypes/pictureclass.h>
#include <libraries/SysInfo.h>
#include <libraries/gadtools.h>
#include <graphics/gfxbase.h>
#include <graphics/text.h>
#include <graphics/gfxmacros.h>
#include <utility/tagitem.h>
#include <utility/hooks.h>
#include <string.h>
#include <clib/macros.h>
#include "gaugeclass.h"
#include "launchclass.h"
#include "tinymeter.h"

extern struct Library *RetinaBase;

extern ULONG    maximum,
		idle;

void drawBackground(struct tm_sys_set *set,struct tm_data *data)
{
    struct RastPort *rp=data->win->RPort;
    UWORD  x,y;

    x=data->win->Width-1;
    y=data->win->Height-1;

    if(data->bg_bm)
    {
	BltBitMapRastPort(data->bg_bm,0,0,rp,0,0,x+1,y+1,0xc0);
	FreeBitMap(data->bg_bm); data->bg_bm=0L;
    }
    else
    {
	SetAPen(rp,data->bg_color);
	RectFill(rp,0,0,x,y);
    }

    SetAPen(rp,data->bright_color);
    switch (set->bd_type)
    {
	case    bd_simple:
		RectFill(rp,0,0,x,0);
		RectFill(rp,0,0,0,y);
		RectFill(rp,x,0,x,y);
		RectFill(rp,0,y,x,y);
		break;
	case    bd_standard:
		RectFill(rp,0,0,x,0);
		RectFill(rp,0,0,0,y);
		SetAPen(rp,data->dark_color);
		RectFill(rp,x,1,x,y);
		RectFill(rp,1,y,x,y);
		break;
	case    bd_double:
		SetAPen(rp,data->dark_color);
		RectFill(rp,0,0,x,0);
		RectFill(rp,0,0,0,y);
		RectFill(rp,1,y-1,x-1,y-1);
		RectFill(rp,x-1,2,x-1,y-1);
		SetAPen(rp,data->bright_color);
		RectFill(rp,x,1,x,y);
		RectFill(rp,1,y,x,y);
		RectFill(rp,1,1,x-1,1);
		RectFill(rp,1,1,1,y-1);
		break;
    }
}

void allocGadgets(struct tm_sys_set *set, struct tm_data *data, Class *gclass, Class *lclass)
{
    struct  tm_gau_set *many;
    ULONG   i;
    int     foo;
    
    for(i=0,many=data->list;i<data->num_of_gaug;i++)
    {
	switch (many->type)
	{
	    case    typ_none:
		    data->gdg[i]=0L;
		    foo=0;
		    break;
	    case    typ_simplelauncher:
	    case    typ_iconlauncher:
		    foo=2;
		    break;
	    default:
		    foo=1;
		    break;
	}
	switch (foo)
	{
	    case    1 :  data->gdg[i]=(struct Gadget *)NewObject(gclass, NULL,
			    GA_ID,              0xFFF4,
			    GA_Top,             calcYPos(i,data,set),
			    GA_Left,            calcXPos(i,data,set),
			    GA_Width,           data->gauge_x_size,
			    GA_Height,          data->gauge_y_size_falling[i],
			    GAU_Type,           many->gauge_type,
			    GAU_Label,          many->label,
			    GAU_TextFormat,     many->format,
			    GAU_TextFont,       data->Font[i],
			    GAU_LabelPos,       data->labelpos,
			    GAU_FmtIndent,      many->indent,

			    GAU_3D,             many->sty_3d,
			    GAU_Border,         many->sty_border,
			    GAU_Background,     many->sty_bg,
			    GAU_ShadowLabel,    many->sty_shadow,
			    GAU_NoGauge,        many->sty_nogauge,
			    GAU_NoFormat,       many->sty_noformat,
			    GAU_NoBase,         many->sty_nobase,
			    GAU_Expansion,      &many->expansion[0],
			    GAU_DisWhat,        many->type,

			    GAU_ColLabel,       &many->Colors[col_label],
			    GAU_ColFormat,      &many->Colors[col_format],
			    GAU_ColBase,        &many->Colors[col_base],
			    GAU_ColCurrent,     &many->Colors[col_current],
			    GAU_ColNegative,    &many->Colors[col_negative],
			    GAU_ColBrightEdg,   &many->Colors[col_bright],
			    GAU_ColDarkEdg,     &many->Colors[col_dark],
			    GAU_ColBackground,  &many->Colors[col_bg],

			    TAG_END);
			if(!data->gdg[i]) show(err_noboo);
			break;
	    case    2 :
			data->gdg[i]=(struct Gadget *)NewObject(lclass, NULL,
			    GA_ID,              i,
			    GA_Top,             calcYPos(i,data,set),
			    GA_Left,            calcXPos(i,data,set),
			    GA_Width,           data->gauge_x_size,
			    GA_Height,          data->gauge_y_size_falling[i],
			    GA_Immediate,       TRUE,
			    GA_RelVerify,       TRUE,

			    LAU_File,           &many->expansion[0],
			    LAU_TextFont,       data->Font[i],
			    LAU_3D,             many->sty_3d,
			    LAU_Border,         many->sty_border,
			    LAU_Background,     many->sty_bg,
			    LAU_ShadowLabel,    many->sty_shadow,
			    LAU_Down,           many->sty_nogauge,
			    LAU_Transparent,    many->sty_noformat,
			    LAU_Screen,         data->scr,
			    LAU_Indent,         many->indent,
			    LAU_Type,           many->type,

			    LAU_ColLabel,       &many->Colors[col_label],
			    LAU_ColEntry,       &many->Colors[col_format],
			    LAU_ColHalfBright,  &many->Colors[col_base],
			    LAU_ColHalfShadow,  &many->Colors[col_current],
			    LAU_ColCursor,      &many->Colors[col_negative],
			    LAU_ColBrightEdg,   &many->Colors[col_bright],
			    LAU_ColDarkEdg,     &many->Colors[col_dark],
			    LAU_ColBackground,  &many->Colors[col_bg],
			    TAG_END);
			if(!data->gdg[i]) show(err_noboo);
			break;


	}
	many=many->next;
    }
}

void removeGadgets(struct tm_sys_set *set, struct tm_data *data)
{
    int i;

    for(i=0;i<data->num_of_gaug;i++)
	if(data->gdg[i])
	{
	    DisposeObject(data->gdg[i]);
	    data->gdg[i]=0L;
	}
}
