Recursion example stack
David Burgun
dburgun at dsl.pipex.com
Wed Sep 28 14:00:28 EDT 2005
Hi All,
Following on from Martin, I write something that I think might be on
interest that uses recursion in a less obvious way. I use it to
handle Card/Group updates in a "standard" manner. This makes the
script much more reusuable. In your mainStack or a Utility stack, add
the following function:
function UtilStackUpdateGroup theOwner
local myIndex
--
-- Update all Groups that need to be Updated
--
repeat with myIndex = 1 to number of groups in theOwner
if word 1 of the name of group myIndex of theOwner = "Group" then
if the long owner of group myIndex of theOwner = theOwner then
if the script of group myIndex of theOwner contains "GroupUpdate" then
send "GroupUpdate" to group myIndex of theOwner
end if
end if
end if
end repeat
repeat with myIndex = 1 to number of controls in theOwner
if word 1 of the name of control myIndex of theOwner <> "Group" then
if the long owner of control myIndex of theOwner = theOwner then
if the script of control myIndex of theOwner contains
"ControlUpdate" then
send "ControlUpdate" to control myIndex of theOwner
end if
end if
end if
end repeat
end UtilStackUpdateGroup
This loops thru each Group in the Object passed to it (Card or Group
in this case) and then thru each Control on the Object passed to it.
An update is triggered when the user does something that affects
another Object(s) on the card, for example for a check box, you'd do
this:
on mouseUp
set the cpCheckBoxFlag of this card to the hilight of me
send "CardUpdate" to this card
end mouseUp
The CardUpdate function, just does this:
on CardUpdate
get UtilStackUpdateGroup(the long name of me)
end CardUpdate
On each group you want to be updated, add this (this will update all
Sub-Groups) in this Group:
on GroupUpdate
get UtilStackUpdateGroup(the long name of me)
end GroupUpdate
On each control you want updated, do this (this is a terminal node
and so ends the current recursion which will bubble back up to the
top again):
on ControlUpdate
if the cpCheckBoxFlag of this card is true then
set the borderColor of me to red
else
set the borderColor of me to green
end if
end ControlUpdate
Add a ControlUpdate to each control you want to update, but only use
Custom Properties to pass information.
The cool thing about this is you never need to refer to any
control/group name, which means you can paste groups into other stack
and not worry about the names of objects, therefore making the whole
process much more standard and easilly moved. All have to do is to
make the the Custom Properties are setup correctly. You just define
which Custiom Properties are used as input and outputs and hey presto
- It works!
This describes the update path, I do the same for initialization and
Stack resizing, e.g. CardInitialize, GroupInitialize etc.
All the Best
Dave
More information about the use-livecode
mailing list