home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / flist.zip / drag.c next >
C/C++ Source or Header  |  1996-09-12  |  11KB  |  343 lines

  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  drag.c
  4.  
  5.  'Fast List' drag and drop
  6. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  7. */
  8. #define INCL_DOS
  9. #define INCL_PM
  10. #define INCL_SPLDOSPRINT
  11. #include <os2.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <process.h>
  16. #include "list.h"
  17. #include "flwin.h"
  18. #include "menu.h"
  19. #include "drag.h"
  20. #include "util.h"
  21. #include "res.h"
  22.  
  23. /*--------------------------------Macros-----------------------------------*/
  24.  
  25. /*-------------------------Function Declarations---------------------------*/
  26. static void CleanUpDrag( void );
  27.  
  28. /*-------------------------------Globals-----------------------------------*/
  29. static DRAGINFO *DragInfo;
  30.  
  31. char  FileName[CCHMAXPATHCOMP];
  32. char *DefaultFileName = "list.txt";
  33. char *RMF = "(DRM_OS2FILE,DRM_PRINT,DRM_DISCARD)X(DRF_TEXT)";
  34.  
  35.  
  36. /*-------------------------------Code Start--------------------------------*/
  37.  
  38.  
  39. /*---------------------------------------------------------------------------
  40.                               ActMsgBeginDrag
  41. ---------------------------------------------------------------------------*/
  42. MRESULT ActMsgBeginDrag( WNDATTR *wndattr )
  43. {
  44.  DRAGITEM  dragitem;
  45.  DRAGIMAGE dragimage;
  46.  HWND      hwndTarget;
  47.  int       marked;
  48.  
  49.   DragInfo = DrgAllocDraginfo( 1 );
  50.   DragInfo->hwndSource = wndattr->Client;
  51.  
  52.   dragitem.hwndItem = wndattr->Client;
  53.   dragitem.ulItemID = 1;
  54.   dragitem.hstrType = DrgAddStrHandle( DRT_TEXT );
  55.   dragitem.hstrRMF = DrgAddStrHandle( RMF );
  56.   dragitem.hstrTargetName = DrgAddStrHandle( DefaultFileName );
  57.   dragitem.hstrContainerName = NULLHANDLE;
  58.   dragitem.hstrSourceName = NULLHANDLE;
  59.   dragitem.fsControl = 0;
  60.   dragitem.fsSupportedOps = DO_COPYABLE | DO_MOVEABLE;
  61.  
  62.   DrgSetDragitem( DragInfo, &dragitem, sizeof dragitem, 0 );
  63.  
  64.   dragimage.cb = sizeof dragimage;
  65.   dragimage.cxOffset = 0;
  66.   dragimage.cyOffset = 0;
  67.   dragimage.fl = DRG_ICON | DRG_STRETCH;
  68.  
  69.   marked = (LPListMarkedLines( wndattr->list ) != 0);
  70.   dragimage.sizlStretch.cx = marked ? 20 : 32;
  71.   dragimage.sizlStretch.cy = marked ? 20 : 32;
  72.  
  73.   dragimage.hImage = WinLoadPointer( HWND_DESKTOP,
  74.                                      NULLHANDLE,
  75.                                      ID_FLWIN_RES );
  76.  
  77.   hwndTarget = DrgDrag( wndattr->Client,
  78.                         DragInfo,
  79.                         &dragimage,
  80.                         1L,
  81.                         VK_ENDDRAG,
  82.                         NULL );
  83.  
  84.   if (hwndTarget == NULLHANDLE)
  85.      CleanUpDrag();
  86.  
  87.  return MRFROMSHORT( FALSE );
  88. }
  89. /*---------------------------------------------------------------------------
  90.                               ActMsgDragOver
  91. ---------------------------------------------------------------------------*/
  92. MRESULT ActMsgDragOver( MPARAM mp1, MPARAM mp2 )
  93. {
  94.  DRAGITEM *dragitem;
  95.  USHORT    response, action;
  96.  
  97.   DragInfo = (DRAGINFO*)PVOIDFROMMP( mp1 );
  98.   DrgAccessDraginfo( DragInfo );
  99.  
  100.   action = DOR_NODROP;
  101.   if (DragInfo->cditem == 1) {
  102.      if (DragInfo->usOperation == DO_COPY ||
  103.          DragInfo->usOperation == DO_MOVE ||
  104.          DragInfo->usOperation == DO_DEFAULT ) {
  105.          dragitem = DrgQueryDragitemPtr( DragInfo, 0 );
  106.          if (dragitem->fsSupportedOps & DO_COPYABLE) {
  107.             response = DOR_DROP;
  108.             action = DragInfo->usOperation;
  109.             action = (action == DO_DEFAULT) ? DO_COPY : action;
  110.          }
  111.      }
  112.   }
  113.   DrgFreeDraginfo( DragInfo );
  114.   DragInfo = NULL;
  115.  
  116.   return MRFROM2SHORT( response, action );
  117. }
  118. /*---------------------------------------------------------------------------
  119.                             ActMsgDragLeave
  120. ---------------------------------------------------------------------------*/
  121. MRESULT ActMsgDragLeave( )
  122. {
  123.  return MRFROMSHORT( FALSE );
  124. }
  125.  
  126. /*---------------------------------------------------------------------------
  127.                                ActMsgDrop
  128. ---------------------------------------------------------------------------*/
  129. MRESULT ActMsgDrop( WNDATTR *wndattr, MPARAM mp1, MPARAM mp2 )
  130. {
  131.  DRAGITEM     *dragitem;
  132.  DRAGTRANSFER *dragxfer;
  133.  BOOL          done;
  134.  char  path[CCHMAXPATHCOMP];
  135.  char  filename[CCHMAXPATHCOMP];
  136.  int   cnt;
  137.  
  138.    DragInfo = (DRAGINFO*)PVOIDFROMMP( mp1 );
  139.    DrgAccessDraginfo( DragInfo );
  140.  
  141.    dragitem = DrgQueryDragitemPtr( DragInfo, 0 );
  142.  
  143.    cnt = DrgQueryStrName( dragitem->hstrContainerName,
  144.                           sizeof path,
  145.                           path );
  146.    done = FALSE;
  147.  
  148.    if (cnt) {
  149.       DosQueryPathInfo( path, FIL_QUERYFULLNAME, path, sizeof path );
  150.  
  151.       cnt = DrgQueryStrName( dragitem->hstrSourceName,
  152.                              sizeof filename,
  153.                              filename );
  154.       if (cnt) {
  155.          strcat( path, filename );
  156.          done = AppendFile( wndattr, path, 0, NULLHANDLE );
  157.       }
  158.    }
  159.    if (done == FALSE) {
  160.        dragxfer = DrgAllocDragtransfer( 1 );
  161.        dragxfer->hwndClient = wndattr->Client;
  162.        dragxfer->pditem = dragitem;
  163.        GetTempDirName( path );
  164.        strcat( path, DefaultFileName );
  165.        dragxfer->hstrRenderToName = DrgAddStrHandle( path );
  166.        dragxfer->hstrSelectedRMF = DrgAddStrHandle( "<DRM_OS2FILE,DRF_TEXT>" );
  167.        dragxfer->usOperation = DragInfo->usOperation;
  168.        DrgSendTransferMsg( DragInfo->hwndSource,
  169.                            DM_RENDER,
  170.                            MPFROMP( dragxfer ),
  171.                            0 );
  172.    } else {
  173.       WinPostMsg( DragInfo->hwndSource,
  174.                   DM_ENDCONVERSATION,
  175.                   MPFROMLONG( dragitem->ulItemID ),
  176.                   MPFROMLONG( DMFL_TARGETSUCCESSFUL ) );
  177.       CleanUpDrag();
  178.   }
  179.  
  180.  
  181.  return MRFROMSHORT( 0 );
  182. }
  183. /*---------------------------------------------------------------------------
  184.                              ActMsgRender
  185. ---------------------------------------------------------------------------*/
  186. MRESULT ActMsgRender( WNDATTR *wndattr, MPARAM mp1 )
  187. {
  188.  void do_render( void * );
  189.  DRAGTRANSFER *dragxfer;
  190.  
  191.   dragxfer = PVOIDFROMMP( mp1 );
  192.   if ((dragxfer->usOperation & (DO_COPY | DO_MOVE))) {
  193.       wndattr->extra = dragxfer;
  194.       _beginthread( do_render, NULL, 0x2000, wndattr );
  195.   }
  196.  
  197.   return MRFROMSHORT( TRUE );
  198. }
  199. /*---------------------------------------------------------------------------
  200.                                do_render
  201. ---------------------------------------------------------------------------*/
  202. void do_render ( void *arg )
  203. {
  204.  WNDATTR      *wndattr;
  205.  DRAGTRANSFER *dragxfer;
  206.  char          filename[CCHMAXPATHCOMP];
  207.  ULONG         mp2;
  208.  ULONG         msg;
  209.  int           marked;
  210.  
  211.   wndattr = (WNDATTR*)arg;
  212.   dragxfer = (DRAGTRANSFER*)wndattr->extra;
  213.  
  214.   marked = (LPListMarkedLines( wndattr->list ) != 0);
  215.   DrgQueryStrName( dragxfer->hstrRenderToName,
  216.                    sizeof filename,
  217.                    filename );
  218.  
  219.   mp2 = DMFL_RENDERFAIL;
  220.   if (SaveList( filename, wndattr->list, marked, FALSE ) == TRUE) {
  221.      if (dragxfer->usOperation & DO_MOVE) {
  222.         msg = marked ? FLM_CLEARMARKED : FLM_CLEAR;
  223.         WinPostMsg( wndattr->Client, msg, 0, 0 );
  224.      }
  225.      mp2 = DMFL_RENDEROK;
  226.   }
  227.   DrgPostTransferMsg( dragxfer->hwndClient,
  228.                       DM_RENDERCOMPLETE,
  229.                       dragxfer,
  230.                       mp2,
  231.                       0L,
  232.                       FALSE);
  233.  
  234.  
  235.   DrgFreeDragtransfer( dragxfer );
  236.  
  237.   return;
  238. }
  239. /*---------------------------------------------------------------------------
  240.                           ActMsgRenderComplete
  241. ---------------------------------------------------------------------------*/
  242. MRESULT ActMsgRenderComplete( WNDATTR *wndattr, MPARAM mp1, MPARAM mp2 )
  243. {
  244.  DRAGTRANSFER *dragxfer;
  245.  DRAGITEM     *dragitem;
  246.  ULONG         response;
  247.  char  filename[CCHMAXPATHCOMP];
  248.  
  249.    dragxfer = PVOIDFROMMP( mp1 );
  250.    dragitem = dragxfer->pditem;
  251.    response = DMFL_TARGETFAIL;
  252.    if (SHORT1FROMMP( mp2 ) == DMFL_RENDEROK) {
  253.       DrgQueryStrName( dragxfer->hstrRenderToName,
  254.                        sizeof filename,
  255.                        filename );
  256.       if (AppendFile( wndattr, filename, 0, NULLHANDLE ) == TRUE)
  257.          response = DMFL_TARGETSUCCESSFUL;
  258.       DosDelete( filename );
  259.    }
  260.    DrgDeleteStrHandle( dragxfer->hstrRenderToName );
  261.    DrgDeleteStrHandle( dragxfer->hstrSelectedRMF );
  262.    DrgFreeDragtransfer( dragxfer );
  263.  
  264.    DrgSendTransferMsg( DragInfo->hwndSource,
  265.                        DM_ENDCONVERSATION,
  266.                        MPFROMLONG( dragitem->ulItemID ),
  267.                        MPFROMLONG( response ) );
  268.    CleanUpDrag();
  269.  
  270.    return MRFROMSHORT( 0 );
  271. }
  272. /*---------------------------------------------------------------------------
  273.                           ActMsgPrintObject
  274. ---------------------------------------------------------------------------*/
  275. MRESULT ActMsgPrintObject( WNDATTR *wndattr, MPARAM mp1, MPARAM mp2 )
  276. {
  277.  PRINTDEST     *pdest;
  278.  DEVOPENSTRUC  *dopstruct;
  279.  HSPL           hspl;
  280.  LPELEM        *elem;
  281.  BOOL           marked;
  282.  
  283.    pdest = PVOIDFROMMP( mp2 );
  284.    dopstruct = (DEVOPENSTRUC*)pdest->pdopData;
  285.  
  286.    dopstruct->pszDataType = "PM_Q_RAW";
  287.    hspl = SplQmOpen( NULL, 5L, (PQMOPENDATA)dopstruct );
  288.    if (hspl != SPL_ERROR) {
  289.       if (SplQmStartDoc( hspl, "List" ) == TRUE) {
  290.          marked = (LPListMarkedLines( wndattr->list ) != 0);
  291.          elem = wndattr->list->next;
  292.          while (elem != wndattr->list) {
  293.             if ((elem->nextmk && marked) || !marked) {
  294.                SplQmWrite( hspl, strlen( elem->data ), elem->data );
  295.                SplQmWrite( hspl, 2, "\r\n" );
  296.             }
  297.             elem = elem->next;
  298.          }
  299.          SplQmEndDoc( hspl );
  300.       }
  301.       SplQmClose( hspl );
  302.    }
  303.  
  304.  return MRFROMSHORT( DRR_SOURCE );
  305. }
  306. /*---------------------------------------------------------------------------
  307.                           ActMsgDiscardObject
  308. ---------------------------------------------------------------------------*/
  309. MRESULT ActMsgDiscardObject( WNDATTR *wndattr )
  310. {
  311.  ULONG msg;
  312.  
  313.    if (LPListMarkedLines( wndattr->list ) == 0)
  314.       msg = FLM_CLEAR;
  315.    else
  316.       msg = FLM_CLEARMARKED;
  317.  
  318.     WinSendMsg( wndattr->Client, msg, 0, 0 );
  319.  
  320.  return MRFROMSHORT( DRR_SOURCE );
  321. }
  322.  
  323. /*---------------------------------------------------------------------------
  324.                            ActMsgEndConversation
  325. ---------------------------------------------------------------------------*/
  326. MRESULT ActMsgEndConversation( )
  327. {
  328.   CleanUpDrag();
  329.  
  330.   return MRFROMSHORT( FALSE );
  331. }
  332. /*---------------------------------------------------------------------------
  333.                               CleanUpDrag
  334. ---------------------------------------------------------------------------*/
  335. void CleanUpDrag()
  336. {
  337.    if (DragInfo != NULL) {
  338.       DrgDeleteDraginfoStrHandles( DragInfo );
  339.       DrgFreeDraginfo( DragInfo );
  340.       DragInfo = NULL;
  341.    }
  342. }
  343.