home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / 2dbmap / 2dbmap.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  16.8 KB  |  414 lines

  1. /******************************************************************************
  2. * .FILE:         2d-bmap.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION:  2D Bitmap Graphic Sample Program:  Implementation            *
  5. *                                                                             *
  6. * .CLASSES:      DrawingAreaPaintHandler                                      *
  7. *                MainCommandHandler                                           *
  8. *                DrawingArea                                                  *
  9. *                MainWindow                                                   *
  10. *                                                                             *
  11. * .COPYRIGHT:                                                                 *
  12. *    Licensed Material - Program-Property of IBM                              *
  13. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  14. *                                                                             *
  15. * .DISCLAIMER:                                                                *
  16. *   The following [enclosed] code is sample code created by IBM               *
  17. *   Corporation.  This sample code is not part of any standard IBM product    *
  18. *   and is provided to you solely for the purpose of assisting you in the     *
  19. *   development of your applications.  The code is provided 'AS IS',          *
  20. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  21. *   arising out of your use of the sample code, even if they have been        *
  22. *   advised of the possibility of such damages.                               *
  23. *                                                                             *
  24. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  25. *                                                                             *
  26. ******************************************************************************/
  27.  
  28. #include <ibase.hpp>
  29. #include <ifiledlg.hpp>
  30. #include <ihandle.hpp>
  31. #include <igelipse.hpp>
  32. #include <igrect.hpp>
  33. #include <igpie.hpp>
  34. #include <igrafctx.hpp>
  35. #include <igregion.hpp>
  36. #include <imenubar.hpp>
  37. #include <ireslib.hpp>
  38. #include <icoordsy.hpp>
  39. #include "2dbmap.hpp"
  40.  
  41. /*****************************************************************************
  42. * main  - Application entry point                                            *
  43. *****************************************************************************/
  44. int main()
  45. {
  46.  
  47.   ICoordinateSystem::setApplicationOrientation(
  48.         ICoordinateSystem::originLowerLeft );
  49.   MainWindow  mainWindow(WND_MAIN);
  50.   IApplication::current().run();
  51.  
  52.   return 0;
  53. } /* end main */
  54.  
  55.  
  56. /*******************************************************************************
  57. * Class MainWindow::MainWindow - Constructor for the main window               *
  58. *                                                                              *
  59. * Define itself as a IFrameWindow                                              *
  60. * Instatiate the viewport to display the bitmap.                               *
  61. * Create a drawing area for the viewport.                                      *
  62. * Instatiate the command handler to handle command events.                     *
  63. *******************************************************************************/
  64. MainWindow::MainWindow(unsigned long windowId)
  65.   : MainWindow::Inherited(              //  Call Inherited constructor
  66.     IFrameWindow::defaultStyle()        //  Use default plus
  67.     | IFrameWindow::animated            //  Set to show with "animation"
  68.     | IFrameWindow::menuBar             //  Frame has a menu bar
  69.     | IFrameWindow::minimizedIcon,      //  Frame has an icon
  70.     windowId),                          //  Main Window ID
  71.     fViewPort( WND_VPORT, this, this ),
  72.     fDrawingArea( WND_DRAW, &fViewPort, &fViewPort ),
  73.     fMainCommandHandler()
  74. {
  75. /*-----------------------------------------------------------------------------|
  76. | Begin handling events for MainWindow and set the client area                 |
  77. ------------------------------------------------------------------------------*/
  78.   fMainCommandHandler.handleEventsFor(this);
  79.   setClient( &fViewPort );
  80.  
  81. /*-----------------------------------------------------------------------------|
  82. | Create the menu bar and disable menu items that require a loaded bitmap.     |                                                                           |
  83. ------------------------------------------------------------------------------*/
  84.   IMenuBar menuBar(windowId, this);
  85.   menuBar.setAutoDestroyWindow(false);
  86.   menuBar.disableItem( IDM_REFLECTHORZ );
  87.   menuBar.disableItem( IDM_REFLECTVERT );
  88.   menuBar.disableItem( IDM_ROTATE90    );
  89.   menuBar.disableItem( IDM_ROTATE180   );
  90.   menuBar.disableItem( IDM_ROTATE270   );
  91.   menuBar.disableItem( IDM_TRANSPOSE   );
  92.   menuBar.disableItem( IDM_CLIPCIRCLES );
  93.   menuBar.disableItem( IDM_CLIPSQUARES );
  94.   menuBar.disableItem( IDM_CLIPRAD     );
  95.   menuBar.disableItem( IDM_CLIPNONE    );
  96.  
  97. /*-----------------------------------------------------------------------------|
  98. | Set the focus for the window and show the window on the screen.              |
  99. ------------------------------------------------------------------------------*/
  100.   setFocus();
  101.   show();
  102. }
  103.  
  104. /*******************************************************************************
  105. * Class MainWindow::~MainWindow - Destructor for the main window               *
  106. *******************************************************************************/
  107. MainWindow::~MainWindow()
  108. {
  109. /*-----------------------------------------------------------------------------|
  110. | Stop handling events for the MainWindow                                      |
  111. ------------------------------------------------------------------------------*/
  112.   fMainCommandHandler.stopHandlingEventsFor(this);
  113. }
  114.  
  115. /*******************************************************************************
  116. * Class MainWindow::modifyBitmap - modify the bitmap according to the command  *
  117. *   event.                                                                     *
  118. *******************************************************************************/
  119. MainWindow& MainWindow::modifyBitmap( unsigned long eventId )
  120. {
  121.   IGBitmap* bitmap(fDrawingArea.bitmap());
  122.   if (bitmap)
  123.   {
  124.     switch (eventId)
  125.     {
  126.       case  IDM_REFLECTHORZ:
  127.         bitmap->reflectHorizontally();
  128.       break;
  129.       case  IDM_REFLECTVERT:
  130.         bitmap->reflectVertically();
  131.       break;
  132.       case  IDM_ROTATE90:
  133.         bitmap->rotateBy90();
  134.       break;
  135.       case  IDM_ROTATE180:
  136.         bitmap->rotateBy180();
  137.       break;
  138.       case  IDM_ROTATE270:
  139.         bitmap->rotateBy270();
  140.       break;
  141.       case  IDM_TRANSPOSE:
  142.         bitmap->transposeXForY();
  143.       break;
  144.     } /* endswitch */
  145.     ISize bitmapSize(bitmap->size());
  146.     fDrawingArea.sizeTo(bitmapSize);
  147.     fViewPort.setViewWindowSize(bitmapSize);
  148.     fDrawingArea.refresh();
  149.   }
  150.   return *this;
  151. }
  152.  
  153. /*******************************************************************************
  154. * Class MainCommandHandler::command - Handles command events for MainWindow    *
  155. *******************************************************************************/
  156. IBase::Boolean MainCommandHandler::command( ICommandEvent& event )
  157. {
  158.   Boolean fProcessed = false;
  159.   switch (event.commandId())
  160.   {
  161.     case  IDM_FILEOPEN:
  162.     {
  163.       MainWindow *mainWindow((MainWindow*)event.window());
  164.       IResourceLibrary reslib;
  165.       IFileDialog::Settings fsettings;      //                                     .
  166.       fsettings.setTitle(TITLE_LD_IMG);//Set Open Dialog Title from Resource  .
  167.       fsettings.setFileName(reslib.loadString(STR_FL_NAME));          //Set FileNames to *.hlo               .
  168.  
  169.       IFileDialog fd(                          //Create File Open Dialiog             .
  170.         IWindow::desktopWindow(),              //  Parent is Desktop                  .
  171.         mainWindow, fsettings);                //  Owner is me with settings          .
  172.       if (fd.pressedOK())                      //Check if ok from file open dialog    .
  173.       {
  174.         mainWindow->loadImageFile(fd.fileName());
  175. /*-----------------------------------------------------------------------------|
  176. | Enable menubar items since a bitmap file has been loaded.                    |
  177. ------------------------------------------------------------------------------*/
  178.         IMenuBar menuBar(mainWindow->id(), mainWindow);
  179.         menuBar.setAutoDestroyWindow(false);
  180.         menuBar.enableItem( IDM_REFLECTHORZ );
  181.         menuBar.enableItem( IDM_REFLECTVERT );
  182.         menuBar.enableItem( IDM_ROTATE90    );
  183.         menuBar.enableItem( IDM_ROTATE180   );
  184.         menuBar.enableItem( IDM_ROTATE270   );
  185.         menuBar.enableItem( IDM_TRANSPOSE   );
  186.         menuBar.enableItem( IDM_CLIPCIRCLES );
  187.         menuBar.enableItem( IDM_CLIPSQUARES );
  188.         menuBar.enableItem( IDM_CLIPRAD     );
  189.         menuBar.enableItem( IDM_CLIPNONE    );
  190.       }
  191.     }
  192.     break;
  193.     case  IDM_QUIT:
  194.     {
  195.       IFrameWindow *frameWindow((IFrameWindow*)event.window());
  196.       frameWindow->close();
  197.     }
  198.     break;
  199.     case  IDM_REFLECTHORZ:
  200.     case  IDM_REFLECTVERT:
  201.     case  IDM_ROTATE90:
  202.     case  IDM_ROTATE180:
  203.     case  IDM_ROTATE270:
  204.     case  IDM_TRANSPOSE:
  205.     {
  206.       MainWindow *mainWindow((MainWindow*)event.window());
  207.       mainWindow->modifyBitmap( event.commandId() );
  208.     }
  209.     break;
  210.  
  211.     case  IDM_CLIPCIRCLES:
  212.     case  IDM_CLIPSQUARES:
  213.     case  IDM_CLIPRAD:
  214.     case  IDM_CLIPNONE:
  215.     {
  216.       MainWindow *mainWindow((MainWindow*)event.window());
  217.       mainWindow->drawingArea().setClipStyle( event.commandId() );
  218.       mainWindow->drawingArea().refresh();
  219.     }
  220.     break;
  221.   } /* endswitch */
  222.  
  223.   return fProcessed;
  224. }
  225.  
  226. /*******************************************************************************
  227. * Class MainWindow::loadImageFile - Loads an image file into a drawing canvas. *
  228. *******************************************************************************/
  229. MainWindow& MainWindow::loadImageFile( const IString& imageFile )
  230. {
  231.   fDrawingArea.loadBitmap( imageFile );
  232.   fViewPort.setViewWindowSize( fDrawingArea.bitmap()->size());
  233.   return *this;
  234. }
  235.  
  236. /*******************************************************************************
  237. * Class DrawingArea :: DrawingArea - Constructor for the drawing area          *
  238. *******************************************************************************/
  239.  
  240. DrawingArea::DrawingArea( unsigned long id, IWindow* parent, IWindow* owner )
  241.   : DrawingArea::Inherited( id, parent, owner, IRectangle(),
  242.                             IDrawingCanvas::defaultStyle() & ~IDrawingCanvas::useDefaultPaintHandler ),
  243.     fBitmap(0),
  244.     fStyle(IDM_CLIPNONE)
  245. {
  246.   drawingAreaPaintHandler.handleEventsFor(this);
  247. }
  248.  
  249.  
  250. /*******************************************************************************
  251. * Class DrawingArea::~DrawingArea - Destructor for the drawing area            *
  252. *******************************************************************************/
  253. DrawingArea::~DrawingArea()
  254. {
  255.   drawingAreaPaintHandler.stopHandlingEventsFor(this);
  256.   if (fBitmap)
  257.     delete fBitmap;
  258. }
  259.  
  260.  
  261. /*******************************************************************************
  262. * Class DrawingArea::bitmap - Returns the IGBitmap currently being used        *
  263. *******************************************************************************/
  264. IGBitmap* DrawingArea::bitmap( ) const
  265. {
  266.   return fBitmap;
  267. }
  268.  
  269. /*******************************************************************************
  270. * Class DrawingArea::loadBitmap - Loads a new bitmap from a file               *
  271. *******************************************************************************/
  272. DrawingArea& DrawingArea::loadBitmap( const IString& imageFile )
  273. {
  274.   if (fBitmap)
  275.     delete fBitmap;
  276.   fBitmap = new IGBitmap( imageFile );
  277.   sizeTo(fBitmap->size());
  278.   refresh();
  279.   return *this;
  280. }
  281.  
  282.  
  283. /*******************************************************************************
  284. * Class DrawingArea::calcMinimumSize - Calculates the size of the bitmap       *
  285. *******************************************************************************/
  286. ISize DrawingArea::calcMinimumSize( ) const
  287. {
  288.   if (fBitmap)
  289.     return fBitmap->size();
  290.   else
  291.     return ISize();
  292. }
  293.  
  294. /*******************************************************************************
  295. * Class DrawingAreaPaintHandler::paintWindow - Handles the repainting of the   *
  296. *   window.                                                                    *
  297. *******************************************************************************/
  298. IBase::Boolean DrawingAreaPaintHandler::paintWindow( IPaintEvent& event )
  299. {
  300. /*-----------------------------------------------------------------------------|
  301.   // Get a graphic context
  302. ------------------------------------------------------------------------------*/
  303.   IGraphicContext gc(event.presSpaceHandle());
  304.  
  305. /*-----------------------------------------------------------------------------|
  306.   // Get a pointer to the current bitmap if one exists.
  307. ------------------------------------------------------------------------------*/
  308.   IGBitmap* bitmap(((DrawingArea*)event.window())->bitmap());
  309.  
  310. /*-----------------------------------------------------------------------------|
  311.   // Get the dimensions of the window
  312. ------------------------------------------------------------------------------*/
  313.   IRectangle windowRect(IPoint(),((DrawingArea*)event.window())->size());
  314.  
  315. /*-----------------------------------------------------------------------------|
  316.   // paint the current background color
  317. ------------------------------------------------------------------------------*/
  318.   event.clearBackground();
  319.  
  320. /*-----------------------------------------------------------------------------|
  321.   // Query the current clipping style
  322. ------------------------------------------------------------------------------*/
  323.   unsigned long clipStyle(((DrawingArea*)event.window())->clipStyle());
  324.  
  325.   IGRegion region;
  326.  
  327. /*-----------------------------------------------------------------------------|
  328.   // Using an empty region, draw a clipping pattern.
  329.   // Set the clipping region.
  330. ------------------------------------------------------------------------------*/
  331.   switch (clipStyle)
  332.   {
  333.     case IDM_CLIPCIRCLES:
  334.     {
  335.       IGRectangle rect( windowRect );
  336.       region -= rect;
  337.       IGEllipse ellipse( windowRect );
  338.       region += ellipse;
  339.       windowRect.shrinkBy( 25 );
  340.       ellipse.setEnclosingRect( windowRect );
  341.       region -= ellipse;
  342.       windowRect.shrinkBy( 25 );
  343.       ellipse.setEnclosingRect( windowRect );
  344.       region += ellipse;
  345.       windowRect.shrinkBy( 25 );
  346.       ellipse.setEnclosingRect( windowRect );
  347.       region -= ellipse;
  348.       windowRect.shrinkBy( 25 );
  349.       ellipse.setEnclosingRect( windowRect );
  350.       region += ellipse;
  351.       windowRect.shrinkBy( 25 );
  352.       ellipse.setEnclosingRect( windowRect );
  353.       region -= ellipse;
  354.       windowRect.shrinkBy( 25 );
  355.       ellipse.setEnclosingRect( windowRect );
  356.       region += ellipse;
  357.       gc.setClipRegion( region );
  358.       break;
  359.     }
  360.     case IDM_CLIPSQUARES:
  361.     {
  362.       IGRectangle rect( windowRect );
  363.       region += rect;
  364.       windowRect.shrinkBy( 25 );
  365.       rect.setEnclosingRect( windowRect );
  366.       region -= rect;
  367.       windowRect.shrinkBy( 25 );
  368.       rect.setEnclosingRect( windowRect );
  369.       region += rect;
  370.       windowRect.shrinkBy( 25 );
  371.       rect.setEnclosingRect( windowRect );
  372.       region -= rect;
  373.       windowRect.shrinkBy( 25 );
  374.       rect.setEnclosingRect( windowRect );
  375.       region += rect;
  376.       windowRect.shrinkBy( 25 );
  377.       rect.setEnclosingRect( windowRect );
  378.       region -= rect;
  379.       windowRect.shrinkBy( 25 );
  380.       rect.setEnclosingRect( windowRect );
  381.       region += rect;
  382.       gc.setClipRegion( region );
  383.       break;
  384.     }
  385.     case IDM_CLIPRAD:
  386.     {
  387.       IGPie pie( windowRect, 0, 60 );
  388.       region += pie;
  389.       pie.setStartAngle( 120 );
  390.       region += pie;
  391.       pie.setStartAngle( 240 );
  392.       region += pie;
  393.       gc.setClipRegion( region );
  394.       break;
  395.     }
  396.   } /* endswitch */
  397.  
  398. /*-----------------------------------------------------------------------------|
  399. | Draw the bitmap if we have one                                               |
  400. ------------------------------------------------------------------------------*/
  401.   if (bitmap)
  402.   {
  403.     bitmap->drawOn(gc);
  404.   }
  405.  
  406. /*-----------------------------------------------------------------------------|
  407. | Clear the clip region                                                        |
  408. ------------------------------------------------------------------------------*/
  409.   gc.clearClipRegion( );
  410.  
  411.   return true;
  412. }
  413.  
  414.