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

  1. // ===========================================================================
  2. //    CGraphWindow.cp            ©1995-97 Timo Eloranta         All rights reserved.
  3. // ===========================================================================
  4.  
  5. #include "CGraphWindow.h"
  6. #include "CGraphGAApp.h"
  7. #include "GraphGA_PaneIDs.h"
  8. #include <LString.h>
  9.  
  10.  
  11. // ---------------------------------------------------------------------------
  12. //        • CGraphWindow
  13. //
  14. //          Called by:    URegistrar::CreateObject
  15. // ---------------------------------------------------------------------------
  16.  
  17. CGraphWindow::CGraphWindow(
  18.     LStream    *inStream)
  19.         : LWindow(inStream)
  20. {
  21. }
  22.  
  23. // ---------------------------------------------------------------------------
  24. //        • FinishCreateSelf 
  25. //
  26. //          Called by:    LView::FinishCreate
  27. // ---------------------------------------------------------------------------
  28. //    Finish Creating a Pane
  29. //
  30. //    This function gets called after creating a Pane from a data stream.
  31. //    Override to perform finishing touches that depend on the entire
  32. //    Pane hierarchy being constructed.
  33.  
  34. void
  35. CGraphWindow::FinishCreateSelf()
  36. {
  37.     mApp = (CGraphGAApp *) mSuperCommander;
  38.  
  39.     mGraphPane = (CGraphPane *) FindPaneByID( pane_GraphDrawing );
  40.     
  41.     mGraphPane -> SetApp( mApp );
  42.  
  43.         //    Make the graph pane latent so that
  44.         //    when the window becomes active
  45.         //    the graph pane is the target.
  46.  
  47.     SetLatentSub( mGraphPane );
  48.     
  49.         //     Set light grey as backcolor.
  50.         //  Actually only backgrounds of the caption fields will get
  51.         //  the light grey color, but hey, I like it that way... :)
  52.         
  53.     const RGBColor greyRGB = { 0xEEEE, 0xEEEE, 0xEEEE };
  54.     SetForeAndBackColors( nil, &greyRGB );
  55.     
  56.     mPopSizeCapt    = (LCaption*) FindPaneByID( capt_PopSize );
  57.     mCurGenerCapt    = (LCaption*) FindPaneByID( capt_CurGeneration );
  58.     mRunningCapt    = (LCaption*) FindPaneByID( capt_RunningTime );
  59.     mLeastCapt        = (LCaption*) FindPaneByID( capt_LeastCrossings );
  60.     mAverageCapt    = (LCaption*) FindPaneByID( capt_AvgCrossings );
  61.     mIterCapt        = (LCaption*) FindPaneByID( capt_IterState );
  62.     mFitnessCapt    = (LCaption*) FindPaneByID( capt_BestEverFitness );
  63.     mBestGenCapt    = (LCaption*) FindPaneByID( capt_BestEverGeneration );
  64.     mNoGenCapt        = (LCaption*) FindPaneByID( capt_NoChangeGenerations );
  65.     mNoTimeCapt        = (LCaption*) FindPaneByID( capt_NoChangeTime );
  66. }
  67.  
  68. // ---------------------------------------------------------------------------
  69. //        • ResetSuperCommanderToApp
  70. //
  71. //          Called by:    CGraphGAApp::RemoveDoc
  72. // ---------------------------------------------------------------------------
  73.  
  74. void
  75. CGraphWindow::ResetSuperCommanderToApp( )
  76. {
  77.     SetSuperCommander( mApp );
  78. }
  79.  
  80. // ---------------------------------------------------------------------------
  81. //        • UpdateGraphInfo
  82. //
  83. //          Called by:    CGraphGAApp::InitForNoGraph
  84. //                        CGraphGAApp::ObeyCommand
  85. //                        CGraphGADoc::PrepareToIterate
  86. // ---------------------------------------------------------------------------
  87.  
  88. void
  89. CGraphWindow::UpdateGraphInfo( CPopulation *inPop )
  90. {
  91.     LCaption     *theCaption;
  92.     LStr255        theString;
  93.  
  94.     // Name
  95.     theCaption     = (LCaption*) FindPaneByID( capt_Name );    
  96.  
  97.     if ( inPop ) {
  98.         GetDescriptor( theString );
  99.         theCaption    -> SetDescriptor( theString );
  100.     } else
  101.         theCaption     -> SetDescriptor( "\pn/a" );
  102.  
  103.     // Nodes
  104.     theCaption     = (LCaption*) FindPaneByID( capt_Nodes );    
  105.     if ( inPop ) {    
  106.         theCaption     -> SetValue( (Int32) inPop -> GetNodeCount() );
  107.         theCaption     -> Refresh();
  108.     } else
  109.         theCaption     -> SetDescriptor( "\pn/a" );
  110.  
  111.     // Edges
  112.     theCaption     = (LCaption*) FindPaneByID( capt_Edges );
  113.     if ( inPop ) {    
  114.         theCaption     -> SetValue( (Int32) inPop -> GetEdgeCount() );
  115.         theCaption  -> Refresh();
  116.     } else
  117.         theCaption     -> SetDescriptor( "\pn/a" );
  118.  
  119.     // Grid size - for example "20 x 20"
  120.     theCaption     = (LCaption*) FindPaneByID( capt_GridSize );
  121.     LStr255    theNbrString( (Int32) mApp -> GetGridSize() );
  122.     
  123.     theString.Assign( theNbrString );
  124.     theString.Append( "\p x " );    
  125.     theString.Append( theNbrString );
  126.  
  127.     theCaption    -> SetDescriptor( theString );
  128. }
  129.  
  130. // ---------------------------------------------------------------------------
  131. //        • UpdatePopInfo
  132. //
  133. //          Called by:    CGraphGAApp::InitForNoGraph
  134. //                        CGraphGAApp::ObeyCommand
  135. //                        CGraphGADoc::SpendTime
  136. // ---------------------------------------------------------------------------
  137.  
  138. void
  139. CGraphWindow::UpdatePopInfo( Boolean inPopSizeToo, CPopulation *inPop )
  140. {
  141.     LStr255            theString;
  142.     Int32            theTimeInTicks, theMinutes, theSeconds;
  143.     static Int32    theOldSeconds1 = 0;
  144.  
  145.     // Pop size
  146.     // Doesn't change during iteration, so we usually don't update it
  147.     if ( inPopSizeToo ) {
  148.         mPopSizeCapt -> SetValue( (Int32) mApp -> mGeneral.sizePop );
  149.         mPopSizeCapt -> Refresh();
  150.     }
  151.  
  152.     // Current generation
  153.     if ( inPop ) {
  154.         mCurGenerCapt -> SetValue( inPop -> GetCurGeneration() );
  155.         mCurGenerCapt -> Refresh();
  156.     } else    
  157.         mCurGenerCapt -> SetDescriptor( "\pn/a" );
  158.  
  159.     // Total running time (1 minute == 3600 ticks...)
  160.     if ( inPop ) {
  161.         theTimeInTicks         = inPop -> GetRunningTime();
  162.         theMinutes            = theTimeInTicks / 3600;
  163.         theSeconds            = (theTimeInTicks % 3600) / 60;
  164.     
  165.         if ( (theSeconds != theOldSeconds1) || (theSeconds == 0) ) {
  166.             theString.Assign( theMinutes );
  167.             if ( theSeconds > 9 )
  168.                     theString.Append( "\p:" );
  169.             else    theString.Append( "\p:0" );
  170.  
  171.             LStr255 theNbrString( theSeconds );
  172.             theString.Append( theNbrString );
  173.             mRunningCapt -> SetDescriptor( theString );
  174.             
  175.             theOldSeconds1 = theSeconds;
  176.         }
  177.     } else
  178.         mRunningCapt -> SetDescriptor( "\pn/a" );
  179.  
  180.     // Least crossings
  181.     if ( inPop ) {
  182.         mLeastCapt -> SetValue( inPop -> GetLeastCrossings() );
  183.         mLeastCapt -> Refresh();
  184.     } else    
  185.         mLeastCapt -> SetDescriptor( "\pn/a" );
  186.  
  187.     // Average crossings
  188.     if ( inPop ) {
  189.         mAverageCapt -> SetValue( inPop -> GetAvgCrossings() );
  190.         mAverageCapt -> Refresh();
  191.     } else
  192.         mAverageCapt -> SetDescriptor( "\pn/a" );
  193. }
  194.  
  195. // ---------------------------------------------------------------------------
  196. //        • UpdateIterStateInfo
  197. //
  198. //          Called by:    CGraphGAApp::InitForNoGraph
  199. //                        CGraphGADoc::SetIterState
  200. // ---------------------------------------------------------------------------
  201.  
  202. void
  203. CGraphWindow::UpdateIterStateInfo( EIterState inIterState )
  204. {
  205.     LStr255        theString;
  206.     
  207.     // Iteration state
  208.  
  209.     switch ( inIterState ) {
  210.  
  211.         case iter_NoGraph:
  212.             theString.Assign( STRx_IterStates, str_NoGraph );
  213.             break;
  214.         case iter_ReadyToGo:
  215.             theString.Assign( STRx_IterStates, str_ReadyToStart );
  216.             break;
  217.         case iter_Running:
  218.             theString.Assign( STRx_IterStates, str_Running );
  219.             break;
  220.         case iter_Paused:
  221.             theString.Assign( STRx_IterStates, str_Paused );
  222.             break;
  223.         case iter_Finished:
  224.             theString.Assign( STRx_IterStates, str_Finished );
  225.             break;
  226.         case iter_Walking:
  227.             theString.Assign( STRx_IterStates, str_Walking );
  228.             break;
  229.     }
  230.  
  231.     mIterCapt -> SetDescriptor( theString );
  232. }
  233.  
  234. // ---------------------------------------------------------------------------
  235. //        • UpdateBestInfo
  236. //
  237. //          Called by:    CGraphGAApp::InitForNoGraph
  238. //                        CGraphGADoc::PrepareToIterate
  239. //                        CGraphGADoc::SpendTime
  240. // ---------------------------------------------------------------------------
  241.  
  242. void
  243. CGraphWindow::UpdateBestInfo( Boolean inBestHasChanged, CPopulation *inPop )
  244. {
  245.     LStr255            theString, theNbrString;
  246.     Int32            theNoChangeTicks, theMinutes, theSeconds;
  247.     static Int32    theOldSeconds2 = 0;
  248.     CFitness        theFitness;
  249.  
  250.     if ( inBestHasChanged ) {
  251.  
  252.         // Fitness
  253.         if ( inPop ) {
  254.             inPop -> GetBestEverFitness( theFitness );
  255.             theString.Assign( (Int32) theFitness.GetCrossings() );
  256.             theString.Append( "\p/" );
  257.             theNbrString.Assign( theFitness.Get2ndValue() );
  258.             theString.Append( theNbrString );
  259.             mFitnessCapt -> SetDescriptor( theString );
  260.         } else
  261.             mFitnessCapt -> SetDescriptor( "\pn/a" );
  262.         
  263.         // Generation
  264.         if ( inPop ) {
  265.             mBestGenCapt -> SetValue( inPop -> GetBestGeneration() );
  266.             mBestGenCapt -> Refresh();    
  267.         } else
  268.             mBestGenCapt -> SetDescriptor( "\pn/a" );
  269.     }
  270.     
  271.     // Generations without improvement
  272.     if ( inPop ) {
  273.         mNoGenCapt -> SetValue( inPop -> GetNoChangeGener() );
  274.         mNoGenCapt -> Refresh();
  275.     } else
  276.         mNoGenCapt -> SetDescriptor( "\pn/a" );
  277.  
  278.     // Time without improvement (1 minute == 3600 ticks...)
  279.     if ( inPop ) {
  280.         theNoChangeTicks     = inPop -> GetNoChangeTime();
  281.         theMinutes            = theNoChangeTicks / 3600;
  282.         theSeconds            = (theNoChangeTicks % 3600) / 60;
  283.     
  284.         if ( (theSeconds != theOldSeconds2) || (theSeconds == 0) ) {
  285.             theString.Assign( theMinutes );
  286.             if ( theSeconds > 9 )
  287.                     theString.Append( "\p:" );
  288.             else    theString.Append( "\p:0" );
  289.             
  290.             theNbrString.Assign( theSeconds );
  291.             theString.Append( theNbrString );
  292.             mNoTimeCapt -> SetDescriptor( theString );
  293.             
  294.             theOldSeconds2 = theSeconds;
  295.         }  
  296.     } else
  297.         mNoTimeCapt -> SetDescriptor( "\pn/a" );
  298. }
  299.  
  300. // ---------------------------------------------------------------------------
  301. //        • DoSetZoom (OVERRIDE)
  302. // ---------------------------------------------------------------------------
  303. //    Zoom window to either the Standard or User state
  304.  
  305. void
  306. CGraphWindow::DoSetZoom(
  307.     Boolean    inZoomToStdState)
  308. {
  309.     LWindow::DoSetZoom( inZoomToStdState);
  310.     
  311.     LCommander::SetUpdateCommandStatus( true );
  312. }
  313.