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

  1. /********************************************************************
  2.  
  3.  FILE: WINMGR.CPP
  4.  
  5. ********************************************************************/
  6.  
  7. #include "winmgr.h"
  8.  
  9. /********************************************************************
  10.  
  11.  WinMgr::handleEvent
  12.  
  13.  This function sends keyboard events to the handleKey function and
  14.  mouse events to the handleMouse function.
  15.  
  16. ********************************************************************/
  17.  
  18. void WinMgr::handleEvent( const Event &action )
  19. {
  20.     switch ( action.getType() )
  21.     {
  22.     case KBD_EVENT:
  23.         handleKey( (KbdEvent &) action );
  24.         break;
  25.  
  26.     case MOUSE_EVENT:
  27.         handleMouse( (MouseEvent &) action );
  28.         break;
  29.  
  30.     default:
  31.         break;
  32.     }
  33. }
  34.  
  35. /********************************************************************
  36.  
  37.  WinMgr::handleMouse
  38.  
  39.  This function directs mouse events to the various windows. First
  40.  it sees whether any windows exist, and then it iterates through the
  41.  list of windows and looks for the first window that the given
  42.  MouseEvent falls on. If that window is the active one, the location
  43.  of the MouseEvent is translated to the window's coordinate system
  44.  and passed to the window's handleEvent function. If not, the
  45.  function sees if the event falls within the borders of another
  46.  window, and if so, it activates that window. The previously active
  47.  window is deactivated, and the new active window is moved to the
  48.  end of the list.
  49.  
  50. ********************************************************************/
  51.  
  52. void WinMgr::handleMouse( const MouseEvent &action )
  53. {
  54.     Win *targetWin;
  55.  
  56.     if( winlist.isEmpty() )
  57.         return;
  58.  
  59.     winlist.initIterator();
  60.     targetWin = (Win *)winlist.getPrev();
  61.     if( targetWin->withinBounds( action.getPosition() ) )
  62.     {
  63.         MouseEvent localAction( action.getPosition() - targetWin->origin(),
  64.                                 action.getButton() );
  65.  
  66.         targetWin->handleEvent( localAction );
  67.     }
  68.     else
  69.      {
  70.         while( (targetWin = (Win *)winlist.getPrev()) != 0 )
  71.         {
  72.             if( targetWin->withinBounds( action.getPosition() ) )
  73.             {
  74.                 Win *oldActive = (Win *)winlist.getTail();
  75.  
  76.                 oldActive->deactivate();
  77.                 oldActive->paint( Rect( 0, 0, oldActive->width(), oldActive->height() ) );
  78.  
  79.                 winlist.remove();   // remove current
  80.                 winlist.addTail( targetWin );
  81.  
  82.                 targetWin->activate();
  83.                 targetWin->paint( Rect( 0, 0, targetWin->width(), targetWin->height() ) );
  84.                 break;
  85.             }
  86.         }
  87.     }
  88.  
  89. }
  90.  
  91. /********************************************************************
  92.  
  93.  WinMgr::handleKey
  94.  
  95.  This function screens keyboard events, responding to some and
  96.  passing the rest on to the active window to handle. If the key hit
  97.  was ESC, the program is exited (this is the only event that can be
  98.  handled without any windows existing). If the key wasn't ESC, the
  99.  function checks whether any windows exist, and then examines the
  100.  keyboard event. The keys handled are TAB (cycle through windows),
  101.  CTRL-T, CTRL-F, CTRL-G, CTRL-V (resizing: make window shorter,
  102.  narrower, wider, or longer, respectively), CTRL-U, CTRL-H, CTRL-J,
  103.  CTRL-N (move window up, left, right, or down, respectively), and
  104.  DELETE (remove window). Any other keyboard events are passed to
  105.  the individual windows.
  106.  
  107. ********************************************************************/
  108.  
  109. void WinMgr::handleKey( const KbdEvent &key )
  110. {
  111.     Rect invalid;
  112.     int w, h;
  113.     Win *activeWin;
  114.  
  115.     if( key.val() == ESC )  // quit
  116.         exit(1);
  117.  
  118.     if( winlist.isEmpty() )
  119.         return;
  120.  
  121.     switch ( key.val() )
  122.     {
  123.     case TAB:         // next window
  124.         Win *oldActive,
  125.             *newActive;
  126.  
  127.         oldActive = (Win *)winlist.getTail();
  128.         oldActive->deactivate();
  129.         oldActive->paint( Rect( 0, 0, oldActive->width(), oldActive->height() ) );
  130.  
  131.         newActive = (Win *)winlist.removeHead();
  132.         winlist.addTail( newActive );
  133.         newActive->activate();
  134.         newActive->paint( Rect( 0, 0, newActive->width(), newActive->height() ) );
  135.         break;
  136.  
  137.     case ExtChar(20,0): // ctrl-T: resize
  138.         activeWin = (Win *)winlist.getTail();
  139.         w = activeWin->width();
  140.         h = activeWin->height();
  141.  
  142.         activeWin->resize(w, h - 1);
  143.  
  144.         invalid = Rect( activeWin->origin() + Point( 0, h - 1 ),
  145.                         activeWin->origin() + Point( w - 1, h - 1 ) );
  146.  
  147.         activeWin->paint( Rect( 0, 0, w, h - 1 ) );
  148.         repaint( invalid );
  149.         break;
  150.  
  151.     case ExtChar(6,0): // ctrl-F: resize
  152.         activeWin = (Win *)winlist.getTail();
  153.         w = activeWin->width();
  154.         h = activeWin->height();
  155.  
  156.         activeWin->resize(w - 1, h );
  157.  
  158.         invalid = Rect( activeWin->origin() + Point( w - 1, 0 ),
  159.                         activeWin->origin() + Point( w - 1, h - 1 ) );
  160.         activeWin->paint( Rect( 0, 0, w - 1, h ) );
  161.         repaint( invalid );
  162.     break;
  163.  
  164.     case ExtChar(7,0): // ctrl-G: resize
  165.         activeWin = (Win *)winlist.getTail();
  166.         w = activeWin->width();
  167.         h = activeWin->height();
  168.  
  169.         activeWin->resize(w + 1, h );
  170.         activeWin->paint( Rect( 0, 0, w + 1, h ) );
  171.         break;
  172.  
  173.     case ExtChar(22,0): // ctrl-V: resize
  174.         activeWin = (Win *)winlist.getTail();
  175.         w = activeWin->width();
  176.         h = activeWin->height();
  177.  
  178.         activeWin->resize( w, h + 1 );
  179.         activeWin->paint( Rect( 0, 0, w, h + 1 ) );
  180.         break;
  181.  
  182.     case ExtChar(21,0): // ctrl-U: move
  183.         activeWin = (Win *)winlist.getTail();
  184.         w = activeWin->width();
  185.         h = activeWin->height();
  186.  
  187.         activeWin->move( activeWin->origin() + Point( 0, -1 ) );
  188.  
  189.         invalid = Rect( activeWin->origin() + Point( 0, h ),
  190.                         activeWin->origin() + Point( w - 1, h ) );
  191.  
  192.         activeWin->paint( Rect( 0, 0, w, h ) );
  193.         repaint( invalid );
  194.         break;
  195.  
  196.     case ExtChar(8,0): // ctrl-H: move
  197.         activeWin = (Win *)winlist.getTail();
  198.         w = activeWin->width();
  199.         h = activeWin->height();
  200.  
  201.         activeWin->move( activeWin->origin() + Point( -1, 0 ) );
  202.  
  203.         invalid = Rect( activeWin->origin() + Point( w, 0 ),
  204.                         activeWin->origin() + Point( w, h - 1 ) );
  205.         activeWin->paint( Rect( 0, 0, w, h ) );
  206.         repaint( invalid );
  207.     break;
  208.  
  209.     case ExtChar(10,0): // ctrl-J: move
  210.         activeWin = (Win *)winlist.getTail();
  211.         w = activeWin->width();
  212.         h = activeWin->height();
  213.  
  214.         activeWin->move( activeWin->origin() + Point( 1, 0 ) );
  215.  
  216.         invalid = Rect( activeWin->origin() + Point( -1, 0 ),
  217.                         activeWin->origin() + Point( -1, h - 1 ) );
  218.  
  219.         activeWin->paint( Rect( 0, 0, w, h ) );
  220.         repaint( invalid );
  221.         break;
  222.  
  223.     case ExtChar(14,0): // ctrl-N: move
  224.         activeWin = (Win *)winlist.getTail();
  225.         w = activeWin->width();
  226.         h = activeWin->height();
  227.  
  228.         activeWin->move( activeWin->origin() + Point( 0, 1 ) );
  229.  
  230.         invalid = Rect( activeWin->origin() + Point( 0, -1 ),
  231.                         activeWin->origin() + Point( w - 1, -1 ) );
  232.  
  233.         activeWin->paint( Rect( 0, 0, w, h ) );
  234.         repaint( invalid );
  235.         break;
  236.  
  237.     case ExtChar(0,83): // DELETE
  238.         deleteWindow();
  239.         break;
  240.  
  241.     default:
  242.         activeWin = (Win *)winlist.getTail();
  243.         activeWin->handleEvent(key);
  244.         break;
  245.     };
  246.  
  247. }
  248.  
  249. /********************************************************************
  250.  
  251.  WinMgr::addWindow
  252.  
  253.  This function adds a window to the list. The previously active
  254.  window is deactivated, and the new window is added to the end of
  255.  the list and activated.
  256.  
  257. ********************************************************************/
  258.  
  259. void WinMgr::addWindow( Win *newWindow )
  260. {
  261.     Win *oldActive;
  262.  
  263.     oldActive = (Win *)winlist.getTail();
  264.     if( oldActive != 0 )
  265.         oldActive->deactivate();
  266.     winlist.addTail( newWindow );
  267.     newWindow->activate();
  268. }
  269.  
  270. /********************************************************************
  271.  
  272.  WinMgr::deleteWindow
  273.  
  274.  This function removes the active window from the list. The window
  275.  is removed and the area it occupied is repainted. The new active
  276.  window is activated.
  277.  
  278. ********************************************************************/
  279.  
  280. void WinMgr::deleteWindow()
  281. {
  282.     Win *deleted,
  283.         *newActive;
  284.     Rect invalid;
  285.     int w, h;
  286.  
  287.     if( !winlist.isEmpty() )
  288.     {
  289.         deleted = (Win *)winlist.removeTail();
  290.         w = deleted->width();
  291.         h = deleted->height();
  292.         invalid = Rect( deleted->origin(),
  293.                         deleted->origin() + Point( w, h ) );
  294.  
  295.         if( (newActive = (Win *)winlist.getTail()) != 0 )
  296.         {
  297.             newActive->activate();
  298.             newActive->paint( Rect( 0, 0, newActive->width(), newActive->height() ) );
  299.         }
  300.         repaint( invalid );
  301.     }
  302. }
  303.  
  304. /********************************************************************
  305.  
  306.  WinMgr::repaint
  307.  
  308.  This function repaints the specified region of the screen. The
  309.  background color is written to the area, and then each window
  310.  in the list paints over the area.
  311.  
  312. ********************************************************************/
  313.  
  314. void WinMgr::repaint( Rect region )
  315. {
  316.     Win *currWin;
  317.     int x, y;
  318.  
  319.     theScreen.cursorOff();
  320.     for( x = region.left; x <= region.right; x++ )
  321.         for( y = region.top; y <= region.bottom; y++ )
  322.             theScreen.showChar( Point( x, y ), ' ', WHITE_FORE | BLACK_BACK );
  323.     theScreen.cursorOn();
  324.  
  325.     winlist.initIterator();
  326.     while( (currWin = (Win *)winlist.getNext()) != 0 )
  327.         currWin->paint( region - currWin->origin() );
  328.  
  329. }
  330.  
  331.