]>
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"
44 #include "wx/os2/private.h"
46 // wxDataObject is tied to OLE/drag and drop implementation,
47 // therefore so is wxClipboard :-(
48 #if wxUSE_DRAG_AND_DROP
49 #include "wx/dataobj.h"
51 static bool wxSetClipboardData(wxDataObject
*data
);
54 // ===========================================================================
56 // ===========================================================================
58 // ---------------------------------------------------------------------------
59 // old-style clipboard functions using Windows API
60 // ---------------------------------------------------------------------------
62 static bool gs_wxClipboardIsOpen
= FALSE
;
64 bool wxOpenClipboard()
66 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
69 wxWindow *win = wxTheApp->GetTopWindow();
72 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
74 if ( !gs_wxClipboardIsOpen )
75 wxLogSysError(_("Failed to open the clipboard."));
77 return gs_wxClipboardIsOpen;
81 wxLogDebug(wxT("Can not open clipboard without a main window."));
89 bool wxCloseClipboard()
91 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
94 gs_wxClipboardIsOpen = FALSE;
96 if ( ::CloseClipboard() == 0 )
98 wxLogSysError(_("Failed to close the clipboard."));
106 bool wxEmptyClipboard()
110 if ( ::EmptyClipboard() == 0 )
112 wxLogSysError(_("Failed to empty the clipboard."));
120 bool wxIsClipboardOpened()
122 return gs_wxClipboardIsOpen
;
125 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
127 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
131 #if wxUSE_DRAG_AND_DROP
132 static bool wxSetClipboardData(wxDataObject
*data
)
136 size_t size = data->GetDataSize();
137 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
140 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
141 "transfer."), size / 1024);
146 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
148 data->GetDataHere(lpGlobalMemory);
150 GlobalUnlock(hGlobal);
152 wxDataFormat format = data->GetPreferredFormat();
153 if ( !::SetClipboardData(format, hGlobal) )
155 wxLogSysError(_("Failed to set clipboard data in format %s"),
156 wxDataObject::GetFormatName(format));
163 #endif // wxUSE_DRAG_AND_DROP
165 bool wxSetClipboardData(wxDataFormat dataFormat
,
167 int width
, int height
)
171 HANDLE handle = 0; // return value of SetClipboardData
176 wxBitmap *bitmap = (wxBitmap *)data;
178 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
179 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
180 HBITMAP old = (HBITMAP)
181 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
182 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
184 bitmap->GetHeight());
187 SelectObject(hdcSrc, old);
193 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
194 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
195 hdcSrc, 0, 0, SRCCOPY);
197 // Select new bitmap out of memory DC
198 SelectObject(hdcMem, old1);
201 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
204 SelectObject(hdcSrc, old);
212 #if wxUSE_IMAGE_LOADING_IN_MSW
213 wxBitmap *bitmap = (wxBitmap *)data;
214 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
215 // NULL palette means to use the system one
216 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
217 handle = SetClipboardData(CF_DIB, hDIB);
225 wxMetafile *wxMF = (wxMetafile *)data;
226 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
227 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
229 mf->mm = wxMF->GetWindowsMappingMode();
232 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
234 wxMF->SetHMETAFILE((WXHANDLE) NULL);
236 handle = SetClipboardData(CF_METAFILEPICT, data);
246 wxLogError(_("Unsupported clipboard format."));
251 dataFormat = wxDF_TEXT;
256 char *s = (char *)data;
258 width = strlen(s) + 1;
260 DWORD l = (width * height);
261 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
264 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
266 memcpy(lpGlobalMemory, s, l);
268 GlobalUnlock(hGlobalMemory);
271 handle = SetClipboardData(dataFormat, hGlobalMemory);
278 wxLogSysError(_("Failed to set clipboard data."));
286 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
291 switch ( dataFormat )
296 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
300 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
301 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
303 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
304 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
306 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
310 SelectObject(hdcSrc, old);
316 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
317 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
318 hdcSrc, 0, 0, SRCCOPY);
320 // Select new bitmap out of memory DC
321 SelectObject(hdcMem, old1);
324 SelectObject(hdcSrc, old);
328 // Create and return a new wxBitmap
329 wxBitmap *wxBM = new wxBitmap;
330 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
331 wxBM->SetWidth(bm.bmWidth);
332 wxBM->SetHeight(bm.bmHeight);
333 wxBM->SetDepth(bm.bmPlanes);
346 wxLogError(_("Unsupported clipboard format."));
351 dataFormat = wxDF_TEXT;
356 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
360 DWORD hsize = ::GlobalSize(hGlobalMemory);
364 char *s = new char[hsize];
368 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
370 memcpy(s, lpGlobalMemory, hsize);
372 ::GlobalUnlock(hGlobalMemory);
380 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
381 if ( !hGlobalMemory )
384 DWORD size = ::GlobalSize(hGlobalMemory);
388 void *buf = malloc(size);
392 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
394 memcpy(buf, lpGlobalMemory, size);
396 ::GlobalUnlock(hGlobalMemory);
405 wxLogSysError(_("Failed to retrieve data from the clipboard."));
413 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
415 // TODO: return ::EnumClipboardFormats(dataFormat);
419 int wxRegisterClipboardFormat(wxChar
*formatName
)
421 // TODO: return ::RegisterClipboardFormat(formatName);
425 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
429 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
433 // ---------------------------------------------------------------------------
435 // ---------------------------------------------------------------------------
437 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
439 wxClipboard::wxClipboard()
443 wxClipboard::~wxClipboard()
448 void wxClipboard::Clear()
452 bool wxClipboard::Flush()
458 bool wxClipboard::Open()
460 return wxOpenClipboard();
463 bool wxClipboard::IsOpened() const
465 return wxIsClipboardOpened();
468 bool wxClipboard::SetData( wxDataObject
*data
)
470 (void)wxEmptyClipboard();
474 return AddData(data);
481 bool wxClipboard::AddData( wxDataObject
*data
)
483 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
485 #if wxUSE_DRAG_AND_DROP
486 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
488 // wxDataFormat format = data->GetFormat();
496 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
497 wxString str(textDataObject->GetText());
498 return wxSetClipboardData(format, str.c_str());
504 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
505 wxBitmap bitmap(bitmapDataObject->GetBitmap());
506 return wxSetClipboardData(data->GetFormat(), &bitmap);
512 wxMetafileDataObject* metaFileDataObject =
513 (wxMetafileDataObject*) data;
514 wxMetafile metaFile = metaFileDataObject->GetMetafile();
515 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
516 metaFileDataObject->GetWidth(),
517 metaFileDataObject->GetHeight());
519 #endif // wxUSE_METAFILE
522 return wxSetClipboardData(data);
524 #else // !wxUSE_DRAG_AND_DROP
527 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
530 void wxClipboard::Close()
535 bool wxClipboard::IsSupported( 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->GetFormat();
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->GetFormat());
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);
617 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
618 #endif // wxUSE_CLIPBOARD