home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
VSCPPv8.zip
/
VACPP
/
IBMCPP
/
samples
/
IOC
/
2D-BMAP
/
2D-BMAP.CPP
next >
Wrap
Text File
|
1995-05-01
|
13KB
|
355 lines
/******************************************************************************/
/* 2D-GRAPHICS BITMAP SAMPLE PROGRAM - Main Program (2d-bmap.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. */
/******************************************************************************/
#ifndef _IBASE_ //Make sure ibase.hpp is included
#include <ibase.hpp> // since that is where IC_<environ>
#endif // is defined.
#include "2d-bmap.hpp"
#include <ifiledlg.hpp>
#include <ihandle.hpp>
#include <igelipse.hpp>
#include <igrect.hpp>
#include <igpie.hpp>
#include <igrafctx.hpp>
#include <igregion.hpp>
#include <imenubar.hpp>
//*************************************************************************
// main - Application entry point *
//*************************************************************************
int main() //Main procedure with no parameters
{
MainWindow mainWindow(WND_MAIN); //Create our main window on the desktop
IApplication::current().run(); //Get the current application and
// run it
return 0;
} /* end main */
/*------------------------------------------------------------------------------
| MainWindow::MainWindow |
| |
| |
------------------------------------------------------------------------------*/
MainWindow::MainWindow(unsigned long windowId)
: MainWindow::Inherited( // Call Inherited constructor
IFrameWindow::defaultStyle() // Use default plus
| IFrameWindow::animated // Set to show with "animation"
| IFrameWindow::menuBar // Frame has a menu bar
| IFrameWindow::minimizedIcon, // Frame has an icon
windowId), // Main Window ID
fViewPort( WND_VPORT, this, this ),
fDrawingArea( WND_DRAW, &fViewPort, &fViewPort ),
fMainCommandHandler()
{
fMainCommandHandler.handleEventsFor(this);
setClient( &fViewPort );
IMenuBar menuBar(windowId, this);
menuBar.setAutoDestroyWindow(false);
menuBar.disableItem( IDM_REFLECTHORZ );
menuBar.disableItem( IDM_REFLECTVERT );
menuBar.disableItem( IDM_ROTATE90 );
menuBar.disableItem( IDM_ROTATE180 );
menuBar.disableItem( IDM_ROTATE270 );
menuBar.disableItem( IDM_TRANSPOSE );
menuBar.disableItem( IDM_CLIPCIRCLES );
menuBar.disableItem( IDM_CLIPSQUARES );
menuBar.disableItem( IDM_CLIPRAD );
menuBar.disableItem( IDM_CLIPNONE );
setFocus();
show();
}
/*------------------------------------------------------------------------------
| MainWindow::MainWindow |
| |
| |
------------------------------------------------------------------------------*/
MainWindow::~MainWindow()
{
fMainCommandHandler.stopHandlingEventsFor(this);
}
MainWindow& MainWindow::modifyBitmap( unsigned long eventId )
{
IGBitmap* bitmap(fDrawingArea.bitmap());
if (bitmap)
{
switch (eventId)
{
case IDM_REFLECTHORZ:
bitmap->reflectHorizontally();
break;
case IDM_REFLECTVERT:
bitmap->reflectVertically();
break;
case IDM_ROTATE90:
bitmap->rotateBy90();
break;
case IDM_ROTATE180:
bitmap->rotateBy180();
break;
case IDM_ROTATE270:
bitmap->rotateBy270();
break;
case IDM_TRANSPOSE:
bitmap->transposeXForY();
break;
} /* endswitch */
ISize bitmapSize(bitmap->size());
fDrawingArea.sizeTo(bitmapSize);
fViewPort.setViewWindowSize(bitmapSize);
fDrawingArea.refresh();
}
return *this;
}
/*------------------------------------------------------------------------------
| MainCommandHandler::command |
| |
| |
------------------------------------------------------------------------------*/
IBase::Boolean MainCommandHandler::command( ICommandEvent& event )
{
Boolean fProcessed = false;
switch (event.commandId())
{
case IDM_FILEOPEN:
{
MainWindow *mainWindow((MainWindow*)event.window());
IFileDialog::Settings fsettings; // .
fsettings.setTitle("Load an image file");//Set Open Dialog Title from Resource .
fsettings.setFileName("*.bmp"); //Set FileNames to *.hlo .
IFileDialog fd( //Create File Open Dialiog .
IWindow::desktopWindow(), // Parent is Desktop .
mainWindow, fsettings); // Owner is me with settings .
if (fd.pressedOK()) //Check if ok from file open dialog .
{
mainWindow->loadImageFile(fd.fileName());
IMenuBar menuBar(mainWindow->id(), mainWindow);
menuBar.setAutoDestroyWindow(false);
menuBar.enableItem( IDM_REFLECTHORZ );
menuBar.enableItem( IDM_REFLECTVERT );
menuBar.enableItem( IDM_ROTATE90 );
menuBar.enableItem( IDM_ROTATE180 );
menuBar.enableItem( IDM_ROTATE270 );
menuBar.enableItem( IDM_TRANSPOSE );
menuBar.enableItem( IDM_CLIPCIRCLES );
menuBar.enableItem( IDM_CLIPSQUARES );
menuBar.enableItem( IDM_CLIPRAD );
menuBar.enableItem( IDM_CLIPNONE );
}
}
break;
case IDM_QUIT:
{
IFrameWindow *frameWindow((IFrameWindow*)event.window());
frameWindow->close();
}
break;
case IDM_REFLECTHORZ:
case IDM_REFLECTVERT:
case IDM_ROTATE90:
case IDM_ROTATE180:
case IDM_ROTATE270:
case IDM_TRANSPOSE:
{
MainWindow *mainWindow((MainWindow*)event.window());
mainWindow->modifyBitmap( event.commandId() );
}
break;
case IDM_CLIPCIRCLES:
case IDM_CLIPSQUARES:
case IDM_CLIPRAD:
case IDM_CLIPNONE:
{
MainWindow *mainWindow((MainWindow*)event.window());
mainWindow->drawingArea().setClipStyle( event.commandId() );
mainWindow->drawingArea().refresh();
}
break;
} /* endswitch */
return fProcessed;
}
/*------------------------------------------------------------------------------
| MainWindow::loadImageFile |
| |
| |
------------------------------------------------------------------------------*/
MainWindow& MainWindow::loadImageFile( const IString& imageFile )
{
fDrawingArea.loadBitmap( imageFile );
fViewPort.setViewWindowSize( fDrawingArea.bitmap()->size());
return *this;
}
DrawingArea::DrawingArea( unsigned long id, IWindow* parent, IWindow* owner )
: DrawingArea::Inherited( id, parent, owner, IRectangle(),
IDrawingCanvas::defaultStyle() & ~IDrawingCanvas::useDefaultPaintHandler ),
fBitmap(0),
fStyle(IDM_CLIPNONE)
{
drawingAreaPaintHandler.handleEventsFor(this);
}
DrawingArea::~DrawingArea()
{
drawingAreaPaintHandler.stopHandlingEventsFor(this);
if (fBitmap)
delete fBitmap;
}
IGBitmap* DrawingArea::bitmap( ) const
{
return fBitmap;
}
DrawingArea& DrawingArea::loadBitmap( const IString& imageFile )
{
if (fBitmap)
delete fBitmap;
fBitmap = new IGBitmap( imageFile );
sizeTo(fBitmap->size());
refresh();
return *this;
}
ISize DrawingArea::calcMinimumSize( ) const
{
if (fBitmap)
return fBitmap->size();
else
return ISize();
}
/*------------------------------------------------------------------------------
| DrawingAreaPaintHandler::paintWindow |
| |
| |
------------------------------------------------------------------------------*/
IBase::Boolean DrawingAreaPaintHandler::paintWindow( IPaintEvent& event )
{
// Get a graphic context
IGraphicContext gc(event.presSpaceHandle());
// Get a pointer to the current bitmap if one exists.
IGBitmap* bitmap(((DrawingArea*)event.window())->bitmap());
// Get the dimensions of the window
IRectangle windowRect(IPoint(),((DrawingArea*)event.window())->size());
// paint the current background color
event.clearBackground();
// Query the current clipping style
unsigned long clipStyle(((DrawingArea*)event.window())->clipStyle());
IGRegion region;
IGRegion oldClipRegion( gc.clipRegion() );
// Clear the current clip region so that the oldClipRegion can
// be used in region operations.
gc.clearClipRegion();
switch (clipStyle)
{
case IDM_CLIPCIRCLES:
{
IGRectangle rect( windowRect );
region -= rect;
IGEllipse ellipse( windowRect );
region += ellipse;
windowRect.shrinkBy( 25 );
ellipse.setEnclosingRect( windowRect );
region -= ellipse;
windowRect.shrinkBy( 25 );
ellipse.setEnclosingRect( windowRect );
region += ellipse;
windowRect.shrinkBy( 25 );
ellipse.setEnclosingRect( windowRect );
region -= ellipse;
windowRect.shrinkBy( 25 );
ellipse.setEnclosingRect( windowRect );
region += ellipse;
windowRect.shrinkBy( 25 );
ellipse.setEnclosingRect( windowRect );
region -= ellipse;
windowRect.shrinkBy( 25 );
ellipse.setEnclosingRect( windowRect );
region += ellipse;
region &= oldClipRegion;
}
break;
case IDM_CLIPSQUARES:
{
IGRectangle rect( windowRect );
region += rect;
windowRect.shrinkBy( 25 );
rect.setEnclosingRect( windowRect );
region -= rect;
windowRect.shrinkBy( 25 );
rect.setEnclosingRect( windowRect );
region += rect;
windowRect.shrinkBy( 25 );
rect.setEnclosingRect( windowRect );
region -= rect;
windowRect.shrinkBy( 25 );
rect.setEnclosingRect( windowRect );
region += rect;
windowRect.shrinkBy( 25 );
rect.setEnclosingRect( windowRect );
region -= rect;
windowRect.shrinkBy( 25 );
rect.setEnclosingRect( windowRect );
region += rect;
region &= oldClipRegion;
break;
}
case IDM_CLIPRAD:
{
IGPie pie( windowRect, 0, 60 );
region += pie;
pie.setStartAngle( 120 );
region += pie;
pie.setStartAngle( 240 );
region += pie;
region &= oldClipRegion;
break;
}
case IDM_CLIPNONE:
region = oldClipRegion;
break;
} /* endswitch */
// set the clip region
gc.setClipRegion( region );
// draw the bitmap if we have one
if (bitmap)
{
bitmap->drawOn(gc);
}
// clear the current clip region
gc.clearClipRegion( );
return true;
}