select mode and radio mode popup menus
#1
I have made a library file for better handeling of popup menus for my yoshi project. This library will allow one to make a popup menu that marks multiple selections or only one to use the popup as a radio selectuion.

test program:

Code:
import popup
window open 100,100 to 310,250, "test", "test"

popup=new_popup("test_name", "test", 1)//radio selection
add_popup_selection$(popup,1,"First selection")
add_popup_selection$(popup,2,"Second selection")
add_popup_selection$(popup,3,"Some other Selection")
add_popup_selection$(popup,4,"Third selection")
add_popup_selection$(popup,5,"Fourth selection")
a$=popup_select$(popup,"Fourth selection")


button 10,20 to 200,40, "popupbutton", "Fourth selection  ▾", "test"

popup1=new_popup("test_name1", "test", 3)//selection mode
add_popup_selection$(popup1,1,"First selection")
add_popup_selection$(popup1,2,"Second selection")
add_popup_selection$(popup1,3,"Some other Selection")
add_popup_selection$(popup1,4,"Third selection")
add_popup_selection$(popup1,5,"Fourth selection")
a$=popup_select$(popup1,"Fourth selection")

button 10,60 to 200,80, "newbutton", "Please make selections ▾", "test"

inloop = true
while(inloop)
    msg$=message$
    if msg$<>"" print msg$
    if instr(msg$,"Quit") inloop=false
    if instr(msg$,"popupbutton") showpopup()
    if instr(msg$,"newbutton") shownewpopup()
wend    

print getselected$(popup)


end
sub showpopup()
selection$=popupdisplay$(10,20,popup)
if selection$<>"" then
    selection$=trim$(selection$)
    a$=popup_select$(popup,selection$)
    if a$="ok" OPTION SET "popupbutton", "Label", selection$+" ▾"
endif


end sub

sub shownewpopup()
selection$=popupdisplay$(10,20,popup1)

if selection$<>"" then
    selection$=trim$(selection$)
    if left$(selection$,3)="√" then // remove the mark to see the real selection
    selection$ = right$(selection$,len(selection$)-4)
    endif
    a$=popup_select$(popup1,selection$)
    
endif


end sub

The library file:

Code:
////////////////////////////////////////////////////////
export sub new_popup(name$, view$, radio)
static size
size=size+1
dim popup$(size,12,1) // name, view, selections ( up to 10)
dim  popup(size,2) //radio flag, 1=radio, 0=not radio
popup$(size,1,0)=name$
popup$(size,2,0)=view$
popup(size,1)=radio
return size
end sub
////////////////////////////////////////////////////////

export sub add_popup_selection$(popup,number,selection$)
popup$(popup,number+2,0)=selection$
return "ok"
end sub

////////////////////////////////////////////////////////

export sub popup_select$(x,selection$)
local i
for i=3 to 12
    if popup$(x,i,0)=selection$ then
        if  popup(x,1)=1 then
            popup(x,2)=i
            return "ok"
        else
            if popup$(x,i,1)="√ " then
                popup$(x,i,1)=""
            else
                popup$(x,i,1)="√ "
            endif
            return "ok"
        endif    
    endif
next



return "error"
end sub

////////////////////////////////////////////////////////

export sub popupdisplay$(x,y,popup)
local i, MenuItems$
MenuItems$=""
if popup(popup,1)=1 then
    for i=3 to 10
        if popup$(popup,i,0)<>"" then
            if popup(popup,2)=i  then
                MenuItems$=MenuItems$+"√ "+popup$(popup,i,0)+"|"
            else
                MenuItems$=MenuItems$+"  "+popup$(popup,i,0)+"|"
            endif
        endif
    next
endif    
if popup(popup,1)=0 then

    for i=3 to 10
        if popup$(popup,i,0)<>"" then
            MenuItems$=MenuItems$+popup$(popup,i,0)+"|"
        endif
    next
endif    
if popup(popup,1)=3 then
    for i=3 to 10
        if popup$(popup,i,0)<>"" then
            MenuItems$=MenuItems$+popup$(popup,i,1)+popup$(popup,i,0)+"|"
        endif
    next
endif

MenuItems$=left$(MenuItems$,len(MenuItems$)-1)
Selected$ = POPUPMENU x,y, MenuItems$, popup$(popup,2,0)
return Selected$
end sub
////////////////////////////////////////////////////////

export sub getselected$(x)
return popup$(x,popup(x,2),0)
end sub

name the library file popup.yab and place it in ~/config/settings/yab to use the code.
Reply


Messages In This Thread
select mode and radio mode popup menus - by bbjimmy - 04-10-2016, 09:36 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)
Free Web Hosting