home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / uicldd.zip / DMSAMP2.CPP < prev    next >
Text File  |  1993-09-02  |  4KB  |  153 lines

  1. extern "C"
  2.   {
  3.   #include <stdio.h>
  4.   #include <sys\stat.h>
  5.   #define INCL_GPIBITMAPS
  6.   #include <os2.h>
  7.   }
  8.  
  9. #include <iostream.h>
  10. #include <fstream.h>
  11.  
  12. #include <iframe.hpp>
  13. #include <ibmpctl.hpp>
  14. #include <istring.hpp>
  15.  
  16. #include <idmtgth.hpp>
  17.  
  18. #include "dmsamp2.hpp"
  19.  
  20. void main()
  21.   {
  22.   // Use a generic frame window...
  23.   IFrameWindow
  24.     frame ( "C Set ++ Direct Manipulation - Sample 2" );
  25.  
  26.   // Create an empty bitmap control...
  27.   IBitmapControl
  28.     bmpControl ( 0, &frame, &frame );
  29.  
  30.   // Create target handler for the bitmap control...
  31.   IDMTargetHandler
  32.     handler;
  33.  
  34.   // Instruct handler to handle dropping on bitmap control...
  35.   handler.handleEventsFor( &bmpControl );
  36.  
  37.   // Create bitmap drag item provider...
  38.   ABitmapProvider
  39.     itemProvider;
  40.  
  41.   // Attach provider to the bitmap control...
  42.   bmpControl.setItemProvider( &itemProvider );
  43.  
  44.   // Create renderer to render items when dropped on bitmap control...
  45.   IDMTargetRenderer
  46.     renderer;
  47.  
  48.   // Set up renderer to accept .bmp files...
  49.   renderer.setSupportedRMFs( "<DRM_OS2FILE,DRF_TEXT>" )
  50.           .setSupportedTypes( "Bitmap" );
  51.  
  52.   handler.addRenderer( &renderer );
  53.  
  54.   // Set the bitmap control as the frame's client and
  55.   // display the frame...
  56.   bmpControl.setText( "Drop .bmp files here." );
  57.   frame.setClient( &bmpControl )
  58.        .showModally();
  59.   }
  60.  
  61. ABitmapItem :: ABitmapItem ( const IDMItemHandle &item )
  62.   : IDMItem( *item )
  63.   {
  64.   }
  65.  
  66. Boolean ABitmapItem :: dropped ( IWindow *target, IDMTargetDropEvent & )
  67.   {
  68.   // Get pointer to target bitmap control...
  69.   IBitmapControl
  70.    *bmpControl = (IBitmapControl*)target;
  71.  
  72.   // Construct dropped .bmp file name from this item...
  73.   IString
  74.     fname = this->containerName() + "\\" + this->sourceName();
  75.  
  76.   // Get file size...
  77.   struct stat
  78.     buf;
  79.   stat( fname, &buf);
  80.  
  81.   // Open and read the file...
  82.   FILE
  83.    *fileptr = fopen( fname, "rb" );
  84.   char
  85.    *buffer = new char[ buf.st_size ];
  86.   fread( buffer, sizeof(char), buf.st_size, fileptr );
  87.  
  88.   // Construct the bitmap from the file...
  89.   BITMAPARRAYFILEHEADER2
  90.    *array = ( BITMAPARRAYFILEHEADER2 * )buffer;
  91.   BITMAPFILEHEADER2
  92.    *header;
  93.  
  94.   // First, see if file holds array of bitmaps...
  95.   if ( array->usType == BFT_BITMAPARRAY ) {
  96.     // It is, point to first file header in array...
  97.     header = &array->bfh2;
  98.   } else {
  99.     // It isn't, point to file header at start of file...
  100.     header = ( BITMAPFILEHEADER2 * )buffer;
  101.   }
  102.  
  103.   // Now check to see if this is a bitmap...
  104.   if ( header->usType == BFT_BMAP) {
  105.  
  106.     // We can proceed, first get a presentation space...
  107.     IPresSpaceHandle
  108.       hps = bmpControl -> presSpace();
  109.  
  110.     if ( hps ) {
  111.       // Now create the bitmap from the file contents...
  112.       IBitmapHandle
  113.         hbm = GpiCreateBitmap( hps,
  114.                                &header->bmp2,
  115.                                CBM_INIT,
  116.                                buffer + header->offBits,
  117.                                (BITMAPINFO2*)&header->bmp2 );
  118.       if ( hbm ) {
  119.         // Get previously dropped bitmap...
  120.         IBitmapHandle
  121.           old = bmpControl -> bitmap();
  122.  
  123.         // Set new one...
  124.         bmpControl -> setBitmap( hbm );
  125.  
  126.         // Destroy old since we no longer need it...
  127.         GpiDeleteBitmap( old );
  128.  
  129.         // Indicate name of dropped file...
  130.         bmpControl -> setText( fname );
  131.       } else {
  132.         bmpControl -> setText( "Couldn't create bitmap!" );
  133.       }
  134.       // Release the presentation space...
  135.       bmpControl -> releasePresSpace(hps);
  136.     } else {
  137.       bmpControl -> setText ( "Couldn't get PS!" );
  138.     }
  139.   } else {
  140.     bmpControl -> setText( fname + " isn't a bitmap!" );
  141.   }
  142.  
  143.   // Free buffer.
  144.   delete [] buffer;
  145.  
  146.   return true;
  147.   }
  148.  
  149. IDMItemHandle ABitmapProvider :: provideTargetItemFor ( const IDMItemHandle &item )
  150.   {
  151.   return new ABitmapItem ( item );
  152.   }
  153.