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 >
Text File  |  1995-05-01  |  13KB  |  355 lines

  1. /******************************************************************************/
  2. /* 2D-GRAPHICS BITMAP SAMPLE PROGRAM - Main Program (2d-bmap.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.  
  16. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  17.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  18. #endif                                  //  is defined.
  19. #include "2d-bmap.hpp"
  20. #include <ifiledlg.hpp>
  21. #include <ihandle.hpp>
  22. #include <igelipse.hpp>
  23. #include <igrect.hpp>
  24. #include <igpie.hpp>
  25. #include <igrafctx.hpp>
  26. #include <igregion.hpp>
  27. #include <imenubar.hpp>
  28.  
  29. //*************************************************************************
  30. // main  - Application entry point                                        *
  31. //*************************************************************************
  32. int main()                              //Main procedure with no parameters
  33. {
  34.   MainWindow  mainWindow(WND_MAIN);     //Create our main window on the desktop
  35.   IApplication::current().run();        //Get the current application and
  36.                                         // run it
  37.   return 0;
  38. } /* end main */
  39.  
  40.  
  41. /*------------------------------------------------------------------------------
  42. | MainWindow::MainWindow                                                       |
  43. |                                                                              |
  44. |                                                                              |
  45. ------------------------------------------------------------------------------*/
  46. MainWindow::MainWindow(unsigned long windowId)
  47.   : MainWindow::Inherited(              //  Call Inherited constructor
  48.     IFrameWindow::defaultStyle()        //  Use default plus
  49.     | IFrameWindow::animated            //  Set to show with "animation"
  50.     | IFrameWindow::menuBar             //  Frame has a menu bar
  51.     | IFrameWindow::minimizedIcon,      //  Frame has an icon
  52.     windowId),                          //  Main Window ID
  53.     fViewPort( WND_VPORT, this, this ),
  54.     fDrawingArea( WND_DRAW, &fViewPort, &fViewPort ),
  55.     fMainCommandHandler()
  56. {
  57.   fMainCommandHandler.handleEventsFor(this);
  58.   setClient( &fViewPort );
  59.   IMenuBar menuBar(windowId, this);
  60.   menuBar.setAutoDestroyWindow(false);
  61.   menuBar.disableItem( IDM_REFLECTHORZ );
  62.   menuBar.disableItem( IDM_REFLECTVERT );
  63.   menuBar.disableItem( IDM_ROTATE90    );
  64.   menuBar.disableItem( IDM_ROTATE180   );
  65.   menuBar.disableItem( IDM_ROTATE270   );
  66.   menuBar.disableItem( IDM_TRANSPOSE   );
  67.   menuBar.disableItem( IDM_CLIPCIRCLES );
  68.   menuBar.disableItem( IDM_CLIPSQUARES );
  69.   menuBar.disableItem( IDM_CLIPRAD     );
  70.   menuBar.disableItem( IDM_CLIPNONE    );
  71.   setFocus();
  72.   show();
  73. }
  74.  
  75. /*------------------------------------------------------------------------------
  76. | MainWindow::MainWindow                                                       |
  77. |                                                                              |
  78. |                                                                              |
  79. ------------------------------------------------------------------------------*/
  80. MainWindow::~MainWindow()
  81. {
  82.   fMainCommandHandler.stopHandlingEventsFor(this);
  83. }
  84.  
  85. MainWindow& MainWindow::modifyBitmap( unsigned long eventId )
  86. {
  87.   IGBitmap* bitmap(fDrawingArea.bitmap());
  88.   if (bitmap)
  89.   {
  90.     switch (eventId)
  91.     {
  92.       case  IDM_REFLECTHORZ:
  93.         bitmap->reflectHorizontally();
  94.       break;
  95.       case  IDM_REFLECTVERT:
  96.         bitmap->reflectVertically();
  97.       break;
  98.       case  IDM_ROTATE90:
  99.         bitmap->rotateBy90();
  100.       break;
  101.       case  IDM_ROTATE180:
  102.         bitmap->rotateBy180();
  103.       break;
  104.       case  IDM_ROTATE270:
  105.         bitmap->rotateBy270();
  106.       break;
  107.       case  IDM_TRANSPOSE:
  108.         bitmap->transposeXForY();
  109.       break;
  110.     } /* endswitch */
  111.     ISize bitmapSize(bitmap->size());
  112.     fDrawingArea.sizeTo(bitmapSize);
  113.     fViewPort.setViewWindowSize(bitmapSize);
  114.     fDrawingArea.refresh();
  115.   }
  116.   return *this;
  117. }
  118.  
  119. /*------------------------------------------------------------------------------
  120. | MainCommandHandler::command                                                  |
  121. |                                                                              |
  122. |                                                                              |
  123. ------------------------------------------------------------------------------*/
  124. IBase::Boolean MainCommandHandler::command( ICommandEvent& event )
  125. {
  126.   Boolean fProcessed = false;
  127.   switch (event.commandId())
  128.   {
  129.     case  IDM_FILEOPEN:
  130.     {
  131.       MainWindow *mainWindow((MainWindow*)event.window());
  132.  
  133.       IFileDialog::Settings fsettings;      //                                     .
  134.       fsettings.setTitle("Load an image file");//Set Open Dialog Title from Resource  .
  135.       fsettings.setFileName("*.bmp");          //Set FileNames to *.hlo               .
  136.  
  137.       IFileDialog fd(                          //Create File Open Dialiog             .
  138.         IWindow::desktopWindow(),              //  Parent is Desktop                  .
  139.         mainWindow, fsettings);                //  Owner is me with settings          .
  140.       if (fd.pressedOK())                      //Check if ok from file open dialog    .
  141.       {
  142.         mainWindow->loadImageFile(fd.fileName());
  143.         IMenuBar menuBar(mainWindow->id(), mainWindow);
  144.         menuBar.setAutoDestroyWindow(false);
  145.         menuBar.enableItem( IDM_REFLECTHORZ );
  146.         menuBar.enableItem( IDM_REFLECTVERT );
  147.         menuBar.enableItem( IDM_ROTATE90    );
  148.         menuBar.enableItem( IDM_ROTATE180   );
  149.         menuBar.enableItem( IDM_ROTATE270   );
  150.         menuBar.enableItem( IDM_TRANSPOSE   );
  151.         menuBar.enableItem( IDM_CLIPCIRCLES );
  152.         menuBar.enableItem( IDM_CLIPSQUARES );
  153.         menuBar.enableItem( IDM_CLIPRAD     );
  154.         menuBar.enableItem( IDM_CLIPNONE    );
  155.       }
  156.     }
  157.     break;
  158.     case  IDM_QUIT:
  159.     {
  160.       IFrameWindow *frameWindow((IFrameWindow*)event.window());
  161.       frameWindow->close();
  162.     }
  163.     break;
  164.     case  IDM_REFLECTHORZ:
  165.     case  IDM_REFLECTVERT:
  166.     case  IDM_ROTATE90:
  167.     case  IDM_ROTATE180:
  168.     case  IDM_ROTATE270:
  169.     case  IDM_TRANSPOSE:
  170.     {
  171.       MainWindow *mainWindow((MainWindow*)event.window());
  172.       mainWindow->modifyBitmap( event.commandId() );
  173.     }
  174.     break;
  175.  
  176.     case  IDM_CLIPCIRCLES:
  177.     case  IDM_CLIPSQUARES:
  178.     case  IDM_CLIPRAD:
  179.     case  IDM_CLIPNONE:
  180.     {
  181.       MainWindow *mainWindow((MainWindow*)event.window());
  182.       mainWindow->drawingArea().setClipStyle( event.commandId() );
  183.       mainWindow->drawingArea().refresh();
  184.     }
  185.     break;
  186.   } /* endswitch */
  187.  
  188.   return fProcessed;
  189. }
  190.  
  191. /*------------------------------------------------------------------------------
  192. | MainWindow::loadImageFile                                                    |
  193. |                                                                              |
  194. |                                                                              |
  195. ------------------------------------------------------------------------------*/
  196. MainWindow& MainWindow::loadImageFile( const IString& imageFile )
  197. {
  198.   fDrawingArea.loadBitmap( imageFile );
  199.   fViewPort.setViewWindowSize( fDrawingArea.bitmap()->size());
  200.   return *this;
  201. }
  202.  
  203. DrawingArea::DrawingArea( unsigned long id, IWindow* parent, IWindow* owner )
  204.   : DrawingArea::Inherited( id, parent, owner, IRectangle(),
  205.                             IDrawingCanvas::defaultStyle() & ~IDrawingCanvas::useDefaultPaintHandler ),
  206.     fBitmap(0),
  207.     fStyle(IDM_CLIPNONE)
  208. {
  209.   drawingAreaPaintHandler.handleEventsFor(this);
  210. }
  211.  
  212. DrawingArea::~DrawingArea()
  213. {
  214.   drawingAreaPaintHandler.stopHandlingEventsFor(this);
  215.   if (fBitmap)
  216.     delete fBitmap;
  217. }
  218.  
  219. IGBitmap* DrawingArea::bitmap( ) const
  220. {
  221.   return fBitmap;
  222. }
  223.  
  224. DrawingArea& DrawingArea::loadBitmap( const IString& imageFile )
  225. {
  226.   if (fBitmap)
  227.     delete fBitmap;
  228.   fBitmap = new IGBitmap( imageFile );
  229.   sizeTo(fBitmap->size());
  230.   refresh();
  231.   return *this;
  232. }
  233.  
  234. ISize DrawingArea::calcMinimumSize( ) const
  235. {
  236.   if (fBitmap)
  237.     return fBitmap->size();
  238.   else
  239.     return ISize();
  240. }
  241.  
  242. /*------------------------------------------------------------------------------
  243. | DrawingAreaPaintHandler::paintWindow                                         |
  244. |                                                                              |
  245. |                                                                              |
  246. ------------------------------------------------------------------------------*/
  247. IBase::Boolean DrawingAreaPaintHandler::paintWindow( IPaintEvent& event )
  248. {
  249.   // Get a graphic context
  250.   IGraphicContext gc(event.presSpaceHandle());
  251.  
  252.   // Get a pointer to the current bitmap if one exists.
  253.   IGBitmap* bitmap(((DrawingArea*)event.window())->bitmap());
  254.  
  255.   // Get the dimensions of the window
  256.   IRectangle windowRect(IPoint(),((DrawingArea*)event.window())->size());
  257.  
  258.   // paint the current background color
  259.   event.clearBackground();
  260.  
  261.   // Query the current clipping style
  262.   unsigned long clipStyle(((DrawingArea*)event.window())->clipStyle());
  263.  
  264.   IGRegion region;
  265.   IGRegion oldClipRegion( gc.clipRegion() );
  266.  
  267.   // Clear the current clip region so that the oldClipRegion can
  268.   // be used in region operations.
  269.   gc.clearClipRegion();
  270.  
  271.   switch (clipStyle)
  272.   {
  273.     case IDM_CLIPCIRCLES:
  274.     {
  275.       IGRectangle rect( windowRect );
  276.       region -= rect;
  277.       IGEllipse ellipse( windowRect );
  278.       region += ellipse;
  279.       windowRect.shrinkBy( 25 );
  280.       ellipse.setEnclosingRect( windowRect );
  281.       region -= ellipse;
  282.       windowRect.shrinkBy( 25 );
  283.       ellipse.setEnclosingRect( windowRect );
  284.       region += ellipse;
  285.       windowRect.shrinkBy( 25 );
  286.       ellipse.setEnclosingRect( windowRect );
  287.       region -= ellipse;
  288.       windowRect.shrinkBy( 25 );
  289.       ellipse.setEnclosingRect( windowRect );
  290.       region += ellipse;
  291.       windowRect.shrinkBy( 25 );
  292.       ellipse.setEnclosingRect( windowRect );
  293.       region -= ellipse;
  294.       windowRect.shrinkBy( 25 );
  295.       ellipse.setEnclosingRect( windowRect );
  296.       region += ellipse;
  297.       region &= oldClipRegion;
  298.     }
  299.     break;
  300.     case IDM_CLIPSQUARES:
  301.     {
  302.       IGRectangle rect( windowRect );
  303.       region += rect;
  304.       windowRect.shrinkBy( 25 );
  305.       rect.setEnclosingRect( windowRect );
  306.       region -= rect;
  307.       windowRect.shrinkBy( 25 );
  308.       rect.setEnclosingRect( windowRect );
  309.       region += rect;
  310.       windowRect.shrinkBy( 25 );
  311.       rect.setEnclosingRect( windowRect );
  312.       region -= rect;
  313.       windowRect.shrinkBy( 25 );
  314.       rect.setEnclosingRect( windowRect );
  315.       region += rect;
  316.       windowRect.shrinkBy( 25 );
  317.       rect.setEnclosingRect( windowRect );
  318.       region -= rect;
  319.       windowRect.shrinkBy( 25 );
  320.       rect.setEnclosingRect( windowRect );
  321.       region += rect;
  322.       region &= oldClipRegion;
  323.       break;
  324.     }
  325.     case IDM_CLIPRAD:
  326.     {
  327.       IGPie pie( windowRect, 0, 60 );
  328.       region += pie;
  329.       pie.setStartAngle( 120 );
  330.       region += pie;
  331.       pie.setStartAngle( 240 );
  332.       region += pie;
  333.       region &= oldClipRegion;
  334.       break;
  335.     }
  336.     case IDM_CLIPNONE:
  337.       region = oldClipRegion;
  338.     break;
  339.   } /* endswitch */
  340.  
  341.   // set the clip region
  342.   gc.setClipRegion( region );
  343.  
  344.   // draw the bitmap if we have one
  345.   if (bitmap)
  346.   {
  347.     bitmap->drawOn(gc);
  348.   }
  349.  
  350.   // clear the current clip region
  351.   gc.clearClipRegion( );
  352.  
  353.   return true;
  354. }
  355.