home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 1.ddi / EXAMPLE.EXE / CSCAPE / EXAMPLE / DEMOTTY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-12  |  4.2 KB  |  166 lines

  1. /*
  2.     testtty.c
  3.  
  4.     Fun with tty cmap windows
  5.  
  6.     jmd 8/06/89
  7.  
  8.     This program demonstrates the use of a character map (cmap) window.
  9.     The function cmap_PlotTTY is called to output text to the window in
  10.     TTY fashion.  See cmap_PlotTTY in the OWL file CMWINTTY.C for details.
  11.  
  12.     Also demostrated here is attaching a border to the window; setting the
  13.     border title and prompt; enabling mouse features in the border; and
  14.     positioning the window.
  15.  
  16.     You may call this program with an ASCII file as an argument and see
  17.     the text roll thru the window:
  18.  
  19.     testtty mytext.txt
  20.  
  21.     Or, you may invoke it with no argument and enter text from the
  22.     keyboard.  Note that there is no cursor.
  23.  
  24. */
  25.  
  26. /*
  27.     if you had included cscape.h you wouldn't have to include oak.h, 
  28.     odisp.h or bordobj.h.
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>          /* for isprint() */
  33.  
  34. #include <oak.h>
  35. #include <odisp.h>
  36.  
  37. #include <cmwinobj.h>       /* for cmwin stuff */
  38. #include <bordobj.h>        /* for border stuff */
  39.  
  40. #include <scancode.h>       /* for referring to keyboard */
  41.  
  42. #define FEATURE     (BD_MOVE | BD_RESIZE | BD_OUTLINE)
  43. #define BORDER      bd_prompt
  44.  
  45. void main(argc, argv)
  46.     int  argc;
  47.     char *argv[];
  48. {
  49.     win_type    win;
  50.     ocbox       cbox;
  51.     FILE        *fp = NULL;
  52.     char        buffer[501];
  53.     SIZE_T      len;
  54.  
  55.     boolean     done;
  56.     int         scancode;
  57.  
  58.     /* open file specified on command line for reading */
  59.  
  60.     if (argc > 1) {
  61.         fp = fopen(argv[1], "r");
  62.     }
  63.  
  64.     /* use the current video mode */
  65.     disp_Init(def_ModeCurrent, NULL);
  66.  
  67.     /* initialize the mouse */
  68.     hard_InitMouse();
  69.     cmwin_MouseInit();
  70.  
  71.     /* set up for a window with half as many row & cols as display */
  72.     cbox.toprow   = 1;
  73.     cbox.leftcol  = 1;
  74.     cbox.botrow   = (int) (disp_GetHeight() / 2);
  75.     cbox.rightcol = (int) (disp_GetWidth() / 2);
  76.  
  77.     /* create the window and attach the border */
  78.  
  79.     /* Create a cmap window */
  80.     win = win_Open(cmwin_Class, &cbox);
  81.     win_SetPosition(win, (int)(disp_GetHeight() / 4), (int)(disp_GetWidth() / 4));
  82.  
  83.     /* attach a border to the window */
  84.     win_SetBorder(win, BORDER);
  85.     bord_SetFeature(win, FEATURE);
  86.     bord_SendMsg(win, BDM_SETTITLE, "Character map (cmap) TTY window", NULL);
  87.  
  88.     /* employ the window and paint it to the display */
  89.     win_Employ(win);
  90.  
  91.     if (fp == NULL) {
  92.         done = FALSE;               /* read keys if no input file given */
  93.         bord_SendMsg(win, BDM_PROMPT, "Ready for keybd input!  ESC=quit", NULL);
  94.         while(!done) {
  95.             switch (scancode = kb_Read()) {
  96.                 case ESC:
  97.                     done = TRUE;                /* ESC to quit */
  98.                     buffer[0] = '\0';
  99.                     break;
  100.  
  101.                 case ENTER:                     /* CR and LF */
  102.                     buffer[0] = '\n';
  103.                     buffer[1] = '\0';
  104.                     break;
  105.  
  106.                 case BACKSPACE:                 /* back up one col in row */
  107.                     buffer[0] = '\b';
  108.                     buffer[1] = '\0';
  109.                     break;
  110.  
  111.                 case UP:                        /* reverse linefeed */
  112.                     buffer[0] = '\v';
  113.                     buffer[1] = '\0';
  114.                     break;
  115.  
  116.                 case RIGHT:                     /* move left one col in row */
  117.                     buffer[0] = ' ';
  118.                     buffer[1] = '\0';
  119.                     break;
  120.  
  121.                 case TAB:                       /* tab */
  122.                     buffer[0] = '\t';
  123.                     buffer[1] = '\0';
  124.                     break;
  125.  
  126.                 case PGDN:                      /* form feed (the height of win) */
  127.                     buffer[0] = '\f';
  128.                     buffer[1] = '\0';
  129.                     break;
  130.                     /* print whats printable */
  131.                 default:
  132.                     buffer[0] = (char) (isprint(ascii(scancode))) ? ascii(scancode) : '\0';
  133.                     buffer[1] = '\0';
  134.                     break;                                     
  135.             }
  136.             cmwin_PlotTTY(win, buffer);
  137.         }
  138.     }
  139.  
  140.     else {                                  /* if we have an input file */
  141.  
  142.         bord_SendMsg(win, BDM_PROMPT, "Press a key to begin!", NULL);
  143.         kb_Read();                          /* wait for a starting keypress */  
  144.         bord_SendMsg(win, BDM_PROMPT, "ESC=quit", NULL);
  145.  
  146.         do {                                /* then read file 500 at a shot */
  147.             len = fread(buffer, 1, 500, fp);
  148.             buffer[len] = '\0';
  149.             cmwin_PlotTTY(win, buffer);     /* and show */
  150.  
  151.         } while (len == 500);               /* till done */
  152.  
  153.         kb_Read();
  154.     }
  155.  
  156.     /* close the OWL and the input file, if present */
  157.  
  158.     disp_Close();
  159.  
  160.     if (fp != NULL) {
  161.         fclose(fp);
  162.     }
  163. }
  164.  
  165.  
  166.