'
' Demo shamelessly ripped from the NCURSES PROGRAMMING HOWTO:
'   http://tldp.org/HOWTO/NCURSES-Programming-HOWTO
'
' Ported to BaCon in June 2010 - PvE (GPLv3)
'
'------------------------------------------------------------------

INCLUDE "curses.bac"
INCLUDE "form.bac"

DECLARE field[3]
DECLARE my_form
DECLARE ch TYPE int

' Initialize curses, get stdscr here!
stdscr = initscr()
cbreak()
noecho()
keypad(stdscr, TRUE)

' Initialize the fields
field[0] = new_field(1, 10, 4, 18, 0, 0)
field[1] = new_field(1, 10, 6, 18, 0, 0)
field[2] = 0

' Set field options
set_field_back(field[0], A_UNDERLINE)
field_opts_off(field[0], O_AUTOSKIP)

set_field_back(field[1], A_UNDERLINE)
field_opts_off(field[1], O_AUTOSKIP)

' Create the form and post it
my_form = new_form(ADDRESS(field))
post_form(my_form)
refresh()

mvprintw(4, 10, "Value 1:")
mvprintw(6, 10, "Value 2:")
refresh()

' Loop through to get user requests, F2 exits
WHILE ch <> KEY_F(2)

    ch = getch()

    SELECT ch
        CASE KEY_DOWN
            ' Go to next field
            form_driver(my_form, REQ_NEXT_FIELD)
            ' Go to the end of the present buffer
            ' Leaves nicely at the last character
            form_driver(my_form, REQ_END_LINE)
        CASE KEY_UP
            ' Go to previous field
            form_driver(my_form, REQ_PREV_FIELD)
            form_driver(my_form, REQ_END_LINE)
        DEFAULT
            ' If this is a normal character, it gets printed
            form_driver(my_form, ch)
    END SELECT
WEND

' Un post form and free the memory
unpost_form(my_form)
free_form(my_form)
free_field(field[0])
free_field(field[1])

endwin()