home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / acdf6 / acdfeat6.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  10.5 KB  |  269 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                      SAMPLE CODE
  3. //
  4. // FileName: ACDFEat6.cpp
  5. //
  6. // ClassName: AEartWindow
  7. //
  8. // Description: Conversion of the Hello World 5 Sample
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11.     
  12. #include <ibase.hpp>                  
  13. #include <icoordsy.hpp>
  14. #include "ACDFEat6.hpp"
  15.  
  16. Star::Star(const IPoint &pt):
  17.     IGLine(pt,pt)
  18. // Constructor for drawing a star
  19. {   IFUNCTRACE_DEVELOP();}
  20.  
  21. Star &Star::setPoint(const IPoint &pt)
  22. // Sets the point where a star is located 
  23. {   IFUNCTRACE_DEVELOP();
  24.     setEndingPoint(pt);
  25.     setStartingPoint(pt); 
  26.     return *this;
  27. }
  28.  
  29. AEarthWindow :: AEarthWindow(unsigned long windowId,
  30.                              IWindow * parowWindow,
  31.                              const IRectangle& rect) :
  32.     IDrawingCanvas(windowId, 
  33.                     parowWindow, 
  34.                     parowWindow, 
  35.                     rect),
  36.     spaceColor(IColor::black),
  37.     globeColor(IColor::cyan),
  38.     starColor(IColor::white),
  39.     earthWindowResizeHandler(this)
  40.     // Constructor for the Earth window
  41. {   IFUNCTRACE_DEVELOP();
  42.  
  43.     // Performs the initial draw of the stars and the world.  The first
  44.     // elements in both starlist and earthArc are set to null so that the
  45.     // objects will be created.  paintStars() and paintWorld will then
  46.     // initialize the objects.
  47.     starlist[0]=0;
  48.     earthArc[0]=0;
  49.     paintWorld();
  50.  
  51.     // Adds all of the objects to the glist and then attaches it to the IDrawingCanvas.
  52.     graphList.addAsLast(space)
  53.              .addAsLast(starGraphicList)
  54.              .addAsLast(earthGraphicList);
  55.     setGraphicList(&graphList);
  56.  
  57.     // The ResizeHandler is attached to the AEarthWindow object to begin
  58.     //  handling resize events.  Each time the IDrawingCanvas window needs to
  59.     //  be resized, AEarthWindowResizeHandler::resizeWindow function will be
  60.     //  called.  The first painting occurs because of IWindow::show.
  61.     earthWindowResizeHandler.handleEventsFor(this);
  62.     show();
  63. }
  64.  
  65. AEarthWindow :: ~AEarthWindow()                                             
  66.     // Destructor for Earth window 
  67. {   IFUNCTRACE_DEVELOP();
  68.  
  69.     // Tell earthWindowResizeHandler to stop handling events for AEarthWindow.
  70.     earthWindowResizeHandler.stopHandlingEventsFor(this);
  71.  
  72.     // Delete the graphic objects in starlist and earthArc.
  73.     for (int i=0;i<stars ;i++ ) 
  74.     {
  75.         delete starlist[i];
  76.     }
  77.     for (i=0;i<=atmosphereLayers ; i++ ) 
  78.     {
  79.         delete earthArc[i];
  80.     } 
  81. }
  82.  
  83. Boolean AEarthWindow :: paintWorld()                                              
  84. // paint a view of Earth from space 
  85. {   IFUNCTRACE_DEVELOP();
  86.     Boolean           worldPainted = false;
  87.  
  88.     // Construct the graphic bundles that will be attached to each layer of
  89.     // atmosphere or planet.  This will only be perormed on the first call of
  90.     // this function.
  91.  
  92.     IGraphicBundle    arcbundle[4];
  93.  
  94.     if (!earthArc[0]) 
  95.     {
  96.         arcbundle[0]
  97.             .setFillPattern(IGraphicBundle::solid)
  98.             .setPenColor(globeColor)
  99.             .setFillColor(globeColor);
  100.         arcbundle[1]
  101.             .setFillPattern(IGraphicBundle::solid)
  102.             .setPenColor(IColor::blue)
  103.             .setFillColor(IColor::blue);
  104.         arcbundle[2]
  105.             .setFillPattern(IGraphicBundle::solid)
  106.             .setPenColor(IColor::darkCyan)
  107.             .setFillColor(IColor::darkCyan);
  108.         arcbundle[3]
  109.             .setFillPattern(IGraphicBundle::solid)
  110.             .setPenColor(IColor::darkBlue)
  111.             .setFillColor(IColor::darkBlue);
  112.     }
  113.  
  114.     // Construct four squares such that the leftCenter, topCenter, and
  115.     // rightCenter points describe the arcs to be drawn for the Earth and
  116.     // its atmosphere.  The squares are constructed from IRectangle objects
  117.     // positioned initially at the center of the presentation space
  118.     // rectangle and then moved 1/2 the square's width to the left and a
  119.     // full square's height plus an arbitrary offset down.
  120.  
  121.     const IRectangle  psRect(rect());                                                           
  122.     const int         arcs=4;
  123.     const float       arcDropFactor[arcs] = {1.0/8,  1.0/16,  1.0/32, 0},                       
  124.                       arcSizeFactor[arcs] = {9.0/4, 21.0/8,  45.0/16, 3};                       
  125.     const long        psHeight=psRect.height();                                                 
  126.     long              arcDrop,
  127.                       arcDiameter;
  128.     IPair             arcOffset;
  129.     IRectangle        arcSquare[arcs];
  130.     int               i;                                                                      
  131.  
  132.     for (i=0;i<arcs;i++ )                                                     
  133.     {                                                                         
  134.         arcDrop = psHeight*arcDropFactor[i];                                    
  135.         arcDiameter = psHeight*arcSizeFactor[i];                                
  136.         arcOffset = IPair(-1*arcDiameter/2,-1*arcDiameter-arcDrop);             
  137.         arcSquare[i] = IRectangle(psRect.center(),                              
  138.                        ISize(arcDiameter, arcDiameter));           
  139.         arcSquare[i].moveBy(arcOffset);                                         
  140.     }                                                                         
  141.  
  142.  
  143.     // Set the background color to space, which is IColor::black.
  144.  
  145.     IGraphicBundle    spaceBundle;
  146.     spaceBundle.setPenColor(spaceColor);
  147.     spaceBundle.setFillColor(spaceColor);
  148.     spaceBundle.setFillPattern(IGraphicBundle::solid);
  149.     space.setEnclosingRect(rect());
  150.     space.setGraphicBundle(spaceBundle);
  151.  
  152.  
  153.     // Draw the earth and the number of layers of atmosphere specified in
  154.     // atmosphereLayers.  arcSquare[0] contains the arc dimension for the
  155.     // earth and the other arcSquare objects specify each atmosphere layer.
  156.     // The arcs are drawn by drawing an ellipse and allowing the program to 
  157.     // clip the bottom half of the ellipse.  This section of code sets
  158.     // the enclosing rectangle that defines the ellipse.  The graphic 
  159.     // bundles are also attached to set the pen color, fill color, and
  160.     // the fill pattern for each ellipse.
  161.     // The ellipses are only instantiated on the first call to this function.
  162.  
  163.     if (earthArc[0]) 
  164.         for(i=atmosphereLayers;i>=0;i--)
  165.             earthArc[i]->setEnclosingRect(arcSquare[i]);
  166.     else
  167.     {
  168.         earthGraphicList.removeAll();
  169.         for(i=atmosphereLayers;i>=0;i--)
  170.         {
  171.             earthArc[i]=new IGEllipse(arcSquare[i]);
  172.             earthArc[i]->setGraphicBundle(arcbundle[i%3+1]);
  173.             earthGraphicList.addAsLast(*earthArc[i]);
  174.         }
  175.         earthArc[0]->setGraphicBundle(arcbundle[0]);
  176.     }
  177.  
  178.     // Call the AEarthWindow function for painting the stars.
  179.  
  180.     worldPainted=paintStars();                                                
  181.     
  182.     return (worldPainted);                                                    
  183.  
  184.  
  185. Boolean AEarthWindow :: paintStars()
  186. // paint the stars in the Earth window 
  187. {   IFUNCTRACE_DEVELOP();
  188.  
  189.     Boolean           starsPainted = false;
  190.  
  191.     // Get the presentation space handle (called "graphics context" in AIX)
  192.     // and the rectangle of the area that needs to be painted.
  193.  
  194.     const IRectangle  psRect(rect());                                                           
  195.  
  196.  
  197.     // Construct a star array where each star is a point within the 
  198.     //  presentation space rectangle.  Each point is computed as a fraction
  199.     //  of the psRect size offset from the origin of the psRect.
  200.     
  201.     const int         stars=13;
  202.     const IPair       psOrigin(psRect.bottomLeft()),                                            
  203.                       psSize(psRect.size());                                                    
  204.     int               i,
  205.                       j;                                                                     
  206.  
  207.     IPoint            star[stars];
  208.     star[0] =IPoint(psOrigin+psSize.scaledBy(0.98,0.43));                     
  209.     star[1] =IPoint(psOrigin+psSize.scaledBy(0.70,0.69));                     
  210.     star[2] =IPoint(psOrigin+psSize.scaledBy(0.20,0.50));                     
  211.     star[3] =IPoint(psOrigin+psSize.scaledBy(0.80,0.63));                     
  212.     star[4] =IPoint(psOrigin+psSize.scaledBy(0.05,0.41));                     
  213.     star[5] =IPoint(psOrigin+psSize.scaledBy(0.50,0.69));                     
  214.     star[6] =IPoint(psOrigin+psSize.scaledBy(0.60,0.94));                     
  215.     star[7] =IPoint(psOrigin+psSize.scaledBy(0.10,0.87));                     
  216.     star[8] =IPoint(psOrigin+psSize.scaledBy(0.40,0.81));                     
  217.     star[9] =IPoint(psOrigin+psSize.scaledBy(0.25,0.69));                     
  218.     star[10]=IPoint(psOrigin+psSize.scaledBy(0.75,0.63));                     
  219.     star[11]=IPoint(psOrigin+psSize.scaledBy(0.30,0.87));                     
  220.     star[12]=IPoint(psOrigin+psSize.scaledBy(0.95,0.87));                     
  221.  
  222.     // Draw the stars by setting the starBundle to white and by setting the
  223.     // position of that the star will be drawn at.  On the first call to this
  224.     // function, each star will instantiated while setting the location and
  225.     // the graphic bundle will be set to starBundle.
  226.  
  227.     IGraphicBundle    starBundle;
  228.     starBundle.setPenColor(starColor);
  229.  
  230.     if (starlist[0])
  231.     {
  232.         for (i=0;i<stars;i++)        
  233.         starlist[i]->setPoint(star[i]);
  234.     } 
  235.     else 
  236.     {
  237.         starGraphicList.removeAll();
  238.         for (int i=0;i<stars ;i++ ) 
  239.         {
  240.             starlist[i]=new Star(star[i]);
  241.             starlist[i]->setGraphicBundle(starBundle);
  242.             starGraphicList.addAsLast(*starlist[i]);
  243.         }
  244.     } 
  245.                                       
  246.     starsPainted = true;                                                      
  247.     return (starsPainted);                                                    
  248. }
  249.  
  250. AEarthWindowResizeHandler::AEarthWindowResizeHandler( AEarthWindow * aew ) :
  251.      IResizeHandler(),
  252.      earthWindow ( aew )
  253. // Constructor for the timecard's pie chart
  254. {   IFUNCTRACE_DEVELOP();}
  255.  
  256. AEarthWindowResizeHandler::~AEarthWindowResizeHandler( )
  257. //  Destructor
  258. {   IFUNCTRACE_DEVELOP();}
  259.  
  260. Boolean AEarthWindowResizeHandler::windowResize( IResizeEvent& event )
  261. // Called when a resize event occurs for the picture.
  262. {   IFUNCTRACE_DEVELOP();
  263.  
  264.     earthWindow->paintWorld();
  265.     return false;
  266. }
  267.  
  268.