Communication between modal stack and main stack
chipp at chipp.com
Fri Nov 4 05:40:57 CST 2005
I like to encapsulate the entire dialog capability into a single
function call. This includes setup and display.
For instance. Let's say you want a dialog box which asks the user to
select a single line from a list of stuff. And you want also to have a
custom prompt. Plus you want an OK and Cancel button
I would write the function 'askListItem' and call it from the button
named "pick your favorite fruit"
(tFruitList is a variable containing a line-delimited list of fruits)
get askListItem(tFruitList,"Please choose a fruit from the list")
if it is "Cancel" then exit mouseUp
put it into tMyFavoriteFruit
So, the question is how to make it this simple?
Easy, you'll want to build a stack "askList" with 2 fields: "theList"
and "thePrompt"; 2 buttons "Cancel" and "OK"; and in the stack script:
function askListItem pList,pPrompt
put "" into the dialogData
put pList into fld "theList" of stack "askList"
put pPrompt into fld "thePrompt" of stack "askList"
modal stack "askList"
return the dialogData
The script of the button "OK" is:
put the hilitedlines of fld "theList" into tLineNum
if tLineNum is "" then
set the dialogData to line tLineNum of fld "theList"
close this stack
and the script for the button "Cancel" is:
set the dialogData to "Cancel"
close this stack
Now the secret to making all this work is that you need to make
'askList' a library stack. This way, all of it's stack scripts will be
available to whatever card/stack/button you're on.
To do this, you'll need to insert into the openStack handler of card 1
of your main stack:
start using stack "askList"
That's all there is to it! Now, you've got an encapsulated stack which
can process list queries from anywhere.
Hope this helps,
Jan Sælid wrote:
> I use several modal stacks to communicate with the user.
> My first question is:
> Is there a way to pass informasjon from a modal stack back to the function on the stack that
> opened it? Like "Return"? I use a custom property for now....
> When the modal stack closes the function in the main stack that opened it continues. Just what I want. But for the remaining message path in the function
> I have to refer to the main stack - even if the fuction resides in the main stack. Or else I get a background not found error.
More information about the use-livecode