home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / OOD / INTERACT.H$ / INTERACT
Encoding:
Text File  |  1991-11-06  |  10.2 KB  |  395 lines

  1. /********************************************************************
  2.  
  3.  FILE: INTERACT.H
  4.  
  5.  Defines the base class for interactors, defines the predefined
  6.  control elements, and defines the base class for windows.
  7.  
  8.                  (Interactor)
  9.                       |
  10.                -------------
  11.                |           |
  12.            (Control)     (Win)
  13.                |
  14.          ------------
  15.          |          |
  16.      ScrollBar  PushButton   
  17.  
  18. ********************************************************************/
  19.  
  20.  
  21. #if !defined( _INTERACTOR_H_ )
  22.  
  23. #define _INTERACTOR_H_
  24.  
  25. #include "list.h"
  26. #include "event.h"
  27.  
  28. typedef int Boolean;
  29.  
  30. const int FALSE = 0;
  31. const int TRUE = 1;
  32.  
  33. struct ScreenChar
  34. {
  35.    char character;
  36.    unsigned char attribute;
  37. };
  38.  
  39. /********************************************************************
  40.  
  41.  Buffer
  42.  
  43.  Defines a rectangular buffer of ScreenChars.
  44.  
  45.  Public Interface:
  46.  
  47.      Buffer - constructor taking dimensions of the buffer and a
  48.          text attribute to use as a default.
  49.  
  50.      Buffer - copy constructor.
  51.  
  52.      operator= - assignment operator.
  53.  
  54.      columns - returns columns.
  55.  
  56.      rows - returns rows.
  57.  
  58.      setChar - sets character and attribute at specified location.
  59.          Uses default attribute if none specified.
  60.  
  61.      getChar - returns ScreenChar at specified location.
  62.  
  63.      putStr - writes string at specified location. Uses default
  64.          attribute if none specified. The text is wrapped at the
  65.          end of each line.
  66.  
  67.      ~Buffer - destructor.
  68.  
  69. ********************************************************************/
  70.  
  71. class Buffer
  72. {
  73. public:
  74.     Buffer( int wid, int len,
  75.             unsigned char defAttr = WHITE_FORE | BLUE_BACK );
  76.     Buffer( const Buffer &other );
  77.     Buffer &operator=( const Buffer &other );
  78.     int  columns() const;
  79.     int  rows() const;
  80.     void setChar( Point loc,char newChar, unsigned char newAttr = 0 );
  81.     ScreenChar getChar( Point loc ) const;
  82.     void putStr( Point loc, char *newStr, unsigned char newAttr = 0 );
  83.    ~Buffer();
  84. private:
  85.    int width,
  86.        length;
  87.    unsigned char defaultAttr;
  88.    ScreenChar *textArray;
  89. };
  90.  
  91. inline int Buffer::columns() const
  92. {
  93.     return width;
  94. }
  95.  
  96. inline int Buffer::rows() const
  97. {
  98.     return length;
  99. }
  100.  
  101. /********************************************************************
  102.  
  103.  Interactor
  104.  
  105.  Defines a rectangular screen area that responds to events. This
  106.  abstract class acts as a base class for windows and control
  107.  elements.
  108.  
  109.  Public Interface:
  110.  
  111.      width - returns width.
  112.  
  113.      height - returns height.
  114.  
  115.      origin - returns coordinates of upper-left corner.
  116.  
  117.      withinBounds - tests whether a point falls within the
  118.          Interactor's bounds.
  119.  
  120.      move - moves Interactor to specified location.
  121.  
  122.      resize - resizes Interactor to specified dimensions.
  123.  
  124.      activate - makes Interactor active (changes appearance).
  125.  
  126.      deactivate - makes Interactor inactive (changes appearance).
  127.  
  128.      paint - displays Interactor on screen.
  129.  
  130.      paintChar - displays single char.
  131.  
  132.      handleEvent - responds to passed Event.
  133.  
  134. ********************************************************************/
  135.  
  136. class Interactor : public ListElem
  137. {
  138. public:
  139.     int   width() const;
  140.     int   height() const;
  141.     Point origin() const;
  142.     Boolean withinBounds( Point loc ) const;
  143.     virtual void move( Point newLoc );
  144.     virtual void resize( int newWidth, int newHeight );
  145.     virtual void activate();
  146.     virtual void deactivate();
  147.     virtual void paint( Rect region ) = 0;
  148.     virtual void paintChar( Point loc,
  149.                             char character, unsigned char attr ) const = 0;
  150.     virtual void handleEvent( const Event &action ) = 0;
  151.     // note: virtual destructor inherited from ListElem
  152. protected:
  153.     Rect area;
  154.     int  active;
  155.     Rect paintingRegion;   // region in which painting occurs
  156.                           // used by paintChar
  157. };
  158.  
  159.  
  160. inline int Interactor::width() const
  161. {
  162.     return area.width() + 1;
  163. }
  164.  
  165. inline int Interactor::height() const
  166. {
  167.     return area.height() + 1;
  168. }
  169.  
  170. inline Point Interactor::origin() const
  171. {
  172.     return Point( area.left, area.top );
  173. }
  174.  
  175. inline Boolean Interactor::withinBounds( Point loc ) const
  176. {
  177.     return area.Contains( loc );
  178. }
  179.  
  180. inline void Interactor::move( Point newLoc )
  181. {
  182.     area = Rect(newLoc.x,
  183.                 newLoc.y,
  184.                 newLoc.x + area.width(),
  185.                 newLoc.y + area.height() );
  186. }
  187.  
  188. inline void Interactor::resize( int newWidth, int newHeight )
  189. {
  190.     area = Rect(area.left,
  191.                 area.top,
  192.                    area.left + newWidth - 1,
  193.                 area.top  + newHeight - 1 );
  194. }
  195.  
  196. inline void Interactor::activate()
  197. {
  198.     active = 1;
  199. }
  200.  
  201. inline void Interactor::deactivate()
  202. {
  203.     active = 0;
  204. }
  205.  
  206. /********************************************************************
  207.  
  208.  Win
  209.  
  210.  Describes properties of all top-level windows, e.g. having a
  211.  title and a frame. This abstract class acts as a base class for
  212.  all windows.
  213.  
  214.  Public Interface:
  215.  
  216.      setTitle - sets title to specified string.
  217.  
  218.      paintFrame - paints frame and title on screen.
  219.  
  220.  (Redefined from Win)
  221.  
  222.      paintChar - redefined to translate local coordinates to
  223.          global coordinates when painting character.
  224.  
  225.  Protected Interface:
  226.  
  227.      Win - constructor taking no arguments. Used to initialize
  228.          title to 0.
  229.  
  230.      ~Win - destructor. Used to free title if one exists.
  231.  
  232. ********************************************************************/
  233.  
  234. class Win : public Interactor
  235. {
  236. public:
  237.     int  setTitle( char *titl );
  238.     void paintFrame() const;
  239.     virtual void paint( Rect region ) = 0;
  240.     void paintChar( Point location,
  241.                    char character, unsigned char attr ) const;
  242.     virtual void handleEvent( const Event &action ) = 0;
  243. protected:
  244.     Win() : title( 0 ) {}
  245.     ~Win();
  246. private:
  247.     char *title;
  248. };
  249.  
  250. inline void Win::paintChar( Point loc,
  251.                             char character, unsigned char attr ) const
  252. {
  253.     if ( paintingRegion.Contains( loc ) )
  254.         theScreen.showChar( origin() + loc, character, attr );
  255. }
  256.  
  257. inline Win::~Win()
  258. {
  259.     if ( title )
  260.         delete title;
  261. }
  262.  
  263. /********************************************************************
  264.  
  265.  Control
  266.  
  267.  Defines properties of all control elements. This abstract class acts
  268.  as a base class for all controls.
  269.  
  270.  Public Interface:
  271.  
  272.  (Redefined from Win)
  273.  
  274.     paintChar - redefined to translate local coordinates to parent
  275.         window's coordinates, and calls parent's paintChar.
  276.  
  277.  Protected Interface:
  278.  
  279.     Control - constructor taking parent window.
  280.         (Because parentWin is a const pointer, it can only be
  281.         initialized in Control's constructor and cannot be assigned
  282.         a value in the constructors of any derived classes. A
  283.         protected constructor lets the derived classes construct a
  284.         Control object and initialize parentWin's value.)
  285.  
  286. ********************************************************************/
  287.  
  288. class Control : public Interactor
  289. {
  290. public:
  291.     virtual void paint( Rect region ) = 0;
  292.     virtual void handleEvent( const Event &action ) = 0;
  293.     void paintChar( Point location,
  294.                     char character, unsigned char attr ) const;
  295. protected:
  296.     Win *const parentWin;
  297.     Control( Win *parent ) : parentWin( parent ) {}
  298.  
  299. };
  300.  
  301.  
  302. inline void Control::paintChar( Point loc,
  303.                                 char character,
  304.                                 unsigned char attr ) const
  305. {
  306.     if ( paintingRegion.Contains( loc ) )
  307.         parentWin->paintChar( origin() + loc, character, attr );
  308. }
  309.  
  310. /********************************************************************
  311.  
  312.  ScrollBar
  313.  
  314.  Defines scroll bar control elements, either horizontal or
  315.  vertical. The scroll bars respond only to single mouse clicks,
  316.  not to mouse drags or holding down a mouse button. A click on
  317.  either end means "move one line," and a click on either side
  318.  of the slider means "move one page." When clicked, a scroll bar
  319.  sends a ScrollEvent to its parent window.
  320.  
  321.  Public Interface:
  322.  
  323.      ScrollBar - constructor taking a parent window, the position of
  324.          the scroll bar on the window, the length of the scroll bar,
  325.          and a flag indicating its orientation
  326.  
  327.      setSlider - sets slider to specified position. Takes a number
  328.          from 1 to 100, inclusive.
  329.  
  330.  (Redefined from Control)
  331.  
  332.      resize - redefined to restrict the width of vertical scroll bars,
  333.          and the height of horizontal ones, to one character.
  334.  
  335.      paint - redefined to paint scroll bar on screen.
  336.  
  337.      handleEvent - redefined to implement scroll bar behavior.
  338.  
  339. ********************************************************************/
  340.  
  341. enum ScrollerType { VERT = 1, HORIZ = 2 };
  342.  
  343. class ScrollBar : public Control
  344. {
  345. public:
  346.     ScrollBar( Win *parent, Point location, int len, int orient );
  347.     // use default copy constructor (memberwise initialization)
  348.     // use default operator=  (memberwise assignment)
  349.     void resize( int newWidth, int newLength );
  350.     int  setSlider( int pos );                // set position of slider
  351.     void paint( Rect region );
  352.     void handleEvent( const Event &action );
  353. private:
  354.     int sliderPos;
  355.     int length;
  356.     int orientation;
  357. };
  358.  
  359.  
  360. /********************************************************************
  361.  
  362.  PushButton
  363.  
  364.  Defines push button control elements. A push button inverts its
  365.  display momentarily when clicked, and sends a PushEvent to its
  366.  parent window.
  367.  
  368.  Public Interface:
  369.  
  370.      PushButton - constructor taking a parent window, the location of
  371.          the button on the window, and a label for the button
  372.  
  373.  (Redefined from Win)
  374.  
  375.      paint - redefined to paint button on screen.
  376.  
  377.      handleEvent - redefined to implement push button behavior.
  378.  
  379. ********************************************************************/
  380.  
  381. class PushButton : public Control
  382. {
  383. public:
  384.     PushButton( Win *parent, Point location, char *labl );
  385.     // use default copy constructor (memberwise initialization)
  386.     // use default operator=  (memberwise assignment)
  387.     void paint( Rect region );
  388.     void handleEvent( const Event &action );
  389. private:
  390.     char label[30];
  391.     int  pressed;
  392. };
  393.  
  394. #endif // _INTERACTOR_H_
  395.