Home > Net >  Box passed window in ncurses.h
Box passed window in ncurses.h

Time:10-01

I'm having a hard time understanding the problem with a window in ncurses. I created a layout of several windows and want to have a function that will border a window. Look like this:

int main() {
    use_env(TRUE);
    initscr();
    start_color();
    cbreak();
    noecho();
    curs_set(0);

    WINDOW *win_corner = newwin(WCLINES, WCCOLS, WCX, WCY);
    choose_lvl(win_corner); 
    ...
}


int choose_lvl(WINDOW *win) {
    box(win, 0, 0);
    wrefresh(win);
    refresh();

    int c, curr_option = 0;
    init_pair(1, COLOR_CYAN, COLOR_BLACK);
    attron(COLOR_PAIR(1));
    mvwprintw(win, 1, 1, "Choose your level");
    ...
}

It seems like box() doesnt have any effect at all - what could it be? My current theory is to suppose something wrong with the coordinates of the window.

CodePudding user response:

refresh() refreshes the updates on stdscr. newwin() creates an independant window. A refresh on stdscr does not update the alternate window. The same without refresh() in choose_lvl() function would work fine as you would force update only on the alternate window.
You can also create a sub-window of stdscr with subwin() to make refresh() update the whole hierarchy:

#include <unistd.h>
#include <curses.h>

#define WCLINES 10
#define WCCOLS 80

#define WCX 4
#define WCY 4

int choose_lvl(WINDOW *win) {
    box(win, 0, 0);
    //wrefresh(win);
    //refresh();

    int c, curr_option = 0;
    init_pair(1, COLOR_CYAN, COLOR_BLACK);
    attron(COLOR_PAIR(1));
    mvwprintw(win, 1, 1, "Choose your level");

    refresh();

    return 0;
}

int main() {
    use_env(TRUE);
    initscr();
    start_color();
    cbreak();
    noecho();
    curs_set(0);

    //WINDOW *win_corner = newwin(WCLINES, WCCOLS, WCX, WCY);
    WINDOW *win_corner = subwin(stdscr, WCLINES, WCCOLS, WCX, WCY);
    choose_lvl(win_corner); 

    // quick&dirty pause to suspend program execution
    // right after the windows display
    pause();
    
    return  0;
}

Execution:

$ gcc win.c -lncurses
$ ./a.out

The result is: enter image description here

  • Related