home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / drag4 / dmsamp4.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  24.5 KB  |  496 lines

  1. /******************************************************************************
  2. * .FILE:         dmsamp4.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION:  Direct Manipulation Sample Program 4: Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      DMSample4Window                                              *
  7. *                CustomerItem                                                 *
  8. *                MyWindow                                                     *
  9. *                MySourceWin                                                  *
  10. *                MyTargetWin                                                  *
  11. *                Customer                                                     *
  12. *                                                                             *
  13. * .COPYRIGHT:                                                                 *
  14. *    Licensed Material - Program-Property of IBM                              *
  15. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  16. *                                                                             *
  17. * .DISCLAIMER:                                                                *
  18. *   The following [enclosed] code is sample code created by IBM               *
  19. *   Corporation.  This sample code is not part of any standard IBM product    *
  20. *   and is provided to you solely for the purpose of assisting you in the     *
  21. *   development of your applications.  The code is provided 'AS IS',          *
  22. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  23. *   arising out of your use of the sample code, even if they have been        *
  24. *   advised of the possibility of such damages.                               *
  25. *                                                                             *
  26. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  27. *                                                                             *
  28. ******************************************************************************/
  29. #include <ibase.hpp>                  //  since that is where IC_<environ>
  30. #include <icoordsy.hpp>
  31. #include "dmsamp4.hpp"
  32.  
  33. /*------------------------------------------------------------------------------
  34. | main                                                                         |
  35. ------------------------------------------------------------------------------*/
  36. int main( int argc, char* argv[] )
  37. {
  38.   ICoordinateSystem::setApplicationOrientation(
  39.           ICoordinateSystem::originLowerLeft );
  40.  
  41.   /***********************************************************************/
  42.   /* Permit debugging during the drag ...                                */
  43.   /***********************************************************************/
  44. #ifdef IC_PM
  45.   IDM::debugSupport = true;
  46. #else
  47.   IDMOperation::setDebugSupport( true );
  48. #endif
  49.  
  50.   /***********************************************************************/
  51.   /* Create window ...                                                   */
  52.   /***********************************************************************/
  53.   DMSample4Window
  54.     frame( argv[1] );
  55.  
  56.   /***********************************************************************/
  57.   /* Show it ...                                                         */
  58.   /***********************************************************************/
  59.   frame.show();
  60.   IApplication::current().run();
  61.   return 0;
  62. }
  63.  
  64. /*------------------------------------------------------------------------------
  65. | CustomerItem::CustomerItem                                                   |
  66. |                                                                              |
  67. | Constructor.                                                                 |
  68. ------------------------------------------------------------------------------*/
  69. CustomerItem :: CustomerItem ( const IDMItem::Handle& item ) :
  70.                 IDMCnrItem ( item )
  71. {
  72.   IString
  73.     rmf1 = IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem ),
  74.     rmf2 = IDMItem::rmfFrom( IDM::rmDiscard, IDM::rfUnknown );
  75.  
  76.   /***********************************************************************/
  77.   /* Get pointer to the associated Customer container object             */
  78.   /***********************************************************************/
  79.   Customer *pCustomer = (Customer *)object();
  80.  
  81.   /***********************************************************************/
  82.   /* Build and set contents.  We can only do this on the source          */
  83.   /* side.  Note that since we call this constructor on both source      */
  84.   /* and target sides, we must distinguish them.  That is done           */
  85.   /* here by checking the "object" pointer.  If this constructor was     */
  86.   /* called from within our generateSourceItems, then this value         */
  87.   /* will be non-zero.  If called from with the template provider's      */
  88.   /* provideTargetItemFor, then it will be zero.                         */
  89.   /***********************************************************************/
  90.   if (pCustomer)
  91.   {
  92.     IString
  93.       contents,
  94.       delim = '\x01';
  95.  
  96.     contents += pCustomer->iconText() + delim;
  97.     contents += pCustomer->name() + delim;
  98.     contents += pCustomer->address() + delim;
  99.     contents += pCustomer->phone() + delim;
  100.     contents += pCustomer->iconId();
  101.  
  102.     setContents( contents );
  103.  
  104.     /*********************************************************************/
  105.     /* Add RMFs supported by this class (IDMCnrItem will have            */
  106.     /* already specified the other RMFs we use)                          */
  107.     /*********************************************************************/
  108.     addRMF( rmf1 );
  109.     addRMF( rmf2 );
  110.   }
  111.   else
  112.   {
  113.     /*********************************************************************/
  114.     /* On target side, add in <rmLibrary,rfSharedMem> if source concurs  */
  115.     /* (and it's not already in there)...                                */
  116.     /*********************************************************************/
  117.     if ((item->supportsRMF( rmf1 ))  &&
  118.         !(supportsRMF( rmf1 )))
  119.     {
  120.       addRMF( rmf1 );
  121.     }
  122.   }
  123. }
  124.  
  125. /*------------------------------------------------------------------------------
  126. | CustomerItem::generateSourceItems                                            |
  127. |                                                                              |
  128. | Called to give CustomerItem opportunity to attach new CustomerItem's to the  |
  129. | argument IDMSourceOperation object.                                          |
  130. ------------------------------------------------------------------------------*/
  131. IBase::Boolean CustomerItem :: generateSourceItems ( IDMSourceOperation* pSrcOp )
  132. {
  133.   /***********************************************************************/
  134.   /* Get generic container items.  Note that we call the inherited       */
  135.   /* function since it already has logic to deal with multi-selection,   */
  136.   /* etc...                                                              */
  137.   /***********************************************************************/
  138.   Boolean result = Inherited::generateSourceItems( pSrcOp );
  139.  
  140.   /***********************************************************************/
  141.   /* Now, replace each IDMCnrItem with a CustomerItem                    */
  142.   /***********************************************************************/
  143.   for (unsigned i = 1; i <= pSrcOp->numberOfItems(); i++)
  144.   {
  145.     pSrcOp->replaceItem( i, new CustomerItem( pSrcOp->item( i ) ) );
  146.   }
  147.  
  148.   /***********************************************************************/
  149.   /* Set stack3AndFade as the default image style and set the stacking   */
  150.   /* percentage that is used to set the stacking offset as a percentage  */
  151.   /* of the image size.                                                  */
  152.   /***********************************************************************/
  153.   pSrcOp->setImageStyle( IDM::stack3AndFade );
  154.   pSrcOp->setStackingPercentage( IPair( 25, 25 ) );
  155.   return( result );
  156. }
  157.  
  158. /*------------------------------------------------------------------------------
  159. | CustomerItem::supportedOperationsFor                                         |
  160. |                                                                              |
  161. | Called when a CustomerItem is dropped on a target container.                 |
  162. ------------------------------------------------------------------------------*/
  163. unsigned long CustomerItem ::
  164.               supportedOperationsFor ( const IString& rmf ) const
  165. {
  166.   if (rmf == IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem ))
  167.   {
  168.     /*********************************************************************/
  169.     /* If using <rmLibrary,rfSharedMem> then only copy is supported      */
  170.     /*********************************************************************/
  171.     return( IDMItem::copyable & supportedOperations() );
  172.   }
  173.  
  174.   /***********************************************************************/
  175.   /* Otherwise, whatever base class supports...                          */
  176.   /***********************************************************************/
  177.   return( Inherited::supportedOperationsFor( rmf ) );
  178. }
  179.  
  180. /*------------------------------------------------------------------------------
  181. | CustomerItem::sourceDiscard                                                  |
  182. |                                                                              |
  183. | Called when a CustomerItem is dropped on a Workplace Shell shredder.         |
  184. ------------------------------------------------------------------------------*/
  185. IBase::Boolean CustomerItem :: sourceDiscard ( IDMSourceDiscardEvent& event )
  186. {
  187.   /***********************************************************************/
  188.   /* Remove the object from the container                                */
  189.   /***********************************************************************/
  190.   IContainerControl
  191.    *pCnr = (IContainerControl *)(event.sourceOperation()->sourceWindow());
  192.   IContainerObject
  193.    *pCnrObj = (IContainerObject *)(object());
  194.  
  195.   pCnr->removeObject( pCnrObj );
  196.   return( true );
  197. }
  198.  
  199. /*------------------------------------------------------------------------------
  200. | CustomerItem::targetDrop                                                     |
  201. |                                                                              |
  202. | Called when a CustomerItem is dropped on a target container.                 |
  203. ------------------------------------------------------------------------------*/
  204. IBase::Boolean CustomerItem :: targetDrop ( IDMTargetDropEvent& event )
  205. {
  206.   Boolean result = true;
  207.  
  208.   /***********************************************************************/
  209.   /* Check if using ICLUI shared memory rendering format                 */
  210.   /***********************************************************************/
  211.   IString myRMF = IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem );
  212.   if (selectedRMF() == myRMF)
  213.   {
  214.     /*********************************************************************/
  215.     /* Yes, construct new Customer object from passed data               */
  216.     /*********************************************************************/
  217.     IString
  218.       contents = this->contents(),
  219.       delim    = '\x01',
  220.       text     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  221.  
  222.     contents   = contents.subString( contents.indexOf( delim ) + 1 );
  223.     IString
  224.       name     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  225.  
  226.     contents   = contents.subString( contents.indexOf( delim ) + 1 );
  227.     IString
  228.       addr     = contents.subString( 1, contents.indexOf( delim ) - 1 );
  229.  
  230.     contents   = contents.subString( contents.indexOf( delim ) + 1 );
  231.     IString
  232.       phone    = contents.subString( 1, contents.indexOf( delim ) - 1 ),
  233.       iconId   = contents.subString( contents.indexOf( delim ) + 1 );
  234.  
  235.     IContainerControl *pCnr = event.container();
  236.     Customer *pNewCustomer = new Customer( text,
  237.                                            iconId.asUnsigned(),
  238.                                            name,
  239.                                            addr,
  240.                                            phone,
  241.                                            (MyWindow *)(pCnr->parent()) );
  242.  
  243.     /*********************************************************************/
  244.     /* ...and insert the new Customer object into the container          */
  245.     /*********************************************************************/
  246.     pCnr->addObject( pNewCustomer );
  247.  
  248.     /*********************************************************************/
  249.     /* Create an IDMItem::Handle                                         */
  250.     /*                                                                   */
  251.     /* Note:  We must break this into 2 statements due to a bug in the   */
  252.     /*        IRefCounted class.  If we use an initializer to create     */
  253.     /*        the handle, this sample will eventually trap due to the    */
  254.     /*        inability of the initializer to properly increment the     */
  255.     /*        drag item object use count:                                */
  256.     /*          IDMItem::Handle thisHandle = this;  //initializer form   */
  257.     /*                                                                   */
  258.     /*        When we break the create into 2 statements, it takes the   */
  259.     /*        form of an assignment which does not have the problem.     */
  260.     /*********************************************************************/
  261.     IDMItem::Handle thisHandle;
  262.     thisHandle = this;
  263.  
  264.     /*********************************************************************/
  265.     /* Position the object within the container                          */
  266.     /*********************************************************************/
  267.     IPoint pos = targetOperation()->dropPosition( thisHandle, event );
  268.     pCnr->moveObjectTo( pNewCustomer,
  269.                         0,
  270.                         pCnr,
  271.                         0,
  272.                         pos );
  273.   }
  274.   else
  275.   {
  276.     /*********************************************************************/
  277.     /* Some other RMF, base class must support it                        */
  278.     /*********************************************************************/
  279.     result = Inherited::targetDrop( event );
  280.   }
  281.  
  282.   return( result );
  283. }
  284.  
  285. /*------------------------------------------------------------------------------
  286. | DMSample4Window::DMSample4Window                                             |
  287. |                                                                              |
  288. | Constructor.                                                                 |
  289. ------------------------------------------------------------------------------*/
  290. DMSample4Window :: DMSample4Window ( const char* aTitle ) :
  291.                    MyWindow( WND_MAIN ),
  292.                    title( this )
  293. {
  294.   /***********************************************************************/
  295.   /* Set title                                                           */
  296.   /***********************************************************************/
  297.   if (aTitle)
  298.     title.setTitleText( aTitle );
  299.   else
  300.     title.setTitleText( WND_MAIN );
  301.  
  302.   /***********************************************************************/
  303.   /* Tailor the container                                                */
  304.   /***********************************************************************/
  305.   this->cnrCtl->hideTitle();
  306.   this->cnrCtl->showIconView();
  307.   this->cnrCtl->arrangeIconView();
  308.   this->cnrCtl->setExtendedSelection();
  309.  
  310.   /***********************************************************************/
  311.   /* Set the item provider                                               */
  312.   /***********************************************************************/
  313.   this->cnrCtl->setItemProvider( &this->provider );
  314.  
  315.   /***********************************************************************/
  316.   /* Enable drag/drop                                                    */
  317.   /***********************************************************************/
  318.   IString sTitle = aTitle;
  319.   if (sTitle.includes( STR_SOURCE_ONLY))
  320.     IDMHandler::enableDragFrom( this->cnrCtl );
  321.   else
  322.   {
  323.     if (sTitle.includes(SET_ONLY))
  324.       IDMHandler::enableDropOn( this->cnrCtl );
  325.     else
  326.       IDMHandler::enableDragDropFor( this->cnrCtl );
  327.   }
  328.  
  329.   /***********************************************************************/
  330.   /* Resize the container                                                */
  331.   /***********************************************************************/
  332.   this->sizeTo( ISize( 250, 275 ) );
  333. }
  334.  
  335. /*------------------------------------------------------------------------------
  336. | MyWindow::MyWindow                                                           |
  337. |                                                                              |
  338. | Constructor.                                                                 |
  339. ------------------------------------------------------------------------------*/
  340. MyWindow :: MyWindow ( unsigned long windowId ) :
  341.             IFrameWindow ( windowId ),
  342.  
  343.             cnrCtl (new ICnrCtl (windowId + 20, this, this, IRectangle())),
  344.  
  345.             car ((Customer *) 0),
  346.             space ((Customer *) 0),
  347.             bolt ((Customer *) 0),
  348.             starfleet ((Customer *) 0),
  349.  
  350.             reslib()
  351. {
  352.   /***********************************************************************/
  353.   /* Set the icon                                                        */
  354.   /* Set the container as the frame client.                              */
  355.   /***********************************************************************/
  356.   setIcon( id() );
  357.   setClient(cnrCtl);
  358.  
  359.   /***********************************************************************/
  360.   /* Create the container objects ...                                    */
  361.   /***********************************************************************/
  362.   car =       new Customer (reslib.loadString(STR_ITEM_11)
  363.                            , CAR
  364.                            ,reslib.loadString(STR_ITEM_12)
  365.                            ,reslib.loadString(STR_ITEM_13)
  366.                            ,reslib.loadString(STR_ITEM_14)
  367.                            ,this);
  368.   space =     new Customer (reslib.loadString(STR_ITEM_21)
  369.                            , SPACE
  370.                            ,reslib.loadString(STR_ITEM_22)
  371.                            ,reslib.loadString(STR_ITEM_23)
  372.                            ,reslib.loadString(STR_ITEM_24)
  373.                            ,this);
  374.   bolt =      new Customer (reslib.loadString(STR_ITEM_31)
  375.                            , BOLT
  376.                            ,reslib.loadString(STR_ITEM_32)
  377.                            ,reslib.loadString(STR_ITEM_33)
  378.                            ,reslib.loadString(STR_ITEM_34)
  379.                            ,this);
  380.   starfleet = new Customer (reslib.loadString(STR_ITEM_41)
  381.                            , STARFLEET
  382.                            ,reslib.loadString(STR_ITEM_42)
  383.                            ,reslib.loadString(STR_ITEM_43)
  384.                            ,reslib.loadString(STR_ITEM_44)
  385.                            ,this);
  386.  /***********************************************************************/
  387.   /* and add them to the container.                                      */
  388.   /***********************************************************************/
  389.   cnrCtl->addObject(car);
  390.   cnrCtl->addObject(space);
  391.   cnrCtl->addObject(bolt);
  392.   cnrCtl->addObject(starfleet, space);
  393.  
  394.   /***********************************************************************/
  395.   /* Set the container's attributes.                                     */
  396.   /***********************************************************************/
  397.   cnrCtl->setDeleteObjectsOnClose();
  398.   cnrCtl->showTreeLine();
  399.   cnrCtl->showTitle();
  400.   cnrCtl->enableDrawItem();
  401.  
  402.   /***********************************************************************/
  403.   /* Container view will be tree icon view.                              */
  404.   /***********************************************************************/
  405.   cnrCtl->showTreeIconView();
  406.  
  407.   /***********************************************************************/
  408.   /* Show it ...                                                         */
  409.   /***********************************************************************/
  410.   cnrCtl->show();
  411. }
  412.  
  413. /*------------------------------------------------------------------------------
  414. | MySourceWin::MySourceWin                                                     |
  415. |                                                                              |
  416. | Constructor.                                                                 |
  417. ------------------------------------------------------------------------------*/
  418. MySourceWin :: MySourceWin ( unsigned long windowId ) :
  419.                MyWindow ( windowId )
  420. {
  421.   ITitle title (this, TITLE_SOURCE );
  422.  
  423.   /***********************************************************************/
  424.   /* Enable the source for dragging from (only).                         */
  425.   /***********************************************************************/
  426.   IDMHandler::enableDragFrom( cnrCtl );
  427. };
  428.  
  429. /*------------------------------------------------------------------------------
  430. | MyTargetWin::MyTargetWin                                                     |
  431. |                                                                              |
  432. | Constructor.                                                                 |
  433. ------------------------------------------------------------------------------*/
  434. MyTargetWin :: MyTargetWin ( unsigned long windowId ) :
  435.                MyWindow ( windowId )
  436. {
  437.   ITitle title (this, TITLE_TARGET );
  438.  
  439.   /***********************************************************************/
  440.   /* Enable the target for dropping on (only).                           */
  441.   /***********************************************************************/
  442.   IDMHandler::enableDropOn( cnrCtl );
  443. }
  444.  
  445. /*------------------------------------------------------------------------------
  446. | Custoner::Customer                                                           |
  447. |                                                                              |
  448. | Copy constructor.                                                            |
  449. ------------------------------------------------------------------------------*/
  450. Customer :: Customer ( const Customer &cnrobj )  :
  451.             IContainerObject ( (const IContainerObject &)cnrobj ),
  452.             strName ( cnrobj.name() ),
  453.             strAddress ( cnrobj.address() ),
  454.             strPhone ( cnrobj.phone() ),
  455.             ulIconId ( cnrobj.iconId() ),
  456.             myWin ( cnrobj.myWin )
  457. {
  458. }
  459.  
  460. /*------------------------------------------------------------------------------
  461. | Custoner::Customer                                                           |
  462. |                                                                              |
  463. | Constructor.                                                                 |
  464. ------------------------------------------------------------------------------*/
  465. Customer :: Customer ( const IString &Text,
  466.                        unsigned long Icon,
  467.                        const IString &Name,
  468.                        const IString &Address,
  469.                        const IString &Phone,
  470.                        MyWindow* win )  :
  471.                        IContainerObject ( Text, Icon ),
  472.                        strName ( Name ),
  473.                        strAddress ( Address ),
  474.                        strPhone ( Phone ),
  475.                        ulIconId ( Icon ),
  476.                        myWin ( win )
  477. {
  478. }
  479.  
  480. /*------------------------------------------------------------------------------
  481. | Customer::objectCopy                                                         |
  482. |                                                                              |
  483. | Make a copy of the Customer object.  Called by                               |
  484. | IContainerObject::copyObjectTo().                                            |
  485. ------------------------------------------------------------------------------*/
  486. IContainerObject* Customer :: objectCopy()
  487. {
  488.   /***********************************************************************/
  489.   /* Use Customer copy constructor to make a copy of the object.         */
  490.   /***********************************************************************/
  491.   Customer *copy = new Customer(*this);
  492.   return((IContainerObject *)copy);
  493. }
  494.  
  495.  
  496.