home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / NeuroSim 1.0.2 / .cp / CParamsDialog.cp < prev    next >
Encoding:
Text File  |  1997-03-25  |  7.0 KB  |  231 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CParamsDialog.cp            ©1996-97 Timo Eloranta
  3. // ===========================================================================
  4. //    This class handles the dialog where the user can view and modify the
  5. //    parameters of a new neural net. CParamsDialog is derived from LGADialogBox
  6. //  - a PowerPlant window class with default (OK) and Cancel buttons.
  7.  
  8. #include "CParamsDialog.h"
  9.  
  10. #include <PP_Messages.h>        // cmd_About
  11. #include <UDesktop.h>
  12. #include <UAttachments.h>
  13.  
  14. #include "NS_Utils.h"            // SignedIntField keyfilter
  15.  
  16. // ---------------------------------------------------------------------------
  17. //        • CParamsDialog
  18. //
  19. //          Called by:    CParamsDialog::CreateParamsDialogStream
  20. // ---------------------------------------------------------------------------
  21. //    Constructor. Call the base class with the LStream object reference.
  22.  
  23. CParamsDialog::CParamsDialog(
  24.     LStream *inStream )
  25.         : LGADialogBox( inStream )
  26. {
  27. }
  28.  
  29. // ---------------------------------------------------------------------------
  30. //        • CreateParamsDialogStream [static]
  31. //
  32. //          Called by:    CNeuroSimApp::ObeyCommand (indirect call)
  33. // ---------------------------------------------------------------------------
  34. //    Return a new ParamsDialog object initialized using data from a Stream.
  35.  
  36. CParamsDialog *
  37. CParamsDialog::CreateParamsDialogStream(
  38.     LStream *inStream)
  39. {
  40.     return ( new CParamsDialog( inStream ) );
  41. }
  42.  
  43. // ---------------------------------------------------------------------------
  44. //        • InitDialog
  45. //
  46. //          Called by:    CNeuroSimApp::ObeyCommand
  47. // ---------------------------------------------------------------------------
  48. //    Initialize all the controls inside the dialog.
  49.  
  50. void
  51. CParamsDialog::InitDialog()
  52. {
  53.     mSizeCapt        = (LCaption *) this -> FindPaneByID( capt_Size );
  54.  
  55.     mMinEdit        = (LGAEditField *) this -> FindPaneByID( edit_Qty_Min );
  56.     mMaxEdit        = (LGAEditField *) this -> FindPaneByID( edit_Qty_Max );
  57.     mAvg_X_Edit        = (LGAEditField *) this -> FindPaneByID( edit_Avg_x );
  58.     mDev_X_Edit        = (LGAEditField *) this -> FindPaneByID( edit_Dev_x );
  59.     mAvg_Y_Edit        = (LGAEditField *) this -> FindPaneByID( edit_Avg_y );
  60.     mDev_Y_Edit        = (LGAEditField *) this -> FindPaneByID( edit_Dev_y );
  61.  
  62.     mFactoryButton    = (LGAPushButton *) this -> FindPaneByID( but_Factory );
  63.  
  64.     if ( mFactoryButton)
  65.         mFactoryButton -> AddListener( this );
  66.  
  67.     mSizeSlider    = (CAGASlider *) this -> FindPaneByID( slid_Size );
  68.  
  69.     if ( mSizeSlider )
  70.         mSizeSlider -> AddListener( this );
  71.  
  72.     mMinEdit -> Enable();
  73.     mMinEdit -> Activate();
  74.     mMinEdit -> SelectAll();
  75.      
  76.      // The average values can be negative and since PowerPlant
  77.      // doesn't have a built-in keyfilter for allowing only
  78.      // numbers and the '-' character, we set our custom keyfilter
  79.      // here "manually". The filter is defined in NS_Utils.h.
  80.      
  81.     mAvg_X_Edit -> SetKeyFilter( SignedIntField );
  82.     mAvg_Y_Edit -> SetKeyFilter( SignedIntField );
  83. }
  84.  
  85. // ---------------------------------------------------------------------------
  86. //        • SetValues
  87. //
  88. //          Called by:    CNeuroSimApp::ObeyCommand
  89. //                        CParamsDialog::SetDefaultValues
  90. // ---------------------------------------------------------------------------
  91. //    Set the controls of the dialog to the values given in "inParams".
  92.  
  93. void
  94. CParamsDialog::SetValues( SGenParams &inParams )
  95. {
  96.     SetSizeValue( inParams.size );
  97.     mSizeSlider -> SetValue( inParams.size );
  98.  
  99.     mMinEdit    -> SetValue( inParams.qtyMin );
  100.     mMaxEdit    -> SetValue( inParams.qtyMax );
  101.     mAvg_X_Edit -> SetValue( inParams.xLengthAvg );
  102.     mDev_X_Edit -> SetValue( inParams.xLengthDev );
  103.     mAvg_Y_Edit -> SetValue( inParams.yLengthAvg );
  104.     mDev_Y_Edit -> SetValue( inParams.yLengthDev );
  105. }
  106.  
  107. // ---------------------------------------------------------------------------
  108. //        • SetSizeValue
  109. //
  110. //          Called by:    CParamsDialog::SetValues
  111. //                        CParamsDialog::ListenToMessage
  112. // ---------------------------------------------------------------------------
  113. //    Transform the given (numeric) net size N to a string of type "N x N" 
  114. //    and set this string to the caption field beside the slider.
  115.  
  116. void
  117. CParamsDialog::SetSizeValue( Int16 inValue )
  118. {
  119.     LStr255        theNbrString( (Int32) inValue );
  120.     LStr255        theString( theNbrString );
  121.  
  122.     theString.Append( "\p x " ); 
  123.     theString.Append( theNbrString ); 
  124.  
  125.     mSizeCapt    -> SetDescriptor( theString );
  126. }
  127.  
  128. // ---------------------------------------------------------------------------
  129. //        • GetValues
  130. //
  131. //          Called by:    CNeuroSimApp::ObeyCommand
  132. // ---------------------------------------------------------------------------
  133. //    Read the new values of the controls. Do also some validity checking.
  134.  
  135. void
  136. CParamsDialog::GetValues( SGenParams &outParams )
  137. {
  138.     outParams.size            = mSizeSlider -> GetValue();
  139.  
  140.     outParams.xLengthAvg    = mAvg_X_Edit -> GetValue();
  141.     outParams.xLengthDev    = mDev_X_Edit -> GetValue();
  142.     outParams.yLengthAvg    = mAvg_Y_Edit -> GetValue();
  143.     outParams.yLengthDev    = mDev_Y_Edit -> GetValue();
  144.  
  145.     outParams.qtyMin        = mMinEdit -> GetValue();
  146.     outParams.qtyMax        = mMaxEdit -> GetValue();
  147.     
  148.     // Minimum can not be bigger than maximum...
  149.     // If it is, set the maximum to be equal to minimum
  150.     // and show an alert box to the silly user!
  151.     
  152.     if ( outParams.qtyMax < outParams.qtyMin ) {
  153.         outParams.qtyMax = outParams.qtyMin;
  154.         LStr255    theParam0( (Int32) outParams.qtyMin );
  155.         
  156.         UDesktop::Deactivate();
  157.         ::ParamText( theParam0, "\p", "\p", "\p");
  158.         ::StopAlert( ALRT_MinMax, nil );
  159.         UDesktop::Activate();
  160.     }
  161. }
  162.  
  163. // ---------------------------------------------------------------------------
  164. //        • ListenToMessage
  165. //
  166. //          Called by:    LBroadcaster::BroadcastMessage
  167. // ---------------------------------------------------------------------------
  168. //    Respond to messages from Broadcasters
  169.  
  170. void
  171. CParamsDialog::ListenToMessage(
  172.     MessageT    inMessage, 
  173.     void        *ioParam )
  174. {
  175.     switch ( inMessage ) {
  176.  
  177.         case slid_Size:
  178.             if ( mSizeCapt ) {
  179.                 SetSizeValue( *(Int32 *) ioParam );
  180.                 mSizeCapt -> Draw( NULL );
  181.             }
  182.             break;
  183.             
  184.         case msg_FactorySettings:
  185.             SetDefaultValues();
  186.             break;
  187.             
  188.         default:    
  189.             LGADialogBox::ListenToMessage( inMessage, ioParam );
  190.             break;
  191.     }
  192. }
  193.  
  194. // ---------------------------------------------------------------------------
  195. //        • SetDefaultValues
  196. //
  197. //          Called by:    CParamsDialog::ListenToMessage
  198. // ---------------------------------------------------------------------------
  199. //    This function is used when the user pushes the "Factory Settings" button.
  200.  
  201. void
  202. CParamsDialog::SetDefaultValues()
  203. {
  204.     SGenParams theDefaults = {    DEFAULT_LENGTH_X_AVG,    DEFAULT_LENGTH_Y_AVG,
  205.                                 DEFAULT_LENGTH_X_DEV,    DEFAULT_LENGTH_Y_DEV,
  206.                                 DEFAULT_NET_SIZE,
  207.                                 DEFAULT_QTY_MIN,        DEFAULT_QTY_MAX };
  208.     SetValues( theDefaults );
  209. }
  210.         
  211. // ---------------------------------------------------------------------------
  212. //        • FindCommandStatus
  213. //
  214. //          Called by:    LCommander::ProcessCommandStatus
  215. // ---------------------------------------------------------------------------
  216. //    Disable all menu commands except cmd_About
  217.  
  218. void
  219. CParamsDialog::FindCommandStatus(
  220.     CommandT    inCommand,
  221.     Boolean        &outEnabled,
  222.     Boolean&    /* outUsesMark */,
  223.     Char16&        /* outMark */,
  224.     Str255        /* outName */)
  225. {
  226.     outEnabled = false;
  227.     if (inCommand == cmd_About) {
  228.         outEnabled = true;
  229.     }
  230. }
  231.