home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c003 / 1.ddi / DEMOS / LOOP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-02  |  5.3 KB  |  183 lines

  1. /* loop.c -- Tutorial on using keyboard loop functions.
  2.  
  3.     ************* Copyright 1985 by Vermont Creative Software **************
  4.  
  5.     DATE:  January 20, 1986
  6.  
  7.     INTRODUCTION
  8.  
  9.     This program shows the steps required to install and use the keyboard loop
  10.     feature provided in the Window library.
  11.  
  12.     To keep the program simple, we will use only 2 windows.  In the main
  13.     window we will display some text and any keystrokes typed by the user.
  14.     In the status window we will display a running count of how many times
  15.     the loop function has been executed.
  16.  
  17. */
  18.  
  19. #define WN_DEBUG
  20. #include <wfc.h>
  21. #include <wfc_glob.h>
  22.  
  23.  
  24. WINDOW stat_wn;             /*declared outside main so that the   */
  25.                     /*loop function can have access to    */
  26.                     /*this variable               */
  27.  
  28. char *message[] =
  29. {
  30.   "This program demonstrates how to install and use keyboard loop functions.  ",
  31.   "Pressing any key will cause the character to be echoed to the main window.  ",
  32.   "If no keystrokes are waiting in the keyboard buffer, the loop function is executed.  ",
  33.   "The loop function simply prints in the status window the number of times it has been ",
  34.   "executed.\n\nTo leave the program, press the ESC key.",
  35.   NULLP
  36. };
  37.  
  38. char *title = "Tutorial on Installing and Using Keyboard Loop Functions";
  39.  
  40. main()
  41. {
  42.     WINDOW main_wn;
  43.     int ch;
  44.     register int i;
  45.     int count();            /*this is the loop function          */
  46.     void prt_ch();            /*this is a primitive editor function */
  47.  
  48.     init_wfc();             /*initialize the WFC system          */
  49.  
  50.     def_wn(&main_wn, 0, 20, 0, 79, 1, 1, BDR_LNP);
  51.     def_wn(&stat_wn, 21, 21, 0, 79, 0, 0, BDR_0P);
  52.  
  53.     se_keyloop(count);            /*install the keyboard loop function  */
  54.  
  55.     set_wn(&main_wn);
  56.     set_wn(&stat_wn);
  57.  
  58.     v_plst(1, CENTER_TEXT, title, &main_wn);   /*display title              */
  59.  
  60.     mv_cs(3, 0, &main_wn);
  61.     i = 0;
  62.     while(message[i] != NULLP)        /*display info messages in main_wn    */
  63.     v_st(message[i++], &main_wn);
  64.  
  65.     mv_csr(11, 0, &main_wn);
  66.     while((ch = ki()) != K_ESC)     /*read keyboard               */
  67.     prt_ch(ch, &main_wn);        /*print character in window          */
  68.  
  69.     mv_csr(v_rwq - 2, 0, &wn0);     /*move cursor to bottom           */
  70.     exit_wfc();
  71.     return(0);
  72. }
  73.  
  74. /*----------------------------------------------------------------------------*/
  75. /*  This is the keyboard loop function that will be executed whenever there   */
  76. /*  are no keystrokes waiting in the keyboard buffer.                  */
  77. /*----------------------------------------------------------------------------*/
  78. int count()
  79. {
  80.     static unsigned int x;
  81.  
  82.     mv_cs(0, 60, &stat_wn);
  83.     v_printf(&stat_wn, "%d", x++);
  84.     return(0);
  85. }
  86.  
  87. /*----------------------------------------------------------------------------*/
  88. /*  This is a very primitive editing function that will display a typed       */
  89. /*  character in a window if it is printable; otherwise an editing function   */
  90. /*  is performed.                                  */
  91. /*----------------------------------------------------------------------------*/
  92. void prt_ch(ch, wnp)
  93. int ch;
  94. WINDOWPTR wnp;
  95. {
  96.     int last_row;
  97.     int last_col;
  98.  
  99.     last_row = row_qty(wnp) - 1;
  100.     last_col = col_qty(wnp) - 1;
  101.  
  102.     switch(ch)
  103.     {
  104.     case K_ENTER:            /*user pressed <ENTER>          */
  105.         wnp->r++;
  106.         if(wnp->r > last_row)
  107.         wnp->r = last_row;
  108.         else
  109.         wnp->c = 0;
  110.         pl_csr(wnp);
  111.         break;
  112.  
  113.     case K_BACK:            /*handle backspace              */
  114.         if(wnp->c > 0)        /*if not at left boundary          */
  115.         {
  116.         wnp->c--;        /*go back one position              */
  117.         v_ch(' ', wnp);         /*print "space"                       */
  118.         wnp->c--;        /*go back one position              */
  119.         pl_csr(wnp);        /*place screen cursor              */
  120.         }
  121.         else            /*can't backspace beyond left boundary*/
  122.         bell();
  123.         break;
  124.  
  125.     case -K_LEFT:            /*handle left cursor              */
  126.         if(wnp->c > 0)        /*if not at left boundary          */
  127.         {
  128.         wnp->c--;        /*move cursor 1 column left          */
  129.         pl_csr(wnp);        /*place screen cursor              */
  130.         }
  131.         else            /*can't move cursor left              */
  132.         bell();
  133.         break;
  134.  
  135.     case -K_RIGHT:            /*handle right cursor              */
  136.         if(wnp->c < last_col)    /*if not at right boundary          */
  137.         {
  138.         wnp->c++;        /*move cursor 1 column right          */
  139.         pl_csr(wnp);        /*place screen cursor              */
  140.         }
  141.         else            /*can't move cursor right             */
  142.         bell();
  143.         break;
  144.  
  145.     case -K_UP:            /*handle up cursor              */
  146.         if(wnp->r > 0)        /*if not at top boundary          */
  147.         {
  148.         wnp->r--;        /*move cursor up one line          */
  149.         pl_csr(wnp);
  150.         }
  151.         else            /*can't move cursor up                */
  152.         bell();
  153.         break;
  154.  
  155.     case -K_DN:            /*handle down cursor              */
  156.         if(wnp->r < last_row)    /*if not at bottom boundary          */
  157.         {
  158.         wnp->r++;        /*move cursor down one line          */
  159.         pl_csr(wnp);
  160.         }
  161.         else            /*can't move cursor down              */
  162.         bell();
  163.         break;
  164.  
  165.     default:            /*handle all other possibilities      */
  166.         if(ch > 0)            /*check for printable character       */
  167.         {
  168.         v_ch(ch, wnp);        /*else write character and continue   */
  169.         if(wnp->r > last_row)
  170.         {
  171.             wnp->r = last_row;
  172.             wnp->c = last_col;
  173.             bell();
  174.         }
  175.         pl_csr(wnp);
  176.         }
  177.         else            /* extended code              */
  178.         bell();         /* let user know it's illegal         */
  179.         break;
  180.     }
  181.     return;
  182. }
  183.