Resize terminal and scrolling problem with ncurses

ubuntiano picture ubuntiano · Jan 19, 2011 · Viewed 12.2k times · Source

I'm programming in C using ncurses libraries (it's the first time) and I've two problems. I'm on ubuntu with the default terminal (gnome terminal).

1) I need to resize the terminal. I used resizeter() and resize_term(), but they fail.

2) I use scrollok() function and the problem is that I lose scrolled lines (when I get back with wscrl(), there are blank lines).

#include <ncurses.h>

int main() {

WINDOW *win, *win2;

int i;
char c;

initscr();
cbreak();
noecho();

win=newwin(8,20,1,1);
box(win,0,0);
win2=newwin(6,18,2,2);
scrollok(win2,1);
wrefresh(win);
wrefresh(win);

for(i=0;i<15;i++){
    c=wgetch(win2);
    if(c=='u'){
        wscrl(win2,-1);
        wrefresh(win2);
    }
    else{
        wprintw(win2,"%c\n",c);
        wrefresh(win2);
    }
}

delwin(win);
delwin(win2);
endwin();

return 0;
}

Answer

Fred Foo picture Fred Foo · Jan 19, 2011
  1. You can't resize the terminal window from ncurses. The functions you mention resize the part of the terminal window that is painted on by curses. The idea is you catch the SIGWINCH signal and call resizeterm in the handler when the user resizes the window from outside the application (using the mouse, probably).

  2. This is intended behavior, though poorly documented in ncurses and in the Unix standard/POSIX. NetBSD's curses docs state it explicitly:

    If n is positive then stdscr is scrolled up. n lines are lost from the top of stdscr and n blank lines are inserted at the bottom. If n is negative then stdscr is scrolled down. n blank lines are inserted at the top of stdscr and n lines are lost from the bottom.

    So you'll have to manually save input and reprint it when scrolling.