namespace win.ui.ctrl; 
import win.ui.ctrl.metaProperty;//导入控件属性元表支持

class listbox{
    ctor(parent,tvalue){ 
    	if( tvalue ){
    		tvalue.style |= 0x1/*_LBS_NOTIFY*/;
    		//tvalue.style |= 0x2/*_LBS_SORT*/;
    		//tvalue.style |= 0x800/*_LBS_EXTENDEDSEL*/; //应该加到右侧属性中，支持通过Shift键或者鼠标进行多选
    		if(tvalue.msel)
    			tvalue.style |= 0x8/*_LBS_MULTIPLESEL*/
			if(tvalue.edge) tvalue.exstyle |= 0x200/*_WS_EX_CLIENTEDGE*/;
		}
    } 
    
    clear = function(){//清除所有项
       ::SendMessage(this.hwnd , 0x184/*_LB_RESETCONTENT*/, null,null)
    }
    
    delete = function(i){//删除选定项
       ::SendMessage(this.hwnd , 0x182/*_LB_DELETESTRING*/, topointer( this.selIndex-1 ),null)
    }
    
    add = function( str,ind=-1){//listbox添加项 
    	if(ind<0)
    		ind = this.count + ind +1;
    	else 
    		ind--;
    		
        ::SendMessage(this.hwnd  , 0x181/*_LB_INSERTSTRING*/, topointer(ind),str)
    }
    
	addfile = function( str){//listbox添加文件到列表项 DlgDirList
		::SendMessage(this.hwnd  , 0x196/*_LB_ADDFILE*/, ,str)
	}
	
	find = function( str ){//在列表框里查找指定的项
		return ::SendMessage(this.hwnd , 0x18F/*_LB_FINDSTRING*/, topointer( this.selIndex-1 ), str ) + 1; 
	}
	
	findEx = function( str ){//精确查找指定的项
		return ::SendMessage(this.hwnd , 0x1A2/*_LB_FINDSTRINGEXACT*/, , str ) + 1;
	}
	
	getItemRect = function(ind,rc){ 
		rc := ::RECT();
		var ret = ::SendMessageByStruct(this.hwnd,0x198/*_LB_GETITEMRECT*/,ind-1,rc);
		..io.print(ret,..lasterr())
		return rc;
	}
	getItemText = function(ind){ 
		var len = ::SendMessageInt(this.hwnd, 0x18A/*_LB_GETTEXTLEN*/, ind-1,0) ; 
		var ret,text  =  ::SendMessageByString(owner.hwnd , 0x189/*_LB_GETTEXT*/,ind-1,len+1); 
		return text;
	}
	 
	getAt = function(x,y){
		var rc = ::RECT()
		for(i=1;this.count){
			this.getItemRect(i,rc); 
			if( ::PtInRect(rc,x,y) )
				return i;
		} 
	} 
	
	selectRange = function( first ,last){//指定选择的范围
		return ::SendMessage(this.hwnd , 0x183/*_LB_SELITEMRANGEEX*/, topointer( 1 ), topointer( ::MAKELONG(first, last) ));
	}
 
	["select"] = function (ind, selected=true) { 
		::SendMessageInt(this.hwnd,0x185/*_LB_SETSEL*/,selected?1:0,ind-1);
		
	}
	selected = function(ind){
		return ::SendMessageInt(this.hwnd,0x187/*_LB_GETSEL*/,ind-1,0);
	} 
	
	@metaProperty;
}

