#ifndef CODE_COLORNAMEGROUP_CXX
#define CODE_COLORNAMEGROUP_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: colornamegroup.cxx,v 1.5 1998/08/06 15:07:41 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 <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Xclasses/gadget.h>
#include <Xclasses/colornamegroup.h>
#include "include/colornamegroup_p.h"

colornamegroup::colornamegroup(void) : bordergroup()
{
	while (!(p=new pcolornamegroup))
	{
		outOfMemory("colornamegroup");
	}
	p->o=this;
	Name("colornamegroup");
	Horiz();

	 p->list.ShowSelected();
	 p->list.Name("names");
	 p->list.Callback(p,(CALLBACK)&pcolornamegroup::handle);
	Add(p->list);
	 p->prev.Size(40,80);
	 p->prev.Name("preview");
	Add(p->prev);
}
pcolornamegroup::pcolornamegroup(void) : callable(), list(), prev(), names()
{
	curcolor[0]=0;
	acol=0;
}
pcolornamegroup::~pcolornamegroup()
{
	color_name *n;
	while (n=(color_name*)names.RemTail())
	{
		delete n;
	}
}
colornamegroup::~colornamegroup()
{
	if (p) delete p;
}

int colornamegroup::Create(void)
{
	p->start();
	return bordergroup::Create();
}
void colornamegroup::Free(void)
{
	bordergroup::Free();
}

int colornamegroup::GActivate(XEvent *e,int why)
{
	return 0;
}

char *colornamegroup::GetColor(void)
{
	return p->curcolor;
}
void colornamegroup::SetColor(char *n)
{
	strncpy(p->curcolor,n,sizeof(p->curcolor));
	if (p->curcolor[0])
	{
		color_name *n;
		if (n=(color_name*)p->names.FindText(p->curcolor))
		{
			p->list.Selected(p->names.Find((node*)n));
			p->handle(&p->list,0);
		}
	}
}

void pcolornamegroup::handle(gadget *g,int i)
{
	color_name *n=(color_name*)names.Find(list.Selected());
	if (n)
	{
		unsigned long ocol=acol;
		strcpy(curcolor,n->name);
		acol=prev.AllocColor(curcolor);
		prev.Foreground(acol);
		prev.Clear();
		prev.Draw();
		if (ocol)
			prev.FreeColor(ocol);
	}
}
void pcolornamegroup::start(void)
{
	FILE *f;

	if (f=fopen("/usr/lib/X11/rgb.txt","r"))
	{
		char buf[100];
		setvbuf(f,NULL,_IOFBF,1000);
		while (fgets(buf,sizeof(buf),f))
		{
			if (buf[0]!='!')
			{
				char *c=buf;
				while (*c && !isalpha(*c)) c++;
				if (*c)
				{
					while (c[strlen(c)-1]=='\n') c[strlen(c)-1]=0;
					color_name *n=new color_name;
					strncpy(n->name,c,sizeof(n->name));
					n->base.text=n->name;
					names.AddAlpha((node*)n);
				}
			}
		}
		fclose(f);

		color_name *n=(color_name*)names.head;
		while (n->base.next)
		{
			list.AddAtEnd(n->name);
			n=(color_name*)n->base.next;
		}
		if (curcolor[0])
		{
			if (n=(color_name*)names.FindText(curcolor))
			{
				list.Selected(names.Find((node*)n));
				handle(&list,0);
			}
		}
	}
}

#endif
