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

namespace win.ui {
	import util.metaProperty;   
	toolbar = class {
		ctor (winform) {
			if( ! winform[["hwnd"]] ){
				if(type(winform)==type.number){
					winform = {hwnd=winform;_mapCommandProc={}}
				}
				else {
					error("第一个构造参数必须指定有效窗口",3)
				} 
			} 
			this.parent = winform;
			this.hParent = winform.hwnd;
			
			this.flat = true;
			this.label = true;
			this.buttons = {};
		};
		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.parent.hwnd);
			::SendMessageInt(this.hwnd, 0x424/*_TB_SETTOOLTIPS*/, this.tooltip.hwnd, 0);
		 
			this.showLabel = label;
			return this.hwnd;
		}; 
		adjust = function(){
			::SendMessageInt(this.hwnd, 0x421/*_TB_AUTOSIZE*/, 0, 0);
		}; 
		add = function (text, proc, iBitmap = 0,id) { 
    		if(!id)
    			id = this.parent._getFreeCommandId();
    		
    		if(proc){
    			if( type(proc)!=type.function) 
    				error("参数二必须是一个回调函数",2	);
    				
    			this.parent._mapCommandProc[id] = proc; 
    		}
    		
			var tbb = TBBUTTON(this);
			tbb.iBitmap = iBitmap - 1;
			tbb.idCommand = id;
			tbb.fsState = 0x4/*_TBSTATE_ENABLED*/;
			tbb.fsStyle = text == null ? 0x1/*_TBSTYLE_SEP*/ : 0x0/*_TBSTYLE_BUTTON*/;
			tbb.iString = text or "";
			..table.push(this.buttons,tbb );
			
			::SendMessageByStruct(this.hwnd, 0x414, 1, tbb);  
			::SendMessageInt(this.hwnd, 0x421/*_TB_AUTOSIZE*/, 0, 0);  
			return tbb;
		} 
		@_metaProperty;
	}
	
	namespace 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;
				}
			};
			style = {
				_get = function () {
					return ::SendMessageInt(owner.parent.hwnd, 0x439/*_TB_GETSTYLE*/, owner.idCommand, 0);
				}
				_set = function (v) {
					return ::SendMessageInt(owner.parent.hwnd, 0x438/*_TB_SETSTYLE*/, owner.idCommand, ::MAKELONG(v, 0));
				}
			}; 
			modifyStyle = function( remove=0,add=0 ){
				var style = ( owner.style & ~remove ) | add ; 
				owner.style = style;
				return style;
			};
			imageList = {
				_get = function () {
					return ::SendMessageInt(owner.hwnd, 0x400/*_WM_USER*/+49/*_TB_GETIMAGELIST*/, 0, 0);
				}
				_set = function (v) { 
					return ::SendMessage(owner.hwnd, 0x400/*_WM_USER*/+48/*_TB_SETIMAGELIST*/, , v);
				}
			}
		) 
		
		class TBBUTTON{
			ctor (parent) {
				this.parent = parent; 
			}
			int iBitmap;
			int idCommand;
			BYTE fsState;
			BYTE fsStyle;
			INT dwData;
			string iString; 
			@_metaProperty;
		}
		
		namespace TBBUTTON{ 
			class INFO {
    			INT cbSize = 32;
    			INT mask = 0;
    			int idCommand;
    			int iImage;
    			BYTE fsState;
    			BYTE fsStyle;
    			WORD cx;
    			INT lParam;
    			pointer text;
    			int cchText;
			};
			INFO_AUTOSET_MASK = function(info){ 
				if( info.text !== null ){ 
					info.cchText = #info.text+1; 
				}
				if( info.cchText ){
					info.mask |= 2/*_TBIF_TEXT*/  
				}
				if( info.fsState ){
					info.mask |= 4/*_TBIF_STATE*/ 
				}
				if( info.idCommand ){
					info.mask |= 0x20/*_TBIF_COMMAND*/ 
				}
				if( info.iImage ){
					info.mask |= 1/*_TBIF_IMAGE*/
				}
				if( info.cx ){
					info.mask |= 0x40/*_TBIF_SIZE*/
				}
				if( info.fsStyle ){
					info.mask |= 8/*_TBIF_STYLE*/
				}
			};
			
			_metaProperty = ..util.metaProperty(
				getInfo = function(){
					var info = INFO();
					if( ::SendMessageByStruct(owner.parent.hwnd,0x441/*_TB_GETBUTTONINFO*/,owner.idCommand,info) )
						return info;
				}
				setInfo = function(info){ 
					if(!info._struct)info = ..table.mixin(INFO(),info);
					INFO_AUTOSET_MASK(info);
					return !! ::SendMessageByStruct(owner.parent.hwnd,0x442/*_TB_SETBUTTONINFO*/,owner.idCommand,info);
				} 
				id = {
					_get = function () {
						return owner.idCommand;
					}
					_set = function (v) {
						if(  owner.setInfo( idCommand = v ) ){ 
							owner.idCommand = v;
						}
					}
				}; 
				text = {
					_get = function () {
						return owner.iString;
					}
					_set = function (v) {
						if( owner.setInfo( text = v ) ){
							owner.iString = v;
						}
					}
				};
				getRect = function(){
					var rc = ::RECT()
					if( ::SendMessageByStruct(owner.parent.hwnd,0x433/*_TB_GETRECT*/,owner.idCommand,rc) )
						return rc;
				};
				state = {
					_get = function () {
						return ::SendMessageInt(owner.parent.hwnd, 0x412/*_TB_GETSTATE*/, owner.idCommand, 0);
					}
					_set = function (v) {
						return ::SendMessageInt(owner.parent.hwnd, 0x411/*_TB_SETSTATE*/, owner.idCommand, ::MAKELONG(v, 0));
					}
				}; 
				disabled = {
					_get = function () {
						return !( owner.state & 0x4);
					}
					_set = function (v) {
						if (v)
							owner.state -= 0x4;
						else
							owner.state |= 0x4;
					}
				}
				enabled = {
					_get = function () {
						return !owner.disabled;
					}
					_set = function (v) {
						owner.disabled = !v;
					}
				}
				checked = {
					_get = function () {
						return owner.state & 0x1;
					}
					_set = function (v) {
						if (v)
							owner.state |= 0x1;
						else
							if (owner.checked)
								owner.state -= 0x1;
					}
				}
			);
		}
	};
	
	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;
	}
}
  
