[JW Lua] Button press callback

Jari Williamsson jari.williamsson at mailbox.swipnet.se
Wed Nov 18 08:54:37 CET 2015


On 2015-11-18 03:38, Charles O. Lawrence wrote:

> I understand about the method CreateTree(), but still do not see how to use
> the FCTreeNode found in the class browser under the filter "Dialog Boxes".
> There is no corresponding method in the FCCustomLuaWindow class.  How is one
> supposed to use FCTreeNode, if at all, or is one to always just use
> AddNode()?

Use the tree node objects together with the various (Lua-supported) 
methods found in FCCtrlTree.

> Similarly, how is one supposed to use FCControl, if at all, or is one to
> always use the methods supplied to create the various controls?

FCControl is a base class for controls. It provides basic functionality 
for its child classes, such as control ID, position, visibility state, 
width/height, but you never create object of this class type.

In fact, you never create controls other than with the "Create" methods 
in the window class.

When you get a call to the callback and has resolved that it's the 
correct control ID, use the returned object from the "Create" method if 
you want to do anything with the control. (Lua can't type cast.)

> I see how you setup the callback for the button press.  What was confusing
> to me was how to check which of several buttons was pressed.  Normally when
> you call a function, in this case mycommandhandler, which has an argument,
> thecontrol, you have to pass the value of the argument to the function.  It
> appears that when you RegisterHandleCommand to setup the callback function,
> you don't specify an argument to the callback function, rather it somehow
> "magically" gets the argument, in this case the control ID, via the fact
> that the definition of the function has an argument.  This is strange to me.

The script never calls the callback function directly. If it would be a 
call, it would be at least "mycommandhandler()". But here the '()' is 
missing.

"mycommandhandler" is called automatically (when needed) from within the 
dialog:ExecuteModal() method.

The script sends a variable called "mycommandhandler" to 
RegisterHandleCommand().

A line like...
print(mycommandhandler)
...will result in..
[Function]

In Lua, you can even write a callback inside the call, although that's 
perhaps too confusing:
dialog:RegisterHandleCommand(function(thecontrol) print(thecontrol) end)

More info about functions as first-class values can be found in 
Programming in Lua, Chapter 6:
http://www.lua.org/pil/6.html

> Also, how can you define mybutton as local and yet it is in the scope of the
> callback function?

"mybutton" is defined before the function, with file scope.


Best regards,

Jari Williamsson





More information about the JWLua mailing list