#ifndef CODE_BUTTON_CXX
#define CODE_BUTTON_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: button.cxx,v 1.3 1999/12/04 23:06:59 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/gfx_text.h>
#include <Xclasses/button.h>
#include "include/button_p.h"

button::button(): gadget(), Ddraw(this)
{
	while (!(p=new pbutton))
	{
		outOfMemory("button");
	}
	p->o=this;
	Name("button");
	SetKeyHandler(KHM_SELECT);
}
button::~button()
{
	if (win) Free();
	if (p) delete p;
}
pbutton::pbutton()
{
	text=strdup("");
}
pbutton::~pbutton()
{
	if (text) free(text);
}
void button::Text(char *t)
{
	if (p->text) free(p->text);
    p->text=strdup(t);
    ApplyKey(gfx_text::ShortCut(t));
}
int button::Create(void)
{ 
	Background(col_background());
	BorderSize(2);
	KeyBorder();
	
    if (gadget::Create())
    {
    	char *res=GetResource("hilight");
    	if (!res || strcasecmp(res,"True"))
	        Mode(GM_BUTTON1|GM_ONEEXPOSE|GM_KEY);
		else
    	    Mode(GM_BUTTON1|GM_ONEEXPOSE|GM_KEY|GM_ABOVE|GM_LOCKATONCE);
        return 1;
    } 
    return 0;
}
void button::GExpose(XEvent *event)
{
	borderUpFill(this,0,0,width,height);
	bordertext(this,0,0,width,height,p->text);
	if (locked)
	{
		borderlocked(this,0,0,width,height);
		borderlockedtext(this,0,0,width,height,p->text);
	}
	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 button::GActionPress(int mx,int my,int rx,int ry,unsigned int state,int button,int reason)
{
	if (active&GA_BUTTONPRESS||reason&GB_BUTTON)
    {
    	borderDownFill(this,0,0,width,height);
		borderselectedtext(this,0,0,width,height,p->text);
		if (locked) 
		{
			borderlockedtext(this,0,0,width,height,p->text);
			borderlocked(this,0,0,width,height);
//			MCursor(XC_watch);
		}
		else
		{
//			MCursor(XC_hand2);
		}
    }
	if (reason&GB_KEY)
	{
		bordermark(this,0,0,width,height);
	}
}
void button::GActionRelease(int mx,int my,int rx,int ry,unsigned int state,int button,int reason)
{
	if (active&GA_BUTTONPRESS||reason&GB_BUTTON)
	{
	   	borderUpFill(this,0,0,width,height);
		bordertext(this,0,0,width,height,p->text);    
		if (locked) 
		{
			borderlockedtext(this,0,0,width,height,p->text);
			borderlocked(this,0,0,width,height);
//			MCursor(XC_watch);
		}
		else
		{
//			MCursor(XC_hand2);
		}
	}
	if (reason&GB_KEY)
	{
		borderunmark(this,0,0,width,height);
	}
}
int button::GActivate(XEvent *event,int why)
{
	int r;
	r=DefaultKeyHandler(event);
	if (r==DKH_NOKEYEVENT)
	{
		if (why==GA_BUTTONPRESS) ActivateKey();
		if (why==GA_ABOVE)
		{
		   	borderUpShineFill(this,0,0,width,height);
			bordertext(this,0,0,width,height,p->text);
		}
    	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);
    	return GAR_ACTIVE;
    }
    return 0;
}
int button::GSelected(XEvent *event,int why,int wasabove)
{
	if (why==GA_ABOVE)
	{
	   	borderUpFill(this,0,0,width,height);
		bordertext(this,0,0,width,height,p->text);
		return 0;
	}
    return (wasabove|(why==GA_KEY))?GSR_SELECTION|GSR_MESSAGE:0;   // selected if above
}
void button::GSetLimits(void)
{
	minwidth=textlength(this,p->text)+6+8;
	minheight=textheight(this,p->text)+6+4;
}

#endif
