#include "WidgetRegistry.h"
#include "../Link.h"
#include "../Game/Pointer.h"
#include "Checkbox.h"
#include "Textbox.h"
#include "Slider.h"
#include "Panel.h"
#include "Listbox.h"
#include "Combobox.h"
#include "Tabpanel.h"
#include "BorderData.h"

using namespace std;

namespace gorgon { namespace widgets {

	WidgetRegistry::WidgetRegistry() : info(new DataArray),
		fonts(new DataArray), colors(new DataArray),
		pointers(new Folder), buttons(new Folder),
		textboxes(new Folder), checkboxes(new Folder),
		labels(new Folder), listboxes(new Folder),
		sliders(new Folder), progressbars(new Folder),
		panels(new Folder), others(new Folder),
		separators(new Folder), borders(new Folder),
		icons(new Folder), pictures(new Folder), sounds(new Folder)
	{
		Subitems.Add(info);
		Subitems.Add(fonts);
		Subitems.Add(colors);
		Subitems.Add(pointers);
		Subitems.Add(buttons);
		Subitems.Add(textboxes);
		Subitems.Add(checkboxes);
		Subitems.Add(labels);
		Subitems.Add(listboxes);
		Subitems.Add(sliders);
		Subitems.Add(progressbars);
		Subitems.Add(panels);
		Subitems.Add(others);
		Subitems.Add(separators);
		Subitems.Add(borders);
		Subitems.Add(icons);
		Subitems.Add(pictures);
		Subitems.Add(sounds);

		info->SetCaption("Info");
		fonts->SetCaption("Fonts");
		colors->SetCaption("Colors");
		pointers->SetCaption("Pointers");
		buttons->SetCaption("Buttons");
		textboxes->SetCaption("Textboxes");
		checkboxes->SetCaption("Checkboxes");
		labels->SetCaption("Labels");
		listboxes->SetCaption("Listboxes");
		sliders->SetCaption("Sliders");
		progressbars->SetCaption("Progressbars");
		panels->SetCaption("Panels");
		others->SetCaption("Others");
		separators->SetCaption("Separators");
		borders->SetCaption("Borders");
		icons->SetCaption("Icons");
		icons->SignificantNames=true;
		pictures->SetCaption("Pictures");
		pictures->SignificantNames=true;
		sounds->SetCaption("Sounds");
		sounds->SignificantNames=true;
	}

