home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / draglb.zip / DRAGLB.CPP < prev    next >
Text File  |  1994-06-08  |  14KB  |  283 lines

  1. #include <ilistbox.hpp>
  2. #include <idmsrcop.hpp>
  3. #include <idmimage.hpp>
  4. #include "draglb.hpp"
  5. #include "operfix.hpp"
  6.  
  7. /*------------------------------------------------------------------------------
  8. | LBItem::LBItem                                                               |
  9. |                                                                              |
  10. | Constructor for source LB drag item.  Note that we only allow moves.         |
  11. ------------------------------------------------------------------------------*/
  12. LBItem::LBItem( IDMSourceOperation *pSrcOp, IString &strListItem )  :
  13.                    IDMItem( pSrcOp,
  14.                             IDM::text,
  15.                             (IDMItem::moveable | IDMItem::copyable),
  16.                             none )
  17. {
  18.   setContents( strListItem );
  19.  
  20.   /********************************************************************/
  21.   /* Set the native rendering mechanism and format for the item       */
  22.   /*                                                                  */
  23.   /* Note how we always use the process rendering format to optimize  */
  24.   /* rendering if we're in the same process.                          */
  25.   /********************************************************************/
  26.   IString tempStr = generateSourceName();
  27.   if( tempStr.size() )
  28.   {
  29.     /******************************************************************/
  30.     /* If the text is less than 255 characters and does not contain   */
  31.     /* embedded NULL characters, we can use the text rendering        */
  32.     /* format on an inter-process move/copy operation, which allows   */
  33.     /* target rendering to occur (i.e. target takes control of the    */
  34.     /* operation after the drop and does not require intervention     */
  35.     /* from the source)                                               */
  36.     /******************************************************************/
  37.     setSourceName(tempStr);
  38.     setRMFs( rmfsFrom( IDM::rmLibrary,
  39.                        rfForThisProcess() + "," + IDM::rfText ) );
  40.   }
  41.   else
  42.   {
  43.     /******************************************************************/
  44.     /* The text is greater than or equal to 255 characters or contains*/
  45.     /* embedded NULL characters.  Therefore, we must use the shared   */
  46.     /* memory rendering format on an inter-process move/copy          */
  47.     /* operation, whereas source rendering will occur (i.e. target    */
  48.     /* requires intervention from the source to complete the move/    */
  49.     /* copy operation after the drop occurs).  Note that we require   */
  50.     /* rendering preparation to help determine the size of the shared */
  51.     /* memory buffer.                                                 */
  52.     /******************************************************************/
  53.     setRMFs( rmfsFrom( IDM::rmLibrary,
  54.                        rfForThisProcess() + "," + IDM::rfSharedMem ) );
  55.  
  56.     /******************************************************************/
  57.     /* Use render prepare event to determine size of transfer buffer  */
  58.     /******************************************************************/
  59.     setRequiresPreparation(true);
  60.   }
  61.  
  62.   /********************************************************************/
  63.   /* Add rendering mechanism and format for the shredder              */
  64.   /********************************************************************/
  65.   addRMF( IDM::rmDiscard, IDM::rfUnknown );
  66.  
  67.   /********************************************************************/
  68.   /* Set the system defined text image as the drag image.             */
  69.   /********************************************************************/
  70.   IDMImage image = IDMImage(ISystemPointerHandle(
  71.                             ISystemPointerHandle::text));
  72.   setImage(image);
  73. }
  74.  
  75. /*------------------------------------------------------------------------------
  76. | LBItem::LBItem                                                               |
  77. |                                                                              |
  78. | Constructor for target LB drag item                                          |
  79. ------------------------------------------------------------------------------*/
  80. LBItem::LBItem( const IDMItem::Handle &item )  :
  81.                          IDMItem( item )
  82. {
  83.   /********************************************************************/
  84.   /* Use these rmfs to verify that at least one is supported by the   */
  85.   /* of the drag operation.                                           */
  86.   /********************************************************************/
  87.   IString defaultRMFs = rmfsFrom( IDM::rmLibrary, rfForThisProcess() +
  88.                                                   ","                +
  89.                                                   IDM::rfText        +
  90.                                                   ","                +
  91.                                                   IDM::rfSharedMem);
  92.  
  93.   IString istrRmf = matchingRMFs( rmfsFrom( rmfs().change(' ', "") ),
  94.                                             defaultRMFs );
  95.  
  96.   /********************************************************************/
  97.   /* Therefore, the RMFs that LBItem understands are                  */
  98.   /* (IDM::rmLibrary)x                                                */
  99.   /*           (IDM::rfProcess=pid,IDM::rfText,IDM::sharedMem)        */
  100.   /********************************************************************/
  101.   setRMFs( istrRmf );
  102. }
  103.  
  104. /*------------------------------------------------------------------------------
  105. | LBItem::~LBItem                                                              |
  106. |                                                                              |
  107. | Destructor                                                                   |
  108. ------------------------------------------------------------------------------*/
  109. LBItem::~LBItem()
  110. {
  111. }
  112.  
  113. /*------------------------------------------------------------------------------
  114. | LBItem::generateSourceItems                                                  |
  115. |                                                                              |
  116. | Static function that gives LBItem an opportunity to attach new LBItem's to   |
  117. | the argument IDMSourceOperation object.  This function is called when a drag |
  118. | operation starts over one of our IListBox objects.  Returns true if items    |
  119. | were provided, false otherwise.                                              |
  120. ------------------------------------------------------------------------------*/
  121. Boolean LBItem::generateSourceItems( IDMSourceOperation *pSrcOp )
  122. {
  123.   Boolean bRC = false;
  124.  
  125.   if (pSrcOp)
  126.   {
  127.     /******************************************************************/
  128.     /* Obtain pointer to list box control from the source window      */
  129.     /******************************************************************/
  130.     IListBox *pLB = (IListBox *)pSrcOp->sourceWindow();
  131.  
  132.     if (pLB)
  133.     {
  134.       /****************************************************************/
  135.       /* Do not allow drag if nothing is selected                     */
  136.       /****************************************************************/
  137.       if (pLB->numberOfSelections() != 0)
  138.       {
  139.         /**************************************************************/
  140.         /* Note:  This logic only supports single selection list      */
  141.         /*        boxes.  See the drag4 sample that ships with        */
  142.         /*        C Set++ V2.1 for an example of what needs to        */
  143.         /*        be done to support multiple items (i.e. the         */
  144.         /*        logic is very similar to what we're doing with      */
  145.         /*        a container in drag4).                              */
  146.         /**************************************************************/
  147.         IString strListItem = pLB->itemText( pLB->selection() );
  148.  
  149.         /**************************************************************/
  150.         /* Create a new list box drag item                            */
  151.         /**************************************************************/
  152.         LBItem *pLBItem = new LBItem( pSrcOp, strListItem );
  153.  
  154.         if (pLBItem)
  155.         {
  156.           /************************************************************/
  157.           /* Add it to the source operation object                    */
  158.           /************************************************************/
  159.           pSrcOp->addItem( pLBItem );
  160.  
  161.           /************************************************************/
  162.           /* Set the drag image style                                 */
  163.           /************************************************************/
  164.           pSrcOp->setImageStyle(IDM::allStacked);
  165.  
  166.           bRC = true;
  167.         }
  168.       }
  169.     }
  170.   }
  171.  
  172.   return( bRC );
  173. }
  174.  
  175. /*------------------------------------------------------------------------------
  176. | LBItem::object                                                               |
  177. |                                                                              |
  178. | Returns a pointer to the list item text.                                     |
  179. ------------------------------------------------------------------------------*/
  180. void *LBItem::object() const
  181. {
  182.   return (void *)&(this->strContents);
  183. }
  184.  
  185. /*------------------------------------------------------------------------------
  186. | LBItem::targetDrop                                                           |
  187. |                                                                              |
  188. | Inserts list box item into the target list box.  Note that item is added     |
  189. | to the end of the list.                                                      |
  190. ------------------------------------------------------------------------------*/
  191. Boolean LBItem::targetDrop( IDMTargetDropEvent & )
  192. {
  193.   IListBox *pLB = (IListBox *)this->targetOperation()->targetWindow();
  194.  
  195.   if (pLB)
  196.     pLB->addAsLast( contents() );
  197.  
  198.   return( true );
  199. }
  200.  
  201. /*------------------------------------------------------------------------------
  202. | LBItem::sourceEnd                                                            |
  203. |                                                                              |
  204. | Remove source list item to complete the move operation.                      |
  205. ------------------------------------------------------------------------------*/
  206. Boolean LBItem :: sourceEnd(IDMSourceEndEvent &event)
  207. {
  208.   if (event.wasTargetSuccessful())
  209.   {
  210.     if (OperFix::dropOperation( event ) == IDMOperation::move)
  211.       return( deleteItem((IListBox *)event.dragItem()->sourceWindow()) );
  212.   }
  213.   return( true );
  214. }
  215.  
  216. /*------------------------------------------------------------------------------
  217. | LBItem::sourceDiscard                                                        |
  218. |                                                                              |
  219. | Remove list box item that was dropped on the shredder.              |
  220. ------------------------------------------------------------------------------*/
  221. Boolean LBItem::sourceDiscard( IDMSourceDiscardEvent &event )
  222. {
  223.   return( deleteItem((IListBox *)event.sourceOperation()->sourceWindow()) );
  224. }
  225.  
  226. /*------------------------------------------------------------------------------
  227. | LBItem::deleteItem                                                           |
  228. |                                                                              |
  229. | Delete selected list box item.                                               |
  230. ------------------------------------------------------------------------------*/
  231. Boolean LBItem::deleteItem( IListBox *pLB )
  232. {
  233.   if (pLB)
  234.   {
  235.     long lItem = pLB->selection();
  236.     if (lItem == -1)
  237.       return( false );
  238.  
  239.     pLB->remove( lItem );
  240.     return( true );
  241.   }
  242.  
  243.   return( false );
  244. }
  245.  
  246. /*------------------------------------------------------------------------------
  247. | LBItem::provideEnterSupport                                                  |
  248. |                                                                              |
  249. | Perform 2 checks:                                                            |
  250. |   1) Prevent a drop in the same window.                                      |
  251. |   2) Verify that we understand the type of the drag item during target enter |
  252. |      event processing.                                                       |
  253. ------------------------------------------------------------------------------*/
  254. LBProvider::provideEnterSupport( IDMTargetEnterEvent &event )
  255. {
  256.   IDMTargetOperation::Handle pTgtOpH = IDMTargetOperation::targetOperation();
  257.  
  258.   /********************************************************************/
  259.   /* If the source and target window are the same, we want to         */
  260.   /* prevent a drop.                                                  */
  261.   /********************************************************************/
  262.   if (pTgtOpH->sourceWindowHandle() == pTgtOpH->targetWindowHandle())
  263.   {
  264.     event.setDropIndicator( IDM::neverOk );
  265.     return( false );
  266.   }
  267.  
  268.   /********************************************************************/
  269.   /* Verify types.                                                    */
  270.   /********************************************************************/
  271.   IString strTypes = pTgtOpH->item(1)->types();
  272.  
  273.   if ((strTypes.indexOf( IDM::plainText ) == 0)  &&
  274.       (strTypes.indexOf( IDM::text ) == 0))
  275.   {
  276.     event.setDropIndicator( IDM::notOk );
  277.     return( false );
  278.   }
  279.  
  280.   return( true );
  281. }
  282.  
  283.