| 1 | /* $Id: oslibdnd.cpp,v 1.1 2003-07-28 11:28:10 sandervl Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | * Window Drag and Drop functions for OS/2 | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 2003 Sander van Leeuwen (sandervl@innotek.de) | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 | #define  INCL_WIN | 
|---|
| 13 | #define  INCL_WINSTDDRAG | 
|---|
| 14 | #define  INCL_PM | 
|---|
| 15 | #include <os2wrap.h> | 
|---|
| 16 | #include <stdlib.h> | 
|---|
| 17 | #include <string.h> | 
|---|
| 18 | #include <stdio.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include <dbglog.h> | 
|---|
| 21 | #include <winconst.h> | 
|---|
| 22 | #include <win32api.h> | 
|---|
| 23 | #include <winuser32.h> | 
|---|
| 24 |  | 
|---|
| 25 | #include "oslibdnd.h" | 
|---|
| 26 |  | 
|---|
| 27 | static HWND  hwndTarget = 0; | 
|---|
| 28 | static PDRAGINFO curDragInfo = NULL; | 
|---|
| 29 | static char *pszCurDragData = NULL; | 
|---|
| 30 |  | 
|---|
| 31 | //****************************************************************************** | 
|---|
| 32 | //****************************************************************************** | 
|---|
| 33 | LPVOID OSLibCreateDragStruct(HWND hwndWin32, DWORD x, DWORD y, LPSTR lpszDnDString) | 
|---|
| 34 | { | 
|---|
| 35 | PDRAGINFO  pdinfo;       /* Pointer to DRAGINFO structure        */ | 
|---|
| 36 | HWND       hwnd;         /* Handle of calling (source) window    */ | 
|---|
| 37 | BOOL       flResult;     /* Result indicator                     */ | 
|---|
| 38 | DRAGITEM   ditem;        /* DRAGITEM structure                   */ | 
|---|
| 39 | RECTL      rectl; | 
|---|
| 40 | ULONG      ScreenHeight; | 
|---|
| 41 |  | 
|---|
| 42 | WinQueryWindowRect(HWND_DESKTOP, &rectl); | 
|---|
| 43 | ScreenHeight = rectl.yTop; | 
|---|
| 44 |  | 
|---|
| 45 | pdinfo = DrgAllocDraginfo(1); /* Create the DRAGINFO structure   */ | 
|---|
| 46 | /* Set the drag item               */ | 
|---|
| 47 | if(pdinfo == NULL) { | 
|---|
| 48 | dprintf(("DrgAllocDraginfo FAILED!!")); | 
|---|
| 49 | return NULL; | 
|---|
| 50 | } | 
|---|
| 51 | pdinfo->usOperation  = DO_DEFAULT; | 
|---|
| 52 | pdinfo->hwndSource   = Win32ToOS2Handle(hwndWin32); | 
|---|
| 53 | pdinfo->xDrop        = x; | 
|---|
| 54 | pdinfo->yDrop        = ScreenHeight - y; | 
|---|
| 55 |  | 
|---|
| 56 | char szDir[CCHMAXPATH*4]; | 
|---|
| 57 |  | 
|---|
| 58 | const char *pszTmpDir = getenv("TMP"); | 
|---|
| 59 | if (!pszTmpDir) | 
|---|
| 60 | pszTmpDir = getenv("TEMP"); | 
|---|
| 61 | if (!pszTmpDir) | 
|---|
| 62 | pszTmpDir = "."; | 
|---|
| 63 |  | 
|---|
| 64 | strncpy(szDir, pszTmpDir, sizeof(szDir) - 2); | 
|---|
| 65 | szDir[sizeof(szDir) - 2] = '\0'; // strncpy doesn't ensure this | 
|---|
| 66 | strcat(szDir, "\\"); | 
|---|
| 67 |  | 
|---|
| 68 | dprintf(("temporary dir %s", szDir)); | 
|---|
| 69 |  | 
|---|
| 70 | ditem.hwndItem       = pdinfo->hwndSource; | 
|---|
| 71 | ditem.ulItemID       = 0; | 
|---|
| 72 | ditem.hstrType       = DrgAddStrHandle(DRT_TEXT); | 
|---|
| 73 | ditem.hstrRMF        = DrgAddStrHandle("<DRM_OS2FILE, DRF_TEXT>"); | 
|---|
| 74 | ditem.hstrContainerName  = DrgAddStrHandle(szDir); | 
|---|
| 75 | ditem.hstrSourceName = 0; //done during rendering | 
|---|
| 76 | ditem.hstrTargetName = 0; | 
|---|
| 77 | ditem.cxOffset       = 0; | 
|---|
| 78 | ditem.cyOffset       = 0; | 
|---|
| 79 | ditem.fsControl      = 0; | 
|---|
| 80 | ditem.fsSupportedOps = DO_COPYABLE | DO_MOVEABLE; | 
|---|
| 81 |  | 
|---|
| 82 | flResult= DrgSetDragitem(pdinfo, &ditem, (ULONG)sizeof(ditem), 0); | 
|---|
| 83 | if(!flResult) dprintf(("DrgSetDragitem failed for %x", &ditem)); | 
|---|
| 84 |  | 
|---|
| 85 | return pdinfo; | 
|---|
| 86 | } | 
|---|
| 87 | //****************************************************************************** | 
|---|
| 88 | //****************************************************************************** | 
|---|
| 89 | void OSLibFreeDragStruct(LPVOID lpDragStruct) | 
|---|
| 90 | { | 
|---|
| 91 | PDRAGINFO pdinfo = (PDRAGINFO)lpDragStruct; | 
|---|
| 92 | BOOL       flResult;     /* Result indicator                     */ | 
|---|
| 93 |  | 
|---|
| 94 | flResult = DrgDeleteDraginfoStrHandles(pdinfo); | 
|---|
| 95 | if(!flResult) dprintf(("DrgDeleteDraginfoStrHandles failed for %x", lpDragStruct)); | 
|---|
| 96 |  | 
|---|
| 97 | flResult = DrgFreeDraginfo(pdinfo); | 
|---|
| 98 | if(!flResult) dprintf(("DrgFreeDragInfo failed for %x", lpDragStruct)); | 
|---|
| 99 |  | 
|---|
| 100 | return; | 
|---|
| 101 | } | 
|---|
| 102 | //****************************************************************************** | 
|---|
| 103 | //****************************************************************************** | 
|---|
| 104 | DWORD OSLibDragOver(LPVOID lpDragStruct, DWORD x, DWORD y) | 
|---|
| 105 | { | 
|---|
| 106 | PDRAGINFO pdinfo = (PDRAGINFO)lpDragStruct; | 
|---|
| 107 | RECTL      rectl; | 
|---|
| 108 | ULONG      ScreenHeight, ret; | 
|---|
| 109 | HWND       hwnd; | 
|---|
| 110 | POINTL     pointl; | 
|---|
| 111 |  | 
|---|
| 112 | WinQueryWindowRect(HWND_DESKTOP, &rectl); | 
|---|
| 113 | ScreenHeight = rectl.yTop; | 
|---|
| 114 |  | 
|---|
| 115 | pdinfo->xDrop = x; | 
|---|
| 116 | pdinfo->yDrop = ScreenHeight - y; | 
|---|
| 117 |  | 
|---|
| 118 | pointl.x = pdinfo->xDrop; | 
|---|
| 119 | pointl.y = pdinfo->yDrop; | 
|---|
| 120 | hwnd = WinWindowFromPoint(HWND_DESKTOP, &pointl, TRUE); | 
|---|
| 121 | if(hwnd == 0) return DROPEFFECT_NONE_W; | 
|---|
| 122 |  | 
|---|
| 123 | if(hwnd != hwndTarget) { | 
|---|
| 124 | OSLibDragLeave(lpDragStruct); | 
|---|
| 125 | } | 
|---|
| 126 | hwndTarget = hwnd; | 
|---|
| 127 |  | 
|---|
| 128 | dprintf(("Post DM_DRAGOVER to %x (%x)", OS2ToWin32Handle(hwnd), hwnd)); | 
|---|
| 129 | ret = (ULONG)WinSendMsg(hwnd, DM_DRAGOVER, (MPARAM)pdinfo, MPFROM2SHORT(pdinfo->xDrop, pdinfo->yDrop)); | 
|---|
| 130 |  | 
|---|
| 131 | if(LOWORD(ret) != DOR_DROP) { | 
|---|
| 132 | return DROPEFFECT_NONE_W; | 
|---|
| 133 | } | 
|---|
| 134 | if(HIWORD(ret) == DO_LINK) { | 
|---|
| 135 | return DROPEFFECT_LINK_W; | 
|---|
| 136 | } | 
|---|
| 137 | if(HIWORD(ret) == DO_MOVE) { | 
|---|
| 138 | return DROPEFFECT_MOVE_W; | 
|---|
| 139 | } | 
|---|
| 140 | return DROPEFFECT_COPY_W; | 
|---|
| 141 | } | 
|---|
| 142 | //****************************************************************************** | 
|---|
| 143 | //****************************************************************************** | 
|---|
| 144 | DWORD OSLibDragLeave(LPVOID lpDragStruct) | 
|---|
| 145 | { | 
|---|
| 146 | PDRAGINFO  pdinfo = (PDRAGINFO)lpDragStruct; | 
|---|
| 147 | ULONG      ret; | 
|---|
| 148 |  | 
|---|
| 149 | if(hwndTarget) { | 
|---|
| 150 | dprintf(("Post DM_DRAGLEAVE to %x (%x)", OS2ToWin32Handle(hwndTarget), hwndTarget)); | 
|---|
| 151 | ret = (ULONG)WinSendMsg(hwndTarget, DM_DRAGLEAVE, (MPARAM)pdinfo, 0); | 
|---|
| 152 | } | 
|---|
| 153 | return DROPEFFECT_NONE_W; | 
|---|
| 154 | } | 
|---|
| 155 | //****************************************************************************** | 
|---|
| 156 | //****************************************************************************** | 
|---|
| 157 | DWORD OSLibDragDrop(LPVOID lpDragStruct, DWORD x, DWORD y, LPSTR lpszDnDString) | 
|---|
| 158 | { | 
|---|
| 159 | PDRAGINFO pdinfo = (PDRAGINFO)lpDragStruct; | 
|---|
| 160 | RECTL      rectl; | 
|---|
| 161 | ULONG      ScreenHeight, ret; | 
|---|
| 162 | HWND       hwnd; | 
|---|
| 163 | POINTL     pointl; | 
|---|
| 164 |  | 
|---|
| 165 | WinQueryWindowRect(HWND_DESKTOP, &rectl); | 
|---|
| 166 | ScreenHeight = rectl.yTop; | 
|---|
| 167 |  | 
|---|
| 168 | pdinfo->xDrop = x; | 
|---|
| 169 | pdinfo->yDrop = ScreenHeight - y; | 
|---|
| 170 |  | 
|---|
| 171 | pointl.x = pdinfo->xDrop; | 
|---|
| 172 | pointl.y = pdinfo->yDrop; | 
|---|
| 173 | hwnd = WinWindowFromPoint(HWND_DESKTOP, &pointl, TRUE); | 
|---|
| 174 | if(hwnd == 0) return DROPEFFECT_NONE_W; | 
|---|
| 175 |  | 
|---|
| 176 | hwndTarget = hwnd; | 
|---|
| 177 | curDragInfo = pdinfo; | 
|---|
| 178 | pszCurDragData = lpszDnDString; | 
|---|
| 179 |  | 
|---|
| 180 | dprintf(("dnd data %s", pszCurDragData)); | 
|---|
| 181 |  | 
|---|
| 182 | ret = (ULONG)WinSendMsg(hwnd, DM_DROP, (MPARAM)pdinfo, 0); | 
|---|
| 183 |  | 
|---|
| 184 | pszCurDragData = NULL; | 
|---|
| 185 | curDragInfo = NULL; | 
|---|
| 186 | hwndTarget = 0; | 
|---|
| 187 | return DROPEFFECT_COPY_W; | 
|---|
| 188 | } | 
|---|
| 189 | //****************************************************************************** | 
|---|
| 190 | //****************************************************************************** | 
|---|
| 191 | BOOL OSLibRenderFormat(PDRAGTRANSFER pDragTransfer) | 
|---|
| 192 | { | 
|---|
| 193 | PDRAGITEM pDragItem = pDragTransfer->pditem; | 
|---|
| 194 | int size = 0, ulBytes; | 
|---|
| 195 | char *tmp; | 
|---|
| 196 |  | 
|---|
| 197 | #ifdef DEBUG | 
|---|
| 198 | size = DrgQueryStrNameLen(pDragTransfer->hstrSelectedRMF); | 
|---|
| 199 | tmp = (char *)malloc(size+2); | 
|---|
| 200 | if(tmp == NULL) { | 
|---|
| 201 | DebugInt3(); | 
|---|
| 202 | return FALSE; | 
|---|
| 203 | } | 
|---|
| 204 | size = DrgQueryStrName(pDragTransfer->hstrSelectedRMF, size+1, tmp); | 
|---|
| 205 | dprintf(("Rendering method %s", tmp)); | 
|---|
| 206 | free(tmp); | 
|---|
| 207 | #endif | 
|---|
| 208 |  | 
|---|
| 209 | size = DrgQueryStrNameLen(pDragTransfer->hstrRenderToName); | 
|---|
| 210 | tmp = (char *)malloc(size+2); | 
|---|
| 211 | if(tmp == NULL) { | 
|---|
| 212 | DebugInt3(); | 
|---|
| 213 | return FALSE; | 
|---|
| 214 | } | 
|---|
| 215 | size = DrgQueryStrName(pDragTransfer->hstrRenderToName, size+1, tmp); | 
|---|
| 216 |  | 
|---|
| 217 | dprintf(("Render to file %s", tmp)); | 
|---|
| 218 |  | 
|---|
| 219 | dprintf(("dnd data %s", pszCurDragData)); | 
|---|
| 220 |  | 
|---|
| 221 | FILE *tmpfile; | 
|---|
| 222 | tmpfile = fopen(tmp, "wb+"); | 
|---|
| 223 | fwrite(pszCurDragData, 1, strlen(pszCurDragData), tmpfile); | 
|---|
| 224 | fclose(tmpfile); | 
|---|
| 225 |  | 
|---|
| 226 | DrgPostTransferMsg(pDragTransfer->hwndClient, DM_RENDERCOMPLETE, pDragTransfer, | 
|---|
| 227 | DMFL_RENDEROK, 0, TRUE); | 
|---|
| 228 |  | 
|---|
| 229 | DrgFreeDragtransfer(pDragTransfer); | 
|---|
| 230 | free(tmp); | 
|---|
| 231 | return TRUE; | 
|---|
| 232 | } | 
|---|
| 233 | //****************************************************************************** | 
|---|
| 234 | //****************************************************************************** | 
|---|
| 235 | ULONG OSLibEndConversation() | 
|---|
| 236 | { | 
|---|
| 237 | return 0; | 
|---|
| 238 | } | 
|---|
| 239 | //****************************************************************************** | 
|---|
| 240 | //****************************************************************************** | 
|---|