How to scroll a window (other than stdscreen) in ncurses?

Mario Gil picture Mario Gil · Apr 10, 2015 · Viewed 9.4k times · Source

I saw this answer researching to solve my problem https://stackoverflow.com/a/8407120/2570513, but, it works only on stdscreen. I implemented this:

#include <ncurses.h>

int main(void)
{
    int i = 2, height, width;
    WINDOW *new;

    initscr();
    getmaxyx(stdscr, height, width);
    new = newwin(height - 2, width - 2, 1, 1);

    scrollok(new,TRUE);

    while(1)
    {
        mvwprintw(new, i, 2, "%d - lots and lots of lines flowing down the terminal", i);
        ++i;
        wrefresh(new);
    }

    endwin();
    return 0;
}

But it doesn't scroll. Whats wrong?

Answer

Gerald Zehetner picture Gerald Zehetner · Apr 10, 2015

Thats because you place the string on a certain position in the window by using mvwprintw, so when i gets bigger than the windowsize it's just not printed on the screen.

In order to use scolling you need to use wprintw which puts the text on the current cursor position.

#include <ncurses.h>

int main(void)
{
    int i = 2, height, width;
    WINDOW *new;

    initscr();
    getmaxyx(stdscr, height, width);
    new = newwin(height - 2, width - 2, 1, 1);

    scrollok(new,TRUE);

    while(1)
    {
        wprintw(new, "%d - lots and lots of lines flowing down the terminal\n", i);
        ++i;
        wrefresh(new);
    }

    endwin();
    return 0;
}

If you want to fill a window with content and then use the arrow keys to scroll up and down, you should have a look at Pads