#ifndef CODE_POPBUTTON_CXX
#define CODE_POPBUTTON_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: popbutton.cxx,v 1.5 2001/01/02 23:22:07 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/popbutton.h>
#include <Xclasses/tools.h>
#include <Xclasses/callable.h>
#include "../include/callback.h"
#include "include/popbutton_p.h"

popbutton::popbutton(): button()
{
	while (!(p=new ppopbutton))
	{
		outOfMemory("popbutton");
	}
	p->o=this;
	Name("popbutton");
	p->pop.ParentClass(this);
	p->pop.Name("popbutton_gpopup");
}
popbutton::~popbutton()
{
	if (win) Free();
	if (p) delete p;
}
ppopbutton::ppopbutton() : pop(), tcb()
{
	size=50;
}
ppopbutton::~ppopbutton()
{
}

int popbutton::Create(void)
{
	if (button::Create())
	{
		p->pop.Callback(this,(CALLBACK)&popbutton::CallCallback);
		return p->pop.Create();
	}
	return 0;
}
void popbutton::Free(void)
{
	p->pop.Free();
	if (win) button::Free();
}

void popbutton::Apply(gadget *g,char *title,int size)
{
	p->pop.Apply(g);
	p->pop.Title(title);
	p->size=size;
}
void popbutton::CloseGadget(gadget *g)
{
	p->pop.CloseGadget(g);
}

int popbutton::GSelected(XEvent *e,int why,int wasabove)
{
	int r=button::GSelected(e,why,wasabove);

	if (r & GSR_SELECTION)
	{
		int x,y,px;
		if (p->tcb.iscallback())
		{
			p->tcb.call();
		}
		GetPositionOnRoot(display(),win,x,y);
		px=x+width/2;
		p->pop.Open(px,y,p->size,x,y,width,height);
	}
	return 0;
}

void popbutton::OpenCallback(void cfunc(void))
{
	p->tcb.set((CBCAST) cfunc);
}
void popbutton::OpenCallback(callable *cl,void (callable::*cfunc)(void))
{
	p->tcb.set(cl,(CALLFUNCTION) cfunc);
}

#endif
