基于 XLUE 实现的 listbox 控件
1. 提供增删查接口,将 obj 作为子控件添加到列表;
2. 提供 Attach/Detach 方法,可以将子控件的事件转发出来;
3. 支持滚动条;
4. 支持鼠标滚轮;


实现过程中的注意点:
1. 使用 ItemObjList 表存储 itemObj , ItemObjList 是一个数组,是 listbox 控件的数据模型;
2. 使用 EventCookieMap 表存储事件回调, Attach 时监听所有 itemObj 的事件,通过 Insert 添加的 itemObj,也要通过 EventCookieMap 表来确定需要监听哪些事件;
3. 鼠标滚轮的实现,子控件鼠标滚轮事件 RouteToFather ,滚动条鼠标滚轮事件重定向到 listbox 控件上;
4. 滚动条的实现,调整位置即可


代码:

simplelistbox.xml

<xlue>
<control class="SimpleListBox">
<attr_def>
<attr name="VScrollBarNormalBkgID" type="string" />
<attr name="VScrollBarHoverBkgID" type="string" />
<attr name="VScrollBarDownBkgID" type="string" />
</attr_def>
<method_def>
<AttachEvent file="simplelistbox.xml.lua" func="AttachEvent" />
<DetachEvent file="simplelistbox.xml.lua" func="DetachEvent" />
<InsertItem file="simplelistbox.xml.lua" func="InsertItem" />
<RemoveItem file="simplelistbox.xml.lua" func="RemoveItem" />
<ClearItem file="simplelistbox.xml.lua" func="ClearItem" />
<GetItem file="simplelistbox.xml.lua" func="GetItem" />
<GetItemNum file="simplelistbox.xml.lua" func="GetItemNum" />
</method_def>
<event_def>
</event_def>
<objtemplate>
<children>
<obj id="list.layout" class="LayoutObject">
<attr>
<left>0</left>
<top>0</top>
<width>father.width</width>
<height>father.height</height>
<limitchild>1</limitchild>
</attr>
<children>
<obj id="listbox.layout" class="LayoutObject">
<attr>
<left>0</left>
<top>0</top>
<width>father.width-4</width>
<height>0</height>
</attr>
<eventlist>
<event name="OnMouseWheel" file="simplelistbox.xml.lua" func="ListLayout_OnMouseWheel" />
</eventlist>
</obj>
<obj id="vscrollbar" class="TextureObject">
<attr>
<left>father.width-4</left>
<top>0</top>
<width>4</width>
<height>0</height>
</attr>
<eventlist>
<event name="OnLButtonDown" file="simplelistbox.xml.lua" func="VScrollBar_OnLButtonDown" />
<event name="OnLButtonUp" file="simplelistbox.xml.lua" func="VScrollBar_OnLButtonUp" />
<event name="OnMouseMove" file="simplelistbox.xml.lua" func="VScrollBar_OnMouseMove" />
<event name="OnMouseLeave" file="simplelistbox.xml.lua" func="VScrollBar_OnMouseLeave" />
<event name="OnMouseWheel" file="simplelistbox.xml.lua" func="ListLayout_OnMouseWheel" redirect="father:listbox.layout"/>
</eventlist>
</obj>
</children>
</obj>
</children>
<eventlist>
<event name="OnInitControl" file="simplelistbox.xml.lua" func="OnInitControl" />
<event name="OnDestroy" file="simplelistbox.xml.lua" func="OnDestroy" />
</eventlist>
</objtemplate>
</control>
</xlue>

simplelistbox.xml.lua

------------------ 以下为外部函数 -------------------

