/*
* bug.c
*
* This is a program that shows the incompatibility between
* the version included in Mandrake 10.1 ( NCURSES_VERSION_PATCH = 20040529 )
* and the official releases of ncurses: ncurses 5.3 (20021012)
* and ncurses 5.4 (20040208).
*
* When built against any of the official releases, the fields are
* correctly filled with the data , according to the current row.
*
* When built against the broken version (20040529), the fields
* remain empty even though they should be filled by the
* filldata(int line) function.
*
* I haven't written any comments in the code. Sorry.
*
* ---------------------------------------------------------------
* For building the program against ncurses 20040529 i used :
*
* gcc -o bug bug.c -lform -lpanel -lncurses
*
* For building the program against ncurses 5.4 (installed in /usr/local):
* i used:
*
* gcc -o bug bug.c -L/usr/local/lib -lform -lpanel -lncurses
*/
#include
#include
WINDOW *frm_win;
FORM *frm;
FIELD *frm_fields[5];
PANEL *frm_panel;
char data[10][3][50] = {
{"1","aaaaaaaaa","123"},
{"2","bbbbbbbbb","123"},
{"3","ccccccccc","123"},
{"4","ddddddddd","123"},
{"5","eeeeeeeee","123"},
{"6","fffffffff","123"},
{"7","ggggggggg","123"},
{"8","hhhhhhhhh","333"},
{"9","iiiiiiiii","123"},
{"10","jjjjjjjjj","555"}
};
void init_ncurses()
{
initscr();
noecho();
cbreak();
keypad(stdscr,TRUE);
notimeout(stdscr,TRUE);
start_color();
init_pair(1,COLOR_CYAN,COLOR_BLACK);
init_pair(2,COLOR_BLACK,COLOR_WHITE);
init_pair(3,COLOR_GREEN,COLOR_BLACK);
init_pair(4,COLOR_BLACK,COLOR_CYAN);
init_pair(5,COLOR_WHITE,COLOR_RED);
}
void end_ncurses()
{
endwin();
}
void create_form()
{
int nFields;
int i,j;
i = 0;
/* field 1 */
{
frm_fields[i]=new_field(1, 10, 0, 0, 0, 0);
set_field_back(frm_fields[i],COLOR_PAIR(4));
field_opts_off(frm_fields[i],O_BLANK);
field_opts_off(frm_fields[i],O_AUTOSKIP);
i++;
}
/* field 2 */
{
frm_fields[i]=new_field(1, 20, 0, 11, 0, 0);
set_field_back(frm_fields[i],COLOR_PAIR(4));
field_opts_off(frm_fields[i],O_BLANK);
field_opts_off(frm_fields[i],O_AUTOSKIP);
i++;
}
/* field 3 */
{
frm_fields[i]=new_field(1, 5, 0, 32, 0, 0);
set_field_back(frm_fields[i],COLOR_PAIR(4));
field_opts_off(frm_fields[i],O_BLANK);
field_opts_off(frm_fields[i],O_AUTOSKIP);
/* use scrolling for this field */
{
field_opts_off(frm_fields[i],O_STATIC);
set_max_field (frm_fields[i],20);
}
i++;
}
frm_fields[i]=NULL;
frm = new_form(frm_fields);
scale_form(frm,&i,&j); // i=rows j=columns
frm_win=newwin(i,j,0,0);
frm_panel=new_panel(frm_win);
form_opts_off(frm,O_NL_OVERLOAD);
form_opts_off(frm,O_BS_OVERLOAD);
set_form_win(frm,frm_win);
post_form(frm);
}
void redraw()
{
top_panel(frm_panel); update_panels(); doupdate();
wrefresh(stdscr); wrefresh(frm_win);
}
void filldata(int l)
{
int i;
char t[40];
wclear(stdscr);
strncpy(t,data[l][0],10); set_field_buffer(frm_fields[0],0,t);
strncpy(t,data[l][1],20); set_field_buffer(frm_fields[1],0,t);
strncpy(t,data[l][2],20); set_field_buffer(frm_fields[2],0,t);
for (i=0; i<10; i++) {
strncpy(t,data[i][0],10); mvprintw(i,0,t);
strncpy(t,data[i][1],20); mvprintw(i,11,t);
strncpy(t,data[i][2],5); mvprintw(i,32,t);
}
}
int main()
{
int key;
int line;
init_ncurses();
create_form();
line=0;
filldata(0);
key = getch();
while (key!='q') {
switch(key) {
case 9: /* next field */
form_driver(frm,REQ_BEG_FIELD);
form_driver(frm,REQ_NEXT_FIELD);
break;
case '`': /* previous field */
form_driver(frm,REQ_BEG_FIELD);
form_driver(frm,REQ_PREV_FIELD);
break;
case KEY_DOWN:
if (line<9) {
line++;
mvwin(frm_win,line,0);
filldata(line);
}
break;
case KEY_UP:
if (line>0) {
line--;
mvwin(frm_win,line,0);
filldata(line);
}
break;
case KEY_LEFT: form_driver(frm,REQ_PREV_CHAR);break;
case KEY_RIGHT: form_driver(frm,REQ_NEXT_CHAR);break;
case KEY_DC:
form_driver(frm,REQ_DEL_CHAR);
break;
case KEY_BACKSPACE:
form_driver(frm,REQ_DEL_PREV);
break;
default:
form_driver(frm,key);
}
redraw();
key=getch();
}
end_ncurses();
}