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