#ifndef CODE_LEFTTEXT_CXX
#define CODE_LEFTTEXT_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: lefttext.cxx,v 1.3 2001/10/22 17:32:24 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 <string.h>
#include "types.h"
#include <Xclasses/gadget.h>
#include <Xclasses/group.h>
#include <Xclasses/Xcolors.h>
#include <Xclasses/lefttext.h>
#include "include/lefttext_p.h"

lefttext::lefttext(void) : gadget()
{
	while (!(p=new plefttext))
	{
		outOfMemory("lefttext");
	}
	p->o=this;
	Name("lefttext");
}
lefttext::lefttext(char *t) : gadget()
{
	lefttext();
	Text(t);
}
lefttext::lefttext(char *t,gadget *g) : gadget()
{
	lefttext();
	Text(t);
	Gadget(g);
}
lefttext::lefttext(char *t,gadget &g) : gadget()
{
	lefttext();
	Text(t);
	Gadget(g);
}
lefttext::lefttext(gadget &lgg,gadget &g) : gadget()
{
	lefttext();
	LeftGadget(lgg);
	Gadget(g);
}
lefttext::lefttext(gadget *lgg,gadget *g) : gadget()
{
	lefttext();
	LeftGadget(lgg);
	Gadget(g);
}

plefttext::plefttext() : text()
{
	gg=NULL;
	gp=NULL;
	lgg=NULL;
	lgp=NULL;
	text.Adjust(ADJUST_RIGHT|ADJUST_MIDDLE);
}
plefttext::~plefttext()
{
}
lefttext::~lefttext()
{
	if (win) Free();
	if (p) delete p;
}

int lefttext::Create(void)
{
	Background(col_background());

	if (!setlimit) GSetLimits();

    if (gadget::Create())
    {
        Mode(GM_ONEEXPOSE);
        if (p->gp)
        {
	        p->gp->Dimensions(win,before_x,0,width-before_x,height);
    	    if (!p->gp->Create())
        	{
				Free();
        		return 0;
        	}
        }
        else
        {
	        p->gg->Dimensions(win,before_x,0,width-before_x,height);
    	    if (!p->gg->Create())
        	{
				Free();
        		return 0;
        	}
        }
        if (p->lgp)
        {
	        p->lgp->Dimensions(win,0,0,before_x-TEXTDISTANCE,height);
    	    if (!p->lgp->Create())
        	{
				Free();
        		return 0;
        	}
        }
        else if (p->lgg)
        {
	        p->lgg->Dimensions(win,0,0,before_x-TEXTDISTANCE,height);
    	    if (!p->lgg->Create())
        	{
				Free();
        		return 0;
        	}
        }
		return 1;
    }
    return 0;
}
void lefttext::Free(void)
{
	if (p->gp)
	{
		p->gp->Free();
		delete p->gp;
		p->gp=NULL;
		p->gg=NULL;
	}
	else
	{
		if (p->gg) p->gg->Free();
		p->gg=NULL;
	}
	if (p->lgp)
	{
		p->lgp->Free();
		delete p->lgp;
		p->lgp=NULL;
		p->lgg=NULL;
	}
	else if (p->lgg)
	{
		if (p->lgg) p->lgg->Free();
		p->lgg=NULL;
	}
	gadget::Free();
}
void lefttext::Resize(int w,int h)
{
	gadget::Resize(w,h);
	if (p->gp)
	{
		if (h>p->gp->gg_maxheight()) h=p->gp->gg_maxheight();
		p->gp->Resize(w-before_x,h);
	}
	else
	{
		if (h>p->gg->gg_maxheight()) h=p->gg->gg_maxheight();
		p->gg->Resize(w-before_x,h);
	}
	if (p->lgp)
	{
		if (h>p->lgp->gg_maxheight()) h=p->lgp->gg_maxheight();
		p->lgp->Resize(before_x-TEXTDISTANCE,h);
	}
	else if (p->lgg)
	{
		if (h>p->lgg->gg_maxheight()) h=p->lgg->gg_maxheight();
		p->lgg->Resize(before_x-TEXTDISTANCE,h);
	}
}
void lefttext::Lock(void)
{
	gadget::Lock();
	if (p->gp) p->gp->Lock(); else p->gg->Lock();
}
void lefttext::Unlock(void)
{
	if (p->gp) p->gp->Unlock(); else p->gg->Unlock();
	gadget::Unlock();
}
void lefttext::Block(int b)
{
	gadget::Block(b);
	if (p->gp) p->gp->Block(b); else p->gg->Block(b);
	if (p->lgp) p->lgp->Block(b); else if (p->lgg) p->lgg->Block(b);
}
void lefttext::Unblock(int b)
{
	if (p->lgp) p->lgp->Unblock(b); else if (p->lgg) p->lgg->Unblock(b);
	if (p->gp) p->gp->Unblock(b); else p->gg->Unblock(b);
	gadget::Unblock(b);
}
void lefttext::Text(char *t)
{
	p->text.Text(t);
}
void lefttext::Top(void)
{
	p->text.Adjust(ADJUST_RIGHT|ADJUST_TOP);
	if (win)
	{
		XClearWindow(display(),win);
		GExpose(NULL);
	}
}
void lefttext::Middle(void)
{
	p->text.Adjust(ADJUST_RIGHT|ADJUST_MIDDLE);
	if (win)
	{
		XClearWindow(display(),win);
		GExpose(NULL);
	}
}
void lefttext::Bottom(void)
{
	p->text.Adjust(ADJUST_RIGHT|ADJUST_BOTTOM);
	if (win)
	{
		XClearWindow(display(),win);
		GExpose(NULL);
	}
}
void lefttext::Gadget(gadget *g)
{
	p->gg=g;
	g->ParentClass(this);
}
void lefttext::LeftGadget(gadget *g)
{
	p->lgg=g;
	g->ParentClass(this);
}

