Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

ncurses

tui

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?

like image 422
Mario Gil Avatar asked Sep 28 '22 08:09

Mario Gil


2 Answers

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

like image 135
Gerald Zehetner Avatar answered Dec 31 '22 11:12

Gerald Zehetner


The mvprintw function first attempts to move the cursor to the indicated position, e.g., with wmove. The wmove function never causes scrolling, and the attempt to move it past the bottom line of the window fails (quoting from wmove manual):

These routines return ERR upon failure and OK (SVr4 specifies only "an integer value other than ERR") upon successful completion.

Specifically, they return an error if the window pointer is null, or if the position is outside the window.

Instead, to do scrolling you must write text with a newline character (i.e., '\n') at the bottom of the window. wprintw is useful; in turn it calls waddch (quoting from the latter's manual):

The addch, waddch, mvaddch and mvwaddch routines put the character ch into the given window at its current window position, which is then advanced. They are analogous to putchar in stdio(3). If the advance is at the right margin:

...

At the bottom of the current scrolling region, and if scrollok is enabled, the scrolling region is scrolled up one line.

If ch is a tab, newline, or backspace, the cursor is moved appropriately within the window:

...

Newline does a clrtoeol, then moves the cursor to the window left margin on the next line, scrolling the window if on the last line.

like image 34
Thomas Dickey Avatar answered Dec 31 '22 11:12

Thomas Dickey