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 >
Wrap
C/C++ Source or Header
|
1995-04-07
|
28KB
|
365 lines
/*****************************************************************************
* HELLO WORLD SAMPLE PROGRAM - Version 5: Class Implementation (aearthw5.cpp)*
* *
* COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1995. *
* *
* DISCLAIMER OF WARRANTIES: *
* The following [enclosed] code is sample code created by IBM *
* Corporation. This sample code is not part of any standard IBM product *
* and is provided to you solely for the purpose of assisting you in the *
* development of your applications. The code is provided "AS IS", *
* without warranty of any kind. IBM shall not be liable for any damages *
* arising out of your use of the sample code, even if they have been *
* advised of the possibility of such damages. *
*****************************************************************************/
// NOTE: WE RECOMMEND USING A FIXED-SPACE FONT TO LOOK AT THE SOURCE.
#ifndef _IBASE_ //Make sure ibase.h is included V5
#include <ibase.hpp> // since that is where IC_<environ> V5
#endif // is defined. V5
#ifdef IC_MOTIF //For MOTIF, V5
extern "C" { // include the X-Windows V5
#include <X11/Xlib.h> // libraries for drawing and V5
#include <X11/Intrinsic.h> // XtDisplay and XtWindow intrinsics V5
} //V5
#endif //V5
#ifdef IC_PM //For PM, V5
#define INCL_GPIPRIMITIVES // include the V5
#define INCL_GPIPATHS // drawing routines V5
#include <os2.h> // from the OS/2 libraries V5
#endif //V5
#include "aearthw5.hpp" //v5
/************************************************************************** V5
* AEarthWindow :: AEarthWindow - Constructor for the Earth window * V5
**************************************************************************/ //V5
AEarthWindow :: AEarthWindow(unsigned long windowId, //V5
IWindow * parowWindow, //V5
const IRectangle& rect) //V5
:IStaticText(windowId, parowWindow, parowWindow, rect) //V5
,atmosphereLayers(3) //V5
,spaceColor(IColor::black) //V5
,globeColor(IColor::cyan) //V5
,starColor(IColor::white) //V5
{ //V5
/*-------------------- Construct Paint Handler and Show ------------------| V5
| The paintHandler is attached to the AEarthWindow object to begin | V5
| handling paint events. Each time the static text window needs to | V5
| be repainted, the APaintHandler::paintWindow function will be | V5
| called. The first repaint occurs because of IWindow::show. | V5
|------------------------------------------------------------------------*/ //V5
paintHandler.handleEventsFor(this); //V5
show(); //V5
} /* end AEarthWindow :: AEarthWindow(...) */ //V5
/************************************************************************** V5
* AEarthWindow :: ~AEarthWindow - Destructor for Earth window * V5
**************************************************************************/ //V5
AEarthWindow :: ~AEarthWindow() //V5
{ //V5
/*-------------------- Stop and Delete Paint Handler ---------------------| V5
| Tell paintHandler to stop handling events for AEarthWindow. | V5
|------------------------------------------------------------------------*/ //V5
paintHandler.stopHandlingEventsFor(this); //V5
} /* end AEarthWindow :: ~AEarthWindow() */ //V5
/************************************************************************** V5
* APaintHandler :: paintWindow * V5
* This function calls the paintWorld function for the * V5
* AEarthWindow object which repaints the entire static text window. * V5
* The IParameter2 parameter of the IPaintEvent is always passed to * V5
* paintWorld, but it is only used for the AIX implementation. * V5
**************************************************************************/ //V5
IBase::Boolean //V5
APaintHandler :: paintWindow(IPaintEvent & paintEvent) //V5
{ //V5
/*------------------ Call AEarthWindow Paint World Function --------------| V5
| Get a pointer the AEarthWindow object from the paint event and use it | V5
| to call the paintWorld function. | V5
|------------------------------------------------------------------------*/ //V5
return ( ((AEarthWindow *)paintEvent.window())->paintWorld()); //V5
} /* end APaintHandler :: paintWindow(...) */ //V5
/************************************************************************** V5
* AEarthWindow :: paintWorld - paint a view of Earth from space * v5
* This function provides an example of how to use underlying low-level * V5
* calls in conjuction with the User Interface Class Library. In this * V5
* case, native graphics routines are used to draw a view of Earth from * V5
* space in an IStaticText control. * V5
**************************************************************************/ //V5
IBase::Boolean //V5
AEarthWindow :: paintWorld() //V5
{ //V5
Boolean worldPainted = false;
/*----------------------- Construct Color Objects ------------------------| V5
| Construct an IColor object for each color used in the graphical window. | V5
|------------------------------------------------------------------------*/ //V5
const IColor //V5
arcColor[4] = //V5
{globeColor, IColor::blue, IColor::darkCyan, IColor::darkBlue}; //V5
/*------------------- Get Presentation Space Objects ---------------------| V5
| Get the presentation space handle (called "graphics context" in AIX) | V5
| and the rectangle of the area that needs to be painted. | V5
|------------------------------------------------------------------------*/ //V5
const IPresSpaceHandle //V5
hps(presSpace()); //V5
const IRectangle //V5
psRect(rect()); //V5
/*------------------- Construct Squares for Arc Casings ------------------| V5
| Construct four squares such that the leftCenter, topCenter, and | v5
| rightCenter points describe the arcs to be drawn for the Earth and | V5
| its atmosphere. The squares are constructed from IRectangle objects | V5
| positioned initially at the center of the presentation space | V5
| rectangle and then moved 1/2 the square's width to the left and a | V5
| full square's height plus an arbitrary offset down. | v5
|------------------------------------------------------------------------*/ //V5
const int arcs=4; //V5
const float //V5
arcDropFactor[arcs] = {1.0/8, 1.0/16, 1.0/32, 0}, //V5
arcSizeFactor[arcs] = {9.0/4, 21.0/8, 45.0/16, 3}; //V5
const long //V5
psHeight=psRect.height(); //V5
long //V5
arcDrop, //V5
arcDiameter; //V5
IPair //V5
arcOffset; //V5
IRectangle //V5
arcSquare[arcs]; //V5
int i; //V5
for (i=0;i<arcs;i++ ) //V5
{ //V5
arcDrop = psHeight*arcDropFactor[i]; //V5
arcDiameter = psHeight*arcSizeFactor[i]; //V5
arcOffset = IPair(-1*arcDiameter/2,-1*arcDiameter-arcDrop); //V5
arcSquare[i] = IRectangle(psRect.center(), //V5
ISize(arcDiameter, arcDiameter)); //V5
arcSquare[i].moveBy(arcOffset); //V5
} //V5
#ifdef IC_MOTIF //V5
/************************************************************************** V5
***** MOTIF-specific code - BEGIN ***** V5
**************************************************************************/ //V5
/*----------------- Construct X-lib Draw Function Arguments --------------| V5
| Construct the window and display objects needed by the X-lib drawing | V5
| functions from the window handle, which is the widget ID. | V5
| XtWindow and XtDisplay are X-lib intrinsic functions. | V5
|------------------------------------------------------------------------*/ //V5
const Window //V5
xWindow(XtWindow(handle())); //V5
Display //V5
*xDisplay(XtDisplay(handle())); //V5
/*------------------------------- Color Space ----------------------------| V5
| Set the background color to space, which is IColor::black. | V5
| Note that because the X-Windows origin is at the top of the | V5
| window as opposed to the bottom in PM, the X-Windows rectangle | V5
| top must be computed by subtracting the arc rectangle's top | V5
| coordinate from the presentation space rectangle's height. | V5
|------------------------------------------------------------------------*/ //V5
XSetForeground(xDisplay, hps, spaceColor.asPixel()); //V5
XFillRectangle(xDisplay, xWindow, hps, //V5
psRect.left(), //V5
psRect.height()-psRect.top(), //V5
psRect.width(), //V5
psRect.height()); //V5
/*-------------------- Draw the Earth and Atmosphere ---------------------| V5
| Draw the earth and the number of layers of atmosphere specified in | V5
| atmosphereLayers. arcSquare[0] contains the arc dimension for the | V5
| earth and the other arcSquare objects specify each atmosphere layer. | V5
| Each arc is drawn by calling the Xlib functions for setting the | V5
| foreground color and for drawing an arc and filling it with the | V5
| foreground color. The arc is described using rectangle coordinates. | V5
| Note that because the X-Windows origin is at the top of the | V5
| window as opposed to the bottom in PM, the X-Windows rectangle | V5
| top must be computed by subtracting the arc rectangle's top | V5
| coordinate from the presentation space rectangle's height. | V5
|------------------------------------------------------------------------*/ //V5
for(i=atmosphereLayers;i>=0;i--) //V5
{ //V5
XSetForeground(xDisplay, hps, arcColor[i].asPixel()); //V5
XFillArc(xDisplay, xWindow, hps, //V5
arcSquare[i].left(), //V5
psHeight-arcSquare[i].top(), //V5
arcSquare[i].width(), //V5
arcSquare[i].height(), //V5
0,180*64 ); //V5
} //V5
/************************************************************************** V5
***** MOTIF-specific code - END ***** V5
**************************************************************************/ //V5
#endif //V5
#ifdef IC_PM //V5
/************************************************************************** V5
***** PM-specific code - BEGIN ***** V5
**************************************************************************/ //V5
/*------------------------------- Color Space ----------------------------| V5
| Set the background color to space, which is IColor::black. | V5
|------------------------------------------------------------------------*/ //V5
POINTL //V5
origin=IPoint(0,0).asPOINTL(), //V5
maxPoint=((IPoint)psRect.size()).asPOINTL(); //V5
//V5
GpiMove (hps, &origin); //V5
GpiSetColor (hps, spaceColor.index()) ; //V5
GpiBox (hps, DRO_OUTLINEFILL, &maxPoint, 0, 0) ; //V5
/*-------------------- Draw the Earth and Atmosphere ---------------------| V5
| Draw the earth and the number of layers of atmosphere specified in | V5
| atmosphereLayers. arcSquare[0] contains the arc dimension for the | V5
| earth and the other arcSquare objects specify each atmosphere layer. | V5
| Each arc is drawn by calling the PM Gpi functions for setting the | V5
| foreground color and for drawing an arc and filling it with the | V5
| foreground color. The arc is described using rectangle coordinates. | V5
|------------------------------------------------------------------------*/ //V5
POINTL //V5
ptlist[3]; //V5
for(i=atmosphereLayers;i>=0;i--) //V5
{ //V5
ptlist[0]=arcSquare[i].leftCenter().asPOINTL(); //V5
ptlist[1]=arcSquare[i].topCenter().asPOINTL(); //V5
ptlist[2]=arcSquare[i].rightCenter().asPOINTL(); //V5
GpiSetColor(hps, arcColor[i].index()); //V5
GpiBeginPath(hps, 1L); //V5
GpiMove(hps,&ptlist[0]); //V5
GpiPointArc(hps,&ptlist[1]); //V5
GpiLine(hps,&ptlist[0]); //V5
GpiEndPath(hps); //V5
GpiFillPath(hps,1L,FPATH_ALTERNATE); //V5
} //V5
/************************************************************************** V5
***** PM-specific code - END ***** V5
**************************************************************************/ //V5
#endif //V5
/*--------------------------- Paint the Stars ----------------------------| V5
| Call the AEarthWindow function for painting the stars. | V5
|------------------------------------------------------------------------*/ //V5
worldPainted=paintStars(); //V5
return (worldPainted); //V5
} /* end AEarthWindow :: paintWorld(..) */ //V5
/************************************************************************** V5
* AEarthWindow :: paintStars - paint the stars in the Earth window * v5
* This function extends the example of how to use underlying low-level * V5
* calls in conjuction with the User Interface Class Library. In this * V5
* case, native graphics routines are used to draw points which appear * V5
* as stars in space. * V5
**************************************************************************/ //V5
IBase::Boolean //V5
AEarthWindow :: paintStars() //V5
{ //V5
Boolean starsPainted = false; //V5
/*------------------- Get Presentation Space Objects ---------------------| V5
| Get the presentation space handle (called "graphics context" in AIX) | V5
| and the rectangle of the area that needs to be painted. | V5
|------------------------------------------------------------------------*/ //V5
const IPresSpaceHandle //V5
hps(presSpace()); //V5
const IRectangle //V5
psRect(rect()); //V5
/*------------------- Construct Stars from IPoints -----------------------| V5
| Construct a star array where each star is a point within the | V5
| presentation space rectangle. Each point is computed as a fraction | V5
| of the psRect size offset from the origin of the psRect. The | V5
| starIntensity will indicate if the star is made up of one point(dim), | V5
| made up of five points(bright), or randomly dim or bright(twinkle). | V5
|------------------------------------------------------------------------*/ //V5
const int //V5
stars=13; //V5
const IPair //V5
psOrigin(psRect.bottomLeft()), //V5
psSize(psRect.size()); //V5
int //V5
i, j; //v6
IPoint //V5
star[stars]; //V5
star[0] =IPoint(psOrigin+psSize.scaledBy(0.98,0.43)); //V5
star[1] =IPoint(psOrigin+psSize.scaledBy(0.70,0.69)); //V5
star[2] =IPoint(psOrigin+psSize.scaledBy(0.20,0.50)); //V5
star[3] =IPoint(psOrigin+psSize.scaledBy(0.80,0.63)); //V5
star[4] =IPoint(psOrigin+psSize.scaledBy(0.05,0.41)); //V5
star[5] =IPoint(psOrigin+psSize.scaledBy(0.50,0.69)); //V5
star[6] =IPoint(psOrigin+psSize.scaledBy(0.60,0.94)); //V5
star[7] =IPoint(psOrigin+psSize.scaledBy(0.10,0.87)); //V5
star[8] =IPoint(psOrigin+psSize.scaledBy(0.40,0.81)); //V5
star[9] =IPoint(psOrigin+psSize.scaledBy(0.25,0.69)); //V5
star[10]=IPoint(psOrigin+psSize.scaledBy(0.75,0.63)); //V5
star[11]=IPoint(psOrigin+psSize.scaledBy(0.30,0.87)); //V5
star[12]=IPoint(psOrigin+psSize.scaledBy(0.95,0.87)); //V5
#ifdef IC_MOTIF //V5
/************************************************************************** V5
***** MOTIF-specific code - BEGIN ***** V5
**************************************************************************/ //V5
/*----------------- Construct X-lib Draw Function Arguments --------------| V5
| Construct the window and display objects needed by the X-lib drawing | V5
| functions from the window handle, which is the widget ID. | V5
| XtWindow and XtDisplay are X-lib intrinsic functions. | V5
|------------------------------------------------------------------------*/ //V5
const Window //V5
xWindow(XtWindow(handle())); //V5
Display //V5
*xDisplay(XtDisplay(handle())); //V5
/*--------------------------- Draw the Stars -----------------------------| V5
| Set the foreground color to white, and then draw each star by calling | V5
| the Xlib function for drawing a point. The thirteen stars are each | V5
| described as coordinates within the presentation space. | V5
| Note that because the X-Windows origin is at the top of the | V5
| window as opposed to the bottom in PM, the y coordinate of the | V5
| X-Windows point must be computed by subtracting the star's y | V5
| coordinate from the presentation space rectangle's height. | V5
|------------------------------------------------------------------------*/ //V5
const long //V5
psHeight=psRect.height(); //V5
XSetForeground(xDisplay, hps, starColor.asPixel()); //V5
for (i=0;i<stars;i++) //V5
{ //V5
XDrawPoint(xDisplay, xWindow, hps, //V5
star[i].x(), psHeight-star[i].y()); //V5
} //V5
starsPainted = true; //V5
/************************************************************************** V5
***** MOTIF-specific code - END ***** V5
**************************************************************************/ //V5
#endif //V5
#ifdef IC_PM //V5
/************************************************************************** V5
***** PM-specific code - BEGIN ***** V5
**************************************************************************/ //V5
/*----------------------------- Draw the Stars ---------------------------| V5
| Draw the stars by setting the foreground color to white and calling | V5
| the PM graphics functions for drawing points. | V5
|------------------------------------------------------------------------*/ //V5
POINTL //V5
ptlist[stars]; //V5
GpiSetMarker(hps, MARKSYM_DOT) ; //V5
GpiSetColor(hps, starColor.index()) ; //V5
for (i=0;i<stars;i++) //V5
{ //V5
ptlist[i]=star[i].asPOINTL(); //V5
} //V5
GpiPolyMarker(hps, stars, &ptlist[0]); //V5
starsPainted = true; //V5
/************************************************************************** V5
***** PM-specific code - END ***** V5
**************************************************************************/ //V5
#endif //V5
return (starsPainted); //V5
} /* end AEarthWindow :: paintStars(...) */ //V5