home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / drgwps.zip / drag.c < prev    next >
C/C++ Source or Header  |  1993-08-02  |  15KB  |  326 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  drag.c                 AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  08-01-93                                           *
  5.  *                                                                   *
  6.  * MODULE DESCRIPTION:                                               *
  7.  *                                                                   *
  8.  *  Part of the 'DRGPMWPS' drag/drop sample program.                 *
  9.  *                                                                   *
  10.  *  This module handles all the Drag/Drop processing for the         *
  11.  *  DRGPMWPS.EXE sample program.                                     *
  12.  *                                                                   *
  13.  * NOTES:                                                            *
  14.  *                                                                   *
  15.  *                                                                   *
  16.  * FUNCTIONS AVALABLE TO OTHER MODULES:                              *
  17.  *                                                                   *
  18.  *   dragInit                                                        *
  19.  *   dragOver                                                        *
  20.  *   dragDrop                                                        *
  21.  *                                                                   *
  22.  *                                                                   *
  23.  * HISTORY:                                                          *
  24.  *                                                                   *
  25.  *  08-01-93 - Program coded.                                        *
  26.  *                                                                   *
  27.  *  Rick Fishman                                                     *
  28.  *  Code Blazers, Inc.                                               *
  29.  *  4113 Apricot                                                     *
  30.  *  Irvine, CA. 92720                                                *
  31.  *  CIS ID: 72251,750                                                *
  32.  *                                                                   *
  33.  *********************************************************************/
  34.  
  35. #pragma strings(readonly)   // used for debug version of memory mgmt routines
  36.  
  37. /*********************************************************************/
  38. /*------- Include relevant sections of the OS/2 header files --------*/
  39. /*********************************************************************/
  40.  
  41. #define  INCL_WINDIALOGS
  42. #define  INCL_WINERRORS
  43. #define  INCL_WINFRAMEMGR
  44. #define  INCL_WININPUT
  45. #define  INCL_WINPOINTERS
  46. #define  INCL_WINSTDCNR
  47. #define  INCL_WINSTDDRAG
  48. #define  INCL_WINWINDOWMGR
  49.  
  50. /**********************************************************************/
  51. /*----------------------------- INCLUDES -----------------------------*/
  52. /**********************************************************************/
  53.  
  54. #include <os2.h>
  55. #include <stdarg.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include "drgpmwps.h"
  60. #include "common.h"
  61.  
  62. /*********************************************************************/
  63. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  64. /*********************************************************************/
  65.  
  66.  
  67. /**********************************************************************/
  68. /*---------------------------- STRUCTURES ----------------------------*/
  69. /**********************************************************************/
  70.  
  71.  
  72. /**********************************************************************/
  73. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  74. /**********************************************************************/
  75.  
  76. BOOL SetDragItem( HWND hwndFrame, PCNRREC pCnrRec, PDRAGINFO pDragInfo,
  77.                   PDRAGIMAGE pDragImage );
  78.  
  79. /**********************************************************************/
  80. /*------------------------ GLOBAL VARIABLES --------------------------*/
  81. /**********************************************************************/
  82.  
  83.  
  84. /**********************************************************************/
  85. /*----------------------------- dragInit -----------------------------*/
  86. /*                                                                    */
  87. /*  PROCESS CN_INITDRAG NOTIFY MESSAGE.                               */
  88. /*                                                                    */
  89. /*  PARMS: frame window handle,                                       */
  90. /*         pointer to the CNRDRAGINIT structure                       */
  91. /*                                                                    */
  92. /*  NOTES:                                                            */
  93. /*                                                                    */
  94. /*  RETURNS: nothing                                                  */
  95. /*                                                                    */
  96. /*--------------------------------------------------------------------*/
  97. /**********************************************************************/
  98. void dragInit( HWND hwndFrame, PCNRDRAGINIT pcdi )
  99. {
  100.     PCNRREC    pCnrRec   = (PCNRREC) pcdi->pRecord;
  101.     DRAGIMAGE  DragImage;
  102.     PDRAGINFO  pDragInfo = NULL;
  103.  
  104.     if( pCnrRec )
  105.     {
  106.         pDragInfo = DrgAllocDraginfo( 1 );
  107.  
  108.         if( pDragInfo )
  109.         {
  110.             // It is necessary to set this to DO_DEFAULT so that the WPS
  111.             // methods wpDraggedOverObject and wpDroppedOnObject work.
  112.  
  113.             pDragInfo->usOperation = DO_DEFAULT;
  114.  
  115.             memset( &DragImage, 0, sizeof DragImage );
  116.  
  117.             if( SetDragItem( hwndFrame, pCnrRec, pDragInfo, &DragImage ) )
  118.             {
  119.                 // Give the WPS object's object window our window handle so if
  120.                 // something is dropped on a WPS object it can let us know
  121.                 // about it.
  122.  
  123.                 if( pCommData && pCommData->hwndComm )
  124.                     WinSendMsg( pCommData->hwndComm, UM_STARTING_DRAG,
  125.                                 MPFROMHWND( hwndFrame ), NULL );
  126.  
  127.  
  128.                 // Turn source emphasis on for this container record.
  129.                 if( !WinSendDlgItemMsg( hwndFrame, FID_CLIENT,
  130.                                         CM_SETRECORDEMPHASIS,
  131.                                         MPFROMP( pCnrRec ),
  132.                                         MPFROM2SHORT( TRUE, CRA_SOURCE ) ) )
  133.                     Msg( "CM_SETRECORDEMPHASIS failed! RC = %X",
  134.                          HWNDERR( hwndFrame ) );
  135.  
  136.                 if( !DrgDrag( hwndFrame, pDragInfo, &DragImage, 1, VK_ENDDRAG,
  137.                               NULL ) )
  138.                 {
  139.                     DrgDeleteDraginfoStrHandles( pDragInfo );
  140.                     DrgFreeDraginfo( pDragInfo );
  141.                 }
  142.  
  143.                 // Remove the container record's source emphasis
  144.                 WinSendDlgItemMsg( hwndFrame, FID_CLIENT, CM_SETRECORDEMPHASIS,
  145.                                    MPFROMP( pCnrRec ),
  146.                                    MPFROM2SHORT( FALSE, CRA_SOURCE ) );
  147.             }
  148.         }
  149.         else
  150.             Msg( "DrgAllocDraginfo failed. RC: %X", HWNDERR( hwndFrame ) );
  151.     }
  152. }
  153.  
  154. /**********************************************************************/
  155. /*--------------------------- SetDragItem ----------------------------*/
  156. /*                                                                    */
  157. /*  SET THE DRAGITEM STRUCT INTO A DRAGINFO STRUCT.                   */
  158. /*                                                                    */
  159. /*  PARMS: frame window handlerecord,                                 */
  160. /*         pointer to container record,                               */
  161. /*         pointer to allocated DRAGINFO struct,                      */
  162. /*         pointer to DRAGIMAGE struct                                */
  163. /*                                                                    */
  164. /*  NOTES:                                                            */
  165. /*                                                                    */
  166. /*  RETURNS: TRUE or FALSE if successful or not                       */
  167. /*                                                                    */
  168. /*--------------------------------------------------------------------*/
  169. /**********************************************************************/
  170. BOOL SetDragItem( HWND hwndFrame, PCNRREC pCnrRec, PDRAGINFO pDragInfo,
  171.                   PDRAGIMAGE pDragImage )
  172. {
  173.     BOOL      fSuccess = TRUE;
  174.     PDRAGITEM pDragItem = HeapAlloc( sizeof( DRAGITEM ) );
  175.  
  176.     if( pDragItem )
  177.     {
  178.         (void) memset( pDragItem, 0, sizeof( DRAGITEM ) );
  179.  
  180.         // Let the WPS object format the DragItem. Only it knows the proper
  181.         // format of the DRAGITEM so that we will be allowed to be dropped on
  182.         // WPS objects.
  183.  
  184.         if( WinSendMsg( pCommData->hwndComm, UM_FORMAT_DRAGITEM,
  185.                         MPFROMP( pDragItem ), NULL ) )
  186.         {
  187.             // The source name and target name are not used so get rid of them.
  188.  
  189.             DrgDeleteStrHandle( pDragItem->hstrSourceName );
  190.             DrgDeleteStrHandle( pDragItem->hstrTargetName );
  191.             pDragItem->hstrSourceName = NULLHANDLE;
  192.             pDragItem->hstrTargetName = NULLHANDLE;
  193.  
  194.             // This is the window handle that DM_ messages will be sent to.
  195.  
  196.             pDragItem->hwndItem       = hwndFrame;
  197.  
  198.             DrgSetDragitem( pDragInfo, pDragItem, sizeof( DRAGITEM ), 0 );
  199.  
  200.             // Free the shared memory since it is no longer needed.
  201.  
  202.             HeapFree( pDragItem );
  203.  
  204.             pDragImage->cb       = sizeof( DRAGIMAGE );
  205.             pDragImage->hImage   = pCnrRec->mrc.hptrIcon;
  206.             pDragImage->fl       = DRG_ICON;
  207.  
  208.             // Save the container record that is being dragged so we have
  209.             // access to it on the drop.
  210.  
  211.             pCnrRecBeingDragged  = pCnrRec;
  212.         }
  213.         else
  214.         {
  215.             fSuccess = FALSE;
  216.             Msg( "UM_FORMAT_DRAGITEM failed!\n" );
  217.         }
  218.     }
  219.     else
  220.     {
  221.         fSuccess = FALSE;
  222.         Msg( "HeapAlloc failed in SetDragItem!\n" );
  223.     }
  224.  
  225.     return fSuccess;
  226. }
  227.  
  228. /**********************************************************************/
  229. /*----------------------------- dragOver -----------------------------*/
  230. /*                                                                    */
  231. /*  PROCESS CN_DRAGOVER NOTIFY MESSAGE.                               */
  232. /*                                                                    */
  233. /*  PARMS: frame window handle,                                       */
  234. /*         pointer to the CNRDRAGINFO structure                       */
  235. /*                                                                    */
  236. /*  NOTES:                                                            */
  237. /*                                                                    */
  238. /*  RETURNS: return value from CN_DRAGOVER processing                 */
  239. /*                                                                    */
  240. /*--------------------------------------------------------------------*/
  241. /**********************************************************************/
  242. MRESULT dragOver( HWND hwndFrame, PCNRDRAGINFO pcdi )
  243. {
  244.     USHORT    usDrop = DOR_NEVERDROP, usDefOp = 0;
  245.     PDRAGINFO pDragInfo = pcdi->pDragInfo;
  246.     PDRAGITEM pDragItem;
  247.  
  248.     if( DrgAccessDraginfo( pDragInfo ) )
  249.     {
  250.         // Don't let us drop on ourself in this simple test program. Normally
  251.         // this would be allowed so the user could move the icon in the
  252.         // container. Not necessary here.
  253.  
  254.         if( pDragInfo->hwndSource != hwndFrame )
  255.         {
  256.             pDragItem = DrgQueryDragitemPtr( pDragInfo, 0 );
  257.  
  258.             // The only items we will let be dropped on us are WPS objects
  259.             // which use the <DRM_OBJECT,DRF_OBJECT> RMF.
  260.  
  261.             if( DrgVerifyRMF( pDragItem, "DRM_OBJECT", "DRF_OBJECT" ) )
  262.             {
  263.                 usDrop  = DOR_DROP;
  264.                 usDefOp = DO_COPY;
  265.             }
  266.         }
  267.  
  268.         DrgFreeDraginfo( pDragInfo );
  269.     }
  270.  
  271.     return MRFROM2SHORT( usDrop, usDefOp );
  272. }
  273.  
  274. /**********************************************************************/
  275. /*---------------------------- dragDrop ------------------------------*/
  276. /*                                                                    */
  277. /*  PROCESS CN_DROP NOTIFY MESSAGE.                                   */
  278. /*                                                                    */
  279. /*  PARMS: frame window handle,                                       */
  280. /*         pointer to the CNRDRAGINFO structure                       */
  281. /*                                                                    */
  282. /*  NOTES:                                                            */
  283. /*                                                                    */
  284. /*  RETURNS: nothing                                                  */
  285. /*                                                                    */
  286. /*--------------------------------------------------------------------*/
  287. /**********************************************************************/
  288. void dragDrop( HWND hwndFrame, PCNRDRAGINFO pcdi )
  289. {
  290.     PDRAGINFO pDragInfo = pcdi->pDragInfo;
  291.  
  292.     if( DrgAccessDraginfo( pDragInfo ) )
  293.     {
  294.         PDRAGITEM pDragItem;
  295.         int       i;
  296.  
  297.         if( pCommData && pCommData->hwndComm )
  298.             for( i = 0; i < pDragInfo->cditem; i++ )
  299.             {
  300.                 pDragItem = DrgQueryDragitemPtr( pDragInfo, i );
  301.  
  302.                 // A WPS object was dropped on us. Request information on this
  303.                 // object from our agent object. It will eventually post the
  304.                 // frame window a UM_DRAGINFO message with that information.
  305.  
  306.                 WinSendMsg( pCommData->hwndComm, UM_DRAGINFO_REQUEST,
  307.                             MPFROMLONG( pDragItem->ulItemID ),
  308.                             MPFROMHWND( hwndFrame ) );
  309.  
  310.                 // Let em know we're done so it can erase source emphasis
  311.  
  312.                 WinSendMsg( pCommData->hwndComm, UM_DROP_ENDCONVERSATION,
  313.                             MPFROMLONG( pDragItem->ulItemID ), NULL );
  314.             }
  315.         else
  316.             Msg( "Dont have access to the Drag Agent's window handle!" );
  317.     }
  318.  
  319.     DrgDeleteDraginfoStrHandles( pDragInfo );
  320.     DrgFreeDraginfo( pDragInfo );
  321. }
  322.  
  323. /*************************************************************************
  324.  *                     E N D     O F     S O U R C E                     *
  325.  *************************************************************************/
  326.