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

  1. #include <owl\applicat.h>
  2. #include <owl\dc.h>
  3. #include <owl\framewin.h>
  4. #include <owl\scroller.h>
  5. #include <owl\window.h>
  6. #include <owl\window.rh>
  7. #include <stdio.h>
  8.  
  9. #include "window2.h"
  10.  
  11. const MAX_LINES = 30;
  12.  
  13. class TMyWindow : public TWindow
  14. {
  15. public:
  16.    TMyWindow(TWindow *parent = 0);
  17.    ~TMyWindow();
  18.  
  19. protected:
  20.    virtual void SetupWindow();
  21.  
  22.    BOOL CanClose();
  23.  
  24.    void CmExit();
  25.    void CmHeight8();
  26.    void CmHeight10();
  27.    void CmHeight14();
  28.    void CmHeight20();
  29.    void CmHeight26();
  30.    void EvKeyDown(UINT, UINT, UINT);
  31.    void EvLButtonDown(UINT, TPoint &);
  32.  
  33.    void Paint(TDC &, BOOL, TRect &);
  34.  
  35. private:
  36.    TFont *pFont;
  37.  
  38.    void NewFont(int);
  39.  
  40.    DECLARE_RESPONSE_TABLE(TMyWindow);
  41. };
  42. DEFINE_RESPONSE_TABLE1(TMyWindow, TWindow)
  43.    EV_WM_KEYDOWN,
  44.    EV_WM_LBUTTONDOWN,
  45.    EV_COMMAND(CM_EXIT, CmExit),
  46.    EV_COMMAND(CM_HEIGHT8, CmHeight8),
  47.    EV_COMMAND(CM_HEIGHT10, CmHeight10),
  48.    EV_COMMAND(CM_HEIGHT14, CmHeight14),
  49.    EV_COMMAND(CM_HEIGHT20, CmHeight20),
  50.    EV_COMMAND(CM_HEIGHT26, CmHeight26),
  51. END_RESPONSE_TABLE;
  52.  
  53. class TMyApp : public TApplication
  54. {
  55. public:
  56.    TMyApp() : TApplication() {}
  57.  
  58.    void InitMainWindow()
  59.       {
  60.       SetMainWindow(new TFrameWindow(  0,
  61.                            "A Simple Read-Only Text Window",
  62.                            new TMyWindow ));
  63.       GetMainWindow()->AssignMenu("EXITMENU");
  64.       }
  65. };
  66.  
  67. TMyWindow::TMyWindow(TWindow *parent)
  68. {
  69.    Init(parent, 0, 0);
  70.    Attr.Style |= WS_VSCROLL | WS_HSCROLL;    // Add scroll bars
  71.    pFont = NULL;
  72. }
  73.  
  74. TMyWindow::~TMyWindow()
  75. {
  76.    if (pFont)
  77.       delete pFont;
  78. }
  79.  
  80. void TMyWindow::SetupWindow()
  81. {
  82.    TWindow::SetupWindow();
  83.  
  84.    // Set up the scroller and font.  Note that
  85.    // dummy values of 7 and 16 are used for the
  86.    // scroll bar's units, but they'll be reset
  87.    // as soon as the font is set.
  88.    //
  89.    Scroller = new TScroller(this, 7, 16, 20, MAX_LINES - 1);
  90.    NewFont(8);                            // Initialize our font
  91. }
  92.  
  93. BOOL TMyWindow::CanClose()
  94. {
  95.    return IDYES == MessageBox("Want to close this application?",
  96.                               "Query",
  97.                               MB_YESNO | MB_ICONQUESTION );
  98. }
  99.  
  100. void TMyWindow::CmExit()
  101. {
  102.    SendMessage(WM_CLOSE);
  103. }
  104.  
  105. void TMyWindow::CmHeight8()
  106. {
  107.    NewFont(8);
  108. }
  109.  
  110. void TMyWindow::CmHeight10()
  111. {
  112.    NewFont(10);
  113. }
  114.  
  115. void TMyWindow::CmHeight14()
  116. {
  117.    NewFont(14);
  118. }
  119.  
  120. void TMyWindow::CmHeight20()
  121. {
  122.    NewFont(20);
  123. }
  124.  
  125. void TMyWindow::CmHeight26()
  126. {
  127.    NewFont(26);
  128. }
  129.  
  130. void TMyWindow::EvKeyDown( UINT key,
  131.                            UINT /*repeatCount*/,
  132.                            UINT /*flags*/ )
  133. {
  134.    if (Scroller)        // Can't scroll if it ain't there!
  135.       switch (key)
  136.          {
  137.          case VK_HOME:
  138.             Scroller->VScroll(SB_TOP, 0);
  139.             break;
  140.          case VK_END:
  141.             Scroller->VScroll(SB_BOTTOM, 0);
  142.             break;
  143.          case VK_PRIOR:
  144.             Scroller->VScroll(SB_PAGEUP, 0);
  145.             break;
  146.          case VK_NEXT:
  147.             Scroller->VScroll(SB_PAGEDOWN, 0);
  148.             break;
  149.          case VK_UP:
  150.             Scroller->VScroll(SB_LINEUP, 0);
  151.             break;
  152.          case VK_DOWN:
  153.             Scroller->VScroll(SB_LINEDOWN, 0);
  154.             break;
  155.          }
  156. }
  157.  
  158. void TMyWindow::EvLButtonDown(UINT, TPoint &)
  159. {
  160.    MessageBox( "You clicked the left button!",
  161.                "Mouse Click Event",
  162.                MB_OK );
  163. }
  164.  
  165. void TMyWindow::Paint(TDC &dc, BOOL /*erase*/, TRect &/*rect*/)
  166. {
  167.    char s[81];
  168.    BOOL ok = TRUE;
  169.    int y = 0;
  170.  
  171.    for (int i = 0; i < MAX_LINES && ok; ++i)
  172.       {
  173.       if (pFont)
  174.          dc.SelectObject(*pFont);
  175.       sprintf(s, "This is line number %d", i);
  176.       ok = dc.TextOut(0, y, s);
  177.       y += dc.GetTextExtent(s, lstrlen(s)).cy;
  178.       if (pFont)
  179.          dc.RestoreFont();
  180.       }
  181. }
  182.  
  183. void TMyWindow::NewFont(int nHeight)
  184. {
  185.    if (pFont)
  186.       delete pFont;
  187.    pFont = new TFont("Arial", nHeight);
  188.  
  189.    // Now reset the the scroller's units
  190.    if (pFont && Scroller)
  191.       {
  192.       TClientDC dc(*this);
  193.       TEXTMETRIC tm;
  194.  
  195.       dc.SelectObject(*pFont);
  196.       dc.GetTextMetrics(tm);
  197.       dc.RestoreFont();
  198.  
  199.       Scroller->SetUnits(  tm.tmAveCharWidth,
  200.                            tm.tmHeight + tm.tmExternalLeading );
  201.  
  202.       Invalidate();
  203.       }
  204. }
  205.  
  206. int OwlMain(int, char *[])
  207. {
  208.    return TMyApp().Run();
  209. }
  210.