[ALLY] Re: Layout Bind or Context in stylized Layouts
From: jeff:rebol at: 13-Nov-2000 5:44
Howdy, Volker:
> there is also an automation for keywords like in [list
> .. data], but i have not figured it out. someone?
>
> Volker
Sure. Here's a fairly simple example of including custom
words in your style:
a-style: stylize [
color-box: box with [
colors: none
action: [
if tail? colors: next colors [
colors: head colors
]
color: first colors
show face
]
words: [colors [new/colors: copy next args]]
]
]
view layout [
styles a-style
color-box colors 255.0.0 0.255.0 0.0.255
127.20.50 30.60.90
button "Close" [unview/all]
]
In your style, you define a field called 'words, and the words
field has the format: [word [action-on-args]].
While LAYOUT is parsing the block you hand it, if it
encounters your style it will then look for your custom words
bracketed between the current style and the next style that it
can see later on. The arguments for your style's custom word
can then be determined and they are passed to the code that
you define in the "action-on-args" block. The arguments that
your custom word action-on-args block receives begin starting
on the custom word you defined. Here's an example to see how
this works more directly:
style-2: stylize [
arg-text: text with [
words: [args [new/text: reform ["My args:" args]]]
]
]
view layout [
styles style-2
txt "Hello"
arg-text black [print "ARG-TEXT"]
args [1 #2 [foo--bar] http://url]
button "Close" [unview/all]
]
Notice above that one of the real advantages to using
custom words is we can now have more than one block of
stuff associated with the style. Having more that one
block of data for a style allows us to both easily define
an action associated with a style along with the ability to
pass in other necessary stuff for the style to use.
Within the "action-on-args" block for the WORDS field, the
newly created instance of our style, arg-text, is referred
to as NEW. Whatever we do to NEW will happen to the
resulting VID object. However, with some experimentation
you'll find that if you include a string with your arg-text
instance in a layout, the string will override your setting
of the text via the 'args word. (ie:
view layout [
arg-text "This is what you'll see" args [1 2 3]
]
) Certain steps in the LAYOUT pipeline take precedence over
others because of when they happen.
For kicks we'll look at one more fun example:
style-3: stylize [
two-act-box: box with [
toggle: a1: a2: none
action: [
do func [face] either toggle: not toggle [a1][a2] self
]
words: [
action1 [new/a1: second args next args]
action2 [new/a2: second args next args]
]
]
]
view layout [
styles style-3
two-act-box "Click me" red
action1 [inform layout [text "Action 1!"]]
action2 [face/color: random 0.0.255 show face]
]
In the code above, we've created a box that has two
different actions that toggle back and forth each time you
click on the box. The trick, here, is to turn our two
separate actions into functions expecting a face, and pass
in SELF.
One other important thing to observe: At the end of the
blocks in WORDS where we define the "action-on-args" for
action1 and action2, we have "NEXT ARGS". The
"action-on-args" block of WORDS returns a value to LAYOUT,
namely it returns the position of the arguments block for
LAYOUT to know how much you've gobbled. The effect of
including "NEXT ARGS" above is to cause LAYOUT, after
parsing your style's custom word, to position itself after
the arguments that you have consumed for your custom words,
(ready to parse the next custom word). Basically, the above
example will not work unless you include those seemingly
mysterious "NEXT ARGS". :-)
Cheers--
-jeff