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