#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/imageclass.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 <graphics/gfxbase.h>
#include <graphics/text.h>
#include <graphics/gfxmacros.h>
#include <utility/tagitem.h>
#include <utility/hooks.h>
#include <string.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/datatypes.h>
#include <datatypes/pictureclass.h>
#include "gaugeclass.h"
#include "tinymeter.h"

/*
    Perhaps the next two functions will be useful for some of you... 8))
    I searched a long time for something similar, finally I did them myself.
    Please leave me a mail, if you are using them...
*/
/*
BltTLine(struct BitMap *src, UWORD s_x, UWORD s_y, struct RastPort *dest, UWORD d_x, UWORD d_y, UWORD h, UBYTE t_col, UBYTE *mask)
{
    ULONG           i,k,pos,skip;
    UBYTE           *s1,*s2,*s3;
    BOOL            firstrun=FALSE;
    if( mask)
    {
	pos =((s_x+7)>>3);
	skip=src->BytesPerRow;

	for(i=0;i<src->Depth;i++)
	{
	    s1 = (UBYTE *)mask+(UBYTE *)pos;
	    s2 = (UBYTE *)src->Planes[i]+(UBYTE *)pos;
	    if(firstrun)
	    {
		if((1<<i)&t_col) for(k=0;k<src->Rows;k++,s1+=skip,s2+=skip) *s1&=         *s2;
		else             for(k=0;k<src->Rows;k++,s1+=skip,s2+=skip) *s1&=       ~(*s2);
	    }
	    else
	    {
		firstrun=TRUE;
		if((1<<i)&t_col) for(k=0;k<src->Rows;k++,s1+=skip,s2+=skip) *s1=(0xFF)&  (*s2);
		else             for(k=0;k<src->Rows;k++,s1+=skip,s2+=skip) *s1=(0xFF)&(~(*s2));
	    }
	}
	BltMaskBitMapRastPort(src,s_x,s_y,dest,d_x,d_y,1,h,0xb0,mask);
    }
}

*/
BltT(struct BitMap *src, UWORD s_x, UWORD s_y, struct RastPort *dest, UWORD d_x, UWORD d_y, UWORD w, UWORD h, UBYTE t_col, UBYTE *mask)
{
    ULONG           i,k;
    UBYTE           *s1,*s2,*s3;
    BOOL            allocated=FALSE,firstrun=FALSE;
    k = src->BytesPerRow*src->Rows;
    if(!mask) { mask=(UBYTE *)AllocVec(k,MEMF_CHIP); allocated=TRUE; }
    if( mask)
    {
	s3 = mask+k+1;
	for(i=0;i<src->Depth;i++)
	{
	    s1 = (UBYTE *)mask;
	    s2 = (UBYTE *)src->Planes[i];
	    if(firstrun)
	    {
		if((1<<i)&t_col) for(;s1<s3;s1++) *s1&=         *(s2++);
		else             for(;s1<s3;s1++) *s1&=       ~(*(s2++));
	    }
	    else
	    {
		firstrun=TRUE;
		if((1<<i)&t_col) for(;s1<s3;s1++) *s1=(0xFF)&  (*(s2++));
		else             for(;s1<s3;s1++) *s1=(0xFF)&(~(*(s2++)));
	    }
	}
	BltMaskBitMapRastPort(src,s_x,s_y,dest,d_x,d_y,w,h,0xb0,mask);
	if(allocated) FreeVec(mask);
    }
}

DrawDTObject(Object *src, UWORD s_x, UWORD s_y, struct RastPort *dest, UWORD d_x, UWORD d_y, WORD w, WORD h, BOOL really)
{
    UBYTE               *ColorTable;
    struct BitMapHeader *bmhd;
    struct BitMap       *bm;
    GetDTAttrs(src,PDTA_BitMapHeader,&bmhd,PDTA_DestBitMap,&bm,PDTA_ColorTable,&ColorTable,TAG_DONE);
    if(w=-1)w=bmhd->bmh_Width;
    if(h=-1)h=bmhd->bmh_Height;
    if (!bm) GetDTAttrs(src,PDTA_BitMap,&bm,TAG_DONE);
    if((bmhd->bmh_Masking==mskHasTransparentColor)&&(ColorTable)&&(really!=FALSE))
	BltT(bm,s_x,s_y,dest,d_x,d_y,w,h,ColorTable[bmhd->bmh_Transparent],NULL);
    else
	BltBitMapRastPort(bm,s_x,s_y,dest,d_x,d_y,w,h,0xc0);
}

int my_strlen(char s[])
{
   int i=0;
   while (s[i]!='\0')++i;
   return(i);
}

UBYTE long_2_string_with_thousand(ULONG num, char output[], char point, BOOL negative)
{
    char             output_private[16];
    char             n      = 0;
    char             n_1    = 0;
    if(negative)output[n_1++]='-';
    sprintf(output_private,"%12ld",num);
    while(output_private[n]==0x20)n++;
    for(;(output_private[n]!=0)&(output_private[n]!=0x20);n++)
    {
	output[n_1++]=output_private[n];
	if((n==2)||(n==5)||(n==8)) output[n_1++]=(char)point;
    }
    output[n_1]=0;
    return(n_1);
}

void draw_border_new( struct RastPort *rp,ULONG x, ULONG y, ULONG width, ULONG height, int b_col1, int b_col2 )
{
    width--;height--;
    SetAPen(rp,b_col1);
    RectFill(rp,x,y,x,y+height);
    RectFill(rp,x,y,x+width,y);
    SetAPen(rp,b_col2);
    RectFill(rp,x+width,y,x+width,y+height);
    RectFill(rp,x,y+height,x+width,y+height);
}

my_RectFill(struct RastPort *rp,WORD x,WORD y,WORD width,WORD height)
{
    if(((x+width)>1)&&((y+height)>1)) RectFill(rp,x,y,x+width-1,y+height-1);
}

my_Blit(struct RastPort *src, WORD topx, WORD topy, struct RastPort *dest, WORD dtopx, WORD dtopy, WORD width, WORD height)
{
    ClipBlit(src,topx,topy,dest,dtopx,dtopy,width,height,0xC0);
}

FreePenNew( struct Screen *scr, struct GAU_Color *col,ULONG number, ULONG *Pens)
{
    if(!col->pen) ReleasePen(scr->ViewPort.ColorMap,Pens[number]);
}

UWORD obtainPen( struct Screen *src, struct GAU_Color *scr)
{
    return(scr->pen ? scr->red : ObtainBestPenA(src->ViewPort.ColorMap, scr->red, scr->green, scr->blue, 0L) );
}

GetPenNew(struct Screen *scr,struct GAU_Color *col,ULONG number, BOOL *PorC, ULONG *Pens)
{
    PorC[number]= col->pen ? FALSE    : TRUE;
    Pens[number]= obtainPen(scr,col);
}

struct TextFont *OpenTopaz()
{
    static struct TextAttr topaz_font =
    {
	"topaz.font",
	8,
	0,
	FPF_ROMFONT,
    };
    return((struct TextFont *)OpenFont( &topaz_font));
}

