/************************************
Author		: lujjjh
Date		: 2010.9
************************************/
import win.ui.ctrl.common;
win.ui.ctrl.common.initEx(  0x4/*_ICC_BAR_CLASSES*/ ) //初始化工具条支持

namespace win.ui {
	import util.metaProperty;
	import win.subclass;
	
  
	
	tbButton = class {
		ctor (hToolbar, id) {
			this.hToolbar = hToolbar;
			this.id = id;
		}
		int hToolbar;
		int id;
		
		@metaProperty;
	}
	tbButton.metaProperty = ..util.metaProperty(
		state = {
			_get = function () {
				return ::SendMessageInt(owner.hToolbar, 0x412/*_TB_GETSTATE*/, owner.id, 0);
			}
			_set = function (v) {
				return ::SendMessageInt(owner.hToolbar, 0x411/*_TB_SETSTATE*/, owner.id, ::MAKELONG(v, 0));
			}
		};
		enabled = {
			_get = function () {
				return owner.state & 0x4;
			}
			_set = function (v) {
				if (v)
					owner.state |= 0x4;
				else
					if (owner.enabled)
						owner.state -= 0x4;
			}
		}
		checked = {
			_get = function () {
				return owner.state & 0x1;
			}
			_set = function (v) {
				if (v)
					owner.state |= 0x1;
				else
					if (owner.checked)
						owner.state -= 0x1;
			}
		}
	);
	
	toolbar = class {
		ctor (form) {
			if (type(form) == type.table && form.hwnd)
				this.hParent = form.hwnd;
			this.flat = true;
			this.label = true;
		}
		create = function () { 
			this.hwnd = ::CreateWindowEx(0, 
										 "ToolbarWindow32", , 
										 0x40000000/*_WS_CHILD*/ | 0x10000000/*_WS_VISIBLE*/ | (this.flat ? 0x800/*_TBSTYLE_FLAT*/ | 0x100/*_TBSTYLE_TOOLTIPS*/: 0),
										 0, 0, 0, 0, 
										 this.hParent, 0 );
			::SendMessageInt(this.hwnd, 0x41e/*_TB_BUTTONSTRUCTSIZE*/, 20, 0);
			
			this.tooltip = ..win.ui.tooltip();
			this.tooltip.create(this.hParent);
			::SendMessageInt(this.hwnd, 0x424/*_TB_SETTOOLTIPS*/, this.tooltip.hwnd, 0);
			this.subclass = ..win.subclass(
				this.hParent, 
				function (hwnd, message, wparam, lparam) {
					select (message) {
						case 0x5/*_WM_SIZE*/ {
							::SendMessageInt(this.hwnd, 0x421/*_TB_AUTOSIZE*/, 0, 0);
						}
						case 0x111/*_WM_COMMAND*/ {
							var id = ::LOWORD(wparam)
							if( id >  _minid )
								tbProcs[id-_minid]();
						}
					}
				}
			);
			
			this.showLabel = label;
			return this.hwnd;
		}
		add = function (text = null, proc, iBitmap = 0) {
			if (!proc)
				proc = function (){};
			var id = ..table.push(tbProcs, proc) ;
			var tbb = TBBUTTON();
			tbb.iBitmap = iBitmap - 1;
			tbb.idCommand = id + _minid;
			tbb.fsState = 0x4/*_TBSTATE_ENABLED*/;
			tbb.fsStyle = text == null ? 0x1/*_TBSTYLE_SEP*/ : 0x0/*_TBSTYLE_BUTTON*/;
			tbb.iString = text or "";
			::SendMessageByStruct(this.hwnd, 0x414, 1, tbb);
			this.buttons[id ] = ..win.ui.tbButton(this.hwnd, id);
			::SendMessageInt(this.hwnd, 0x421/*_TB_AUTOSIZE*/, 0, 0);
		}
		int hParent;
		bool flat;
		bool label;
		int hwnd;
		int himl;
		int hbmp;
		struct tooltip;
		table buttons = {};
		struct subclass;
		
		@metaProperty;
	}
	toolbar.metaProperty = ..util.metaProperty(
		showLabel = {
			_get = function () {
				return owner.label;
			}
			_set = function (v) {
				if (owner.hwnd)
					::SendMessageInt(owner.hwnd, 0x400/*_WM_USER*/+60/*_TB_SETMAXTEXTROWS*/, v ? 1 : 0, 0);
				owner.label = v ? true : false;
			}
		}
		imageList = {
			_get = function () {
				return ::SendMessageInt(owner.hwnd, 0x400/*_WM_USER*/+49/*_TB_GETIMAGELIST*/, 0, 0);
			}
			_set = function (v) {
				var handle = v;
				if (type(v) == type.table) {
					handle = v.handle;
				}
				return ::SendMessageInt(owner.hwnd, 0x400/*_WM_USER*/+48/*_TB_SETIMAGELIST*/, 0, handle);
			}
		}
	)
	
