home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / OOD / TEXTWIN.H$ / TEXTWIN
Encoding:
Text File  |  1991-10-24  |  5.1 KB  |  171 lines

  1. /********************************************************************
  2.  
  3.  FILE: TEXTWIN.H
  4.  
  5.  Defines text windows and editable text windows (derived from Win).
  6.  
  7.   TextWin
  8.      |
  9.   EditWin
  10.  
  11. ********************************************************************/
  12.  
  13. #if !defined( _TEXTWIN_H_ )
  14.  
  15. #define _TEXTWIN_H_
  16.  
  17. #include "interact.h"
  18.  
  19. /********************************************************************
  20.  
  21.  TextWin
  22.  
  23.  Defines scrollable text windows. A text window receives a
  24.  rectangular text buffer at construction, and can have vertical
  25.  and/or horizontal scrollbars depending on the dimensions of the
  26.  text buffer compared to those of the window. A text window has
  27.  a cursor that responds to the arrow keys and the PGUP, PGDN,
  28.  HOME and END keys. The scrollbars can be used to scroll the
  29.  text, and their sliders reflect the position of the displayed
  30.  text when the cursor is used to scroll.
  31.  
  32.  Public Interface:
  33.  
  34.      TextWin - constructor taking a location, the dimensions of the
  35.          window, and a text buffer.
  36.  
  37.      TextWin - copy constructor.
  38.  
  39.      operator= - assignment operator.
  40.  
  41.  (Redefined from Win)
  42.  
  43.      resize - redefined to add, remove, or resize scroll bars.
  44.  
  45.      activate - redefined to activate scroll bars.
  46.  
  47.      deactivate - redefined to deactivate scroll bars.
  48.  
  49.      paint - redefined to paint text window.
  50.  
  51.      handleEvent - redefined to implement text window behavior.
  52.  
  53.  Protected Interface:
  54.  
  55.      setCanvasOffset - sets the canvas offset (the text buffer
  56.          coordinates of the character displayed in the upper left
  57.          corner of the window) to the specified position. If the
  58.          cursor position doesn't fall within the region of text
  59.          specified by the canvas offset and the window dimensions,
  60.          the cursor position is updated accordingly. This keeps the
  61.          cursor on screen when the scroll bars are used to scroll.
  62.  
  63.      getCanvasOffset - returns canvas offset.
  64.  
  65.      setCursorPos - moves the cursor to the specified position within
  66.          the text buffer. If the new cursor position doesn't fall
  67.          within the region of text specified by the canvas offset and
  68.          the window dimensions, the canvas offset is updated
  69.          accordingly. This allows scrolling to be performed by moving
  70.          the cursor. Returns TRUE if the canvas offset has been
  71.          changed, indicating that paintText must be called.
  72.          Otherwise, only updateCursor needs to be called.
  73.  
  74.      getCursorPos - returns the cursor position.
  75.  
  76.      updateCursor - updates the screen display of the cursor to reflect
  77.          the cursor's position.
  78.  
  79.      updateScrollBars - repaints the scroll bars to reflect the cursor's
  80.          position.
  81.  
  82.      paintText - repaints the text to reflect the current canvas offset.
  83.  
  84.  (data members)
  85.  
  86.      hscroller, vscroller - pointers to horizontal and vertical scroll
  87.          bars. equal to 0 if no such scroll bar exists.
  88.  
  89.      canvas - Buffer object containing text being displayed.
  90.  
  91.      textView - rectangle indicating region where text is displayed
  92.          (as opposed to window frame, buttons, etc.).
  93.  
  94. ********************************************************************/
  95.  
  96. const int TEXTWIN_MINWIDTH = 5;
  97. const int TEXTWIN_MINHEIGHT = 5;
  98.  
  99. class TextWin : public Win
  100. {
  101. public:
  102.     TextWin( Point location, int wid, int len, Buffer *sheet );
  103.     TextWin( const TextWin &other );
  104.     TextWin &operator=( const TextWin &other );
  105.     void resize( int newWidth, int newHeight );
  106.     void activate();
  107.     void deactivate();
  108.     void paint( Rect region );
  109.     void handleEvent( const Event &action );
  110.    ~TextWin();
  111. protected:
  112.     void  setCanvasOffset( Point pos );  // position of window on canvas
  113.     Point getCanvasOffset() const;
  114.     int   setCursorPos( Point pos );
  115.     Point getCursorPos() const;
  116.     void  updateCursor() const;
  117.     void  updateScrollBars();
  118.     void  paintText() const;
  119.  
  120.     ScrollBar *hscroller;
  121.     ScrollBar *vscroller;
  122.     Buffer &canvas;
  123.     Rect   textView;
  124. private:
  125.     void  handleKey( const KbdEvent &key );
  126.     void  handleMouse( const MouseEvent &action );
  127.     void  handleScroll( const ScrollEvent &action );
  128.  
  129.     Point canvasOffset;                // position of window on canvas
  130.     Point cursorPos;
  131. };
  132.  
  133. inline Point TextWin::getCanvasOffset() const
  134. {
  135.     return canvasOffset;
  136. }
  137.  
  138. inline Point TextWin::getCursorPos() const
  139. {
  140.     return cursorPos;
  141. }
  142.  
  143. /********************************************************************
  144.  
  145.  EditWin
  146.  
  147.  Defines scrollable text windows that accept input from the
  148.  keyboard.
  149.  
  150.  Public Interface:
  151.  
  152.      EditWin - constructor taking a location, the dimensions of the
  153.          window, and a text buffer
  154.  
  155.  (inherited from TextWin)
  156.  
  157.      handleEvent - redefined to allows text input
  158.  
  159. ********************************************************************/
  160.  
  161. class EditWin : public TextWin
  162. {
  163. public:
  164.     EditWin( Point location, int wid, int len, Buffer *sheet )
  165.      : TextWin( location, wid, len, sheet) {}
  166.     void handleEvent( const Event &action );
  167. };
  168.  
  169.  
  170. #endif // _TEXTWIN_H_
  171.