	void WidgetRegistry::Save(std::ostream &out) {
		info->DeleteAll();
		fonts->DeleteAll();
		colors->DeleteAll();

		buttons->DeleteAll();
		textboxes->DeleteAll();
		checkboxes->DeleteAll();
		labels->DeleteAll();
		listboxes->DeleteAll();
		sliders->DeleteAll();
		progressbars->DeleteAll();
		panels->DeleteAll();
		others->DeleteAll();
		separators->DeleteAll();
		borders->DeleteAll();

		info->Add(parenttype::WidgetSpacing).name="Widget spacing";
		info->Add(parenttype::ShadowMargins).name="Shadow margins";

		fonts->Add(parenttype::Fonts.Normal		).name="Normal";
		fonts->Add(parenttype::Fonts.H1			).name="H1";
		fonts->Add(parenttype::Fonts.H2			).name="H2";
		fonts->Add(parenttype::Fonts.H3			).name="H3";
		fonts->Add(parenttype::Fonts.Strong		).name="Strong";
		fonts->Add(parenttype::Fonts.Emphasize	).name="Emphasize";
		fonts->Add(parenttype::Fonts.Quote		).name="Quote";
		fonts->Add(parenttype::Fonts.Info		).name="Info";
		fonts->Add(parenttype::Fonts.Small		).name="Small";
		fonts->Add(parenttype::Fonts.Link		).name="Link";
		fonts->Add(parenttype::Fonts.Required	).name="Required";
		fonts->Add(parenttype::Fonts.Important	).name="Important";
		fonts->Add(parenttype::Fonts.Error		).name="Error";
		fonts->Add(parenttype::Fonts.Warning		).name="Warning";
		fonts->Add(parenttype::Fonts.Success		).name="Success";
		fonts->Add(parenttype::Fonts.Tooltip		).name="Tooltip";
		fonts->Add(parenttype::Fonts.Fixed		).name="Fixed";
		fonts->Add(parenttype::Fonts.Decorative	).name="Decorative";

		colors->Add(parenttype::Colors.Text					).name="Text";
		colors->Add(parenttype::Colors.Forecolor				).name="Forecolor";
		colors->Add(parenttype::Colors.Background			).name="Background";
		colors->Add(parenttype::Colors.Alternate				).name="Alternate";
		colors->Add(parenttype::Colors.AlternateBackground	).name="Alternate BG";
		colors->Add(parenttype::Colors.Highlight				).name="Highlight";
		colors->Add(parenttype::Colors.Light					).name="Light";
		colors->Add(parenttype::Colors.Dark					).name="Dark";
		colors->Add(parenttype::Colors.Saturated				).name="Saturated";
		colors->Add(parenttype::Colors.Desaturated			).name="Desaturated";
		colors->Add(parenttype::Colors.Shadow				).name="Shadow";

		buttons->Add(new Link(parenttype::Buttons.Button		,"Button"));
		buttons->Add(new Link(parenttype::Buttons.Dialog		,"Dialog"));
		buttons->Add(new Link(parenttype::Buttons.Menu		,"Menu"));
		buttons->Add(new Link(parenttype::Buttons.Tool		,"Tool"));
		buttons->Add(new Link(parenttype::Buttons.Navigation	,"Navigation"));
		buttons->Add(new Link(parenttype::Buttons.Large		,"Large"));
		buttons->Add(new Link(parenttype::Buttons.Small		,"Small"));
		buttons->Add(new Link(parenttype::Buttons.Symbol		,"Symbol"));
		buttons->Add(new Link(parenttype::Buttons.Browse		,"Browse"));
		buttons->Add(new Link(parenttype::Buttons.Decorative	,"Decorative"));

		textboxes->Add(new Link(parenttype::Textboxes.Textbox	,"Textbox"));
		textboxes->Add(new Link(parenttype::Textboxes.Numberbox	,"Numberbox"));
		textboxes->Add(new Link(parenttype::Textboxes.Small		,"Small"));
		textboxes->Add(new Link(parenttype::Textboxes.Password	,"Password"));
		textboxes->Add(new Link(parenttype::Textboxes.TextEdit	,"TextEdit"));
		textboxes->Add(new Link(parenttype::Textboxes.CodeEdit	,"CodeEdit"));

		checkboxes->Add(new Link(parenttype::Checkboxes.Checkbox		,"Checkbox"));
		checkboxes->Add(new Link(parenttype::Checkboxes.Radio		,"Radio"));
		checkboxes->Add(new Link(parenttype::Checkboxes.Toggle		,"Toggle"));
		checkboxes->Add(new Link(parenttype::Checkboxes.More			,"More"));
		checkboxes->Add(new Link(parenttype::Checkboxes.MenuCheck	,"MenuCheck"));
		checkboxes->Add(new Link(parenttype::Checkboxes.MenuRadio	,"MenuRadio"));
		checkboxes->Add(new Link(parenttype::Checkboxes.Lock			,"Lock"));

		labels->Add(new Link(parenttype::Labels.Label		,"Label"));
		labels->Add(new Link(parenttype::Labels.Title		,"Title"));
		labels->Add(new Link(parenttype::Labels.	DataCaption	,"DataCaption"));
		labels->Add(new Link(parenttype::Labels.	Bold		,"Bold"));
		labels->Add(new Link(parenttype::Labels.	Heading		,"Heading"));
		labels->Add(new Link(parenttype::Labels.	Subheading	,"Subheading"));
		labels->Add(new Link(parenttype::Labels.	ListCaption	,"ListCaption"));
		labels->Add(new Link(parenttype::Labels.	Required	,"Required"));
		labels->Add(new Link(parenttype::Labels.	Hint		,"Hint"));
		labels->Add(new Link(parenttype::Labels.	Tooltip		,"Tooltip"));
		labels->Add(new Link(parenttype::Labels.	Link		,"Link"));

		listboxes->Add(new Link(parenttype::Listboxes.Listbox	,"Listbox"));
		listboxes->Add(new Link(parenttype::Listboxes.Action		,"Action"));
		listboxes->Add(new Link(parenttype::Listboxes.Picture	,"Picture"));
		listboxes->Add(new Link(parenttype::Listboxes.File		,"File"));
		listboxes->Add(new Link(parenttype::Listboxes.FileView	,"FileView"));
		listboxes->Add(new Link(parenttype::Listboxes.Folder		,"Folder"));
		listboxes->Add(new Link(parenttype::Listboxes.DataTable	,"DataTable"));
		listboxes->Add(new Link(parenttype::Listboxes.Detailed	,"Detailed"));
		listboxes->Add(new Link(parenttype::Listboxes.Checklist	,"Checklist"));
		listboxes->Add(new Link(parenttype::Listboxes.Radiolist	,"Radiolist"));
		listboxes->Add(new Link(parenttype::Listboxes.Edit		,"Edit"));

		sliders->Add(new Link(parenttype::Sliders.Slider			,"Slider"));
		sliders->Add(new Link(parenttype::Sliders.Selectbar		,"Selectbar"));
		sliders->Add(new Link(parenttype::Sliders.ImageSelector	,"ImageSelector"));
		sliders->Add(new Link(parenttype::Sliders.Named			,"Named"));
		sliders->Add(new Link(parenttype::Sliders.Radiobar		,"Radiobar"));
		sliders->Add(new Link(parenttype::Sliders.NumberSpinner	,"NumberSpinner"));
		sliders->Add(new Link(parenttype::Sliders.ItemSpinner	,"ItemSpinner"));

		sliders->Add(new Link(parenttype::Scrollbars.Scrollbar	,"Scrollbar"));
		sliders->Add(new Link(parenttype::Scrollbars.Dialog		,"Dialog"));
		sliders->Add(new Link(parenttype::Scrollbars.Picture		,"Picture"));
		
		progressbars->Add(new Link(parenttype::Progressbars.Progressbar	,"Progressbar"));
		progressbars->Add(new Link(parenttype::Progressbars.File			,"File"));
		progressbars->Add(new Link(parenttype::Progressbars.Working		,"Working"));
		progressbars->Add(new Link(parenttype::Progressbars.Loading		,"Loading"));
		progressbars->Add(new Link(parenttype::Progressbars.Download		,"Download"));
		progressbars->Add(new Link(parenttype::Progressbars.Percent		,"Percent"));
		progressbars->Add(new Link(dynamic_cast<gorgon::Base*>(parenttype::Progressbars.Unknown),"Unknown"));
		
		panels->Add(new Link(parenttype::Panels.Panel			,"Panel"));
		panels->Add(new Link(parenttype::Panels.OverlayControls	,"OverlayControls"));
		panels->Add(new Link(parenttype::Panels.Window			,"Window"));
		panels->Add(new Link(parenttype::Panels.DialogWindow		,"DialogWindow"));
		panels->Add(new Link(parenttype::Panels.Toolbar			,"Toolbar"));
		panels->Add(new Link(parenttype::Panels.Menubar			,"Menubar"));
		panels->Add(new Link(parenttype::Panels.ToolWindow		,"ToolWindow"));
		panels->Add(new Link(parenttype::Panels.SettingsWindow	,"SettingsWindow"));
		panels->Add(new Link(parenttype::Panels.Tabpanel			,"Tabpanel"));
		panels->Add(new Link(parenttype::Panels.Tooltab			,"Tooltab"));
		panels->Add(new Link(parenttype::Panels.Settingstab		,"Settingstab"));
		panels->Add(new Link(parenttype::Panels.Fullscreen		,"Fullscreen"));

		others->Add(new Link(parenttype::Others.Combobox		,"Combobox"));
		others->Add(new Link(parenttype::Others.Autocomplete	,"Autocomplete"));
		others->Add(new Link(parenttype::Others.JumpList		,"JumpList"));

		borders->Add(new Link((gorgon::Base*)NULL,"Picture frame"));
		borders->Add(new Link((gorgon::Base*)NULL,"Photo frame"));
		borders->Add(new Link((gorgon::Base*)NULL,"Thin frame"));
		borders->Add(new Link((gorgon::Base*)NULL,"Thick frame"));
		borders->Add(new Link(parenttype::Borders.Selection	,"Selection"));
		borders->Add(new Link(parenttype::Borders.Focus		,"Focus"));
		borders->Add(new Link((gorgon::Base*)NULL,"Line numbers"));
		borders->Add(new Link((gorgon::Base*)NULL,"Service column"));

		auto start=WriteObjectStart(out, *this);

		WriteSubItems(out, *this);

		WriteEnd(out, start);
	}

