DEV Community

Gurkirat Singh
Gurkirat Singh

Posted on • Updated on

Windows in NCurses (Part 3)

Hey there, if you haven't read Part-2 yet, I would suggest you read it first.

Ncurses provide one main window known as Standard Screen by default, defined as stdscr. It also allows you to make windows that divide the stdscr i.e Main Window.

A window may be as large as entire stdscr or as small as a single character

To declare a window you will have to use WINDOW typed object defined in ncurses.h. and newwin(int height, int width, int starty, int starty) function to initialize

For example,

WINDOW *win = newwin(10, 20, 1,10);
refresh(); // this will refresh "all" the screens
Enter fullscreen mode Exit fullscreen mode

In the real world, you would be working with different windows. So if we want or refresh a specific window, we will be using wrefresh(WINDOW *) function of ncurses.h that takes a window and refresh memory contents related to that window only.

Like mvprintw() (in part 2) is for stdscr, you will have to use window specific function to print in particular window mvwprintw(WINDOW *, int y, int x, “String”, ….);

Ok now everything is done, except one, and that is borders. To add borders to screen there are two functions

  • box(WINDOW*, int v, int h): Function to add borders to window
    • v is the value for vertical borders. Default is 0
    • h is the value for horizontal borders. Default is 0
  • wborder(WINDOW*, int leftChar, int rightChar, int topChar, int bottomChar, int topLeftCorner, int topRightCorner, int bottomLeftCorner, int bottomRightCorner): The most precise function to add borders to a window
#include <ncurses.h>
using namespace std;

int main(int argc, char **argv)
{
    initscr();

    // creating a window;
    // with height = 15 and width = 10
    // also with start x axis 10 and start y axis = 20
    WINDOW *win = newwin(15, 17, 2, 10);
    refresh();

    // making box border with default border styles
    box(win, 0, 0);

    // move and print in window
    mvwprintw(win, 0, 1, "Greeter");
    mvwprintw(win, 1, 1, "Hello");

    // refreshing the window
    wrefresh(win);

    getch();
    endwin();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
sergix profile image
Peyton McGinnis

Ahh, I remember my days in ncurses. I've tried and failed multiple times to write a decent wrapper around it in C++. My problem was always efficiently managing a custom WINDOW stack and allowing global keybindings.

Collapse
 
gansm profile image
Markus Gans

Maybe FINAL CUT would be something for you?

Collapse
 
sergix profile image
Peyton McGinnis

Oh wow, I've never seen that before. That looks awesome and just what I had been looking for. Thanks for sharing!