home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
VSCPPv8.zip
/
VACPP
/
IBMCPP
/
samples
/
COMPILER
/
UILINES
/
PMLINES.CPP
< prev
next >
Wrap
Text File
|
1995-06-01
|
15KB
|
381 lines
/*+--------------------------------------------------------------------------+*/
/*| |*/
/*| PROGRAM NAME: PMLINES |*/
/*| ------------- |*/
/*| A Simple OS/2 Presentation Manager Graphics Demonstration Program |*/
/*| |*/
/*| COPYRIGHT: |*/
/*| ---------- |*/
/*| Copyright (C) International Business Machines Corp., 1992, 1993. |*/
/*| |*/
/*| 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. |*/
/*| |*/
/*| REVISION LEVEL: 1.1 |*/
/*| |*/
/*| CHANGE HISTORY: |*/
/*| Rel Programmer Stamp Date Description |*/
/*| --- ----------------- ----- -------- ------------------------------------|*/
/*| 1.0 Noel Sales njCs 12/16/92 Port the PMLINES program to code it |*/
/*| in C++ and to implement the PM |*/
/*| interface using the User Interface |*/
/*| Class Library. The original PMLINES |*/
/*| program is coded in C and uses the |*/
/*| PM API directly. |*/
/*| 1.1 Bill Sarantakos bjs 09/05/95 Minor fixes |*/
/*| |*/
/*+--------------------------------------------------------------------------+*/
/*+--------------------------------------------------------------------------+*/
/*| |*/
/*| The following features of the User Interface Class Library are |*/
/*| demonstrated by this program: |*/
/*| 1. Window Creation. |*/
/*| 2. Event Handling |*/
/*| 3. Simple multitasking |*/
/*| 4. Use of GPI or native PM API with User Interface Library objects |*/
/*| |*/
/*+--------------------------------------------------------------------------+*/
/*+--------------------------------------------------------------------------+*/
/*| The following includes are for PM functions and macros |*/
/*+--------------------------------------------------------------------------+*/
#define INCL_NOCOMMON
#define INCL_GPIPRIMITIVES
#define INCL_WINMESSAGEMGR
#define INCL_WINWINDOWMGR
#include <os2.h>
/*+--------------------------------------------------------------------------+*/
/*| Include headers for the User Interface Class Library |*/
/*+--------------------------------------------------------------------------+*/
#include <iapp.hpp> // IApplication
#include <ireslib.hpp> // IResourceLibrary/IResourceId
#include <itrace.hpp> // ITrace
#include <imsgbox.hpp>
/*+--------------------------------------------------------------------------+*/
/*| include our class declarations |*/
/*+--------------------------------------------------------------------------+*/
#include "pmlines.hpp"
/*+--------------------------------------------------------------------------+*/
/*| include our symbols definitions |*/
/*+--------------------------------------------------------------------------+*/
#include "pmlines.h"
/*+--------------------------------------------------------------------------+*/
/*| main |*/
/*| Application entry point |*/
/*+--------------------------------------------------------------------------+*/
int main()
{
MyWindow myWin(ID_WINDOW); /* Create the Main Window */
/* Display the application window */
myWin.setFocus().show();
IApplication::current().run(); /* Run the application */
return 0;
}
/*+--------------------------------------------------------------------------+*/
/*| MyWindow::MyWindow nCs 16/12|*/
/*| Constructor for our main window |*/
/*+--------------------------------------------------------------------------+*/
MyWindow::MyWindow( unsigned long windowId )
:IFrameWindow( classDefaultStyle | accelerator,
windowId ),
menubar( windowId, this ),
myClient( ID_CLIENT, this, this )
{
/* Set ourselves in the event handler stack. */
ICommandHandler::handleEventsFor( this );
IPaintHandler::handleEventsFor( &myClient );
IMouseClickHandler::handleEventsFor( &myClient );
IFrameHandler::handleEventsFor( this );
/* Set the client to the frame window */
setClient( &myClient );
/* Compute the initial Client Area dimensions */
long cx, cy, bx, by, ex, ey;
ISize ClientSize = myClient.size();
myClient.SetcxClient( cx = ClientSize.width() );
myClient.SetcyClient( cy = ClientSize.height() );
bx = cx / 2;
by = cy / 2;
myClient.SetBeginPoint( IPoint( bx, by ) );
ex = bx / 2;
ey = by / 2;
myClient.SetEndPoint( IPoint( ex, ey ) );
/* Dispatch the DrawLines function on a separate thread. */
/* tell library to initialize thread when created. */
thread.start( new IThreadMemberFn<MyClientWindow>( myClient,
MyClientWindow::DrawLines), true );
}
IBase::Boolean MyWindow::closed( IFrameEvent &event )
{
myClient.SetThreadMessage( WM_USER_END_THREAD );
IThread::current().waitFor( thread );
return false;
}
/*+--------------------------------------------------------------------------+*/
/*| MyWindow :: mouseClicked nCs 16/12|*/
/*| Handle mouse Clicked Event |*/
/*+--------------------------------------------------------------------------+*/
IBase::Boolean MyWindow::mouseClicked( IMouseClickEvent &event )
{
if ( IMouseClickEvent::button1 == event.mouseNumber() &&
IMouseClickEvent::click == event.mouseAction() )
{
/* Change the foreground color */
setFocus();
myClient.IncColorCount();
long Fc = myClient.GetFcolor();
Fc++;
myClient.SetFcolor( Fc );
if ( myClient.GetFcolor() == myClient.GetBcolor() )
{
Fc++;
myClient.SetFcolor( Fc );
}
if ( myClient.GetFcolor() >= CLR_PALEGRAY )
myClient.SetFcolor( CLR_BLUE );
myClient.SetThreadMessage( WM_USER_REPAINT );
return true;
}
else if ( IMouseClickEvent::button1 == event.mouseNumber() &&
IMouseClickEvent::doubleClick == event.mouseAction() )
{
/* Change the background color */
setFocus();
if ( myClient.GetBcolor() == CLR_PALEGRAY )
myClient.SetBcolor( CLR_BLUE );
else
{
long Bc = myClient.GetBcolor();
Bc++;
myClient.SetBcolor(Bc);
}
if ( myClient.GetFcolor() == myClient.GetBcolor() )
myClient.SetFcolor( CLR_BLUE );
myClient.SetThreadMessage(WM_USER_REPAINT);
return true;
}
else if ( IMouseClickEvent::button2 == event.mouseNumber() &&
IMouseClickEvent::click == event.mouseAction() )
{
/* Reset the drawing cycle */
myClient.SetThreadMessage( WM_USER_REPAINT );
return true;
}
else
return false;
}
/*+--------------------------------------------------------------------------+*/
/*| MyWindow :: paintWindow nCs 16/12|*/
/*| Handle paint Event |*/
/*+--------------------------------------------------------------------------+*/
IBase::Boolean MyWindow::paintWindow( IPaintEvent& event )
{
IPresSpaceHandle
hps = event.presSpaceHandle();
long
cx, cy, bx, by, ex, ey;
ISize
ClientSize = myClient.size();
myClient.SetcxClient( cx = ClientSize.width() );
myClient.SetcyClient( cy = ClientSize.height() );
bx = cx / 2;
by = cy / 2;
myClient.SetBeginPoint( IPoint( bx,by ) );
ex = bx / 2;
ey = by / 2;
myClient.SetEndPoint( IPoint( ex, ey ) );
myClient.SetThreadMessage( WM_USER_REPAINT );
return false;
}
/*+--------------------------------------------------------------------------+*/
/*| MyWindow :: command nCs 16/12|*/
/*| Handle command event |*/
/*+--------------------------------------------------------------------------+*/
IBase::Boolean MyWindow::command( ICommandEvent& cmdevt )
{
switch( cmdevt.commandId() )
{
case IDM_EXITPROG:
{
myClient.SetThreadMessage( WM_USER_END_THREAD );
IThread::current().waitFor( thread );
postEvent( WM_CLOSE, 0, 0 );
return true;
}
case IDM_RESUME:
return true;
case IDM_HELPINSTRUCTIONS:
{
IMessageBox
Mbox( this );
Mbox.setTitle( "UILINES" );
Mbox.show( INSTRUCTIONS, IMessageBox::information );
return true;
}
case IDM_HELPABOUT:
{
IMessageBox
Mbox( this );
Mbox.setTitle( "UILINES" );
Mbox.show( ABOUT, IMessageBox::information );
return true;
}
}
return false;
}
/*+--------------------------------------------------------------------------+*/
/*| Define MyClientWindow Methods |*/
/*+--------------------------------------------------------------------------+*/
MyClientWindow::MyClientWindow(unsigned long Id,
IWindow* parent,
IWindow* owner)
: ICanvas(Id, parent, owner),
MyParent(parent),
lFcolour(CLR_BLUE),
lBcolour(CLR_DARKGRAY),
cxClient(0),cyClient(0),
fcolourcounter(0l)
{
ptl1.x = ptl1.y = 0;
ptl2.x = ptl2.y = 0;
SetThreadMessage(WM_USER_REPAINT);
}
/*+--------------------------------------------------------------------------+*/
/*| DrawLines |*/
/*+--------------------------------------------------------------------------+*/
void MyClientWindow::DrawLines( )
{
/* Declare local variables. */
LONG delta1x = START_DELTA_X; /* start point delta values */
LONG delta1y = START_DELTA_Y;
LONG delta2x = END_DELTA_X; /* end point delta values */
LONG delta2y = END_DELTA_Y;
/* Change the priority class of this thread to idle class. */
IThread::current().setPriority( IApplication::idleTime, 0 );
/* Get a presentation space. */
hps = presSpace();
/* Run drawing loop */
while ( GetThreadMessage() != WM_USER_END_THREAD )
{
if ( GetThreadMessage() == WM_USER_REPAINT )
{
rclClient = IRectangle(IPoint(0,0), size()).asRECTL();
WinFillRect( hps, &rclClient, GetBcolor() );
SetThreadMessage(WM_USER_REPAINT+1);
}
else
{
/* Increment the foreground colour if necessary. */
IncColorCount();
long ColorCount = GetColorCount();
if ( ColorCount > ((cxClient + cyClient ) >> 1 ))
{
SetColorCount(1); /* Reset fcolourcounter and */
long Fc = GetFcolor(); /* increment foreground color*/
Fc++;
SetFcolor(Fc);
/* Foreground and background colors must differ */
if (GetFcolor() == GetBcolor())
{
Fc++;
SetFcolor(Fc);
}
/* Make sure the foregound colour does not exceed CLR_PALEGRAY. */
if (GetFcolor() >= CLR_PALEGRAY )
SetFcolor(CLR_BLUE);
}
/* Add deltas to the start and end points for a line. */
/* If the start point would be invalid, negate the delta.*/
if ( ( ptl1.x + delta1x ) > cxClient )
delta1x = -delta1x;
if ( ( ptl1.x + delta1x ) < 1 )
delta1x = -delta1x;
if ( ( ptl1.y + delta1y ) > cyClient )
delta1y = -delta1y;
if ( ( ptl1.y + delta1y ) < 1 )
delta1y = -delta1y;
/* Add delta to start point. */
ptl1.x += delta1x;
ptl1.y += delta1y;
/* If the end point would be invalid, negate the delta. */
if ( ( ptl2.x + delta2x ) > cxClient )
delta2x = -delta2x;
if ( ( ptl2.x + delta2x ) < 1 )
delta2x = -delta2x;
if ( ( ptl2.y + delta2y ) > cyClient )
delta2y = -delta2y;
if ( ( ptl2.y + delta2y ) < 1 )
delta2y = -delta2y;
/* Add delta to end point.*/
ptl2.x += delta2x;
ptl2.y += delta2y;
/* Now draw the line.*/
GpiSetColor( hps, GetFcolor());
GpiMove( hps, &ptl1 ); /* Move to start point */
GpiLine( hps, &ptl2 ); /* Draw new line */
}
}
/* Clean up and terminate drawing thread.*/
releasePresSpace( hps );
}