home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / TimGA 1.2.1 / .cp / CSelectionDialog.cp < prev    next >
Encoding:
Text File  |  1997-07-16  |  6.6 KB  |  242 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CSelectionDialog.cp        ©1995-97 Timo Eloranta        All rights reserved.
  3. // ===========================================================================
  4.  
  5. #include "CSelectionDialog.h"
  6. #include "GraphGA_PaneIDs.h"
  7. #include "CPopulation.h"        // AutomSelectionStep
  8.  
  9. #include <PP_Messages.h>
  10.  
  11. #include <LCaption.h>
  12. #include <LGAPushButton.h>
  13. #include <LGACheckbox.h>
  14. #include <LGARadioButton.h>
  15.  
  16. #include "CAGASlider.h"
  17.  
  18. // ---------------------------------------------------------------------------
  19. //        • CSelectionDialog
  20. //
  21. //          Called by:    URegistrar::CreateObject
  22. // ---------------------------------------------------------------------------
  23.  
  24. CSelectionDialog::CSelectionDialog( LStream *inStream )
  25.     : LGADialogBox( inStream )
  26. {
  27. }
  28.  
  29. // ---------------------------------------------------------------------------
  30. //        • InitDialog
  31. //
  32. //          Called by:    CGraphGAApp::ObeyCommand
  33. // ---------------------------------------------------------------------------
  34.  
  35. void
  36. CSelectionDialog::InitDialog()
  37. {
  38.     mStepCapt        = (LCaption *) this -> FindPaneByID( capt_Step );
  39.     mMinCapt        = (LCaption *) this -> FindPaneByID( capt_Minimum );
  40.     
  41.     mAutomatic        = (LGARadioButton *) this -> FindPaneByID( rbut_Automatic );
  42.     mManual            = (LGARadioButton *) this -> FindPaneByID( rbut_Manual );
  43.     
  44.     mFactoryButton    = (LGAPushButton *) this -> FindPaneByID( but_FactorySettings );
  45.         
  46.     mElitismBox        = (LGACheckbox *) this -> FindPaneByID( cbox_Elitism );
  47.     
  48.     mStepSlider        = (CAGASlider *) this -> FindPaneByID( slid_StepSlider );
  49.     mMinSlider        = (CAGASlider *) this -> FindPaneByID( slid_MinSlider );
  50.  
  51.     if ( mAutomatic )         mAutomatic ->        AddListener( this );
  52.     if ( mManual )             mManual ->            AddListener( this );
  53.     if ( mElitismBox )        mElitismBox ->        AddListener( this );
  54.     if ( mFactoryButton)    mFactoryButton ->    AddListener( this );
  55.     if ( mStepSlider )        mStepSlider ->        AddListener( this );
  56.     if ( mMinSlider )        mMinSlider ->        AddListener( this );
  57. }
  58.  
  59. // ---------------------------------------------------------------------------
  60. //        • SetValues
  61. //
  62. //          Called by:    CGraphGAApp::ObeyCommand
  63. //                        CSelectionDialog::ListenToMessage
  64. // ---------------------------------------------------------------------------
  65.  
  66. void
  67. CSelectionDialog::SetValues( SSelection &inSelection, Int16 inPopSize )
  68. {
  69.     mPopSize = inPopSize;
  70.     Int16    theAutoValue;
  71.  
  72.     if ( mMinCapt )
  73.         mMinCapt -> SetValue( inSelection.min );
  74.     if ( mMinSlider )
  75.         mMinSlider -> SetValue( inSelection.min );
  76.  
  77.     if ( mStepSlider )
  78.         mStepSlider -> SetValue( inSelection.step );
  79.         
  80.     if ( inSelection.autom )     
  81.             mAutomatic    -> SetValue( Button_On );
  82.     else    mManual        -> SetValue( Button_On );
  83.         
  84.     if ( mStepCapt ) {
  85.         if ( inSelection.autom ) {
  86.             theAutoValue = AutomSelectionStep( inSelection.min, inPopSize );
  87.             mStepCapt -> SetValue( theAutoValue );
  88.             mStepCapt -> Refresh();
  89.         } else
  90.             mStepCapt -> SetValue( inSelection.step );
  91.     }
  92.         
  93.     if ( inSelection.elitism )
  94.             mElitismBox -> SetValue( Button_On );
  95.     else    mElitismBox -> SetValue( Button_Off );
  96. }
  97.  
  98. // ---------------------------------------------------------------------------
  99. //        • GetValues
  100. //
  101. //          Called by:    CGraphGAApp::SetSelFromDialog
  102. //                        CSelectionDialog::AdjustFactoryButton
  103. // ---------------------------------------------------------------------------
  104.  
  105. void
  106. CSelectionDialog::GetValues( SSelection &outSelection )
  107. {
  108.     if ( mStepCapt )
  109.         outSelection.step = mStepCapt -> GetValue();
  110.     if ( mMinCapt )
  111.         outSelection.min = mMinCapt -> GetValue();
  112.     if ( mAutomatic )
  113.         outSelection.autom = mAutomatic -> GetValue();
  114.     if ( mElitismBox )
  115.         outSelection.elitism = mElitismBox -> GetValue();
  116. }
  117.  
  118. // ---------------------------------------------------------------------------
  119. //        • ListenToMessage
  120. //
  121. //          Called by:    LBroadcaster::BroadcastMessage
  122. // ---------------------------------------------------------------------------
  123.  
  124. void
  125. CSelectionDialog::ListenToMessage(
  126.     MessageT    inMessage, 
  127.     void        *ioParam )
  128. {
  129.     Int16    theAutoValue;
  130.     
  131.     switch ( inMessage ) {
  132.  
  133.         case msg_Automatic:
  134.             if ( (mAutomatic -> GetValue() == Button_On) && mStepCapt ) {
  135.                 theAutoValue =
  136.                     AutomSelectionStep( mMinCapt -> GetValue(), mPopSize ); 
  137.                 mStepCapt -> SetValue( theAutoValue );
  138.                 mStepCapt -> Refresh();
  139.                 
  140.                 mStepSlider -> Disable();    // 10.04.97                
  141.                 AdjustFactoryButton();
  142.             }
  143.             break;
  144.         
  145.         case msg_ManualStep:
  146.             if ( (mManual -> GetValue() == Button_On) && mStepCapt ) {
  147.                 mStepCapt -> SetValue( mStepSlider -> GetValue() );
  148.                 mStepCapt -> Refresh();
  149.                 
  150.                 mStepSlider -> Enable();    // 10.04.97
  151.                 AdjustFactoryButton();
  152.             }
  153.             break;
  154.         
  155.         case msg_Elitism:
  156.             AdjustFactoryButton();
  157.             break;
  158.         
  159.         case msg_StepSlider:
  160.             if ( mStepCapt ) {
  161.                 if ( mManual -> GetValue() == Button_Off ) {
  162.                     mManual -> SetValue( Button_On );
  163.                 }
  164.                 mStepCapt -> SetValue( *(Int32 *) ioParam );
  165.                 mStepCapt -> Draw(nil);
  166.                 AdjustFactoryButton();
  167.             }
  168.             break;
  169.  
  170.         case msg_MinSlider:
  171.             if ( mMinCapt ) {
  172.                 mMinCapt -> SetValue( *(Int32 *) ioParam );
  173.                 mMinCapt -> Draw(nil);
  174.                 if ( (mAutomatic -> GetValue() ) && mStepCapt ) {
  175.                     theAutoValue = 
  176.                     AutomSelectionStep( (*(Int32 *) ioParam), mPopSize );
  177.                     if ( theAutoValue != mStepCapt -> GetValue() ) {
  178.                         mStepCapt -> SetValue( theAutoValue );
  179.                         mStepCapt -> Draw(nil);
  180.                     }
  181.                 }
  182.                 AdjustFactoryButton();
  183.             }
  184.             break;
  185.             
  186.         case msg_FactorySettings:
  187.             SSelection theDefaults = {    DEFAULT_SELECTION_STEP,
  188.                                         DEFAULT_SELECTION_MIN,
  189.                                         DEFAULT_SELECTION_AUTOM,
  190.                                         DEFAULT_ELITISM };
  191.             SetValues( theDefaults, mPopSize );
  192.             break;
  193.             
  194.         default:    
  195.             LGADialogBox::ListenToMessage( inMessage, ioParam );
  196.             break;
  197.     }
  198. }
  199.  
  200. // ---------------------------------------------------------------------------
  201. //        • AdjustFactoryButton    (PRIVATE)
  202. //
  203. //          Called by:    CSelectionDialog::ListenToMessage
  204. // ---------------------------------------------------------------------------
  205.  
  206. void
  207. CSelectionDialog::AdjustFactoryButton( )
  208. {
  209.     SSelection theS;
  210.  
  211.     GetValues( theS );
  212.     
  213.     if ( theS.min        == DEFAULT_SELECTION_MIN    &&
  214.          theS.autom        == DEFAULT_SELECTION_AUTOM    &&
  215.          theS.elitism    == DEFAULT_ELITISM )
  216.             mFactoryButton -> Disable();
  217.     else
  218.         if ( ! mFactoryButton -> IsEnabled() ) 
  219.              mFactoryButton -> Enable();
  220. }
  221.  
  222. // ---------------------------------------------------------------------------
  223. //        • FindCommandStatus
  224. //
  225. //          Called by:    LCommander::ProcessCommandStatus
  226. // ---------------------------------------------------------------------------
  227. //    Disable all menu items except for the one which opens the About box.
  228.  
  229. void
  230. CSelectionDialog::FindCommandStatus(
  231.     CommandT    inCommand,
  232.     Boolean        &outEnabled,
  233.     Boolean&    /* outUsesMark */,
  234.     Char16&        /* outMark */,
  235.     Str255        /* outName */)
  236. {
  237.     outEnabled = false;
  238.     if (inCommand == cmd_About) {
  239.         outEnabled = true;
  240.     }
  241. }
  242.