/**intellisense()
win.ui.toolbar(__/*父窗口*/) = 创建一个 Toolbar 控件 
!toolbar.create() = 创建工具条
!toolbar.add(标题,回调函数,图像索引,可选指定命令ID) = @.add(\n	"文字", \n	function (id) {\n\n	},1\n)
!toolbar.add() = 添加一个分隔条\n!toolbar_button.
!toolbar.style = 工具栏样式,\n 使用 _TBSTYLE_ 前缀常量表示样式
!toolbar.modifyStyle(.(移除样式,添加样式) = 所有参数可选,默认为0\n使用 _TBSTYLE_ 前缀常量表示样式
!toolbar.showLabel = 是否在按钮上显示文字
!toolbar.imageList = 指定 Toolbar 的 imageList 
!toolbar.buttons[1__/*索引*/] = 返回指定的按钮对象\nadd()函数返回按钮id
!toolbar.buttons[] = !toolbar_button.
?win.ui.toolbar = !toolbar.
win.ui.toolbar() = !toolbar.
!toolbar_button.id = 命令ID
!toolbar_button.state = 读写按钮状态
!toolbar_button.disabled = 是否禁用
!toolbar_button.checked = 是否选中 
!toolbar_button.text = 按钮文本
!toolbar_button.getRect() = 返回按钮区块\n!rect.
end intellisense**/
 
 /**intellisense()

_TB_SETSTYLE=@0x438/*_TB_SETSTYLE*/
_TB_GETSTYLE=@0x439/*_TB_GETSTYLE*/
_TB_GETBUTTONSIZE=@0x43A/*_TB_GETBUTTONSIZE*/
_TB_SETBUTTONWIDTH=@0x43B/*_TB_SETBUTTONWIDTH*/
_TB_SETDISABLEDIMAGELIST=@0x436/*_TB_SETDISABLEDIMAGELIST*/
_TB_GETDISABLEDIMAGELIST=@0x437/*_TB_GETDISABLEDIMAGELIST*/
_TB_GETRECT=@0x433/*_TB_GETRECT*/
_TB_GETTOOLTIPS=@0x423/*_TB_GETTOOLTIPS*/
_TB_SETTOOLTIPS=@0x424/*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=@0x8000/*_TBSTYLE_TRANSPARENT*/ 
end intellisense**/