//控件元属性
import util.metaProperty;
namespace win.ui.ctrl;

/**intellisense(win.ui.ctrl)
metaProperty = 属性元表
metaProperty(__) = @.metaProperty(\n\n	属性 = {\n		_get = function(){ \n			/*读取属性代码写在这里*/\n			return null;\n		}\n		_set = function( v ){\n			/*写入属性代码写在这里*/\n			..io.print( owner,v)\n		} 	\n	};\n)
end intellisense**/

class metaProperty{
	ctor( meta ){
		this = ..util.metaProperty(meta);  
	} 
	@{ _get = common  }//公用属性
}
 
SetCapture			= ::User32.api("SetCapture", "void(int hwnd)");
ReleaseCapture		= User32.api("ReleaseCapture", "void()");

metaProperty.common = {
	
	redraw = function( flag = 0x105 /*_RDW_INVALIDATE | _RDW_UPDATENOW  | _RDW_ERASE*/ ) { 
		::RedrawWindow(owner[["hwnd"]],{},null,flag);
 	};
 	invalidate = function(rc,erase=1){ 
 		rc := {} 
 		var hwnd = rc.hwnd; 
 		if(hwnd)
 		    rc = ..win.getRect(hwnd);
 		
 		::InvalidateRect(owner.hwnd,rc,erase?1:0); 
 	}
 	update = function(){
 		::UpdateWindow(owner[["hwnd"]])
 	} 
 	capture = { 
		_set = function( v ){ 
			if(v)
				SetCapture(owner[["hwnd"]])
			else 
				ReleaseCapture();
			
		} 	
	}; 	 
	text = {
		_get = function(){ 
			return ..win.getText( owner[["hwnd"]] );
		}
		_set = function(v){ 
			..win.setText( owner[["hwnd"]],tostring(v) ); 
			if(owner[["transparent"]]){
				owner[["parent"]].invalidate(owner,1);  
				owner[["parent"]].update();
			}
		}
	
	}
	hide = {
		_get = function(){ 
			return ! ..win.getStyle(owner[["hwnd"]],0x10000000/*_WS_VISIBLE*/ ) 
		}
		_set = function(v){ 
			owner.show(!v ? true:0x0/*_SW_HIDE*/)
		}
	
	};
	close = function(){
		::SendMessage(owner[["hwnd"]] ,0x10/*_WM_CLOSE*/ );  
	}
	modifyStyle = function(remove=0,add=0,flags){
			return ..win.modifyStyle(owner[["hwnd"]],remove,add,flags);
	} 
	modifyStyleEx = function(remove=0,add=0,flags){
			return ..win.modifyStyleEx(owner[["hwnd"]],remove,add,flags);
	} 
	show = function(flag=true) {    
		..win.show(owner[["hwnd"]],flag);
	}
	disabled = {
		_get = function(){
			return ..win.getStyle(owner[["hwnd"]],0x8000000/*_WS_DISABLED*/ ) 
		}
		_set = function(v){
			..win.enable(owner[["hwnd"]],!v?1:0);  
		}
	
	};
	
	wndproc = {
/*		_get = function(){ 
		//只读属性
		}*/
		_set = function(v){ 
			//Remove All
			if( v === null){
				owner[["wndproc__"]] = null;
				return;
			};
			assert(type(v) == type.function,"消息回调函数必须是一个函数对象!") 
			 	
			var preProc = owner[["wndproc__"]];
			if(preProc){
				//Decorate the previous WndProc
				owner[["wndproc__"]] = function(...){
						var ret = v(...);
						if(ret===null)
							return preProc(...);
							
						return ret;
				}
				return;
			} 
			 
			owner[["wndproc__"]] = v;
			 
			//真正的回调函数只能创建一次
			if(owner[["_wndproc_c"]] )
				return;
				
			var _defwndpoc_c = 0;
			var wndproc = function(hwnd,  message, wParam , lParam){
				var result;
				var proc = owner[["wndproc__"]];
				
				if(proc)
					result = proc( hwnd,  message, wParam , lParam);
					
				if( result  === null ){ //必须用恒等式才能判断是否有返回值(包括false,0等)
					return  ::CallWindowProc( _defwndpoc_c,  hwnd,message, wParam , lParam );
				} 
				return result;//用户函数中返回了自定义的值
			}
			owner[["_wndproc_c"]] = ..raw.tostdcall( wndproc ,   ..win.CALLBACK.WNDPROC,owner)
			_defwndpoc_c=  ::SetWindowPointer(owner[["hwnd"]],0xFFFFFFFC/*_GWL_WNDPROC*/,  owner[["_wndproc_c"]]  ); 
		}
	
	};
	rect = {
		_get = function(){
			return ..win.getRect( owner.hwnd );
		}
		_set = function(v){ 
			if( v[["left"]]===null ) 
				error("参数必须是正确的:;RECT结构体",2) 
			..win.setRect(owner.hwnd,v)
		}
	
	};
	clientRect = {
		_get = function(){
			return ..win.getClientRect(owner.hwnd)
		}  
	};
	left = {
		_get = function(){
			var rc = ..win.getRect( owner.hwnd ) 
			return  rc.left; 
		}
		_set = function(v){
			var rc = ..win.getRect( owner.hwnd )
			owner.setPos( v,rc.top);
		}
	
	};
	right = {
		_get = function(){
			var rc = ..win.getRect( owner.hwnd ) 
			return  rc.right; 
		}
		_set = function(v){
			var rc = ..win.getRect( owner.hwnd )
			owner.setPos( , ,v - rc.left,rc.bottom-rc.top); 
		}
	
	};
	top = {
		_get = function(){
			var rc = ..win.getRect( owner.hwnd ) 
			return  rc.top; 
		}
		_set = function(v){
			var rc = ..win.getRect( owner.hwnd )
			owner.setPos( rc.left,v);
		}
	
	};
	bottom = {
		_get = function(){
			var rc = ..win.getRect( owner.hwnd ) 
			return  rc.bottom;   
		}
		_set = function(v){
			var rc = ..win.getRect( owner.hwnd ) 
			owner.setPos( , ,rc.right - rc.left,v-rc.top); 
		}
	
	}; 
	getPos = function(){
		return ..win.getPos(owner.hwnd)
	}
	setPos = function(...){
		var defRc = owner[["_defCtrlRect"]];
		if( defRc){
			var rc = owner.rect;
			xr = rc.left / defRc.left;
			tr = rc.top / defRc.top;
			rr = rc.right / defRc.right;
			br = rc.bottom / defRc.bottom; 
			
			..win.setPos(owner.hwnd,...)
			
			var rc2 = owner.rect; 
			defRc.left  = rc2.left / xr;
			defRc.top  = rc2.top / tr;
			defRc.right  = rc2.right / rr;
			defRc.bottom  = rc2.bottom / br;
		}
		else
			..win.setPos(owner.hwnd,...)
	} 
	updatePos = function(){
		if(owner.parent && owner._defCtrlRect ){
			
			
			
		}
	} 
	setFocus = function(){
		..win.setFocus(owner.hwnd)
	} 
	font = {
		_get = function(){  
		}
		_set = function(v){
			..win.gdi.setfont( owner[["hwnd"]],v );
		}
	
	};
	theme = { 
		_get = function(){ 
		}
		_set = function(v){
			if(..win.SetWindowTheme){
				if(v === true )
					v =   "Explorer";
					
				var subid = " ";
				if(v)subid=null;
					..win.SetWindowTheme(owner[["hwnd"]],v:" ",subid); 
			}
		}
	
	}; 
}

 