#ifndef CODE_SELBUTTON_CXX
#define CODE_SELBUTTON_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: selbutton.cxx,v 1.2 1998/07/31 06:35:13 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/gfx_text.h>
#include <Xclasses/selgadget.h>
#include <Xclasses/selbutton.h>
#include "include/selbutton_p.h"

selbutton::selbutton() : selgadget(), Ddraw(this)
{
	while (!(p=new pselbutton))
	{
		outOfMemory("selbutton");
	}
	p->o=this;
	Name("selbutton");
}
pselbutton::pselbutton()
{
	state=RELEASED;
	key=0;
}
pselbutton::~pselbutton()
{
}
selbutton::~selbutton()
{
	if (win) Free();
	if (p) delete p;
}
void selbutton::Text(char *t)
{
    p->text=t;
    ApplyKey(gfx_text::ShortCut(t));
}
int selbutton::Create(void)
{ 
	Background(col_background());
	BorderSize(2);
	KeyBorder();
	
    if (selgadget::Create())
    {
//        MCursor(XC_hand2); 
        Mode(GM_BUTTON1|GM_ONEEXPOSE|GM_KEY);
        return 1;
    } 
    return 0;
}
void selbutton::State(int s)
{
    p->state=s; 
    if (win) GExpose(NULL);
}
int selbutton::State(void)
{
    return p->state;
}
void pselbutton::Press(void)
{
   	o->borderDownFill(o,0,0,o->width,o->height);
   	if (o->locked)
   	{
		o->borderlockedtext(o,0,0,o->width,o->height,text);
		o->borderlocked(o,0,0,o->width,o->height,0);
   	}
   	else
   	{
		o->borderselectedtext(o,0,0,o->width,o->height,text);
	}
	if (key)
	{
		o->bordermark(o,0,0,o->width,o->height);
	}
	else
	{
		o->borderunmark(o,0,0,o->width,o->height);
	}
}
void pselbutton::Release(void)
{
   	o->borderUpFill(o,0,0,o->width,o->height);
   	if (o->locked)
   	{
		o->borderlockedtext(o,0,0,o->width,o->height,text);
		o->borderlocked(o,0,0,o->width,o->height,0);
   	}
   	else
   	{
		o->bordertext(o,0,0,o->width,o->height,text);
	}
	if (key)
	{
		o->bordermark(o,0,0,o->width,o->height);
	}
	else
	{
		o->borderunmark(o,0,0,o->width,o->height);
	}
}

void selbutton::GExpose(XEvent *event)
{
	if (p->state==PRESSED)
	{
		p->Press();
	}
	else
	{
		p->Release();
	}
}
void 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 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 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 selbutton::GSelected(XEvent *event,int why,int wasabove)
{
    return (wasabove|(why==GA_KEY))?GSR_SELECTION|GSR_MESSAGE:0;   // selected if above
}
void selbutton::GSetLimits(void)
{
	minwidth=textlength(this,p->text)+6+8;
	minheight=textheight(this,p->text)+6+4;
}

#endif
