Written by : Udhaya Kumar.V
Document : Curses sample programs for SCO UNIX environment
Date          : September – 1999
Contact     :URL: http://www.udhaya.com , Mail Id: [email protected]


1. Building message box in curses environment, Current example is having yes, no and cancel button. This program was tested in the SCO UNIX environment.

#include <stdio.h>
#include <curses.h>

main()
{
 int i=0,j=0,key,col;
 static char values[3][6]={" Yes "," No ","Cancel"};
 WINDOW *w;
 initscr();
 w=newwin(7,40,10,20);
 box(w,0,0);
 wattron(w,A_REVERSE);
 mvwprintw(w,1,1,"                                      ");
 mvwprintw(w,1,2,"Hai How are ..");
 wattroff(w,A_REVERSE);

 mvwhline(w,2,0,ACS_HLINE,40);
 mvwaddch(w,2,0,ACS_LTEE);
 mvwaddch(w,2,39,ACS_RTEE);

 mvwprintw(w,3,10,"Do you want to save ?");
 for(i=0,col=10;i<=2;i++,col+=8)
  mvwprintw(w,5,col,"%s",values[i]);
 wattron(w,A_REVERSE);
 mvwprintw(w,5,10,"%s",values[0]);
 wattroff(w,A_REVERSE);

 i=0;col=10;
 refresh();

 keypad(w,TRUE);
 noecho();
 while((key=wgetch(w)) != '\n')
 {
  switch(key)
  {
   case KEY_LEFT :
    mvwprintw(w,5,col,"%s",values[i]);
    col=col-8;i--;
    if(col<8) { col=26;i=2; }
    if(i<0) i=0;
    break;
   case KEY_RIGHT :
    mvwprintw(w,5,col,"%s",values[i]);
    col=col+8;
    i++;
    if(i>2) { i=0; col=10; }
    if(col>34) { col=10; }
    break;
  }
  wattron(w,A_REVERSE);
  mvwprintw(w,5,col,"%s",values[i]);
  wattroff(w,A_REVERSE);
  wrefresh(w);
 }
 endwin();
}

How to compile the curses program?

$ cc box.c –lcurses -o box
$ box

Note: We have to link the curses library
 

Hosted by www.Geocities.ws

1