#Resizing Hello World in NCurses.html
>ncurses is very moody about SIGWINCH, more specifically some terminal emulators will not
update their terminfo correctly if its handled, while in others interrupting getch
causes it to return the error code ERR
>NOTE: BELOW is NOT the correct way to do it; scroll further for the correct way
{@begin=c@
// @BAKE g++ $@ -o ncurses_faulty_resize_pufka_example.out $(pkg-config --cflags --libs ncurses)
#include
#include
int scr_h, scr_w;
WINDOW* myWindow;
char greeting[] = "Hello Ncurses!";
void display(int sig = 0){
if(sig != 0){
delwin(myWindow);
endwin();
refresh();
clear();
}
getmaxyx(stdscr, scr_h, scr_w);
myWindow = newwin(3, ( sizeof(greeting)-1 ) + 4,
( scr_h / 2 ) - 1, (scr_w - ( sizeof(greeting)-1) ) / 2);
refresh();
box(myWindow, 0, 0);
mvwaddstr(myWindow, 1, 2, greeting);
wrefresh(myWindow);
}
signed main(){
initscr();
noecho();
curs_set(0);
signal(SIGWINCH, display);
display();
while(1){}
endwin();
return 0;
}
@end=c@}
>the correct way to do it is to handle KEY_RESIZE, which is a special input read by wgetch()
>KEY_RESIZE does not require keypad()
>KEY_RESIZE seems to litter the input buffer, so it is advised to flush it afterwards with flushinp();
>NOTE: the following IS the intended way to do it; however, its still broken in some terminals
{@begin=c@
// @BAKE g++ $@ -o $*.out $(pkg-config --cflags --libs ncurses)
#include
#include
int scr_h, scr_w;
WINDOW* myWindow = NULL;
char greeting[] = "Hello Ncurses!";
void display(){
if(myWindow){
delwin(myWindow);
endwin();
erase();
refresh();
clear();
}
getmaxyx(stdscr, scr_h, scr_w);
myWindow = newwin(3, ( sizeof(greeting)-1 ) + 4,
( scr_h / 2 ) - 1, (scr_w - ( sizeof(greeting)-1) ) / 2);
refresh();
box(myWindow, 0, 0);
mvwaddstr(myWindow, 1, 2, greeting);
wrefresh(myWindow);
}
signed main(){
initscr();
noecho();
curs_set(0);
display();
while(1){
if(wgetch(stdscr) == KEY_RESIZE){
display();
flushinp();
}
}
endwin();
return 0;
}
@end=c@}