home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DRAGIN.ZIP / DRAGINFO.C < prev    next >
Text File  |  1993-02-14  |  18KB  |  471 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /*  PROGRAM NAME:  DRAGINFO                                                  */
  4. /*                                                                           */
  5. /*  PURPOSE:       This program provides information for testing and         */
  6. /*                 debugging drag-and-drop applications.  When an object is  */
  7. /*                 dragged from this program's window, the window shows the  */
  8. /*                 messages received from the target.                        */
  9. /*                 Additionally, the Draginfo and Dragitem-structures that   */
  10. /*                 are used to specify the requested operation can be        */
  11. /*                 configured on screen.It is also possible to select the    */
  12. /*                 desired Drag-API (DrgDrag or DrgDragFiles).               */
  13. /*                 This program only simulates the dragging; no explicit     */
  14. /*                 rendering is performed (yet).                             */
  15. /*                                                                           */
  16. /*                 This program was created using code from the DROPINFO     */
  17. /*                 sample program that comes with the Redbook Vol.4 sample   */
  18. /*                 code. Both programs complement eachother.                 */
  19. /*                                                                           */
  20. /*                 As far as I'm concerned you may use this code in whatever */
  21. /*                 way you choose, provided I'm in no way responsible for    */
  22. /*                 the outcome. Good luck.                                   */
  23. /*                                                                           */
  24. /*                 Christian Sell, Germany CIS 100021,3151                   */
  25. /*****************************************************************************/
  26.  
  27. #include "draginfo.h"                            /* Application header file  */
  28.  
  29.  
  30. /*****************************************************************************/
  31. /* Window procedure prototypes                                               */
  32. /*****************************************************************************/
  33. MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  34. MRESULT EXPENTRY wpSubList( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  35.  
  36. /*****************************************************************************/
  37. /* Function prototypes                                                       */
  38. /*****************************************************************************/
  39. void PutMsg( USHORT unSrc, ULONG msg, MPARAM mp1, MPARAM mp2);
  40. HWND DoDrgDrag( HWND hwndSrc );
  41. void DoDragFiles( HWND hwnd );
  42.  
  43. /*****************************************************************************/
  44. /* Global data                                                               */
  45. /*****************************************************************************/
  46. HAB  hAB;                       /* Anchor block handle      */
  47. PFNWP pwpList;                  /* Listbox winproc address  */
  48. HWND  hwndTarget;
  49. HWND  hListBox;                 /* Listbox window handle    */
  50. HPOINTER  hptrDrag;                 /* Drag Pointer handle */
  51.  
  52. /*****************************************************************************/
  53. /* Application main routine                                                  */
  54. /*****************************************************************************/
  55. INT main (VOID)
  56. {
  57.   HMQ  hMsgQ;                                    /* Message queue handle     */
  58.   QMSG qMsg;                                     /* Message structure        */
  59.   HWND hFrame;                                   /* Frame window handle      */
  60.  
  61.   ULONG flCreate = FCF_STANDARD;     /* Frame creation flags     */
  62.  
  63.   ULONG rc;                                      /* Return code              */
  64.  
  65.   hAB = WinInitialize(0);                        /* Register application     */
  66.  
  67.   hMsgQ = WinCreateMsgQueue(hAB, 0);             /* Create message queue     */
  68.  
  69.   rc = WinRegisterClass(hAB,                     /* Register window class    */
  70.                         (PSZ)"MyWindow",         /* Class name               */
  71.                         (PFNWP)MyWindowProc,     /* Window procedure address */
  72.                         CS_SIZEREDRAW,           /* Class style              */
  73.                         sizeof(PVOID));          /* Window words             */
  74.  
  75.   hFrame = WinCreateStdWindow(HWND_DESKTOP,      /* Desktop is parent        */
  76.                               0,                 /* Standard window style    */
  77.                               &flCreate,         /* Frame control flags      */
  78.                               "MyWindow",        /* Window class name        */
  79.                               "DragInfo",        /* Window title text        */
  80.                               0,                 /* No special class style   */
  81.                               (HMODULE)0L,       /* Resources in EXE file    */
  82.                               ID_WINDOW,         /* Frame window identifier  */
  83.                               NULL);             /* No pres params           */
  84.  
  85.   while (WinGetMsg(hAB, &qMsg, 0L, 0, 0))        /* Process messages until   */
  86.         WinDispatchMsg(hAB, &qMsg);              /* WM_QUIT received         */
  87.  
  88.   WinDestroyWindow(hFrame);                      /* Destroy window           */
  89.   WinDestroyMsgQueue(hMsgQ);                     /* Destroy message queue    */
  90.   WinTerminate(hAB);                             /* Deregister application   */
  91.   
  92.   return (rc);
  93. }
  94.  
  95. /*****************************************************************************/
  96. /* Main window procedure                                                     */
  97. /*****************************************************************************/
  98. MRESULT EXPENTRY MyWindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  99. {
  100.   SWP   swp;
  101.   ULONG rc;
  102.   HWND  hFrame, hwndMenu;
  103.  
  104.   switch (msg)
  105.   {
  106.   case WM_CREATE:
  107.        hListBox = WinCreateWindow(hwnd,
  108.                                   WC_LISTBOX,
  109.                                   NULL,
  110.                                   WS_VISIBLE     |
  111.                                   LS_NOADJUSTPOS |
  112.                                   LS_HORZSCROLL,
  113.                                   0, 0, 0, 0,
  114.                                   hwnd,
  115.                                   HWND_TOP,
  116.                                   ID_LISTBOX,
  117.                                   0,
  118.                                   0);
  119.  
  120.        pwpList = WinSubclassWindow(hListBox,
  121.                     wpSubList);
  122.  
  123.        hFrame = WinQueryWindow(hwnd,
  124.                                QW_PARENT);
  125.        rc = WinSetWindowPos(hFrame,
  126.                             HWND_TOP,
  127.                             50, 250,
  128.                             300, 200,
  129.                             SWP_MOVE     |
  130.                             SWP_SIZE     |
  131.                             SWP_ACTIVATE |
  132.                             SWP_SHOW);
  133.        hptrDrag = WinLoadPointer( HWND_DESKTOP, NULLHANDLE, ID_DRAGPTR);
  134.                             
  135.        break;
  136.  
  137.   case WM_SIZE:
  138.        WinQueryWindowPos(hwnd, &swp);
  139.        hListBox=WinWindowFromID(hwnd,
  140.                                 ID_LISTBOX);
  141.        WinSetWindowPos(hListBox,
  142.                        HWND_TOP,
  143.                        swp.x, swp.y,
  144.                        swp.cx, swp.cy,
  145.                        SWP_SIZE  |
  146.                        SWP_SHOW);
  147.        break;
  148.  
  149.   /* just record drag messages sent to client window 
  150.   */
  151.   case DM_DROP:
  152.   case DM_DRAGOVER:
  153.   case DM_DRAGLEAVE:
  154.   case DM_DROPHELP:
  155.   case DM_ENDCONVERSATION:
  156.   case DM_PRINT:
  157.   case DM_RENDER:
  158.   case DM_RENDERCOMPLETE:
  159.   case DM_RENDERPREPARE:
  160.   case DM_DRAGFILECOMPLETE:
  161.   case DM_EMPHASIZETARGET:
  162.   case DM_DRAGERROR:
  163.   case DM_FILERENDERED:
  164.   case DM_RENDERFILE:
  165.   case DM_DRAGOVERNOTIFY:
  166.   case DM_PRINTOBJECT:
  167.   case DM_DISCARDOBJECT:
  168.     PutMsg( 0, msg, mp1, mp2 );
  169.     return (MRESULT)FALSE;
  170.     break;
  171.  
  172.   case WM_MENUSELECT:
  173.     switch( SHORT1FROMMP(mp1) )
  174.     {
  175.     case ID_OPTIONS:
  176.       WinCheckMenuItem(HWNDFROMMP(mp2), IDM_DRGDRAG, bDrgDrag);
  177.       WinCheckMenuItem(HWNDFROMMP(mp2), IDM_DRGDRAGFILES, !bDrgDrag);
  178.       return (MRESULT)FALSE;
  179.  
  180.     default:
  181.       return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  182.     }
  183.   case WM_COMMAND:
  184.     if( SHORT1FROMMP(mp2) == CMDSRC_MENU )
  185.     {
  186.       hwndMenu = WinWindowFromID(hFrame, FID_MENU);
  187.       switch( SHORT1FROMMP(mp1) )
  188.       {
  189.       case IDM_DRGDRAG:
  190.         bDrgDrag = TRUE;
  191.         WinCheckMenuItem(hwndMenu, IDM_DRGDRAG, bDrgDrag);
  192.         WinCheckMenuItem(hwndMenu, IDM_DRGDRAGFILES, !bDrgDrag);
  193.         return (MRESULT)FALSE;
  194.   
  195.       case IDM_DRGDRAGFILES:
  196.         bDrgDrag = FALSE;
  197.         WinCheckMenuItem(hwndMenu, IDM_DRGDRAG, bDrgDrag);
  198.         WinCheckMenuItem(hwndMenu, IDM_DRGDRAGFILES, !bDrgDrag);
  199.         return (MRESULT)FALSE;
  200.   
  201.       case IDM_CONFIGDLG:
  202.         if( bDrgDrag == TRUE )
  203.           WinDlgBox(HWND_DESKTOP, hwnd, wpConfDrgDrag, NULLHANDLE,
  204.                     IDD_CONFIG1, NULL);
  205.         else
  206.           WinDlgBox(HWND_DESKTOP, hwnd, wpConfDrgDragFiles, NULLHANDLE,
  207.                     IDD_CONFIG2, NULL);
  208.         return (MRESULT)TRUE;
  209.     
  210.       case IDM_CLEARLIST:
  211.         WinSendMsg( hListBox, LM_DELETEALL, (MPARAM)0, (MPARAM)0 );
  212.         return((MRESULT)TRUE);
  213.     
  214.       case IDM_DOSOMETHING:
  215.         if( WinMessageBox(HWND_DESKTOP, hwnd, "OK, so do something else!",
  216.                        "Message", 0, MB_OKCANCEL ) == MBID_OK )
  217.           WinPostMsg( hwnd, WM_QUIT, (MPARAM)0, (MPARAM)0 );
  218.         return((MRESULT)TRUE);
  219.       }
  220.     }
  221.     break;
  222.  
  223.   default:
  224.        return(WinDefWindowProc(hwnd,
  225.                                msg,
  226.                                mp1,
  227.                                mp2));
  228.   }
  229.   return((MRESULT)FALSE);
  230. }
  231.  
  232. /*****************************************************************************/
  233. /* Listbox subclass window procedure                                         */
  234. /*****************************************************************************/
  235. MRESULT EXPENTRY wpSubList(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  236. {
  237.   switch (msg)
  238.   {
  239.   /***********************/
  240.   /*Drag-Initialisierung */
  241.   /***********************/
  242.   case WM_BEGINDRAG:
  243.     if( bDrgDrag == TRUE )
  244.     {
  245.       hwndTarget = DoDrgDrag( hwnd );
  246.       return (MRESULT)(hwndTarget ? TRUE : FALSE);
  247.     }
  248.     else
  249.     {
  250.       DoDragFiles( hwnd );
  251.       return (MRESULT)TRUE;
  252.     }
  253.     break;
  254.   /****************************************/
  255.   /* messages to drag source window       */
  256.   /****************************************/
  257.   case DM_DRAGOVERNOTIFY:   /* Objekt über anderem window */
  258.                             /* mp1: Draginfo, mp2: Target indicators */
  259.     /*PutMsg( 1, msg, mp1, mp2 );*/
  260.     return (MRESULT)FALSE;
  261.     break;
  262.   case DM_RENDER:           /* Render-Anforderung */
  263.     /* mp1: DragTransfer, return: success/error */
  264.     PutMsg( 1, msg, mp1, mp2 );
  265.     return (MRESULT)FALSE;
  266.     break;
  267.   case DM_PRINTOBJECT:      /* Print-Anforderung */
  268.     /* mp1: Draginfo, mp2: PrintDest, returns: action */
  269.     PutMsg( 1, msg, mp1, mp2 );
  270.     return (MRESULT)usDrgReturn;      
  271.     break;
  272.   case DM_DISCARDOBJECT:    /* Discard-Anforderung */
  273.     /* mp1: Draginfo, mp2: reserved, returns: action */
  274.     PutMsg( 1, msg, mp1, mp2 );
  275.     return (MRESULT)usDrgReturn;  
  276.     break;
  277.   case DM_ENDCONVERSATION:  /* Ende-Meldung */
  278.     /* mp1: ItemId, mp2: success/fail */ 
  279.     PutMsg( 1, msg, mp1, mp2 );
  280.     return (MRESULT)FALSE;
  281.     break;
  282.   /*****************************/
  283.   /* messages to target window */
  284.   /*****************************/
  285.   case DM_DRAGOVER:
  286.     /* mp1: Draginfo, mp2: Koordinaten, return: indicator */
  287.     PutMsg( 1, msg, mp1, mp2 );
  288.     return(MPFROM2SHORT(DOR_NEVERDROP, DO_UNKNOWN));
  289.     break;
  290.   case DM_EMPHASIZETARGET:
  291.     /* mp1: Koordinaten, mp2: Flag */
  292.   case DM_DRAGLEAVE:
  293.     /* mp1: Draginfo */
  294.   case DM_DROP:
  295.     /* mp1: Draginfo, return: indicator */
  296.   /**********************************/
  297.   /*Messages für DrgDragFiles usw.. */
  298.   /**********************************/
  299.   case DM_DRAGERROR:
  300.     /* nur DrgDragFiles.. */
  301.   case DM_FILERENDERED:
  302.     /* mp1: Renderfile, mp2: success/fail */
  303.   case DM_DRAGFILECOMPLETE:
  304.     /* nur DrgDragFiles.. */
  305.     PutMsg( 1, msg, mp1, mp2 );
  306.     return (MRESULT)FALSE;
  307.     break;
  308.  
  309.   } /* endswitch */
  310.  
  311.   return((MRESULT)pwpList(hwnd, msg, mp1, mp2));
  312. }
  313.  
  314.  
  315. /*************************************************************************/
  316. /* fhis function performs the drag ...                                   */
  317. /*************************************************************************/
  318. HWND DoDrgDrag( HWND hwndSrc )
  319. {
  320.   PDRAGINFO     pDInfo;
  321.   DRAGITEM      DItem;
  322.   DRAGIMAGE     DImage;
  323.   HWND          hDrop=NULLHANDLE;
  324.   APIRET        rc;
  325.  
  326.   pDInfo = DrgAllocDraginfo(1);  /* Allocate DRAGINFO     */
  327.   pDInfo->usOperation = usDrgOperation;
  328.  
  329.   /* Initialize DRAGITEM   */ 
  330.   DItem.hwndItem            = hwndSrc;         
  331.   DItem.ulItemID            = (ULONG)55;       /* nothing special */
  332.   DItem.hstrType            = DrgAddStrHandle( szDrgType );
  333.   DItem.hstrRMF             = DrgAddStrHandle( szDrgRMF );
  334.   DItem.hstrContainerName   = DrgAddStrHandle( szPath );
  335.   DItem.hstrSourceName      = DrgAddStrHandle( szFile );
  336.   DItem.hstrTargetName      = DrgAddStrHandle( szFile );
  337.   DItem.fsControl           = usDrgControl;
  338.   DItem.cxOffset            = 2;
  339.   DItem.cyOffset            = 2;
  340.   DItem.fsSupportedOps      = DO_COPYABLE|DO_MOVEABLE|DO_LINKABLE;
  341.  
  342.   rc = DrgSetDragitem(pDInfo,    /* Set item in DRAGINFO  */
  343.           &DItem,                /* Pointer to DRAGITEM   */
  344.           sizeof(DItem),         /* Size of DRAGITEM      */
  345.           0);                    /* Index of DRAGITEM     */
  346.  
  347.   DImage.cb = sizeof(DRAGIMAGE); /* Initialize DRAGIMAGE  */
  348.   DImage.cptl = 0;               /* Not a polygon         */
  349.   DImage.hImage = hptrDrag;      /* Icon handle for drag  */
  350.   DImage.fl = DRG_ICON | DRG_TRANSPARENT;
  351.   DImage.cxOffset = 0;           /* No hotspot            */
  352.   DImage.cyOffset = 0;
  353.  
  354.   /*****************************/
  355.   /* now do the drag           */
  356.   /*****************************/
  357.   hDrop = DrgDrag(hwndSrc,          /* Initiate drag         */
  358.              pDInfo,             /* DRAGINFO structure    */
  359.              &DImage,            /* DRAGIMAGE structure  */
  360.              1,                  /* Only one DRAGIMAGE    */
  361.              VK_ENDDRAG,         /* End of drag indicator */
  362.              NULL);              /* Reserved              */
  363.  
  364.   DrgDeleteDraginfoStrHandles(pDInfo);
  365.   DrgFreeDraginfo(pDInfo);       /* Free DRAGINFO struct  */
  366.  
  367.   return hDrop;
  368. }
  369.  
  370.  
  371. /**********************************************************/
  372. /*this function performs the drag via the DrgDragFiles API*/
  373. /**********************************************************/
  374. void DoDragFiles( HWND hwnd )
  375. {
  376.   BOOL     fSuccess;
  377.   PSZ      pFiles[1];
  378.   PSZ      pTargets[1];
  379.  
  380.   pFiles[0] = szFilePath;
  381.   pTargets[0] = NULL;
  382.  
  383.   if( !DrgDragFiles(hwnd, pFiles, NULL, pTargets, 1,
  384.                    hptrDrag, VK_BUTTON2, bSourceRender, 0L) )
  385.     PutMsg( 2, (ULONG)"DrgDragFiles failed", NULL, NULL );
  386.   else
  387.     PutMsg( 2, (ULONG)"DrgDragFiles OK", NULL, NULL );
  388. }
  389.  
  390.  
  391. /*****************************************************************************/
  392. /* Put message info in listbox item                                          */
  393. /*****************************************************************************/
  394. void PutMsg( USHORT unSrc, ULONG msg, MPARAM mp1, MPARAM mp2)
  395. {
  396.   char buf[100];                 /* Message buffer */
  397.  
  398.   switch( unSrc )
  399.   {
  400.   case 0:
  401.     strcpy( buf, "message to client window: " );
  402.     break;
  403.   case 1:
  404.     strcpy( buf, "message to listbox: " );
  405.     break;
  406.   default:
  407.     strcpy( buf, "general: " );
  408.     strcat( buf, (char *)msg );
  409.   }
  410.  
  411.   switch( msg )
  412.   {
  413.   case DM_DROP:
  414.     strcat( buf, "DM_DROP" );
  415.     break;
  416.   case DM_DRAGOVER:
  417.     strcat( buf, "DM_DRAGOVER" );
  418.     break;
  419.   case DM_DRAGLEAVE:
  420.     strcat( buf, "DM_DRAGLEAVE" );
  421.     break;
  422.   case DM_DROPHELP:
  423.     strcat( buf, "DM_DROPHELP" );
  424.     break;
  425.   case DM_ENDCONVERSATION:
  426.     strcat( buf, "DM_ENDCONVERSATION" );
  427.     break;
  428.   case DM_PRINT:
  429.     strcat( buf, "DM_PRINT" );
  430.     break;
  431.   case DM_RENDER:
  432.     strcat( buf, "DM_RENDER" );
  433.     break;
  434.   case DM_RENDERCOMPLETE:
  435.     strcat( buf, "DM_RENDERCOMPLETE" );
  436.     break;
  437.   case DM_RENDERPREPARE:
  438.     strcat( buf, "DM_RENDERPREPARE" );
  439.     break;
  440.   case DM_DRAGFILECOMPLETE:
  441.     strcat( buf, "DM_DRAGFILECOMPLETE" );
  442.     break;
  443.   case DM_EMPHASIZETARGET:
  444.     strcat( buf, "DM_EMPHASIZETARGET" );
  445.     break;
  446.   case DM_DRAGERROR:
  447.     strcat( buf, "DM_DRAGERROR" );
  448.     break;
  449.   case DM_FILERENDERED:
  450.     strcat( buf, "DM_FILERENDERED" );
  451.     break;
  452.   case DM_RENDERFILE:
  453.     strcat( buf, "DM_RENDERFILE" );
  454.     break;
  455.   case DM_DRAGOVERNOTIFY:
  456.     strcat( buf, "DM_DRAGOVERNOTIFY" );
  457.     break;
  458.   case DM_PRINTOBJECT:
  459.     strcat( buf, "DM_PRINTOBJECT" );
  460.     break;
  461.   case DM_DISCARDOBJECT:
  462.     strcat( buf, "DM_DISCARDOBJECT" );
  463.     break;
  464.   }
  465.   WinSendMsg( hListBox, LM_INSERTITEM, (MPARAM)LIT_END, MPFROMP(buf));
  466.  
  467.   return;
  468. }
  469.  
  470.  
  471.