home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
SAMPLES
/
COMPILER
/
SAMPLE1B
/
PMLINES.CPP
< prev
next >
Wrap
Text File
|
1993-05-07
|
15KB
|
399 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.0 |*/
/*| |*/
/*| 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. |*/
/*| |*/
/*+--------------------------------------------------------------------------+*/
/*+--------------------------------------------------------------------------+*/
/*| |*/
/*| 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 thread handling only. |*/
/*+--------------------------------------------------------------------------+*/
#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#define INCL_DOSERRORS
#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 |*/
/*+--------------------------------------------------------------------------+*/
void main()
{
IApplication app();
MyWindow myWin(ID_WINDOW); /* Create the Main Window */
IApplication::current().run(); /* Run the application */
}/* end main */
/*+--------------------------------------------------------------------------+*/
/*| MyWindow::MyWindow nCs 16/12|*/
/*| Constructor for our main window |*/
/*+--------------------------------------------------------------------------+*/
MyWindow::MyWindow( unsigned long windowId )
:IFrameWindow( animated | border | sizingBorder | windowList |
IFrameWindow::accelerator |
classDefaultStyle,
windowId )
{
/* Set ourselves in the event handler stack. */
addHandler((ICommandHandler *)this);
addHandler((IPaintHandler *)this);
addHandler((IMouseClickHandler *)this);
/* Set the main menu */
IMenuBar* pabmnMain=new IMenuBar(ID_WINDOW,this);
/* Instantiate the Client */
pMyClient = new MyClientWindow(ID_CLIENT,this,this);
setClient(pMyClient);
/* Display the application window */
update();
setFocus();
show();
/* Compute the initial Client Area dimensions */
long cx, cy, bx, by, ex, ey;
ISize ClientSize = pMyClient->size();
pMyClient->SetcxClient(cx= ClientSize.width());
pMyClient->SetcyClient(cy= ClientSize.height());
bx= cx / 2;
by= cy / 2;
pMyClient->SetBeginPoint(IPoint(bx,by));
ex= bx / 2;
ey= ex / 2;
pMyClient->SetEndPoint(IPoint(ex,ey));
/* Dispatch the DrawLines function on a separate thread. */
Thread.start(new IThreadMemberFn<MyClientWindow> (*pMyClient,
MyClientWindow::DrawLines));
}/* end MyWindow :: MyWindow(...) */
/*+--------------------------------------------------------------------------+*/
/*| MyWindow :: mouseClicked nCs 16/12|*/
/*| Handle mouse Clicked Event |*/
/*+--------------------------------------------------------------------------+*/
Boolean MyWindow :: mouseClicked(IMouseClickEvent &evt)
{
if (IMouseClickEvent::button1 == evt.mouseNumber() &&
IMouseClickEvent::click == evt.mouseAction())
{
/* Change the foreground color */
setFocus();
pMyClient->IncColorCount();
long Fc= pMyClient->GetFcolor();
Fc++;
pMyClient->SetFcolor(Fc);
if (pMyClient->GetFcolor() == pMyClient->GetBcolor())
{
Fc++;
pMyClient->SetFcolor(Fc);
}
if (pMyClient->GetFcolor() >= CLR_PALEGRAY)
pMyClient->SetFcolor(CLR_BLUE);
pMyClient->SetThreadMessage(WM_USER_REPAINT);
return true;
}
else if (IMouseClickEvent::button1 == evt.mouseNumber() &&
IMouseClickEvent::doubleClick == evt.mouseAction())
{
/* Change the background color */
setFocus();
if (pMyClient->GetBcolor() == CLR_PALEGRAY)
pMyClient->SetBcolor(CLR_BLUE);
else
{
long Bc= pMyClient->GetBcolor();
Bc++;
pMyClient->SetBcolor(Bc);
}
if (pMyClient->GetFcolor() == pMyClient->GetBcolor())
pMyClient->SetFcolor(CLR_BLUE);
pMyClient->SetThreadMessage(WM_USER_REPAINT);
return true;
}
else if (IMouseClickEvent::button2 == evt.mouseNumber() &&
IMouseClickEvent::click == evt.mouseAction())
{
/* Reset the drawing cycle */
pMyClient->SetThreadMessage(WM_USER_REPAINT);
return true;
}
else
return false;
}
/*+--------------------------------------------------------------------------+*/
/*| MyWindow :: paintWindow nCs 16/12|*/
/*| Handle paint Event |*/
/*+--------------------------------------------------------------------------+*/
Boolean MyWindow :: paintWindow(IPaintEvent& evt)
{
IPresSpaceHandle hps= presSpace();
long cx, cy, bx, by, ex, ey;
ISize ClientSize= pMyClient->size();
pMyClient->SetcxClient(cx= ClientSize.width());
pMyClient->SetcyClient(cy= ClientSize.height());
bx= cx / 2;
by= cy / 2;
pMyClient->SetBeginPoint(IPoint(bx,by));
ex= bx / 2;
ey= ex / 2;
pMyClient->SetEndPoint(IPoint(ex,ey));
releasePresSpace(hps);
pMyClient->SetThreadMessage(WM_USER_REPAINT);
return true;
}
/*+--------------------------------------------------------------------------+*/
/*| MyWindow :: command nCs 16/12|*/
/*| Handle command event |*/
/*+--------------------------------------------------------------------------+*/
Boolean MyWindow :: command(ICommandEvent& cmdevt)
{
switch(cmdevt.commandId())
{
case IDM_EXITPROG:
{
pMyClient->SetThreadMessage(WM_USER_END_THREAD);
IThreadId Tid= Thread.id();
DosWaitThread((PTID)&Tid, DCWW_WAIT);
WinPostMsg(handle(), WM_CLOSE, 0L, 0L);
return true;
}
case IDM_RESUME:
return true;
case IDM_HELPINSTRUCTIONS:
{
IMessageBox *Mbox= new IMessageBox(this);
Mbox->setTitle("PMLINES");
Mbox->show(INSTRUCTIONS, IMessageBox::information);
delete Mbox;
return true;
}
case IDM_HELPABOUT:
{
IMessageBox *Mbox= new IMessageBox(this);
Mbox->setTitle("PMLINES");
Mbox->show(ABOUT,IMessageBox::information);
delete Mbox;
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. */
DosSetPriority( PRTYS_THREAD, PRTYC_IDLETIME, 0L, 0UL );
/* Initialize thread to PM. */
habThread2 = WinInitialize( 0UL );
/* Get a presentation space. */
hps= MyParent->presSpace();
/* Run drawing loop */
while ( GetThreadMessage() != WM_USER_END_THREAD )
{
if ( GetThreadMessage() == WM_USER_REPAINT )
{
rclClient= rect().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.*/
MyParent->releasePresSpace( hps );
WinTerminate( habThread2 );
DosExit( EXIT_THREAD, 0UL );
}