function AttachEvent(ctrlObj, eventName, callback)
if type(eventName) ~= "string" or type(callback) ~= "function" then
return
end
local attr = ctrlObj:GetAttribute()
if attr.EventCookieMap[eventName] == nil then
attr.EventCookieMap[eventName] = {}
end
local cookie = table.maxn(attr.EventCookieMap[eventName]) +
attr.EventCookieMap[eventName][cookie] = callback for index,itemObj in ipairs(attr.ItemObjList) do
local itemcookie,bRet = itemObj:AttachListener(eventName, true, function(...)
for cookie,callback in pairs(attr.EventCookieMap[eventName]) do
callback(index, ...)
end
end)
end
return cookie
end function DetachEvent(ctrlObj, eventName, cookie)
if type(eventName) ~= "string" or type(cookie) ~= "number" then
return
end
local attr = ctrlObj:GetAttribute()
if attr.EventCookieMap[eventName] == nil then
return
end
attr.EventCookieMap[eventName][cookie] = nil
end function InsertItem(ctrlObj, index, obj)
if type(index) ~= "number" or obj == nil then
return
end
local attr = ctrlObj:GetAttribute()
local listLayout = ctrlObj:GetControlObject("listbox.layout") -- 插入item,调整位置
listLayout:AddChild(obj)
local l,t,r,b = obj:GetObjPos()
local width,height = r-l,b-t
if index <= or #attr.ItemObjList == then -- 插入到最前
for i,itemObj in ipairs(attr.ItemObjList) do
local l,t,r,b = itemObj:GetObjPos()
itemObj:SetObjPos(l,t+height,r,b+height)
end
obj:SetObjPos(,,width,height)
index =
elseif index > #attr.ItemObjList then -- 插入到最后
local lastObj = attr.ItemObjList[#attr.ItemObjList]
local _,_,_,bottom = lastObj:GetObjPos()
obj:SetObjPos(,bottom,width,bottom+height)
index = #attr.ItemObjList +
else -- 插入到中间
local preObj = attr.ItemObjList[index-]
local _,_,_,bottom = preObj:GetObjPos()
for i=index, #attr.ItemObjList do
local itemObj = attr.ItemObjList[i]
local l,t,r,b = itemObj:GetObjPos()
itemObj:SetObjPos(l,t+height,r,b+height)
end
obj:SetObjPos(,bottom,width,bottom+height)
end
table.insert(attr.ItemObjList, index, obj) -- 调整 listLayout 大小
local lastObj = attr.ItemObjList[#attr.ItemObjList]
local _,_,_,bottom = lastObj:GetObjPos()
local l,t,r,b = listLayout:GetObjPos()
listLayout:SetObjPos(l,t,r,t+bottom) -- 调整 VScrollBar 位置
__AdjustVScrollBarPos(ctrlObj) -- 监听新添加item的事件
for eventName,cookieTable in pairs(attr.EventCookieMap) do
if table.maxn(cookieTable) > then
obj:AttachListener(eventName, true, function(...)
for cookie, callback in pairs(cookieTable) do
callback(index, ...)
end
end)
end
end
-- 监听OnPosChange,当 obj height 改变时要调整其它 itemObj 的位置
obj:AttachListener("OnPosChange", true, function(obj,oldLeft,oldTop,oldRight,oldBottom,newLeft,newTop,newRight,newBottom)
local oldHeight,newHeight = oldBottom-oldTop,newBottom-newTop
if oldHeight == newHeight then
return
end local index = nil
for i,itemObj in ipairs(attr.ItemObjList) do
if itemObj:GetID() == obj:GetID() and obj:GetID() ~= nil then
index = i
break
end
end
if not index then
return
end
for i=index+,#attr.ItemObjList do
local itemObj = attr.ItemObjList[i]
local l,t,r,b = itemObj:GetObjPos()
itemObj:SetObjPos(l,t+newHeight-oldHeight,r,b+newHeight-oldHeight)
end
-- 调整 VScrollBar 位置
__AdjustVScrollBarPos(ctrlObj)
end)
-- 监听 OnMouseWheel ,在 itemObj 上的鼠标滚轮事件也要被响应
obj:AttachListener("OnMouseWheel", true, function(obj, x, y, distance, flags)
obj:RouteToFather()
end)
end function RemoveItem(ctrlObj, index)
if type(index) ~= "number" then
return
end
local attr = ctrlObj:GetAttribute()
local listLayout = ctrlObj:GetControlObject("listbox.layout")
if #attr.ItemObjList == then
return
end if index <= then
index =
elseif index > #attr.ItemObjList then
index = #attr.ItemObjList
end -- 删除 obj 调整其它 itemObj 位置
local obj = attr.ItemObjList[index]
local l,t,r,b = obj:GetObjPos()
local width,height = r-l,b-t
for i=index+, #attr.ItemObjList do
local itemObj = attr.ItemObjList[i]
local l,t,r,b = itemObj:GetObjPos()
itemObj:SetObjPos(l,t-height,r,b-height)
end
listLayout:RemoveChild(obj)
table.remove(attr.ItemObjList, index) -- 调整 listLayout 高度
local bottom =
local lastObj = attr.ItemObjList[#attr.ItemObjList]
if lastObj then
_,_,_,bottom = lastObj:GetObjPos()
end
local l,t,r,b = listLayout:GetObjPos()
listLayout:SetObjPos(l,t,r,t+bottom) -- 调整 VScrollBar 位置
__AdjustVScrollBarPos(ctrlObj)
end function ClearItem(ctrlObj)
local attr = ctrlObj:GetAttribute()
local listLayout = ctrlObj:GetControlObject("listbox.layout")
listLayout:RemoveAllChild()
attr.ItemObjList = {}
end function GetItem(ctrlObj, index)
if type(index) ~= "number" then
return
end
local attr = ctrlObj:GetAttribute()
return attr.ItemObjList[index]
end function GetItemNum(ctrlObj)
local attr = ctrlObj:GetAttribute()
return #attr.ItemObjList
end ----------------- 以下为事件处理函数 ------------------ function OnInitControl(ctrlObj)
local attr = ctrlObj:GetAttribute()
attr.ItemObjList = {}
attr.EventCookieMap = {} local vsbarObj = ctrlObj:GetControlObject("vscrollbar")
if attr.VScrollBarNormalBkgID then
vsbarObj:SetTextureID(attr.VScrollBarNormalBkgID)
end
end function OnDestroy(ctrlObj) end function ListLayout_OnMouseWheel(obj, x, y, distance, flags)
local ctrlObj = obj:GetOwnerControl()
local listLayout = ctrlObj:GetControlObject("listbox.layout")
local fatherLayout = listLayout:GetParent()
local left,top,right,bottom = listLayout:GetObjPos()
local _,fatherTop,_,fatherBottom = fatherLayout:GetObjPos()
local fatherHeight = fatherBottom-fatherTop -- distance 为正数时, listLayout 向下移动,反之向上移动
local moveDistance = distance/
if moveDistance > and top + moveDistance > then -- 向下移动到顶了
if top < then
listLayout:SetObjPos(left,,right,bottom-top)
end
elseif moveDistance < and bottom + moveDistance < fatherHeight then -- 向上移动到底了
if bottom > fatherHeight then
listLayout:SetObjPos(left,fatherHeight-(bottom-top),right,fatherHeight)
end
else
listLayout:SetObjPos(left,top+moveDistance,right,bottom+moveDistance)
end
__AdjustVScrollBarPos(ctrlObj)
end function VScrollBar_OnLButtonDown(vsbarObj)
vsbarObj:SetCaptureMouse(true)
local ctrlObj = vsbarObj:GetOwnerControl()
local attr = ctrlObj:GetAttribute()
if attr.VScrollBarDownBkgID then
vsbarObj:SetTextureID(attr.VScrollBarDownBkgID)
end
end function VScrollBar_OnLButtonUp(vsbarObj)
vsbarObj:SetCaptureMouse(false)
local ctrlObj = vsbarObj:GetOwnerControl()
local attr = ctrlObj:GetAttribute()
if attr.VScrollBarNormalBkgID then
vsbarObj:SetTextureID(attr.VScrollBarNormalBkgID)
end
end function VScrollBar_OnMouseMove(vsbarObj, x, y, flags)
if flags == then -- 鼠标左键被按下
local listLayout = vsbarObj:GetOwnerControl():GetControlObject("listbox.layout")
local fatherLayout = vsbarObj:GetParent()
local _,listTop,_,listBottom = listLayout:GetObjPos()
local _,fatherTop,_,fatherBottom = fatherLayout:GetObjPos()
local listHeight,fatherHeight = listBottom-listTop,fatherBottom-fatherTop
local _,vsbTop,_,vsbBottom = vsbarObj:GetObjPos()
local vsbHeight = vsbBottom-vsbTop -- 调整 VScrollBar 的位置
local vsbTop = y
if y < then
vsbTop =
elseif y > fatherHeight - vsbHeight then
vsbTop = fatherHeight - vsbHeight
end
local l,t,r,b = vsbarObj:GetObjPos()
if t ~= vsbTop then
vsbarObj:SetObjPos(l,vsbTop,r,vsbTop+vsbHeight)
end -- 调整 listLayout 的位置
local listTop =
listTop = -listHeight * vsbTop/fatherHeight
local l,t,r,b = listLayout:GetObjPos()
if t ~= listTop then
listLayout:SetObjPos(l,listTop,r,listTop+listHeight)
end
end if flags ~= then
local ctrlObj = vsbarObj:GetOwnerControl()
local attr = ctrlObj:GetAttribute()
if attr.VScrollBarHoverBkgID then
vsbarObj:SetTextureID(attr.VScrollBarHoverBkgID)
end
end
end function VScrollBar_OnMouseLeave(vsbarObj)
local ctrlObj = vsbarObj:GetOwnerControl()
local attr = ctrlObj:GetAttribute()
if attr.VScrollBarNormalBkgID then
vsbarObj:SetTextureID(attr.VScrollBarNormalBkgID)
end
end -------------------- 以下为内部函数 ------------------- function __AdjustVScrollBarPos(ctrlObj)
local listLayout = ctrlObj:GetControlObject("listbox.layout")
local fatherLayout = listLayout:GetParent()
local vsbarObj = ctrlObj:GetControlObject("vscrollbar") local _,listTop,_,listBottom = listLayout:GetObjPos()
local _,fatherTop,_,fatherBottom = fatherLayout:GetObjPos()
local listHeight,fatherHeight = listBottom-listTop,fatherBottom-fatherTop -- 大小
local vsbHeight =
if listHeight > fatherHeight then
vsbHeight = fatherHeight * fatherHeight/listHeight
end
-- 位置
local vsbTop =
if listHeight > fatherHeight then
vsbTop = fatherHeight * (-listTop)/listHeight
end local l,t,r,b = vsbarObj:GetObjPos()
if t ~= vsbTop or b-t ~= vsbHeight then
vsbarObj:SetObjPos(l,vsbTop,r,vsbTop+vsbHeight)
end
end

使用 xlue 实现简单 listbox 控件的更多相关文章

  1. MFC中Listbox控件的简单使用

    MFC中listbox控件是为了显示一系列的文本,每个文本占一行.   Listbox控件可以设置属性为: LBS_CHILD   :(默认)子窗口 LBS_Visible :(默认)可视 LBS_M ...

  2. C# LIstbox 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题

    解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题 分类: winform2008-05-24 02:33 2592人阅读 评论(11) 收藏 举报 winf ...

  3. UWP入门(八)--几个简单的控件

    原文:UWP入门(八)--几个简单的控件 每天看几个,要不聊几天我就可以看完啦,加油! 看效果 1. CheckBox <TextBlock Grid.Row="0" Tex ...

  4. 编写有提示的listbox控件 2008-06-29 17:13

    在MFC中几乎所有的控件都有信息提示,而惟有listbox却没有这样的一个功能,每当我们把鼠标移到listbox上控件时,啥玩意儿都没有是不是很气馁啊,所以我今天特地写了一个简单的有提示的listbo ...

  5. asp.net中的ListBox控件添加双击事件

    问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项 页面: <asp:ListBox ID=&qu ...

  6. WPF中ListBox控件在选择模式(SelectionMode)为Single时仍然出现多个Item被选中的问题

    最近在学习WPF过程中使用到了ListBox控件,在使用时遇到下面的奇怪问题: 代码如下: listBox.Items.Add("绘图"); listBox.Items.Add(& ...

  7. MATLAB GUI程序设计中ListBox控件在运行期间消失的原因及解决方法

    在运行期间,ListBox控件突然消失,同时给出如下错误提示: Warning: single-selection listbox control requires that Value be an ...

  8. 异步方式向WPF ListBox控件中一条一条添加记录

    向ListBox绑定数据源时,如果数据量过大,可能会使得程序卡死,这是就需要一条一条的向ListBox的数据源中添加记录了,下面是个小Demo: 1.前台代码,就是一个ListBox控件 <Wi ...

  9. asp.net Listbox控件用法

    2008-02-18 19:56 来源: 作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项.列表框可以为用户提供所有选项的列表.虽然也可设置列表框为多 ...

随机推荐

  1. attribute用法

    attribute 用法 摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了_ ...

  2. XSS之浪潮已经来临

    前些天和Roy厉在微博上聊到微信公众账号,我说我在辛苦运营“网站安全中心”这个账号呢,他说我这账号粉丝少是少了点,不过用户定位精确,我说我不希望精确,因为我在尽可能写科普,科普需要传播. Roy厉说过 ...

  3. jQuery最佳实践:如何用好jQuery

    一.用对选择器 在jQuery中,你可以用多种选择器,选择同一个网页元素.每种选择器的性能是不一样的,你应该了解它们的性能差异. (1)最快的选择器:id选择器和元素标签选择器 举例来说,下面的语句性 ...

  4. http://blog.csdn.net/a9529lty/article/details/6454156

    http://blog.csdn.net/a9529lty/article/details/6454156

  5. scrapy-splash抓取动态数据例子三

    一.介绍 本例子用scrapy-splash抓取今日头条网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...

  6. redhat6.3 找不到libXP.so.6解决方法

    最近做redhat安装matlab2008a 操作,遇到上面的问题,先记录如下: 网上找到安装包:libXp-1.0.2-1mamba.x86_64.rpm 然后使用命令: sudo rpm -ivh ...

  7. HTML5基础知识汇总_(2)自己定义属性及表单新特性

    自己定义属性data-* 说起这个属性,事实上如今非经常见了;怎么说呢,由于在一些框架都能看到他的身影!!! 比方Jquery mobile,里面非常频繁的使用了这个属性; 这个属性是哪里来的-.当然 ...

  8. SSMybatis整合 --详细解读Mybatis对oracle数据库进行增删改查(一)

    Mybatis是现在主流的持久化层框架,与Hibernate不同的是,它鼓励程序员使用原声SQL语句对数据库进行操作.因此提供了非常灵活的功能.特别是当数据库同时访问数过多,需要进行优化时,使用sql ...

  9. MySql sqlstate代码大全(转载)

    http://blog.csdn.net/u013847120/article/details/52887813 本章列出了当你用任何主机语言调用MySQL时可能出现的错误.首先列出了服务器错误消息. ...

  10. Android 如何添加一个新的时区

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...