]>
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"
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 :-(
40 #if wxUSE_DRAG_AND_DROP
41 #include "wx/dataobj.h"
43 // static bool wxSetClipboardData(wxDataObject *data);
46 // ===========================================================================
48 // ===========================================================================
50 // ---------------------------------------------------------------------------
51 // old-style clipboard functions using Windows API
52 // ---------------------------------------------------------------------------
54 static bool gs_wxClipboardIsOpen
= false;
56 bool wxOpenClipboard()
58 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
61 wxWindow *win = wxTheApp->GetTopWindow();
64 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
66 if ( !gs_wxClipboardIsOpen )
67 wxLogSysError(_("Failed to open the clipboard."));
69 return gs_wxClipboardIsOpen;
73 wxLogDebug(wxT("Can not open clipboard without a main window."));
81 bool wxCloseClipboard()
83 wxCHECK_MSG( gs_wxClipboardIsOpen
, false, wxT("clipboard is not opened") );
86 gs_wxClipboardIsOpen = false;
88 if ( ::CloseClipboard() == 0 )
90 wxLogSysError(_("Failed to close the clipboard."));
98 bool wxEmptyClipboard()
102 if ( ::EmptyClipboard() == 0 )
104 wxLogSysError(_("Failed to empty the clipboard."));
112 bool wxIsClipboardOpened()
114 return gs_wxClipboardIsOpen
;
117 bool wxIsClipboardFormatAvailable(wxDataFormat
WXUNUSED(dataFormat
))
119 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
124 #if wxUSE_DRAG_AND_DROP
125 static bool wxSetClipboardData(wxDataObject
*data
)
129 size_t size = data->GetDataSize();
130 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
133 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
134 "transfer."), size / 1024);
139 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
141 data->GetDataHere(lpGlobalMemory);
143 GlobalUnlock(hGlobal);
145 wxDataFormat format = data->GetPreferredFormat();
146 if ( !::SetClipboardData(format, hGlobal) )
148 wxLogSysError(_("Failed to set clipboard data in format %s"),
149 wxDataObject::GetFormatName(format));
156 #endif // wxUSE_DRAG_AND_DROP
159 bool wxSetClipboardData(wxDataFormat
WXUNUSED(dataFormat
),
160 const void *WXUNUSED(data
),
161 int WXUNUSED(width
), int WXUNUSED(height
))
165 HANDLE handle = 0; // return value of SetClipboardData
170 wxBitmap *bitmap = (wxBitmap *)data;
172 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
173 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
174 HBITMAP old = (HBITMAP)
175 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
176 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
178 bitmap->GetHeight());
181 SelectObject(hdcSrc, old);
187 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
188 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
189 hdcSrc, 0, 0, SRCCOPY);
191 // Select new bitmap out of memory DC
192 SelectObject(hdcMem, old1);
195 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
198 SelectObject(hdcSrc, old);
206 #if wxUSE_IMAGE_LOADING_IN_MSW
207 wxBitmap *bitmap = (wxBitmap *)data;
208 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
209 // NULL palette means to use the system one
210 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
211 handle = SetClipboardData(CF_DIB, hDIB);
219 wxMetafile *wxMF = (wxMetafile *)data;
220 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
221 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
223 mf->mm = wxMF->GetWindowsMappingMode();
226 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
228 wxMF->SetHMETAFILE((WXHANDLE) NULL);
230 handle = SetClipboardData(CF_METAFILEPICT, data);
240 wxLogError(_("Unsupported clipboard format."));
245 dataFormat = wxDF_TEXT;
250 char *s = (char *)data;
252 width = strlen(s) + 1;
254 DWORD l = (width * height);
255 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
258 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
260 memcpy(lpGlobalMemory, s, l);
262 GlobalUnlock(hGlobalMemory);
265 handle = SetClipboardData(dataFormat, hGlobalMemory);
272 wxLogSysError(_("Failed to set clipboard data."));
280 void *wxGetClipboardData(wxDataFormat
WXUNUSED(dataFormat
), long *WXUNUSED(len
))
282 // void *retval = NULL;
285 switch ( dataFormat )
290 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
294 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
295 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
297 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
298 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
300 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
304 SelectObject(hdcSrc, old);
310 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
311 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
312 hdcSrc, 0, 0, SRCCOPY);
314 // Select new bitmap out of memory DC
315 SelectObject(hdcMem, old1);
318 SelectObject(hdcSrc, old);
322 // Create and return a new wxBitmap
323 wxBitmap *wxBM = new wxBitmap;
324 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
325 wxBM->SetWidth(bm.bmWidth);
326 wxBM->SetHeight(bm.bmHeight);
327 wxBM->SetDepth(bm.bmPlanes);
340 wxLogError(_("Unsupported clipboard format."));
345 dataFormat = wxDF_TEXT;
350 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
354 DWORD hsize = ::GlobalSize(hGlobalMemory);
358 char *s = new char[hsize];
362 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
364 memcpy(s, lpGlobalMemory, hsize);
366 ::GlobalUnlock(hGlobalMemory);
374 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
375 if ( !hGlobalMemory )
378 DWORD size = ::GlobalSize(hGlobalMemory);
382 void *buf = malloc(size);
386 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
388 memcpy(buf, lpGlobalMemory, size);
390 ::GlobalUnlock(hGlobalMemory);
399 wxLogSysError(_("Failed to retrieve data from the clipboard."));
407 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
409 // TODO: return ::EnumClipboardFormats(dataFormat);
413 int wxRegisterClipboardFormat(wxChar
*WXUNUSED(formatName
))
415 // TODO: return ::RegisterClipboardFormat(formatName);
419 bool wxGetClipboardFormatName(wxDataFormat
WXUNUSED(dataFormat
),
420 wxChar
*WXUNUSED(formatName
),
421 int WXUNUSED(maxCount
))
423 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
427 // ---------------------------------------------------------------------------
429 // ---------------------------------------------------------------------------
431 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
433 wxClipboard::wxClipboard()
437 wxClipboard::~wxClipboard()
442 void wxClipboard::Clear()
446 bool wxClipboard::Flush()
452 bool wxClipboard::Open()
454 return wxOpenClipboard();
457 bool wxClipboard::IsOpened() const
459 return wxIsClipboardOpened();
462 bool wxClipboard::SetData( wxDataObject
*WXUNUSED(data
) )
464 (void)wxEmptyClipboard();
468 return AddData(data);
475 bool wxClipboard::AddData( wxDataObject
*data
)
477 wxCHECK_MSG( data
, false, wxT("data is invalid") );
479 #if wxUSE_DRAG_AND_DROP
480 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
482 // wxDataFormat format = data->GetPreferredFormat();
490 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
491 wxString str(textDataObject->GetText());
492 return wxSetClipboardData(format, str.c_str());
498 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
499 wxBitmap bitmap(bitmapDataObject->GetBitmap());
500 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
506 wxMetafileDataObject* metaFileDataObject =
507 (wxMetafileDataObject*) data;
508 wxMetafile metaFile = metaFileDataObject->GetMetafile();
509 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
510 metaFileDataObject->GetWidth(),
511 metaFileDataObject->GetHeight());
513 #endif // wxUSE_METAFILE
516 return wxSetClipboardData(data);
518 #else // !wxUSE_DRAG_AND_DROP
523 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
526 void wxClipboard::Close()
531 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
533 return wxIsClipboardFormatAvailable(format
);
536 bool wxClipboard::GetData( wxDataObject
& WXUNUSED(data
) )
538 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
540 #if wxUSE_DRAG_AND_DROP
541 // wxDataFormat format = data.GetPreferredFormat();
549 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
550 char* s = (char*) wxGetClipboardData(format);
553 textDataObject.SetText(s);
564 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
565 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetPreferredFormat());
568 bitmapDataObject.SetBitmap(* bitmap);
578 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
579 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
582 metaFileDataObject.SetMetafile(*metaFile);
593 void *buf = wxGetClipboardData(format, &len);
596 // FIXME this is for testing only!
597 ((wxPrivateDataObject &)data).SetData(buf, len);
614 #endif // wxUSE_CLIPBOARD