I've done drop-down menus before, and so have many ppl. I wrote a new one from scratch, I think the code is much smaller than what I used to have and easier to use.
The "check" variable isn't used yet, but will be for adding checkmarks next to menu items.
-1 = no check option at all
0 = not checked
1 = checked
global _mouseFlag = 0
type Menu
name as string
active as boolean
width as integer
count as integer
endtype
type MenuItem
parentID as integer
item as string
enabled as boolean
check as integer
endtype
dim menus() as Menu
dim menuItems() as MenuItem
MENU_FILE = addMenu("File")
MENU_EDIT = addMenu("Edit")
MENU_FILTER = addMenu("Filter")
addMenuItem(MENU_FILE,"New",1,-1)
addMenuItem(MENU_FILE,"Open",1,-1)
addMenuItem(MENU_FILE,"Save",0,-1)
addMenuItem(MENU_FILE,"Exit",1,-1)
addMenuItem(MENU_EDIT,"Undo",1,-1)
addMenuItem(MENU_EDIT,"Copy",1,-1)
addMenuItem(MENU_EDIT,"Paste",0,-1)
sync on
sync rate 60
repeat
cls
set cursor 0,200
_mc = mouseclick()
if _mc = 0 then _mouseFlag = 0
v$ = _handleMenus$(_mc)
if v$ <> "" then value$ = v$
if v$ = "File-Exit" then exit
ink rgb(255,255,255),0
text 1,400,value$
sync
until spacekey()
function addMenu(name as string)
m as Menu
m.name = name
array insert at bottom menus()
count = array count(menus())
menus(count) = m
endfunction count
function addMenuItem(pid as integer, item as string, enabled as boolean, check as integer)
m as MenuItem
m.parentID = pid
m.item = item
m.enabled = enabled
m.check = check
array insert at bottom menuItems()
menuItems(array count(menuItems())) = m
rem make sure menu is drawn wide enough to handle this item's name
width = text width(item)+6
if width > menus(pid).width then menus(pid).width = width
menus(pid).count = menus(pid).count + 1
endfunction
function _handleMenus$(mc as integer)
v$ = ""
offsetX = 0
active = -1
activeOffset = 0
rem text height, vertical spacing
tw = 16
for i = 0 to array count(menus())
width = text width(menus(i).name)
if mc = 1
if mousex() >= offsetX and mousex() <= offsetX+width+6 and mousey() >= 0 and mousey() <= tw
if _mouseFlag = 0
_mouseFlag = 1
if menus(i).active = 0
menus(i).active = 1
else
menus(i).active = 0
endif
for t = 0 to array count(menus())
if t <> i then menus(t).active = 0
next t
endif
endif
endif
if menus(i).active = 1
active = i
activeOffset = offsetX
ink rgb(0,180,250),0
else
ink rgb(192,192,192),0
endif
box offsetX,0,offsetX+width+6,tw
ink 0,0
text offsetX+3,0,menus(i).name
offsetX = offsetX + width + 6
next i
if active > -1
ink rgb(192,192,192),0
box activeOffset,tw,activeOffset+menus(active).width,tw+menus(active).count*tw
offsetY = 0
ink 0,0
for i = 0 to array count(menuItems())
if menuItems(i).parentID = active
inc offsetY
if mousex() > activeOffset and mousex() < activeOffset+menus(active).width and mousey() > offsetY*tw and mousey() < (offsetY+1)*tw
ink rgb(0,180,250),0
box activeOffset+1,offsetY*tw+1,activeOffset+menus(active).width-1,(offsetY+1)*tw-1
rem if this item was clicked
if mc = 1
if _mouseFlag = 0
_mouseFlag = 1
if menuItems(i).enabled = 1
v$ = menus(active).name + "-" + menuItems(i).item
menus(active).active = 0
exitfunction v$
endif
endif
endif
endif
if menuItems(i).enabled = 1
ink 0,0
else
ink rgb(120,120,120),0
endif
text activeOffset+3,offsetY*tw,menuItems(i).item
endif
next i
endif
endfunction v$
"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike