home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / windows / winterm / paint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-27  |  3.9 KB  |  156 lines

  1. /*** paint ***/
  2.  
  3. #include "windows.h"
  4. #include "paint.h"
  5. #include "ascii.h"
  6.  
  7. extern HWND hMainWnd;
  8.  
  9. #define NROWS 20
  10. #define NCOLS 80
  11.  
  12. /* private variables */
  13. static int TheRow = 0;     /* current row */
  14. static int TheCol = 0;     /* current col */
  15. static int TopRow;
  16. static int LeftCol;
  17. static int RightCol;
  18. static char Buffer[NROWS][NCOLS];  /* display buffer */
  19. static char *RowPtr[NROWS];        /* array of row pointers */
  20. static TEXTMETRIC tm;
  21. static int CharHeight;
  22. static int CharWidth;
  23.  
  24. int GetYposition(void)
  25. {return(TheRow*CharHeight);
  26. }
  27.  
  28. int GetXposition(void)
  29. {return(TheCol*CharWidth);
  30. }
  31.  
  32. void InitPaint()
  33. {int Col;
  34.  int Row;
  35.  HDC hDC;
  36.  hDC = GetDC(hMainWnd);
  37.  SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  38.  GetTextMetrics(hDC,&tm);
  39.  ReleaseDC(hMainWnd,hDC);
  40.  CharHeight = tm.tmHeight + tm.tmExternalLeading;
  41.  CharWidth = tm.tmMaxCharWidth;
  42.  /* initialize screen buffer */
  43.  for(Row=0;Row<NROWS;Row++)
  44.    {for(Col=0;Col<NCOLS;Col++) Buffer[Row][Col] = ' ';
  45.     RowPtr[Row] = &Buffer[Row][0];
  46.    }
  47. } /* end InitPaint */
  48.  
  49. void PaintMain(HDC hDC,PAINTSTRUCT *ps)
  50. {int Row;
  51.  int FirstRow;
  52.  int FirstCol;
  53.  int NbrRows;
  54.  int NbrCols;
  55.  int ColWidth;
  56.  int X;
  57.  int Y;
  58.  RECT rect;
  59.  /* compute row & col stuff */
  60.  FirstRow = ps->rcPaint.top  / CharHeight;
  61.  FirstCol = ps->rcPaint.left / CharWidth;
  62.  NbrRows = (ps->rcPaint.bottom - ps->rcPaint.top)  / CharHeight;
  63.  ColWidth = ps->rcPaint.right  - ps->rcPaint.left;
  64.  NbrCols = (1+ColWidth) / CharWidth;
  65.  X = ps->rcPaint.left;
  66.  /* consider each row */
  67.  for(Row=FirstRow;Row<FirstRow+NbrRows;Row++)
  68.    {/* paint part of row */
  69.     if((Row>=0)&&(Row<NROWS))
  70.       {/* good row number */
  71.        Y = CharHeight*Row;
  72.        /* compute bounding rectangle */
  73.        rect.left = X;
  74.        rect.top  = Y;
  75.        rect.right  = X + ColWidth;
  76.        rect.bottom = Y + CharHeight;
  77.        /* paint it */
  78.        SetBkMode(hDC,OPAQUE);
  79.        ExtTextOut(hDC,X,Y,ETO_OPAQUE|ETO_CLIPPED,&rect,RowPtr[Row]+FirstCol,NbrCols,NULL);
  80.       }
  81.    }
  82. } /* end PaintMain */
  83.  
  84. void DoTheScroll(void)
  85. {int Row;
  86.  int Col;
  87.  char *Ptr;
  88.  RECT rect;
  89.  /* scroll display buffer */
  90.  TheRow = NROWS-1;
  91.  Ptr = RowPtr[0];
  92.  for(Row=0;Row<NROWS-1;Row++) RowPtr[Row] = RowPtr[Row+1];
  93.  RowPtr[NROWS-1] = Ptr;
  94.  for(Col=0;Col<NCOLS;Col++) *Ptr++ = ' ';
  95.  /* scroll the display */
  96.  ScrollWindow(hMainWnd,0,0-CharHeight,NULL,NULL);
  97.  /* invalidate last row */
  98.  rect.left = 0;
  99.  rect.top  = CharHeight * (NROWS-2);
  100.  rect.right  = CharWidth * (RightCol+1);
  101.  rect.bottom = CharHeight * (NROWS-1);
  102.  InvalidateRect(hMainWnd,&rect,TRUE);
  103.  /* reset boundary */
  104.  TopRow = TheRow;
  105.  LeftCol = TheCol;
  106.  RightCol = TheCol;
  107. } /* end DoTheScroll */
  108.  
  109. void WriteTheString(char *String,int Count)
  110. {int i;
  111.  char TheChar;
  112.  RECT rect;
  113.  TopRow = TheRow;
  114.  LeftCol = TheCol;
  115.  RightCol = TheCol;
  116.  for(i=0;i<Count;i++)
  117.    {TheChar = *String++;
  118.     switch(TheChar)
  119.      {case BS:
  120.         if(TheCol>0)
  121.           {*(RowPtr[TheRow]+TheCol) = ' ';
  122.            TheCol--;
  123.           }
  124.         break;
  125.       case CR:
  126.         TheCol = 0;
  127.         LeftCol = 0;
  128.         break;
  129.       case LF:
  130.         /* next line */
  131.         if(++TheRow>=NROWS) DoTheScroll();
  132.         break;
  133.       default:
  134.         /* put char into display buffer */
  135.         *(RowPtr[TheRow]+TheCol) = (char)TheChar;
  136.         /* increment 'cursor' */
  137.         if(++TheCol>=NCOLS)
  138.           {/* next line */
  139.            TheCol = 0;
  140.            LeftCol = 0;
  141.            if(++TheRow>=NROWS) DoTheScroll();
  142.           }
  143.         else RightCol++;
  144.         break;
  145.      } /* end switch */
  146.    } /* end for */
  147.  /* compute invalid rectangle */
  148.  if((TopRow!=TheRow)||(LeftCol!=TheCol)||(RightCol!=TheCol))
  149.    {rect.left = CharWidth * LeftCol;
  150.     rect.top  = CharHeight * TopRow;
  151.     rect.right  = CharWidth * (RightCol+1);
  152.     rect.bottom = CharHeight * (TheRow+1);
  153.     InvalidateRect(hMainWnd,&rect,TRUE);
  154.    }
  155. }  /* end WriteTheString */
  156.