home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / 14_5.cpp < prev    next >
C/C++ Source or Header  |  1994-05-26  |  6KB  |  197 lines

  1.  
  2.      #include <owl\framewin.h>
  3.      #include <owl\applicat.h>
  4.      #include <owl\dc.h>
  5.      #include <mem.h>
  6.  
  7.      #pragma hdrstop
  8.  
  9.      #include "real.rh"
  10.  
  11.      const int maxLines = 25;
  12.      const int maxWidth = 80;
  13.      const int maxData = maxLines * (maxWidth + 1);
  14.  
  15.      class BaseWindow : public TWindow
  16.        {
  17.      protected:
  18.        int   currentLine;         // line being typed in now
  19.        int   lineLen[maxLines];   // length of each line
  20.        char *linePtrs[maxLines];  // string for each line
  21.        BOOL  isMinimized;         // TRUE if window is an icon
  22.        TSize windowSize;          // structure with size in pixels
  23.  
  24.        void EvChar(UINT key, UINT repeatCount, UINT flags);
  25.        void Paint(TDC& dc, BOOL, TRect&);
  26.        void EvSize(UINT sizeType, TSize& size);
  27.  
  28.        void CmAbout();
  29.        void CmClear();
  30.  
  31.        // Menu choice, end the program
  32.        void CmFileExit() { PostQuitMessage(0); }
  33.  
  34.      public:
  35.        BaseWindow(TWindow *parent = 0);
  36.        ~BaseWindow() {}
  37.        DECLARE_RESPONSE_TABLE(BaseWindow); // says we'll have a
  38.        };                                  // response table
  39.  
  40.      DEFINE_RESPONSE_TABLE1(BaseWindow, TWindow)
  41.        EV_WM_CHAR,
  42.        EV_WM_SIZE,
  43.        EV_COMMAND( CM_ABOUT,    CmAbout),
  44.        EV_COMMAND( CM_FILEEXIT, CmFileExit),
  45.        EV_COMMAND( CM_CLEAR,    CmClear),
  46.      END_RESPONSE_TABLE;
  47.  
  48.      class MyApp : public TApplication
  49.        {
  50.      public:
  51.        MyApp() : TApplication() {}
  52.  
  53.        void InitMainWindow();
  54.        };
  55.  
  56.      BaseWindow::BaseWindow(TWindow *parent)
  57.        {
  58.        int lineNum;
  59.  
  60.        Init(parent, 0, 0);
  61.        linePtrs[0] = new char[maxData]; // allocate edit buffer
  62.        lineLen[0] = currentLine = 0;
  63.  
  64.        // apportion the buffer out to the line pointer array
  65.        for (lineNum = 1; lineNum < maxLines; ++lineNum)
  66.          {
  67.          linePtrs[lineNum] = linePtrs[lineNum - 1] + maxWidth;
  68.          lineLen[lineNum] = 0;
  69.          }
  70.        }
  71.  
  72.      // Menu choice, display an About box,
  73.      // use a message box to do it
  74.      void
  75.        BaseWindow::CmAbout()
  76.        {
  77.        MessageBox("Teach Yourself BC++ 4 in 21 Days", "About");
  78.        }
  79.  
  80.      // Menu choice, clear the display
  81.      void
  82.        BaseWindow::CmClear()
  83.        {
  84.        for (int lineNum = 0; lineNum < maxLines; ++lineNum)
  85.          lineLen[lineNum] = 0;    // empty all lines
  86.  
  87.        currentLine = 0;           // move back to top line
  88.        Invalidate();              // window is invalid, repaint
  89.        }
  90.  
  91.      // this is called whenever the window changes size
  92.      void
  93.        BaseWindow::EvSize(UINT sizeType, TSize& size)
  94.        {
  95.        if (sizeType == SIZE_MINIMIZED) // if shrunk to icon
  96.          isMinimized = TRUE;
  97.        else
  98.          {
  99.          windowSize = size;            // save window size
  100.          isMinimized = FALSE;
  101.          }
  102.        }
  103.  
  104.      // called when time to update (paint) the screen
  105.      void
  106.        BaseWindow::Paint(TDC& dc, BOOL, TRect&)
  107.        {
  108.        int   lineNum;         // line number to write
  109.        int   yPos;            // vertical position on screen
  110.        int   displayedLines;  // number of linePtrs in this window
  111.        TSize textSize;        // used to get char height in pixels
  112.  
  113.        if (isMinimized)       // don't write to an icon
  114.           return;
  115.  
  116.        // get char sizes so that height is saved
  117.        textSize = dc.GetTextExtent("W", 1);
  118.        displayedLines = windowSize.cy / textSize.cy;
  119.  
  120.        if (displayedLines > maxLines)
  121.           displayedLines = maxLines;
  122.  
  123.        for (lineNum = yPos = 0; lineNum < displayedLines; ++lineNum)
  124.           {
  125.           if (lineLen[lineNum] > 0)  // if any text on the line
  126.              dc.TextOut(0, yPos, linePtrs[lineNum], lineLen[lineNum]);
  127.  
  128.           yPos += textSize.cy;   // adjust screen line position
  129.           }
  130.        }
  131.  
  132.      // called when a normal key is pressed
  133.      void
  134.        BaseWindow::EvChar(UINT key, UINT repeatCount, UINT)
  135.        {
  136.        BOOL invalidDisplay = FALSE;
  137.        BOOL eraseBackground = FALSE;
  138.  
  139.        while (repeatCount--)
  140.          {
  141.          if ((key >= ' ') && (key <= '~')) // if a printable key
  142.            {
  143.            if (currentLine >= maxLines) // if buffer full
  144.              {
  145.              MessageBeep(-1);           //   complain
  146.              break;
  147.              }
  148.            else                         // else
  149.              {                          //   add char
  150.              linePtrs[currentLine][lineLen[currentLine]] = (char) key;
  151.  
  152.              if (++lineLen[currentLine] >= maxWidth)
  153.                ++currentLine;
  154.  
  155.              invalidDisplay = TRUE;
  156.              }
  157.            }
  158.          else if (key == '\b')  // rubout, delete char
  159.            {
  160.            if (currentLine >= maxLines)
  161.              break;
  162.            else if (lineLen[currentLine] == 0)
  163.              {
  164.              if (currentLine > 0)
  165.                --currentLine;
  166.              }
  167.            else
  168.              --lineLen[currentLine];
  169.  
  170.            invalidDisplay = eraseBackground = TRUE;
  171.            }
  172.          else if (key == '\r') // if carriage return (Enter key)
  173.             ++currentLine;
  174.          }
  175.  
  176.        if (currentLine >= maxLines)
  177.          currentLine = maxLines - 1;
  178.  
  179.        if (invalidDisplay)            // if buffer has changed
  180.          Invalidate(eraseBackground); //   force window repaint
  181.        }
  182.  
  183.      void
  184.        MyApp::InitMainWindow()
  185.        {
  186.        SetMainWindow(new TFrameWindow(0, "Program 14.5",
  187.                                       new BaseWindow()));
  188.        GetMainWindow()->AssignMenu("MENU_1");
  189.        }
  190.  
  191.      int
  192.        OwlMain(int, char **)
  193.        {
  194.        return MyApp().Run();
  195.        }
  196.  
  197.