]>
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
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/clipbrd.h"
20 #include "wx/object.h"
24 #include "wx/bitmap.h"
28 #include "wx/dataobj.h"
32 #include "wx/metafile.h"
37 #include "wx/os2/private.h"
39 // wxDataObject is tied to OLE/drag and drop implementation,
40 // therefore so is wxClipboard :-(
42 // ===========================================================================
44 // ===========================================================================
46 // ---------------------------------------------------------------------------
47 // old-style clipboard functions using Windows API
48 // ---------------------------------------------------------------------------
50 static bool gs_wxClipboardIsOpen
= false;
52 bool wxOpenClipboard()
54 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
57 wxWindow *win = wxTheApp->GetTopWindow();
60 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
62 if ( !gs_wxClipboardIsOpen )
63 wxLogSysError(_("Failed to open the clipboard."));
65 return gs_wxClipboardIsOpen;
69 wxLogDebug(wxT("Can not open clipboard without a main window."));
77 bool wxCloseClipboard()
79 wxCHECK_MSG( gs_wxClipboardIsOpen
, false, wxT("clipboard is not opened") );
82 gs_wxClipboardIsOpen = false;
84 if ( ::CloseClipboard() == 0 )
86 wxLogSysError(_("Failed to close the clipboard."));
94 bool wxEmptyClipboard()
98 if ( ::EmptyClipboard() == 0 )
100 wxLogSysError(_("Failed to empty the clipboard."));
108 bool wxIsClipboardOpened()
110 return gs_wxClipboardIsOpen
;
113 bool wxIsClipboardFormatAvailable(wxDataFormat
WXUNUSED(dataFormat
))
115 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
120 #if wxUSE_DRAG_AND_DROP
121 static bool wxSetClipboardData(wxDataObject
*data
)
125 size_t size = data->GetDataSize();
126 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
129 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
130 "transfer."), size / 1024);
135 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
137 data->GetDataHere(lpGlobalMemory);
139 GlobalUnlock(hGlobal);
141 wxDataFormat format = data->GetPreferredFormat();
142 if ( !::SetClipboardData(format, hGlobal) )
144 wxLogSysError(_("Failed to set clipboard data in format %s"),
145 wxDataObject::GetFormatName(format));
152 #endif // wxUSE_DRAG_AND_DROP
155 bool wxSetClipboardData(wxDataFormat
WXUNUSED(dataFormat
),
156 const void *WXUNUSED(data
),
157 int WXUNUSED(width
), int WXUNUSED(height
))
161 HANDLE handle = 0; // return value of SetClipboardData
166 wxBitmap *bitmap = (wxBitmap *)data;
168 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
169 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
170 HBITMAP old = (HBITMAP)
171 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
172 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
174 bitmap->GetHeight());
177 SelectObject(hdcSrc, old);
183 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
184 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
185 hdcSrc, 0, 0, SRCCOPY);
187 // Select new bitmap out of memory DC
188 SelectObject(hdcMem, old1);
191 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
194 SelectObject(hdcSrc, old);
202 #if wxUSE_IMAGE_LOADING_IN_MSW
203 wxBitmap *bitmap = (wxBitmap *)data;
204 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
205 // NULL palette means to use the system one
206 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
207 handle = SetClipboardData(CF_DIB, hDIB);
215 wxMetafile *wxMF = (wxMetafile *)data;
216 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
217 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
219 mf->mm = wxMF->GetWindowsMappingMode();
222 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
224 wxMF->SetHMETAFILE((WXHANDLE) NULL);
226 handle = SetClipboardData(CF_METAFILEPICT, data);
236 wxLogError(_("Unsupported clipboard format."));
241 dataFormat = wxDF_TEXT;
246 char *s = (char *)data;
248 width = strlen(s) + 1;
250 DWORD l = (width * height);
251 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
254 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
256 memcpy(lpGlobalMemory, s, l);
258 GlobalUnlock(hGlobalMemory);
261 handle = SetClipboardData(dataFormat, hGlobalMemory);
268 wxLogSysError(_("Failed to set clipboard data."));
276 void *wxGetClipboardData(wxDataFormat
WXUNUSED(dataFormat
), long *WXUNUSED(len
))
278 // void *retval = NULL;
281 switch ( dataFormat )
286 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
290 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
291 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
293 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
294 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
296 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
300 SelectObject(hdcSrc, old);
306 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
307 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
308 hdcSrc, 0, 0, SRCCOPY);
310 // Select new bitmap out of memory DC
311 SelectObject(hdcMem, old1);
314 SelectObject(hdcSrc, old);
318 // Create and return a new wxBitmap
319 wxBitmap *wxBM = new wxBitmap;
320 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
321 wxBM->SetWidth(bm.bmWidth);
322 wxBM->SetHeight(bm.bmHeight);
323 wxBM->SetDepth(bm.bmPlanes);
336 wxLogError(_("Unsupported clipboard format."));
341 dataFormat = wxDF_TEXT;
346 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
350 DWORD hsize = ::GlobalSize(hGlobalMemory);
354 char *s = new char[hsize];
358 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
360 memcpy(s, lpGlobalMemory, hsize);
362 ::GlobalUnlock(hGlobalMemory);
370 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
371 if ( !hGlobalMemory )
374 DWORD size = ::GlobalSize(hGlobalMemory);
378 void *buf = malloc(size);
382 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
384 memcpy(buf, lpGlobalMemory, size);
386 ::GlobalUnlock(hGlobalMemory);
395 wxLogSysError(_("Failed to retrieve data from the clipboard."));
403 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
405 // TODO: return ::EnumClipboardFormats(dataFormat);
409 int wxRegisterClipboardFormat(wxChar
*WXUNUSED(formatName
))
411 // TODO: return ::RegisterClipboardFormat(formatName);
415 bool wxGetClipboardFormatName(wxDataFormat
WXUNUSED(dataFormat
),
416 wxChar
*WXUNUSED(formatName
),
417 int WXUNUSED(maxCount
))
419 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
423 // ---------------------------------------------------------------------------
425 // ---------------------------------------------------------------------------
427 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
429 wxClipboard::wxClipboard()
433 wxClipboard::~wxClipboard()
438 void wxClipboard::Clear()
442 bool wxClipboard::Flush()
448 bool wxClipboard::Open()
450 return wxOpenClipboard();
453 bool wxClipboard::IsOpened() const
455 return wxIsClipboardOpened();
458 bool wxClipboard::SetData( wxDataObject
*WXUNUSED(data
) )
460 (void)wxEmptyClipboard();
464 return AddData(data);
471 bool wxClipboard::AddData( wxDataObject
*data
)
473 wxCHECK_MSG( data
, false, wxT("data is invalid") );
475 #if wxUSE_DRAG_AND_DROP
476 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
478 // wxDataFormat format = data->GetPreferredFormat();
486 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
487 wxString str(textDataObject->GetText());
488 return wxSetClipboardData(format, str.c_str());
494 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
495 wxBitmap bitmap(bitmapDataObject->GetBitmap());
496 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
502 wxMetafileDataObject* metaFileDataObject =
503 (wxMetafileDataObject*) data;
504 wxMetafile metaFile = metaFileDataObject->GetMetafile();
505 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
506 metaFileDataObject->GetWidth(),
507 metaFileDataObject->GetHeight());
509 #endif // wxUSE_METAFILE
512 return wxSetClipboardData(data);
514 #else // !wxUSE_DRAG_AND_DROP
519 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
522 void wxClipboard::Close()
527 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
529 return wxIsClipboardFormatAvailable(format
);
532 bool wxClipboard::GetData( wxDataObject
& WXUNUSED(data
) )
534 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
536 #if wxUSE_DRAG_AND_DROP
537 // wxDataFormat format = data.GetPreferredFormat();
545 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
546 char* s = (char*) wxGetClipboardData(format);
549 textDataObject.SetText(s);
560 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
561 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetPreferredFormat());
564 bitmapDataObject.SetBitmap(* bitmap);
574 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
575 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
578 metaFileDataObject.SetMetafile(*metaFile);
589 void *buf = wxGetClipboardData(format, &len);
592 // FIXME this is for testing only!
593 ((wxPrivateDataObject &)data).SetData(buf, len);
610 #endif // wxUSE_CLIPBOARD