So, I've been messing around with form.h from ncurses, this is great, the menu appears to work. But, there's just something missing. What exactly do I do with the data that has been inputted? I have scoured the Internet looking for some form of example, and in none of the references can I find anyone who's using the form data for anything. Sure we have a great way to input text into fields in a form, but then afterwards, we destroy the data by freeing up the form and then the fields, and then we never even touch. We definitely don't use it for anything.
How exactly can I use the fields? Are we talking field_buffer?
#include <form.h>
int main() {
FIELD *field[4];
FORM *my_form;
int ch;
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize the fields */
field[0] = new_field(1, 30, 1, 25, 0, 0);
field[1] = new_field(1, 30, 3, 25, 0, 0);
field[2] = new_field(1, 30, 4, 25, 0, 0);
field[3] = new_field(1, 30, 5, 25, 0, 0);
/* Set field options */
for (int i = 0; i < 4; i++)
{
set_field_back(field[i], A_UNDERLINE);
field_opts_off(field[i], O_AUTOSKIP);
}
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
mvprintw(0, 20, "");
mvprintw(1, 10, "1:");
mvprintw(2, 20, "");
mvprintw(3, 10, "2:");
mvprintw(4, 10, "3:");
mvprintw(5, 10, "4:");
mvprintw(6, 10, "Send (F1)");
refresh();
form_driver(my_form, REQ_FIRST_FIELD);
/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
case KEY_BACKSPACE:
/* Delete last character */
form_driver(my_form, REQ_DEL_PREV);
break;
case KEY_DC:
/* Delete contents of field */
form_driver(my_form, REQ_CLR_FIELD);
break;
case KEY_ENTER:
form_driver(my_form, REQ_NEXT_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
form_driver(my_form, REQ_VALIDATION);
for (int i = 0; i < 4; i++)
{
mvprintw(7+i, 20, field_buffer(field[i], 0)); // is this even workable?
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
for (int i = 0; i < 4; i++)
{
free_field(field[i]);
}
endwin();
return 0;
}