#ifndef CODE_EASYDIALOG_CXX
#define CODE_EASYDIALOG_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: easy_dialog.cxx,v 1.2 2000/11/05 19:11:33 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 <math.h>
#include "types.h"
#include <Xclasses/tools.h>
#include <Xclasses/Xwindows.h>
#include <Xclasses/Xcolors.h>
#include <Xclasses/gfx_object.h>
#include <Xclasses/easy_dialog.h>
#include "include/easy_dialog_p.h"
#include "../include/defaults.h"

//#include <Xclasses/xclassesanim.h>
#include "../include/xclassesanim.h"

easy_dialog::easy_dialog(void) : Xcolors()
{
	while (!(p=new peasy_dialog))
	{
		outOfMemory("easy_dialog");
	}
	p->o=this;
	p->aw.ParentClass(this);
	Name("easy_dialog");
}
peasy_dialog::peasy_dialog() : maingp(), butgp(), txtgp(), rlr(), txt(), aw(), img()
{
	opt="OK";
	font=defaults.font[0];
//	txt.AutoFormat();
	txt.NoUnderline();
	gfx=NULL;
}
peasy_dialog::~peasy_dialog()
{
}
easy_dialog::~easy_dialog()
{
	if (p) delete p;
}

void easy_dialog::Text(char *t)
{
	if (strlen(t)>100)
	{
		p->font=defaults.font[FONT_GADGET];
	}
	else
	{
		p->font=defaults.font[FONT_BIG];
	}
	p->txt.Text(t);
}
void easy_dialog::Options(char *t)
{
	p->opt=t;
}
void easy_dialog::Logo(class gfx_object *g)
{
	p->gfx=g;
}
int easy_dialog::Start(Xwindows *w)
{
	int i,c,ende;
//	int x,y;
//	XWindowAttributes xwa;
	xclassesanim *img=NULL;

	p->maingp.Vert();
	p->maingp.Center();
	p->maingp.Add(p->txtgp);
	 p->txtgp.Border(10);
	 p->txtgp.Horiz();
	 p->txtgp.XDistance(10);
//	 p->txtgp.Center();
	 if ((int)p->gfx==1)
	 {
	 	img=new xclassesanim;
	 	p->txtgp.Add(img);
	 }
	 else if (p->gfx)
	 {
	 	p->txtgp.Add(p->img);
	  	 p->img.Object(p->gfx);
	 }
	 p->txtgp.Add(p->txt);
	  p->txt.Font(p->font);
	  p->txt.FixedText();
	p->maingp.Add(p->rlr); p->rlr.Space(2);
	p->maingp.Add(p->butgp);
	 p->butgp.Horiz();
	 p->butgp.DialogMode();
	 p->butgp.Border(10);

	i=0; c=0;
	while (p->opt[i])
	{
		if (p->opt[i]=='|')
		{
			c++;	
		}
		i++;
	}			
	if (p->opt[strlen(p->opt)-1]!='|') c++;

	if (p->bt=new char[strlen(p->opt)+c])
	{
		if (p->b=new button[c+1])
		{
			int j;
			c=0; i=0; j=0;
			strcpy(p->bt,p->opt);
			while (p->bt[i])
			{
				if (p->bt[i]=='|')
				{
					p->bt[i]=0;
					p->b[c].Text(&p->bt[j]);
					p->b[c].ID(c+1);
					p->b[c].Font(p->font);
					p->butgp.Add(p->b[c]);
					c++; j=i+1;
				}
				i++;
			}
			if (j<i)
			{
				p->b[c].Text(&p->bt[j]);
				p->b[c].ID(c+1);
				p->b[c].Font(p->font);
				p->butgp.Add(p->b[c]);
				c++;
			}
//			GetPositionOnRoot(display(),w->window(),x,y);
//			XGetWindowAttributes(display(),w->window(),&xwa);

			p->aw.InitSize(5);
			p->aw.Init();
			p->aw.TransientFor(w);
			p->aw.SetGroup(p->maingp);
			p->aw.Adjust(w,ADJUST_CENTER|ADJUST_MIDDLE);
//			p->aw.Adjust(x+xwa.x,y+xwa.y,xwa.width,xwa.height,ADJUST_CENTER|ADJUST_MIDDLE);
			p->aw.Create();

			p->aw.BlockExcept();
			p->b[c-1].ActivateKey();
			p->b[c-1].ApplyXKey(XK_Escape);

			ende=0;
			while (!ende)
			{
				i=p->aw.NextEvent();
				if (i==EV_WINDOWCLOSE)
				{
					if (p->aw.CloseMe()) ende=c;
				}
				else if (i==EV_GADGET)
				{
					ende=p->aw.ID();
				}
			}
			p->aw.UnblockExcept();
			p->aw.Close();
			delete [] p->b;
			delete p->bt;
			c=ende;
		}
	}
	if (img) delete img;
	return c;
}

#endif
