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