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

  1. /******************************************************************************
  2. * .FILE:         aearthw5.cpp                                                 *
  3. *                                                                             *
  4. * .DESCRIPTION:  Hello World Sample Program Version 5: Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      AEarthWindow                                                 *
  7. *                AEarthWindowResizeHandler                                    *
  8. *                Star                                                         *
  9. *                                                                             *
  10. * .COPYRIGHT:                                                                 *
  11. *    Licensed Material - Program-Property of IBM                              *
  12. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  13. *                                                                             *
  14. * .DISCLAIMER:                                                                *
  15. *   The following [enclosed] code is sample code created by IBM               *
  16. *   Corporation.  This sample code is not part of any standard IBM product    *
  17. *   and is provided to you solely for the purpose of assisting you in the     *
  18. *   development of your applications.  The code is provided 'AS IS',          *
  19. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  20. *   arising out of your use of the sample code, even if they have been        *
  21. *   advised of the possibility of such damages.                               *
  22. *                                                                             *
  23. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  24. *                                                                             *
  25. ******************************************************************************/
  26.  
  27. #include <ibase.hpp>
  28. #include "aearthw5.hpp"
  29. /**************************************************************************
  30. * Class Star::Star - Constructor for drawing a star                        *
  31. **************************************************************************/
  32. Star::Star(const IPoint &pt)
  33.    : IGLine(pt,pt)
  34. {
  35. }
  36.  
  37. /**************************************************************************
  38. * Class Star::setPoint - Sets the point where a star is located            *
  39. **************************************************************************/
  40. Star
  41.  &Star::setPoint(const IPoint &pt)
  42. {
  43.   setStartingPoint(pt);
  44.   setEndingPoint(IPoint(pt.x()+1,pt.y()+1));
  45.   return *this;
  46. }
  47.  
  48. /**************************************************************************
  49. * AEarthWindow :: AEarthWindow - Constructor for the Earth window         *
  50. **************************************************************************/
  51. AEarthWindow :: AEarthWindow(unsigned long windowId,
  52.                              IWindow * parowWindow,
  53.                              const IRectangle& rect)
  54.                :IDrawingCanvas(windowId, parowWindow, parowWindow, rect)
  55.                 ,spaceColor(IColor::black)
  56.                 ,globeColor(IColor::cyan)
  57.                 ,starColor(IColor::white)
  58.                 ,earthWindowResizeHandler(this)
  59. {
  60. /*------------------------------------------------------------------------|
  61. |  Performs the initial draw of the stars and the world.  The first       |
  62. |  elements in both starlist and earthArc are set to null so that the     |
  63. |  objects will be created.  paintStars() and paintWorld will then        |
  64. |  initialize the objects.                                                |
  65. -------------------------------------------------------------------------*/
  66.     starlist[0]=0;
  67.     earthArc[0]=0;
  68.     paintWorld();
  69.  
  70. /*------------------------------------------------------------------------|
  71. |  Adds all of the objects to the glist and then attaches it to the       |
  72. |    IDrawingCanvas.                                                      |
  73. -------------------------------------------------------------------------*/
  74.     graphList.addAsLast(space)
  75.              .addAsLast(starGraphicList)
  76.              .addAsLast(earthGraphicList);
  77.     setGraphicList(&graphList);
  78.  
  79. /*------------------------------------------------------------------------|
  80. |   The ResizeHandler is attached to the AEarthWindow object to begin     |
  81. |   handling resize events.  Each time the IDrawingCanvas window needs to |
  82. |   be resized, AEarthWindowResizeHandler::resizeWindow function will be  |
  83. |   called.  The first painting occurs because of IWindow::show.          |
  84. -------------------------------------------------------------------------*/
  85.  
  86.    earthWindowResizeHandler.handleEventsFor(this);
  87.    show();
  88. } /* end AEarthWindow :: AEarthWindow(...) */
  89.  
  90. /**************************************************************************
  91. * AEarthWindow :: ~AEarthWindow - Destructor for Earth window             *
  92. **************************************************************************/
  93. AEarthWindow :: ~AEarthWindow()
  94. {
  95. /*------------------------------------------------------------------------|
  96. | Tell earthWindowResizeHandler to stop handling events for AEarthWindow. |
  97. -------------------------------------------------------------------------*/
  98.   earthWindowResizeHandler.stopHandlingEventsFor(this);
  99.  
  100. /*------------------------------------------------------------------------|
  101. | Delete the graphic objects in starlist and earthArc.                    |
  102. -------------------------------------------------------------------------*/
  103.   for (int i=0;i<stars ;i++ )
  104.   {
  105.      delete starlist[i];
  106.   }
  107.   for (i=0;i<=atmosphereLayers ; i++ )
  108.   {
  109.      delete earthArc[i];
  110.   }
  111. } /* end AEarthWindow :: ~AEarthWindow() */
  112.  
  113.  
  114. /**************************************************************************
  115. * AEarthWindow :: paintWorld - paint a view of Earth from space           *
  116. **************************************************************************/
  117. IBase::Boolean AEarthWindow :: paintWorld()
  118. {
  119.   Boolean
  120.     worldPainted = false;
  121. /*------------------------------------------------------------------------|
  122. | Construct the graphic bundles that will be attached to each layer of    |
  123. | atmosphere or planet.  This will only be perormed on the first call of  |
  124. | this function.                                                          |
  125. |------------------------------------------------------------------------*/
  126.   IGraphicBundle
  127.     arcbundle[4];
  128.   if (!earthArc[0])
  129.   {
  130.     arcbundle[0]
  131.      .setFillPattern(IGraphicBundle::filled)
  132.      .setPenColor(globeColor)
  133.      .setFillColor(globeColor);
  134.     arcbundle[1]
  135.      .setFillPattern(IGraphicBundle::filled)
  136.      .setPenColor(IColor::blue)
  137.      .setFillColor(IColor::blue);
  138.     arcbundle[2]
  139.      .setFillPattern(IGraphicBundle::filled)
  140.      .setPenColor(IColor::darkCyan)
  141.      .setFillColor(IColor::darkCyan);
  142.     arcbundle[3]
  143.      .setFillPattern(IGraphicBundle::filled)
  144.      .setPenColor(IColor::darkBlue)
  145.      .setFillColor(IColor::darkBlue);
  146.   }
  147.  
  148.  
  149. /*------------------- Construct Squares for Arc Casings ------------------|
  150. | Construct four squares such that the leftCenter, topCenter, and         |
  151. |   rightCenter points describe the arcs to be drawn for the Earth and    |
  152. |   its atmosphere.  The squares are constructed from IRectangle objects  |
  153. |   positioned initially at the center of the presentation space          |
  154. |   rectangle and then moved 1/2 the square's width to the left and a     |
  155. |   full square's height plus an arbitrary offset down.                   |
  156. |------------------------------------------------------------------------*/
  157.   const IRectangle
  158.     psRect(rect());
  159.   const int
  160.     arcs=4;
  161.   const float
  162.     arcDropFactor[arcs] = {1.0/8,  1.0/16,  1.0/32, 0},
  163.     arcSizeFactor[arcs] = {9.0/4, 21.0/8,  45.0/16, 3};
  164.   const long
  165.     psHeight=psRect.height();
  166.   long
  167.     arcDrop,
  168.     arcDiameter;
  169.   IPair
  170.     arcOffset;
  171.   IRectangle
  172.     arcSquare[arcs];
  173.   int i;
  174.  
  175.   for (i=0;i<arcs;i++ )
  176.   {
  177.     arcDrop = psHeight*arcDropFactor[i];
  178.     arcDiameter = psHeight*arcSizeFactor[i];
  179.     arcOffset = IPair(-1*arcDiameter/2,-1*arcDiameter-arcDrop);
  180.     arcSquare[i] = IRectangle(psRect.center(),
  181.                      ISize(arcDiameter, arcDiameter));
  182.     arcSquare[i].moveBy(arcOffset);
  183.   }
  184.  
  185. /*------------------------------- Color Space ----------------------------|
  186. | Set the background color to space, which is IColor::black.              |
  187. |------------------------------------------------------------------------*/
  188.   IGraphicBundle spaceBundle;
  189.   spaceBundle.setPenColor(spaceColor);
  190.   spaceBundle.setFillColor(spaceColor);
  191.   spaceBundle.setFillPattern(IGraphicBundle::filled);
  192.   space.setEnclosingRect(rect());
  193.   space.setGraphicBundle(spaceBundle);
  194.  
  195.  
  196. /*-------------------- Draw the Earth and Atmosphere ---------------------|
  197. | Draw the earth and the number of layers of atmosphere specified in      |
  198. |   atmosphereLayers.  arcSquare[0] contains the arc dimension for the    |
  199. |   earth and the other arcSquare objects specify each atmosphere layer.  |
  200. | The arcs are drawn by drawing an ellipse and allowing the program to    |
  201. |   clip the bottom half of the ellipse.  This section of code sets       |
  202. |   the enclosing rectangle that defines the ellipse.  The graphic        |
  203. |   bundles are also attached to set the pen color, fill color, and       |
  204. |   the fill pattern for each ellipse.                                    |
  205. | The ellipses are only instantiated on the first call to this function.  |
  206. |------------------------------------------------------------------------*/
  207.   if (earthArc[0])
  208.      for(i=atmosphereLayers;i>=0;i--)
  209.        earthArc[i]->setEnclosingRect(arcSquare[i]);
  210.   else
  211.   {
  212.      earthGraphicList.removeAll();
  213.      for(i=atmosphereLayers;i>=0;i--)
  214.      {
  215.         earthArc[i]=new IGEllipse(arcSquare[i]);
  216.         earthArc[i]->setGraphicBundle(arcbundle[i%3+1]);
  217.         earthGraphicList.addAsLast(*earthArc[i]);
  218.      }
  219.      earthArc[0]->setGraphicBundle(arcbundle[0]);
  220.   }
  221. /*--------------------------- Paint the Stars ----------------------------|
  222. | Call the AEarthWindow function for painting the stars.                  |
  223. |------------------------------------------------------------------------*/
  224.   worldPainted=paintStars();
  225.  
  226.   return (worldPainted);
  227.  
  228. } /* end AEarthWindow :: paintWorld(..) */
  229.  
  230. /**************************************************************************
  231. * AEarthWindow :: paintStars - paint the stars in the Earth window        *
  232. **************************************************************************/
  233. IBase::Boolean AEarthWindow :: paintStars()
  234. {
  235.   Boolean starsPainted = false;
  236.  
  237. /*------------------- Get Presentation Space Objects ---------------------|
  238. | Get the presentation space handle (called "graphics context" in AIX)    |
  239. |   and the rectangle of the area that needs to be painted.               |
  240. |------------------------------------------------------------------------*/
  241.   const IRectangle
  242.     psRect(rect());
  243.  
  244. /*------------------- Construct Stars from IPoints -----------------------|
  245. | Construct a star array where each star is a point within the            |
  246. |   presentation space rectangle.  Each point is computed as a fraction   |
  247. |   of the psRect size offset from the origin of the psRect.              |
  248. |------------------------------------------------------------------------*/
  249.   const int
  250.     stars=13;
  251.   const IPair
  252.     psOrigin(psRect.bottomLeft()),
  253.     psSize(psRect.size());
  254.   int
  255.     i, j;
  256.  
  257.   IPoint
  258.     star[stars];
  259.   star[0] =IPoint(psOrigin+psSize.scaledBy(0.98,0.43));
  260.   star[1] =IPoint(psOrigin+psSize.scaledBy(0.70,0.69));
  261.   star[2] =IPoint(psOrigin+psSize.scaledBy(0.20,0.50));
  262.   star[3] =IPoint(psOrigin+psSize.scaledBy(0.80,0.63));
  263.   star[4] =IPoint(psOrigin+psSize.scaledBy(0.05,0.41));
  264.   star[5] =IPoint(psOrigin+psSize.scaledBy(0.50,0.69));
  265.   star[6] =IPoint(psOrigin+psSize.scaledBy(0.60,0.94));
  266.   star[7] =IPoint(psOrigin+psSize.scaledBy(0.10,0.87));
  267.   star[8] =IPoint(psOrigin+psSize.scaledBy(0.40,0.81));
  268.   star[9] =IPoint(psOrigin+psSize.scaledBy(0.25,0.69));
  269.   star[10]=IPoint(psOrigin+psSize.scaledBy(0.75,0.63));
  270.   star[11]=IPoint(psOrigin+psSize.scaledBy(0.30,0.87));
  271.   star[12]=IPoint(psOrigin+psSize.scaledBy(0.95,0.87));
  272.  
  273.  
  274. /*------------------------------------------------------------------------|
  275. | Draw the stars by setting the starBundle to white and by setting the    |
  276. | position of that the star will be drawn at.  On the first call to this  |
  277. | function, each star will instantiated while setting the location and    |
  278. | the graphic bundle will be set to starBundle.                           |
  279. |------------------------------------------------------------------------*/
  280.   IGraphicBundle starBundle;
  281.   starBundle.setPenColor(starColor);
  282.  
  283.   if (starlist[0])
  284.   {
  285.     for (i=0;i<stars;i++)
  286.       starlist[i]->setPoint(star[i]);
  287.   }
  288.   else
  289.   {
  290.     starGraphicList.removeAll();
  291.     for (int i=0;i<stars ;i++ )
  292.     {
  293.       starlist[i]=new Star(star[i]);
  294.       starlist[i]->setGraphicBundle(starBundle);
  295.       starGraphicList.addAsLast(*starlist[i]);
  296.     }
  297.   }
  298.  
  299.   starsPainted = true;
  300.   return (starsPainted);
  301.  
  302. } /* end AEarthWindow :: paintStars(...) */
  303.  
  304. /******************************************************************************
  305. * Class AEarthWIndowResizeHandler :: AEarthWindowResizeHandler - Constructor  *
  306. *   for the timecard's pie chart                                              *
  307. *                                                                             *
  308. * Define yourself as an IResizeHandler                                        *
  309. * Store a pointer to the picture                                              *
  310. ******************************************************************************/
  311. AEarthWindowResizeHandler::AEarthWindowResizeHandler( AEarthWindow * aew )
  312.      :IResizeHandler()
  313.      , earthWindow ( aew )
  314. {
  315. }
  316.  
  317.  
  318. /******************************************************************************
  319. * Class AEarthWindowResizeHandler :: ~AEarthWindowResizeHandler - Destructor  *
  320. ******************************************************************************/
  321. AEarthWindowResizeHandler::~AEarthWindowResizeHandler( )
  322. {
  323. }
  324.  
  325.  
  326. /******************************************************************************
  327. * Class AEarthWindowResizeHandler :: windowResize() - Called when a resize    *
  328. *   event occurs for the picture.                                             *
  329. ******************************************************************************/
  330. IBase::Boolean AEarthWindowResizeHandler::windowResize( IResizeEvent& event )
  331. {
  332. /*-----------------------------------------------------------------------------
  333. | Call our own resizing function and repaint the drawing canvas.              |
  334. -----------------------------------------------------------------------------*/
  335.    earthWindow->paintWorld();
  336.    earthWindow->refresh();
  337.    return false;
  338. }
  339.  
  340.