]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: David Webster
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/clipbrd.h"
19 #include "wx/object.h"
23 #include "wx/bitmap.h"
27 #include "wx/dataobj.h"
31 #include "wx/metafile.h"
36 #include "wx/os2/private.h"
38 // wxDataObject is tied to OLE/drag and drop implementation,
39 // therefore so is wxClipboard :-(
41 // ===========================================================================
43 // ===========================================================================
45 // ---------------------------------------------------------------------------
46 // old-style clipboard functions using Windows API
47 // ---------------------------------------------------------------------------
49 static bool gs_wxClipboardIsOpen
= false;
51 bool wxOpenClipboard()
53 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
56 wxWindow *win = wxTheApp->GetTopWindow();
59 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
61 if ( !gs_wxClipboardIsOpen )
63 wxLogSysError(_("Failed to open the clipboard."));
66 return gs_wxClipboardIsOpen;
70 wxLogDebug(wxT("Cannot open clipboard without a main window."));
78 bool wxCloseClipboard()
80 wxCHECK_MSG( gs_wxClipboardIsOpen
, false, wxT("clipboard is not opened") );
83 gs_wxClipboardIsOpen = false;
85 if ( ::CloseClipboard() == 0 )
87 wxLogSysError(_("Failed to close the clipboard."));
95 bool wxEmptyClipboard()
99 if ( ::EmptyClipboard() == 0 )
101 wxLogSysError(_("Failed to empty the clipboard."));
109 bool wxIsClipboardOpened()
111 return gs_wxClipboardIsOpen
;
114 bool wxIsClipboardFormatAvailable(wxDataFormat
WXUNUSED(dataFormat
))
116 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
121 #if wxUSE_DRAG_AND_DROP
122 static bool wxSetClipboardData(wxDataObject
*data
)
126 size_t size = data->GetDataSize();
127 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
130 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
131 "transfer."), size / 1024);
136 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
138 data->GetDataHere(lpGlobalMemory);
140 GlobalUnlock(hGlobal);
142 wxDataFormat format = data->GetPreferredFormat();
143 if ( !::SetClipboardData(format, hGlobal) )
145 wxLogSysError(_("Failed to set clipboard data in format %s"),
146 wxDataObject::GetFormatName(format));
153 #endif // wxUSE_DRAG_AND_DROP
156 bool wxSetClipboardData(wxDataFormat
WXUNUSED(dataFormat
),
157 const void *WXUNUSED(data
),
158 int WXUNUSED(width
), int WXUNUSED(height
))
162 HANDLE handle = 0; // return value of SetClipboardData
167 wxBitmap *bitmap = (wxBitmap *)data;
169 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
170 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
171 HBITMAP old = (HBITMAP)
172 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
173 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
175 bitmap->GetHeight());
178 SelectObject(hdcSrc, old);
184 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
185 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
186 hdcSrc, 0, 0, SRCCOPY);
188 // Select new bitmap out of memory DC
189 SelectObject(hdcMem, old1);
192 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
195 SelectObject(hdcSrc, old);
203 #if wxUSE_IMAGE_LOADING_IN_MSW
204 wxBitmap *bitmap = (wxBitmap *)data;
205 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
206 // NULL palette means to use the system one
207 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
208 handle = SetClipboardData(CF_DIB, hDIB);
216 wxMetafile *wxMF = (wxMetafile *)data;
217 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
218 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
220 mf->mm = wxMF->GetWindowsMappingMode();
223 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
225 wxMF->SetHMETAFILE((WXHANDLE) NULL);
227 handle = SetClipboardData(CF_METAFILEPICT, data);
237 wxLogError(_("Unsupported clipboard format."));
242 dataFormat = wxDF_TEXT;
247 char *s = (char *)data;
249 width = strlen(s) + 1;
251 DWORD l = (width * height);
252 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
255 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
257 memcpy(lpGlobalMemory, s, l);
259 GlobalUnlock(hGlobalMemory);
262 handle = SetClipboardData(dataFormat, hGlobalMemory);
269 wxLogSysError(_("Failed to set clipboard data."));
277 void *wxGetClipboardData(wxDataFormat
WXUNUSED(dataFormat
), long *WXUNUSED(len
))
279 // void *retval = NULL;
282 switch ( dataFormat )
287 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
291 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
292 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
294 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
295 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
297 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
301 SelectObject(hdcSrc, old);
307 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
308 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
309 hdcSrc, 0, 0, SRCCOPY);
311 // Select new bitmap out of memory DC
312 SelectObject(hdcMem, old1);
315 SelectObject(hdcSrc, old);
319 // Create and return a new wxBitmap
320 wxBitmap *wxBM = new wxBitmap;
321 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
322 wxBM->SetWidth(bm.bmWidth);
323 wxBM->SetHeight(bm.bmHeight);
324 wxBM->SetDepth(bm.bmPlanes);
337 wxLogError(_("Unsupported clipboard format."));
342 dataFormat = wxDF_TEXT;
347 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
351 DWORD hsize = ::GlobalSize(hGlobalMemory);
355 char *s = new char[hsize];
359 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
361 memcpy(s, lpGlobalMemory, hsize);
363 ::GlobalUnlock(hGlobalMemory);
371 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
372 if ( !hGlobalMemory )
375 DWORD size = ::GlobalSize(hGlobalMemory);
379 void *buf = malloc(size);
383 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
385 memcpy(buf, lpGlobalMemory, size);
387 ::GlobalUnlock(hGlobalMemory);
396 wxLogSysError(_("Failed to retrieve data from the clipboard."));
404 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
406 // TODO: return ::EnumClipboardFormats(dataFormat);
410 int wxRegisterClipboardFormat(wxChar
*WXUNUSED(formatName
))
412 // TODO: return ::RegisterClipboardFormat(formatName);
416 bool wxGetClipboardFormatName(wxDataFormat
WXUNUSED(dataFormat
),
417 wxChar
*WXUNUSED(formatName
),
418 int WXUNUSED(maxCount
))
420 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
424 // ---------------------------------------------------------------------------
426 // ---------------------------------------------------------------------------
428 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
430 wxClipboard::wxClipboard()
434 wxClipboard::~wxClipboard()
439 void wxClipboard::Clear()
443 bool wxClipboard::Flush()
449 bool wxClipboard::Open()
451 return wxOpenClipboard();
454 bool wxClipboard::IsOpened() const
456 return wxIsClipboardOpened();
459 bool wxClipboard::SetData( wxDataObject
*WXUNUSED(data
) )
461 (void)wxEmptyClipboard();
465 return AddData(data);
472 bool wxClipboard::AddData( wxDataObject
*data
)
474 wxCHECK_MSG( data
, false, wxT("data is invalid") );
476 #if wxUSE_DRAG_AND_DROP
477 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
479 // wxDataFormat format = data->GetPreferredFormat();
487 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
488 wxString str(textDataObject->GetText());
489 return wxSetClipboardData(format, str.c_str());
495 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
496 wxBitmap bitmap(bitmapDataObject->GetBitmap());
497 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
503 wxMetafileDataObject* metaFileDataObject =
504 (wxMetafileDataObject*) data;
505 wxMetafile metaFile = metaFileDataObject->GetMetafile();
506 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
507 metaFileDataObject->GetWidth(),
508 metaFileDataObject->GetHeight());
510 #endif // wxUSE_METAFILE
513 return wxSetClipboardData(data);
515 #else // !wxUSE_DRAG_AND_DROP
520 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
523 void wxClipboard::Close()
528 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
530 return wxIsClipboardFormatAvailable(format
);
533 bool wxClipboard::GetData( wxDataObject
& WXUNUSED(data
) )
535 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
537 #if wxUSE_DRAG_AND_DROP
538 // wxDataFormat format = data.GetPreferredFormat();
546 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
547 char* s = (char*) wxGetClipboardData(format);
550 textDataObject.SetText(s);
561 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
562 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetPreferredFormat());
565 bitmapDataObject.SetBitmap(* bitmap);
575 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
576 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
579 metaFileDataObject.SetMetafile(*metaFile);
590 void *buf = wxGetClipboardData(format, &len);
593 // FIXME this is for testing only!
594 ((wxPrivateDataObject &)data).SetData(buf, len);
611 #endif // wxUSE_CLIPBOARD