home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / HELLO5 / AEARTHW5.CPP < prev    next >
C/C++ Source or Header  |  1995-04-07  |  28KB  |  365 lines

  1. /*****************************************************************************
  2. * HELLO WORLD SAMPLE PROGRAM - Version 5: Class Implementation (aearthw5.cpp)*
  3. *                                                                            *
  4. * COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1995. *
  5. *                                                                            *
  6. * DISCLAIMER OF WARRANTIES:                                                  *
  7. *   The following [enclosed] code is sample code created by IBM              *
  8. *   Corporation.  This sample code is not part of any standard IBM product   *
  9. *   and is provided to you solely for the purpose of assisting you in the    *
  10. *   development of your applications.  The code is provided "AS IS",         *
  11. *   without warranty of any kind.  IBM shall not be liable for any damages   *
  12. *   arising out of your use of the sample code, even if they have been       *
  13. *   advised of the possibility of such damages.                              *
  14. *****************************************************************************/
  15. // NOTE: WE RECOMMEND USING A FIXED-SPACE FONT TO LOOK AT THE SOURCE.
  16. #ifndef _IBASE_                         //Make sure ibase.h is included       V5
  17.   #include <ibase.hpp>                  //  since that is where IC_<environ>  V5
  18. #endif                                  //  is defined.                       V5
  19. #ifdef IC_MOTIF                         //For MOTIF,                          V5
  20.   extern "C" {                          //  include the X-Windows             V5
  21.     #include <X11/Xlib.h>               //  libraries for drawing and         V5
  22.     #include <X11/Intrinsic.h>          //  XtDisplay and XtWindow intrinsics V5
  23.   }                                                                         //V5
  24. #endif                                                                      //V5
  25. #ifdef IC_PM                            //For PM,                             V5
  26.   #define INCL_GPIPRIMITIVES            //  include the                       V5
  27.   #define INCL_GPIPATHS                 //  drawing routines                  V5
  28.   #include <os2.h>                      //  from the OS/2 libraries           V5
  29. #endif                                                                      //V5
  30.  
  31. #include "aearthw5.hpp"                                                     //v5
  32.  
  33. /**************************************************************************   V5
  34. * AEarthWindow :: AEarthWindow - Constructor for the Earth window         *   V5
  35. **************************************************************************/ //V5
  36. AEarthWindow :: AEarthWindow(unsigned long windowId,                        //V5
  37.                              IWindow * parowWindow,                         //V5
  38.                              const IRectangle& rect)                        //V5
  39.                :IStaticText(windowId, parowWindow, parowWindow, rect)       //V5
  40.                 ,atmosphereLayers(3)                                        //V5
  41.                 ,spaceColor(IColor::black)                                  //V5
  42.                 ,globeColor(IColor::cyan)                                   //V5
  43.                 ,starColor(IColor::white)                                   //V5
  44. {                                                                           //V5
  45. /*-------------------- Construct Paint Handler and Show ------------------|   V5
  46. | The paintHandler is attached to the AEarthWindow object to begin        |   V5
  47. |   handling paint events.  Each time the static text window needs to     |   V5
  48. |   be repainted, the APaintHandler::paintWindow function will be         |   V5
  49. |   called.  The first repaint occurs because of IWindow::show.           |   V5
  50. |------------------------------------------------------------------------*/ //V5
  51.   paintHandler.handleEventsFor(this);                                       //V5
  52.   show();                                                                   //V5
  53. } /* end AEarthWindow :: AEarthWindow(...) */                               //V5
  54.  
  55. /**************************************************************************   V5
  56. * AEarthWindow :: ~AEarthWindow - Destructor for Earth window             *   V5
  57. **************************************************************************/ //V5
  58. AEarthWindow :: ~AEarthWindow()                                             //V5
  59. {                                                                           //V5
  60. /*-------------------- Stop and Delete Paint Handler ---------------------|   V5
  61. | Tell paintHandler to stop handling events for AEarthWindow.             |   V5
  62. |------------------------------------------------------------------------*/ //V5
  63.   paintHandler.stopHandlingEventsFor(this);                                 //V5
  64. } /* end AEarthWindow :: ~AEarthWindow() */                                 //V5
  65.  
  66. /**************************************************************************   V5
  67. * APaintHandler :: paintWindow                                            *   V5
  68. *   This function calls the paintWorld function for the                   *   V5
  69. *   AEarthWindow object which repaints the entire static text window.     *   V5
  70. *   The IParameter2 parameter of the IPaintEvent is always passed to      *   V5
  71. *   paintWorld, but it is only used for the AIX implementation.           *   V5
  72. **************************************************************************/ //V5
  73. IBase::Boolean                                                              //V5
  74.   APaintHandler :: paintWindow(IPaintEvent & paintEvent)                    //V5
  75. {                                                                           //V5
  76.  
  77. /*------------------ Call AEarthWindow Paint World Function --------------|   V5
  78. | Get a pointer the AEarthWindow object from the paint event and use it   |   V5
  79. |   to call the paintWorld function.                                      |   V5
  80. |------------------------------------------------------------------------*/ //V5
  81.   return ( ((AEarthWindow *)paintEvent.window())->paintWorld());            //V5
  82.  
  83. } /* end APaintHandler :: paintWindow(...) */                               //V5
  84.  
  85. /**************************************************************************   V5
  86. * AEarthWindow :: paintWorld - paint a view of Earth from space           *   v5
  87. *   This function provides an example of how to use underlying low-level  *   V5
  88. *   calls in conjuction with the User Interface Class Library.  In this   *   V5
  89. *   case, native graphics routines are used to draw a view of Earth from  *   V5
  90. *   space in an IStaticText control.                                      *   V5
  91. **************************************************************************/ //V5
  92. IBase::Boolean                                                              //V5
  93.   AEarthWindow :: paintWorld()                                              //V5
  94. {                                                                           //V5
  95. Boolean worldPainted = false;
  96. /*----------------------- Construct Color Objects ------------------------|   V5
  97. | Construct an IColor object for each color used in the graphical window. |   V5
  98. |------------------------------------------------------------------------*/ //V5
  99. const IColor                                                                //V5
  100.   arcColor[4] =                                                             //V5
  101.         {globeColor, IColor::blue, IColor::darkCyan, IColor::darkBlue};     //V5
  102.  
  103. /*------------------- Get Presentation Space Objects ---------------------|   V5
  104. | Get the presentation space handle (called "graphics context" in AIX)    |   V5
  105. |   and the rectangle of the area that needs to be painted.               |   V5
  106. |------------------------------------------------------------------------*/ //V5
  107. const IPresSpaceHandle                                                      //V5
  108.   hps(presSpace());                                                         //V5
  109. const IRectangle                                                            //V5
  110.   psRect(rect());                                                           //V5
  111.  
  112. /*------------------- Construct Squares for Arc Casings ------------------|   V5
  113. | Construct four squares such that the leftCenter, topCenter, and         |   v5
  114. |   rightCenter points describe the arcs to be drawn for the Earth and    |   V5
  115. |   its atmosphere.  The squares are constructed from IRectangle objects  |   V5
  116. |   positioned initially at the center of the presentation space          |   V5
  117. |   rectangle and then moved 1/2 the square's width to the left and a     |   V5
  118. |   full square's height plus an arbitrary offset down.                   |   v5
  119. |------------------------------------------------------------------------*/ //V5
  120. const int arcs=4;                                                           //V5
  121. const float                                                                 //V5
  122.   arcDropFactor[arcs] = {1.0/8,  1.0/16,  1.0/32, 0},                       //V5
  123.   arcSizeFactor[arcs] = {9.0/4, 21.0/8,  45.0/16, 3};                       //V5
  124. const long                                                                  //V5
  125.   psHeight=psRect.height();                                                 //V5
  126. long                                                                        //V5
  127.   arcDrop,                                                                  //V5
  128.   arcDiameter;                                                              //V5
  129. IPair                                                                       //V5
  130.   arcOffset;                                                                //V5
  131. IRectangle                                                                  //V5
  132.   arcSquare[arcs];                                                          //V5
  133. int i;                                                                      //V5
  134.  
  135.   for (i=0;i<arcs;i++ )                                                     //V5
  136.   {                                                                         //V5
  137.     arcDrop = psHeight*arcDropFactor[i];                                    //V5
  138.     arcDiameter = psHeight*arcSizeFactor[i];                                //V5
  139.     arcOffset = IPair(-1*arcDiameter/2,-1*arcDiameter-arcDrop);             //V5
  140.     arcSquare[i] = IRectangle(psRect.center(),                              //V5
  141.                                 ISize(arcDiameter, arcDiameter));           //V5
  142.     arcSquare[i].moveBy(arcOffset);                                         //V5
  143.   }                                                                         //V5
  144.  
  145. #ifdef IC_MOTIF                                                             //V5
  146. /**************************************************************************   V5
  147. *****  MOTIF-specific code - BEGIN                                    *****   V5
  148. **************************************************************************/ //V5
  149. /*----------------- Construct X-lib Draw Function Arguments --------------|   V5
  150. | Construct the window and display objects needed by the X-lib drawing    |   V5
  151. |   functions from the window handle, which is the widget ID.             |   V5
  152. |   XtWindow and XtDisplay are X-lib intrinsic functions.                 |   V5
  153. |------------------------------------------------------------------------*/ //V5
  154. const Window                                                                //V5
  155.   xWindow(XtWindow(handle()));                                              //V5
  156. Display                                                                     //V5
  157.  *xDisplay(XtDisplay(handle()));                                            //V5
  158.  
  159. /*------------------------------- Color Space ----------------------------|   V5
  160. | Set the background color to space, which is IColor::black.              |   V5
  161. |      Note that because the X-Windows origin is at the top of the        |   V5
  162. |      window as opposed to the bottom in PM, the X-Windows rectangle     |   V5
  163. |      top must be computed by subtracting the arc rectangle's top        |   V5
  164. |      coordinate from the presentation space rectangle's height.         |   V5
  165. |------------------------------------------------------------------------*/ //V5
  166.   XSetForeground(xDisplay, hps, spaceColor.asPixel());                      //V5
  167.   XFillRectangle(xDisplay, xWindow, hps,                                    //V5
  168.             psRect.left(),                                                  //V5
  169.             psRect.height()-psRect.top(),                                   //V5
  170.             psRect.width(),                                                 //V5
  171.             psRect.height());                                               //V5
  172.  
  173. /*-------------------- Draw the Earth and Atmosphere ---------------------|   V5
  174. | Draw the earth and the number of layers of atmosphere specified in      |   V5
  175. |   atmosphereLayers.  arcSquare[0] contains the arc dimension for the    |   V5
  176. |   earth and the other arcSquare objects specify each atmosphere layer.  |   V5
  177. | Each arc is drawn by calling the Xlib functions for setting the         |   V5
  178. |   foreground color and for drawing an arc and filling it with the       |   V5
  179. |   foreground color.  The arc is described using rectangle coordinates.  |   V5
  180. |      Note that because the X-Windows origin is at the top of the        |   V5
  181. |      window as opposed to the bottom in PM, the X-Windows rectangle     |   V5
  182. |      top must be computed by subtracting the arc rectangle's top        |   V5
  183. |      coordinate from the presentation space rectangle's height.         |   V5
  184. |------------------------------------------------------------------------*/ //V5
  185.   for(i=atmosphereLayers;i>=0;i--)                                          //V5
  186.   {                                                                         //V5
  187.     XSetForeground(xDisplay, hps, arcColor[i].asPixel());                   //V5
  188.     XFillArc(xDisplay, xWindow, hps,                                        //V5
  189.             arcSquare[i].left(),                                            //V5
  190.             psHeight-arcSquare[i].top(),                                    //V5
  191.             arcSquare[i].width(),                                           //V5
  192.             arcSquare[i].height(),                                          //V5
  193.             0,180*64 );                                                     //V5
  194.   }                                                                         //V5
  195. /**************************************************************************   V5
  196. *****  MOTIF-specific code - END                                      *****   V5
  197. **************************************************************************/ //V5
  198. #endif                                                                      //V5
  199. #ifdef IC_PM                                                                //V5
  200. /**************************************************************************   V5
  201. *****  PM-specific code - BEGIN                                       *****   V5
  202. **************************************************************************/ //V5
  203. /*------------------------------- Color Space ----------------------------|   V5
  204. | Set the background color to space, which is IColor::black.              |   V5
  205. |------------------------------------------------------------------------*/ //V5
  206. POINTL                                                                      //V5
  207.   origin=IPoint(0,0).asPOINTL(),                                            //V5
  208.   maxPoint=((IPoint)psRect.size()).asPOINTL();                              //V5
  209.                                                                             //V5
  210.   GpiMove (hps, &origin);                                                   //V5
  211.   GpiSetColor (hps, spaceColor.index()) ;                                   //V5
  212.   GpiBox (hps, DRO_OUTLINEFILL, &maxPoint, 0, 0) ;                          //V5
  213.  
  214. /*-------------------- Draw the Earth and Atmosphere ---------------------|   V5
  215. | Draw the earth and the number of layers of atmosphere specified in      |   V5
  216. |   atmosphereLayers.  arcSquare[0] contains the arc dimension for the    |   V5
  217. |   earth and the other arcSquare objects specify each atmosphere layer.  |   V5
  218. | Each arc is drawn by calling the PM Gpi functions for setting the       |   V5
  219. |   foreground color and for drawing an arc and filling it with the       |   V5
  220. |   foreground color.  The arc is described using rectangle coordinates.  |   V5
  221. |------------------------------------------------------------------------*/ //V5
  222. POINTL                                                                      //V5
  223.   ptlist[3];                                                                //V5
  224.   for(i=atmosphereLayers;i>=0;i--)                                          //V5
  225.   {                                                                         //V5
  226.     ptlist[0]=arcSquare[i].leftCenter().asPOINTL();                         //V5
  227.     ptlist[1]=arcSquare[i].topCenter().asPOINTL();                          //V5
  228.     ptlist[2]=arcSquare[i].rightCenter().asPOINTL();                        //V5
  229.     GpiSetColor(hps, arcColor[i].index());                                  //V5
  230.     GpiBeginPath(hps, 1L);                                                  //V5
  231.     GpiMove(hps,&ptlist[0]);                                                //V5
  232.     GpiPointArc(hps,&ptlist[1]);                                            //V5
  233.     GpiLine(hps,&ptlist[0]);                                                //V5
  234.     GpiEndPath(hps);                                                        //V5
  235.     GpiFillPath(hps,1L,FPATH_ALTERNATE);                                    //V5
  236.   }                                                                         //V5
  237. /**************************************************************************   V5
  238. *****  PM-specific code - END                                         *****   V5
  239. **************************************************************************/ //V5
  240. #endif                                                                      //V5
  241.  
  242. /*--------------------------- Paint the Stars ----------------------------|   V5
  243. | Call the AEarthWindow function for painting the stars.                  |   V5
  244. |------------------------------------------------------------------------*/ //V5
  245.   worldPainted=paintStars();                                                //V5
  246.   return (worldPainted);                                                    //V5
  247.  
  248. } /* end AEarthWindow :: paintWorld(..) */                                  //V5
  249.  
  250. /**************************************************************************   V5
  251. * AEarthWindow :: paintStars - paint the stars in the Earth window        *   v5
  252. *   This function extends the example of how to use underlying low-level  *   V5
  253. *   calls in conjuction with the User Interface Class Library.  In this   *   V5
  254. *   case, native graphics routines are used to draw points which appear   *   V5
  255. *   as stars in space.                                                    *   V5
  256. **************************************************************************/ //V5
  257. IBase::Boolean                                                              //V5
  258.   AEarthWindow :: paintStars()                                              //V5
  259. {                                                                           //V5
  260. Boolean starsPainted = false;                                               //V5
  261.  
  262. /*------------------- Get Presentation Space Objects ---------------------|   V5
  263. | Get the presentation space handle (called "graphics context" in AIX)    |   V5
  264. |   and the rectangle of the area that needs to be painted.               |   V5
  265. |------------------------------------------------------------------------*/ //V5
  266. const IPresSpaceHandle                                                      //V5
  267.   hps(presSpace());                                                         //V5
  268. const IRectangle                                                            //V5
  269.   psRect(rect());                                                           //V5
  270.  
  271. /*------------------- Construct Stars from IPoints -----------------------|   V5
  272. | Construct a star array where each star is a point within the            |   V5
  273. |   presentation space rectangle.  Each point is computed as a fraction   |   V5
  274. |   of the psRect size offset from the origin of the psRect.  The         |   V5
  275. |   starIntensity will indicate if the star is made up of one point(dim), |   V5
  276. |   made up of five points(bright), or randomly dim or bright(twinkle).   |   V5
  277. |------------------------------------------------------------------------*/ //V5
  278. const int                                                                   //V5
  279.   stars=13;                                                                 //V5
  280. const IPair                                                                 //V5
  281.   psOrigin(psRect.bottomLeft()),                                            //V5
  282.   psSize(psRect.size());                                                    //V5
  283. int                                                                         //V5
  284.   i, j;                                                                     //v6
  285.  
  286. IPoint                                                                      //V5
  287.   star[stars];                                                              //V5
  288.   star[0] =IPoint(psOrigin+psSize.scaledBy(0.98,0.43));                     //V5
  289.   star[1] =IPoint(psOrigin+psSize.scaledBy(0.70,0.69));                     //V5
  290.   star[2] =IPoint(psOrigin+psSize.scaledBy(0.20,0.50));                     //V5
  291.   star[3] =IPoint(psOrigin+psSize.scaledBy(0.80,0.63));                     //V5
  292.   star[4] =IPoint(psOrigin+psSize.scaledBy(0.05,0.41));                     //V5
  293.   star[5] =IPoint(psOrigin+psSize.scaledBy(0.50,0.69));                     //V5
  294.   star[6] =IPoint(psOrigin+psSize.scaledBy(0.60,0.94));                     //V5
  295.   star[7] =IPoint(psOrigin+psSize.scaledBy(0.10,0.87));                     //V5
  296.   star[8] =IPoint(psOrigin+psSize.scaledBy(0.40,0.81));                     //V5
  297.   star[9] =IPoint(psOrigin+psSize.scaledBy(0.25,0.69));                     //V5
  298.   star[10]=IPoint(psOrigin+psSize.scaledBy(0.75,0.63));                     //V5
  299.   star[11]=IPoint(psOrigin+psSize.scaledBy(0.30,0.87));                     //V5
  300.   star[12]=IPoint(psOrigin+psSize.scaledBy(0.95,0.87));                     //V5
  301.  
  302. #ifdef IC_MOTIF                                                             //V5
  303. /**************************************************************************   V5
  304. *****  MOTIF-specific code - BEGIN                                    *****   V5
  305. **************************************************************************/ //V5
  306. /*----------------- Construct X-lib Draw Function Arguments --------------|   V5
  307. | Construct the window and display objects needed by the X-lib drawing    |   V5
  308. |   functions from the window handle, which is the widget ID.             |   V5
  309. |   XtWindow and XtDisplay are X-lib intrinsic functions.                 |   V5
  310. |------------------------------------------------------------------------*/ //V5
  311. const Window                                                                //V5
  312.   xWindow(XtWindow(handle()));                                              //V5
  313. Display                                                                     //V5
  314.  *xDisplay(XtDisplay(handle()));                                            //V5
  315.  
  316. /*--------------------------- Draw the Stars -----------------------------|   V5
  317. | Set the foreground color to white, and then draw each star by calling   |   V5
  318. |   the Xlib function for drawing a point.  The thirteen stars are each   |   V5
  319. |   described as coordinates within the presentation space.               |   V5
  320. |      Note that because the X-Windows origin is at the top of the        |   V5
  321. |      window as opposed to the bottom in PM, the y coordinate of the     |   V5
  322. |      X-Windows point must be computed by subtracting the star's y       |   V5
  323. |      coordinate from the presentation space rectangle's height.         |   V5
  324. |------------------------------------------------------------------------*/ //V5
  325. const long                                                                  //V5
  326.   psHeight=psRect.height();                                                 //V5
  327.   XSetForeground(xDisplay, hps, starColor.asPixel());                       //V5
  328.   for (i=0;i<stars;i++)                                                     //V5
  329.   {                                                                         //V5
  330.     XDrawPoint(xDisplay, xWindow, hps,                                      //V5
  331.                 star[i].x(), psHeight-star[i].y());                         //V5
  332.   }                                                                         //V5
  333.   starsPainted = true;                                                      //V5
  334. /**************************************************************************   V5
  335. *****  MOTIF-specific code - END                                      *****   V5
  336. **************************************************************************/ //V5
  337. #endif                                                                      //V5
  338. #ifdef IC_PM                                                                //V5
  339. /**************************************************************************   V5
  340. *****  PM-specific code - BEGIN                                       *****   V5
  341. **************************************************************************/ //V5
  342. /*----------------------------- Draw the Stars ---------------------------|   V5
  343. | Draw the stars by setting the foreground color to white and calling     |   V5
  344. |   the PM graphics functions for drawing points.                         |   V5
  345. |------------------------------------------------------------------------*/ //V5
  346. POINTL                                                                      //V5
  347.   ptlist[stars];                                                            //V5
  348.   GpiSetMarker(hps, MARKSYM_DOT) ;                                          //V5
  349.   GpiSetColor(hps, starColor.index()) ;                                     //V5
  350.   for (i=0;i<stars;i++)                                                     //V5
  351.   {                                                                         //V5
  352.     ptlist[i]=star[i].asPOINTL();                                           //V5
  353.   }                                                                         //V5
  354.   GpiPolyMarker(hps, stars, &ptlist[0]);                                    //V5
  355.   starsPainted = true;                                                      //V5
  356. /**************************************************************************   V5
  357. *****  PM-specific code - END                                         *****   V5
  358. **************************************************************************/ //V5
  359. #endif                                                                      //V5
  360.   return (starsPainted);                                                    //V5
  361.  
  362. } /* end AEarthWindow :: paintStars(...) */                                 //V5
  363.  
  364.  
  365.