*** 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* wxTheClipboard = (wxClipboard *)NULL;
440
441 wxClipboard::wxClipboard()
442 {
443 }
444
445 wxClipboard::~wxClipboard()
446 {
447 Clear();
448 }
449
450 void wxClipboard::Clear()
451 {
452 }
453
454 bool wxClipboard::Flush()
455 {
456 // TODO:
457 return FALSE;
458 }
459
460 bool wxClipboard::Open()
461 {
462 return wxOpenClipboard();
463 }
464
465 bool wxClipboard::IsOpened() const
466 {
467 return wxIsClipboardOpened();
468 }
469
470 bool wxClipboard::SetData( wxDataObject *data )
471 {
472 (void)wxEmptyClipboard();
473 // TODO:
474 /*
475 if ( data )
476 return AddData(data);
477 else
478 return TRUE;
479 */
480 return TRUE;
481 }
482
483 bool wxClipboard::AddData( wxDataObject *data )
484 {
485 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
486
487 #if wxUSE_DRAG_AND_DROP
488 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
489
490 // wxDataFormat format = data->GetFormat();
491 // TODO:
492 /*
493 switch ( format )
494 {
495 case wxDF_TEXT:
496 case wxDF_OEMTEXT:
497 {
498 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
499 wxString str(textDataObject->GetText());
500 return wxSetClipboardData(format, str.c_str());
501 }
502
503 case wxDF_BITMAP:
504 case wxDF_DIB:
505 {
506 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
507 wxBitmap bitmap(bitmapDataObject->GetBitmap());
508 return wxSetClipboardData(data->GetFormat(), &bitmap);
509 }
510
511 #if wxUSE_METAFILE
512 case wxDF_METAFILE:
513 {
514 wxMetafileDataObject* metaFileDataObject =
515 (wxMetafileDataObject*) data;
516 wxMetafile metaFile = metaFileDataObject->GetMetafile();
517 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
518 metaFileDataObject->GetWidth(),
519 metaFileDataObject->GetHeight());
520 }
521 #endif // wxUSE_METAFILE
522
523 default:
524 return wxSetClipboardData(data);
525 }
526 #else // !wxUSE_DRAG_AND_DROP
527 */
528 return FALSE;
529 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
530 }
531
532 void wxClipboard::Close()
533 {
534 wxCloseClipboard();
535 }
536
537 bool wxClipboard::IsSupported( wxDataFormat format )
538 {
539 return wxIsClipboardFormatAvailable(format);
540 }
541
542 bool wxClipboard::GetData( wxDataObject *data )
543 {
544 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
545
546 #if wxUSE_DRAG_AND_DROP
547 // wxDataFormat format = data->GetFormat();
548 // TODO:
549 /*
550 switch ( format )
551 {
552 case wxDF_TEXT:
553 case wxDF_OEMTEXT:
554 {
555 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
556 char* s = (char*) wxGetClipboardData(format);
557 if ( s )
558 {
559 textDataObject->SetText(s);
560 delete[] s;
561 return TRUE;
562 }
563 else
564 return FALSE;
565 }
566
567 case wxDF_BITMAP:
568 case wxDF_DIB:
569 {
570 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject *)data;
571 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat());
572 if (bitmap)
573 {
574 bitmapDataObject->SetBitmap(* bitmap);
575 delete bitmap;
576 return TRUE;
577 }
578 else
579 return FALSE;
580 }
581 #if wxUSE_METAFILE
582 case wxDF_METAFILE:
583 {
584 wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject *)data;
585 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
586 if (metaFile)
587 {
588 metaFileDataObject->SetMetafile(*metaFile);
589 delete metaFile;
590 return TRUE;
591 }
592 else
593 return FALSE;
594 }
595 #endif
596 default:
597 {
598 long len;
599 void *buf = wxGetClipboardData(format, &len);
600 if ( buf )
601 {
602 // FIXME this is for testing only!!
603 ((wxPrivateDataObject *)data)->SetData(buf, len);
604 free(buf);
605
606 return TRUE;
607 }
608 }
609
610 return FALSE;
611 }
612 #else
613 */
614 return FALSE;
615 #endif
616 }
617
618 #else
619 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
620 #endif // wxUSE_CLIPBOARD
621