#ifndef CODE_GRAPHIC_CXX
#define CODE_GRAPHIC_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: graphic.cxx,v 1.2 1998/07/31 06:35:15 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 "types.h"
#include <Xclasses/gadget.h>
#include <Xclasses/Xcolors.h>
#include <Xclasses/Ddraw.h>
#include <Xclasses/graphic.h>
#include "include/graphic_p.h"

graphic::graphic(): gadget(), Ddraw(this)
{
	while (!(p=new pgraphic))
	{
		outOfMemory("graphic");
	}
	p->o=this;
	Name("graphic");
	BorderSize(1);
}
pgraphic::pgraphic()
{
	pixmap=0;
	gc=0;
}
pgraphic::~pgraphic()
{
}
graphic::~graphic()
{
	if (win) Free();
	if (p) delete p;
}
Pixmap graphic::pixmap(void)
{
	return p->pixmap;
}
GC graphic::gc(void)
{
	return p->gc;
}
void graphic::Size(int w,int h)
{
	p->w=w;
	p->h=h;
}
void graphic::Foreground(int c)
{
	XSetForeground(display(),p->gc,c);
	p->col=c;
}
void graphic::Line(int x1,int y1,int x2,int y2)
{
	XDrawLine(display(),p->pixmap,p->gc,x1,y1,x2,y2);
}
void graphic::DrawString(int x,int y,char *t)
{
	XDrawString(display(),p->pixmap,p->gc,x,y,t,strlen(t));
}
int graphic::StringLength(char *t)
{
	XCharStruct cs;
	int dhint,fasc,fdec;
	XTextExtents(gg_font(),t,strlen(t),&dhint,&fasc,&fdec,&cs);
	return cs.width;
}
void graphic::FillRectangle(int x,int y,int w,int h)
{
	XFillRectangle(display(),p->pixmap,p->gc,x,y,w,h);
}
void graphic::Clear(void)
{
	XFillRectangle(display(),p->pixmap,p->gc,0,0,p->w,p->h);
}
void graphic::Draw(void)
{
	if (win) XCopyArea(display(),p->pixmap,win,gadget::gc,0,0,p->w,p->h,RealSize(),RealSize());
}
int graphic::FontHeight(void)
{
	return gg_font()->ascent+gg_font()->descent;
}
int graphic::FontAscent(void)
{
	return gg_font()->ascent;
}
int graphic::Font(char *name)
{
	int r=gadget::Font(name);
   	if (gg_font() && p->gc) XSetFont(display(),p->gc,gg_font()->fid);
	return r;
}
int graphic::Create(void)
{ 
	Background(col_background());
	
    if (gadget::Create())
    {
        Mode(GM_ONEEXPOSE);
		if (CreatePixmap()) return 1;
		Free();
    } 
    return 0;
}
void graphic::Free(void)
{
	if (p->pixmap) XFreePixmap(display(),p->pixmap);
	p->pixmap=0;
	if (p->gc) XFreeGC(display(),p->gc);
	p->gc=0;
	gadget::Free();
}
int graphic::CreatePixmap(void)
{
	if (p->pixmap=XCreatePixmap(display(),rootwindow(),p->w,p->h,DefaultDepth(display(),DefaultScreen(display()))))
	{
		p->gc=XCreateGC(display(),p->pixmap,0,0);
		Foreground(col_white());
		XFillRectangle(display(),p->pixmap,p->gc,0,0,p->w,p->h);
		return 1;
	}
	return 0;
}
int graphic::CreatePixmap(int depth)
{
	if (p->pixmap=XCreatePixmap(display(),rootwindow(),p->w,p->h,depth))
	{
		p->gc=XCreateGC(display(),p->pixmap,0,0);
		Foreground(col_white());
		XFillRectangle(display(),p->pixmap,p->gc,0,0,p->w,p->h);
		return 1;
	}
	return 0;
}
void graphic::GExpose(XEvent *event)
{
	if (event->type!=NoExpose && event->type!=GraphicsExpose)
	{
		borderDownFill(this,0,0,width,height);
		XSetForeground(display(),p->gc,p->col);
		Draw();
	}
}
int graphic::GActivate(XEvent *event,int why)
{
	if (why==GA_TICK)
	{

	}
	return 0;
}
int graphic::GSelected(XEvent *event,int why,int wasabove)
{
	return 0;
}
void graphic::GActionPress(int mx,int my,int rx,int ry,unsigned int state,int button,int reason)
{
}
void graphic::GActionRelease(int mx,int my,int rx,int ry,unsigned int state,int button,int reason)
{
}
void graphic::GSetLimits(void)
{
	minwidth=p->w+RealSize()*2;
	minheight=p->h+RealSize()*2;
	maxwidth=p->w+RealSize()*2;
	maxheight=p->h+RealSize()*2;
}

#endif
