lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 07.04.12 10:11, schrieb Miles Bader:
> Marc Balmer <marc@msys.ch> writes:
>> It is very easy to define a callback when creating a widget hierarchy in
>> LuaMotif:
> ...
>> But what when you want to pass parameters to that function?  I resorted
>> to some kind of convention (to avoid the word 'hack'):
>>
>> data = {42, 'Mr. Wong', 'foo', function () print('hello') end }
>>
>> myform = Form {
> ...
>> 			activateCallback = Callback {
>> 				function (data)
>> 					print('hi there ' .. data[2])
>> 				end,
>> 				data
> 
> Why can't you just make the callback a closure, and reference "data"
> directly from it (without making it a parameter)?
> 
> e.g.:
> 
>> data = {42, 'Mr. Wong', 'foo', function () print('hello') end }
>>
>> myform = Form {
> ...
>> 			activateCallback = function ()
>> 					print('hi there ' .. data[2])
>> 				end

I am using it in a loop, e.g. like this:

local action_items = {
	{ "Suchen",	ok_pushed,	'hello, world' },
	{ "Loeschen",	clear_pushed,	text },
	{ "Abbruch",	close_dialog,	search_dialog },
}

function CreateActionArea(items)
	local f = Form {
		fractionBase = 8 * #items - 1,
		leftOffest = 10,
		rightOffset = 10,
		paneMaximum = 72,
		paneMinimum = 72
	}
	
	for i, item in pairs(items) do
		local la
		local ra
		local default
		
		if i == 1 then
			la = ATTACH_FORM
			ra = ATTACH_POSITION
			default = 1
		else
			la = ATTACH_POSITION
			ra = ATTACH_POSITION
			default = 0
		end
		if i == #items then
			ra = ATTACH_FORM
		end
		
		f[item[1]] = PushButton {
			leftAttachment = la,
			leftPosition =		8 * (i - 1),
			topAttachment= 		XmATTACH_FORM,
			bottomAttachment =	XmATTACH_FORM,
			rightAttachment=	ra,
			rightPosition =		8 * (i - 1) + (8 - 1),
			showAsDefault =		default,
			defaultButtonShadowThickness =	1,
			activateCallback = Callback {
				item[2], -- the function
				item[3]	 -- data to be passed
			}
		}
	end
	return f
end

CreateActionArea(action_items)

So I think a closure would not work here?