void ObjectBarDialog::OnClickObject(NMHDR *pNMHDR, LRESULT *pResult)
{
	POSITION Pos = objects.GetFirstSelectedItemPosition();
	int Item = objects.GetNextSelectedItem(Pos);

	if (!layout) return;

	if (layout->m_ObjectFrameIsLockedTo != 0) // InputLocked()
		return;

	// Select in layout editor, if it's open
	if (parent.m_tabs.SelectionGet() == 0 && parent.m_tabs.ItemGetCount() == 2)
	{
		parent.layout_editor[0][0]->m_sel.RemoveAll();

		// This is intentionally here; clear selection if clicked on whitespace
		if(Item == -1)
		{
			parent.layout_editor[0][0]->Invalidate();
			g_PropertyBar->Update(parent.layout_editor[0][0], TYPE_LAYOUT, NULL, layout, NULL, application); 

			return;
		}

		// Now we have to wangle in the selected object
		int ObjectIdentifier = objects.GetItemData(Item);

		if (ObjectIdentifier==-1)
			return; //folder

		CObj*			pObject = 0;
		CObjType*		pObjectType = 0;

		POSITION LayerPos = layout->layers.GetHeadPosition();

		// So we have to find all the CObj's with this CObjType in the layout, and add them
		// For each layer
		while(LayerPos)
		{
			CLayer* pLayer = layout->layers.GetNext(LayerPos);

			// Loop all objects
			CObjList Objects;
			pLayer->GetEveryObject(Objects, layout);

			POSITION ObjectPos = Objects.GetHeadPosition();

			for (int i = 0; i < Objects.GetCount(); i++) 
			{
				CObj* pTestObject;
				long ID = Objects.GetNext(ObjectPos);
				layout->objects.Lookup(ID, pTestObject);

				if (pTestObject->GetGlobalID() != -1) 
				{
					CObjType* pTestType = pTestObject->GetObjectType(application);

					if (pTestType->ObjectIdentifier == ObjectIdentifier)
					{
						pObjectType = pTestType;
						pObject = pTestObject;

						long nKey = pObject->GetInstanceID();
						parent.layout_editor[0][0]->m_sel.AddTail(nKey);
					}
				}
			}
		}

		g_PropertyBar->Update(parent.layout_editor[0][0], TYPE_OBJECT, &parent.layout_editor[0][0]->m_sel, layout, &layout->objects, application); // Show object properties

		parent.layout_editor[0][0]->Invalidate();

		// While we're here, show animations for object
		// Future note: .. to be continued

		if(!pObjectType)
			return;

		int iRoot = -1;
		OINFO* oInfo = GetOINFO(pObjectType->DLLIndex);
		if (oInfo->ETGetAnimationHandle)
		{
			oInfo->ETGetAnimationHandle(pObject->editObject, iRoot);
			pMainWnd->animator.UpdateAnimations(application, layout, pObjectType, iRoot);
		}
	}
}
void ObjectBarDialog::OnDblClickObject(NMHDR *pNMHDR, LRESULT *pResult)
{
	POSITION Pos = objects.GetFirstSelectedItemPosition();
	int Item = objects.GetNextSelectedItem(Pos);

	if (Item == -1) return;

	CObjType* pType;
	long ID = objects.GetItemData(Item);

	if (ID == -1)  // object folder
	{
		if (folderfilter == -1)  //Default folder
		{
			for(int i=0; i < application->object_folders.size(); i++)
			{
				if (objects.GetItemText(Item,0) == application->object_folders[i].name)
				{
					folderfilter=i;
					break;
				}
			}
		}
		else //return folder
		{
			folderfilter = -1;
		}
		Refresh();
		return;
	}

	application->object_types.Lookup(ID, pType);

	if (!pType) return;

	// If layout editor open..
	if (parent.m_tabs.SelectionGet() == 0 && parent.m_tabs.ItemGetCount() == 2)
	{
		POSITION ObjectPos = layout->objects.GetStartPosition();
		CObj* pObject;
		long ID = 0;

		for (int i = 0; i < layout->objects.GetCount(); i++) 
		{
			layout->objects.GetNextAssoc(ObjectPos, ID, pObject);

			CObjType* type = pObject->GetObjectType(application);

			if (type->ObjectIdentifier == pType->ObjectIdentifier)
			{
				OINFO* info = GetOINFO(pType->DLLIndex);
				if(info->ETOnNotify)
					info->ETOnNotify(pObject->editObject, 1);

				return;
			}
		}
	}

	// If event sheet editor, see if it has conditions. If so, open Event Wizard
	if ((parent.m_tabs.SelectionGet() == 1) && (parent.m_tabs.ItemGetCount() == 2) || parent.m_tabs.ItemGetCount() == 1)
	{
		bool bUse = false;

		for (int i = 0; i < pType->GetTableCount(CONDITION); i++)
		{
			ACESEntry2* pAce = pType->GetACESEntry(CONDITION, i);
			if(pAce == NULL || pAce->aceListName == "")
				continue; // Ignore null entries

			bUse = true;
		}

		if (bUse)
		{
			parent.m_pEventView[0][0]->m_InitialStage = 2;
			parent.m_pEventView[0][0]->m_InitialSelect = ID;
			parent.m_pEventView[0][0]->m_OldName = pType->GetName();
			parent.m_pEventView[0][0]->AddCondition(true);
		}
	}
}
void ObjectBarDialog::Refresh(bool layer_changed)
{
	// check for unnecessary refresh
	if (layer_changed && !show_only_selected_layer)
		return;

	// clear all lists
	objects.DeleteAllItems();

	for (int i = 0; i < large_images.GetImageCount(); i++)
		large_images.Remove(0);

	for (int i = 0; i < small_images.GetImageCount(); i++)
		small_images.Remove(0);

	if(folderfilter > -1 && folderfilter >= application->object_folders.size())
		folderfilter = -1;

	CObj*			pObject;
	CObjType*		pObjectType;
	CStringArray	List; // Object list

	//object folders
	if (folderfilter == -1)
	{
		HBITMAP large_image = (HBITMAP) LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(20150), IMAGE_BITMAP, 32, 32, LR_LOADTRANSPARENT);
		HBITMAP small_image = (HBITMAP) LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(20150), IMAGE_BITMAP, 16, 16, LR_LOADTRANSPARENT);
		
		int ImageID = ImageList_Add(large_images.m_hImageList, large_image, NULL);
		ImageList_Add(small_images.m_hImageList, small_image, NULL);
		
		DeleteObject(large_image);
		DeleteObject(small_image);

		for (int i=0; i<application->object_folders.size(); ++i)
		{
			if(application->object_folders[i].name == "Default")
				continue;
			int item = objects.InsertItem(objects.GetItemCount(), application->object_folders[i].name, ImageID);
			objects.SetItemData(item, (DWORD_PTR)(const char*)"-1");
		}
	} // -1 is Default, -2 is disabled
	else if(folderfilter != -2 && application->object_folders[folderfilter].name != "Default")
	{
		HBITMAP large_image = (HBITMAP) LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(20150), IMAGE_BITMAP, 32, 32, LR_LOADTRANSPARENT);
		HBITMAP small_image = (HBITMAP) LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(20150), IMAGE_BITMAP, 16, 16, LR_LOADTRANSPARENT);
		
		int ImageID = ImageList_Add(large_images.m_hImageList, large_image, NULL);
		ImageList_Add(small_images.m_hImageList, small_image, NULL);
		
		DeleteObject(large_image);
		DeleteObject(small_image);

		int item = objects.InsertItem(0,"...\\"+application->object_folders[folderfilter].name, ImageID);
		objects.SetItemData(item, (DWORD_PTR)(const char*)"-1");
	}

	if (layout)
	{
		POSITION LayerPos = layout->layers.GetHeadPosition();

		// For each layer
		while(LayerPos)
		{
			CLayer* pLayer = layout->layers.GetNext(LayerPos);

			if (show_only_selected_layer && pLayer != layout->current_layer)
				continue;

			if (!show_nonlayout_objects && pLayer->m_layerType == LAYER_NONFRAME)
				continue;

			// Loop all objects
			CObjList Objects;
			pLayer->GetEveryObject(Objects, layout);

			POSITION ObjectPos = Objects.GetHeadPosition();

			for (int i = 0; i < Objects.GetCount(); i++) 
			{
				long ID = Objects.GetNext(ObjectPos);
				layout->objects.Lookup(ID, pObject);

				if (pObject->GetGlobalID() != -1) 
				{
					pObjectType = pObject->GetObjectType(application);
					
					// Failed/invalid object type, for some reason
					if(!pObjectType)
						continue;

					//folder filtering
					if(folderfilter ==-1 && pObjectType->GetFolder() != "Default")
						continue;
					else if(folderfilter > -1 && pObjectType->GetFolder()!=application->object_folders[folderfilter].name)
						continue;

					bool bAdd = true;

					for (int j = 0; j < List.GetSize(); j++)
						if (List.GetAt(j) == pObjectType->GetName())
							bAdd = false;

					if (bAdd)
					{
						HBITMAP large_image = pObjectType->m_Image.MakeBitmap();
						HBITMAP small_image = pObjectType->small_image.MakeBitmap();

						int ImageID = ImageList_Add(large_images.m_hImageList, large_image, NULL);
						ImageList_Add(small_images.m_hImageList, small_image, NULL);

						DeleteObject(large_image);
						DeleteObject(small_image);

						int Item = objects.InsertItem(objects.GetItemCount(), pObjectType->GetName(), ImageID);

						objects.SetItemData(Item, (LPARAM)((const char*)pObjectType->GetName()));

						List.Add(pObjectType->GetName());
					}
				} // Iterate each object
			} // Iterate each layer
		}
	}

	else
	{
		POSITION Pos = application->object_types.GetStartPosition();
		CObjType* oT;
		long nKey = 0;

		while (Pos != NULL) 
		{	
			application->object_types.GetNextAssoc(Pos, nKey, oT);
			if(oT && !oT->m_bIsGroupType)
			{
				int ImageID = ImageList_Add(large_images.m_hImageList, oT->m_Image.MakeBitmap(), NULL);
				int Item = objects.InsertItem(objects.GetItemCount(), oT->GetName(), ImageID);

				objects.SetItemData(Item, (DWORD)((LPCSTR)oT->GetName()));

				List.Add(oT->GetName());
			}
		}
	}

	objects.ShowScrollBar(SB_VERT); 

	// Work out if there's a vertical scrollbar. If there is, we'll resize a bit.
	int iListCount = objects.GetItemCount();
	int iListSize = objects.GetCountPerPage();

	if(iListCount < iListSize)
	{
		// Disable the arrows on the vertical scrollbar
		objects.EnableScrollBar(SB_VERT, ESB_DISABLE_BOTH);
		objects.SetScrollRange(SB_VERT, 0, 2);
	}

	else
	{
		// Enable the vertical scrollbar
		objects.EnableScrollBar(SB_VERT, ESB_ENABLE_BOTH);
	}

	if (sorting == ob_sort_az)
		objects.SortItems(ObjectCompare, (LPARAM)&objects);
	if (sorting == ob_sort_za)
		objects.SortItems(ObjectCompareZA, (LPARAM)&objects);

	// now replace the ids
	for (int i = 0; i < objects.GetItemCount(); i++)
	{
		if((const char*)objects.GetItemData(i) == "-1")
		{
			objects.SetItemData(i, -1);
			continue;
		}
		CObjType* type = GetTypeFromName(application, objects.GetItemText(i, 0));
		objects.SetItemData(i, type->ObjectIdentifier);
	}
}