NSPredator
NSPredator

Reputation: 474

How do I put a subwindow within a subwindow?

I am using NCurses to try and get the desired output: I am trying to put a blue box inside the white space

However with the following code:

int main(void)
{
     WINDOW *white_space,*red_space,*blue_box;
     int maxx,maxy;

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


     white_space = subwin(stdscr,10,76,6,2);
     red_space = subwin(stdscr,1,80,23,0);
     getmaxyx(white_space,maxy,maxx);
     blue_box = subwin(white_space,maxy-1,maxx-1,8,20);
     if(white_space == NULL)
     {
     addstr("Unable to create subwindow\n");
     endwin();
     return 1;
     }


     bkgd(COLOR_PAIR(1));
     addstr("Master window");
     wbkgd(white_space,COLOR_PAIR(2));
     mvwprintw(white_space, 0, 0, "%46s", "White space");
     wbkgd(blue_box,COLOR_PAIR(4));
     wbkgd(red_space,COLOR_PAIR(3));
     waddstr(red_space,"Alert area");
     wrefresh(white_space);
     wrefresh(stdscr);


     refresh();
     getch();

     endwin();
     return 0;
}

I get the following output: enter image description here

Is it possible to create a subwindow within a subwindow?

Thanks

Upvotes: 1

Views: 493

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54505

ncurses (any non-buggy version of curses) supports subwindows, as noted in the manual.

In the given example, there are a few problems noticed:

 white_space = subwin(stdscr,10,76,6,2);
 red_space = subwin(stdscr,1,80,23,0);
 getmaxyx(white_space,maxy,maxx);
 blue_box = subwin(white_space,maxy-1,maxx-1,8,20);

For instance:

  • the initial size given to white_space does not take into account the actual screen-size (which could be narrower than 80 columns)
  • the size asked for blue_box is only slightly smaller than white_space, but starts far enough to the right that the window could not (in 80 columns) be shown. If so, no window is created.

There are several programs in ncurses-examples using subwin and the related derwin, which may be useful for comparison.

Upvotes: 1

Related Questions