* iupButtons for v7

For plugin authors to discuss plugin programming
Post Reply
avatar
quarlton
Famous
Posts: 166
Joined: 26 Feb 2004 13:07
Family Historian: V7
Location: Lincolnshire
Contact:

iupButtons for v7

Post by quarlton »

I've been asked to update a plugin that i wrote in 2016 so that it works with v7.
My stumbling block is around iupButtons which I can't find a solution for.
Any help would be much appreciated.

It fails on for intArgNum, strButton in ipairs(arg) do with "error attempt to index a nil value"
Function

Code: Select all

function iupButtons(strTitle,strMessage,strBoxType,...)
    local intButton = 0   -- Returned value if X Close button is used

    -- Create the GUI labels and buttons
    local lblMessage = iup.label{title=strMessage,expand="YES"}
    local lblLineSep = iup.label{separator="HORIZONTAL"}
    local iupBox = iup.hbox{homogeneous="YES"}
    if strBoxType == "V" then
        iupBox = iup.vbox{homogeneous="YES"}
    end
--THIS IS WHERE IT FAILS
    for intArgNum, strButton in ipairs(arg) do
        local btnName = iup.button{title=strButton,expand="YES",padding="4",
            action=function() intButton=intArgNum return iup.CLOSE end}
            iup.Append(iupBox,btnName)
    end

    -- Create dialogue and turn off resize, maximize, minimize, and menubox except Close button
    local dialogue = iup.dialog{title=strTitle,iup.vbox{lblMessage,lblLineSep,iupBox},
    dialogframe="YES",background="250 250 250",gap="8",margin="8x8"}
    dialogue:show()
    if (iup.MainLoopLevel()==0) then iup.MainLoop() end
        dialogue:destroy()
        return intButton
    end -- function iupButtons

Usage

Code: Select all

--Call the function which will return a value of 1-6 for the button
    intButton = iupButtons("Last updated..","Choose record types..","V","Individuals","Families","Sources","Notes", "Multimedia", "Cancel")

    --Check the values and if not 'Cancel' set the value for strRecordType and the label for column 3 of the display
    if intButton == 1 then strRecordType = "INDI" strRecordLabel = "Person"     end
    if intButton == 2 then strRecordType = "FAM"  strRecordLabel = "Family"     end
    if intButton == 3 then strRecordType = "SOUR" strRecordLabel = "Source"     end
    if intButton == 4 then strRecordType = "NOTE" strRecordLabel = "Note"       end
    if intButton == 5 then strRecordType = "OBJE" strRecordLabel = "Multimedia" end
    if intButton == 6 then return end    --'Cancel' pressed so Exit the plugin
Last edited by tatewise on 09 Dec 2023 14:40, edited 1 time in total.
Reason: Subject iupVuttons to iupButtons
Dave Simpson ~ Boulton, Braham, Carney, Simpson and Jacobs
User avatar
ColeValleyGirl
Megastar
Posts: 5520
Joined: 28 Dec 2005 22:02
Family Historian: V7
Location: Cirencester, Gloucestershire
Contact:

Re: iupButtons for v7

Post by ColeValleyGirl »

From Writing and Maintaining Plugins Compatible with Versions 5, 6 & 7

Code: Select all

--[[Pseudo-argument arg
Pseudo-argument arg was deprecated in Lua 5.1 and removed in Lua 5.2.
Insert the following code into functions which use the ... argument
arg = {...}
arg['n'] = #arg
--]]
-- Example 
local function test(...)
  local arg = {...}
  arg['n'] = #arg
  for i,v in ipairs(arg) do
    print(i,v)
  end
end
Last edited by tatewise on 09 Dec 2023 14:40, edited 1 time in total.
Reason: Subject iupVuttons to iupButtons
avatar
quarlton
Famous
Posts: 166
Joined: 26 Feb 2004 13:07
Family Historian: V7
Location: Lincolnshire
Contact:

