home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLUI / DRAG4 / DMSAMP4.HPP < prev    next >
Text File  |  1993-10-21  |  7KB  |  206 lines

  1. #include "dmsamp3.hpp"
  2.  
  3. #include <idmprov.hpp>
  4. #include <idmcnrit.hpp>
  5. #include <idmevent.hpp>
  6. #include <idmimage.hpp>
  7. #include <ititle.hpp>
  8. #include <itrace.hpp>
  9. #include <istring.hpp>
  10.  
  11. class CustomerItem : public IDMCnrItem {
  12. typedef IDMCnrItem
  13.   Inherited;
  14. public:
  15.   CustomerItem ( const IDMItem::Handle &item )
  16.     : IDMCnrItem( item )
  17.     {
  18.     IString
  19.       rmf1 = IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem ),
  20.       rmf2 = IDMItem::rmfFrom( IDM::rmDiscard, IDM::rfUnknown );
  21.     // Get pointer to the associated Customer container object...
  22.     Customer
  23.      *customer = (Customer*)this->object();
  24.  
  25.     // Build and set contents.  We can only do this on the source
  26.     // side.  Note that since we call this ctor on both source
  27.     // and target sides, we must distinguish them.  That is done
  28.     // here by checking the "object" pointer.  If this ctor was
  29.     // called from within our generateSourceItems, then this value
  30.     // will be non-zero.  If called from with the template provider's
  31.     // provideTargetItemFor, then it will be zero.
  32.     if ( customer )
  33.       {
  34.       IString
  35.         contents,
  36.         delim = '\x01';
  37.       contents += customer -> iconText() + delim;
  38.       contents += customer -> name() + delim;
  39.       contents += customer -> address() + delim;
  40.       contents += customer -> phone() + delim;
  41.       contents += customer -> iconId();
  42.  
  43.       this -> setContents( contents );
  44.  
  45.       // Add RMFs supported by this class (IDMCnrItem will have
  46.       // already specified others)...
  47.       this -> addRMF( rmf1 );
  48.       this -> addRMF( rmf2 );
  49.       }
  50.     else
  51.       // On target side, add in <rmLibrary,rfSharedMem> if source concurs
  52.       // (and it's not already in there)...
  53.       if ( item -> supportsRMF( rmf1 )
  54.            &&
  55.            !( this -> supportsRMF( rmf1 ) ) )
  56.         this -> addRMF( rmf1 );
  57.     }
  58.  
  59.   ~CustomerItem ( )
  60.     {
  61.     }
  62.  
  63. static Boolean
  64.   generateSourceItems ( const IDMSourceOperation::Handle &srcOp )
  65.     {
  66.     // Get generic container items.  Note that we call the inherited
  67.     // function since it already has smarts to deal with multi-selection,
  68.     // etc...
  69.     Boolean
  70.       result = Inherited::generateSourceItems( srcOp );
  71.  
  72.     // Now, replace each IDMCnrItem with a customer item...
  73.     for ( unsigned i = 1; i <= srcOp->numberOfItems(); i++ )
  74.       srcOp -> replaceItem( i, new CustomerItem( srcOp -> item( i ) ) );
  75.  
  76.     // Set stack3AndFade as the default image style ...
  77.     srcOp -> setImageStyle( IDM::stack3AndFade );
  78.     return result;
  79.     }
  80.  
  81. virtual Boolean
  82.   sourceDiscard ( IDMSourceDiscardEvent &event )
  83.     {
  84.     // Remove the object from the container...
  85.     IContainerControl
  86.      *cnr = (IContainerControl*)( event.sourceOperation()->sourceWindow() );
  87.     IContainerObject
  88.      *obj = (IContainerObject*)( this->object() );
  89.     cnr -> removeObject( obj );
  90.     return true;
  91.     }
  92.  
  93. virtual Boolean
  94.   targetDrop ( IDMTargetDropEvent &event )
  95.     {
  96.     Boolean
  97.       result = true;
  98.     // Check if using rfSharedMem...
  99.     IString
  100.       myRMF = IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem );
  101.     if ( this->selectedRMF() == myRMF )
  102.       {
  103.       // Yes, construct new Customer from passed data...
  104.       IString
  105.         contents = this->contents(),
  106.         delim    = '\x01',
  107.         text     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  108.       contents = contents.subString( contents.indexOf( delim ) + 1 );
  109.       IString
  110.         name     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  111.       contents = contents.subString( contents.indexOf( delim ) + 1 );
  112.       IString
  113.         addr     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  114.       contents = contents.subString( contents.indexOf( delim ) + 1 );
  115.       IString
  116.         phone    = contents.subString( 1, contents.indexOf( delim ) - 1 ),
  117.         iconId   = contents.subString( contents.indexOf( delim ) + 1 );
  118.       IContainerControl
  119.        *cnr = event.container();
  120.       Customer
  121.        *pNewCustomer = new Customer( text,
  122.                                      iconId.asUnsigned(),
  123.                                      name,
  124.                                      addr,
  125.                                      phone,
  126.                                      (MyWindow*)( cnr->parent() ) );
  127.       // ...and insert the new Customer into the container...
  128.       cnr -> setRefreshOff();
  129.       cnr -> addObject( pNewCustomer );
  130.       Handle
  131.         thisHandle;
  132.       thisHandle = this;
  133.       IPoint
  134.         pos = this->targetOperation()->dropPosition( thisHandle, event );
  135.       cnr -> moveObjectTo( pNewCustomer,
  136.                            0,
  137.                            cnr,
  138.                            0,
  139.                            pos );
  140.       cnr -> setRefreshOn();
  141.       cnr -> refresh();
  142.       }
  143.     else
  144.       // Some other RMF, base class must support it...
  145.       result = Inherited::targetDrop( event );
  146.  
  147.     return result;
  148.     }
  149. virtual unsigned long
  150.   supportedOperationsFor ( const IString &rmf ) const
  151.     {
  152.     if ( rmf == IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem ) )
  153.       // If using <rmLibrary,rfSharedMem> then only copy is supported...
  154.       return IDMItem::copyable & this->supportedOperations();
  155.     else
  156.       // Else, whatever base class supports...
  157.       return Inherited::supportedOperationsFor( rmf );
  158.     }
  159. }; // Class CustomerItem
  160.  
  161. class DMSample4Window : public MyWindow {
  162. /*******************************************************************************
  163. * These windows are similar to those from dmsamp3 but use icon view and        *
  164. * permit dragging/dropping within the same window.  Note that sometimes it     *
  165. * is necessary to restrict target support so if the window title include       *
  166. * "source only" or "target only" we only permit drag/drop, respectively.       *
  167. *******************************************************************************/
  168. public:
  169.   DMSample4Window ( const char *aTitle )
  170.     : MyWindow( 0 ),
  171.       title( this )
  172.     {
  173.     // Set title...                                              /
  174.     if ( aTitle )
  175.       title.setTitleText( aTitle );
  176.     else
  177.       title.setTitleText( "Direct Manipulation Sample 4" );
  178.  
  179.     // Tailor the container...
  180.     this->cnrCtl -> hideTitle();
  181.     this->cnrCtl -> showIconView();
  182.     this->cnrCtl -> arrangeIconView();
  183.     this->cnrCtl -> setExtendedSelection();
  184.  
  185.     // Use appropriate item provider...
  186.     this->cnrCtl -> setItemProvider( &this->provider );
  187.  
  188.     // Enable drag/drop...
  189.     IString
  190.       sTitle = aTitle;
  191.     if ( sTitle.includes( "source only" ) )
  192.       IDMHandler::enableDragFrom( this->cnrCtl );
  193.     else if ( sTitle.includes( "target only" ) )
  194.       IDMHandler::enableDropOn( this->cnrCtl );
  195.     else
  196.       IDMHandler::enableDragDropFor( this->cnrCtl );
  197.  
  198.     // Make size more reasonable...
  199.     this->sizeTo( ISize( 250, 250 ) );
  200.     }
  201. IDMItemProviderFor< CustomerItem >
  202.   provider;
  203. ITitle
  204.   title;
  205. }; // class DMSample4Window
  206.