#include "designer.h"
#include "appwindow.h"
#include "toplevelwindow.h"

#ifdef __unix__
#define SEPARATOR "/"
#endif

#ifndef __unix__
#define SEPARATOR "\\\\"
#endif

const char* AppWindow::type="AppWindow";

class AppWindow::MyWindowListener:public TkTopLevelListener
{
	AppWindow* parent;
public:
	MyWindowListener(AppWindow* parent):parent(parent)
	{}
	void onDestroy(TkTopLevel* source)
	{
		parent->designer->onExit();
	}
};

AppWindow::AppWindow(Ptr<TclInterp>& interp,Designer* designer)
{
	this->designer=designer;
	this->window=new TkTopLevel(interp);
	this->interp=interp;
	window->setWindowListener(new MyWindowListener(this));
	
	butArrow=new ToolButton(interp,this,"arrow");
	buttons.push_back(butArrow);
	buttons.push_back(new ToolButton(interp,this,"button"));
	buttons.push_back(new ToolButton(interp,this,"entry"));
	buttons.push_back(new ToolButton(interp,this,"checkbutton"));
	buttons.push_back(new ToolButton(interp,this,"label"));
	buttons.push_back(new ToolButton(interp,this,"listbox"));
	buttons.push_back(new ToolButton(interp,this,"radiobutton"));
	buttons.push_back(new ToolButton(interp,this,"spinbox"));
	buttons.push_back(new ToolButton(interp,this,"text"));

	int i=0;
	Ptr<TclStringVar> var=new TclStringVar(interp);
	for(list<Ptr<ToolButton> >::iterator it=buttons.begin();it!=buttons.end();it++)
	{
		Ptr<ToolButton> button=*it;
		window->addChild(button->getButton(),new TkPlaceConstraints(i,26,24,24));
		button->getButton()->setOption(TkRadioButton::OPT_VARIABLE,var);
		i+=26;
	}

	(*buttons.begin())->getButton()->setSelected(true);
	activeButton=(*buttons.begin());
		
	Ptr<TkButton> tButton=new TkButton(interp);
	string topLevelGif=string("images")+SEPARATOR+"toplevel.gif";
	tButton->setOption(TkCheckButton::OPT_IMAGE,new TkPhoto(interp,topLevelGif.c_str()));
	tButton->setOption(TkCheckButton::OPT_INDICATORON,new TkBoolOption(false));
	window->addChild(tButton,new TkPlaceConstraints(i+12,26,24,24));
	tButton->addButtonPressListener(this);

	window->setLayoutManager(new TkPlaceLayout());
	window->setBounds(600,72,0,0);
	window->setResizable(false,false);

	window->create();
	window->setTitle("Designer");
	window->doLayout();

	Ptr<TkMenu> menu=new TkMenu(interp);
	
	Ptr<TkMenu> mFile=new TkMenu(interp);
	mFile->setOption(TkMenu::OPT_TEAROFF,new TkBoolOption(false));
	mFile->addCommand("Save","save");
	mFile->addSeparator();
	mFile->addCommand("Exit","exit");
	menuListener=new AppWindow::MyMenuListener(this);
	mFile->addMenuListener(menuListener);

	Ptr<TkMenu> mShow=new TkMenu(interp);
	mShow->setOption(TkMenu::OPT_TEAROFF,new TkBoolOption(false));
	mShow->addCommand("Properties editor","editor");
	mShow->addCommand("Forms list","list");
	mShow->addMenuListener(menuListener);

	menu->addSubmenu("File",mFile);
	menu->addSubmenu("Show",mShow);
    
	window->setMenu(menu);
}

void AppWindow::onButtonPress(EHeader& header,int button,const string& state,int x,int y,int x_root,int y_root)
{	
	TkTopLevel* topLevel=new TkTopLevel(interp);
	topLevel->setLayoutManager(new TkPlaceLayout());
	topLevel->create();
	topLevel->addButtonPressListener(new TopLevelWindow(interp,designer,topLevel));

	lstTopLevels.push_back(Ptr<TkTopLevel>(topLevel));
	designer->getGui()->insertSourceForm(topLevel);
	designer->getFrmList()->newTopLevel(topLevel);
}

void AppWindow::enlistWindow(TkTopLevel* topLevel)
{
	lstTopLevels.push_back(Ptr<TkTopLevel>(topLevel));
	topLevel->addButtonPressListener(new TopLevelWindow(interp,designer,topLevel));
}

void AppWindow::toolButtonPressed(TkBase* button)
{
	for(t_buttons::iterator it=buttons.begin();it!=buttons.end();it++)
	{
		if ((*it)->getButton().asObject()==button)
			activeButton=*it;
	}
}
void AppWindow::setArrowToolActive()
{
	window->getRadioGroupByName("toolbar")->set(butArrow->getGeneratedId());
	activeButton=butArrow;
}

void AppWindow::MyMenuListener::handleEvent(TkMenu* menu,const string& commandId)
{
	if (commandId=="save")
	{
        parent->designer->saveAll();		
	}
	if (commandId=="exit")
	{
		parent->designer->onExit();
	}
	if (commandId=="editor")
	{
		parent->designer->getPropertyEditor()->setVisible(true);
	}
	if (commandId=="list")
	{
		parent->designer->getFrmList()->setVisible(true);
	}
}

void AppWindow::setVisible(bool value)
{
	if (value) 
		window->setWindowState(TkWinStateOption::normal);
	else
		window->setWindowState(TkWinStateOption::withdrawn);
}

