Question

I simply want to make a box window that borders around the terminal. All I'm basically asking is...

Say I have a window:

window(h,w,y,x);

y = LINES +1
x = COLS +1

How do I make it so that h and w are like MAX_X -1 or MAX_Y -1

So that the box that I create outlines the terminal? and how would I fill this box with a certain color?

Was it helpful?

Solution

You can just use the box() function to draw a border around the edge of your window, you don't need to know the height and width to do it. Here's a simple example, with a white border on a blue background:

#include <stdlib.h>
#include <curses.h>

#define MAIN_WIN_COLOR 1

int main(void) {

    /*  Create and initialize window  */

    WINDOW * win;
    if ( (win = initscr()) == NULL ) {
        fputs("Could not initialize screen.", stderr);
        exit(EXIT_FAILURE);
    }


    /*  Initialize colors  */

    if ( start_color() == ERR || !has_colors() || !can_change_color() ) {
        delwin(win);
        endwin();
        refresh();
        fputs("Could not use colors.", stderr);
        exit(EXIT_FAILURE);
    }

    init_pair(MAIN_WIN_COLOR, COLOR_WHITE, COLOR_BLUE);
    wbkgd(win, COLOR_PAIR(MAIN_WIN_COLOR));


    /*  Draw box  */

    box(win, 0, 0);
    wrefresh(win);


    /*  Wait for keypress before ending  */

    getch();


    /*  Clean up and exit  */

    delwin(win);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}

If you want to know the dimensions of the window anyway, you can use ioctl() like so:

#include <sys/ioctl.h>

void get_term_size(unsigned short * height, unsigned short * width) {
    struct winsize ws = {0, 0, 0, 0};
    if ( ioctl(0, TIOCGWINSZ, &ws) < 0 ) {
        exit(EXIT_FAILURE);
    }

    *height = ws.ws_row;
    *width = ws.ws_col;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top