	tooltip = class {
		create = function (hParent) {
			this.hwnd = ::CreateWindowEx(0, "tooltips_class32", , 
										 0x80000000/*_WS_POPUP*/ | 0x1 /*_TTS_ALWAYSTIP*/,
										 0, 0, 0, 0,
										 hParent, 0 );
		}
		int hwnd;
	}
}

namespace win.ui.toolbar {
	_minid = 5000 + 5000//5000是菜单的最小ID
	TBBUTTON = class {
		int iBitmap;
		int idCommand;
		BYTE fsState;
		BYTE fsStyle;
		INT dwData;
		string iString;
	}
	tbProcs = {}; 
	
	/**intellisense()
	_TOOLBARCLASSNAME = @"ToolbarWindow32";
	_ICC_BAR_CLASSES=@0x4/*_ICC_BAR_CLASSES*/
	_TBSTYLE_TOOLTIPS=@0x100/*_TBSTYLE_TOOLTIPS*/
	_TBSTYLE_BUTTON=@0x0/*_TBSTYLE_BUTTON*/
	_TBSTYLE_TOOLTIPS=@0x100/*_TBSTYLE_TOOLTIPS*/
	_TBSTYLE_SEP=@0x1/*_TBSTYLE_SEP*/
	_TBSTATE_ENABLED=@0x4/*_TBSTATE_ENABLED*/
	_TB_ADDBUTTONS=@0x414/*_TB_ADDBUTTONS*/
	_TB_BUTTONSTRUCTSIZE=@0x41e/*_TB_BUTTONSTRUCTSIZE*/
	_TBSTYLE_FLAT=@0x800/*_TBSTYLE_FLAT*/
	_ILC_COLOR8=@0x8/*_ILC_COLOR8*/
	_ILC_MASK=@0x1/*_ILC_MASK*/
	_TB_SETIMAGELIST=@0x400/*_WM_USER*/+48/*_TB_SETIMAGELIST*/
	_TB_GETIMAGELIST=@0x400/*_WM_USER*/+49/*_TB_GETIMAGELIST*/
	_TB_AUTOSIZE=@0x421/*_TB_AUTOSIZE*/
	_TB_SETTOOLTIPS=@0x424/*_TB_SETTOOLTIPS*/
	_TB_SETMAXTEXTROWS=@0x400/*_WM_USER*/+60/*_TB_SETMAXTEXTROWS*/
	_TB_GETSTATE=@0x412/*_TB_GETSTATE*/
	_TB_SETSTATE=@0x411/*_TB_SETSTATE*/
	end intellisense**/
}

namespace win.ui.tooltip {
	/*intellisense()
	_TOOLTIPS_CLASS=@"tooltips_class32";
	end intellisense*/
}

/**intellisense()
win.ui.toolbar(__/*父窗口*/) = 创建一个 Toolbar 控件
win.ui.toolbar._minid = 工具条按钮的最小命令ID
!tb.create() = 创建工具条
!tb.add(__/*文字*/, /*单击函数*/, -1/*图片*/) = @.add("文字", function () {

})
!tb.add() = 添加一个分隔条
!tb.showLabel = 是否在按钮上显示文字
!tb.imageList = 指定 Toolbar 的 imagelist
!tb.buttons = 返回所有按钮组成的 table
?win.ui.toolbar = !tb.
end intellisense**/
 

/**intellisense(tb.buttons)
enabled = 是否可用，false 为禁止
checked = 是否选中
end intellisense**/