]>
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
* wxTheClipboard
= (wxClipboard
*)NULL
;
441 wxClipboard::wxClipboard()
445 wxClipboard::~wxClipboard()
450 void wxClipboard::Clear()
454 bool wxClipboard::Open()
456 return wxOpenClipboard();
459 bool wxClipboard::SetData( wxDataObject
*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->GetFormat();
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->GetFormat(), &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
518 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
521 void wxClipboard::Close()
526 bool wxClipboard::IsSupported( wxDataFormat format
)
528 return wxIsClipboardFormatAvailable(format
);
531 bool wxClipboard::GetData( wxDataObject
*data
)
533 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
535 #if wxUSE_DRAG_AND_DROP
536 // wxDataFormat format = data->GetFormat();
544 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
545 char* s = (char*) wxGetClipboardData(format);
548 textDataObject->SetText(s);
559 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject *)data;
560 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat());
563 bitmapDataObject->SetBitmap(* bitmap);
573 wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject *)data;
574 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
577 metaFileDataObject->SetMetafile(*metaFile);
588 void *buf = wxGetClipboardData(format, &len);
591 // FIXME this is for testing only!!
592 ((wxPrivateDataObject *)data)->SetData(buf, len);
607 //-----------------------------------------------------------------------------
609 //-----------------------------------------------------------------------------
611 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
613 bool wxClipboardModule::OnInit()
615 wxTheClipboard
= new wxClipboard();
620 void wxClipboardModule::OnExit()
622 if (wxTheClipboard
) delete wxTheClipboard
;
623 wxTheClipboard
= (wxClipboard
*) NULL
;
627 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
628 #endif // wxUSE_CLIPBOARD