Search code examples
user-interfaceluaiup

How to display arrays declared as vbox/hbox into main window?


I was creating a function that was supposed to display elements into the Main window after a certain function is performed in IUP for Lua.

The problem was that whenever I run the function without arrays for vbox/hbox, the program displays the GUI elements as normal (in this test case, a textbox). I also tested this by having this display on a new window, which also worked. This is the normal code that I used:

function wa()
    local txt = {}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(hrbox, txt[1])
    iup.Map(txt[1])
    iup.Refresh(hrbox)
end

But when I ran the code by declaring an array variable as a hbox/vbox into the function, suddenly, the program just won't display the element into the main window, nor at the new window, at all:

function wa()
    local txt = {}
    local a = {}
    a[1] = iup.vbox{}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(a[1], txt[1])
    iup.Map(txt[1])
    iup.Refresh(a[1])
    iup.Append(hrbox, a[1])
    iup.Refresh(hrbox)
end

And then the strangest thing is that when I made the hrbox (the box I used to display elements into the main window) into a separate variable altogether, it does not display it in the main window, but it does display in the new window. Here's the code that came afterwards:

function wa()
    local txt = {}
    hrbox = iup.hbox{}
    a = {}
    a[1] = iup.vbox{}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(a[1], txt[1])
    iup.Map(txt[1])
    iup.Refresh(a[1])
    iup.Append(hrbox, a[1])
    iup.Refresh(hrbox)
end

How can I make an array variable that is declared as a hbox/vbox element work so that it can displayed into the main window? I have no idea how to do it, and after going through all the research, I'm essentially stuck.

Any answers and suggestions is appreciated.

Thanks in advance!


Solution

  • I tried to create a minimal reproducible example:

    local iup = require("iuplua")
    
    local Button = iup.button{TITLE="Add"}
    local hrbox  = iup.vbox{Button}
    local Frame  = iup.dialog{hrbox,SIZE="THIRDxTHIRD"}
    
    function wa()
        local txt = {}
        txt[1] = iup.text{
          multiline = "NO",
          padding = "5x5",
          size="100x15",
          scrollbar="Horizontal",
        }
        iup.Append(hrbox, txt[1])
        iup.Map(txt[1])
        iup.Refresh(hrbox)
    end
    
    Button.action = function (Ih)
      wa()
    end
    
    Frame:show()
    iup.MainLoop()
    

    So, basically, your code is working properly. Please make sure you declare hrbox above the wa.

    EDIT: I just understand your question finally

    You have an issue in this piece of code:

    function wa()
        local txt = {}
        local a = {}
        a[1] = iup.vbox{}
        txt[1] = iup.text{
          multiline = "NO",
          padding = "5x5",
          size="100x15",
          scrollbar="Horizontal",
        }
        iup.Append(a[1], txt[1])
        iup.Map(txt[1])
        iup.Refresh(a[1])
        iup.Append(hrbox, a[1])
        iup.Refresh(hrbox)
    end
    

    You actually need to map the newly created hbox. The children will be automatically mapped in the same time. Calling refresh one time is enough.

    local iup = require("iuplua")
    
    local Button = iup.button{TITLE="Add"}
    local hrbox  = iup.vbox{Button}
    local Frame  = iup.dialog{hrbox,SIZE="THIRDxTHIRD"}
    
    function wa()
        local txt = {}
        local a = {}
        a[1] = iup.vbox{}
        txt[1] = iup.text{
          multiline = "NO",
          padding = "5x5",
          size="100x15",
          scrollbar="Horizontal",
        }
        iup.Append(a[1], txt[1]) -- Append the text to the new-box
        iup.Append(hrbox, a[1])  -- Append the new-box to the main box
        iup.Map(a[1])            -- Map the new-box and its children
        iup.Refresh(hrbox)       -- Re-calculate the layout
    end
    
    Button.action = function (Ih)
      wa()
    end
    
    Frame:show()
    iup.MainLoop()