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