home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / HToggleRadioButton 1.2 / HToggleRadioButton.cp < prev    next >
Encoding:
Text File  |  1997-06-22  |  4.6 KB  |  180 lines  |  [TEXT/CWIE]

  1. /*******************************************************************************\
  2. |                                                                                |
  3. |    HToggleRadioButton.cp ©1996-1997 John C. Daub. All rights reserved.            |
  4. |    See the file "HToggleRadioButton README" for full details, instructions,    |
  5. |    changes, licensing agreement, etc.  Due to the important information        |
  6. |    included in that file, if you did not receive a copy of it, please contact    |
  7. |    the author for a copy.                                                        |
  8. |                                                                                |
  9. |    John C. Daub <mailto:hsoi@eden.com>                                            |
  10. |    <http://www.eden.com/~hsoi/>  <http://www.eden.com/~hsoi/prog.html>            |
  11. |                                                                                |
  12. \*******************************************************************************/
  13.  
  14. #include <LStream.h>
  15. #include <PP_Messages.h>
  16. #include <LToggleButton.h>
  17. #include "HToggleRadioButton.h"
  18.  
  19. extern const    Int32    delay_Animation    = 6;    // defined in LToggleButton.cp
  20.  
  21.  
  22. #if ( __PowerPlant__ < 0x01608000 ) // version 1.6/CW11
  23.  
  24. // ---------------------------------------------------------------------------
  25. //        • CreateFromStream                            [static, public]
  26. // ---------------------------------------------------------------------------
  27. //    Used in class registration. Only used if the RegisterClass_() macro is
  28. //    not available (less than PP 1.6)
  29.  
  30. HToggleRadioButton*
  31. HToggleRadioButton::CreateFromStream( LStream *inStream )
  32. {
  33.     return (new HToggleRadioButton(inStream) );
  34. }
  35.  
  36. #endif
  37.  
  38.  
  39. // ---------------------------------------------------------------------------
  40. //        • HToggleRadioButton                        [public]
  41. // ---------------------------------------------------------------------------
  42. //    Default constructor
  43.  
  44. HToggleRadioButton::HToggleRadioButton()
  45. {
  46.     // nothing
  47. }
  48.  
  49.  
  50. // ---------------------------------------------------------------------------
  51. //        • HToggleRadioButton                        [public]
  52. // ---------------------------------------------------------------------------
  53. //    Parameterized constructor
  54.  
  55. HToggleRadioButton::HToggleRadioButton(
  56.     const SPaneInfo    &inPaneInfo,
  57.     MessageT        inClickedMessage,
  58.     OSType            inGraphicsType,
  59.     ResIDT            inOnID,
  60.     ResIDT            inOnClickID,
  61.     ResIDT            inOffID,
  62.     ResIDT            inOffClickID,
  63.     ResIDT            inTransitionID)
  64.         : LToggleButton( inPaneInfo, inClickedMessage,
  65.                             inGraphicsType, inOnID,
  66.                             inOnClickID, inOffID,
  67.                             inOffClickID, inTransitionID )
  68. {
  69.     // nothing
  70. }
  71.  
  72.  
  73. // ---------------------------------------------------------------------------
  74. //        • HToggleRadioButton                        [public]
  75. // ---------------------------------------------------------------------------
  76. //    LStream constructor
  77.  
  78. HToggleRadioButton::HToggleRadioButton(
  79.     LStream *inStream )
  80.         : LToggleButton( inStream )
  81. {
  82.     // nothing
  83. }
  84.  
  85.  
  86. // ---------------------------------------------------------------------------
  87. //        • ~HToggleRadioButton                        [public, virtual]
  88. // ---------------------------------------------------------------------------
  89. //    Destructor
  90.  
  91. HToggleRadioButton::~HToggleRadioButton()
  92. {
  93.     // nothing
  94. }
  95.  
  96.  
  97. // ---------------------------------------------------------------------------
  98. //        • SetValue                                    [public, virtual]
  99. // ---------------------------------------------------------------------------
  100. //    Sets the value of the button (on or off)
  101.  
  102. void
  103. HToggleRadioButton::SetValue(
  104.     Int32 inValue )
  105. {                
  106.     // set the value of the button clicked on
  107.     
  108.     if ( inValue != mValue ) {
  109.         LControl::SetValue( inValue );
  110.         Refresh();
  111.     }
  112.  
  113.     // and if the button clicked on turned on, have the others
  114.     // turned off
  115.     
  116.     if ( inValue == Button_On ) {
  117.         BroadcastMessage( msg_ControlClicked, (void *)this );
  118.     }
  119.  
  120. }
  121.  
  122.  
  123. // ---------------------------------------------------------------------------
  124. //        • HotSpotResult                                [protected, virtual]
  125. // ---------------------------------------------------------------------------
  126. //    Action when the button is clicked in. Here is where we animate the
  127. //    the button and prevent the retoggling if clicked in again
  128.  
  129. void
  130. HToggleRadioButton::HotSpotResult(
  131.     Int16     inHotSpot )
  132. {    
  133.     // if it's already the same
  134.     
  135.     if ( inHotSpot == mValue ) {
  136.  
  137.         // determine which graphic to draw
  138.         
  139.         ResIDT currentClickState = mOnID;
  140.         if ( mValue == 0 ) {
  141.             currentClickState = mOffID;
  142.         }
  143.         
  144.         // draw it
  145.         
  146.         DrawGraphic( currentClickState );
  147.                 
  148.         // broadcast the value message
  149.         
  150.         SetValue( mValue );
  151.         
  152.         // go home
  153.         
  154.         return;
  155.     }
  156.  
  157.     // else it's not the same, so let's animate the button
  158.         
  159.     // Animate Button from current state to a transition state
  160.     // and then to the end state
  161.  
  162.     if (mTransitionID != resID_Undefined) {
  163.  
  164.         FocusDraw();
  165.         
  166.         DrawGraphic(mTransitionID);
  167.         Int32 ticks;
  168.         ::Delay(delay_Animation, &ticks);
  169.         
  170.         ResIDT    endClickState = mOnClickID;
  171.         if (mValue != 0) {
  172.             endClickState = mOffClickID;
  173.         }
  174.  
  175.         DrawGraphic(endClickState);
  176.     }
  177.     
  178.     SetValue(1 - GetValue());        // Toggle between on/off
  179. }
  180.