//imageList 图像索引列表
/************************************
Author		: lujjjh
Date		: 2010.9
************************************/
import com.picture;

namespace win {
	imageList = class {
		ctor (v1 = 0, v2 = -1) {
			if (v2 == -1) {
				this.handle = v1;
				var re, cx, cy = ::ImageList_GetIconSize(this.handle, 0, 0);
				this.width = cx;
				this.height = cy;
			}
			else {
				this.handle = ::ImageList_Create(v1, v2, 0x00000020 | 0x1/*_ILC_MASK*/, 0, 1);
				this.width = v1;
				this.height = v2;
			}
		}
		
		add = function (pic, color=0xff00ff) { 
			var iPic = ..com.picture.load(pic);
			if(!iPic)
				error("加载图片失败",2)
			var hbmp = iPic.handle; 
			::ImageList_AddMasked(this.handle, topointer(hbmp), color);
			::DeleteObject(topointer(hbmp));
			..com.Release(iPic)
			return this;
		}
		
		addIcon = function (iconData) {
			var hIcon = this.hIconFromStr(iconData);
			::ImageList_ReplaceIcon(this.handle, -1, hIcon);
			return this;
		}
		
		hIconFromStr = function (iconData) {
			if (#iconData <= 0x104/*_MAX_PATH*/) {
				iconData = ..string.load( iconData );
			}
			
			var nNum = ..raw.convert({ byte b[2] = ..string.sub(iconData, 5, 6) }, { word n }).n;
			for (i = 0; nNum - 1) {
				var entry = IconDirEntry();
				..raw.convert({ byte b[16] = ..string.sub(iconData, 7 + i * 16, 7 + i * 16 + 16) }, entry);
				if (entry.bWidth == this.width) {
					return CreateIconFromResource(
						..string.sub(iconData, entry.dwImageOffset + 1, entry.dwImageOffset + 1 + entry.dwBytesInRes),
						entry.dwBytesInRes,
						1,
						196608
					);
				}
				..raw.convert({ byte b[16] = ..string.sub(iconData, 7, 23) }, entry);
				return CreateIconFromResource(
					..string.sub(iconData, entry.dwImageOffset + 1, entry.dwImageOffset + 1 + entry.dwBytesInRes),
					entry.dwBytesInRes,
					1,
					196608
				);
			}
		}
		@meta
	}
	imagelist = imageList;
}

namespace win.imageList {
	meta = {
		_topointer = function(){ 
			return owner.handle;
		} 
	}
	
	/*intellisense(::)*/
	::Comctl32 := ..raw.loadDll("Comctl32.dll");
	::ImageList_Create := ::Comctl32.api("ImageList_Create", "pointer(int cx, int cy, INT flags, int cInitial, int cGrow)");
	::ImageList_AddMasked = ::Comctl32.api("ImageList_AddMasked", "int(pointer himl, pointer hbmImage, int crMask)");
	::ImageList_GetIconSize = ::Comctl32.api("ImageList_GetIconSize","int(pointer himl, int &cx, int &cy)");
	::ImageList_ReplaceIcon = ::Comctl32.api("ImageList_ReplaceIcon","int(pointer himl, int i, pointer hicon)");
	/*end intellisense*/
	
	CreateIconFromResource = ::User32.api("CreateIconFromResource","pointer(string presbits, INT dwResSize, int fIcon, INT dwVer)");
	
	IconDirEntry = class {
		byte bWidth;
		byte bHeight;
		byte bColorCount;
		byte bReserved;
		word wPlanes;
		word wBitCount;
		int dwBytesInRes;
		int dwImageOffset;
	}
} 

/**intellisense(win)
imageList = 图像列表\n用于工具条、树形控件等
imageList(__/*句柄*/)  = 通过句柄创建一个 imageList
imageList(.(宽度,高度)  = 创建一个 imageList
end intellisense**/

/**intellisense() 
!imagelist.add("__/*图片路径*/",0xFF00FF/*透明色*/) = 向 imageList中添加图片\n该函数返回当前win.imageList对象
?win.imageList = !imageList.
win.imageList() = !imageList.
end intellisense**/

