#ifndef CODE_GOSELBUTTON_CXX
#define CODE_GOSELBUTTON_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: go_selbutton.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/selgadget.h>
#include <Xclasses/go_selbutton.h>
#include "include/go_selbutton_p.h"
#ifdef SHAPE
#include <X11/extensions/shape.h>
#endif

go_selbutton::go_selbutton() : selgadget(), Ddraw(this)
{
	while (!(p=new pgo_selbutton))
	{
		outOfMemory("go_selbutton");
	}
	p->o=this;
	BorderSize(2);
	KeyBorder();
	Name("go_selbutton");
}
pgo_selbutton::pgo_selbutton()
{
	obj=NULL;
	state=RELEASED;
	key=0;
	limit=0;
	glocked=0;
	border=1;
	keylook=0;
	minw=-1;
	minh=-1;
	maxw=-1;
	maxh=-1;
}
pgo_selbutton::~pgo_selbutton()
{
}
go_selbutton::~go_selbutton()
{
	if (win) Free();
	if (p) delete p;
}
void go_selbutton::Object(gfx_object *o)
{
    p->obj=o;
}
void go_selbutton::UseLimit(void)
{
	p->limit=1;
}
void go_selbutton::NoLimit(void)
{
	p->limit=0;
}
void go_selbutton::UseLocked(void)
{
	p->glocked=1;
}
void go_selbutton::NotUseLocked(void)
{
	p->glocked=0;
}
void go_selbutton::UseBorder(void)
{
	p->border=1;
	BorderSize(2);
	KeyBorder();
}
void go_selbutton::NoBorder(void)
{
	p->border=0;
	BorderSize(0);
	KeyBorder();
}
void go_selbutton::NoKeyBorder(void)
{
	p->border=1;
	NoKeyBorder();
}
void go_selbutton::Aspect(int minw,int minh,int maxw,int maxh)
{
	p->minw=minw;
	p->minh=minh;
	p->maxw=maxw;
	p->maxh=maxh;
}
void go_selbutton::Symmetric(void)
{
	p->keylook=1;
	Aspect(1,1,1,1);
}

int go_selbutton::Create(void)
{ 
	Background(col_background());
	p->txtheight=textheight(this,"T");
	
    if (gadget::Create())
    {
        Mode(GM_BUTTON1|GM_ONEEXPOSE|GM_KEY);
        return 1;
    } 
    return 0;
}
void go_selbutton::State(int s)
{
    p->state=s; 
    if (win) GExpose(NULL);
}
int go_selbutton::State(void)
{
    return p->state;
}

#define DX	(keylook?(o->width-txtheight)/2:o->RealSize())
#define DY	(keylook?(o->height-txtheight)/2:o->RealSize())
#define DW	(keylook?o->width-(o->height-txtheight):o->width-o->RealSize()*2)
#define DH	(keylook?txtheight:o->height-o->RealSize()*2)

void pgo_selbutton::Press(void)
{
   	if (border) o->borderDownFill(o,0,0,o->width,o->height); else o->borderUpFill(o,0,0,o->width,o->height);
   	if (o->locked)
   	{
		obj->draw_selectedlocked(o,DX,DY,DW,DH);
		if (glocked) o->borderlocked(o,0,0,o->width,o->height);
   	}
   	else
   	{
		obj->draw_selected(o,DX,DY,DW,DH);
	}

	if (key)
	{
		o->bordermark(o,0,0,o->width,o->height);
	}
	else
	{
		o->borderunmark(o,0,0,o->width,o->height);
	}
}
void pgo_selbutton::Release(void)
{
   	o->borderUpFill(o,0,0,o->width,o->height);
   	if (o->locked)
   	{
		obj->draw_locked(o,DX,DY,DW,DH);
		if (glocked) o->borderlocked(o,0,0,o->width,o->height);
   	}
   	else
   	{
		obj->draw_normal(o,DX,DY,DW,DH);
	}
	if (key)
	{
		o->bordermark(o,0,0,o->width,o->height);
	}
	else
	{
		o->borderunmark(o,0,0,o->width,o->height);
	}
}

void go_selbutton::GExpose(XEvent *event)
{
	if (!event || (event->type!=NoExpose && event->type!=GraphicsExpose))
	{
		if (p->state==PRESSED)
		{
			p->Press();
		}
		else
		{
			p->Release();
		}
	}
}
void go_selbutton::GActionPress(int mx,int my,int rx,int ry,unsigned int gstate,int button,int reason)
{
	if (reason&GB_KEY) p->key=GB_KEY;
	if (active&GA_BUTTONPRESS)
	{
		p->state=1-p->state;
	}
	GExpose(NULL);
}
void go_selbutton::GActionRelease(int mx,int my,int rx,int ry,unsigned int gstate,int button,int reason)
{
	if (reason&GB_KEY) p->key=0;
	if (active&GA_BUTTONPRESS)
	{
		p->state=1-p->state;
	}
	GExpose(NULL);
}
int go_selbutton::GActivate(XEvent *event,int why)
{
	int r;
	r=DefaultKeyHandler(event);
	if (r==DKH_NOKEYEVENT)
	{
		if (why==GA_BUTTONPRESS) ActivateKey();
    	return GAR_ACTIVE;   // always active
    }
    if (r==DKH_SELECT)
    {
		p->state=1-p->state;
		GExpose(NULL);
    	return GAR_ACTIVE;
    }
    return 0;
}
int go_selbutton::GSelected(XEvent *event,int why,int wasabove)
{
    return (wasabove|(why==GA_KEY))?GSR_SELECTION|GSR_MESSAGE:0;   // selected if above
}
void go_selbutton::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
