Search code examples
objectrebolred-lang

How to access values from panels in Red language


I am using following code to find products of 2 series of numbers and then to find sum of these products:

  make-row: func [][
    compose [
        t1: text "N1:"
        f1: field 
        t2: text "N2: "
        f2: field
        t3: text "Product: "
        t4: text ""
        b: button "Get product" [
            x: face/extra/2/text          
            y: face/extra/4/text
            z: (to-integer x) * (to-integer y)
            face/extra/6/text: rejoin [z]]
        do [b/extra: reduce [t1 f1 t2 f2 t3 t4]] ]  ]

  view compose [
        (make-row) return 
        (make-row) return
    b: button "Calculate" [t2/text: "..to be given"]
    t1: text "Sum of products:"
    t2: text ""                       ; NEED TO GET SUM OF ALL PRODUCTS IN ABOVE ROWS. 
  ]  

The first part is working all right - The products are being calculated properly. But how can I access these individual products to find sum of products? I could not find any way since the rows are not really objects whose public variables or methods/functions I may be able to access. How can this be solved? Thanks for your help.


Solution

  • As I just learned about faces and panes, here a solution without error handling

    make-row: func [][
         compose [
            text "N1:"
            field 
            text "N2: "
            field
            text "Product: "
            text ""
            button "Get product" [
                b1: index? find face/parent/pane  face
                face/parent/pane/(b1 - 1)/text:  form multiply   to-integer   face/parent/pane/(b1 - 5)/text  to-integer face/parent/pane/(b1 - 3)/text  
            ]
    
        ]  
    ]
    view compose [
        (make-row) return 
        (make-row) return
        button "Calculate" [
            t2/text:   form add to-integer face/parent/pane/6/text  to-integer face/parent/pane/13/text
        ]
        text "Sum of products:"
        t2: text ""  
    ]   
    

    All face objects are ordered in the pane block of the parent face. So looking at the index of the clicked face object you get a reference to compute the position of other face objects.