]> git.saurik.com Git - wxWidgets.git/blob - src/os2/dnd.cpp
factoring out Background setter
[wxWidgets.git] / src / os2 / dnd.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/dnd.cpp
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/21/99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_DRAG_AND_DROP
16
17 #include "wx/window.h"
18 #include "wx/app.h"
19 #include "wx/gdicmn.h"
20 #include "wx/dnd.h"
21
22 #define INCL_PM
23 #define INCL_DOS
24 #include <os2.h>
25
26 // ----------------------------------------------------------------------------
27 // global
28 // ----------------------------------------------------------------------------
29
30 /////////////////////////////////////////////////////////////////////////////
31 // Private functions
32 /////////////////////////////////////////////////////////////////////////////
33
34 #if 0
35 static wxDragResult ConvertDragEffectToResult (
36 DWORD dwEffect
37 )
38 {
39 switch (dwEffect)
40 {
41 case DO_COPY:
42 return wxDragCopy;
43
44 case DO_LINK:
45 return wxDragLink;
46
47 case DO_MOVE:
48 return wxDragMove;
49
50 default:
51 case DO_DEFAULT:
52 return wxDragNone;
53 }
54 } // end of ConvertDragEffectToResult
55
56 static DWORD ConvertDragResultToEffect (
57 wxDragResult eResult
58 )
59 {
60 switch (eResult)
61 {
62 case wxDragCopy:
63 return DO_COPY;
64
65 case wxDragLink:
66 return DO_LINK;
67
68 case wxDragMove:
69 return DO_MOVE;
70
71 default:
72 case wxDragNone:
73 return DO_DEFAULT;
74 }
75 } // end of ConvertDragResultToEffect
76 #endif
77
78 class CIDropTarget
79 {
80 public:
81 CIDropTarget(wxDropTarget* pTarget)
82 {
83 m_pTarget = pTarget;
84 m_pDragItem = NULL;
85 }
86 virtual ~CIDropTarget() { }
87
88 //
89 // Accessors for CDropTarget
90 //
91 void Free(void) { ::DrgFreeDraginfo(m_pDragInfo); }
92 PDRAGINFO GetDataSource(void) { return m_pDragInfo; }
93 void SetDataSource(PDRAGINFO pDragInfo) { m_pDragInfo = pDragInfo; }
94 void SetHWND(HWND hWnd) { m_hWnd = hWnd; }
95
96 //
97 // CIDropTarget methods
98 //
99 bool DragLeave(void);
100 MRESULT DragOver(void);
101 MRESULT Drop(void);
102
103 protected:
104
105 PDRAGINFO m_pDragInfo;
106 PDRAGITEM m_pDragItem; // !NULL between DragEnter and DragLeave/Drop
107 wxDropTarget* m_pTarget; // the real target (we're just a proxy)
108 HWND m_hWnd; // window we're associated with
109 }; // end of CLASS CIDropTarget
110
111 bool CIDropTarget::DragLeave()
112 {
113 //
114 // Remove the UI feedback
115 //
116 m_pTarget->OnLeave();
117
118 //
119 // Release the held object
120 //
121 Free();
122 return true;
123 } // end of CIDropTarget::DragLeave
124
125 MRESULT CIDropTarget::DragOver ()
126 {
127 char zBuffer[128];
128 ULONG ulBytes;
129 USHORT uOp = 0;
130 USHORT uIndicator;
131 ULONG ulItems;
132 ULONG i;
133
134 ::DrgAccessDraginfo(m_pDragInfo);
135 switch(m_pDragInfo->usOperation)
136 {
137 case DO_UNKNOWN:
138 Free();
139 return (MRFROM2SHORT(DOR_NODROPOP, 0));
140
141 case DO_DEFAULT:
142 m_pDragItem = ::DrgQueryDragitemPtr(m_pDragInfo, 0);
143 ulBytes = ::DrgQueryStrName( m_pDragItem->hstrContainerName
144 ,128
145 ,zBuffer
146 );
147 if (!ulBytes)
148 return (MRFROM2SHORT(DOR_NODROPOP, 0));
149 else
150 uOp = DO_MOVE;
151 break;
152
153 case DO_COPY:
154 case DO_MOVE:
155 uOp = m_pDragInfo->usOperation;
156 break;
157 }
158 uIndicator = DOR_DROP;
159 ulItems = (ULONG)::DrgQueryDragitemCount(m_pDragInfo);
160 for (i = 0; i < ulItems; i++)
161 {
162 m_pDragItem = ::DrgQueryDragitemPtr(m_pDragInfo, i);
163 if (((m_pDragItem->fsSupportedOps & DO_COPYABLE) &&
164 (uOp == (USHORT)DO_COPY)) ||
165 ((m_pDragItem->fsSupportedOps & DO_MOVEABLE) &&
166 (uOp == (USHORT)DO_COPY)))
167 {
168 if (::DrgVerifyRMF(m_pDragItem, "DRM_OS2FILE", "DRF_UNKNOWN"))
169 uIndicator = (USHORT)DOR_DROP;
170 else
171 uIndicator = (USHORT)DOR_NEVERDROP;
172 }
173 }
174 Free();
175 return (MRFROM2SHORT(uIndicator, uOp));
176 } // end of CIDropTarget::DragOver
177
178 // #pragma page "CIDropTarget::Drop"
179 /////////////////////////////////////////////////////////////////////////////
180 //
181 // CIDropTarget::Drop
182 //
183 // Instructs the drop target to paste data that was just now dropped on it.
184 //
185 // PARAMETERS
186 // pIDataSource -- the data to paste
187 // dwKeyState -- kbd & mouse state
188 // pt -- mouse coordinates
189 // pdwEffect -- effect flag
190 //
191 // RETURN VALUE
192 // STDMETHODIMP S_OK
193 //
194 /////////////////////////////////////////////////////////////////////////////
195 MRESULT CIDropTarget::Drop ()
196 {
197 char zBuffer[128];
198 ULONG ulBytes;
199 USHORT uOp = 0;
200 USHORT uIndicator;
201 ULONG ulItems;
202 ULONG i;
203
204 ::DrgAccessDraginfo(m_pDragInfo);
205 switch(m_pDragInfo->usOperation)
206 {
207 case DO_UNKNOWN:
208 Free();
209 return (MRFROM2SHORT(DOR_NODROPOP, 0));
210
211 case DO_DEFAULT:
212 m_pDragItem = ::DrgQueryDragitemPtr(m_pDragInfo, 0);
213 ulBytes = ::DrgQueryStrName( m_pDragItem->hstrContainerName
214 ,128
215 ,zBuffer
216 );
217 if (!ulBytes)
218 return (MRFROM2SHORT(DOR_NODROPOP, 0));
219 else
220 uOp = DO_MOVE;
221 break;
222
223 case DO_COPY:
224 case DO_MOVE:
225 uOp = m_pDragInfo->usOperation;
226 break;
227 }
228 uIndicator = DOR_DROP;
229 ulItems = (ULONG)::DrgQueryDragitemCount(m_pDragInfo);
230 for (i = 0; i < ulItems; i++)
231 {
232 m_pDragItem = ::DrgQueryDragitemPtr(m_pDragInfo, i);
233 if (((m_pDragItem->fsSupportedOps & DO_COPYABLE) &&
234 (uOp == (USHORT)DO_COPY)) ||
235 ((m_pDragItem->fsSupportedOps & DO_MOVEABLE) &&
236 (uOp == (USHORT)DO_COPY)))
237 {
238 if (::DrgVerifyRMF(m_pDragItem, "DRM_OS2FILE", "DRF_UNKNOWN"))
239 uIndicator = (USHORT)DOR_DROP;
240 else
241 uIndicator = (USHORT)DOR_NEVERDROP;
242 }
243 }
244
245 //
246 // First ask the drop target if it wants data
247 //
248 if (m_pTarget->OnDrop( m_pDragInfo->xDrop
249 ,m_pDragInfo->yDrop
250 ))
251 {
252 wxDragResult eRc = wxDragNone;
253
254 //
255 // And now it has the data
256 //
257 eRc = m_pTarget->OnData( m_pDragInfo->xDrop
258 ,m_pDragInfo->yDrop
259 ,eRc
260 );
261 }
262 //else: OnDrop() returned false, no need to copy data
263
264 //
265 // Release the held object
266 //
267 Free();
268 return (MRFROM2SHORT(uIndicator, uOp));
269 } // end of CIDropTarget::Drop
270
271 // ----------------------------------------------------------------------------
272 // wxDropTarget
273 // ----------------------------------------------------------------------------
274
275 wxDropTarget::wxDropTarget (
276 wxDataObject* pDataObject
277 )
278 {
279 m_dataObject = pDataObject;
280 m_pDropTarget = new CIDropTarget(this);
281 } // end of wxDropTarget::wxDropTarget
282
283 wxDropTarget::~wxDropTarget()
284 {
285 Release();
286 } // end of wxDropTarget::~wxDropTarget
287
288 bool wxDropTarget::GetData ()
289 {
290 wxDataFormat vFormat = GetSupportedFormat(m_pDropTarget->GetDataSource());
291
292 if (vFormat == wxDF_INVALID)
293 {
294 return false;
295 }
296 //
297 // Under OS/2 we already have the data via the attached DRAGITEM's
298 //
299 return true;
300 } // end of wxDropTarget::GetData
301
302 wxDataFormat wxDropTarget::GetSupportedFormat (
303 PDRAGINFO pDataSource
304 ) const
305 {
306 PDRAGITEM pDragItem;
307 wxDataFormat vFormat;
308 wxDataFormat* pFormats;
309 ULONG ulFormats = m_dataObject->GetFormatCount(wxDataObject::Set);
310 ULONG ulItems = (ULONG)::DrgQueryDragitemCount(pDataSource);
311 ULONG i;
312 ULONG n;
313 wxString sMechanism;
314 wxString sFormat;
315 bool bValid = false;
316
317 pFormats = ulFormats == 1 ? &vFormat : new wxDataFormat[ulFormats];
318 m_dataObject->GetAllFormats( pFormats
319 ,wxDataObject::Set
320 );
321
322 for (n = 0; n < ulFormats; n++)
323 {
324 switch(pFormats[n].GetType())
325 {
326 case wxDF_TEXT:
327 case wxDF_FILENAME:
328 case wxDF_HTML:
329 sMechanism = _T("DRM_OS2FILE");
330 sFormat = _T("DRF_TEXT");
331 break;
332
333 case wxDF_OEMTEXT:
334 sMechanism = _T("DRM_OS2FILE");
335 sFormat = _T("DRF_OEMTEXT");
336 break;
337
338 case wxDF_BITMAP:
339 sMechanism = _T("DRM_OS2FILE");
340 sFormat = _T("DRF_BITMAP");
341 break;
342
343 case wxDF_METAFILE:
344 case wxDF_ENHMETAFILE:
345 sMechanism = _T("DRM_OS2FILE");
346 sFormat = _T("DRF_METAFILE");
347 break;
348
349 case wxDF_TIFF:
350 sMechanism = _T("DRM_OS2FILE");
351 sFormat = _T("DRF_TIFF");
352 break;
353
354 case wxDF_SYLK:
355 sMechanism = _T("DRM_OS2FILE");
356 sFormat = _T("DRF_SYLK");
357 break;
358
359 case wxDF_DIF:
360 sMechanism = _T("DRM_OS2FILE");
361 sFormat = _T("DRF_DIF");
362 break;
363
364 case wxDF_DIB:
365 sMechanism = _T("DRM_OS2FILE");
366 sFormat = _T("DRF_DIB");
367 break;
368
369 case wxDF_PALETTE:
370 case wxDF_PENDATA:
371 case wxDF_RIFF:
372 case wxDF_WAVE:
373 case wxDF_UNICODETEXT:
374 case wxDF_LOCALE:
375 sMechanism = _T("DRM_OS2FILE");
376 sFormat = _T("DRF_UNKNOWN");
377 break;
378
379 case wxDF_PRIVATE:
380 sMechanism = _T("DRM_OBJECT");
381 sFormat = _T("DRF_UNKNOWN");
382 break;
383 }
384 for (i = 0; i < ulItems; i++)
385 {
386 pDragItem = ::DrgQueryDragitemPtr(pDataSource, i);
387 if (::DrgVerifyRMF(pDragItem, (PSZ)sMechanism.c_str(), (PSZ)sFormat.c_str()))
388 {
389 bValid = true;
390 break;
391 }
392 }
393 if (bValid)
394 {
395 vFormat = pFormats[n];
396 break;
397 }
398 }
399 if (pFormats != &vFormat)
400 {
401 //
402 // Free memory if we allocated it
403 //
404 delete [] pFormats;
405 }
406 return (n < ulFormats ? vFormat : wxFormatInvalid);
407 } // end of wxDropTarget::GetSupportedFormat
408
409 bool wxDropTarget::IsAcceptedData (
410 PDRAGINFO pDataSource
411 ) const
412 {
413 return (GetSupportedFormat(pDataSource) != wxDF_INVALID);
414 } // end of wxDropTarget::IsAcceptedData
415
416 void wxDropTarget::Release ()
417 {
418 m_pDropTarget->Free();
419 } // end of wxDropTarget::Release
420
421
422 wxDragResult wxDropTarget::OnData (
423 wxCoord WXUNUSED(vX)
424 , wxCoord WXUNUSED(y)
425 , wxDragResult WXUNUSED(vResult)
426 )
427 {
428 return (wxDragResult)0;
429 } // end of wxDropTarget::OnData
430
431 bool wxDropTarget::OnDrop (
432 wxCoord WXUNUSED(x)
433 , wxCoord WXUNUSED(y)
434 )
435 {
436 return true;
437 } // end of wxDropTarget::OnDrop
438
439 //-------------------------------------------------------------------------
440 // wxDropSource
441 //-------------------------------------------------------------------------
442
443 wxDropSource::wxDropSource ( wxWindow* WXUNUSED(pWin) )
444 {
445 Init();
446 } // end of wxDropSource::wxDropSource
447
448 wxDropSource::wxDropSource ( wxDataObject& rData, wxWindow* WXUNUSED(pWin) )
449 {
450 Init();
451 SetData(rData);
452 } // end of wxDropSource::wxDropSource
453
454 wxDropSource::~wxDropSource ()
455 {
456 ::DrgFreeDraginfo(m_pDragInfo);
457 } // end of wxDropSource::~wxDropSource
458
459 wxDragResult wxDropSource::DoDragDrop (
460 int WXUNUSED(flags)
461 )
462 {
463 //
464 // Need to specify drag items in derived classes that know their data types
465 // before calling DoDragDrop
466 //
467 if (::DrgDrag( m_pWindow->GetHWND()
468 ,m_pDragInfo
469 ,&m_vDragImage
470 ,m_ulItems
471 ,VK_BUTTON2
472 ,NULL
473 ) != NULLHANDLE)
474 {
475 switch(m_pDragInfo->usOperation)
476 {
477 case DO_COPY:
478 return wxDragCopy;
479
480 case DO_MOVE:
481 return wxDragCopy;
482
483 case DO_LINK:
484 return wxDragCopy;
485
486 default:
487 return wxDragNone;
488 }
489 }
490 return wxDragError;
491 } // end of wxDropSource::DoDragDrop
492
493 bool wxDropSource::GiveFeedback (
494 wxDragResult eEffect
495 )
496 {
497 const wxCursor& rCursor = GetCursor(eEffect);
498
499 if (rCursor.Ok())
500 {
501 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)rCursor.GetHCURSOR());
502 m_vDragImage.hImage = (LHANDLE)rCursor.GetHCURSOR();
503 switch(eEffect)
504 {
505 case wxDragCopy:
506 m_pDragInfo->usOperation = DO_COPY;
507 break;
508
509 case wxDragMove:
510 m_pDragInfo->usOperation = DO_MOVE;
511 break;
512
513 case wxDragLink:
514 m_pDragInfo->usOperation = DO_LINK;
515 break;
516
517 case wxDragNone:
518 case wxDragCancel:
519 case wxDragError:
520 break;
521 }
522 return true;
523 }
524 else
525 {
526 return false;
527 }
528 } // end of GuiAdvDnd_CDropSource::GiveFeedback
529
530 void wxDropSource::Init ()
531 {
532 m_pDragInfo = ::DrgAllocDraginfo(m_ulItems);
533
534 //
535 // Set a default drag image struct with what we know so far
536 //
537 m_vDragImage.cb = sizeof(DRAGIMAGE);
538 m_vDragImage.cptl = 0; // non-zero if fl is DRG_POLYGON
539 m_vDragImage.hImage = 0; // Set in GiveFeedback
540 m_vDragImage.sizlStretch.cx = 20L;
541 m_vDragImage.sizlStretch.cy = 20L;
542 m_vDragImage.fl = DRG_ICON | DRG_STRETCH;
543 m_vDragImage.cxOffset = 0;
544 m_vDragImage.cyOffset = 0;
545
546 HSTR hStrType = ::DrgAddStrHandle(DRT_UNKNOWN);
547 HSTR hStrRMF;
548 HSTR hStrContainer;
549 wxChar zFormats[128];
550 wxChar zContainer[128];
551 USHORT uSize = (USHORT)(GetDataObject()->GetDataSize(GetDataObject()->GetPreferredFormat()) + 1);
552 wxChar* pzBuffer = new wxChar[uSize];
553
554 memset(pzBuffer, '\0', GetDataObject()->GetDataSize(GetDataObject()->GetPreferredFormat()));
555 pzBuffer[GetDataObject()->GetDataSize(GetDataObject()->GetPreferredFormat())] = '\0';
556 GetDataObject()->GetDataHere( GetDataObject()->GetPreferredFormat()
557 ,(void*)pzBuffer
558 );
559
560 wxStrcpy(zFormats, _T("<DRM_OS2FILE, DRF_UNKNOWN>"));
561 wxStrcpy(zContainer, GetDataObject()->GetPreferredFormat().GetId());
562
563 hStrRMF = ::DrgAddStrHandle((PSZ)zFormats);
564 hStrContainer = ::DrgAddStrHandle((PSZ)zContainer);
565
566 m_pDragItem = new DRAGITEM[m_ulItems];
567 for (ULONG i = 0; i < m_ulItems; i++)
568 {
569 m_pDragItem[i].hwndItem = m_pWindow->GetHWND();
570 m_pDragItem[i].hstrType = hStrType;
571 m_pDragItem[i].hstrRMF = hStrRMF;
572 m_pDragItem[i].hstrContainerName = hStrContainer;
573 m_pDragItem[i].fsControl = 0;
574 m_pDragItem[i].fsSupportedOps = DO_COPYABLE | DO_MOVEABLE | DO_LINKABLE;
575 m_pDragItem[i].hstrSourceName = ::DrgAddStrHandle((PSZ)pzBuffer);
576 m_pDragItem[i].hstrTargetName = m_pDragItem[i].hstrSourceName;
577 m_pDragItem[i].ulItemID = i;
578 ::DrgSetDragitem( m_pDragInfo
579 ,&m_pDragItem[i]
580 ,sizeof(DRAGITEM)
581 ,0
582 );
583 }
584 delete [] pzBuffer;
585 delete [] m_pDragItem;
586 } // end of wxDropSource::Init
587
588 #endif //wxUSE_DRAG_AND_DROP