#include "designer.h"
#include "toplevelwindow.h"
#include <iostream>

using namespace std;

class MyWindowListener:public TkTopLevelListener
{
public:
	void onDestroy(TkTopLevel* source)
	{
		source->setWindowState(TkWinStateOption::withdrawn);
	}
};

class TopLevelWindow::MyConfigureListener:public ConfigureListener
{
	TopLevelWindow* parent;
public:
	MyConfigureListener(TopLevelWindow* parent):parent(parent)
	{
	}
	void onConfigure(EHeader& header,const string& above,int height,int width,int borderWidth,bool override,const string& valueMask,int x,int y)
	{
		if (parent->window==parent->designer->getPropertyEditor()->getObject())
		{
			parent->designer->getPropertyEditor()->setBoundValues(x,y,width,height);
		}
	}
};

TopLevelWindow::TopLevelWindow(TclInterp* interp,Designer* designer,TkTopLevel* window)
	:interp(interp),designer(designer),window(window)
{
	window->setWindowListener(new MyWindowListener());
	window->addConfigureListener(new MyConfigureListener(this));
}

void TopLevelWindow::onButtonPress(EHeader& header,int button,const string& state,int x,int y,int x_root,int y_root)
{
	string name=designer->getAppWindow()->getActiveButton()->getName();

	TkBase* component=window->getChildByCoord(x_root,y_root);
	if (component!=0)
	{
		designer->getPropertyEditor()->setObject(component);
		return;
	}

	if (name=="arrow")
	{
		designer->getPropertyEditor()->setObject(window);
	}

	if (name=="button")
	{
		Ptr<TkButton> button=new TkButton(interp);
		window->addChild(button,new TkPlaceConstraints(x,y,24,24));
		designer->getPropertyEditor()->setObject(button);
		designer->getAppWindow()->setArrowToolActive();
	}
	if (name=="entry")
	{
		Ptr<TkEntry> entry=new TkEntry(interp);
		window->addChild(entry,new TkPlaceConstraints(x,y,64,24));
		designer->getPropertyEditor()->setObject(entry);
		designer->getAppWindow()->setArrowToolActive();
	}
	if (name=="checkbutton")
	{
		Ptr<TkCheckButton> chxButton=new TkCheckButton(interp);
		window->addChild(chxButton,new TkPlaceConstraints(x,y,64,24));
		designer->getPropertyEditor()->setObject(chxButton);
		designer->getAppWindow()->setArrowToolActive();
	}
	if (name=="label")
	{
		Ptr<TkLabel> label=new TkLabel(interp);
		window->addChild(label,new TkPlaceConstraints(x,y,64,24));
		designer->getPropertyEditor()->setObject(label);
		designer->getAppWindow()->setArrowToolActive();
	}
	if (name=="listbox")
	{
		Ptr<TkListbox> listbox=new TkListbox(interp);
		window->addChild(listbox,new TkPlaceConstraints(x,y,64,32));
		designer->getPropertyEditor()->setObject(listbox);
		designer->getAppWindow()->setArrowToolActive();
	}
	if (name=="radiobutton")
	{
		Ptr<TkRadioButton> rdButton=new TkRadioButton(interp);
		window->addChild(rdButton,new TkPlaceConstraints(x,y,64,24));
		designer->getPropertyEditor()->setObject(rdButton);
		designer->getAppWindow()->setArrowToolActive();
	}
	if (name=="spinbox")
	{
		Ptr<TkSpinBox> spinbox=new TkSpinBox(interp);
		window->addChild(spinbox,new TkPlaceConstraints(x,y,64,24));
		designer->getPropertyEditor()->setObject(spinbox);
		designer->getAppWindow()->setArrowToolActive();
	}
	if (name=="text")
	{
		Ptr<TkText> text=new TkText(interp);
		window->addChild(text,new TkPlaceConstraints(x,y,32,32));
		designer->getPropertyEditor()->setObject(text);
		designer->getAppWindow()->setArrowToolActive();
	}
}

/*
void TopLevelWindow::onFocusIn(EHeader& header,const string& detail,const string& mode)
{
	cout<<"focus in"<<endl;
	//designer->getPropertyEditor()->setObject(window);
}
*/
