home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / DRAG2 / DMSAMP2.CPP < prev    next >
Text File  |  1995-05-01  |  12KB  |  263 lines

  1. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  2.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  3. #endif                                  //  is defined.
  4. #include <iframe.hpp>
  5. #include <ibmpctl.hpp>
  6. #include <idmhndlr.hpp>
  7. #include <igbitmap.hpp>
  8. #include <igrafctx.hpp>
  9. #include <igbundle.hpp>
  10. #include <igrect.hpp>
  11. #include <istring.hpp>
  12.  
  13. #include "dmsamp2.hpp"
  14. #include "dmsamp2.h"
  15.  
  16. Boolean ABitmapProvider::bAlreadyDrawn(false);
  17.  
  18. /*------------------------------------------------------------------------------
  19. | main                                                                         |
  20. ------------------------------------------------------------------------------*/
  21. int main()
  22. {
  23.   /***********************************************************************/
  24.   /* Create a generic frame window.                                      */
  25.   /***********************************************************************/
  26.   IFrameWindow
  27.     frame ( "Direct Manipulation Sample 2", WND_MAIN );
  28.  
  29.   /***********************************************************************/
  30.   /* Create an empty bitmap control.                                     */
  31.   /***********************************************************************/
  32.   IBitmapControl
  33.     bmpControl ( 0, &frame, &frame );
  34.  
  35.   /***********************************************************************/
  36.   /* Create a target handler for the bitmap control and use default      */
  37.   /* renderers.                                                          */
  38.   /***********************************************************************/
  39.   IDMHandler::enableDropOn( &bmpControl );
  40.  
  41.   /***********************************************************************/
  42.   /* Create bitmap drag item provider.                                   */
  43.   /***********************************************************************/
  44.   ABitmapProvider
  45.     itemProvider;
  46.  
  47.   /***********************************************************************/
  48.   /* Attach provider to the bitmap control.                              */
  49.   /***********************************************************************/
  50.   bmpControl.setItemProvider( &itemProvider );
  51.  
  52.   /***********************************************************************/
  53.   /* Set the bitmap's control as the frame's client                      */
  54.   /* and display the frame.                                              */
  55.   /***********************************************************************/
  56.   bmpControl.setText( "Drop .bmp files here." );
  57.   bmpControl.setFocus();
  58.  
  59.   frame.sizeTo( ISize( 400, 350 ) );
  60.   frame.setIcon( frame.id() )
  61.        .setClient( &bmpControl )
  62.        .show();
  63.   IApplication::current().run();
  64.   return 0;
  65. }
  66.  
  67. /*------------------------------------------------------------------------------
  68. | ABitmapItem::ABitmapItem                                                     |
  69. |                                                                              |
  70. | Constructor.                                                                 |
  71. ------------------------------------------------------------------------------*/
  72. ABitmapItem :: ABitmapItem ( const IDMItem::Handle& item )
  73.   : IDMItem( item )
  74. {
  75. }
  76.  
  77. /*------------------------------------------------------------------------------
  78. | ABitmapItem::targetDrop                                                      |
  79. |                                                                              |
  80. | Take the dropped file, create a PM bitmap object,                            |
  81. | and set it into the target window.                                           |
  82. ------------------------------------------------------------------------------*/
  83. Boolean ABitmapItem :: targetDrop ( IDMTargetDropEvent& event )
  84. {
  85.   /***********************************************************************/
  86.   /* Get pointer to the target bitmap control.                           */
  87.   /***********************************************************************/
  88.   IBitmapControl
  89.    *bmpControl = (IBitmapControl *)targetOperation()->targetWindow();
  90.  
  91.   /***********************************************************************/
  92.   /* Turn off target emphasis.                                           */
  93.   /***********************************************************************/
  94.   ABitmapProvider
  95.    *provider = (ABitmapProvider *)bmpControl->itemProvider();
  96.   provider->drawEmphasis( bmpControl, event, false );
  97.  
  98.   /***********************************************************************/
  99.   /* Construct dropped .BMP file name from this drag item and attempt to */
  100.   /* load the bitmap from a system file                                  */
  101.   /***********************************************************************/
  102.   IString
  103.     fname = containerName() + sourceName();
  104.   IGBitmap
  105.     bitMap( fname );
  106.  
  107.   /***********************************************************************/
  108.   /* If bitmap was successfully loaded, then set it.  Note that the old  */
  109.   /* one will be automatically deleted.                                  */
  110.   /***********************************************************************/
  111.   if (bitMap.handle())
  112.   {
  113.     bmpControl->setBitmap( bitMap.handle() );
  114.  
  115.     /********************************************************************/
  116.     /* Indicate name of dropped file.                                   */
  117.     /********************************************************************/
  118.     bmpControl->setText( fname );
  119.   }
  120.   else
  121.   {
  122.     bmpControl->setText( "Couldn't create bitmap!" );
  123.   }
  124.  
  125.   return( true );
  126. }
  127.  
  128. /*------------------------------------------------------------------------------
  129. | ABitmapProvider::provideEnterSupport                                         |
  130. |                                                                              |
  131. | Verify that we're dealing with a bitmap object before                        |
  132. | allowing a drop, as well as draw the target emphasis.                        |
  133. ------------------------------------------------------------------------------*/
  134. Boolean ABitmapProvider :: provideEnterSupport ( IDMTargetEnterEvent& event )
  135. {
  136.   /***********************************************************************/
  137.   /* Get handle to the drag target operation                             */
  138.   /***********************************************************************/
  139.   IDMTargetOperation::Handle targetOp = IDMTargetOperation::targetOperation();
  140.  
  141.   /***********************************************************************/
  142.   /* Get pointer to the target bitmap control.                           */
  143.   /***********************************************************************/
  144.   IBitmapControl
  145.    *bmpControl = (IBitmapControl *)event.window();
  146.  
  147.   /***********************************************************************/
  148.   /* Draw the target emphasis.                                           */
  149.   /***********************************************************************/
  150.   drawEmphasis( bmpControl, event );
  151.  
  152.   /***********************************************************************/
  153.   /* Filter the types of items that we allow to be dropped.              */
  154.   /***********************************************************************/
  155.   IDMItem::Handle pTgtDIH = targetOp->item(1);
  156.   IString strTypes = pTgtDIH->types();
  157.  
  158.   /***********************************************************************/
  159.   /* If type is either "Bitmap" or "Plain Text" (used by WPS), we can    */
  160.   /* display the drag item.  If type is "Plain Text", then filter based  */
  161.   /* upon the ".bmp" extension.                                          */
  162.   /***********************************************************************/
  163.   if (strTypes.indexOf( IDM::bitmap ))
  164.   {
  165.     return( true );
  166.   }
  167.   else
  168.   {
  169.     if ((strTypes.includes( IDM::plainText ))  &&
  170.         (pTgtDIH->sourceName().lowerCase().includes( ".bmp" )))
  171.     {
  172.       return( true );
  173.     }
  174.   }
  175.  
  176.   /***********************************************************************/
  177.   /* Type is not recognized - set the drop indicator to prevent a drop!  */
  178.   /***********************************************************************/
  179.   event.setDropIndicator( IDM::neverOk );
  180.   return( false );
  181. }
  182.  
  183. /*------------------------------------------------------------------------------
  184. | ABitmapProvider::provideLeaveSupport                                         |
  185. |                                                                              |
  186. | Remove the target emphasis.                                                  |
  187. ------------------------------------------------------------------------------*/
  188. Boolean ABitmapProvider :: provideLeaveSupport ( IDMTargetLeaveEvent& event )
  189. {
  190.   /***********************************************************************/
  191.   /* Get pointer to the target bitmap control.                           */
  192.   /***********************************************************************/
  193.   IBitmapControl
  194.    *bmpControl = (IBitmapControl *)event.window();
  195.  
  196.   /***********************************************************************/
  197.   /* Remove the target emphasis.                                         */
  198.   /***********************************************************************/
  199.   drawEmphasis( bmpControl, event, false );
  200.   return(true);
  201. }
  202.  
  203. /*------------------------------------------------------------------------------
  204. | ABitmapProvider::drawEmphasis                                                |
  205. |                                                                              |
  206. | Draw/remove the target emphasis                                              |
  207. ------------------------------------------------------------------------------*/
  208. ABitmapProvider& ABitmapProvider::drawEmphasis ( IBitmapControl* bmpControl,
  209.                                                  IDMTargetEvent& event,
  210.                                                  Boolean bDraw )
  211. {
  212.   /***********************************************************************/
  213.   /* Return if the request is to draw the emphasis, and its already      */
  214.   /* drawn.                                                              */
  215.   /***********************************************************************/
  216.   if (bDraw && bAlreadyDrawn)
  217.     return( *this );
  218.  
  219.   if (bDraw)
  220.     bAlreadyDrawn = true;
  221.   else
  222.     bAlreadyDrawn = false;
  223.  
  224.   /***********************************************************************/
  225.   /* Create the graphic context and set the mix mode to cause the pen    */
  226.   /* color to be the inverse of the drawing surface.  Also, set the draw */
  227.   /* operation, so that only the frame of the rectangle is drawn.        */
  228.   /***********************************************************************/
  229.   IGraphicContext
  230.     graphicContext( event.presSpace() );
  231.  
  232.   graphicContext.setMixMode( IGraphicBundle::invert )
  233.                 .setDrawOperation( IGraphicBundle::frame );
  234.  
  235.   /***********************************************************************/
  236.   /* Define the points for the emphasis rectangle and adjust their       */
  237.   /* position so the rectangle will fit within the control window.       */
  238.   /***********************************************************************/
  239.   IPoint
  240.     origin  ( bmpControl->rect().left(),  bmpControl->rect().bottom() ),
  241.     topRight( bmpControl->rect().width(), bmpControl->rect().height() );
  242.  
  243.   origin -= 2;
  244.   topRight -= 4;
  245.  
  246.   /***********************************************************************/
  247.   /* Create an IRectangle object based upon the points defined and use   */
  248.   /* it to construct a 2-Dimensional rectangle object: IGRectangle.      */
  249.   /* Draw the emphasis rectangle using the IGRectangle object.           */
  250.   /***********************************************************************/
  251.   IGRectangle
  252.     graphicRectangle( IRectangle( origin, topRight ) );
  253.  
  254.   graphicRectangle.drawOn( graphicContext );
  255.  
  256.   /***********************************************************************/
  257.   /* Release presentation space and return.                              */
  258.   /***********************************************************************/
  259.   event.releasePresSpace();
  260.   return( *this );
  261. }
  262.  
  263.