#ifndef CODE_GFXXPM_CXX
#define CODE_GFXXPM_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: gfx_xpm.cxx,v 1.2 2000/10/14 20:40:14 schmitzj 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 <ctype.h>
#include "types.h"
#include <Xclasses/Xcolors.h>
#include <Xclasses/gadget.h>
#include <Xclasses/gfx_object.h>
#include <Xclasses/gfx_xpm.h>
#include "include/gfx_xpm_p.h"

gfx_xpm::gfx_xpm(void) : gfx_object()
{
	while (!(p=new pgfx_xpm))
	{
		outOfMemory("gfx_xpm");
	}
	p->o=this;
	Name("gfx_xpm");
}
gfx_xpm::gfx_xpm(char *t[]) : gfx_object()
{
	while (!(p=new pgfx_xpm))
	{
		outOfMemory("gfx_xpm");
	}
	p->o=this;
	Name("gfx_xpm");
	p->d=t;
}
pgfx_xpm::pgfx_xpm(void)
{
	pix=0;
	shad=0;
	d=NULL;
}
pgfx_xpm::~pgfx_xpm()
{
	if (pix && d)
	{
		XFreeColors(o->display(),o->Cmap(),xat.pixels,xat.npixels,0);
		XpmFreeAttributes(&xat);
		XFreePixmap(o->display(),pix);
		if (shad) XFreePixmap(o->display(),shad);
	}
}
gfx_xpm::~gfx_xpm()
{
	if (p) delete p;
}

void gfx_xpm::Adjust(int a)
{
	p->adjust=a;
}
void gfx_xpm::Xpm(char *d[])
{
	p->d=d;
	p->Create();
}
void pgfx_xpm::Create(void)
{
	if (!pix && d)
	{
		xat.colormap=o->Cmap();
		xat.closeness=0x9999;
		xat.valuemask=XpmColormap|XpmCloseness;
		if (o->display())
			XpmCreatePixmapFromData(o->display(),o->rootwindow(),d,&pix,&shad,&xat);
	}
}
void gfx_xpm::draw_normal(class gadget *g,int x,int y,int w,int h)
{
	if (!p->pix) p->Create();
	if (w>p->xat.width) w=p->xat.width;
	if (h>p->xat.height) h=p->xat.height;
	if (p->pix)
	{
		XSetClipMask(g->display(),g->gg_gc(),p->shad);
		XSetClipOrigin(g->display(),g->gg_gc(),x,y);
		XCopyArea(g->display(),p->pix,g->gg_win(),g->gg_gc(),0,0,w,h,x,y);
		XSetClipMask(g->display(),g->gg_gc(),None);
	}
}
void gfx_xpm::draw_selected(class gadget *g,int x,int y,int w,int h)
{
	if (!p->pix) p->Create();
	if (w>p->xat.width) w=p->xat.width;
	if (h>p->xat.height) h=p->xat.height;
	if (p->pix)
	{
		XSetClipMask(g->display(),g->gg_gc(),p->shad);
		XSetClipOrigin(g->display(),g->gg_gc(),x+2,y+2);
		XCopyArea(g->display(),p->pix,g->gg_win(),g->gg_gc(),0,0,w-2,h-2,x+2,y+2);
		XSetClipMask(g->display(),g->gg_gc(),None);
	}
}
void gfx_xpm::draw_locked(class gadget *g,int x,int y,int w,int h)
{
	draw_normal(g,x,y,w,h);
}
void gfx_xpm::draw_selectedlocked(class gadget *g,int x,int y,int w,int h)
{
	draw_selected(g,x,y,w,h);
}

int gfx_xpm::height(class gadget *g)
{
	if (!p->pix) p->Create();
	return p->xat.height>0?p->xat.height:1;
}
int gfx_xpm::width(class gadget *g)
{
	if (!p->pix) p->Create();
	return p->xat.width>0?p->xat.width:1;
}

Pixmap gfx_xpm::pixmap(void)
{
	if (!p->pix) p->Create();
	return p->pix;
}
Pixmap gfx_xpm::shadowmap(void)
{
	if (!p->pix) p->Create();
	return p->shad;
}

#endif
