*** empty log message ***
[wxWidgets.git] / src / os2 / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/setup.h"
17 #endif
18
19 #if wxUSE_CLIPBOARD
20
21 #ifndef WX_PRECOMP
22 #include "wx/object.h"
23 #include "wx/event.h"
24 #include "wx/app.h"
25 #include "wx/frame.h"
26 #include "wx/bitmap.h"
27 #include "wx/utils.h"
28 #include "wx/intl.h"
29 #endif
30
31 #if wxUSE_METAFILE
32 #include "wx/metafile.h"
33 #endif
34
35 #include "wx/log.h"
36 #include "wx/clipbrd.h"
37
38 #include <string.h>
39
40 #define INCL_DOS
41 #define INCL_PM
42 #include <os2.h>
43
44 #include "wx/os2/private.h"
45
46 // wxDataObject is tied to OLE/drag and drop implementation,
47 // therefore so is wxClipboard :-(
48 #if wxUSE_DRAG_AND_DROP
49 #include "wx/dataobj.h"
50
51 static bool wxSetClipboardData(wxDataObject *data);
52 #endif
53
54 // ===========================================================================
55 // implementation
56 // ===========================================================================
57
58 // ---------------------------------------------------------------------------
59 // old-style clipboard functions using Windows API
60 // ---------------------------------------------------------------------------
61
62 static bool gs_wxClipboardIsOpen = FALSE;
63
64 bool wxOpenClipboard()
65 {
66 wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, wxT("clipboard already opened.") );
67 // TODO:
68 /*
69 wxWindow *win = wxTheApp->GetTopWindow();
70 if ( win )
71 {
72 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
73
74 if ( !gs_wxClipboardIsOpen )
75 wxLogSysError(_("Failed to open the clipboard."));
76
77 return gs_wxClipboardIsOpen;
78 }
79 else
80 {
81 wxLogDebug(wxT("Can not open clipboard without a main window."));
82
83 return FALSE;
84 }
85 */
86 return FALSE;
87 }
88
89 bool wxCloseClipboard()
90 {
91 wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, wxT("clipboard is not opened") );
92 // TODO:
93 /*
94 gs_wxClipboardIsOpen = FALSE;
95
96 if ( ::CloseClipboard() == 0 )
97 {
98 wxLogSysError(_("Failed to close the clipboard."));
99
100 return FALSE;
101 }
102 */
103 return TRUE;
104 }
105
106 bool wxEmptyClipboard()
107 {
108 // TODO:
109 /*
110 if ( ::EmptyClipboard() == 0 )
111 {
112 wxLogSysError(_("Failed to empty the clipboard."));
113
114 return FALSE;
115 }
116 */
117 return TRUE;
118 }
119
120 bool wxIsClipboardOpened()
121 {
122 return gs_wxClipboardIsOpen;
123 }
124
125 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
126 {
127 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
128 return FALSE;
129 }
130
131 #if wxUSE_DRAG_AND_DROP
132 static bool wxSetClipboardData(wxDataObject *data)
133 {
134 // TODO:
135 /*
136 size_t size = data->GetDataSize();
137 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
138 if ( !hGlobal )
139 {
140 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
141 "transfer."), size / 1024);
142
143 return FALSE;
144 }
145
146 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
147
148 data->GetDataHere(lpGlobalMemory);
149
150 GlobalUnlock(hGlobal);
151
152 wxDataFormat format = data->GetPreferredFormat();
153 if ( !::SetClipboardData(format, hGlobal) )
154 {
155 wxLogSysError(_("Failed to set clipboard data in format %s"),
156 wxDataObject::GetFormatName(format));
157
158 return FALSE;
159 }
160 */
161 return TRUE;
162 }
163 #endif // wxUSE_DRAG_AND_DROP
164
165 bool wxSetClipboardData(wxDataFormat dataFormat,
166 const void *data,
167 int width, int height)
168 {
169 // TODO:
170 /*
171 HANDLE handle = 0; // return value of SetClipboardData
172 switch (dataFormat)
173 {
174 case wxDF_BITMAP:
175 {
176 wxBitmap *bitmap = (wxBitmap *)data;
177
178 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
179 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
180 HBITMAP old = (HBITMAP)
181 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
182 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
183 bitmap->GetWidth(),
184 bitmap->GetHeight());
185 if (!hBitmap)
186 {
187 SelectObject(hdcSrc, old);
188 DeleteDC(hdcMem);
189 DeleteDC(hdcSrc);
190 return FALSE;
191 }
192
193 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
194 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
195 hdcSrc, 0, 0, SRCCOPY);
196
197 // Select new bitmap out of memory DC
198 SelectObject(hdcMem, old1);
199
200 // Set the data
201 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
202
203 // Clean up
204 SelectObject(hdcSrc, old);
205 DeleteDC(hdcSrc);
206 DeleteDC(hdcMem);
207 break;
208 }
209
210 case wxDF_DIB:
211 {
212 #if wxUSE_IMAGE_LOADING_IN_MSW
213 wxBitmap *bitmap = (wxBitmap *)data;
214 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
215 // NULL palette means to use the system one
216 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
217 handle = SetClipboardData(CF_DIB, hDIB);
218 #endif
219 break;
220 }
221
222 #if wxUSE_METAFILE
223 case wxDF_METAFILE:
224 {
225 wxMetafile *wxMF = (wxMetafile *)data;
226 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
227 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
228
229 mf->mm = wxMF->GetWindowsMappingMode();
230 mf->xExt = width;
231 mf->yExt = height;
232 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
233 GlobalUnlock(data);
234 wxMF->SetHMETAFILE((WXHANDLE) NULL);
235
236 handle = SetClipboardData(CF_METAFILEPICT, data);
237 break;
238 }
239 #endif
240 case CF_SYLK:
241 case CF_DIF:
242 case CF_TIFF:
243 case CF_PALETTE:
244 default:
245 {
246 wxLogError(_("Unsupported clipboard format."));
247 return FALSE;
248 }
249
250 case wxDF_OEMTEXT:
251 dataFormat = wxDF_TEXT;
252 // fall through
253
254 case wxDF_TEXT:
255 {
256 char *s = (char *)data;
257
258 width = strlen(s) + 1;
259 height = 1;
260 DWORD l = (width * height);
261 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
262 if ( hGlobalMemory )
263 {
264 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
265
266 memcpy(lpGlobalMemory, s, l);
267
268 GlobalUnlock(hGlobalMemory);
269 }
270
271 handle = SetClipboardData(dataFormat, hGlobalMemory);
272 break;
273 }
274 }
275
276 if ( handle == 0 )
277 {
278 wxLogSysError(_("Failed to set clipboard data."));
279
280 return FALSE;
281 }
282 */
283 return TRUE;
284 }
285
286 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
287 {
288 void *retval = NULL;
289 // TODO:
290 /*
291 switch ( dataFormat )
292 {
293 case wxDF_BITMAP:
294 {
295 BITMAP bm;
296 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
297 if (!hBitmap)
298 break;
299
300 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
301 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
302
303 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
304 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
305
306 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
307
308 if (!hNewBitmap)
309 {
310 SelectObject(hdcSrc, old);
311 DeleteDC(hdcMem);
312 DeleteDC(hdcSrc);
313 break;
314 }
315
316 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
317 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
318 hdcSrc, 0, 0, SRCCOPY);
319
320 // Select new bitmap out of memory DC
321 SelectObject(hdcMem, old1);
322
323 // Clean up
324 SelectObject(hdcSrc, old);
325 DeleteDC(hdcSrc);
326 DeleteDC(hdcMem);
327
328 // Create and return a new wxBitmap
329 wxBitmap *wxBM = new wxBitmap;
330 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
331 wxBM->SetWidth(bm.bmWidth);
332 wxBM->SetHeight(bm.bmHeight);
333 wxBM->SetDepth(bm.bmPlanes);
334 wxBM->SetOk(TRUE);
335 retval = wxBM;
336 break;
337 }
338
339 case wxDF_METAFILE:
340 case CF_SYLK:
341 case CF_DIF:
342 case CF_TIFF:
343 case CF_PALETTE:
344 case wxDF_DIB:
345 {
346 wxLogError(_("Unsupported clipboard format."));
347 return FALSE;
348 }
349
350 case wxDF_OEMTEXT:
351 dataFormat = wxDF_TEXT;
352 // fall through
353
354 case wxDF_TEXT:
355 {
356 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
357 if (!hGlobalMemory)
358 break;
359
360 DWORD hsize = ::GlobalSize(hGlobalMemory);
361 if (len)
362 *len = hsize;
363
364 char *s = new char[hsize];
365 if (!s)
366 break;
367
368 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
369
370 memcpy(s, lpGlobalMemory, hsize);
371
372 ::GlobalUnlock(hGlobalMemory);
373
374 retval = s;
375 break;
376 }
377
378 default:
379 {
380 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
381 if ( !hGlobalMemory )
382 break;
383
384 DWORD size = ::GlobalSize(hGlobalMemory);
385 if ( len )
386 *len = size;
387
388 void *buf = malloc(size);
389 if ( !buf )
390 break;
391
392 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
393
394 memcpy(buf, lpGlobalMemory, size);
395
396 ::GlobalUnlock(hGlobalMemory);
397
398 retval = buf;
399 break;
400 }
401 }
402
403 if ( !retval )
404 {
405 wxLogSysError(_("Failed to retrieve data from the clipboard."));
406 }
407
408 return retval;
409 */
410 return NULL;
411 }
412
413 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
414 {
415 // TODO: return ::EnumClipboardFormats(dataFormat);
416 return dataFormat;
417 }
418
419 int wxRegisterClipboardFormat(wxChar *formatName)
420 {
421 // TODO: return ::RegisterClipboardFormat(formatName);
422 return 0;
423 }
424
425 bool wxGetClipboardFormatName(wxDataFormat dataFormat,
426 wxChar *formatName,
427 int maxCount)
428 {
429 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
430 return 0;
431 }
432
433 // ---------------------------------------------------------------------------
434 // wxClipboard
435 // ---------------------------------------------------------------------------
436
437 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
438
439 wxClipboard::wxClipboard()
440 {
441 }
442
443 wxClipboard::~wxClipboard()
444 {
445 Clear();
446 }
447
448 void wxClipboard::Clear()
449 {
450 }
451
452 bool wxClipboard::Flush()
453 {
454 // TODO:
455 return FALSE;
456 }
457
458 bool wxClipboard::Open()
459 {
460 return wxOpenClipboard();
461 }
462
463 bool wxClipboard::IsOpened() const
464 {
465 return wxIsClipboardOpened();
466 }
467
468 bool wxClipboard::SetData( wxDataObject *data )
469 {
470 (void)wxEmptyClipboard();
471 // TODO:
472 /*
473 if ( data )
474 return AddData(data);
475 else
476 return TRUE;
477 */
478 return TRUE;
479 }
480
481 bool wxClipboard::AddData( wxDataObject *data )
482 {
483 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
484
485 #if wxUSE_DRAG_AND_DROP
486 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
487
488 // wxDataFormat format = data->GetFormat();
489 // TODO:
490 /*
491 switch ( format )
492 {
493 case wxDF_TEXT:
494 case wxDF_OEMTEXT:
495 {
496 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
497 wxString str(textDataObject->GetText());
498 return wxSetClipboardData(format, str.c_str());
499 }
500
501 case wxDF_BITMAP:
502 case wxDF_DIB:
503 {
504 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
505 wxBitmap bitmap(bitmapDataObject->GetBitmap());
506 return wxSetClipboardData(data->GetFormat(), &bitmap);
507 }
508
509 #if wxUSE_METAFILE
510 case wxDF_METAFILE:
511 {
512 wxMetafileDataObject* metaFileDataObject =
513 (wxMetafileDataObject*) data;
514 wxMetafile metaFile = metaFileDataObject->GetMetafile();
515 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
516 metaFileDataObject->GetWidth(),
517 metaFileDataObject->GetHeight());
518 }
519 #endif // wxUSE_METAFILE
520
521 default:
522 return wxSetClipboardData(data);
523 }
524 #else // !wxUSE_DRAG_AND_DROP
525 */
526 return FALSE;
527 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
528 }
529
530 void wxClipboard::Close()
531 {
532 wxCloseClipboard();
533 }
534
535 bool wxClipboard::IsSupported( wxDataFormat format )
536 {
537 return wxIsClipboardFormatAvailable(format);
538 }
539
540 bool wxClipboard::GetData( wxDataObject *data )
541 {
542 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
543
544 #if wxUSE_DRAG_AND_DROP
545 // wxDataFormat format = data->GetFormat();
546 // TODO:
547 /*
548 switch ( format )
549 {
550 case wxDF_TEXT:
551 case wxDF_OEMTEXT:
552 {
553 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
554 char* s = (char*) wxGetClipboardData(format);
555 if ( s )
556 {
557 textDataObject->SetText(s);
558 delete[] s;
559 return TRUE;
560 }
561 else
562 return FALSE;
563 }
564
565 case wxDF_BITMAP:
566 case wxDF_DIB:
567 {
568 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject *)data;
569 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat());
570 if (bitmap)
571 {
572 bitmapDataObject->SetBitmap(* bitmap);
573 delete bitmap;
574 return TRUE;
575 }
576 else
577 return FALSE;
578 }
579 #if wxUSE_METAFILE
580 case wxDF_METAFILE:
581 {
582 wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject *)data;
583 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
584 if (metaFile)
585 {
586 metaFileDataObject->SetMetafile(*metaFile);
587 delete metaFile;
588 return TRUE;
589 }
590 else
591 return FALSE;
592 }
593 #endif
594 default:
595 {
596 long len;
597 void *buf = wxGetClipboardData(format, &len);
598 if ( buf )
599 {
600 // FIXME this is for testing only!!
601 ((wxPrivateDataObject *)data)->SetData(buf, len);
602 free(buf);
603
604 return TRUE;
605 }
606 }
607
608 return FALSE;
609 }
610 #else
611 */
612 return FALSE;
613 #endif
614 }
615
616 #else
617 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
618 #endif // wxUSE_CLIPBOARD
619