#include <stream.h>
#include <fstream.h>
#include <stdlib.h>
#include <xmObject.h>

class TestWindow : public XmWindow
{
	XmStaticImage* img;
	char imgNames[100][100];
	int imgCount, curImg;
public:
	TestWindow();

	void cmd(char*);
	void open(void*);
	void close(void*);
	void about(void*);

	void quit(void*);
};


void XmApp::initialize()
{
	new TestWindow;
};

TestWindow::TestWindow() : XmWindow("Test")
{
	char* fn = "images.lst";

	if(App->getCmdArg(1))
		fn = App->getCmdArg(1);
	ifstream in(fn);
	if(!in.good())
	{	cout << App->getCmdArg(0) << ": invalid file \"" << fn << "\"\n";
		quit(NULL);
	}
	imgCount = curImg = 0;
	while(in.good())
	{	in.getline(imgNames[imgCount++], 32);
//		cout << imgNames[imgCount - 1] << "\n";
	}
	imgCount--;

	setInitWindowSize(300, 300, 500, 400);

	XmDropdownMenu* m = createDropdownMenu();

	m->addLabels("&File", "&Misc", NULL);
	m->setCurrentLabel("File");
	m->addItems(Entry("&Open", CB(TestWindow::open)),
				Entry("&Close", CB(TestWindow::close)), NULLENTRY);
	m->addSeparator();
	m->addItem(Entry("E&xit", CB(TestWindow::quit)));
	m->setCurrentLabel("Misc");
	m->addItem(Entry("&About...", CB(TestWindow::about)));

	addSubpane(img = new XmStaticImage("img"));
	img->setImage(getRuntimeImage(imgNames[curImg]));

	XmToolBar* tb;

	tb = addToolbar(XmSleft);
	tb->add((new XmPushButton("&Next")), this, CB(TestWindow::cmd), CB_OBJ_NAME);
	tb->add((new XmPushButton("&Prev")), this, CB(TestWindow::cmd), CB_OBJ_NAME);
	tb->pushButton("Prev")->disable();
}

void TestWindow::cmd(char* n)
{
	switch(*n)
	{	case 'N':
		curImg++;
		break;
		case 'P':
		curImg--;
		break;
	}
	if(curImg > imgCount - 1)
		toolbar(XmSleft)->pushButton("Next")->disable();
	else
		toolbar(XmSleft)->pushButton("Next")->enable();
	if(curImg == 0)
		toolbar(XmSleft)->pushButton("Prev")->disable();
	else
		toolbar(XmSleft)->pushButton("Prev")->enable();
	img->setImage(getRuntimeImage(imgNames[curImg]));
}

void TestWindow::open(void*)
{
	(new TestWindow)->realize();
}

void TestWindow::close(void*)
{
	destroyOnDelete();
	delete this;
}

void TestWindow::about(void*)
{
}

void TestWindow::quit(void*)
{
	exit(0);
}




