home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / pclw11 / paint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-03  |  4.0 KB  |  158 lines

  1. /*** paint ***/
  2.  
  3. #include "windows.h"
  4. #include "paint.h"
  5. #include "ascii.h"
  6.  
  7. extern HWND hMainWnd;
  8.  
  9. #define MIN(a,b) ((a<=b)?(a):(b))
  10.  
  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.  CharHeight = tm.tmHeight + tm.tmExternalLeading;
  41.  CharWidth = tm.tmMaxCharWidth;
  42.  ReleaseDC(hMainWnd,hDC);
  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 = MIN(NCOLS,(1+ColWidth) / CharWidth);
  66.  
  67.  X = ps->rcPaint.left;
  68.  /* consider each row */
  69.  for(Row=FirstRow;Row<FirstRow+NbrRows;Row++)
  70.    {/* paint part of row */
  71.     if((Row>=0)&&(Row<NROWS))
  72.       {/* good row number */
  73.        Y = CharHeight*Row;
  74.        /* compute bounding rectangle */
  75.        rect.left = X;
  76.        rect.top  = Y;
  77.        rect.right  = X + ColWidth;
  78.        rect.bottom = Y + CharHeight;
  79.        /* paint it */
  80.        SetBkMode(hDC,OPAQUE);
  81.        ExtTextOut(hDC,X,Y,ETO_OPAQUE|ETO_CLIPPED,&rect,RowPtr[Row]+FirstCol,NbrCols,NULL);
  82.       }
  83.    }
  84. } /* end PaintMain */
  85.  
  86. void DoTheScroll(void)
  87. {int Row;
  88.  int Col;
  89.  char *Ptr;
  90.  RECT rect;
  91.  /* scroll display buffer */
  92.  TheRow = NROWS-1;
  93.  Ptr = RowPtr[0];
  94.  for(Row=0;Row<NROWS-1;Row++) RowPtr[Row] = RowPtr[Row+1];
  95.  RowPtr[NROWS-1] = Ptr;
  96.  for(Col=0;Col<NCOLS;Col++) *Ptr++ = ' ';
  97.  /* scroll the display */
  98.  ScrollWindow(hMainWnd,0,0-CharHeight,NULL,NULL);
  99.  /* invalidate last row */
  100.  rect.left = 0;
  101.  rect.top  = CharHeight * (NROWS-2);
  102.  rect.right  = CharWidth * (RightCol+1);
  103.  rect.bottom = CharHeight * (NROWS-1);
  104.  InvalidateRect(hMainWnd,&rect,TRUE);
  105.  /* reset boundary */
  106.  TopRow = TheRow;
  107.  LeftCol = TheCol;
  108.  RightCol = TheCol;
  109. } /* end DoTheScroll */
  110.  
  111. void WriteTheString(char *String,int Count)
  112. {int i;
  113.  char TheChar;
  114.  RECT rect;
  115.  TopRow = TheRow;
  116.  LeftCol = TheCol;
  117.  RightCol = TheCol;
  118.  for(i=0;i<Count;i++)
  119.    {TheChar = *String++;
  120.     switch(TheChar)
  121.      {case BS:
  122.         if(TheCol>0)
  123.           {*(RowPtr[TheRow]+TheCol) = ' ';
  124.            TheCol--;
  125.           }
  126.         break;
  127.       case CR:
  128.         TheCol = 0;
  129.         LeftCol = 0;
  130.         break;
  131.       case LF:
  132.         /* next line */
  133.         if(++TheRow>=NROWS) DoTheScroll();
  134.         break;
  135.       default:
  136.         /* put char into display buffer */
  137.         *(RowPtr[TheRow]+TheCol) = (char)TheChar;
  138.         /* increment 'cursor' */
  139.         if(++TheCol>=NCOLS)
  140.           {/* next line */
  141.            TheCol = 0;
  142.            LeftCol = 0;
  143.            if(++TheRow>=NROWS) DoTheScroll();
  144.           }
  145.         else RightCol++;
  146.         break;
  147.      } /* end switch */
  148.    } /* end for */
  149.  /* compute invalid rectangle */
  150.  if((TopRow!=TheRow)||(LeftCol!=TheCol)||(RightCol!=TheCol))
  151.    {rect.left = CharWidth * LeftCol;
  152.     rect.top  = CharHeight * TopRow;
  153.     rect.right  = CharWidth * (RightCol+1);
  154.     rect.bottom = CharHeight * (TheRow+1);
  155.     InvalidateRect(hMainWnd,&rect,TRUE);
  156.    }
  157. }  /* end WriteTheString */
  158.