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;
}
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).
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 ofstdscr
and n blank lines are inserted at the bottom. If n is negative thenstdscr
is scrolled down. n blank lines are inserted at the top ofstdscr
and n lines are lost from the bottom.
So you'll have to manually save input and reprint it when scrolling.
POSIX does not cover this case, because the curses document is not part of POSIX. The Open Group happens to maintain documentation for both:
signal.h
(note that SIGWINCH
is absent)As noted in the manual page for resizeterm
, you should not call that function from within a signal handler, because it calls "unsafe" functions. The topic of "unsafe" functions is discussed in several places; that in gcc's documentation would do for a start.
Regarding documentation, @larsmans appears to be quoting from scroll(3)
, but not citing comparable links for ncurses and "POSIX". For what it's worth:
Back to OP's question:
resizeterm
nor of resize_term
. It is not stated, but presumably OP resized the terminal window and the program did not respond. The manual page for resizeterm
is clear enough that ncurses does not cause the terminal to resize. For that (on some terminals), one can use the -s
option of resize
(a utility program for xterm
). If successful, that resizes the terminal, which in turn sends a SIGWINCH
.
ncurses has a predefined signal handler for that, but at the application level, handling KEY_RESIZE
is the recommended way. There are several programs in ncurses-examples which do this.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With