cncurses

Background color of text bleeding over to border symbols (c, ncurses)


Window border symbols pick up background color that only the text should have. If border symbols are removed background color is cut short at the end of the text instead of covering the whole window width. What am I missing and how should I go about fixing it?

If not clear, since stackoverflow is nagging me for details, my intention is to have the background color of the text in each window to cover the whole width of the window on the given line.

#include <ncurses.h>

typedef struct ncwin
{
    int startx;
    int starty;
    int width;
    int height;
    WINDOW *win;
} ncwin;


WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);

int main(int argc, char *argv[])
{   ncwin win[3];
    int ch, i;

    initscr();          /* Start curses mode        */
    cbreak();           /* Line buffering disabled, Pass on
                     * everty thing to me       */
    keypad(stdscr, TRUE);       /* I need that nifty F1     */
    refresh();

    start_color();
    init_pair(1, COLOR_RED, COLOR_BLACK);
    init_pair(2, COLOR_BLUE, COLOR_BLACK);
    init_pair(3, COLOR_GREEN, COLOR_BLACK);

    for (i = 0; i < 3; i++)
    {
        win[i].height = LINES;
        win[i].width =  (COLS / 6) * (i + 1);
        win[i].starty = 0;
        win[i].startx = (i == 0) ? 0 : win[i - 1].startx + win[i - 1].width + 1;
        win[i].win = create_newwin(win[i].height, win[i].width, win[i].starty, win[i].startx);

        wattron(win[i].win, COLOR_PAIR(i + 1));
        wprintw(win[i].win, " hello!\n");
        wattron(win[i].win, COLOR_PAIR(i + 1));
        wrefresh(win[i].win);
    }

    while((ch = getch()) != KEY_F(1));
        
    for (i = 0; i < 3; i++)
        destroy_win(win[i].win);

    refresh();
    endwin();
    return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx)
{
    WINDOW *local_win;

    local_win = newwin(height, width, starty, startx);
    box(local_win, 0, 0);
    wrefresh(local_win);

    return local_win;
}

void destroy_win(WINDOW *local_win)
{   
    wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
    wrefresh(local_win);
    delwin(local_win);
}

Solution

  • To solve this, you need two windows: