#include <vcl.h>
#include <windows.h>
#include <memory>
#include <vector>
using namespace std;
#include "IconsStuff.h"
#pragma hdrstop
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
	return 1;
}
//---------------------------------------------------------------------------
IconsStuff::IconsStuff(){hShell32 = NULL;}
bool IconsStuff::GetSystemImageList()
{
	typedef BOOL (WINAPI * SHGIL_PROC) (HIMAGELIST *hLarge, HIMAGELIST *hSmall);
	typedef BOOL (WINAPI * FII_PROC) (BOOL fFullInit);
	// Don't free until we terminate - otherwise, the image-lists will be destroyed
	if(hShell32 == NULL)
  	hShell32 = LoadLibrary("shell32.dll");
	
	SHGIL_PROC Shell_GetImageLists;
	FII_PROC FileIconInit;
	if(&hLarge == 0 || &hSmall == 0 || hShell32 == 0)
		return false;
  
	// Get Undocumented APIs from Shell32.dll:
	// Shell_GetImageLists and FileIconInit
	//
	Shell_GetImageLists = (SHGIL_PROC) GetProcAddress(hShell32, (LPCSTR)71);
	FileIconInit = (FII_PROC) GetProcAddress(hShell32, (LPCSTR)660);
	if(Shell_GetImageLists == 0)
	{
		FreeLibrary(hShell32);
		return false;
	}

	// Initialize imagelist for this process - function not present on win95/98
	if(FileIconInit != 0)
		FileIconInit(true);

	// Get handles to the large+small system image lists!
		Shell_GetImageLists(&hLarge, &hSmall);

	//if(!ImageList_GetIcon(hSmall, 0, NULL))
		//return false;
	return true;
}

void IconsStuff::AddIcons(TImageList* ImageList)
{
	int size = ImageList_GetImageCount(hSmall);
	HICON SystemImageListIconHandle;
	Graphics::TIcon *myIcon = new Graphics::TIcon();
	
	for(int i=0; i<size; i++)
	{
		SystemImageListIconHandle = ImageList_GetIcon(hSmall,i,ILD_TRANSPARENT);
		if(SystemImageListIconHandle)
		{
			myIcon->Handle = SystemImageListIconHandle;
			ImageList->AddIcon(myIcon);
		}
		else
		{
			ImageList->AddIcon(NULL);
			transparent.push_back(i);
		}
	}
}

void IconsStuff::EraseFromTransparent(int offset)
{
	this->transparent.erase(transparent.begin()+offset);
}
