]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/clipbrd.cpp
3474d380ab6a59e49330cb04c5a9eaae43d85ee3
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
WXUNUSED(dataFormat
))
123 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
128 #if wxUSE_DRAG_AND_DROP
129 static bool wxSetClipboardData(wxDataObject
*data
)
133 size_t size = data->GetDataSize();
134 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
137 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
138 "transfer."), size / 1024);
143 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
145 data->GetDataHere(lpGlobalMemory);
147 GlobalUnlock(hGlobal);
149 wxDataFormat format = data->GetPreferredFormat();
150 if ( !::SetClipboardData(format, hGlobal) )
152 wxLogSysError(_("Failed to set clipboard data in format %s"),
153 wxDataObject::GetFormatName(format));
160 #endif // wxUSE_DRAG_AND_DROP
163 bool wxSetClipboardData(wxDataFormat dataFormat
,
165 int width
, int height
)
169 HANDLE handle = 0; // return value of SetClipboardData
174 wxBitmap *bitmap = (wxBitmap *)data;
176 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
177 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
178 HBITMAP old = (HBITMAP)
179 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
180 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
182 bitmap->GetHeight());
185 SelectObject(hdcSrc, old);
191 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
192 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
193 hdcSrc, 0, 0, SRCCOPY);
195 // Select new bitmap out of memory DC
196 SelectObject(hdcMem, old1);
199 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
202 SelectObject(hdcSrc, old);
210 #if wxUSE_IMAGE_LOADING_IN_MSW
211 wxBitmap *bitmap = (wxBitmap *)data;
212 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
213 // NULL palette means to use the system one
214 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
215 handle = SetClipboardData(CF_DIB, hDIB);
223 wxMetafile *wxMF = (wxMetafile *)data;
224 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
225 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
227 mf->mm = wxMF->GetWindowsMappingMode();
230 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
232 wxMF->SetHMETAFILE((WXHANDLE) NULL);
234 handle = SetClipboardData(CF_METAFILEPICT, data);
244 wxLogError(_("Unsupported clipboard format."));
249 dataFormat = wxDF_TEXT;
254 char *s = (char *)data;
256 width = strlen(s) + 1;
258 DWORD l = (width * height);
259 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
262 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
264 memcpy(lpGlobalMemory, s, l);
266 GlobalUnlock(hGlobalMemory);
269 handle = SetClipboardData(dataFormat, hGlobalMemory);
276 wxLogSysError(_("Failed to set clipboard data."));
284 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
286 // void *retval = NULL;
289 switch ( dataFormat )
294 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
298 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
299 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
301 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
302 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
304 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
308 SelectObject(hdcSrc, old);
314 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
315 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
316 hdcSrc, 0, 0, SRCCOPY);
318 // Select new bitmap out of memory DC
319 SelectObject(hdcMem, old1);
322 SelectObject(hdcSrc, old);
326 // Create and return a new wxBitmap
327 wxBitmap *wxBM = new wxBitmap;
328 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
329 wxBM->SetWidth(bm.bmWidth);
330 wxBM->SetHeight(bm.bmHeight);
331 wxBM->SetDepth(bm.bmPlanes);
344 wxLogError(_("Unsupported clipboard format."));
349 dataFormat = wxDF_TEXT;
354 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
358 DWORD hsize = ::GlobalSize(hGlobalMemory);
362 char *s = new char[hsize];
366 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
368 memcpy(s, lpGlobalMemory, hsize);
370 ::GlobalUnlock(hGlobalMemory);
378 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
379 if ( !hGlobalMemory )
382 DWORD size = ::GlobalSize(hGlobalMemory);
386 void *buf = malloc(size);
390 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
392 memcpy(buf, lpGlobalMemory, size);
394 ::GlobalUnlock(hGlobalMemory);
403 wxLogSysError(_("Failed to retrieve data from the clipboard."));
411 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
413 // TODO: return ::EnumClipboardFormats(dataFormat);
417 int wxRegisterClipboardFormat(wxChar
*formatName
)
419 // TODO: return ::RegisterClipboardFormat(formatName);
423 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
427 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
431 // ---------------------------------------------------------------------------
433 // ---------------------------------------------------------------------------
435 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
437 wxClipboard::wxClipboard()
441 wxClipboard::~wxClipboard()
446 void wxClipboard::Clear()
450 bool wxClipboard::Flush()
456 bool wxClipboard::Open()
458 return wxOpenClipboard();
461 bool wxClipboard::IsOpened() const
463 return wxIsClipboardOpened();
466 bool wxClipboard::SetData( wxDataObject
*data
)
468 (void)wxEmptyClipboard();
472 return AddData(data);
479 bool wxClipboard::AddData( wxDataObject
*data
)
481 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
483 #if wxUSE_DRAG_AND_DROP
484 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
486 // wxDataFormat format = data->GetPreferredFormat();
494 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
495 wxString str(textDataObject->GetText());
496 return wxSetClipboardData(format, str.c_str());
502 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
503 wxBitmap bitmap(bitmapDataObject->GetBitmap());
504 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
510 wxMetafileDataObject* metaFileDataObject =
511 (wxMetafileDataObject*) data;
512 wxMetafile metaFile = metaFileDataObject->GetMetafile();
513 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
514 metaFileDataObject->GetWidth(),
515 metaFileDataObject->GetHeight());
517 #endif // wxUSE_METAFILE
520 return wxSetClipboardData(data);
522 #else // !wxUSE_DRAG_AND_DROP
527 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
530 void wxClipboard::Close()
535 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
537 return wxIsClipboardFormatAvailable(format
);
540 bool wxClipboard::GetData( wxDataObject
& data
)
542 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
544 #if wxUSE_DRAG_AND_DROP
545 // wxDataFormat format = data.GetPreferredFormat();
553 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
554 char* s = (char*) wxGetClipboardData(format);
557 textDataObject.SetText(s);
568 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
569 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetPreferredFormat());
572 bitmapDataObject.SetBitmap(* bitmap);
582 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
583 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
586 metaFileDataObject.SetMetafile(*metaFile);
597 void *buf = wxGetClipboardData(format, &len);
600 // FIXME this is for testing only!!
601 ((wxPrivateDataObject &)data).SetData(buf, len);
618 #endif // wxUSE_CLIPBOARD