home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / DRAG4 / DMSAMP4.CPP < prev    next >
Text File  |  1995-05-01  |  22KB  |  445 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 "dmsamp4.hpp"
  5.  
  6. /*------------------------------------------------------------------------------
  7. | main                                                                         |
  8. ------------------------------------------------------------------------------*/
  9. int main( int argc, char* argv[] )
  10. {
  11.   /***********************************************************************/
  12.   /* Permit debugging during the drag ...                                */
  13.   /***********************************************************************/
  14.   IDM::debugSupport = true;
  15.  
  16.   /***********************************************************************/
  17.   /* Create window ...                                                   */
  18.   /***********************************************************************/
  19.   DMSample4Window
  20.     frame( argv[1] );
  21.  
  22.   /***********************************************************************/
  23.   /* Show it ...                                                         */
  24.   /***********************************************************************/
  25.   frame.show();
  26.   IApplication::current().run();
  27.   return 0;
  28. }
  29.  
  30. /*------------------------------------------------------------------------------
  31. | CustomerItem::CustomerItem                                                   |
  32. |                                                                              |
  33. | Constructor.                                                                 |
  34. ------------------------------------------------------------------------------*/
  35. CustomerItem :: CustomerItem ( const IDMItem::Handle& item ) :
  36.                 IDMCnrItem ( item )
  37. {
  38.   IString
  39.     rmf1 = IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem ),
  40.     rmf2 = IDMItem::rmfFrom( IDM::rmDiscard, IDM::rfUnknown );
  41.  
  42.   /***********************************************************************/
  43.   /* Get pointer to the associated Customer container object             */
  44.   /***********************************************************************/
  45.   Customer *pCustomer = (Customer *)object();
  46.  
  47.   /***********************************************************************/
  48.   /* Build and set contents.  We can only do this on the source          */
  49.   /* side.  Note that since we call this constructor on both source      */
  50.   /* and target sides, we must distinguish them.  That is done           */
  51.   /* here by checking the "object" pointer.  If this constructor was     */
  52.   /* called from within our generateSourceItems, then this value         */
  53.   /* will be non-zero.  If called from with the template provider's      */
  54.   /* provideTargetItemFor, then it will be zero.                         */
  55.   /***********************************************************************/
  56.   if (pCustomer)
  57.   {
  58.     IString
  59.       contents,
  60.       delim = '\x01';
  61.  
  62.     contents += pCustomer->iconText() + delim;
  63.     contents += pCustomer->name() + delim;
  64.     contents += pCustomer->address() + delim;
  65.     contents += pCustomer->phone() + delim;
  66.     contents += pCustomer->iconId();
  67.  
  68.     setContents( contents );
  69.  
  70.     /*********************************************************************/
  71.     /* Add RMFs supported by this class (IDMCnrItem will have            */
  72.     /* already specified the other RMFs we use)                          */
  73.     /*********************************************************************/
  74.     addRMF( rmf1 );
  75.     addRMF( rmf2 );
  76.   }
  77.   else
  78.   {
  79.     /*********************************************************************/
  80.     /* On target side, add in <rmLibrary,rfSharedMem> if source concurs  */
  81.     /* (and it's not already in there)...                                */
  82.     /*********************************************************************/
  83.     if ((item->supportsRMF( rmf1 ))  &&
  84.         !(supportsRMF( rmf1 )))
  85.     {
  86.       addRMF( rmf1 );
  87.     }
  88.   }
  89. }
  90.  
  91. /*------------------------------------------------------------------------------
  92. | CustomerItem::generateSourceItems                                            |
  93. |                                                                              |
  94. | Called to give CustomerItem opportunity to attach new CustomerItem's to the  |
  95. | argument IDMSourceOperation object.                                          |
  96. ------------------------------------------------------------------------------*/
  97. Boolean CustomerItem :: generateSourceItems ( IDMSourceOperation* pSrcOp )
  98. {
  99.   /***********************************************************************/
  100.   /* Get generic container items.  Note that we call the inherited       */
  101.   /* function since it already has logic to deal with multi-selection,   */
  102.   /* etc...                                                              */
  103.   /***********************************************************************/
  104.   Boolean result = Inherited::generateSourceItems( pSrcOp );
  105.  
  106.   /***********************************************************************/
  107.   /* Now, replace each IDMCnrItem with a CustomerItem                    */
  108.   /***********************************************************************/
  109.   for (unsigned i = 1; i <= pSrcOp->numberOfItems(); i++)
  110.   {
  111.     pSrcOp->replaceItem( i, new CustomerItem( pSrcOp->item( i ) ) );
  112.   }
  113.  
  114.   /***********************************************************************/
  115.   /* Set stack3AndFade as the default image style and set the stacking   */
  116.   /* percentage that is used to set the stacking offset as a percentage  */
  117.   /* of the image size.                                                  */
  118.   /***********************************************************************/
  119.   pSrcOp->setImageStyle( IDM::stack3AndFade );
  120.   pSrcOp->setStackingPercentage( IPair( 25, 25 ) );
  121.   return( result );
  122. }
  123.  
  124. /*------------------------------------------------------------------------------
  125. | CustomerItem::supportedOperationsFor                                         |
  126. |                                                                              |
  127. | Called when a CustomerItem is dropped on a target container.                 |
  128. ------------------------------------------------------------------------------*/
  129. unsigned long CustomerItem ::
  130.               supportedOperationsFor ( const IString& rmf ) const
  131. {
  132.   if (rmf == IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem ))
  133.   {
  134.     /*********************************************************************/
  135.     /* If using <rmLibrary,rfSharedMem> then only copy is supported      */
  136.     /*********************************************************************/
  137.     return( IDMItem::copyable & supportedOperations() );
  138.   }
  139.  
  140.   /***********************************************************************/
  141.   /* Otherwise, whatever base class supports...                          */
  142.   /***********************************************************************/
  143.   return( Inherited::supportedOperationsFor( rmf ) );
  144. }
  145.  
  146. /*------------------------------------------------------------------------------
  147. | CustomerItem::sourceDiscard                                                  |
  148. |                                                                              |
  149. | Called when a CustomerItem is dropped on a Workplace Shell shredder.         |
  150. ------------------------------------------------------------------------------*/
  151. Boolean CustomerItem :: sourceDiscard ( IDMSourceDiscardEvent& event )
  152. {
  153.   /***********************************************************************/
  154.   /* Remove the object from the container                                */
  155.   /***********************************************************************/
  156.   IContainerControl
  157.    *pCnr = (IContainerControl *)(event.sourceOperation()->sourceWindow());
  158.   IContainerObject
  159.    *pCnrObj = (IContainerObject *)(object());
  160.  
  161.   pCnr->removeObject( pCnrObj );
  162.   return( true );
  163. }
  164.  
  165. /*------------------------------------------------------------------------------
  166. | CustomerItem::targetDrop                                                     |
  167. |                                                                              |
  168. | Called when a CustomerItem is dropped on a target container.                 |
  169. ------------------------------------------------------------------------------*/
  170. Boolean CustomerItem :: targetDrop ( IDMTargetDropEvent& event )
  171. {
  172.   Boolean result = true;
  173.  
  174.   /***********************************************************************/
  175.   /* Check if using ICLUI shared memory rendering format                 */
  176.   /***********************************************************************/
  177.   IString myRMF = IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem );
  178.   if (selectedRMF() == myRMF)
  179.   {
  180.     /*********************************************************************/
  181.     /* Yes, construct new Customer object from passed data               */
  182.     /*********************************************************************/
  183.     IString
  184.       contents = this->contents(),
  185.       delim    = '\x01',
  186.       text     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  187.  
  188.     contents   = contents.subString( contents.indexOf( delim ) + 1 );
  189.     IString
  190.       name     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  191.  
  192.     contents   = contents.subString( contents.indexOf( delim ) + 1 );
  193.     IString
  194.       addr     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  195.  
  196.     contents   = contents.subString( contents.indexOf( delim ) + 1 );
  197.     IString
  198.       phone    = contents.subString( 1, contents.indexOf( delim ) - 1 ),
  199.       iconId   = contents.subString( contents.indexOf( delim ) + 1 );
  200.  
  201.     IContainerControl *pCnr = event.container();
  202.     Customer *pNewCustomer = new Customer( text,
  203.                                            iconId.asUnsigned(),
  204.                                            name,
  205.                                            addr,
  206.                                            phone,
  207.                                            (MyWindow *)(pCnr->parent()) );
  208.  
  209.     /*********************************************************************/
  210.     /* ...and insert the new Customer object into the container          */
  211.     /*********************************************************************/
  212.     pCnr->addObject( pNewCustomer );
  213.  
  214.     /*********************************************************************/
  215.     /* Create an IDMItem::Handle                                         */
  216.     /*                                                                   */
  217.     /* Note:  We must break this into 2 statements due to a bug in the   */
  218.     /*        IRefCounted class.  If we use an initializer to create     */
  219.     /*        the handle, this sample will eventually trap due to the    */
  220.     /*        inability of the initializer to properly increment the     */
  221.     /*        drag item object use count:                                */
  222.     /*          IDMItem::Handle thisHandle = this;  //initializer form   */
  223.     /*                                                                   */
  224.     /*        When we break the create into 2 statements, it takes the   */
  225.     /*        form of an assignment which does not have the problem.     */
  226.     /*********************************************************************/
  227.     IDMItem::Handle thisHandle;
  228.     thisHandle = this;
  229.  
  230.     /*********************************************************************/
  231.     /* Position the object within the container                          */
  232.     /*********************************************************************/
  233.     IPoint pos = targetOperation()->dropPosition( thisHandle, event );
  234.     pCnr->moveObjectTo( pNewCustomer,
  235.                         0,
  236.                         pCnr,
  237.                         0,
  238.                         pos );
  239.   }
  240.   else
  241.   {
  242.     /*********************************************************************/
  243.     /* Some other RMF, base class must support it                        */
  244.     /*********************************************************************/
  245.     result = Inherited::targetDrop( event );
  246.   }
  247.  
  248.   return( result );
  249. }
  250.  
  251. /*------------------------------------------------------------------------------
  252. | DMSample4Window::DMSample4Window                                             |
  253. |                                                                              |
  254. | Constructor.                                                                 |
  255. ------------------------------------------------------------------------------*/
  256. DMSample4Window :: DMSample4Window ( const char* aTitle ) :
  257.                    MyWindow( WND_MAIN ),
  258.                    title( this )
  259. {
  260.   /***********************************************************************/
  261.   /* Set title                                                           */
  262.   /***********************************************************************/
  263.   if (aTitle)
  264.     title.setTitleText( aTitle );
  265.   else
  266.     title.setTitleText( "Direct Manipulation Sample 4" );
  267.  
  268.   /***********************************************************************/
  269.   /* Tailor the container                                                */
  270.   /***********************************************************************/
  271.   this->cnrCtl->hideTitle();
  272.   this->cnrCtl->showIconView();
  273.   this->cnrCtl->arrangeIconView();
  274.   this->cnrCtl->setExtendedSelection();
  275.  
  276.   /***********************************************************************/
  277.   /* Set the item provider                                               */
  278.   /***********************************************************************/
  279.   this->cnrCtl->setItemProvider( &this->provider );
  280.  
  281.   /***********************************************************************/
  282.   /* Enable drag/drop                                                    */
  283.   /***********************************************************************/
  284.   IString sTitle = aTitle;
  285.   if (sTitle.includes( "source only" ))
  286.     IDMHandler::enableDragFrom( this->cnrCtl );
  287.   else
  288.   {
  289.     if (sTitle.includes( "target only" ))
  290.       IDMHandler::enableDropOn( this->cnrCtl );
  291.     else
  292.       IDMHandler::enableDragDropFor( this->cnrCtl );
  293.   }
  294.  
  295.   /***********************************************************************/
  296.   /* Resize the container                                                */
  297.   /***********************************************************************/
  298.   this->sizeTo( ISize( 250, 275 ) );
  299. }
  300.  
  301. /*------------------------------------------------------------------------------
  302. | MyWindow::MyWindow                                                           |
  303. |                                                                              |
  304. | Constructor.                                                                 |
  305. ------------------------------------------------------------------------------*/
  306. MyWindow :: MyWindow ( unsigned long windowId ) :
  307.             IFrameWindow ( windowId ),
  308.  
  309.             cnrCtl (new ICnrCtl (windowId + 20, this, this, IRectangle())),
  310.  
  311.             car ((Customer *) 0),
  312.             space ((Customer *) 0),
  313.             bolt ((Customer *) 0),
  314.             starfleet ((Customer *) 0),
  315.  
  316.             reslib()
  317. {
  318.   /***********************************************************************/
  319.   /* Set the icon                                                        */
  320.   /* Set the container as the frame client.                              */
  321.   /***********************************************************************/
  322.   setIcon( id() );
  323.   setClient(cnrCtl);
  324.  
  325.   /***********************************************************************/
  326.   /* Create the container objects ...                                    */
  327.   /***********************************************************************/
  328.   car =       new Customer ("Auto Racer", CAR, "Al Uncer",
  329.                             "Le Mans, France", "(919) 555-1234", this);
  330.   space =     new Customer ("Astronaut", SPACE, "E. T.",
  331.                             "Pirx the Pilot", "(919) 555-4321", this);
  332.   bolt =      new Customer ("Technician", BOLT, "Joe Blow",
  333.                             "The Sky", "(919) 555-4321", this);
  334.   starfleet = new Customer ("Captain", STARFLEET, "J. L. Picard",
  335.                             "Starfleet", "(919) 555-1212", this);
  336.  
  337.   /***********************************************************************/
  338.   /* and add them to the container.                                      */
  339.   /***********************************************************************/
  340.   cnrCtl->addObject(car);
  341.   cnrCtl->addObject(space);
  342.   cnrCtl->addObject(bolt);
  343.   cnrCtl->addObject(starfleet, space);
  344.  
  345.   /***********************************************************************/
  346.   /* Set the container's attributes.                                     */
  347.   /***********************************************************************/
  348.   cnrCtl->setDeleteObjectsOnClose();
  349.   cnrCtl->showTreeLine();
  350.   cnrCtl->showTitle();
  351.   cnrCtl->enableDrawItem();
  352.  
  353.   /***********************************************************************/
  354.   /* Container view will be tree icon view.                              */
  355.   /***********************************************************************/
  356.   cnrCtl->showTreeIconView();
  357.  
  358.   /***********************************************************************/
  359.   /* Show it ...                                                         */
  360.   /***********************************************************************/
  361.   cnrCtl->show();
  362. }
  363.  
  364. /*------------------------------------------------------------------------------
  365. | MySourceWin::MySourceWin                                                     |
  366. |                                                                              |
  367. | Constructor.                                                                 |
  368. ------------------------------------------------------------------------------*/
  369. MySourceWin :: MySourceWin ( unsigned long windowId ) :
  370.                MyWindow ( windowId )
  371. {
  372.   ITitle title (this, "Direct Manipulation - Source Container" );
  373.  
  374.   /***********************************************************************/
  375.   /* Enable the source for dragging from (only).                         */
  376.   /***********************************************************************/
  377.   IDMHandler::enableDragFrom( cnrCtl );
  378. };
  379.  
  380. /*------------------------------------------------------------------------------
  381. | MyTargetWin::MyTargetWin                                                     |
  382. |                                                                              |
  383. | Constructor.                                                                 |
  384. ------------------------------------------------------------------------------*/
  385. MyTargetWin :: MyTargetWin ( unsigned long windowId ) :
  386.                MyWindow ( windowId )
  387. {
  388.   ITitle title (this, "Direct Manipulation - Target Container" );
  389.  
  390.   /***********************************************************************/
  391.   /* Enable the target for dropping on (only).                           */
  392.   /***********************************************************************/
  393.   IDMHandler::enableDropOn( cnrCtl );
  394. }
  395.  
  396. /*------------------------------------------------------------------------------
  397. | Custoner::Customer                                                           |
  398. |                                                                              |
  399. | Copy constructor.                                                            |
  400. ------------------------------------------------------------------------------*/
  401. Customer :: Customer ( const Customer &cnrobj )  :
  402.             IContainerObject ( (const IContainerObject &)cnrobj ),
  403.             strName ( cnrobj.name() ),
  404.             strAddress ( cnrobj.address() ),
  405.             strPhone ( cnrobj.phone() ),
  406.             ulIconId ( cnrobj.iconId() ),
  407.             myWin ( cnrobj.myWin )
  408. {
  409. }
  410.  
  411. /*------------------------------------------------------------------------------
  412. | Custoner::Customer                                                           |
  413. |                                                                              |
  414. | Constructor.                                                                 |
  415. ------------------------------------------------------------------------------*/
  416. Customer :: Customer ( const IString &Text,
  417.                        unsigned long Icon,
  418.                        const IString &Name,
  419.                        const IString &Address,
  420.                        const IString &Phone,
  421.                        MyWindow* win )  :
  422.                        IContainerObject ( Text, Icon ),
  423.                        strName ( Name ),
  424.                        strAddress ( Address ),
  425.                        strPhone ( Phone ),
  426.                        ulIconId ( Icon ),
  427.                        myWin ( win )
  428. {
  429. }
  430.  
  431. /*------------------------------------------------------------------------------
  432. | Customer::objectCopy                                                         |
  433. |                                                                              |
  434. | Make a copy of the Customer object.  Called by                               |
  435. | IContainerObject::copyObjectTo().                                            |
  436. ------------------------------------------------------------------------------*/
  437. IContainerObject* Customer :: objectCopy()
  438. {
  439.   /***********************************************************************/
  440.   /* Use Customer copy constructor to make a copy of the object.         */
  441.   /***********************************************************************/
  442.   Customer *copy = new Customer(*this);
  443.   return((IContainerObject *)copy);
  444. }
  445.