user3753834
user3753834

Reputation: 369

C- Ncurses, Window not displaying / printing

Ive tried looking for a solution, i just don't know why the window is not displaying. The code is fairly simple and straight forward. Why is this the case? I asked a similar question before but know one seems to have been able to provide the right answer, so I made it a bit simpilar and only included the important stuff.

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    initscr();
    WINDOW* win;
    int height = 10;
    int width = 40;
    int srtheight = 1;
    int srtwidth = 0;
    win = newwin(height, width, srtheight ,srtwidth);
    mvwprintw(win, height/2,width/2,"First line");
    wrefresh(win);
    getch();
    delwin(win);
    endwin();


    return 0;
}

Upvotes: 6

Views: 5773

Answers (3)

Thomas Dickey
Thomas Dickey

Reputation: 54475

The problem is that getch refreshes the standard window stdscr, overwriting the refresh which was done for win on the previous line. If you had called wgetch(win) rather than those two lines, it would have worked.

Like this:

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    initscr();
    WINDOW* win;
    int height = 10;
    int width = 40;
    int srtheight = 1;
    int srtwidth = 0;
    win = newwin(height, width, srtheight ,srtwidth);
    mvwprintw(win, height/2,width/2,"First line");
    /* wrefresh(win); */
    wgetch(win);
    delwin(win);
    endwin();


    return 0;
}

Further reading:

Upvotes: 5

Edwin Buck
Edwin Buck

Reputation: 70899

You forgot to call refresh.

Basically, you did call refresh on your newly create window, but you forgot to refresh the parent window, so it never redrew.

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    WINDOW* my_win;
    int height = 10;
    int width = 40;
    int srtheight = 1;
    int srtwidth = 1;
    initscr();
    printw("first"); // added for relative positioning
    refresh();  //  need to draw the root window
                //  without this, apparently the children never draw
    my_win = newwin(height, width, 5, 5);
    box(my_win, 0, 0);  // added for easy viewing
    mvwprintw(my_win, height/2,width/2,"First line");
    wrefresh(my_win);
    getch();
    delwin(my_win);
    endwin();
    return 0;
}   

gives the windows as you expected.

Upvotes: 10

Paul R
Paul R

Reputation: 212949

You need a call to refresh() after newwin():

win = newwin(height, width, srtheight ,srtwidth);
refresh(); // <<<
mvwprintw(win, height/2,width/2,"First line");

Upvotes: 1

Related Questions