]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/object.h"
26 #include "wx/bitmap.h"
32 #include "wx/metafile.h"
36 #include "wx/clipbrd.h"
40 #include "wx/os2/private.h"
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"
47 static bool wxSetClipboardData(wxDataObject
*data
);
50 // ===========================================================================
52 // ===========================================================================
54 // ---------------------------------------------------------------------------
55 // old-style clipboard functions using Windows API
56 // ---------------------------------------------------------------------------
58 static bool gs_wxClipboardIsOpen
= FALSE
;
60 bool wxOpenClipboard()
62 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
65 wxWindow *win = wxTheApp->GetTopWindow();
68 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
70 if ( !gs_wxClipboardIsOpen )
71 wxLogSysError(_("Failed to open the clipboard."));
73 return gs_wxClipboardIsOpen;
77 wxLogDebug(wxT("Can not open clipboard without a main window."));
85 bool wxCloseClipboard()
87 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
90 gs_wxClipboardIsOpen = FALSE;
92 if ( ::CloseClipboard() == 0 )
94 wxLogSysError(_("Failed to close the clipboard."));
102 bool wxEmptyClipboard()
106 if ( ::EmptyClipboard() == 0 )
108 wxLogSysError(_("Failed to empty the clipboard."));
116 bool wxIsClipboardOpened()
118 return gs_wxClipboardIsOpen
;
121 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
123 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
127 #if wxUSE_DRAG_AND_DROP
128 static bool wxSetClipboardData(wxDataObject
*data
)
132 size_t size = data->GetDataSize();
133 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
136 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
137 "transfer."), size / 1024);
142 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
144 data->GetDataHere(lpGlobalMemory);
146 GlobalUnlock(hGlobal);
148 wxDataFormat format = data->GetPreferredFormat();
149 if ( !::SetClipboardData(format, hGlobal) )
151 wxLogSysError(_("Failed to set clipboard data in format %s"),
152 wxDataObject::GetFormatName(format));
159 #endif // wxUSE_DRAG_AND_DROP
161 bool wxSetClipboardData(wxDataFormat dataFormat
,
163 int width
, int height
)
167 HANDLE handle = 0; // return value of SetClipboardData
172 wxBitmap *bitmap = (wxBitmap *)data;
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,
180 bitmap->GetHeight());
183 SelectObject(hdcSrc, old);
189 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
190 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
191 hdcSrc, 0, 0, SRCCOPY);
193 // Select new bitmap out of memory DC
194 SelectObject(hdcMem, old1);
197 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
200 SelectObject(hdcSrc, old);
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);
221 wxMetafile *wxMF = (wxMetafile *)data;
222 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
223 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
225 mf->mm = wxMF->GetWindowsMappingMode();
228 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
230 wxMF->SetHMETAFILE((WXHANDLE) NULL);
232 handle = SetClipboardData(CF_METAFILEPICT, data);
242 wxLogError(_("Unsupported clipboard format."));
247 dataFormat = wxDF_TEXT;
252 char *s = (char *)data;
254 width = strlen(s) + 1;
256 DWORD l = (width * height);
257 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
260 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
262 memcpy(lpGlobalMemory, s, l);
264 GlobalUnlock(hGlobalMemory);
267 handle = SetClipboardData(dataFormat, hGlobalMemory);
274 wxLogSysError(_("Failed to set clipboard data."));
282 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
287 switch ( dataFormat )
292 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
296 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
297 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
299 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
300 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
302 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
306 SelectObject(hdcSrc, old);
312 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
313 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
314 hdcSrc, 0, 0, SRCCOPY);
316 // Select new bitmap out of memory DC
317 SelectObject(hdcMem, old1);
320 SelectObject(hdcSrc, old);
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);
342 wxLogError(_("Unsupported clipboard format."));
347 dataFormat = wxDF_TEXT;
352 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
356 DWORD hsize = ::GlobalSize(hGlobalMemory);
360 char *s = new char[hsize];
364 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
366 memcpy(s, lpGlobalMemory, hsize);
368 ::GlobalUnlock(hGlobalMemory);
376 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
377 if ( !hGlobalMemory )
380 DWORD size = ::GlobalSize(hGlobalMemory);
384 void *buf = malloc(size);
388 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
390 memcpy(buf, lpGlobalMemory, size);
392 ::GlobalUnlock(hGlobalMemory);
401 wxLogSysError(_("Failed to retrieve data from the clipboard."));
409 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
411 // TODO: return ::EnumClipboardFormats(dataFormat);
415 int wxRegisterClipboardFormat(wxChar
*formatName
)
417 // TODO: return ::RegisterClipboardFormat(formatName);
421 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
425 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
429 // ---------------------------------------------------------------------------
431 // ---------------------------------------------------------------------------
433 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
435 wxClipboard::wxClipboard()
439 wxClipboard::~wxClipboard()
444 void wxClipboard::Clear()
448 bool wxClipboard::Flush()
454 bool wxClipboard::Open()
456 return wxOpenClipboard();
459 bool wxClipboard::IsOpened() const
461 return wxIsClipboardOpened();
464 bool wxClipboard::SetData( wxDataObject
*data
)
466 (void)wxEmptyClipboard();
470 return AddData(data);
477 bool wxClipboard::AddData( wxDataObject
*data
)
479 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
481 #if wxUSE_DRAG_AND_DROP
482 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
484 // wxDataFormat format = data->GetFormat();
492 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
493 wxString str(textDataObject->GetText());
494 return wxSetClipboardData(format, str.c_str());
500 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
501 wxBitmap bitmap(bitmapDataObject->GetBitmap());
502 return wxSetClipboardData(data->GetFormat(), &bitmap);
508 wxMetafileDataObject* metaFileDataObject =
509 (wxMetafileDataObject*) data;
510 wxMetafile metaFile = metaFileDataObject->GetMetafile();
511 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
512 metaFileDataObject->GetWidth(),
513 metaFileDataObject->GetHeight());
515 #endif // wxUSE_METAFILE
518 return wxSetClipboardData(data);
520 #else // !wxUSE_DRAG_AND_DROP
525 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
528 void wxClipboard::Close()
533 bool wxClipboard::IsSupported( wxDataFormat format
)
535 return wxIsClipboardFormatAvailable(format
);
538 bool wxClipboard::GetData( wxDataObject
& data
)
540 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
542 #if wxUSE_DRAG_AND_DROP
543 // wxDataFormat format = data.GetFormat();
551 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
552 char* s = (char*) wxGetClipboardData(format);
555 textDataObject.SetText(s);
566 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
567 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat());
570 bitmapDataObject.SetBitmap(* bitmap);
580 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
581 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
584 metaFileDataObject.SetMetafile(*metaFile);
595 void *buf = wxGetClipboardData(format, &len);
598 // FIXME this is for testing only!!
599 ((wxPrivateDataObject &)data).SetData(buf, len);
617 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
618 #endif // wxUSE_CLIPBOARD