void lefttext::GExpose(XEvent *event)
{
	if (!p->lgg)
		p->text.draw_normal(this,0,0,before_x-TEXTDISTANCE,height);
}
void lefttext::GSetLimits(void)
{
	int w=0,h=0;
	p->gg->GSetLimits();
	if (p->lgg)
	{
		p->lgg->GSetLimits();

		if (p->lgg->gg_minxaspect())
		{
			if (p->lgp=new group)
			{
	       		p->lgp->Add(p->lgg);
				p->lgp->GSetLimits();
				w=p->lgp->gg_minwidth();
				h=p->lgp->gg_minheight();
			}
		}
		else
		{
			w=p->lgg->gg_minwidth();
			h=p->lgg->gg_minheight();
		}
		w+=TEXTDISTANCE;
	}
	else
	{
		w=p->text.width(this)+TEXTDISTANCE;
		h=p->text.height(this);
	}
	if (p->gg->gg_minxaspect())
	{
		if (p->gp=new group)
		{
	       	p->gp->Add(p->gg);
			p->gp->GSetLimits();
			minwidth=w+p->gp->gg_minwidth();
			if (p->gp->gg_maxwidth()!=0xffffff)
			{
				maxwidth=w+p->gp->gg_maxwidth();
			}
			minheight=(h>p->gp->gg_minheight()?h:p->gp->gg_minheight());
			maxheight=(h>p->gp->gg_maxheight()?h:p->gp->gg_maxheight());
		}
	}
	else
	{
		minwidth=w+p->gg->gg_minwidth();
		if (p->gg->gg_maxwidth()!=0xffffff)
		{
			maxwidth=w+p->gg->gg_maxwidth();
		}
		minheight=(h>p->gg->gg_minheight()?h:p->gg->gg_minheight());
		maxheight=(h>p->gg->gg_maxheight()?h:p->gg->gg_maxheight());
	}
	before_x=w;
	setlimit=1;
	if (locked) Lock();
}

#endif
