#ifndef CODE_CHECKBOX_CXX
#define CODE_CHECKBOX_CXX

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

checkbox::checkbox() : selgadget(), Ddraw(this)
{
	while (!(p=new pcheckbox))
	{
		outOfMemory("checkbox");
	}
	p->o=this;
	Name("checkbox");
	SetKeyHandler(KHM_SELECT);
	p->knob.ParentClass(this);
}
pcheckbox::pcheckbox() : knob()
{
	state=RELEASED;
	text=NULL;
	locked=0;
	key=0;
}
pcheckbox::~pcheckbox()
{
}
checkbox::~checkbox()
{
	if (p->text) free(p->text);
	if (win) Free();
	if (p) delete p;
}
void checkbox::State(int s)
{
    p->state=s==PRESSED?1:0; 
    if (win) GExpose(NULL);
}
int checkbox::State(void)
{
    return p->state?PRESSED:RELEASED;
}
void checkbox::Text(char *t)
{
	if (p->text) free(p->text);
	p->text=strdup(t);
    ApplyKey(gfx_text::ShortCut(t));
}

int checkbox::Create(void)
{
	Background(col_background());
	BorderSize(0);
	KeyBorder();
	StripesMark();
	if (!setlimit) p->h=textheight(this,"text")/2-2;

	if (selgadget::Create())
	{
		Mode(GM_BUTTON1|GM_ONEEXPOSE|GM_KEY);
		return 1;
	} 
	return 0;
}

void pcheckbox::dtext(void)
{
	if (o->locked)
	{
		o->borderlockedtext(o,4+h+h+4,0,o->width-4-h-h-4,o->height,text,ADJUST_LEFT|ADJUST_MIDDLE);
	}
	else
	{
		o->bordertext(o,4+h+h+4,0,o->width-4-h-h-4,o->height,text,ADJUST_LEFT|ADJUST_MIDDLE);
	}
}
void pcheckbox::draw(void)
{
	if (state==PRESSED)
	{
		if (o->locked) knob.draw_selectedlocked(o,1,1,h+h+4,h+h+4); else knob.draw_selected(o,1,1,h+h+4,h+h+4);
	}
	else
	{
		if (o->locked) knob.draw_locked(o,1,1,h+h+4,h+h+4); else knob.draw_normal(o,1,1,h+h+4,h+h+4);
	}
	if (key)
	{
		o->bordermark(o,0,0,o->width,o->height);
	}
	else
	{
		o->borderunmark(o,0,0,o->width,o->height);
	}
}
void checkbox::GExpose(XEvent *event)
{
	if ((event || p->locked!=locked) && !locked)
	{
		XClearWindow(display(),win);
		if (!event) p->dtext();
	}
	p->dtext();
	p->draw();
	p->locked=locked;
}
void checkbox::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;
	}
	p->draw();
}
void checkbox::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;
	}
	p->draw();
}
int checkbox::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;
		p->draw();
    	return GAR_ACTIVE;
    }
    return 0;
}
int checkbox::GSelected(XEvent *event,int why,int wasabove)
{
    return (wasabove|(why==GA_KEY))?GSR_SELECTION|GSR_MESSAGE:0;
}
void checkbox::GSetLimits(void)
{
	setlimit=1;
	p->h=textheight(this,"text")/2-2;
	minwidth=textlength(this,p->text);
	minheight=textheight(this,p->text);
	maxheight=minheight;
	minwidth+=minheight;
	maxwidth=minwidth;
	minwidth+=8;
	maxwidth+=8;
	minheight+=2;
	maxheight+=2;
}

#endif
