#ifndef CODE_ANIM_CXX
#define CODE_ANIM_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: anim.cxx,v 1.2 1998/07/31 06:35:14 beckerr Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *-----------------------------------------------------------------------
 */

#include <math.h>
#include "types.h"
#include <Xclasses/gadget.h>
#include <Xclasses/Xcolors.h>
#include <Xclasses/Ddraw.h>
#include <Xclasses/anim.h>
#include "include/anim_p.h"

anim::anim(): gadget(), Ddraw(this)
{
	while (!(p=new panim))
	{
		outOfMemory("anim");
	}
	p->o=this;
	Name("anim");
}
panim::panim()
{
	rc=0;
	rate=2;
	pix=NULL;
	total=0;
	cpic=0;
	xat=NULL;
	run=1;
}
panim::~panim()
{
}
anim::~anim()
{
	if (win) Free();
	if (p) delete p;
}
int anim::Create(void)
{ 
	Background(col_selected());
	BorderSize(1);
	
    if (gadget::Create())
    {
        Mode(GM_ONEEXPOSE|GM_NORMALTICK|GM_BUTTON1);
        MCursor(XC_dot);
        return 1;
    } 
    return 0;
}
void anim::Free(void)
{
	gadget::Free();
	if (p->pix)
	{
		for(int i=0;i<p->total;i++)
		{
			if (p->pix[p->cpic])
			{
				XFreeColors(display(),Cmap(),p->xat[i].pixels,p->xat[i].npixels,0);
				XpmFreeAttributes(&p->xat[i]);
				XFreePixmap(display(),p->pix[i]);
			}
		}
		delete p->xat;
		delete p->pix;
		p->pix=NULL;
	}
}
void anim::Rate(int r)
{
	p->rate=r;
	p->rc=0;
}
void anim::Pics(char ***data)
{
	char ***d=data;
	p->total=0;
	while (d[p->total]) p->total++;
	p->pix=new Pixmap[p->total];
	p->xat=new XpmAttributes[p->total];

	for(int i=0;i<p->total;i++)
	{
		p->xat[i].colormap=Cmap();
		p->xat[i].valuemask=XpmColormap;
		XpmCreatePixmapFromData(display(),rootwindow(),data[i],&p->pix[i],NULL,&p->xat[i]);
	}	
}
void anim::GExpose(XEvent *event)
{
	if (event->type!=NoExpose && event->type!=GraphicsExpose)
	{
		if (p->pix[p->cpic] && p->xat[p->cpic].width>0 && p->xat[p->cpic].height>0)
			XCopyArea(display(),p->pix[p->cpic],win,gc,0,0,p->xat[p->cpic].width,p->xat[p->cpic].height,0,0);
	}
}
int anim::GActivate(XEvent *event,int why)
{
	if (why==GA_TICK)
	{
		if (p->run)
		{
			p->rc--;
			if (p->rc<=0)
			{
				p->rc=p->rate;
				p->cpic++;
				if (p->cpic>=p->total) p->cpic=0;
				if (p->pix[p->cpic] && p->xat[p->cpic].width>0 && p->xat[p->cpic].height>0)
					XCopyArea(display(),p->pix[p->cpic],win,gc,0,0,p->xat[p->cpic].width,p->xat[p->cpic].height,0,0);
			}
		}
	}
	if (why==GA_BUTTONPRESS)
	{
		p->run=!p->run;
	}
	return 0;
}
int anim::GSelected(XEvent *event,int why,int wasabove)
{
	return 0;
}
void anim::GActionPress(int mx,int my,int rx,int ry,unsigned int state,int button,int reason)
{
}
void anim::GActionRelease(int mx,int my,int rx,int ry,unsigned int state,int button,int reason)
{
}
void anim::GSetLimits(void)
{
	minwidth=maxwidth=p->xat[0].width;
	minheight=maxheight=p->xat[0].height;
	if (minwidth<=0) minwidth=maxwidth=1;
	if (minheight<=0) minheight=maxheight=1;
}

#endif