	template<class T_>
	T_ *GetWithDefault(Folder *folder, File &file, int ind, T_ *def) {
		T_ *temp=NULL;
		if(folder->Subitems.GetCount()>ind) {
			try {
				Link &lnk=folder->Get<Link>(ind);
				temp=dynamic_cast<T_*>(lnk.GetTargetPtr());
			} 
			catch(...) { }
		}

		if(!temp)
			temp=def;

		return temp;
	}

	void WidgetRegistry::Prepare(gge::GGEMain &main, File &file) {
		Base::Prepare(main,file);

		int item=18;

		DataArray &infodata=Get<DataArray>(item);
		WidgetSpacing=infodata.getPoint(0);
		if(infodata.GetCount()>1)
			ShadowMargins=infodata.getMargins(1);


		DataArray &fontdata=Get<DataArray>(++item);
		Fonts.Normal		=fontdata.getFont(0);
		Fonts.H1			=fontdata.getFont(1);
		Fonts.H2			=fontdata.getFont(2);
		Fonts.H3			=fontdata.getFont(3);
		Fonts.Strong		=fontdata.getFont(4);
		Fonts.Emphasize		=fontdata.getFont(5);
		Fonts.Quote			=fontdata.getFont(6);
		Fonts.Info			=fontdata.getFont(7);
		Fonts.Small			=fontdata.getFont(8);
		Fonts.Link			=fontdata.getFont(9);
		Fonts.Required		=fontdata.getFont(10);
		Fonts.Important		=fontdata.getFont(11);
		Fonts.Error			=fontdata.getFont(12);
		Fonts.Warning		=fontdata.getFont(13);
		Fonts.Success		=fontdata.getFont(14);
		Fonts.Tooltip		=fontdata.getFont(15);
		Fonts.Fixed			=fontdata.getFont(16);
		Fonts.Decorative	=fontdata.getFont(17);


		DataArray &colordata=Get<DataArray>(++item);
		Colors.Text					=colordata.getInt(0);
		Colors.Forecolor			=colordata.getInt(1);
		Colors.Background			=colordata.getInt(2);
		Colors.Alternate			=colordata.getInt(3);
		Colors.AlternateBackground	=colordata.getInt(4);
		Colors.Highlight			=colordata.getInt(5);
		Colors.Light				=colordata.getInt(6);
		Colors.Dark					=colordata.getInt(7);
		Colors.Saturated			=colordata.getInt(8);
		Colors.Desaturated			=colordata.getInt(9);
		Colors.Shadow				=colordata.getInt(10);


		Folder *folder;

		//gge::Pointers.Fetch(); //!todo
		folder=&Get<Folder>(++item);
		for(auto it=folder->Subitems.First();it.IsValid();it.Next()) {
			gge::Pointers.Add(new gge::Pointer(dynamic_cast<Pointer&>(*it)));
			pointers->Add(*it);
		}
		folder->RemoveAll();
		gge::Pointers.Show();

		folder=&Get<Folder>(++item);
		Buttons.Button=Button=dynamic_cast<widgets::Checkbox*>(folder->Get<Link>(0).GetTargetPtr());
		Buttons.Dialog		= GetWithDefault(folder, file, 1, Buttons.Button);
		Buttons.Menu		= GetWithDefault(folder, file, 2, Buttons.Button);
		Buttons.Tool		= GetWithDefault(folder, file, 3, Buttons.Dialog);
		Buttons.Navigation	= GetWithDefault(folder, file, 4, Buttons.Dialog);
		Buttons.Large		= GetWithDefault(folder, file, 5, Buttons.Button);
		Buttons.Small		= GetWithDefault(folder, file, 6, Buttons.Button);
		Buttons.Symbol		= GetWithDefault(folder, file, 7, Buttons.Tool);
		Buttons.Browse		= GetWithDefault(folder, file, 8, Buttons.Symbol);
		Buttons.Decorative	= GetWithDefault(folder, file, 9, Buttons.Large);


		folder=&Get<Folder>(++item);
		Textboxes.Textbox=Textbox=dynamic_cast<widgets::Textbox*>(folder->Get<Link>(0).GetTargetPtr());
		Textboxes.Numberbox	= GetWithDefault(folder, file, 1, Textboxes.Textbox);
		Textboxes.Small		= GetWithDefault(folder, file, 2, Textboxes.Textbox);
		Textboxes.Password	= GetWithDefault(folder, file, 3, Textboxes.Textbox);
		Textboxes.TextEdit	= GetWithDefault(folder, file, 4, Textboxes.Textbox);
		Textboxes.CodeEdit	= GetWithDefault(folder, file, 5, Textboxes.TextEdit);


		folder=&Get<Folder>(++item);
		Checkboxes.Checkbox=Checkbox=dynamic_cast<widgets::Checkbox*>(folder->Get<Link>(0).GetTargetPtr());
		Checkboxes.Radio	=RadioButton=dynamic_cast<widgets::Checkbox*>(folder->Get<Link>(1).GetTargetPtr());
		Checkboxes.Toggle	= GetWithDefault(folder, file, 2, Checkboxes.Checkbox);
		Checkboxes.More		= GetWithDefault(folder, file, 3, Checkboxes.Checkbox);
		Checkboxes.MenuCheck= GetWithDefault(folder, file, 4, Checkboxes.Checkbox);
		Checkboxes.MenuRadio= GetWithDefault(folder, file, 5, Checkboxes.Radio);
		Checkboxes.Lock		= GetWithDefault(folder, file, 6, Checkboxes.Checkbox);


		folder=&Get<Folder>(++item);
		Labels.Label=Label=dynamic_cast<widgets::Checkbox*>(folder->Get<Link>(0).GetTargetPtr());
		Labels.Title		= GetWithDefault(folder, file, 1, Labels.Label);
		Labels.DataCaption	= GetWithDefault(folder, file, 2, Labels.Title);
		Labels.Bold			= GetWithDefault(folder, file, 3, Labels.Title);
		Labels.Heading		= GetWithDefault(folder, file, 4, Labels.Bold);
		Labels.Subheading	= GetWithDefault(folder, file, 5, Labels.Heading);
		Labels.ListCaption	= GetWithDefault(folder, file, 6, Labels.Title);
		Labels.Required		= GetWithDefault(folder, file, 7, Labels.Label);
		Labels.Hint			= GetWithDefault(folder, file, 8, Labels.Label);
		Labels.Tooltip		= GetWithDefault(folder, file, 9, Labels.Label);
		Labels.Link			= GetWithDefault(folder, file, 10, Labels.Label);


		folder=&Get<Folder>(++item);
		Listboxes.Listbox=Listbox=dynamic_cast<widgets::Listbox*>(folder->Get<Link>(0).GetTargetPtr());
		Listboxes.Action	= GetWithDefault(folder, file, 1, Listboxes.Listbox);
		Listboxes.Picture	= GetWithDefault(folder, file, 2, Listboxes.Listbox);
		Listboxes.File		= GetWithDefault(folder, file, 3, Listboxes.Listbox);
		Listboxes.FileView	= GetWithDefault(folder, file, 4, Listboxes.Listbox);
		Listboxes.Folder	= GetWithDefault(folder, file, 5, Listboxes.File);
		Listboxes.DataTable	= GetWithDefault(folder, file, 6, Listboxes.Listbox);
		Listboxes.Detailed	= GetWithDefault(folder, file, 7, Listboxes.DataTable);
		Listboxes.Checklist	= GetWithDefault(folder, file, 8, Listboxes.Listbox);
		Listboxes.Radiolist	= GetWithDefault(folder, file, 9, Listboxes.Checklist);
		Listboxes.Edit		= GetWithDefault(folder, file, 10, Listboxes.Listbox);


		folder=&Get<Folder>(++item);
		Sliders.Slider=Slider=dynamic_cast<widgets::Slider*>(folder->Get<Link>(0).GetTargetPtr());
		Sliders.Selectbar	 = GetWithDefault(folder, file, 2, Sliders.Slider);
		Sliders.ImageSelector= GetWithDefault(folder, file, 3, Sliders.Selectbar);
		Sliders.Named		 = GetWithDefault(folder, file, 4, Sliders.Selectbar);
		Sliders.Radiobar	 = GetWithDefault(folder, file, 5, Sliders.Selectbar);
		Sliders.NumberSpinner= GetWithDefault(folder, file, 8, Sliders.Slider);
		Sliders.ItemSpinner	 = GetWithDefault(folder, file, 9, Sliders.NumberSpinner);

		Scrollbars.Scrollbar=Scrollbar=dynamic_cast<widgets::Slider*>(folder->Get<Link>(1).GetTargetPtr());
		Scrollbars.Dialog	= GetWithDefault(folder, file, 6, Scrollbars.Scrollbar);
		Scrollbars.Picture	= GetWithDefault(folder, file, 7, Scrollbars.Scrollbar);


		folder=&Get<Folder>(++item);
		Progressbars.Progressbar=Progressbar=dynamic_cast<widgets::Slider*>(folder->Get<Link>(0).GetTargetPtr());
		Progressbars.File		 = GetWithDefault(folder, file, 1, Progressbars.Progressbar);
		Progressbars.Working	 = GetWithDefault(folder, file, 2, Progressbars.Progressbar);
		Progressbars.Loading	 = GetWithDefault(folder, file, 3, Progressbars.Progressbar);
		Progressbars.Download	 = GetWithDefault(folder, file, 4, Progressbars.Progressbar);
		Progressbars.Percent	 = GetWithDefault(folder, file, 6, Progressbars.Progressbar);

		Progressbars.Unknown=dynamic_cast<gge::animation::RectangularGraphic2DAnimationProvider*>(folder->Get<Link>(5).GetTargetPtr());
		if(!Progressbars.Unknown)
			Progressbars.Unknown=&gge::resource::NullImage::Get();

		folder=&Get<Folder>(++item);
		Panels.Panel=Panel=dynamic_cast<widgets::Panel*>(folder->Get<Link>(0).GetTargetPtr());
		Panels.OverlayControls	= GetWithDefault(folder, file, 1, Panels.Panel);
		Window=Panels.Window	= GetWithDefault(folder, file, 2, Panels.Panel);
		Panels.DialogWindow		= GetWithDefault(folder, file, 3, Panels.Window);
		Panels.Toolbar			= GetWithDefault(folder, file, 4, Panels.Panel);
		Panels.Menubar			= GetWithDefault(folder, file, 5, Panels.Toolbar);
		Panels.ToolWindow		= GetWithDefault(folder, file, 6, Panels.Window);
		Panels.SettingsWindow	= GetWithDefault(folder, file, 7, Panels.Window);
		Panels.Tabpanel=dynamic_cast<widgets::Tabpanel*>(folder->Get<Link>(8).GetTargetPtr());
		Panels.Tooltab			= GetWithDefault(folder, file, 9, Panels.Tabpanel);
		Panels.Fullscreen		= GetWithDefault(folder, file, 11, Panels.Panel);


		folder=&Get<Folder>(++item);
		Others.Combobox=Combobox=dynamic_cast<widgets::Combobox*>(folder->Get<Link>(0).GetTargetPtr());
		Others.Autocomplete	= GetWithDefault(folder, file, 1, Others.Combobox);
		Others.JumpList	= GetWithDefault(folder, file, 2, Others.Combobox);

		++item; //separators

		folder=&Get<Folder>(++item);
		Borders.Selection=dynamic_cast<BorderData*>(folder->Get<Link>(4).GetTargetPtr());
		Borders.Focus=dynamic_cast<BorderData*>(folder->Get<Link>(5).GetTargetPtr());



		for(auto it=Get<Folder>(++item).Subitems.First();it.IsValid();it.Next()) {
			try {
				if(it->GetName()=="") {
					parenttype::icons.insert(pair<string, gge::animation::RectangularGraphic2DSequenceProvider&>(it->GetName(), dynamic_cast<gge::animation::RectangularGraphic2DSequenceProvider&>(*it)));
					continue;
				}

				string name=it->GetName();
				string::size_type pos;
				do {
					pos=name.find_first_of(',');
					
					if(pos==string::npos)
						pos=name.length();
					
					string n=name.substr(0, pos);
					
					if(pos<name.length())
						name=name.substr(pos+1);
					else
						name="";

					if(n!="")
						parenttype::icons.insert(pair<string, gge::animation::RectangularGraphic2DSequenceProvider&>(n, dynamic_cast<gge::animation::RectangularGraphic2DSequenceProvider&>(*it)));
				} while(name!="");
			}
			catch(...) { }//don't insert if it is not an animation
		}

		for(auto it=Get<Folder>(++item).Subitems.First();it.IsValid();it.Next()) {
			try {
				if(it->GetName()=="") {
					parenttype::pictures.insert(pair<string, gge::animation::RectangularGraphic2DSequenceProvider&>(it->GetName(), dynamic_cast<gge::animation::RectangularGraphic2DSequenceProvider&>(*it)));
					continue;
				}

				string name=it->GetName();
				string::size_type pos;
				do {
					pos=name.find_first_of(',');

					if(pos==string::npos)
						pos=name.length();

					string n=name.substr(0, pos);

					if(pos<name.length())
						name=name.substr(pos+1);
					else
						name="";

					if(n!="")
						parenttype::pictures.insert(pair<string, gge::animation::RectangularGraphic2DSequenceProvider&>(n, dynamic_cast<gge::animation::RectangularGraphic2DSequenceProvider&>(*it)));
				} while(name!="");
			}
			catch(...) { }//don't insert if it is not an animation
		}

		for(auto it=Get<Folder>(++item).Subitems.First();it.IsValid();it.Next()) {
			try {
				if(it->GetName()=="") {
					parenttype::sounds.insert(pair<string, Sound&>(it->GetName(), dynamic_cast<Sound&>(*it)));
					continue;
				}

				string name=it->GetName();
				string::size_type pos;
				do {
					pos=name.find_first_of(',');

					if(pos==string::npos)
						pos=name.length();

					string n=name.substr(0, pos);

					if(pos<name.length())
						name=name.substr(pos+1);
					else
						name="";

					if(n!="")
						parenttype::sounds.insert(pair<string, Sound&>(n, dynamic_cast<Sound&>(*it)));
				} while(name!="");
			}
			catch(...) { }//don't insert if it is not a sound
		}
			
		for(int i=15;i<18+15;i++)
			Subitems.Delete(15);

		icons=&Get<Folder>(15);
		pictures=&Get<Folder>(16);
		sounds=&Get<Folder>(17);

	}

