#ifndef CODE_GOBUTTON_CXX
#define CODE_GOBUTTON_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: go_button.cxx,v 1.3 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 "types.h"
#include <Xclasses/gadget.h>
#include <Xclasses/Xcolors.h>
#include <Xclasses/Ddraw.h>
#include <Xclasses/go_button.h>
#include "include/go_button_p.h"
#include <Xclasses/gfx_object.h>
#ifdef SHAPE
#include <X11/extensions/shape.h>
#endif

go_button::go_button(): gadget(), Ddraw(this)
{
	while (!(p=new pgo_button))
	{
		outOfMemory("go_button");
	}
	p->o=this;
	BorderSize(2);
	KeyBorder();
	Name("go_button");
	SetKeyHandler(KHM_SELECT);
}
go_button::~go_button()
{
	if (win) Free();
	if (p) delete p;
}
pgo_button::pgo_button()
{
	obj=NULL;
	limit=0;
	glocked=0;
	border=1;
	ticks=0;
	key=0;
	keylook=0;
	minw=-1;
	minh=-1;
	maxw=-1;
	maxh=-1;
	txtheight=0;
}
pgo_button::~pgo_button()
{
}
void go_button::Object(gfx_object *o)
{
    p->obj=o;
    if (win) GExpose(NULL);
}
void go_button::UseLimit(void)
{
	p->limit=1;
}
void go_button::NoLimit(void)
{
	p->limit=0;
}
void go_button::UseLocked(void)
{
	p->glocked=1;
}
void go_button::NotUseLocked(void)
{
	p->glocked=0;
}
void go_button::UseBorder(void)
{
	p->border=1;
	BorderSize(2);
	KeyBorder();
}
void go_button::NoBorder(void)
{
	p->border=0;
	BorderSize(0);
	KeyBorder();
}
void go_button::NoKeyBorder(void)
{
	Ddraw::NoKeyBorder();
}
void go_button::SendTicks(void)
{
	p->ticks=1;
}
void go_button::NoTicks(void)
{
	p->ticks=0;
}
void go_button::Aspect(int minw,int minh,int maxw,int maxh)
{
	p->minw=minw;
	p->minh=minh;
	p->maxw=maxw;
	p->maxh=maxh;
}
void go_button::Symmetric(void)
{
	p->keylook=1;
	Aspect(1,1,1,1);
}

int go_button::Create(void)
{ 
	Background(col_background());
	p->txtheight=textheight(this,"T");
	
    if (gadget::Create())
    {
    	char *res=GetResource("hilight");
    	if (!res || strcasecmp(res,"True"))
	        Mode(GM_BUTTON1|GM_ONEEXPOSE|GM_KEY|GM_ACTIVETICK);
		else
    	    Mode(GM_BUTTON1|GM_ONEEXPOSE|GM_KEY|GM_ABOVE|GM_LOCKATONCE|GM_ACTIVETICK);

        p->obj->Adjust(ADJUST_CENTER|ADJUST_MIDDLE);
        return 1;
    } 
    return 0;
}
#define DX	(p->keylook?(width-p->txtheight)/2:RealSize())
#define DY	(p->keylook?(height-p->txtheight)/2:RealSize())
#define DW	(p->keylook?width-(height-p->txtheight):width-RealSize()*2)
#define DH	(p->keylook?p->txtheight:height-RealSize()*2)

