home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / PPL4C11.ZIP / WIN_IO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-18  |  6.1 KB  |  264 lines

  1. /*
  2. **  -- win_io.c --
  3. **
  4. **  Implements non-overlapped screen I/O.
  5. **  Requires DOS_IO.C.
  6. **
  7. **     WinCreate : Creates a screen window.
  8. **     WinNormal : All subsequent I/O is normal text.
  9. **    WinInverse : All subsequent I/O is inverse text.
  10. **    WinPutChar : Print a character to the window.
  11. **    WinGetChar : Gets a single character from the window.
  12. **  WinPutString : Print a string to the window.
  13. **  WinGetString : Get a string, echoing to the window.
  14. **      WinClear : Clear a window with blanks.
  15. **     WinSetPos : Set the cursor position within a window.
  16. **     WinGetPos : Get the cursor position within a window.
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <dos.h>
  21. #include "dos_io.h"
  22. #include "win_io.h"
  23.  
  24. #define NBR_WINS 5
  25.  
  26. #define CR  0x0d
  27. #define LF  0x0a
  28. #define BS  0x08
  29. #define ESC 0x1b
  30. #define CAN 0x18
  31.  
  32. #define NORMAL  0x07
  33. #define INVERSE 0x70
  34.  
  35. #define FALSE 0
  36. #define TRUE !FALSE
  37. #define BYTE unsigned char
  38.  
  39. static BYTE IsReady = FALSE;
  40. static BYTE Attribute[NBR_WINS];
  41. static BYTE Enabled[NBR_WINS];
  42. static BYTE CurRow[NBR_WINS];
  43. static BYTE CurCol[NBR_WINS];
  44. static BYTE TopRow[NBR_WINS];
  45. static BYTE BotRow[NBR_WINS];
  46. static BYTE LeftCol[NBR_WINS];
  47. static BYTE RghtCol[NBR_WINS];
  48.  
  49. /*** PRIVATE ***/
  50.  
  51. static void Initialize()
  52. {int i;
  53.  if(!IsReady)
  54.   {IsReady = TRUE;
  55.    for(i=0;i<NBR_WINS;i++) Enabled[i] = FALSE;
  56.   }
  57. }
  58.  
  59. static int Check(int Win)
  60. {if(!IsReady) Initialize();
  61.  if((Win<0)||(Win>=NBR_WINS)) return WIN_RANGE;
  62.  if(Enabled[Win]) return WIN_AOK;
  63.  else return WIN_DISABLED;
  64. }
  65.  
  66. static void NewRow(int Win)
  67. {if(++CurRow[Win]>BotRow[Win])
  68.    {Scroll(TopRow[Win],LeftCol[Win],BotRow[Win],RghtCol[Win],1,Attribute[Win]);
  69.     CurRow[Win] = BotRow[Win];
  70.    }
  71. }
  72.  
  73. static void Advance(int Win)
  74. {if(++CurCol[Win]>RghtCol[Win])
  75.   {CurCol[Win] = LeftCol[Win];
  76.    NewRow(Win);
  77.   }
  78.  Position(CurRow[Win],CurCol[Win]);
  79. }
  80.  
  81. static int WriteChar(int Win,char C)
  82. {
  83.  switch(C)
  84.    {case LF:
  85.       NewRow(Win);
  86.       /* fall thru */;
  87.     case CR:
  88.       CurCol[Win] = LeftCol[Win];
  89.       WinGetPos(Win,NULL,NULL);  /* cory ??? */
  90.       break;
  91.     case BS:
  92.       if(CurCol[Win] > LeftCol[Win])
  93.         {/* back up */
  94.          Position(CurRow[Win],--CurCol[Win]);
  95.          AttrWrite((BYTE)' ', (BYTE)Attribute[Win]);
  96.         }
  97.       break;
  98.     default:
  99.       if((C<' ')||(C>'~')) C = ' ';
  100.       AttrWrite((BYTE)C, (BYTE)Attribute[Win]);
  101.       Advance(Win);
  102.       break;
  103.    }
  104. return WIN_AOK;
  105. }
  106.  
  107. /*** PUBLIC ***/
  108.  
  109. /* set attribute to normal */
  110.  
  111. int WinNormal(int Win)
  112. {int rc;
  113.  if((rc=Check(Win))!=WIN_AOK) return rc;
  114.  Attribute[Win] = NORMAL;
  115.  return WIN_AOK;
  116. }
  117.  
  118. /* set attribute to inverse */
  119.  
  120. int WinInverse(int Win)
  121. {int rc;
  122.  if((rc=Check(Win))!=WIN_AOK) return rc;
  123.  Attribute[Win] = INVERSE;
  124.  return WIN_AOK;
  125. }
  126.  
  127. /* create window */
  128.  
  129. int WinCreate(int Win,int r1,int c1,int r2,int c2)
  130. {if(!IsReady) Initialize();
  131.  if((Win<0)||(Win>=NBR_WINS)) return WIN_RANGE;
  132.  if(Enabled[Win]) return WIN_ENABLED;
  133.  Enabled[Win] = TRUE;
  134.  Attribute[Win] = NORMAL;
  135.  if(r1>r2) return FALSE;
  136.  if(r2>24) return FALSE;
  137.  if(c1>c2) return FALSE;
  138.  if(c2>79) return FALSE;
  139.  TopRow[Win] = r1;
  140.  BotRow[Win] = r2;
  141.  LeftCol[Win] = c1;
  142.  RghtCol[Win] = c2;
  143.  CurRow[Win] = r1;
  144.  CurCol[Win] = c1;
  145.  Position((BYTE)r1,(BYTE)c1);
  146.  return WIN_AOK;
  147. }
  148.  
  149. /* write char to window */
  150.  
  151. int WinPutChar(int Win,char C)
  152. {int rc;
  153.  if((rc=Check(Win))!=WIN_AOK) return rc;
  154.  Position(CurRow[Win],CurCol[Win]);
  155.  return WriteChar(Win,C);
  156. }
  157.  
  158. /* write string to window */
  159.  
  160. int WinPutString(int Win,char *String)
  161. {int rc;
  162.  if((rc=Check(Win))!=WIN_AOK) return rc;
  163.  Position(CurRow[Win],CurCol[Win]);
  164.  while(*String!='\0') WriteChar(Win,*String++);
  165.  return WIN_AOK;
  166. }
  167.  
  168. /* clear window */
  169.  
  170. int WinClear(int Win)
  171. {int rc;
  172.  if((rc=Check(Win))!=WIN_AOK) return rc;
  173.  Scroll(TopRow[Win],LeftCol[Win],BotRow[Win],RghtCol[Win],0,Attribute[Win]);
  174.  CurRow[Win] = TopRow[Win];
  175.  CurCol[Win] = LeftCol[Win];
  176.  return WIN_AOK;
  177. }
  178.  
  179. /* get character from Window */
  180.  
  181. char WinGetChar(int Win)
  182. {int  i, rc;
  183.  char c;
  184.  if((rc=Check(Win))!=WIN_AOK) return rc;
  185.  Position(CurRow[Win],CurCol[Win]);
  186.  c = ReadKbd();
  187.  AttrWrite((BYTE)c, (BYTE)Attribute[Win]);
  188.  Advance(Win);
  189.  return c;
  190. }
  191.  
  192. /* get string from window */
  193.  
  194. int WinGetString(int Win, char *String, int Length)
  195. {int  i, rc;
  196.  char c;
  197.  if((rc=Check(Win))!=WIN_AOK) return rc;
  198.  Position(CurRow[Win],CurCol[Win]);
  199.  /* input text from user */
  200.  i = 0;
  201.  while(1)
  202.      {c = ReadKbd();
  203.       switch(c)
  204.         {case LF:
  205.            /* ignore LFs */
  206.            break;
  207.          case CR:
  208.            /* done */
  209.            String[i] = '\0';
  210.            return WIN_AOK;
  211.          case ESC:
  212.          case CAN:
  213.            /* aborting */
  214.            *String = '\0';
  215.            return WIN_AOK;
  216.          case BS:
  217.            if(i>0)
  218.              {/* back up */
  219.               i--;
  220.               Position(CurRow[Win],--CurCol[Win]);
  221.               AttrWrite((BYTE)' ', (BYTE)Attribute[Win]);
  222.              }
  223.            break;
  224.          default:
  225.            /* save character & display on status line */
  226.            if((c>=' ')&&(c<='~'))
  227.               {String[i++] = c;
  228.                AttrWrite((BYTE)c, (BYTE)Attribute[Win]);
  229.                Advance(Win);
  230.                if(i==Length)
  231.                   {/* user string done */
  232.                    String[i] = '\0';
  233.                    return WIN_AOK;
  234.                   }
  235.               }
  236.            break;
  237.         } /* end case */
  238.      } /* end while */
  239. }
  240.  
  241. /* set cursor position in window */
  242.  
  243. int WinSetPos(int Win,int Row, int Col)
  244. {int rc;
  245.  if((rc=Check(Win))!=WIN_AOK) return rc;
  246.  if(TopRow[Win]  + Row > BotRow[Win])  return WIN_RANGE;
  247.  if(LeftCol[Win] + Col > RghtCol[Win]) return WIN_RANGE;
  248.  CurRow[Win] = TopRow[Win]+Row;
  249.  CurCol[Win] = LeftCol[Win]+Col;
  250.  Position(CurRow[Win],CurCol[Win]);
  251.  return WIN_AOK;
  252. }
  253.  
  254. /* get window cursor position */
  255.  
  256. int WinGetPos(int Win,int *RowP,int *ColP)
  257. {int rc;
  258.  if((rc=Check(Win))!=WIN_AOK) return rc;
  259.  Position(CurRow[Win],CurCol[Win]);
  260.  if(RowP) *RowP = CurRow[Win] - TopRow[Win];
  261.  if(ColP) *ColP = CurCol[Win] - LeftCol[Win];
  262.  return WIN_AOK;
  263. }
  264.