home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / Expander / Expander Classes / CExpanderList.cp < prev    next >
Encoding:
Text File  |  1994-11-30  |  5.6 KB  |  245 lines  |  [TEXT/KAHL]

  1. /***********************************************************************************
  2.     CExpanderList.cp
  3.  
  4.     Copyright © 1994 B-Ray Software. All rights reserved.
  5.     Developed using Symantec C++ 7.0.2 and Symantec's TCL library.
  6.     Portions of this code courtesy Symantec, Inc.
  7.  
  8.     This code may be freely distributed as long as this notice remains. This code
  9.     may not be used in any commercial software without the consent of B-Ray Software.
  10.  
  11.     ---
  12.  
  13.     CExpanderList expands on CExpander to provide selections. Children can be
  14.     selected, and key events alter the active selection.
  15.  
  16. ***********************************************************************************/
  17. #include "ExpanderMessages.h"
  18.  
  19. #include "CExpanderList.h"
  20.  
  21.  
  22. TCL_DEFINE_CLASS_D1( CExpanderList, CExpander );
  23.  
  24. /*
  25.  * CExpanderList constructor
  26.  *
  27.  * Default constructor - should only be called when created by a file read.
  28.  */
  29.  
  30. CExpanderList :: CExpanderList() : CExpander()
  31. {
  32.     selectedIndex = 0;
  33.  
  34.     TCL_END_CONSTRUCTOR
  35. }
  36.  
  37.  
  38. /*
  39.  * CExpanderList constructor
  40.  *
  41.  * Normal constructor - should always be called when created in code.
  42.  */
  43.  
  44. CExpanderList :: CExpanderList( CView *anEnclosure, CBureaucrat *aSupervisor,
  45.                                 short aWidth, short aHeight, short aHLoc, short aVLoc,
  46.                                 SizingOption aHSizing, SizingOption aVSizing )
  47.                     : CExpander( anEnclosure, aSupervisor, aWidth, aHeight, aHLoc, aVLoc,
  48.                                  aHSizing, aVSizing )
  49. {
  50.     selectedIndex = 0;
  51.  
  52.     TCL_END_CONSTRUCTOR
  53. }
  54.  
  55.  
  56. /*
  57.  * CExpanderList destructor
  58.  *
  59.  * Just a place-holder for Inspector
  60.  */
  61.  
  62. CExpanderList :: ~CExpanderList()
  63. {
  64.     TCL_START_DESTRUCTOR
  65. }
  66.  
  67.  
  68. /*
  69.  * SelectChild method
  70.  *
  71.  * Protected method used to select a given child. Starts the messaging
  72.  * process going.
  73.  */
  74.  
  75. void CExpanderList :: SelectChild( CFamily *aChild )
  76. {
  77.     if ( aChild )        // child is real - let it send the message
  78.         aChild->TellParent( kExpanderChildSelect, aChild );
  79.     else                // NULL - deselect everything
  80.         TellParent( kExpanderChildSelect, NULL );
  81. }
  82.  
  83.  
  84. /*
  85.  * SetSelectedIndex method
  86.  *
  87.  * Public access method to select the child at the given index.
  88.  */
  89.  
  90. void CExpanderList :: SetSelectedIndex( long index )
  91. {
  92.     CFamily    *aChild = GetChildAtIndex( index );
  93.  
  94.     if ( aChild ) {
  95.         aChild = aChild->ResolveToLeaf( FALSE );    // make sure we are at a node
  96.     }
  97.  
  98.     SelectChild( aChild );
  99. }
  100.  
  101.  
  102. /*
  103.  * DoKeyDown method - OVERRIDE
  104.  *
  105.  * Handles key down events. We are only interested in the cursor keys.
  106.  */
  107.  
  108. void CExpanderList :: DoKeyDown( char theChar, Byte keyCode, EventRecord *macEvent )
  109. {
  110.     CFamily    *aChild;
  111.     Boolean    childAtEnd;
  112.  
  113.     switch ( keyCode ) {            // first - look at the keyCode value
  114.         case KeyUpCursor:            // move up a child
  115.             aChild = PrevChild( selectedIndex );
  116.             childAtEnd = TRUE;
  117.             break;
  118.  
  119.         case KeyDownCursor:            // move down a child
  120.             aChild = NextChild( selectedIndex );
  121.             childAtEnd = FALSE;
  122.             break;
  123.  
  124.         default:
  125.             switch ( theChar ) {    // look at theChar value    
  126.                 case kUpCursor:        // move up a child
  127.                     aChild = PrevChild( selectedIndex );
  128.                     childAtEnd = TRUE;
  129.                     break;
  130.  
  131.                 case kDownCursor:    // move down a child
  132.                     aChild = NextChild( selectedIndex );
  133.                     childAtEnd = FALSE;
  134.                     break;
  135.  
  136.                 default:            // not handled
  137.                     CExpander::DoKeyDown( theChar, keyCode, macEvent );
  138.                     return;
  139.                     break;
  140.             }
  141.     }
  142.  
  143.     if ( aChild ) {            // had a valid child to make selected
  144.         aChild = aChild->ResolveToLeaf( childAtEnd );
  145.         SelectChild( aChild );
  146.     }
  147.     else if ( itsParent ) {    // let parent handle the key down event
  148.         CPane    *aPane = itsParent->ChildToPane();
  149.         aPane->DoKeyDown( theChar, keyCode, macEvent );
  150.     }
  151. }
  152.  
  153.  
  154. /*
  155.  * SetExpandedState method - OVERRIDE
  156.  *
  157.  * If we had a child selected, we must deselect it if we shrink ourselves.
  158.  */
  159.  
  160. void CExpanderList :: SetExpandedState( Boolean fState )
  161. {
  162.     CExpander::SetExpandedState( fState );
  163.     
  164.     /*
  165.      * See if we need to adjust the selection
  166.      */
  167.     if ( selectedIndex > 1 && fState == FALSE ) {
  168.         if ( itsLabel->CanSelect() == TRUE ) {
  169.             SetSelectedIndex( 1 );        // select our header
  170.         }
  171.         else {
  172.             TellParent( kExpanderChildSelect, NULL );    // don't select anything
  173.         }
  174.     }
  175. }
  176.  
  177.  
  178. /*
  179.  * ChildMessage method - OVERRIDE
  180.  *
  181.  * Handles kExpanderChildSelect message. When a child wishes to become selected, we
  182.  * remember its index as a negative value for later one when our parent broadcasts the
  183.  * same message.
  184.  */
  185.  
  186. void CExpanderList :: ChildMessage( CFamily *aChild, long message, void *param )
  187. {
  188.     if ( message == kExpanderChildSelect ) {
  189.         selectedIndex = param ? -FindChildIndex( aChild ) : 0;    // get negative index of child
  190.         CExpander::ChildMessage( aChild, message, param );        // pass on message to parent
  191.         selectedIndex *= -1;                                    // make index normal
  192.     }
  193.     else {
  194.         CExpander::ChildMessage( aChild, message, param );        // let superclass handle rest
  195.     }
  196. }
  197.  
  198.  
  199. /*
  200.  * ParentMessage method - OVERRIDE
  201.  *
  202.  * Handles kExpanderChildSelect message. When a parent notifies its children of a child
  203.  * selection, the children should check to see if they have a positive selectedIndex value.
  204.  * If so, then they should reset the value because they are not along the path of a selected
  205.  * leaf node.
  206.  */
  207.  
  208. void CExpanderList :: ParentMessage( long message, void *param )
  209. {
  210.     if ( message == kExpanderChildSelect ) {
  211.         if ( selectedIndex > 0 )                // not in path of selected child
  212.             selectedIndex = 0;                    // zap previous value
  213.     }
  214.  
  215.     CExpander::ParentMessage( message, param );    // pass message on to superclass
  216. }
  217.  
  218.  
  219. /*
  220.  * PutTo method - OVERRIDE
  221.  *
  222.  * Writes to the stream all the info we need to save.
  223.  */
  224.  
  225. void CExpanderList :: PutTo( CStream &stream )
  226. {
  227.     stream << selectedIndex;
  228.  
  229.     CExpander::PutTo( stream );
  230. }
  231.  
  232.  
  233. /*
  234.  * GetFrom method - OVERRIDE
  235.  *
  236.  * Reads from the stream all of the info that we saved.
  237.  */
  238.  
  239. void CExpanderList :: GetFrom( CStream &stream )
  240. {
  241.     stream >> selectedIndex;
  242.  
  243.     CExpander::GetFrom( stream );
  244. }
  245.