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

  1. /***********************************************************************************
  2.     CExpanderLabel.h
  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.     CExpanderLabel manages the title of a CExpander object. The label, if selectable
  14.     can also issue a command when double-clicked.
  15.  
  16. ***********************************************************************************/
  17. #include "CExpanderLabel.h"
  18. #include "CExpanderLabelEnv.h"
  19.  
  20. extern short    gClicks;
  21.  
  22.  
  23. TCL_DEFINE_CLASS_D1( CExpanderLabel, CExpanderText );
  24.  
  25.  
  26. /*
  27.  * CExpanderLabel constructor
  28.  *
  29.  * Default constructor - should only be called when created by a file read.
  30.  */
  31.  
  32. CExpanderLabel :: CExpanderLabel() : CExpanderText()
  33. {
  34.     itsText = NULL;
  35.     itsCommand = cmdNull;
  36.  
  37.     SetWantsClicks( TRUE );
  38.  
  39.     TCL_END_CONSTRUCTOR
  40. }
  41.  
  42.  
  43. /*
  44.  * CExpanderLabel constructor
  45.  *
  46.  * Normal constructor - should always be called when created in code.
  47.  */
  48.  
  49. CExpanderLabel :: CExpanderLabel( CView *anEnclosure, CBureaucrat *aSupervisor,
  50.                                   short aWidth, short aHeight, short aHLoc, short aVLoc, 
  51.                                   SizingOption aHSizing, SizingOption aVSizing )
  52.             : CExpanderText( anEnclosure, aSupervisor, aWidth, aHeight, aHLoc, aVLoc,
  53.                               aHSizing, aVSizing )
  54. {
  55.     itsText = NULL;
  56.     itsCommand = cmdNull;
  57.  
  58.     SetWantsClicks( TRUE );
  59.  
  60.     MakeEnvironment();                // create drawing environment for the label.
  61.  
  62.     TCL_END_CONSTRUCTOR
  63. }
  64.  
  65.  
  66. /*
  67.  * CExpanderLabel destructor
  68.  *
  69.  * Just a place-holder for Inspector
  70.  */
  71.  
  72. CExpanderLabel :: ~CExpanderLabel()
  73. {
  74.     TCL_START_DESTRUCTOR
  75.  
  76.     if ( itsText ) {
  77.         ReleaseResource( itsText );
  78.         DisposeHandle( itsText );
  79.     }
  80. }
  81.  
  82.  
  83. /*
  84.  * MakeEnvironment method
  85.  *
  86.  * Protected method that creates a new environment for the label to draw in.
  87.  * Override this to install a different one.
  88.  */
  89.  
  90. void CExpanderLabel :: MakeEnvironment( void )
  91. {
  92.     try_ {
  93.         itsEnvironment = TCL_NEW( CExpanderLabelEnv, () );
  94.     }
  95.     catch_all_() {
  96.         throw_same_();
  97.     }
  98.     end_try_;
  99. }
  100.  
  101.  
  102. short CExpanderLabel :: CalcFrameWidth( void )
  103. {
  104.     short    value = 0;
  105.  
  106.     if ( itsText ) {
  107.         if ( *itsText == NULL ) {
  108.             LoadResource( itsText );
  109.         }
  110.         Prepare();
  111.         HLock( itsText );
  112.         value = StringWidth( (unsigned char *)*itsText );
  113.         HUnlock( itsText );
  114.     }
  115.  
  116.     return value;
  117. }
  118.  
  119.  
  120. void CExpanderLabel :: SetLabelTextRsrcID( short rsrcID )
  121. {
  122.     TCLForgetResource( itsText );
  123.  
  124.     itsText = GetResource( 'STR ', rsrcID );
  125.     FailNILRes( itsText );
  126.  
  127.     TextSizeChanged();
  128. }
  129.  
  130.  
  131. void CExpanderLabel :: Draw( Rect *area )
  132. {
  133.     if ( itsText == NULL || area->top >= lineHeight || area->bottom <= 0 )
  134.         return;
  135.  
  136.     MoveTo( 0, ascent );
  137.     DrawLabel( area );
  138.  
  139.     if ( ! printing ) {
  140.         if ( IsSelected() ) {
  141.             HiliteSelf( IsActive() );
  142.         }
  143.     }
  144. }
  145.  
  146.  
  147. void CExpanderLabel :: DrawLabel( Rect *area )
  148. {
  149.     if ( *itsText == NULL ) {
  150.         LoadResource( itsText );
  151.     }
  152.  
  153.     HLock( itsText );
  154.     DrawString( (unsigned char *)*itsText );
  155.     HUnlock( itsText );
  156. }
  157.  
  158.  
  159. void CExpanderLabel :: Activate( void )
  160. {
  161.     CExpanderText::Activate();
  162.  
  163.     if ( IsSelected() ) {
  164.         Refresh();
  165.     }
  166. }
  167.  
  168.  
  169. void CExpanderLabel :: Deactivate( void )
  170. {
  171.     CExpanderText::Deactivate();
  172.  
  173.     if ( IsSelected() ) {
  174.         Refresh();
  175.     }
  176. }
  177.  
  178.  
  179. /*
  180.  * DoClick method - OVERRIDE
  181.  *
  182.  * Handles clicks by the user. We are only interested in double-clicks. If
  183.  * there is one, we issue the command we currently have stored in itsCommand.
  184.  * Otherwise, we just let the ExpanderPane class handle the click.
  185.  */
  186.  
  187. void CExpanderLabel :: DoClick( Point hitPt, short modifierKeys, long when )
  188. {
  189.     if ( gClicks == 2 ) {            // double-click from user
  190.         DoCommand( itsCommand );    // tell application to do the command
  191.     }
  192.     else {
  193.         CExpanderText::DoClick( hitPt, modifierKeys, when );
  194.     }
  195. }
  196.  
  197.  
  198. /*
  199.  * PutTo method - OVERRIDE
  200.  *
  201.  * Writes to the stream all the info we need to save.
  202.  */
  203.  
  204. void CExpanderLabel :: PutTo( CStream &stream )
  205. {
  206.     short    id;
  207.     ResType    type;
  208.     Str255    name;
  209.  
  210.     CExpanderText::PutTo( stream );
  211.  
  212.     stream << itsCommand;
  213.  
  214.     if ( itsText ) {
  215.         GetResInfo( itsText, &id, &type, name );
  216.         FailResError();
  217.     }
  218.     else {
  219.         id = -1;
  220.     }
  221.  
  222.     stream << id;
  223. }
  224.  
  225.  
  226. /*
  227.  * GetFrom method - OVERRIDE
  228.  *
  229.  * Reads from the stream all of the info that we saved.
  230.  */
  231.  
  232. void CExpanderLabel :: GetFrom( CStream &stream )
  233. {
  234.     short    id;
  235.  
  236.     CExpanderText::GetFrom( stream );
  237.  
  238.     stream >> itsCommand;
  239.     stream >> id;
  240.  
  241.     if ( id != -1 ) {
  242.         SetLabelTextRsrcID( id );
  243.     }
  244.     else {
  245.         itsText = NULL;
  246.     }
  247. }
  248.