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

class DemoWindow : public XmWindow
{
//	bool initWindowSize(int&, int&, int& nw, int& nh)		{ nw = 300; nh = 400; return(TRUE); } 

	void initialize();

	void msgboxDemo(void*);
	void prompterDemo(void*);
	void selboxDemo(void*);
	void fileboxDemo(void*);
	void userDlgDemo(void*);

	void open(void*);
	void close(void*);
	void quit(void*);
public:
	DemoWindow() : XmWindow("Demo Window")
	{ }

	void gnuInit()	{ setInitWindowSize(300, 300, 300, 400); DemoWindow::initialize(); }
	void gnuOpen()	{ gnuInit(); realize(); }
};

class DemoDialog : public XmUserDialog
{
//	bool initWindowSize(int&, int&, int&, int&);

	void createContents();
	void initContents();

	void myRadioButton(XmRadioButton*);
	void myCheckBox(XmCheckBox*);
	void myEdit(XmEdit*);
	void myListBox(XmListBox*);
	void myComboBox(XmComboBox*);
	void myPushButton(XmPushButton*);
	void myQuitButton(XmPushButton*);
public:
	DemoDialog(XmObject* parent = NULL) : XmUserDialog("Demo Dialog", parent)
	{ }

	void gnuOpen()	{ setInitWindowSize(300, 300, 470, 364); DemoDialog::createContents(); DemoDialog::initContents(); realize(); }
};

char* items[] = { "One", "Two", "Three", "Four", "Five", "Six", NULL };

// implementation ///////////////////////////////////////////////////////

void DemoWindow::initialize()
{
	XmDropdownMenu* m = createDropdownMenu();

	m->addLabels("&File", "&Test", NULL);

	m->setCurrentLabel("File");
	m->addItems(Entry("&Open", CB(DemoWindow::open)),
				Entry("&Close", CB(DemoWindow::close)), NULLENTRY);
	m->addSeparator();
	m->addItems(Entry("E&xit", CB(DemoWindow::quit)), NULLENTRY);

	m->setCurrentLabel("Test");
	m->addItems(Entry("&Message...", CB(DemoWindow::msgboxDemo)),
				Entry("&Prompter...", CB(DemoWindow::prompterDemo)),
				Entry("Se&lection box...", CB(DemoWindow::selboxDemo)),
				Entry("&File selector box...", CB(DemoWindow::fileboxDemo)), NULLENTRY);
	m->addSeparator();
	m->addItems(Entry("&User Dialog...", CB(DemoWindow::userDlgDemo)), NULLENTRY);

	addSubpane(Edit, "display");
}

void DemoWindow::msgboxDemo(void*)
{
	(new XmMsgBox("This is a message.", "Test Message", XmSdlgAppModal | XmSmsgInfo | XmSmsgOk))->showMsg();
	edit("display")->setText("viewed the message.");
}

void DemoWindow::prompterDemo(void*)
{
	char* txt = (new XmPrompter("This is a prompter. Enter some text:", "Test Prompter", "nothing"))->prompt();
	edit("display")->setText(txt ? txt : " propter canceled.");
}

void DemoWindow::selboxDemo(void*)
{
	int ndx = (new XmListPrompter(items, "This is a list-prompter. Select an item:", "Test List Prompter", "<nothing>"))->promptIndex();
	edit("display")->setText(ndx ? items[ndx] : " list prompter canceled.");
}

void DemoWindow::fileboxDemo(void*)
{
	char* txt = (new XmFileSelector("/*"))->promptFile();
	edit("display")->setText(txt ? txt : " file selector canceled.");
}

void DemoWindow::userDlgDemo(void*)
{
	(new DemoDialog(this))->gnuOpen();		// open();
}

void DemoWindow::open(void*)
{
	(new DemoWindow)->gnuOpen();		// open();
}

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

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

#include "demodlg.C"

void DemoDialog::initContents()
{
	radioButton("RadioButton1")->setCallback(this, CB(DemoDialog::myRadioButton));
	radioButton("RadioButton2")->setCallback(this, CB(DemoDialog::myRadioButton));
	radioButton("RadioButton3")->setCallback(this, CB(DemoDialog::myRadioButton));
	pushButton("PushButton")->setCallback(this, CB(DemoDialog::myPushButton));
	checkBox("CheckBox")->setCallback(this, CB(DemoDialog::myCheckBox));
	edit("Edit1")->setCallback(this, CB(DemoDialog::myEdit));
	edit("Edit2")->setCallback(this, CB(DemoDialog::myEdit));
	listBox("ListBox")->addAll(items);
	listBox("ListBox")->setCallback(this, CB(DemoDialog::myListBox));
	comboBox("ComboBox")->addAll(items);
	((XmGroupBox* )comboBox("ComboBox"))->setCallback(this, CB(DemoDialog::myComboBox));
	pushButton("QuitButton")->setCallback(this, CB(DemoDialog::myQuitButton));
}

void DemoDialog::myPushButton(XmPushButton* p)
{
	cout << "pushButton: " << p->getName() << "\n";
}

void DemoDialog::myCheckBox(XmCheckBox* b)
{
	cout << "checkBox: " << b->getName() << " state: " << b->getState() << "\n";
}

void DemoDialog::myRadioButton(XmRadioButton* r)
{
	cout << "radioButton: " << r->getName() << " selected.\n";
}

void DemoDialog::myEdit(XmEdit* e)
{
	cout << "edit: " << e->getName() << " new text: " << e->getText() << "\n";
}

void DemoDialog::myListBox(XmListBox* l)
{
	cout << "listBox: " << l->getName() << " selection: " << l->selectedItem() << " (" << l->selectedIndex() << ")\n";
	cout << "  currently selected: \n";
	char** strings = l->selectedItems();
	int* ndxs = l->selectedIndices();
	for(int i = 0; strings[i]; i++)
		cout << strings[i] << " (" << ndxs[i] << ")\n";
	cout << "\n";
}

void DemoDialog::myComboBox(XmComboBox* l)
{
	cout << "comboBox: " << l->getName() << "\n";
	cout << "current text: " << l->getText() << ".\n";
	cout << "currently selected: " << l->selectedItem() << " (" << l->selectedIndex() << ")\n";
}

void DemoDialog::myQuitButton(XmPushButton* p)
{
	destroyOnDelete();
	delete this;
}


// application init method ///////////////////////////////////////////

void XmApp::initialize()
{
	(new DemoWindow)->gnuInit();	// initialize();
};

