Page 1 of 1

communicating between stacks

Posted: Sat Sep 15, 2012 8:44 pm
by marksmithhfx
Hi, I am having a problem passing information between two stacks (well, maybe more than that but lets start there). The scenario is I am trying to implement a properties window similar to how properties are set in the IDE.

The mainstack has button A.
I open a properties window with "go stack properties as palette" which is a substack
The substack has field B
I would like the value from field B in the substack to set a custom property in button A of the mainstack.

The following line of code works but....

set the cproperty of btn "button A" in stack "my_hard_coded_stack_name" to fld "this field"

Its all hard coded and is not very generalizable to other objects I may want to use the same substack with (other buttons for example). Also, the stack name happens to contain the version number so each time I change the version number I would have to change this line of code.

I have tried (in the card script) :

set the cproperty of btn "button A" in the mainstack of me to fld "this field"

but I get an error "can't set property". This was just a random attempt. I understand that a card does not have a "mainstack" property so I tried moving the code to the stack with no better luck. (the script is triggered by closing the palette stack). The error in this case was "error in object expression".

Anyway, I am sure this doable since the property windows in the IDE seem to do something similar with palette windows I am presuming are implemented as substacks. It would be interesting to hear ideas on how to do this.

Second, I noticed that when I "go stack properties as palette" that the code in the calling routine continues to execute. Is this the way it is supposed to work? I was hoping to be able to set some properties AND be able to respond to the new values in the same script but if it is going to open the palette window and then continue executing the code I am not sure how I am going to do that. Perhaps an example might explain.

If I want to set the label of the button I could just write

ask "Please enter a new name" and the answer (new label name) is in it.

However if I want to set a whole bunch of properties it seems to me you might want to open a form, collect all the value, close the form and then operate on the result. So, call a new window/substack as a palette, get the values, close the palette/properties window and then respond to the new settings. However, if my codes says "go stack properites as palette" and then the next line says "set the label of me to "new name" but we don't even have the new name yet how is that going to work? So there is something fundamental in the use of the "go" command that I am not understanding. I would have thought "go" interrupts the execution of the current handler in some way but it appears not to.

So, those are the primary problems I am facing... (a) how to get a field in a substack to communicate its value to a custom property in a mainstack and (b) how to get the execution of a script to pause when I "go to" a substack (similar to how it pauses when you "go to" an ask dialog).

Thanks

PS Relatedly, is there a command I can use in the msg box that would list all of the properties of an object in LC? Something like "list the properties of button X" or "list the properties of card Y"??

Thanks again

-- mark

Re: communicating between stacks

Posted: Sun Sep 16, 2012 2:40 am
by marksmithhfx
I recognize this is sort of a complicated question so, since I've been plugging away at it all day, I'll post my solution. Not ideal, but it works.

1. Open the substack using the MODAL option. This pauses the originating script until the modal dialog is closed. Not ideal because you have to add a close button to the modal dialog, and I was hoping to use the close box decoration. While using the "decoration" property seemed like an interesting possibility, in either preopenstack or openstack handlers I could not change the decorations of a window/stack opened with the modal option. Maybe there is a way, but I have not figured it out.

2. I could not find a way to get the mainstack name into the substack card so I wrote a function to do it and put the function into the mainstack. Its called whatis() because every time I typed mainstack() LC gave it a "special" color suggesting it was a reserved word so I decided not to tempt the LC gods. whatis() returns the name of the mainstack.

3. And while I wasn't thrilled about doing it (one more global variable) the only way I could see to get the objectname over into the substack was to stuff it into a global variable.

The end result looks something like the following (on the card of the substack that is called using the modal option)

global gCurrentButtonName

on opencard
# populate the fields with the correct values from the custom properties of the original object
put the cname of the btn gCurrentButtonName of stack whatis() into fld "name"
put the clocation of the btn gCurrentButtonName of stack whatis() into fld "location"
select the text of fld "name"
end opencard

on closecard
# put the field values back into the custom properties of the original object
set the cname of the btn gCurrentButtonName of stack whatis() to fld "name"
set the clocation of the btn gCurrentButtonName of stack whatis() to fld "location"
end closecard

And there you have it... a pseudo-properties window for a button object. It certainly does not work like a properties window in the IDE, but it gets the job done. If anyone has any suggestions for improving this I would greatly appreciate hearing them.

Thanks

-- Mark

Re: communicating between stacks

Posted: Sun Sep 16, 2012 2:51 am
by sturgis
To get the mainstack from a substack you can use "owner"

You can also just do

put the mainstack of stack "whateverstack" -- if whateverstack is a substack it will give you the name of the mainstack. Otherwise if the stack is the mainstack, it gives you its own name back.

Re: communicating between stacks

Posted: Sun Sep 16, 2012 3:39 am
by jacque
I use a function I swiped from Richard. It lives in the stack script of the mainstack:

Code: Select all

function main
  return the short name of me
end main
Then anything that needs to know, can use: get the <whatever> of stack main()

You can get all the properties of an object with "the properties" function: get the properties of btn 1

Handlers always run to completion (except in the case where a modal is displayed, as you found out. In that case the handler pauses till the modal stack closes, then continues.) To do the same thing without using a modal stack, break the handler into two parts. Get the properties you need and display them in your properties stack. When that stack closes, have it call another handler in the main stack that does the second half of whatever you need to do. This method lets you control the stack decorations too, since the display stack won't be a modal.

Also, I'd avoid naming stacks with reserved words, like "properties". You can set the stack's label to "Properties" so that word will show in the titlebar even while its name is something entirely different.

Re: communicating between stacks

Posted: Sun Sep 16, 2012 5:11 am
by marksmithhfx
Thanks Jacque and Sturgis... there are some great suggestions there to work on tomorrow (including renaming my whatis() function to main()

-- Mark