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

metaProperty = class {
	ctor( meta ){
		this = ..util.metaProperty(meta);  
	} 
	@_metaProperty  //公用属性
} 
namespace metaProperty { 

	commonProperty = {
		
		redraw = function( flag = 0x105 /*_RDW_INVALIDATE | _RDW_UPDATENOW  | _RDW_ERASE*/ ) { 
			::RedrawWindow(owner[["hwnd"]],{},null,flag);
 		};
 		redrawTransparent = function(){
 			if(owner[["transparent"]] && owner[["parent"]][["hwnd"]] ){
				owner[["parent"]].invalidate(owner,1);  
				owner[["parent"]].update();
			}	
			else {
				owner.redraw();
			} 
 		} 
 		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.redrawTransparent();
			}
		
		};
		hide = {
			_get = function(){ 
				return ! ..win.getStyle(owner[["hwnd"]],0x10000000/*_WS_VISIBLE*/ ) 
			}
			_set = function(v){ 
				owner.show(!v ? true:0x0/*_SW_HIDE*/)
			}
		
		};
		close = function(){ 
			if(owner._closed){
				return;
			}
			var p = owner[["_parentForm"]];
			if( p  ){
				p._ctrlHwnd[owner.hwnd]=null;
				p._ctrlId[owner.id]=null;
				if( ! owner.isForm ){
					if( owner._onDestroy ) owner._onDestroy();
					if( owner.onDestroy ) owner.onDestroy();
					if( owner.bgBrush )::DeleteObject( owner[["bgBrush"]]  );
				}  
			}
			::PostMessage(owner[["hwnd"]],0x10/*_WM_CLOSE*/,0,0);
			owner._closed = true; 
		};
		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);  
			}
		
		};
		tailWndproc = {
			_set = function(v){
				var pre = owner[["queueWndProc__"]];
				owner[["queueWndProc__"]] = null;
				owner.wndproc = v;
				if( pre ){
					owner.wndproc = pre;
				}
			};
		};
		wndproc = { 
			//追加式赋值,可赋值为回调函数,或者键为消息ID,值为回调函数的表对象
			_set = function(v){  
				if( v === null){
					owner[["queueWndProc__"]] = null;
					return;
				};
				
				var this = owner; 
				var tv = type(v);
				if( tv == type.table ){
					if( owner[["_defaultProc"]] ){
						..table.mixin( owner[["_defaultProc"]],v );
						return;
					}
					else{
						owner[["_defaultProc"]] = v;
						v = function(hwnd,message, wParam, lParam){
							var proc = this[["_defaultProc"]][message];
							if(proc)return proc(hwnd,message,wParam,lParam);
						}  
					};
				}
				elseif( tv != type.function ) 
					error("消息回调函数必须是一个函数对象!",2) 
			 		
				var pre = owner[["queueWndProc__"]];
				if( pre ){ 
					owner[["queueWndProc__"]] = function(...){
						var ret = v(...);
						if( ret!==null )
							return ret;
								
						return pre(...); 
					}
					return;
				} 
			 	else{
					owner[["queueWndProc__"]] = v;
				}
			 	
				//真正的回调函数只能创建一次
				if(owner[["_wndproc_c"]] )
					return;
					  
				var wndproc = function(hwnd,  message, wParam , lParam){ 
					if( this[["queueWndProc__"]] ){
						var result; 
						result = this.queueWndProc__( hwnd,  message, wParam , lParam);
						if( result !== null ) return result;
					}
					return  ::CallWindowProc( this._wndproc_c_prev,  hwnd,message, wParam , lParam ); 
				}
				owner[["_wndproc_c"]] = ..raw.tostdcall( wndproc ,   ..win.CALLBACK.WNDPROC,owner)
				owner._wndproc_c_prev =  ::SetWindowPointer(owner[["hwnd"]],0xFFFFFFFC/*_GWL_WNDPROC*/,  owner[["_wndproc_c"]]  ); 
			};
			_get = function(){
				return owner[["queueWndProc__"]];
			};
		};
		//追加式赋值
		adjust = {
			_set = function(v){
				if( v===null ){
					owner[["queueAdjust__"]] = null;
					return;
				}
				if(type(v) != type.function ) error("adjust函数必须是一个函数对象!",2) 
				
				var pre = owner[["queueAdjust__"]];
				if( !pre  ){ 
					owner[["queueAdjust__"]] = v;
				}
				else {
					owner[["queueAdjust__"]] = function(...){
						var ret = v(...);
						if( ret!==null )
							return ret;
								
						return pre(...); 
					} 
				} 
			}
			_get = function(){
				return owner[["queueAdjust__"]];
			} 
		};
		getRect = function(screen){ 
			return ..win.getRect( owner.hwnd,screen );
		}; 
		getClientRect = function(){
			return ..win.getClientRect(owner.hwnd)
		};
		left = {
			_get = function(){
				return ..win.getRect( owner.hwnd ).left; 
			}
			_set = function(x){
				var x2,y2 = owner.getPos();
				owner.setPos( x,y2);
			} 
		};
		right = {
			_get = function(){
				return ..win.getRect( owner.hwnd ).right; 
			}
			_set = function(v){
				var rc = ..win.getRect( owner.hwnd )
				owner.setPos( , ,v - rc.left,rc.bottom-rc.top); 
			}
		
		};
		top = {
			_get = function(){
				return ..win.getRect( owner.hwnd ).top; 
			}
			_set = function(v){
				var rc = ..win.getRect( owner.hwnd )
				owner.setPos( rc.left,v);
			} 
		}; 
		bottom = {
			_get = function(){
				return ..win.getRect( owner.hwnd ).bottom;  
			}
			_set = function(v){
				var rc = ..win.getRect( owner.hwnd ) 
				owner.setPos( , ,rc.right - rc.left,v-rc.top); 
			}
		
		}; 
		height = {
			_get = function(){
				var x,y,cx,cy = owner.getPos();
				return cy;
			}
			_set = function(v){
				var x,y,cx,cy = owner.getPos() 
				owner.setPos( , ,cx,v );
			} 
		};
		width = {
			_get = function(){
				var x,y,cx,cy = owner.getPos();
				return cx;
			}
			_set = function(v){
				var x,y,cx,cy = owner.getPos() 
				owner.setPos( , ,v,cy );
			} 
		};
		getPos = function(){
			return ..win.getPos(owner.hwnd)
		};
		setPos = function(...){ 
			if( owner[["_defRect"]] ){ 
				var l,t,r,b = owner._getPosRatio();  
				..win.setPos(owner.hwnd,...)
				owner._setPosRatio(l,t,r,b );
				if(owner.adjust){
					var x,y,cx,cy = owner.getPos();
					owner.adjust(cx,cy);
				}
			}
			else
				..win.setPos(owner.hwnd,...)
		};
		setRect = function(rc,screen){ 
			if( owner[["_defRect"]] ){
				var l,t,r,b = owner._getPosRatio(); 
				..win.setRect(owner.hwnd,rc,screen) 
				owner._setPosRatio(l,t,r,b );
				 
				if(owner.adjust){
					var x,y,cx,cy = owner.getPos();
					owner.adjust(cx,cy);
				}
			}
			else
				..win.setRect(owner.hwnd,rc,screen)  
		};
		_getPosRatio = function(){
			var rc = owner.getRect() 
			var defRc = owner[["_defRect"]];
			if(defRc){ 
				return rc.left?( rc.left / defRc.left),rc.top ? (rc.top / defRc.top) ,rc.right / defRc.right,rc.bottom / defRc.bottom
			}
		}; 
		_setPosRatio = function(xr,tr,rr,br){
			var rc = owner.getRect(); 
			var defRc = owner[["_defRect"]];
			if(defRc){ 
				defRc.left  = xr?(rc.left / xr); 
				defRc.top  = tr?(rc.top / tr);
				defRc.right  = rr?(rc.right / rr);
				defRc.bottom  = br?(rc.bottom / br);
			}
		}; 
		getParent = function(){
			return owner.parent;
		};
		setParent = function(parent){
			if( parent[["hwnd"]] && ( (parent._parentForm == owner._parentForm) || (parent == owner._parentForm) ) ){ 
				var rc = owner.getRect(true);
				..win.toScreenRect(owner.parent.hwnd,owner[["_defRect"]] ) 
				..win.setParent(owner.hwnd,parent[["hwnd"]] );
				owner.parent = parent;
				..win.toClientRect(owner.parent.hwnd,owner[["_defRect"]] )  
				//不要调用owner.setRect(),以避免调用_setPosRatio() 记录错误的相对位置
				..win.setRect(owner.hwnd,rc,true);
			}
			else 
				error("父窗口必须在同一窗体内!",2);
		}; 
		addCtrl = function(param){
			if( owner.isForm )
				return owner.add(param) ; 
			return owner._parentForm.add(param,owner);
		};
		setFocus = function(){
			..win.setFocus(owner.hwnd)
		}; 
		vScroll = function(pos = 0x7/*_SB_BOTTOM*/){ 
			::SendMessageInt(owner[["hwnd"]],0x115/*_WM_VSCROLL*/ , pos, 0); 
		};
		hScroll = function(pos = 0x7/*_SB_RIGHT*/ ){
			::SendMessageInt(owner[["hwnd"]],0x114/*_WM_HSCROLL*/ , pos, 0);
		}; 
		getFont = function(){
			var font = ..gdi.getFont(owner[["hwnd"]])
			font.color = owner.color
			return font;
		};
		setFont = function(f){
			if(f.color)owner.color=f.color;
			..gdi.setFont( owner[["hwnd"]],f );
			if(owner[["transparent"]] ) owner.redrawTransparent();
		};  
		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); 
				}
			}
		}; 
		rect = { //废弃
			_get = function(){ return owner.getRect(); } 
		};
		clientRect = { //废弃
			_get = function(){ return owner.getClientRect() }  
		};
		font = { //废弃
			_get = function(){ return owner.getFont() }
			_set = function(v){ owner.setFont(v) }
		};
		isForm = {
			_get = function(){ return owner@ == ..win.form._metaProperty } 
		}
	}
	_metaProperty = { _get = commonProperty  };
 	 
	SetCapture = ::User32.api("SetCapture", "void(int hwnd)");
	ReleaseCapture = User32.api("ReleaseCapture", "void()"); 
}


/**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**/ 