NAME=PopupMenu
AUTHOR=
ITEM=function PopupMenu$(options$, width, bgColor$, textColor$, selBackColor$, selTextColor$)
DESCRIPTION=Function creates a dialog window that works as a popup menu. The last four parameters specify the colour scheme, if these are empty strings, a default white-black-darkblue scheme is used. Function returns the text of the menu option selected, or an empty string if nothing was selected.
OUTSIDE CODE=

function PopupMenu$(options$, width, bgColor$, textColor$, selBackColor$, selTextColor$)
    'arguments:
    'options$ - comma-separated list of menu options
    'width - window-width, default = 100
    'bgColor$ - background color of the dialog
    'textColor$ - color of inactive text
    'selBackColor$ - backcolor of active, selected text
    'selTextColor$ - color of active, selected text
    'NOTE: colors are either a string of rgb values, one of the windows colours or
        'empty string (use default colour scheme)
    while word$(options$, count+1, ",") <> ""
        count = count+1
    wend
    height = count*20+38
    width = int(width) : if width < 100 then width = 100
    if bgColor$ = "" then bgColor$ = "white"
    if textColor$ = "" then textColor$ = "black"
    if selBackColor$ = "" then selBackColor$ = "darkblue"
    if selTextColor$ = "" then selTextColor$ = "white"
    WindowHeight = height
    WindowWidth = width
    UpperLeftX = MouseX
    UpperLeftY = MouseY
    graphicbox #popup.graph, 0, 0, width, height
    open title$ for dialog_modal_nf as #popup
    #popup, "trapclose [popupDlgCancel]"
    #popup, "font ms_sans_serif 16 9"
    #popup.graph, "down; fill "; bgColor$
    #popup.graph, "color "; textColor$; "; backcolor "; bgColor$
    for i = 1 to count
        #popup.graph, "place 4 "; i*20 - 2
        #popup.graph, "\"; word$(options$, i, ",")
    next i
    #popup.graph, "flush"
    #popup.graph, "when mouseMove [popupDlgMove]"
    #popup.graph, "when leftButtonDown [popupDlgSelect]"
    wait

[popupDlgMove]
    this = (MouseY-3)/20 : if this >= 0 then this = this + 1
    this = int(this)
    if this <> selection then
        #popup.graph, "backcolor "; bgColor$; "; color "; bgColor$
        #popup.graph, "place 2 "; selection*20 - 16; "; boxfilled "; width-12; " "; selection*20+2
        #popup.graph, "color "; textColor$
        #popup.graph, "place 4 "; selection*20 - 2
        #popup.graph, "\"; word$(options$, selection, ",")
        if this > 0 and this <= count then
            #popup.graph, "backcolor "; selBackColor$; "; color "; selBackColor$
            #popup.graph, "place 2 "; this*20 - 16; "; boxfilled "; width-12; " "; this*20+2
            #popup.graph, "color "; selTextColor$
            #popup.graph, "place 4 "; this*20 - 2
            #popup.graph, "\"; word$(options$, this, ",")
        end if
        selection = this
    end if
    wait
[popupDlgSelect]
    this = (MouseY-3)/20 : if this >= 0 then this = this + 1
    this = int(this)
    if this > count or this < 1 then wait
    PopupMenu$ = word$(options$, this, ",")
[popupDlgCancel]
    close #popup
    end function
