Generate a list of the controls in a layer in Lua
User Question:
I need to iterate over a set of controls within a layer to resize and move the controls dynamically. I've looked and looked and can't find an example of either getting a list or iterating.
Answer:
Thanks for contacting us!
Getting a list of controls within a layer can be done by utilizing the Lua DOM module.
First you'll have to create a DOMOBJECT handle of the layer:
local dom_object = gredom.get_object("Screen.Layer")
Then you can get a table containing the names of the controls by using the get_children() function
dom_object:get_children()
Here is an example function that stores the children in a table and prints their names:
function cb_dom(mapargs)
local children_list = {}
local dom_object = gredom.get_object("Screen.Layer")
table.insert(children_list, dom_object:get_children())
table.foreach(children_list[1], print)
end
In my test application, this gives the output:
1 Layer.Background
2 Layer.Table
3 Layer.Button
4 Layer.Button2
By default the table value containing the control name has the type 'userdata' and not the type 'string'.
This means that before using this value in any sort of attribute command, you will have to explicitly cast it as a string. Here's a re-written version of the previous function that sets the text of table cells to the names of the controls in the layer:
function cb_dom(mapargs)
local children_list = {}
local dom_object = gredom.get_object("Screen.Layer")
table.insert(children_list, dom_object:get_children())
for k,v in pairs(children_list[1]) do
print(k,v)
--notice the 'tostring()' on the control name var 'v'
gre.set_value(string.format("Layer.Table.text.%s.1", k), tostring(v))
end
end
For further reading about the Lua DOM Module:
http://resources.cranksoftware.com/cranksoftware/v6.0.0/docs/webhelp/index.html#lua_dom_api.html
Comments
Is it possible to get_children() from a control dom_object? i.e. return a table of the render extensions used on the control --I'm not having any luck achieving this.
Or is there a different way of retrieving that table?
never mind - I see those are considered variables
Please sign in to leave a comment.