listbox.metaProperty = ..win.ui.ctrl.metaProperty(

	count = {
		_get = function(){ 
			return ::SendMessage(owner.hwnd, 0x18B/*_LB_GETCOUNT*/ )  
		}
		_set = function( v ){
			::SendMessage(owner.hwnd, 0x1A7/*_LB_SETCOUNT*/, topointer(v)  )
		} 	
	};
	selIndex = {
		_get = function(){ 
			return ::SendMessage(owner.hwnd, 0x188/*_LB_GETCURSEL*/, null,null) +1 
		}
		_set = function( v ){ 
			::SendMessage(owner.hwnd,0x186/*_LB_SETCURSEL*/,topointer(v-1));
		} 	
	}; 
	selText = {
		_get = function(){ 
			var sel = ::SendMessage(owner.hwnd, 0x188/*_LB_GETCURSEL*/) ; 
			if(sel>-1){
     			var len = ::SendMessage(owner.hwnd, 0x18A/*_LB_GETTEXTLEN*/, topointer(sel)) ;   
     			var ret,text  =  ::SendMessageByString(owner.hwnd, 0x189/*_LB_GETTEXT*/,sel,len+1);   
     			return ret ? ..string.str( text );
     		}
		}
		_set = function( v ){ 
			::SendMessage(owner.hwnd, 0x18C/*_LB_SELECTSTRING*/, ,v); 
		} 	
	};
	items = {
		_get = function(){ 
			var count = ::SendMessage(owner.hwnd, 0x18B/*_LB_GETCOUNT*/, null);
				tab = {};
				for(i=1;count){ 
               	 	..table.push(tab,owner.getItemText(i) )
                }
                
				return tab;
		}
		_set = function( v ){
			for(i=1;#(v) ){ 
				    ::SendMessage(owner.hwnd , 0x180/*_LB_ADDSTRING*/, null,v[i]);
			}
		} 	
	}
	
)



/***intellisense()
?win.ui.ctrl.listbox  =!listbox.
!listbox.clear() = 清除列表框所有内容
!listbox.delete() = 删除当前选中项
!listbox.getItemText(__/*项索引*/) = 获取指定项文本
!listbox.getItemRect(__/*项索引*/) = 获取指定项区块位置\n返回::RECT() 结构体
!listbox.getAt(x,y) = 返回指定客户区坐标所在的项索引.
!listbox.add(__/*请输入文本*/) = 添加列表项到尾部
!listbox.add(__/*请输入文本*/,-1) = 添加列表项到尾部
!listbox.add(__/*请输入文本*/,1) = 添加列表项到头部
!listbox.selIndex = 获取或设置列表框当前选中项
!listbox.selText = 获取或设置当前选中的文本
!listbox.hwnd = 控件句柄
!listbox.id = 控件ID
!listbox.parent = 父窗口
!listbox.parent.hwnd = 父窗口句柄
!listbox.disabled = 控件ID
!listbox.left = 左侧坐标
!listbox.right = 右侧坐标
!listbox.top = 顶部坐标
!listbox.bottom = 底部坐标
!listbox.count = 列表项数目
!listbox.selectRange(1,__) = 选中指定范围
!listbox.select(1) = 选中指定项
!listbox.select(1,false) = 取消选中指定项
!listbox.selected(1) = 是否选中
!listbox.findEx(__/*请输入文本*/) = 精确查找指定的项\n找不到返回0
!listbox.find(__/*请输入文本*/) = 在列表框里查找指定的项\n找不到返回0
!listbox.addfile(__/*请输入路径*/) = 向列表框里增加一个文件(包括目录)
!listbox.redraw() = 刷新
!listbox.items = 列表项集合\ntable对象
!listbox.show(true__) = 显示控件
!listbox.rect = 控件区块位置(::RECT结构体)
!listbox.clientRect =  控件客户区块位置(::RECT结构体)
!listbox.font = 控件字体(::LOGFONT结构体)
!listbox.clientRect =  获取控件客户区块位置(::RECT结构体)
!listbox.theme = 外观主题,例如\nwinform.button.theme = "Explorer"\nwinform.button.theme = false
!listbox.modifyStyle(.(remove,add) = 如果指定第三个参数，则使用此参数调用::SetWidnowPos 
!listbox.modifyStyleEx(.(remove,add) = 如果指定第三个参数，则使用此参数调用::SetWidnowPos
!listbox.close() = 关闭控件窗
!listbox.capture = 是否捕获全局鼠标消自息
!listbox.invalidate(__/*可选使用::RECT()对象指定客户区*/) = 使窗口绘图区无效
!listbox.invalidate(__/*可选使用::RECT()对象指定客户区*/,0) = 使窗口绘图区无效\n不刷新背景
!listbox.update() = 重绘invalidate函数指定的区块 
!listbox.setFocus() = 设置焦点 
!listbox.setPos(.(x坐标,y坐标,宽,高,插入位置,参数) = 调整窗口位置或排序,所有参数可选\n同时指定x,y坐标则移动位置\n同时指定宽高则改变大小\n指定插入位置(句柄或_HWND前缀常量)则调整Z序
!listbox.getPos() = 返回相对坐标,宽,高\nx,y,cx,cy=win.getPos(hwnd)


end intellisense***/