void go_button::GExpose(XEvent *event)
{
	if (!event || (event->type!=NoExpose && event->type!=GraphicsExpose))
	{
		borderUpFill(this,0,0,width,height);
		if (locked)
		{
			p->obj->draw_locked(this,DX,DY,DW,DH);
			if (p->glocked) borderlocked(this,0,0,width,height);
		}
		else
		{
			p->obj->draw_normal(this,DX,DY,DW,DH);
		}
		if (!event)
		{
//			if (locked) MCursor(XC_watch); else MCursor(XC_hand2);
		}
    	if (active & GA_KEY)
	    {
   		    bordermark(this,0,0,width,height);   	    
	    }
    	else
	    {
			borderunmark(this,0,0,width,height);
	    }
	}
}
void go_button::GActionPress(int mx,int my,int rx,int ry,unsigned int state,int go_button,int reason)
{
	if (active&GA_BUTTONPRESS||reason&GB_BUTTON)
    {
    	if (p->border) borderDownFill(this,0,0,width,height); else borderUpFill(this,0,0,width,height);
		if (locked)
		{
			p->obj->draw_selectedlocked(this,DX,DY,DW,DH);
			if (p->glocked) borderlocked(this,0,0,width,height);
		}
		else
		{
			p->obj->draw_selected(this,DX,DY,DW,DH);
		}
    }
	if (reason&GB_KEY)
	{
		bordermark(this,0,0,width,height);
	}
}
void go_button::GActionRelease(int mx,int my,int rx,int ry,unsigned int state,int go_button,int reason)
{
	if (active&GA_BUTTONPRESS||reason&GB_BUTTON)
	{
	   	borderUpFill(this,0,0,width,height);
		if (locked)
		{
			p->obj->draw_locked(this,DX,DY,DW,DH);
			if (p->glocked) borderlocked(this,0,0,width,height);
		}
		else
		{
			p->obj->draw_normal(this,DX,DY,DW,DH);
		}
	}
	if (reason&GB_KEY)
	{
		borderunmark(this,0,0,width,height);
	}
}
int go_button::GActivate(XEvent *event,int why)
{
	if (why==GA_TICK)
	{
		if (p->ticks && !p->key)
		{
			if (p->tickcounter<4)
			{
				p->tickcounter++;
				if (p->tickcounter==1) return GAR_ACTIVE;
			}
			else
			{
				if (state) return GAR_ACTIVE;
			}
		}
		return 0;
	}
	else if (why==GA_ABOVE)
	{
	   	borderUpShineFill(this,0,0,width,height);
		if (locked)
		{
			p->obj->draw_locked(this,DX,DY,DW,DH);
			if (p->glocked) borderlocked(this,0,0,width,height);
		}
		else
		{
			p->obj->draw_normal(this,DX,DY,DW,DH);
		}
	}
	p->tickcounter=0;
	p->key=0;
	int r;
	r=DefaultKeyHandler(event);
	if (r==DKH_NOKEYEVENT)
	{
		if (why==GA_BUTTONPRESS) ActivateKey();
    	return GAR_ACTIVE;   // always active
    }
    if (r==DKH_SELECT)
    {
    	GActionPress(0,0,0,0,0,0,GB_BUTTON); XFlush(display());
    	go_sleep(200000);
    	GActionRelease(0,0,0,0,0,0,GB_BUTTON);
    	p->key=1;
    	return GAR_ACTIVE;
    }
    return 0;
}
int go_button::GSelected(XEvent *event,int why,int wasabove)
{
	if (why==GA_ABOVE)
	{
	   	borderUpFill(this,0,0,width,height);
		if (locked)
		{
			p->obj->draw_locked(this,DX,DY,DW,DH);
			if (p->glocked) borderlocked(this,0,0,width,height);
		}
		else
		{
			p->obj->draw_normal(this,DX,DY,DW,DH);
		}
		return 0;
	}

	if (why==GA_BUTTONPRESS && p->ticks && p->tickcounter>0) return 0;
    return (wasabove|(why==GA_KEY)?GSR_SELECTION|GSR_MESSAGE:0);   // selected if above
}
void go_button::GSetLimits(void)
{
	if (p->obj->width(this)!=NOLIMIT)
	{
		minwidth=p->obj->width(this)+RealSize()*2;
		if (p->limit) maxwidth=minwidth;
		p->keylook=0;
	}
	else
	{
		minwidth=RealSize()*2+2;
	}
	if (p->obj->height(this)!=NOLIMIT)
	{
		minheight=p->obj->height(this)+RealSize()*2;
		if (p->limit) maxheight=minheight;
		p->keylook=0;
	}
	else
	{
		minheight=RealSize()*2+2;
	}
	if (p->minw!=-1)
	{
		minxaspect=p->minw;
		minyaspect=p->minh;
		maxxaspect=p->maxw;
		maxyaspect=p->maxh;
	}
}

#endif
