Home   Archive   Permalink

foreach inside view

What I'm trying to accomplish is to view an image of its size and corresponding label of its size close to one another. I didn't use `hgroup` in the code.
The problem is that it seems I cannot use foreach inside the view block.
do %./r3-gui.r3
dir: %./
bl: copy []
foreach file load dir [
     if find [%.gif] suffix? file [
         a: load file
         txt: a/size
         pair_bl: copy []
         append pair_bl a
         append pair_bl txt
         append bl pair_bl
view layout [
     foreach [c1 c2] bl [
         label "c2"                 ;c2 as string just to simplify example
Within the view block (I'm forgetting about the image now), I was expecting to generate labels or texts with the sizes of the images in the current directory.
What should I do to accomplish this? Is this situation mentioned in http://www.learnrebol.com/rebol3_book.html ?

posted by:   Luis       20-Jun-2014/7:40:34-7:00

Don't believe FOREACH works like that inside the GUI dialect. Also note the handy COLLECT function:
view collect [
     foreach file load dir [
         keep 'label
         file: load file
         keep file/size

posted by:   Chris       23-Jun-2014/9:47:40-7:00

I have tried something similar with success, but, looking back, I think I did it in a non-recommended way.
I wanted to display an array of buttons, with each button launching a program. The program that displayed this array did not know, until run time, what those programs were, or how many there were. There is the similarity to your problem I think; you want do display a bunch of images but you can't code a layout in advance because you don't know what images you want to show, or how many there are, until the program runs and discovers that.
So what I did, and you might try, is to have the program generate VID code, based on its discovery of what images it wants to display, and then run that VID code through the layout function before viewing it. When I did it, I made a big string, but I think that might not be the recommended way, but it did work. In other words, the program sort of writes itself. That's one of the slick features of REBOL, that it can do that.

posted by:   Steven White       23-Jun-2014/10:55:23-7:00

Or if this is of some use:
R E B O L []
files: read %.
images: [ ]
length-counter: 0
foreach file files [
     if (find file %.png) [
         append images file
         append images size? file
         loaded: load file
         length: loaded/size/2
         length-counter: length-counter + length
grid: copy [across space 0] ; the GUI block containing the grid of fields
forskip images 2 [append grid compose [
     image (load images/1)
     text (form images/2)return
view center-face layout [
     space 0
     display: box 800x800 with [
         pane: layout/tight grid pane/offset: 0x0
     scroller as-pair 16 (length-counter) [
         display/pane/offset/y: display/size/y - display/pane/size/y * negate value
         show display
adapted from http://musiclessonz.com/rebol_tutorial.html

posted by:   Peter K       24-Jun-2014/22:39:14-7:00