1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
/*
* This file implements some Win32 OLE2 specific Drag and Drop structures.
* Most of the code is a modification of the OLE2 SDK sample programs
* "drgdrps" and "drgdrpt". If you want to see them, don't search at
* Microsoft (I didn't find them there, seems they reorganised their servers)
* but instead at
* http://ftp.se.kde.org/pub/vendor/microsoft/Softlib/MSLFILES/
*/
#if defined(WIN32) && defined(USE_EXULTSTUDIO) && !defined(UNDER_CE)
#include <iostream>
#include "windrag.h"
#include "u7drag.h"
static UINT CF_EXULT = RegisterClipboardFormat("ExultData");
// Statics
void Windnd::CreateStudioDropDest(Windnd *& windnd, HWND &hWnd,
Drop_shape_handler_fun shapefun,
Drop_chunk_handler_fun cfun,
Drop_shape_handler_fun facefun,
void *udata)
{
hWnd = GetActiveWindow();
windnd = new Windnd(hWnd, shapefun, cfun, facefun, udata);
if (FAILED(RegisterDragDrop(hWnd, windnd))) {
std::cout << "Something's wrong with OLE2 ..." << std::endl;
}
}
void Windnd::DestroyStudioDropDest(Windnd *& windnd, HWND &hWnd)
{
RevokeDragDrop(hWnd);
delete windnd;
windnd = 0;
hWnd = 0;
}
// IDropTarget implementation
Windnd::Windnd(HWND hgwnd, Move_shape_handler_fun movefun,
Move_combo_handler_fun movecmbfun,
Drop_shape_handler_fun shapefun,
Drop_chunk_handler_fun cfun, Drop_combo_handler_fun combfun
) : gamewin(hgwnd), udata(0), move_shape_handler(movefun),
move_combo_handler(movecmbfun), shape_handler(shapefun),
chunk_handler(cfun), face_handler(0), combo_handler(combfun),
drag_id(-1)
{
std::memset (&data, 0, sizeof(data));
m_cRef = 1;
};
Windnd::Windnd(HWND hgwnd, Drop_shape_handler_fun shapefun,
Drop_chunk_handler_fun cfun, Drop_shape_handler_fun ffun, void *d
)
:gamewin(hgwnd), udata(d), move_shape_handler(0), move_combo_handler(0),
shape_handler(shapefun), chunk_handler(cfun),
face_handler(ffun), combo_handler(0), drag_id(-1)
{
std::memset (&data, 0, sizeof(data));
m_cRef = 1;
};
Windnd::~Windnd()
{
};
STDMETHODIMP
Windnd::QueryInterface(REFIID iid, void ** ppvObject)
{
*ppvObject=NULL;
if (IID_IUnknown==iid || IID_IDropTarget==iid)
*ppvObject=this;
if (NULL==*ppvObject)
return E_NOINTERFACE;
((LPUNKNOWN)*ppvObject)->AddRef();
return NOERROR;
};
STDMETHODIMP_(ULONG)
Windnd::AddRef(void)
{
return ++m_cRef;
};
STDMETHODIMP_(ULONG)
Windnd::Release(void)
{
if (0!=--m_cRef)
return m_cRef;
delete this;
return 0;
};
STDMETHODIMP
Windnd::DragEnter(IDataObject * pDataObject,
DWORD grfKeyState,
POINTL pt,
DWORD * pdwEffect)
{
if (!is_valid(pDataObject)) {
*pdwEffect = DROPEFFECT_NONE;
} else {
*pdwEffect = DROPEFFECT_COPY;
};
std::memset (&data, 0, sizeof(data));
FORMATETC fetc;
fetc.cfFormat = CF_EXULT;
fetc.ptd = NULL;
fetc.dwAspect = DVASPECT_CONTENT;
fetc.lindex = -1;
fetc.tymed = TYMED_HGLOBAL;
STGMEDIUM med;
pDataObject->GetData(&fetc, &med);
windragdata wdd((unsigned char *)GlobalLock(med.hGlobal));
GlobalUnlock(med.hGlobal);
ReleaseStgMedium(&med);
drag_id=wdd.get_id();
switch (drag_id) {
case U7_TARGET_SHAPEID:
Get_u7_shapeid(wdd.get_data(), data.shape.file, data.shape.shape, data.shape.frame);
break;
case U7_TARGET_CHUNKID:
Get_u7_chunkid(wdd.get_data(), data.chunk.chunknum);
break;
case U7_TARGET_COMBOID:
Get_u7_comboid(wdd.get_data(), data.combo.xtiles, data.combo.ytiles, data.combo.right, data.combo.below, data.combo.cnt, data.combo.combo);
break;
default:
break;
}
prevx = -1;
prevy = -1;
return S_OK;
};
STDMETHODIMP
Windnd::DragOver(DWORD grfKeyState,
POINTL pt,
DWORD * pdwEffect)
{
*pdwEffect = DROPEFFECT_COPY;
// Todo
POINT pnt = { pt.x, pt.y};
ScreenToClient(gamewin, &pnt);
switch (drag_id) {
case U7_TARGET_SHAPEID:
if (data.shape.file == U7_SHAPE_SHAPES) {
if (move_shape_handler) move_shape_handler(data.shape.shape, data.shape.frame,
pnt.x, pnt.y, prevx, prevy, true);
}
break;
case U7_TARGET_COMBOID:
if (data.combo.cnt > 0) {
if (move_combo_handler) move_combo_handler(data.combo.xtiles, data.combo.ytiles,
data.combo.right, data.combo.below, pnt.x, pnt.y, prevx, prevy, true);
}
break;
default:
break;
}
prevx = pnt.x;
prevy = pnt.y;
return S_OK;
};
STDMETHODIMP
Windnd::DragLeave(void)
{
move_shape_handler(-1, -1, 0, 0, prevx, prevy, true);
switch (drag_id) {
case U7_TARGET_SHAPEID:
break;
case U7_TARGET_COMBOID:
delete data.combo.combo;
break;
default:
break;
}
std::memset (&data, 0, sizeof(data));
drag_id = -1;
return S_OK;
};
STDMETHODIMP
Windnd::Drop(IDataObject * pDataObject,
DWORD grfKeyState,
POINTL pt,
DWORD * pdwEffect)
{
*pdwEffect = DROPEFFECT_COPY;
;
// retrieve the dragged data
FORMATETC fetc;
fetc.cfFormat = CF_EXULT;
fetc.ptd = NULL;
fetc.dwAspect = DVASPECT_CONTENT;
fetc.lindex = -1;
fetc.tymed = TYMED_HGLOBAL;
STGMEDIUM med;
pDataObject->GetData(&fetc, &med);
windragdata wdd((unsigned char *)GlobalLock(med.hGlobal));
GlobalUnlock(med.hGlobal);
ReleaseStgMedium(&med);
POINT pnt = { pt.x, pt.y};
ScreenToClient(gamewin, &pnt);
int id = wdd.get_id();
if (id == U7_TARGET_SHAPEID) {
int file,shape,frame;
Get_u7_shapeid(wdd.get_data(), file, shape, frame);
if (file == U7_SHAPE_SHAPES) {
if (shape_handler) (*shape_handler)(shape, frame, pnt.x, pnt.y, udata);
}
else if (file == U7_SHAPE_FACES) {
if (face_handler) (*face_handler)(shape, frame, pnt.x, pnt.y, udata);
}
} else if (id == U7_TARGET_CHUNKID) {
int chunknum;
Get_u7_chunkid(wdd.get_data(), chunknum);
if (chunk_handler) (*chunk_handler)(chunknum, pnt.x, pnt.y, udata);
} else if (id == U7_TARGET_COMBOID) {
int xtiles, ytiles;
int right, below, cnt;
U7_combo_data *combo;
Get_u7_comboid(wdd.get_data(), xtiles, ytiles, right, below, cnt, combo);
if (combo_handler) combo_handler(cnt, combo, pnt.x, pnt.y, udata);
delete combo;
}
return S_OK;
};
bool Windnd::is_valid(IDataObject * pDataObject)
{
FORMATETC fetc;
fetc.cfFormat = CF_EXULT;
fetc.ptd = NULL;
fetc.dwAspect = DVASPECT_CONTENT;
fetc.lindex = -1;
fetc.tymed = TYMED_HGLOBAL;
if (FAILED(pDataObject->QueryGetData(& fetc))) {
return false;
} else {
return true;
}
};
// IDropSource implementation
Windropsource::Windropsource(HBITMAP pdrag_bitmap, int x0, int y0)
:drag_bitmap(pdrag_bitmap)
{
m_cRef = 1;
drag_shape = 0;
#if 0
// doesn't work yet
SIZE bsize;
GetBitmapDimensionEx(drag_bitmap, &bsize);
bsize.cx = 32; bsize.cy = 32;
int shw = bsize.cx, shh = bsize.cy;
drag_shape = CreateWindowEx(WS_EX_TRANSPARENT,
"STATIC",
NULL,
WS_POPUP, // | SS_OWNERDRAW, /*SS_OWNERDRAW,*/
x0, y0,
shw, shh,
GetForegroundWindow(), // TODO: owner window
NULL,
(HINSTANCE)GetWindowLong(GetForegroundWindow(), GWL_HINSTANCE),
NULL);
if (FAILED(drag_shape)) {
g_warning("Create Window FAILED !");
};
#endif
};
Windropsource::~Windropsource()
{
DestroyWindow(drag_shape);
};
STDMETHODIMP
Windropsource::QueryInterface(REFIID iid, void ** ppvObject)
{
*ppvObject=NULL;
if (IID_IUnknown==iid || IID_IDropSource==iid)
*ppvObject = this;
if (NULL==*ppvObject)
return E_NOINTERFACE;
((LPUNKNOWN)*ppvObject)->AddRef();
return NOERROR;
};
STDMETHODIMP_(ULONG)
Windropsource::AddRef(void)
{
m_cRef = m_cRef + 1;
return m_cRef;
};
STDMETHODIMP_(ULONG)
Windropsource::Release(void)
{
if (0!=--m_cRef)
return m_cRef;
delete this;
return 0;
};
STDMETHODIMP
Windropsource::QueryContinueDrag(
BOOL fEscapePressed,
DWORD grfKeyState
)
{
#if 0
// doesn't work yet
POINT pnt;
GetCursorPos(&pnt);
MoveWindow(drag_shape,
pnt.x, pnt.y,
shw, shh,
true);
#endif
if (fEscapePressed)
return DRAGDROP_S_CANCEL;
else if (!(grfKeyState & MK_LBUTTON))
return DRAGDROP_S_DROP;
else
return NOERROR;
};
STDMETHODIMP
Windropsource::GiveFeedback(
DWORD dwEffect
)
{
return DRAGDROP_S_USEDEFAULTCURSORS;
};
// IDataObject implementation
Winstudioobj::Winstudioobj(windragdata pdata)
:data(pdata)
{
m_cRef = 1;
drag_image = 0;
};
Winstudioobj::~Winstudioobj()
{
};
STDMETHODIMP
Winstudioobj::QueryInterface(REFIID iid, void ** ppvObject)
{
*ppvObject=NULL;
if (IID_IUnknown==iid || IID_IDataObject==iid)
*ppvObject=this;
if (NULL==*ppvObject)
return E_NOINTERFACE;
((LPUNKNOWN)*ppvObject)->AddRef();
return NOERROR;
};
STDMETHODIMP_(ULONG)
Winstudioobj::AddRef(void)
{
return ++m_cRef;
};
STDMETHODIMP_(ULONG)
Winstudioobj::Release(void)
{
if (0!=--m_cRef)
return m_cRef;
delete this;
return 0;
};
STDMETHODIMP
Winstudioobj::GetData(
FORMATETC * pFormatetc,
STGMEDIUM * pmedium
)
{
std::cout << "In GetData"<< std::endl;
HGLOBAL hText;
unsigned char *ldata;
pmedium->tymed = 0;
pmedium->pUnkForRelease = NULL;
pmedium->hGlobal = NULL;
// This method is called by the drag-drop target to obtain the data
// that is being dragged.
if (pFormatetc->cfFormat == CF_EXULT &&
pFormatetc->dwAspect == DVASPECT_CONTENT &&
pFormatetc->tymed == TYMED_HGLOBAL)
{
hText = GlobalAlloc(GMEM_SHARE | GMEM_ZEROINIT, 8 + data.get_size());
if (!hText)
return E_OUTOFMEMORY;
// This provides us with a pointer to the allocated memory
ldata = (unsigned char *)GlobalLock(hText);
data.serialize(ldata);
GlobalUnlock(hText);
pmedium->tymed = TYMED_HGLOBAL;
pmedium->hGlobal = hText;
return S_OK;
}
return DATA_E_FORMATETC;
};
STDMETHODIMP
Winstudioobj::GetDataHere(
FORMATETC * pFormatetc,
STGMEDIUM * pmedium
)
{
return DATA_E_FORMATETC;
};
STDMETHODIMP
Winstudioobj::QueryGetData(
FORMATETC * pFormatetc
)
{
// This method is called by the drop target to check whether the source
// provides data is a format that the target accepts.
if (pFormatetc->cfFormat == CF_EXULT
&& pFormatetc->dwAspect == DVASPECT_CONTENT
&& pFormatetc->tymed & TYMED_HGLOBAL)
return S_OK;
else return S_FALSE;
};
STDMETHODIMP
Winstudioobj::GetCanonicalFormatEtc(
FORMATETC * pFormatetcIn,
FORMATETC * pFormatetcOut
)
{
pFormatetcOut->ptd = NULL;
return E_NOTIMPL;
};
STDMETHODIMP
Winstudioobj::SetData(
FORMATETC * pFormatetc,
STGMEDIUM * pmedium,
BOOL fRelease
)
{
return E_NOTIMPL;
};
STDMETHODIMP
Winstudioobj::EnumFormatEtc(
DWORD dwDirection,
IEnumFORMATETC ** ppenumFormatetc
)
{
SCODE sc = S_OK;
FORMATETC fmtetc;
fmtetc.cfFormat = CF_EXULT;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.tymed = TYMED_HGLOBAL;
fmtetc.ptd = NULL;
fmtetc.lindex = -1;
if (dwDirection == DATADIR_GET){
std::cout << "EnumFmt" << std::endl;
// I was too lazy to implement still another OLE2 interface just for something
// we don't even use. This function is supposed to be called by a drop target
// to find out if it can accept the provided data type. I suppose with
// E_NOTIMPL, other drop targets don't care about Exult data.
/**ppenumFormatetc = OleStdEnumFmtEtc_Create(1, &fmtetc);
if (*ppenumFormatetc == NULL)
sc = E_OUTOFMEMORY;*/
sc = E_NOTIMPL;
} else if (dwDirection == DATADIR_SET){
// A data transfer object that is used to transfer data
// (either via the clipboard or drag/drop does NOT
// accept SetData on ANY format.
sc = E_NOTIMPL;
goto error;
} else {
sc = E_INVALIDARG;
goto error;
}
error:
return sc;
};
STDMETHODIMP
Winstudioobj::DAdvise(
FORMATETC * pFormatetc,
DWORD advf,
IAdviseSink * pAdvSink,
DWORD * pdwConnection
)
{
return OLE_E_ADVISENOTSUPPORTED;
};
STDMETHODIMP
Winstudioobj::DUnadvise(
DWORD dwConnection
)
{
return OLE_E_ADVISENOTSUPPORTED;
};
STDMETHODIMP
Winstudioobj::EnumDAdvise(
IEnumSTATDATA ** ppenumAdvise
)
{
return OLE_E_ADVISENOTSUPPORTED;
};
#endif
|