	void WidgetRegistry::AddIcon(const std::string &key, gge::animation::RectangularGraphic2DSequenceProvider &image) {
		gorgon::Base &img=dynamic_cast<gorgon::Base&>(image);
		icons->Add(img);
		img.SetName(key);
		gge::widgets::WidgetRegistry::icons.insert(std::pair<std::string, gge::animation::RectangularGraphic2DSequenceProvider &>(key, image));
	}

	void WidgetRegistry::AddPicture(const std::string &key, gge::animation::RectangularGraphic2DSequenceProvider &image) {
		gorgon::Base &img=dynamic_cast<gorgon::Base&>(image);
		pictures->Add(img);
		img.SetName(key);
		gge::widgets::WidgetRegistry::pictures.insert(std::pair<std::string, gge::animation::RectangularGraphic2DSequenceProvider &>(key, image));
	}

	void WidgetRegistry::AddSound(const std::string &key, Sound &snd) {
		throw std::runtime_error("Is not implemented");
	}

	WidgetRegistry *LoadWidgetRegistry(Base *parent, File &file, std::istream &in, int Size) {
		WidgetRegistry &wr=*new WidgetRegistry;
		if(parent)
			wr.SetParent(*parent);
		else
			wr.RemoveParent();

		std::vector<namecaption> names;

		LoadEntries(in, Size, file, wr, [&](GID::Type &gid, int size) -> bool {
			return false;
		}, names);

		return &wr;
	}

} }
