[ALLY] Re: Making dynamic button layouts in VIEW
From: bo:rebol at: 30-Jan-2001 15:25
For the enlightenment of gjones and others that may have joined to late,
I am replying to Jeff's response to my original post.
Jeff shows a very good approach to dynamic layouts. I myself was
pleasantly broadened by clever details in his solution of the problem.
The method I show can be extended to handle more complex situations, as
Jeff pointed out in his reply. It is Carl's desire that people realize
how VID and VIEW faces are interrelated, but not at the expense of
added complexity where it is not needed.
Some day someone will write ''10,001 clever methods in REBOL/View''.
This will just be scratching the tip of the iceberg. :-)
On 29-Jan-2001/19:11:40-8:00, [jeff--rebol--com] wrote:
>Your example is elightening, Bo. I'm usually a little too
>lazy of a VID programmer to redo the work that layout does for
>me, though, so I generally take a different approach to those
>kinds of layouts. Here's an alternative way to do the same:
>view layout [
> text "Enter names for some buttons divided by spaces"
> f: field 200 (go: [
> buttons: copy [space 0 style bt button [request/ok face/text]]
> foreach item parse f/text none [repend buttons ['bt item]]
> view/new layout/origin buttons 0x0
> button "Show me!" go
>The above approach can be called "Meta-programming VID" which
>means we use REBOL to generate the VID code that we send off
>to LAYOUT. You see this meta-programming when we use FOREACH
>to add a BT style to the layout we're constructing for each
>text peice. In the end we don't have to worry about where
>anything's placed or how big the window is because that's what
>layout does for us.
>Not all VIEW programming problems are amenable to
>meta-programming, though. Especially in situations where you
>have a layout that you are not going to be re-laying out or
>you have moving and changing faces in a layout-- then directly
>modifying faces and sizes, offsets and other face facets
>becomes very necessary. But if you're just going to create a
>single layout out of some random stuff, why not use
>One other notable difference is instead of reconstructing each
>button and directly setting its action function separately, we
>just created a STYLE, BT, which takes care of the same action
>for all other instances of that style. That's the kind of
>thing styles are good at taking care of.
>> I get asked this question from time to time and there is
>> some insight involved into how VIEW works, so I thought I
>> would post an example to this list for the enlightenment of
>> ;This is a template for a blank button
>> button-layout: layout [button "text" ]
>> ;This is your main VIEW
>> view layout [
>> text "Enter names for some buttons divided by
>> spaces" f: field 200 button "Show me!" [
>> ;; first, start off with an empty face to hold
>> ;; the buttons
>> button-face: layout 
>> ;; keep an offset variable so we know where
>> ;; each button goes
>> ofst: 0x0
>> ;; Break input from field above into a series
>> ;; of strings
>> ;; and perform some actions on each one
>> foreach item parse f/text none [
>> ;; The /pane is where sub-faces are stored.
>> ;; This adds the modified blank button
>> ;; templates into the
>> ;; blank button face we created above.
>> append button-face/pane make
>> button-layout/pane/1 [
>> offset: ofst
>> ;; Buttons expect their label to be in
>> ;; a field called
>> ;; 'texts' which is a block of
>> ;; strings.
>> texts: reduce [item]
>> ;; The action field determines what
>> ;; will be done when
>> ;; the button is clicked. The COMPOSE
>> ;; statement reduces only the items in
>> ;; parentheses ().
>> action: func [f e] compose [request/ok
>> ;; Increment the offset for the next button
>> ofst: ofst + (button-layout/pane/1/size *
>> ;; Set the size for the button-face based on
>> ;; the last offset.
>> button-face/size: ofst +
>> ;; View it!
>> view/new button-face
>> Have fun!
Bohdan "Bo" Lechnowsky
REBOL Adventure Guide
REBOL Technologies 707-467-8000 (http://www.rebol.com)
The Official Source for REBOL Books (http://www.REBOLpress.com)