home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CURSES.LZH / DEMO.C < prev    next >
Text File  |  1990-02-02  |  16KB  |  599 lines

  1. /*
  2.  * demo.c        demonstrate the method of pop-up windows without
  3.  *              a hardware clear screen.
  4.  *
  5.  *        a hardware clear screen could be done as follows:
  6.  *            *** place just before endwin() ***
  7.  *            wclear(s_image);
  8.  *            wnoutrefresh(s_image);
  9.  *            touchwin(pop);
  10.  *            wrefresh(pop);
  11.  *            endwin();
  12.  *            exit(0);
  13.  *
  14.  *        the wclear() not only sets the window to spaces with
  15.  *        A_NORMAL attribute, but also sets the clear screen
  16.  *        flag.  this leaves no reason to move the cursor back
  17.  *        to its origin.
  18.  */
  19. #include <stdio.h>
  20. #define _NO_STDIO 1
  21. #include <stdlib.h>
  22. #include "curses.h"
  23.  
  24. #define CTRL(c)        (c & 037)
  25. #define MAX_WNDW    8
  26. #define BEG_WNDW    0
  27. #define START_ROW   5
  28. #define START_COL   5
  29.  
  30.  
  31. char *intro[] = {"\tWelcome to a demonstration of using curses on the "
  32.         ,"\tIBM PC.  This curses package is a public domain   "
  33.         ,"\tsoftware package, which includes source code and  "
  34.         ,"\ttwo libraries, a medimum memory model library and "
  35.         ,"\ta large memory model library.                     "
  36.         ,""
  37.         ,"\tThis demonstration program will show only some of "
  38.         ,"\tthe abilities of curses to use and manipulate     "
  39.         ,"\twindows on the display screen.  A maximum of 8    "
  40.         ,"\tdisplay windows will be used by this program.     "
  41.         ,""
  42.         ,"\tPress any key to continue from this message to the"
  43.         ,"\trest of the demonstration or press ESC to stop    "
  44.         ,"\tnow.  When other windows appear you may press the "
  45.         ,"\tPgUp key to move to the previous window display;  "
  46.         ,"\tuse the PgDn key to move to the next window       "
  47.         ,"\tdisplay.  Also, CTRL+P instead of PgUp and CTRL+N "
  48.         ,"\tcan be used instead of PgDn.  ESC ends the        "
  49.         ,"\tdemonstration at any time.                        "
  50.         ,""
  51.         ,"\tPress any key to continue or ESC to stop.         "
  52.         ,(char *)NULL
  53.         };
  54.  
  55. main()
  56. {
  57. WINDOW
  58.     *s_image    /* base window, stdscr, where everything else is laid */
  59.     ,*pop[8]    /* an array of pop up windows, just for demonstration */
  60.     ;
  61. int
  62.     row         /* starting row of window */
  63.     ,col        /* starting column of window */
  64.     ,height     /* height in rows of a window */
  65.     ,width      /* width in columns of a window */
  66.     ,wndx       /* index into array of pop up windows */
  67.     ,direction  /* 0=current window; 1=next window; -1=previous window */
  68.     ;
  69. /*
  70. *       function declarations
  71. */
  72. int
  73.     show_intro(WINDOW *, char *[])
  74.     ;
  75. void
  76.     fill_pop(WINDOW *, int)
  77.     ,close_wndws(WINDOW *[])
  78.     ,werrwarn(char *, char *, WINDOW *)
  79.     ;
  80.     /*
  81.     *       make s_image point to the stdscr
  82.     */
  83.     if(initscr() == ERR)
  84.     {
  85.     fprintf(stderr,"\nCould not start curses environment.");
  86.     exit(1);
  87.     }
  88.     /*
  89.     *       set certain terminal input attributes
  90.     */
  91.     raw();      /* get all eight bits of each character from keyboard */
  92.     nonl();     /* turn off acceptance of carriage-return as newline */
  93.     noecho();   /* do not echo to the screen */
  94.     cbreak();   /* send all keystrokes directly to this application */
  95.     keypad(stdscr,TRUE);   /* enable use of keys on numeric pad */
  96.     /*
  97.     *       clear the window
  98.     */
  99.     wclear(stdscr);
  100.     /*
  101.     *       box in the window
  102.     */
  103.     box(stdscr,'=','=');
  104.     /*
  105.     *       display introduction message
  106.     */
  107.     if( !show_intro(stdscr,intro) )
  108.     {
  109.     /*
  110.     *       close out curses environment gracefully
  111.     */
  112.     wmove(stdscr,LINES,0);
  113.     endwin();
  114.     exit(0);
  115.     }
  116.     /*
  117.     *       loop on pop up of a max of eight windows
  118.     */
  119.     for( wndx = 0, direction = 0, height = 8,
  120.        width = 50, row = 5, col = 5;
  121.      wndx < MAX_WNDW && direction != KEY_ESC;)
  122.     {
  123.     /*
  124.     *   pop up a window only if not coming back from other window
  125.     */
  126.     if(direction != KEY_PPAGE && direction != CTRL('P'))
  127.     {
  128.         pop[wndx] = newwin(height, width, row, col);
  129.         keypad(pop[wndx],TRUE);
  130.         fill_pop(pop[wndx],wndx);
  131.     }
  132.     /*
  133.     *       read associated keyboard with window
  134.     */
  135.     direction = wgetch(pop[wndx]);
  136.     switch (direction)
  137.     {
  138.         case KEY_ESC:
  139.  
  140.         close_wndws(pop);
  141.         break;
  142.  
  143.         /*
  144.         *       going forward in window list
  145.         */
  146. /*        case KEY_NPAGE: break;*/
  147.         case CTRL('N'): 
  148.         case CTRL('n'): 
  149.  
  150.         if((row + 5) > (LINES - height))
  151.             row = START_ROW;
  152.         else
  153.             row += 5;
  154.         if( (col + 5) > (COLS - width) )
  155.             col = START_COL;
  156.         else
  157.             col += 5;
  158.         /*
  159.         *       increment index into window array and retain largest
  160.         *       index
  161.         */
  162.         ++wndx;
  163.         break;
  164.  
  165.       /*case KEY_PPAGE: break;*/
  166.         case CTRL('P'): 
  167.         case CTRL('p'): 
  168.  
  169.         /*
  170.         *       moving to previous, requires current
  171.         *       window on top be discarded
  172.         */
  173.         wclear(pop[wndx]);
  174.         wrefresh(pop[wndx]);
  175.         delwin(pop[wndx]);
  176.         pop[wndx] = (WINDOW *)NULL;
  177.         if( --wndx < BEG_WNDW)
  178.             direction = KEY_ESC;
  179.         /*
  180.         *       previous window must be redisplayed completely
  181.         */
  182.         if( wndx >= BEG_WNDW)
  183.         {
  184.             touchwin(pop[wndx]);
  185.             wrefresh(pop[wndx]);
  186.         }
  187.         break;
  188.  
  189.     }/* end of switch */
  190.  
  191.     }/* end of for */
  192.     /*
  193.     *       restore complete image of standard screen
  194.     */
  195.     touchwin(stdscr);
  196.     /*
  197.     *       find cursor location within window
  198.     */
  199.     getyx(stdscr,row,col);
  200.     wattrset(stdscr,A_REVERSE);
  201.     mvwaddstr(stdscr,++row,1,"\tI hope you enjoyed the demonstration.");
  202.     mvwaddstr(stdscr,++row,1,"\tPress any key to terminate demonstration.");
  203.     wattrset(stdscr,A_DIM);
  204.     /*
  205.     *       bring window image to screen
  206.     */
  207.     wrefresh(stdscr);
  208.     wgetch(stdscr);
  209.     /*
  210.     *       clear the standard screen window
  211.     */
  212.     wclear(stdscr);
  213.     wrefresh(stdscr);
  214.     /*
  215.     *       close out the curses environment
  216.     */
  217.     endwin();
  218.     /*
  219.     *   endwin() moved the cursor to LINES-1,0
  220.     *   just move it back to where it started.
  221.     *   not dependant on internals which endwin
  222.     *   shut down.
  223.     */
  224.     mvcur((LINES-1), 0, 0, 0);
  225.     exit(0);
  226.  
  227. }/* end of main() */
  228.  
  229. int
  230. show_intro(basewndw,msg)
  231. WINDOW *basewndw;
  232. char *msg[];
  233. {
  234. int i
  235.     ,chr
  236.     ;
  237.     /*
  238.     *       write the message to basewndw
  239.     *       message starts on line 1 of window, because
  240.     *       border is in line 0
  241.     */
  242.     for(i = 0; msg[i]; ++i)
  243.     mvwaddstr(basewndw,i+1,1,msg[i]);
  244.     /*
  245.     *       display contents of window on screen
  246.     */
  247.     wrefresh(basewndw);
  248.     /*
  249.     *       read the keyboard associated with the window for input
  250.     */
  251.     if( (chr = wgetch(basewndw)) == KEY_ESC)
  252.     return ( FALSE);    /* ESC pressed, signal calling function */
  253.     return( TRUE );     /* any key was pressed but ESC continue */
  254.  
  255. } /* end of show_intro() */
  256.  
  257. char *note1[] = { "\tHi, i'm \"pop\" window number 1."
  258.         , "                                  "
  259.         , "\tI just pop up to show you how   "
  260.         , "\tto emulate a pop-up style system"
  261.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  262.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  263.         , (char *)NULL
  264.         };
  265.  
  266. char *note2[] = { "\tHi, i'm \"pop\" window number 2."
  267.         , "                                  "
  268.         , "\tI just pop up to show you how   "
  269.         , "\tto emulate a pop-up style system"
  270.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  271.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  272.         , (char *)NULL
  273.         };
  274.  
  275. char *note3[] = { "\tHi, i'm \"pop\" window number 3."
  276.         , "                                  "
  277.         , "\tI just pop up to show you how   "
  278.         , "\tto emulate a pop-up style system"
  279.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  280.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  281.         , (char *)NULL
  282.         };
  283.  
  284. char *note4[] = { "\tHi, i'm \"pop\" window number 4."
  285.         , "                                  "
  286.         , "\tI just pop up to show you how   "
  287.         , "\tto emulate a pop-up style system"
  288.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  289.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  290.         , (char *)NULL
  291.         };
  292.  
  293. char *note5[] = { "\tHi, i'm \"pop\" window number 5."
  294.         , "                                  "
  295.         , "\tI just pop up to show you how   "
  296.         , "\tto emulate a pop-up style system"
  297.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  298.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  299.         , (char *)NULL
  300.         };
  301.  
  302. char *note6[] = { "\tHi, i'm \"pop\" window number 6."
  303.         , "                                  "
  304.         , "\tI just pop up to show you how   "
  305.         , "\tto emulate a pop-up style system"
  306.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  307.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  308.         , (char *)NULL
  309.         };
  310.  
  311. char *note7[] = { "\tHi, i'm \"pop\" window number 7."
  312.         , "                                  "
  313.         , "\tI just pop up to show you how   "
  314.         , "\tto emulate a pop-up style system"
  315.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  316.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  317.         , (char *)NULL
  318.         };
  319.  
  320. char *note8[] = { "\tHi, i'm \"pop\" window number 8."
  321.         , "                                  "
  322.         , "\tI just pop up to show you how   "
  323.         , "\tto emulate a pop-up style system"
  324.         , "\tCTRL+P=PREV CTRL+N=NEXT or      "
  325.         , "\tPgUp=PREV PgDn=NEXT ESC=EXIT    "
  326.         , (char *)NULL
  327.         };
  328.  
  329. void
  330. fill_pop(WINDOW *wndw, int indx)
  331. {
  332. int i;
  333.     /*
  334.     *       which set of notes are to be used for this display
  335.     */
  336.     switch (indx)
  337.     {
  338.     case 0:
  339.         box(wndw, '*', '-');
  340.         for(i=0; note1[i]; i++)
  341.         mvwaddstr(wndw, i+1, 1, note1[i]);
  342.         break;
  343.  
  344.     case 1:
  345.         box(wndw, '|', '=');
  346.         for(i=0; note2[i]; ++i)
  347.         mvwaddstr(wndw, i+1, 1, note2[i]);
  348.         break;
  349.  
  350.     case 2:
  351.         box(wndw,'|','=');
  352.         for(i=0; note3[i]; ++i)
  353.         mvwaddstr(wndw, i+1, 1, note3[i]);
  354.         break;
  355.  
  356.     case 3:
  357.         box(wndw,'|','-');
  358.         for(i=0; note4[i]; ++i)
  359.         mvwaddstr(wndw, i+1, 1, note4[i]);
  360.         break;
  361.  
  362.     case 4:
  363.         box(wndw,'*','-');
  364.         for(i=0; note5[i]; ++i)
  365.         mvwaddstr(wndw, i+1, 1, note5[i]);
  366.         break;
  367.  
  368.     case 5:
  369.         box(wndw,'|','-');
  370.         for(i=0; note6[i]; ++i)
  371.         mvwaddstr(wndw, i+1, 1, note6[i]);
  372.         break;
  373.  
  374.     case 6:
  375.         box(wndw,'*','=');
  376.         for(i=0; note7[i]; ++i)
  377.         mvwaddstr(wndw, i+1, 1, note7[i]);
  378.         break;
  379.  
  380.     case 7:
  381.         box(wndw,'|','=');
  382.         for(i=0; note8[i]; ++i)
  383.         mvwaddstr(wndw, i+1, 1, note8[i]);
  384.         break;
  385.  
  386.     }/* end of switch */
  387.     /*
  388.     *    make text window visible
  389.     */
  390.     wrefresh(wndw);
  391.  
  392. }/* end of fill_pop() */
  393.  
  394. void
  395. close_wndws(WINDOW *wns[])
  396. {
  397. register int i;
  398.     /*
  399.     *       loop thru array of WINDOWs and remove them
  400.     */
  401.     for(i = 0; i < MAX_WNDW; ++i)
  402.     {
  403.        if( wns[i] != (WINDOW *)NULL)
  404.        {
  405.           wclear(wns[i]);
  406.           delwin(wns[i]);
  407.        }
  408.     }
  409.  
  410. }/* end of close_wndws() */
  411. /*********************************************************************
  412. *   Function Name:  void werrwarn(first,second,wndw)
  413. *   Source Name  :  \source\werrwarn.c
  414. *   Author       :
  415. *   Installation :
  416. *   Date Written :
  417. *   Description  :  Displays a two string diagnostic message on
  418. *                   line 24, waits for the user to press ESC,
  419. *                   and then erases the diagnostic message.
  420. *********************************************************************/
  421.  
  422. #include <string.h>
  423.  
  424. void
  425. werrwarn(first,second,wn)
  426. char
  427.     *first      /* first message string to be displayed */
  428.     ,*second    /* second message string to be displayed */
  429.     ;
  430. WINDOW
  431.     *wn         /* window that this function opens a window over */
  432.     ;
  433. {
  434. WINDOW
  435.     *msg                            /* message window */
  436.     ;
  437. int
  438.     ch                              /* character returned from keyboard */
  439.     ;
  440. short
  441.     first_len = strlen(first);      /* length of first string */
  442. short
  443.     second_len = strlen(second);    /* length of second string */
  444.     /*
  445.     *       open message window
  446.     */
  447.     if((msg = newwin(4,80,20,0)) == (WINDOW *)NULL)
  448.     {
  449.     fprintf(stderr,"ALLOCATE ERROR: Could not open window!");
  450.     exit(1);
  451.     }
  452.     /*
  453.     *       draw box around the window
  454.     */
  455.     box(msg,'*','-');
  456.     /*
  457.     *       write text into window
  458.     */
  459.     mvwaddstr(msg,1,1,first);
  460.     mvwaddstr(msg,1,2 + first_len,second);
  461.     mvwaddstr(msg,2,1,"Strike any key to continue...");
  462.     /*
  463.     *       display the window
  464.     */
  465.     wrefresh(msg);
  466.     /*
  467.     *       key is pressed
  468.     */
  469.     ch = wgetch(msg);
  470.     /*
  471.     *       remove the message window
  472.     */
  473.     wclear(msg);
  474.     delwin(msg);
  475.     touchwin(wn);
  476.  
  477. }/* end of err_warn() **************************************************/
  478. /************************************************************************
  479. *   Function Name   :   void werrexit(loc,first,second,wn);
  480. *   Source Name     :   \source\werrwarn.c
  481. *   Author          :
  482. *   Company         :
  483. *   Date Written    :
  484. *   Description     :   Displays a diagnostic message through err_warn()
  485. *                   :   and calls exit() to terminate execution.  The
  486. *                   :   two part message is displayed on line 24.  When
  487. *                   :   the user presses ESC, the message is erased.
  488. **********************************************************************/
  489.  
  490. void
  491. werrexit(loc,first,second,wn)
  492. int
  493.     loc         /* program location indicator */
  494.     ;
  495. char
  496.     *first      /* first dipslay message */
  497.     ,*second    /* second display message */
  498.     ;
  499. WINDOW
  500.     *wn         /* window being overlaid */
  501.     ;
  502. {
  503. char
  504.     log_buf[512]        /* buffer to be passed to logentry() */
  505.     ,locnum[6]          /* buffer to hold location number */
  506.     ;
  507. void logentry(), werrwarn();
  508.  
  509.     strcpy(log_buf," LOCATION: ");
  510.     itoa(loc,locnum,10);
  511.     strcat(log_buf,locnum);
  512.     strcat(log_buf," ");
  513.     strcat(log_buf, first);
  514.     strcat(log_buf, " ");
  515.     strcat(log_buf, second);
  516.     logentry(log_buf,wn);           /* record the message in the log */
  517.     werrwarn(first,second,wn);
  518.     endwin();
  519.     exit(loc);                         /* terminate program: FAIL status */
  520.  
  521. }/*end of err_exit() ***************************************************/
  522. /**********************************************************************
  523. *   Function Name   :   void logentry(buf,wn)
  524. *   Source Name     :   \source\logentry.c
  525. *   Author          :
  526. *   Company         :
  527. *   Date Written    :
  528. *   Date Updated    :
  529. *   Description     :   Record error messages in a transaction log file.
  530. *********************************************************************/
  531.  
  532. #include <time.h>
  533.  
  534. void
  535. logentry(buf,wn)
  536. char
  537.     *buf
  538.     ;
  539. WINDOW
  540.     *wn
  541.     ;
  542. {
  543. int
  544.     buf_len=strlen(buf)     /* length of buffer being logged */
  545.     ;
  546. static
  547.     FILE *fp = NULL;        /* file pointer to logging file */
  548. char
  549.     *datetime               /* time and date */
  550.     ,*gettime()             /* function returning date and time */
  551.     ;
  552.     /*
  553.     *       Open logfile, if it isn't already open.
  554.     */
  555.     if(!fp && !(fp = fopen("logfile.dat","a")))
  556.     {
  557.     werrwarn("No File:","logfile.dat",wn);
  558.     abort();
  559.     }
  560.     /*
  561.     *       time and date stamp the log entry
  562.     */
  563.     datetime = gettime();
  564.     fprintf(fp,"%s%s\n",datetime,buf);
  565.  
  566. }/* end of logentry() ***************************************************/
  567. /********************************************************************
  568. *   Function Name   :   char *gettime();
  569. *   Source Name     :   \source\gettime.c
  570. *   Author          :   Jim Ragsdale
  571. *   Company         :   Copyright (C) 1989 Ragsdale Software Engineering Co.
  572. *   Date Written    :
  573. *   Date Updated    :
  574. *   Description     :   This function will retrieve the date and time
  575. *                   :   and return a pointer to the string.
  576. ********************************************************************/
  577.  
  578. #include <time.h>
  579.  
  580. char *gettime()
  581. {
  582. long timeptr;   /* long integer to hold date/time value from system */
  583. char *datetime; /* pointer to beginning of date-time string */
  584.     /*
  585.     *       call the time() function to get long integer value
  586.     */
  587.     time(&timeptr);
  588.     /*
  589.     *       format date and time and return pointer
  590.     */
  591.     datetime = ctime(&timeptr);
  592.     /*
  593.     *       get rid of newline character
  594.     */
  595.     *(datetime + 24) = '\0';
  596.     return(datetime);
  597.  
  598. }/* end of gettime() *************************************************/
  599.