Re: iupButtons for v7

Post by quarlton »

Hi Helen

Many thanks for your reply, and I can get it to work but only partially.
The example prints i & v to the console with the correct values.

However, when I try and incorporate it into the code to produce the buttons, I can't get it right.
It displays the grid for the buttons and displays the Title and message correctly, but the buttons are blank.
So this implies that I've gone wrong somewhere.

OLD CODE

Code: Select all

for intArgNum, strButton in ipairs(arg) do
        local btnName = iup.button{title=strButton,expand="YES",padding="4",
            action=function() intButton=intArgNum return iup.CLOSE end}
            iup.Append(iupBox,btnName)
    end
    
    
    -- Create dialogue and turn off resize, maximize, minimize, and menubox except Close button
    local dialogue = iup.dialog{title=strTitle,iup.vbox{lblMessage,lblLineSep,iupBox},
    dialogframe="YES",background="250 250 250",gap="8",margin="8x8"}
    dialogue:show()
    if (iup.MainLoopLevel()==0) then iup.MainLoop() end
        dialogue:destroy()
        return intButton
    end -- function iupButtons
NEW CODE

Code: Select all

local arg = {...}
  arg['n'] = #arg
  for i,v in ipairs(arg) do
   local btnName = iup.button{title=strButton,expand="YES",padding="4",
        action=function() intButton=arg return iup.CLOSE end}
        iup.Append(iupBox,btnName)
  end

    -- Create dialogue and turn off resize, maximize, minimize, and menubox except Close button
    local dialogue = iup.dialog{title=strTitle,iup.vbox{lblMessage,lblLineSep,iupBox},
    dialogframe="YES",background="250 250 250",gap="8",margin="8x8"}
    dialogue:show()
    if (iup.MainLoopLevel()==0) then iup.MainLoop() end
        dialogue:destroy()
        return intButton
    end -- function iupButtons
Any further suggestions would be much appreciated

Dave
Last edited by tatewise on 09 Dec 2023 14:41, edited 1 time in total.
Reason: Subject iupVuttons to iupButtons
Dave Simpson ~ Boulton, Braham, Carney, Simpson and Jacobs
User avatar
ColeValleyGirl
Megastar
Posts: 5520
Joined: 28 Dec 2005 22:02
Family Historian: V7
Location: Cirencester, Gloucestershire
Contact:

Re: iupButtons for v7

Post by ColeValleyGirl »

Dave, where's strButton set :)
Last edited by tatewise on 09 Dec 2023 14:41, edited 1 time in total.
Reason: Subject iupVuttons to iupButtons
User avatar
tatewise
Megastar
Posts: 28488
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: iupButtons for v7

Post by tatewise »

May I help?

Dave you have overcomplicated it.
Keep all your original code and just replace arg in for loop with {...} because it is only used once.
i.e.
for intArgNum, strButton in ipairs({...}) do
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
avatar
quarlton
Famous
Posts: 166
Joined: 26 Feb 2004 13:07
Family Historian: V7
Location: Lincolnshire
Contact:

Re: iupButtons for v7

Post by quarlton »

Hi Helen

strButton is I believe picked up from the input table.
However I'm not really sure - the code is from 'User Interface Buttons Snippet' by Mike and Jane.

Mike
Many thanks, that works a treat.

Thanks to you both.

Dave
Dave Simpson ~ Boulton, Braham, Carney, Simpson and Jacobs
User avatar
ColeValleyGirl
Megastar
Posts: 5520
Joined: 28 Dec 2005 22:02
Family Historian: V7
Location: Cirencester, Gloucestershire
Contact:

Re: iupButtons for v7

Post by ColeValleyGirl »

Dave, for the record, in the original code you had:

Code: Select all

for intArgNum, strButton in ipairs(arg) do
You should have left that unchanged rather than replacing it with

Code: Select all

  for i,v in ipairs(arg) do 
Post Reply