home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / AW2.ZIP / SCR.C < prev    next >
C/C++ Source or Header  |  1989-03-31  |  5KB  |  242 lines

  1. #define INCL_PM
  2. #include <os2.h>
  3. extern HAB hAB;
  4. extern HHEAP hHeap;
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <memory.h>
  8. #include "scr.h"
  9.  
  10. static void NEAR DoLF(PTTYWND pTTYWnd);
  11. static void NEAR DoCR(PTTYWND pTTYWnd);
  12. static void NEAR DoBS(PTTYWND pTTYWnd);
  13. static void NEAR DoTab(PTTYWND pTTYWnd);
  14.  
  15. InitWindow(pWnd,left,top,width,height,charwidth,charheight,LFonCR,CRonLF,wrap,mask)
  16. PTTYWND pWnd;
  17. short left, top, width, height, charwidth, charheight;
  18. BOOL LFonCR, CRonLF, wrap;
  19. BYTE mask;
  20. {
  21.  
  22.     int bufsize;
  23.  
  24.     pWnd->Left = left;
  25.     pWnd->Top = top;
  26.     pWnd->Width = width;
  27.     pWnd->Height = height;
  28.     pWnd->CWidth = charwidth;
  29.     pWnd->CHeight = charheight;
  30.     pWnd->MaxLines = height / charheight;
  31.     pWnd->MaxCols = width / charwidth;
  32.     pWnd->MaxLineLength = pWnd->MaxCols + 1;
  33.     bufsize = pWnd->MaxLines * pWnd->MaxLineLength;
  34.  
  35.     if (pWnd->pVidBuf = WinAllocMem(hHeap, bufsize))
  36.     {
  37.     memset(pWnd->pVidBuf, NUL, bufsize);
  38.     pWnd->Pos.y = 0;
  39.     pWnd->Pos.x = 0;
  40.     pWnd->oCurrentLine = pWnd->CurLineOffset = 0;
  41.     pWnd->oVidLastLine = (pWnd->MaxLines-1) * pWnd->MaxLineLength;
  42.     pWnd->LFonCR = LFonCR;
  43.     pWnd->CRonLF = CRonLF;
  44.     pWnd->Wrap = wrap;
  45.     pWnd->ebitmask = mask;
  46.     return TRUE;
  47.     }
  48.     return FALSE;
  49. }
  50.  
  51. int NEAR Display(pWnd,len,str)
  52. PTTYWND pWnd;
  53. short len;
  54. BYTE *str;
  55. {
  56.     HPS hPS;
  57.     register BYTE *ptr;
  58.     register short ctr;
  59.     short toff, txpos;
  60.     BYTE *tbuf;
  61.  
  62.     short cols = pWnd->MaxCols;
  63.     short cwidth = pWnd->CWidth;
  64.     BYTE mask = pWnd->ebitmask;
  65.     while (len)
  66.     {
  67.     ptr = str;
  68.     ctr = 0;
  69.     txpos = (short)pWnd->Pos.x;
  70.     tbuf = pWnd->pVidBuf + pWnd->oCurrentLine;
  71.     toff = pWnd->CurLineOffset;
  72.     while((*ptr &= mask) >= SP)
  73.     {
  74.         if ((len) && (toff < cols))
  75.         {
  76.         ctr += 1;
  77.         *(tbuf + toff++) = *ptr++;
  78.         txpos += cwidth;
  79.         len -= 1;
  80.         }
  81.         else
  82.         break;
  83.     }
  84.     if (ctr)
  85.     {
  86.         hPS = WinGetPS(pWnd->hWnd);
  87.         GpiSetBackMix(hPS, BM_OVERPAINT);
  88.         GpiCharStringAt(hPS,(PPOINTL)&pWnd->Pos,(LONG)ctr,(PCH)str);
  89.         GpiRestorePS(hPS, -1L);
  90.         WinReleasePS (hPS);
  91.         if (toff < cols)
  92.         {
  93.         pWnd->CurLineOffset = toff;
  94.         pWnd->Pos.x = (LONG)txpos;
  95.         }
  96.         else if(pWnd->Wrap)
  97.         {
  98.             DoCR(pWnd);
  99.             DoLF(pWnd);
  100.         }
  101.     }
  102.     while ((*ptr &= mask) < SP)
  103.     {
  104.         if (len)
  105.         {
  106.         switch(*ptr)
  107.         {
  108.             case BEL:
  109.             WinAlarm(HWND_DESKTOP, WA_ERROR);
  110.             break;
  111.             case HT:
  112.             DoTab(pWnd);
  113.             break;
  114.             case CR:
  115.             DoCR(pWnd);
  116.             if (pWnd->LFonCR)
  117.                 DoLF(pWnd);
  118.             break;
  119.             case LF:
  120.             if (pWnd->CRonLF)
  121.                 DoCR(pWnd);
  122.             DoLF(pWnd);
  123.             break;
  124.             case BS:
  125.             DoBS(pWnd);
  126.             break;
  127.         }
  128.         len -= 1;
  129.         ptr++;
  130.         }
  131.         else
  132.         break;
  133.     }
  134.     str = ptr;
  135.     }
  136.     return (len);
  137. }
  138.  
  139. static void NEAR DoCR(pWnd)
  140. PTTYWND pWnd;
  141. {
  142.     pWnd->CurLineOffset = 0;
  143.     pWnd->Pos.x = 0;
  144. }
  145.  
  146. static void NEAR DoLF(pWnd)
  147. PTTYWND pWnd;
  148. {
  149.     BYTE *pCurr;
  150.     short offset, cols;
  151.     int i;
  152.     HWND hWnd = pWnd->hWnd;
  153.     cols = pWnd->MaxCols;
  154.  
  155.     if ((pWnd->oCurrentLine+=pWnd->MaxLineLength) > pWnd->oVidLastLine)
  156.     pWnd->oCurrentLine = 0;
  157.     pCurr = pWnd->pVidBuf + pWnd->oCurrentLine;
  158.     offset = pWnd->CurLineOffset;
  159.  
  160.     for (i = 0; i < offset; i++)
  161.     *(pCurr + i) = SP;
  162.     for (i = offset; i < cols; i++)
  163.     *(pCurr + i) = NUL;
  164.  
  165.     WinScrollWindow(hWnd,0,pWnd->CHeight,NULL,NULL,NULL,NULL,SW_INVALIDATERGN);
  166.     WinUpdateWindow(hWnd);
  167. }
  168.  
  169. static void NEAR DoBS(pWnd)
  170. PTTYWND pWnd;
  171. {
  172.     if (pWnd->CurLineOffset > 0)
  173.     {
  174.     pWnd->Pos.x -= (LONG)pWnd->CWidth;
  175.     pWnd->CurLineOffset -= 1;
  176.     }
  177. }
  178.  
  179. static void NEAR DoTab(pWnd)
  180. PTTYWND pWnd;
  181. {
  182.     short curoffset, xpos, ypos, cols, cwidth;
  183.     BYTE *pCurr;
  184.     RECTL myrect;
  185.     cols = pWnd->MaxCols;
  186.     curoffset = pWnd->CurLineOffset;
  187.     cwidth = pWnd->CWidth;
  188.  
  189.     if (curoffset < (cols - 1))
  190.     {
  191.     xpos = (short)pWnd->Pos.x;
  192.     ypos = (short)pWnd->Pos.y;
  193.     pCurr = pWnd->pVidBuf + pWnd->oCurrentLine;
  194.     do
  195.     {
  196.         if (*(pCurr + curoffset) == NUL)
  197.         *(pCurr + curoffset) = SP;
  198.         curoffset += 1;
  199.         xpos += cwidth;
  200.     }while ((curoffset % 8 != 0) && (curoffset < (cols - 1)));
  201.     WinSetRect(hAB,&myrect,xpos,ypos,pWnd->Width,ypos+pWnd->CHeight);
  202.     WinInvalidateRect(pWnd->hWnd, &myrect, TRUE);
  203.     pWnd->Pos.x = xpos;
  204.     pWnd->CurLineOffset = curoffset;
  205.     }
  206. }
  207.  
  208. void NEAR WndPaint(pWnd,hPS,top)
  209. PTTYWND pWnd;
  210. HPS hPS;
  211. SHORT top;
  212. {
  213.     POINTL pt;
  214.     BYTE *lineptr;
  215.     BYTE *pBuf, *pEnd;
  216.     short lines, length;
  217.     short cheight;
  218.     int i;
  219.  
  220.     GpiErase (hPS);
  221.     pt.x = 0;
  222.     pBuf = pWnd->pVidBuf;
  223.     pEnd = pBuf + pWnd->oVidLastLine;
  224.  
  225.     lineptr = pBuf + pWnd->oCurrentLine;
  226.     pt.y = pWnd->Pos.y;
  227.  
  228.     length = pWnd->MaxLineLength;
  229.     cheight = pWnd->CHeight;
  230.     lines = top / cheight;
  231.     if (top % cheight)
  232.     lines += 1;
  233.     lines = min(pWnd->MaxLines, lines);
  234.     for (i = 0; i < lines; i++)
  235.     {
  236.     GpiCharStringAt(hPS,&pt,(LONG)strlen(lineptr),(PCH)lineptr);
  237.     pt.y += (LONG)cheight;
  238.     if ((lineptr -= length) < pBuf)
  239.         lineptr = pEnd;
  240.     }
  241. }
  242.