#ifndef CODE_FILEINPUT_CXX
#define CODE_FILEINPUT_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: fileinput.cxx,v 1.6 1999/12/04 23:06:59 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/fileinput.h>
#include "include/fileinput_p.h"

fileinput::fileinput(void) : group(), fi_fdialog()
{
	while (!(p=new pfileinput))
	{
		outOfMemory("fileinput");
	}
	p->o=this;
	Name("fileinput");
	Horiz();
	UseSmallest();
	p->fi_input.Name("fi_input");
	p->fi_input.ParentClass(this);
	fi_fdialog.Name("fi_fdialog");
	fi_fdialog.ParentClass(this);
	p->fi_input.Weight(150);
	p->fi_input.Callback(this,(CALLBACK)&fileinput::CallCallback);
	p->fi_input.MaxChars(INPUTSIZE);
	Add(p->fi_input);
}

pfileinput::pfileinput() : fi_input()
{
	fi_button=NULL;
	fi_gbutton=NULL;
	fi_gfx=NULL;
}
pfileinput::~pfileinput()
{
	if (fi_button) delete fi_button;
	if (fi_gbutton) delete fi_gbutton;
	if (fi_gfx) delete fi_gfx;
}
fileinput::~fileinput()
{
	if (win) Free();
	if (p) delete p;
}

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

	if (!setlimit) GSetLimits();
    if (group::Create())
    {
		return 1;
    }
    return 0;
}
void fileinput::Free(void)
{
	group::Free();
}

void pfileinput::opend(gadget *g,int i)
{
 	char buf[INPUTSIZE];
 	fi_input.Input(buf);
 	o->fi_fdialog.SetFile(buf);
	if (o->fi_fdialog.Start((Xwindows*)o->ParentClassType("Xwindows")))
	{
		char dbuf[500];
		o->fi_fdialog.GetDir(dbuf,sizeof(dbuf));
		char fbuf[500];
		o->fi_fdialog.GetFile(fbuf,sizeof(fbuf));
		char tbuf[1000];
		strcpy(tbuf,dbuf);
		strcat(tbuf,fbuf);
		fi_input.Default(tbuf);
		o->CallCallback();
	}
}
void pfileinput::mode(int m)
{
	if (fi_gfx) delete fi_gfx;
	fi_gfx=NULL;
	switch (m)
	{
		case 1:	// command
			fi_gfx=new gfx_command;
			o->fi_fdialog.LoadMode();
			break;
		case 2:	// load file
			fi_gfx=new gfx_file;
			o->fi_fdialog.LoadMode();
			break;
		case 3:	// save file
			fi_gfx=new gfx_file;
			o->fi_fdialog.SaveMode();
			break;
		case 4:	// directory
			fi_gfx=new gfx_file;
			o->fi_fdialog.LoadMode();
			o->fi_fdialog.DirSelector();
			break;
	}
}

void fileinput::GSetLimits(void)
{
	if (!setlimit)
	{
		char *r=GetResource("mode");
		if (r)
		{
			if (!strcasecmp(r,"command")) p->mode(1);
			else if (!strcasecmp(r,"loadfile")) p->mode(2);
			else if (!strcasecmp(r,"Savefile")) p->mode(3);
			else p->mode(0);
		}

		if (p->fi_gfx)
		{
			p->fi_gbutton=new go_button;
			p->fi_gbutton->Name("fi_button");
			p->fi_gbutton->Aspect(1,1,1,1);
			p->fi_gbutton->Object(p->fi_gfx);
			p->fi_gbutton->Callback(p,(CALLBACK)&pfileinput::opend);
			p->fi_gbutton->Symmetric();
			p->fi_gbutton->UseLocked();
			Add(p->fi_gbutton);
			p->fi_gfx->ParentClass(p->fi_gbutton);
		}
		else
		{
			p->fi_button=new button;
			p->fi_button->Name("fi_button");
			p->fi_button->Weight(30);
			p->fi_button->Text("browse...");
			p->fi_button->Callback(p,(CALLBACK)&pfileinput::opend);
			Add(p->fi_button);
		}
	}
	if (gg_locked()) Lock();
	group::GSetLimits();
	setlimit=1;
}

void fileinput::SetFile(char *t)
{
	p->fi_input.Default(t);
}
void fileinput::GetFile(char *t,int size)
{
	char buf[INPUTSIZE];
	p->fi_input.Input(buf);
	strncpy(t,buf,size);
	t[size-1]=0;
}
void fileinput::ModeCommand(void)
{
	p->mode(1);
}
void fileinput::ModeLoadFile(void)
{
	p->mode(2);
}
void fileinput::ModeSaveFile(void)
{
	p->mode(3);
}
void fileinput::ModeDir(void)
{
	p->mode(4);
}

#endif
