LQ Newbie
Registered: Apr 2010
Posts: 4
Rep:
|
C and ncurses on ubuntu linux.
See the example below, please. My question is this (ncurses in C): If I don't write code like this with the getch() under comment /* Write to each window */ , it doesn't show anything. Otherwise, works fine.
Also, some other question, in C and ncurses, it is like touchwin() has no effect it should has. The second example I mean, it doesn't work like both the main window and help can be switched among the work properly. I run on Ubuntu 9.10.
EXAMPLE 1.
#include <ncurses.h>
#include <stdlib.h>
void bomb(void);
int main(void)
{
WINDOW *a,*b,*c,*d;
int maxx,maxy,halfx,halfy;
initscr();
/* calculate window sizes and locations */
getmaxyx(stdscr,maxy,maxx);
halfx = maxx >> 1;
halfy = maxy >> 1;
/* create four windows to fill the screen */
if( (a = newwin(halfy,halfx,0,0)) == NULL) bomb();
if( (b = newwin(halfy,halfx,0,halfx)) == NULL) bomb();
if( (c = newwin(halfy,halfx,halfy,0)) == NULL) bomb();
if( (d = newwin(halfy,halfx,halfy,halfx)) == NULL) bomb();
/* Write to each window */
getch(); /*-----------------THIS--------------------------*/
mvwaddstr(a,0,0,"This is window A\n");
wrefresh(a);
mvwaddstr(b,0,0,"This is window B\n");
wrefresh(b);
mvwaddstr(c,0,0,"This is window C\n");
wrefresh(c);
mvwaddstr(d,0,0,"This is window D\n");
wrefresh(d);
getch();
endwin();
return 0;
}
void bomb(void)
{
addstr("Unable to allocate memory for new window.\n");
refresh();
endwin();
exit(1);
}
**********************************************************************
EXAMPLE 2.
#include <ncurses.h>
void showhelp(void);
WINDOW *help;
int main(void)
{
int ch;
initscr();
/* build help menu */
if((help = newwin(0,0,0,0)) == NULL)
{
addstr("Unable to allocate window memory\n");
endwin();
return(1);
}
mvwaddstr(help,6,32,"Help menu Screen");
mvwaddstr(help,9,28,"Press the ~ key to quit");
mvwaddstr(help,12,28,"Press ENTER to go back");
/* now start the program loop */
addstr("Typer Program\n");
addstr("Press + for help:\n\n");
refresh();
noecho();
do
{
ch = getch();
refresh();
if(ch == '+')
showhelp();
else
addch(ch);
} while (ch != '~');
endwin();
return 0;
}
void showhelp(void)
{
touchwin(help);
wrefresh(help);
getch(); /* wait for key press */
touchwin(stdscr);
refresh();
}
|